scripts: support -I <dir> -L <dir> and -l <lib> for cl in compile
authorPeter Rosin <peda@lysator.liu.se>
Tue, 6 Mar 2012 08:14:50 +0000 (09:14 +0100)
committerPeter Rosin <peda@lysator.liu.se>
Tue, 6 Mar 2012 08:14:50 +0000 (09:14 +0100)
POSIX mandates that the compiler accepts a space between the -I,
-l and -L options and their respective arguments.  See
http://pubs.opengroup.org/onlinepubs/000095399/utilities/c99.html

* lib/compile (func_cl_dashl): New function with factored out code
for implementing the -l option for the cl wrapper.
(func_cl_dashL): New function with factored out code implementing
the -L option for the cl wrapper.
(func_cl_wrapper): Use func_cl_dashl to implement both -l <lib>
and -l<lib>, and func_cl_dashL to implement both -L <dir> and
-L<dir>.  Also add support for -I <dir>.
(scriptversion): Update.
* tests/compile3.test: Test both with and without a space between
-I, -l and -L and their respective arguments.
* tests/compile5.test: Likewise.
* tests/compile6.test: Likewise.
* tests/compile3.test: Likewise.
* NEWS: Update.

NEWS
lib/compile
tests/compile3.test
tests/compile4.test
tests/compile5.test
tests/compile6.test

diff --git a/NEWS b/NEWS
index 442b303..c7d9c94 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -115,6 +115,10 @@ New in 1.11.0a:
     action is now a synonym for "r" (replace).  Also, the script has been
     ignoring the "v" (verbose) modifier already since Automake 1.11.3.
 
+  - When the 'compile' script is used to wrap MSVC, it now accepts an
+    optional space between the -I, -L and -l options and their respective
+    arguments, for better POSIX compliance.
+
 Bugs fixed in 1.11.0a:
 
 * Bugs introduced by 1.11.2:
index b1f4749..862a14e 100755 (executable)
@@ -1,7 +1,7 @@
 #! /bin/sh
 # Wrapper for compilers which do not understand '-c -o'.
 
-scriptversion=2012-01-04.17; # UTC
+scriptversion=2012-03-05.13; # UTC
 
 # Copyright (C) 1999, 2000, 2003, 2004, 2005, 2009, 2010, 2012 Free
 # Software Foundation, Inc.
@@ -79,6 +79,48 @@ func_file_conv ()
   esac
 }
 
+# func_cl_dashL linkdir
+# Make cl look for libraries in LINKDIR
+func_cl_dashL ()
+{
+  func_file_conv "$1"
+  if test -z "$lib_path"; then
+    lib_path=$file
+  else
+    lib_path="$lib_path;$file"
+  fi
+  linker_opts="$linker_opts -LIBPATH:$file"
+}
+
+# func_cl_dashl library
+# Do a library search-path lookup for cl
+func_cl_dashl ()
+{
+  lib=$1
+  found=no
+  save_IFS=$IFS
+  IFS=';'
+  for dir in $lib_path $LIB
+  do
+    IFS=$save_IFS
+    if $shared && test -f "$dir/$lib.dll.lib"; then
+      found=yes
+      lib=$dir/$lib.dll.lib
+      break
+    fi
+    if test -f "$dir/$lib.lib"; then
+      found=yes
+      lib=$dir/$lib.lib
+      break
+    fi
+  done
+  IFS=$save_IFS
+
+  if test "$found" != yes; then
+    lib=$lib.lib
+  fi
+}
+
 # func_cl_wrapper cl arg...
 # Adjust compile command to suit cl
 func_cl_wrapper ()
@@ -109,43 +151,34 @@ func_cl_wrapper ()
              ;;
          esac
          ;;
+       -I)
+         eat=1
+         func_file_conv "$2" mingw
+         set x "$@" -I"$file"
+         shift
+         ;;
        -I*)
          func_file_conv "${1#-I}" mingw
          set x "$@" -I"$file"
          shift
          ;;
