Various compatibility fixes and improvements to msvcc.sh.
authorRyan VanderMeulen <ryanvm@gmail.com>
Tue, 18 Mar 2014 16:09:45 +0000 (12:09 -0400)
committerRyan VanderMeulen <ryanvm@gmail.com>
Tue, 18 Mar 2014 16:09:45 +0000 (12:09 -0400)
* Don't try to mix incompatible optimization flags in debug builds.
* Workaround ax_cc_maxopt.m4 not supporting MSVC and change -O3 to -O2.
* Fix MSVC warning by properly passing linker flags to compiler.
* Make msvcc.sh return 1 if invalid command line options are used rather than silently eating them.
* Add more comments.

msvcc.sh

index dcdbeab..7440deb 100755 (executable)
--- a/msvcc.sh
+++ b/msvcc.sh
@@ -42,6 +42,7 @@
 # format and translated into something sensible for cl or ml.
 #
 
+args_orig=$@
 args="-nologo -W3"
 md=-MD
 cl="cl"
@@ -72,14 +73,35 @@ do
       shift 1
     ;;
     -O*)
-      # If we're optimizing, make sure we explicitly turn on some optimizations
-      # that are implicitly disabled by debug symbols (-Zi).
-      args="$args $1 -OPT:REF -OPT:ICF -INCREMENTAL:NO"
+      # Runtime error checks (enabled by setting -RTC1 in the -DFFI_DEBUG
+      # case below) are not compatible with optimization flags and will
+      # cause the build to fail. Therefore, drop the optimization flag if
+      # -DFFI_DEBUG is also set.
+      case $args_orig in
+        *-DFFI_DEBUG*)
+          args="$args"
+        ;;
+        *)
+          # The ax_cc_maxopt.m4 macro from the upstream autoconf-archive
+          # project doesn't support MSVC and therefore ends up trying to
+          # use -O3. Use the equivalent "max optimization" flag for MSVC
+          # instead of erroring out.
+          case $1 in
+            -O3)
+              args="$args -O2"
+            ;;
+            *)
+              args="$args $1"
+            ;;
+          esac
+          opt="true"
+        ;;
+      esac
       shift 1
     ;;
     -g)
       # Enable debug symbol generation.
-      args="$args -Zi -DEBUG"
+      args="$args -Zi"
       shift 1
     ;;
     -DFFI_DEBUG)
@@ -126,6 +148,10 @@ do
       # to do here.
       shift 1
     ;;
+    -pedantic)
+      # libffi tests -pedantic with -Wall, so drop it also.
+      shift 1
+    ;;
     -Werror)
       args="$args -WX"
       shift 1
@@ -170,6 +196,13 @@ do
   esac
 done
 
+# If -Zi is specified, certain optimizations are implicitly disabled
+# by MSVC. Add back those optimizations if this is an optimized build.
+# NOTE: These arguments must come after all others.
+if [ -n "$opt" ]; then
+    args="$args -link -OPT:REF -OPT:ICF -INCREMENTAL:NO"
+fi
+
 if [ -n "$assembly" ]; then
     if [ -z "$outdir" ]; then
       outdir="."
@@ -189,7 +222,10 @@ if [ -n "$assembly" ]; then
 else
     args="$md $args"
     echo "$cl $args"
-    eval "\"$cl\" $args"
+    # Return an error code of 1 if an invalid command line parameter is passed
+    # instead of just ignoring it.
+    eval "(\"$cl\" $args 2>&1 1>&3 | \
+          awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1"
     result=$?
 fi