# file      : tests/api/testscript
# license   : ISC; see accompanying COPYING file

posix = ($c.target.class != 'windows')
sp = ' '

: double-quoted
:
{
  +cat <<EOI >=libfoo.pc
    prefix="C:\\Program   Files\\Foo"
    exec_prefix=${prefix}
    var="A\"B" 'C\'D'
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Libs: "-LC:\\Program   Files\\Foo" "-lC:\\Program   Files\\Foo\\foo"
    Cflags: "-IC:\\Program   Files\\Foo"
    EOI

  f = $~/libfoo.pc

  : cflags
  :
  $* --cflags $f >>EOO
    I C:\Program   Files\Foo
    EOO

  : libs
  :
  $* --libs $f >>EOO
    L C:\Program   Files\Foo
    l C:\Program   Files\Foo\foo
    EOO

  : vars
  :
  $* --vars $f >>EOO
    var A"B 'C\'D'
    exec_prefix C:\\Program   Files\\Foo
    prefix C:\\Program   Files\\Foo
    EOO
}

: single-quoted
:
{
  +cat <<EOI >=libfoo.pc
    prefix='C:\Program   Files\Foo'
    exec_prefix=${prefix}
    var='A\'B' "C\"D"
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Libs: '-LC:\Program   Files\Foo' '-lC:\Program   Files\Foo\foo'
    Cflags: '-IC:\Program   Files\Foo'
    EOI

  f = $~/libfoo.pc

  : cflags
  :
  $* --cflags $f >>EOO
    I C:\Program   Files\Foo
    EOO

  : libs
  :
  $* --libs $f >>EOO
    L C:\Program   Files\Foo
    l C:\Program   Files\Foo\foo
    EOO

  : vars
  :
  $* --vars $f >>EOO
    var A'B "C\"D"
    exec_prefix C:\Program   Files\Foo
    prefix C:\Program   Files\Foo
    EOO
}

: unquoted
:
{
  +cat <<EOI >=libfoo.pc
    prefix=C:\\Program\ \ \ Files\\Foo
    exec_prefix=${prefix}
    var=X A\'B' "C\"D"
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Libs: -LC:\\Program\ \ \ Files\\Foo -lC:\\Program\ \ \ Files\\Foo\\foo
    Cflags: -IC:\\Program\ \ \ Files\\Foo
    EOI

  f = $~/libfoo.pc

  : cflags
  :
  $* --cflags $f >>EOO
    I C:\Program   Files\Foo
    EOO

  : libs
  :
  $* --libs $f >>EOO
    L C:\Program   Files\Foo
    l C:\Program   Files\Foo\foo
    EOO

  : vars
  :
  $* --vars $f >>EOO
    var X A\'B' "C\"D"
    exec_prefix C:\\Program\ \ \ Files\\Foo
    prefix C:\\Program\ \ \ Files\\Foo
    EOO
}

: no-merge
:
: Test that the -framework options are not merged into a single fragment.
:
{
  +cat <<EOI >=libfoo.pc
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Libs: -L/Users/build/install/lib -lcurl
    Libs.private: -lssl -lcrypto -lz -framework CoreFoundation -framework Security
    EOI

  f = $~/libfoo.pc

  : libs
  :
  $* --libs $f >>EOO
    L /Users/build/install/lib
    l curl
    l ssl
    l crypto
    l z
      -framework
      CoreFoundation
      -framework
      Security
    EOO
}

: no-drop
:
: Test that the -framework options are not dropped.
:
{
  +cat <<EOI >=libfoo.pc
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Libs: -L/Users/build/install/lib -lcurl -framework Cocoa -framework IOKit -framework CoreFoundation
    EOI

  f = $~/libfoo.pc

  : libs
  :
  $* --libs $f >>EOO
    L /Users/build/install/lib
    l curl
      -framework
      Cocoa
      -framework
      IOKit
      -framework
      CoreFoundation
    EOO
}

: no-oversplit
:
{
  +cat <<EOI >=libfoo.pc
    Name: libfoo
    Description: Foo library
    Version: 1.0
    Cflags: -I -Ifoo
    Libs: -L -lfoo -l -Lbar
    EOI

  f = $~/libfoo.pc

  : cflags
  :
  : Test that the '-I -Ifoo' options sequence is parsed into 2 fragments: the
  : first has the type 'I' and no value and the second has no type and the
  : '-Ifoo' value.
  :
  $* --cflags $f >>"EOO"
    I$sp
      -Ifoo
    EOO

  : libs
  :
  : Similar to the above but for the -L and -l options.
  :
  $* --libs $f >>"EOO"
    L$sp
      -lfoo
    l$sp
      -Lbar
    EOO
}

: long-var
:
: Test long variable name and value.
:
{
  a='abcdefghijklmnopqrstuvwxyz';

  # ~0,5K
  #
  n = "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a";

  # ~64K
  #
  v = '1234567890abcdefghijklmnopqrstuvwxyz ';
  v = "$v$v$v$v$v$v$v$v$v$v$v$v$v";
  v = "$v$v$v$v$v$v$v$v$v$v$v$v";
  v = "$v$v$v$v$v$v$v$v$v$v$v$n$n";

  # >64K
  #
  V = "$v$v$v";

  cat <<"EOI" >=libfoo.pc;
    $n="$v"
    var=\${$n}
    $n$n="$V"
    Name: libfoo
    Description: Foo library
    Version: 1.0
    EOI

  f = $~/libfoo.pc;

  $* --vars $f >>"EOO"
    $n$n $V
    var $v
    $n $v
    EOO
}

: pcfiledir-var
:
{
  # Generate the .pc file with the platform-specific directory separators.
  #
  s = ($posix ? '/' : '\\')

  +cat <<"EOI" >=libfoo.pc
    Name: libfoo
    Description: Foo library
    Version: 1.0
    inc=\${pcfiledir}$(s)..$(s)..$(s)include
    Libs: -L\${pcfiledir}$(s)..
    Cflags: -I\${inc}
    EOI

  f = [path] $~/libfoo.pc

  : no-escape
  :
  {
    : libs
    :
    $* --libs $f >>/"EOO"
      L $directory($f)/..
      EOO

    : cflags
    :
    $* --cflags $f >>/"EOO"
      I $directory($f)/../../include
      EOO
  }

  : escape
  :
  {
    d = [dir_path] ($posix ? 'a "b c\d'"'" : 'a b c')
    +mkdir -p $d

    f2 = [path] $~/$d/libfoo.pc
    +cp $f $f2

    : libs
    :
    $* --libs $f2 >>/"EOO"
      L $directory($f2)/..
      EOO

    : cflags
    :
    $* --cflags $f2 >>/"EOO"
      I $directory($f2)/../../include
      EOO
  }
}