+       -l)
+         eat=1
+         func_cl_dashl "$2"
+         set x "$@" "$lib"
+         shift
+         ;;
        -l*)
-         lib=${1#-l}
-         found=no
-         save_IFS=$IFS
-         IFS=';'
-         for dir in $lib_path $LIB
-         do
-           IFS=$save_IFS
-           if $shared && test -f "$dir/$lib.dll.lib"; then
-             found=yes
-             set x "$@" "$dir/$lib.dll.lib"
-             break
-           fi
-           if test -f "$dir/$lib.lib"; then
-             found=yes
-             set x "$@" "$dir/$lib.lib"
-             break
-           fi
-         done
-         IFS=$save_IFS
-
-         test "$found" != yes && set x "$@" "$lib.lib"
+         func_cl_dashl "${1#-l}"
+         set x "$@" "$lib"
          shift
          ;;
+       -L)
+         eat=1
+         func_cl_dashL "$2"
+         ;;
        -L*)
-         func_file_conv "${1#-L}"
-         if test -z "$lib_path"; then
-           lib_path=$file
-         else
-           lib_path="$lib_path;$file"
-         fi
-         linker_opts="$linker_opts -LIBPATH:$file"
+         func_cl_dashL "${1#-L}"
          ;;
        -static)
          shared=false
index 15064a6..5f2df7d 100755 (executable)
@@ -31,24 +31,29 @@ END
 
 chmod +x ./cl
 
-# Check if compile handles "-o foo", -I, -l, -L, -Xlinker -Wl,
-opts=`LIB= ./compile ./cl foo.c -o foo -lbar -Lgazonk -Ibaz -Xlinker foobar -Wl,-foo,bar`
-test x"$opts" = x"foo.c -Fefoo bar.lib -Ibaz -link -LIBPATH:gazonk foobar -foo bar"
-
-# Check if compile handles "-o foo.obj"
-opts=`./compile ./cl -c foo.c -o foo.obj -Ibaz`
-test x"$opts" = x"-c foo.c -Fofoo.obj -Ibaz"
-
-# Check if compile handles "-o foo.o"
-opts=`./compile ./cl -c foo.c -o foo.o -Ibaz`
-test x"$opts" = x"-c foo.c -Fofoo.o -Ibaz"
-
-# Check if compile handles "foo.cc" as C++.
-opts=`./compile ./cl -c foo.cc -o foo.o -Ibaz`
-test x"$opts" = x"-c -Tpfoo.cc -Fofoo.o -Ibaz"
-
-# Check if compile clears the "eat" variable properly.
-opts=`eat=1 ./compile ./cl -c foo.c -o foo.obj -Ibaz`
-test x"$opts" = x"-c foo.c -Fofoo.obj -Ibaz"
+# POSIX mandates that the compiler accepts a space between the -I,
+# -l and -L options and their respective arguments.  Traditionally,
+# this should work also without a space.  Try both usages.
+for sp in '' ' '; do
+  # Check if compile handles "-o foo", -I, -l, -L, -Xlinker -Wl,
+  opts=`LIB= ./compile ./cl foo.c -o foo -l${sp}bar -L${sp}gazonk -I${sp}baz -Xlinker foobar -Wl,-foo,bar`
+  test x"$opts" = x"foo.c -Fefoo bar.lib -Ibaz -link -LIBPATH:gazonk foobar -foo bar"
+
+  # Check if compile handles "-o foo.obj"
+  opts=`./compile ./cl -c foo.c -o foo.obj -I${sp}baz`
+  test x"$opts" = x"-c foo.c -Fofoo.obj -Ibaz"
+
+  # Check if compile handles "-o foo.o"
+  opts=`./compile ./cl -c foo.c -o foo.o -I${sp}baz`
+  test x"$opts" = x"-c foo.c -Fofoo.o -Ibaz"
+
+  # Check if compile handles "foo.cc" as C++.
+  opts=`./compile ./cl -c foo.cc -o foo.o -I${sp}baz`
+  test x"$opts" = x"-c -Tpfoo.cc -Fofoo.o -Ibaz"
+
+  # Check if compile clears the "eat" variable properly.
+  opts=`eat=1 ./compile ./cl -c foo.c -o foo.obj -I${sp}baz`
+  test x"$opts" = x"-c foo.c -Fofoo.obj -Ibaz"
+done
 
 :
