msvcc.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/bin/sh
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is the MSVC wrappificator.
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Timothy Wall <twalljava@dev.java.net>.
  19. # Portions created by the Initial Developer are Copyright (C) 2009
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Daniel Witte <dwitte@mozilla.com>
  24. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either the GNU General Public License Version 2 or later (the "GPL"), or
  27. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. #
  39. # GCC-compatible wrapper for cl.exe and ml.exe. Arguments are given in GCC
  40. # format and translated into something sensible for cl or ml.
  41. #
  42. args_orig=$@
  43. args="-nologo -W3"
  44. static_crt=
  45. debug_crt=
  46. cl="cl"
  47. ml="ml"
  48. safeseh="-safeseh"
  49. output=
  50. while [ $# -gt 0 ]
  51. do
  52. case $1
  53. in
  54. -fexceptions)
  55. # Don't enable exceptions for now.
  56. #args="$args -EHac"
  57. shift 1
  58. ;;
  59. -m32)
  60. shift 1
  61. ;;
  62. -m64)
  63. ml="ml64" # "$MSVC/x86_amd64/ml64"
  64. safeseh=
  65. shift 1
  66. ;;
  67. -clang-cl)
  68. cl="clang-cl"
  69. safeseh=
  70. shift 1
  71. ;;
  72. -O0)
  73. args="$args -Od"
  74. shift 1
  75. ;;
  76. -O*)
  77. # Runtime error checks (enabled by setting -RTC1 in the -DFFI_DEBUG
  78. # case below) are not compatible with optimization flags and will
  79. # cause the build to fail. Therefore, drop the optimization flag if
  80. # -DFFI_DEBUG is also set.
  81. case $args_orig in
  82. *-DFFI_DEBUG*)
  83. args="$args"
  84. ;;
  85. *)
  86. # The ax_cc_maxopt.m4 macro from the upstream autoconf-archive
  87. # project doesn't support MSVC and therefore ends up trying to
  88. # use -O3. Use the equivalent "max optimization" flag for MSVC
  89. # instead of erroring out.
  90. case $1 in
  91. -O3)
  92. args="$args -O2"
  93. ;;
  94. *)
  95. args="$args $1"
  96. ;;
  97. esac
  98. opt="true"
  99. ;;
  100. esac
  101. shift 1
  102. ;;
  103. -g)
  104. # Enable debug symbol generation.
  105. args="$args -Zi"
  106. shift 1
  107. ;;
  108. -DFFI_DEBUG)
  109. # Enable runtime error checks.
  110. args="$args -RTC1"
  111. defines="$defines $1"
  112. shift 1
  113. ;;
  114. -DUSE_STATIC_RTL)
  115. # Link against static CRT.
  116. static_crt=1
  117. shift 1
  118. ;;
  119. -DUSE_DEBUG_RTL)
  120. # Link against debug CRT.
  121. debug_crt=1
  122. shift 1
  123. ;;
  124. -c)
  125. args="$args -c"
  126. args="$(echo $args | sed 's%/Fe%/Fo%g')"
  127. single="-c"
  128. shift 1
  129. ;;
  130. -D*=*)
  131. name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
  132. value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
  133. args="$args -D${name}='$value'"
  134. defines="$defines -D${name}='$value'"
  135. shift 1
  136. ;;
  137. -D*)
  138. args="$args $1"
  139. defines="$defines $1"
  140. shift 1
  141. ;;
  142. -I)
  143. args="$args -I$2"
  144. includes="$includes -I$2"
  145. shift 2
  146. ;;
  147. -I*)
  148. args="$args $1"
  149. includes="$includes $1"
  150. shift 1
  151. ;;
  152. -W|-Wextra)
  153. # TODO map extra warnings
  154. shift 1
  155. ;;
  156. -Wall)
  157. # -Wall on MSVC is overzealous, and we already build with -W3. Nothing
  158. # to do here.
  159. shift 1
  160. ;;
  161. -pedantic)
  162. # libffi tests -pedantic with -Wall, so drop it also.
  163. shift 1
  164. ;;
  165. -Werror)
  166. args="$args -WX"
  167. shift 1
  168. ;;
  169. -W*)
  170. # TODO map specific warnings
  171. shift 1
  172. ;;
  173. -S)
  174. args="$args -FAs"
  175. shift 1
  176. ;;
  177. -o)
  178. outdir="$(dirname $2)"
  179. base="$(basename $2|sed 's/\.[^.]*//g')"
  180. if [ -n "$single" ]; then
  181. output="-Fo$2"
  182. else
  183. output="-Fe$2"
  184. fi
  185. if [ -n "$assembly" ]; then
  186. args="$args $output"
  187. else
  188. args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
  189. fi
  190. shift 2
  191. ;;
  192. *.S)
  193. src=$1
  194. assembly="true"
  195. shift 1
  196. ;;
  197. *.c)
  198. args="$args $1"
  199. shift 1
  200. ;;
  201. *)
  202. # Assume it's an MSVC argument, and pass it through.
  203. args="$args $1"
  204. shift 1
  205. ;;
  206. esac
  207. done
  208. # If -Zi is specified, certain optimizations are implicitly disabled
  209. # by MSVC. Add back those optimizations if this is an optimized build.
  210. # NOTE: These arguments must come after all others.
  211. if [ -n "$opt" ]; then
  212. args="$args -link -OPT:REF -OPT:ICF -INCREMENTAL:NO"
  213. fi
  214. if [ -n "$static_crt" ]; then
  215. md=-MT
  216. else
  217. md=-MD
  218. fi
  219. if [ -n "$debug_crt" ]; then
  220. md="${md}d"
  221. fi
  222. if [ -n "$assembly" ]; then
  223. if [ -z "$outdir" ]; then
  224. outdir="."
  225. fi
  226. ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
  227. echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
  228. "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
  229. output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
  230. args="-nologo $safeseh $single $output $ppsrc"
  231. echo "$ml $args"
  232. eval "\"$ml\" $args"
  233. result=$?
  234. # required to fix ml64 broken output?
  235. #mv *.obj $outdir
  236. else
  237. args="$md $args"
  238. echo "$cl $args"
  239. # Return an error code of 1 if an invalid command line parameter is passed
  240. # instead of just ignoring it.
  241. eval "(\"$cl\" $args 2>&1 1>&3 | \
  242. awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1"
  243. result=$?
  244. fi
  245. exit $result