index 0b3e981..bac8a54 100755 (executable)
@@ -79,8 +79,15 @@ if test -f sub/libfoo.a; then
   cp sub/libfoo.a sub/foo.lib
 fi
 
-./compile cl $CFLAGS $LDFLAGS -L"$absfoodir" "$absmainobj" -o main -lfoo
+# POSIX mandates that the compiler accepts a space between the -I,
+# -l and -L options and their respective arguments.  Traditionally,
+# this should work also without a space.  Try both usages.
+for sp in '' ' '; do
+  rm -f main
 
-./main
+  ./compile cl $CFLAGS $LDFLAGS -L${sp}"$absfoodir" "$absmainobj" -o main -l${sp}foo
+
+  ./main
+done
 
 :
index cd1468f..e80a76f 100755 (executable)
@@ -65,17 +65,22 @@ $AUTOMAKE -a
 
 pwd=`pwd`
 
-# Check if "compile cl" transforms absolute file names to
-# host format (e.g /somewhere -> c:/msys/1.0/somewhere).
+# POSIX mandates that the compiler accepts a space between the -I,
+# -l and -L options and their respective arguments.  Traditionally,
+# this should work also without a space.  Try both usages.
+for sp in '' ' '; do
+  # Check if "compile cl" transforms absolute file names to
+  # host format (e.g /somewhere -> c:/msys/1.0/somewhere).
 
-res=`./compile ./cl -L"$pwd" | sed -e 's/-link -LIBPATH://'`
+  res=`./compile ./cl -L${sp}"$pwd" | sed -e 's/-link -LIBPATH://'`
 
-case $res in
-  ?:[\\/]*)
-    ;;
-  *)
-    Exit 1
-    ;;
-esac
+  case $res in
+    ?:[\\/]*)
+      ;;
+    *)
+      Exit 1
+      ;;
+  esac
+done
 
 :
index f45a534..c2eadc0 100755 (executable)
@@ -31,69 +31,76 @@ END
 
 chmod +x ./cl
 
-mkdir syslib
-:> syslib/foo.lib
+# POSIX mandates that the compiler accepts a space between the -I,
+# -l and -L options and their respective arguments.  Traditionally,
+# this should work also without a space.  Try both usages.
+for sp in '' ' '; do
+  rm -rf lib lib2 syslib "sys  lib2"
 
-syslib=`pwd`/syslib
-LIB=$syslib
-export LIB
+  mkdir syslib
+  :> syslib/foo.lib
 
-mkdir lib
-:> lib/bar.lib
-:> lib/bar.dll.lib
+  syslib=`pwd`/syslib
+  LIB=$syslib
+  export LIB
 
-# Check if compile library search correctly
-opts=`./compile ./cl foo.c -o foo -Llib -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib"
+  mkdir lib
+  :> lib/bar.lib
+  :> lib/bar.dll.lib
 
-# Check if -static makes compile avoid bar.dll.lib
-opts=`./compile ./cl foo.c -o foo -Llib -static -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo lib/bar.lib $syslib/foo.lib -link -LIBPATH:lib"
+  # Check if compile library search correctly
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib"
 
-:> syslib/bar.lib
-:> syslib/bar.dll.lib
+  # Check if -static makes compile avoid bar.dll.lib
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib -static -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo lib/bar.lib $syslib/foo.lib -link -LIBPATH:lib"
 
-# Check if compile finds bar.dll.lib in syslib
-opts=`./compile ./cl foo.c -o foo -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo $syslib/bar.dll.lib $syslib/foo.lib"
+  :> syslib/bar.lib
+  :> syslib/bar.dll.lib
 
-# Check if compile prefers -L over $LIB
-opts=`./compile ./cl foo.c -o foo -Llib -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib"
+  # Check if compile finds bar.dll.lib in syslib
+  opts=`./compile ./cl foo.c -o foo -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo $syslib/bar.dll.lib $syslib/foo.lib"
 
-mkdir lib2
-:> lib2/bar.dll.lib
+  # Check if compile prefers -L over $LIB
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib"
 
-# Check if compile avoids bar.dll.lib in lib2 when -static
-opts=`./compile ./cl foo.c -o foo -Llib2 -static -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo $syslib/bar.lib $syslib/foo.lib -link -LIBPATH:lib2"
+  mkdir lib2
+  :> lib2/bar.dll.lib
 
-# Check if compile gets two different bar libraries when -static
-# is added in the middle
-opts=`./compile ./cl foo.c -o foo -Llib2 -Llib -lbar -static -lbar`
-test x"$opts" = x"foo.c -Fefoo lib2/bar.dll.lib lib/bar.lib -link -LIBPATH:lib2 -LIBPATH:lib"
+  # Check if compile avoids bar.dll.lib in lib2 when -static
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib2 -static -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo $syslib/bar.lib $syslib/foo.lib -link -LIBPATH:lib2"
 
-# Check if compile gets the correct bar.dll.lib
-opts=`./compile ./cl foo.c -o foo -Llib -Llib2 -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib -LIBPATH:lib2"
+  # Check if compile gets two different bar libraries when -static
+  # is added in the middle
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib2 -L${sp}lib -l${sp}bar -static -l${sp}bar`
+  test x"$opts" = x"foo.c -Fefoo lib2/bar.dll.lib lib/bar.lib -link -LIBPATH:lib2 -LIBPATH:lib"
 
-# Check if compile gets the correct bar.dll.lib
-opts=`./compile ./cl foo.c -o foo -Llib2 -Llib -lbar -lfoo`
-test x"$opts" = x"foo.c -Fefoo lib2/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib2 -LIBPATH:lib"
+  # Check if compile gets the correct bar.dll.lib
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib -L${sp}lib2 -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo lib/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib -LIBPATH:lib2"
 
-mkdir "sys  lib2"
-:> "sys  lib2/foo.dll.lib"
+  # Check if compile gets the correct bar.dll.lib
+  opts=`./compile ./cl foo.c -o foo -L${sp}lib2 -L${sp}lib -l${sp}bar -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo lib2/bar.dll.lib $syslib/foo.lib -link -LIBPATH:lib2 -LIBPATH:lib"
 
-syslib2="`pwd`/sys  lib2"
-LIB="$syslib2;$LIB"
+  mkdir "sys  lib2"
+  :> "sys  lib2/foo.dll.lib"
 
-# Check if compile handles spaces in $LIB and that it prefers the order
-# in a multi-component $LIB.
-opts=`./compile ./cl foo.c -o foo -lfoo`
-test x"$opts" = x"foo.c -Fefoo $syslib2/foo.dll.lib"
+  syslib2="`pwd`/sys  lib2"
+  LIB="$syslib2;$LIB"
 
-# Check if compile handles the 2nd directory in a multi-component $LIB.
-opts=`./compile ./cl foo.c -o foo -static -lfoo`
-test x"$opts" = x"foo.c -Fefoo $syslib/foo.lib"
+  # Check if compile handles spaces in $LIB and that it prefers the order
+  # in a multi-component $LIB.
+  opts=`./compile ./cl foo.c -o foo -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo $syslib2/foo.dll.lib"
+
+  # Check if compile handles the 2nd directory in a multi-component $LIB.
+  opts=`./compile ./cl foo.c -o foo -static -l${sp}foo`
+  test x"$opts" = x"foo.c -Fefoo $syslib/foo.lib"
+done
 
 :