compile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #! /bin/sh
  2. # Wrapper for compilers which do not understand `-c -o'.
  3. scriptversion=2010-11-15.09; # UTC
  4. # Copyright (C) 1999, 2000, 2003, 2004, 2005, 2009, 2010 Free Software
  5. # Foundation, Inc.
  6. # Written by Tom Tromey <tromey@cygnus.com>.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2, or (at your option)
  11. # any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. # As a special exception to the GNU General Public License, if you
  21. # distribute this file as part of a program that contains a
  22. # configuration script generated by Autoconf, you may include it under
  23. # the same distribution terms that you use for the rest of that program.
  24. # This file is maintained in Automake, please report
  25. # bugs to <bug-automake@gnu.org> or send patches to
  26. # <automake-patches@gnu.org>.
  27. nl='
  28. '
  29. # We need space, tab and new line, in precisely that order. Quoting is
  30. # there to prevent tools from complaining about whitespace usage.
  31. IFS=" "" $nl"
  32. file_conv=
  33. # func_file_conv build_file lazy
  34. # Convert a $build file to $host form and store it in $file
  35. # Currently only supports Win32 hosts. If the determined conversion
  36. # type is listed in (the comma separated) LAZY, no conversion will
  37. # take place.
  38. func_file_conv ()
  39. {
  40. file=$1
  41. case $file in
  42. / | /[!/]*) # absolute file, and not a UNC file
  43. if test -z "$file_conv"; then
  44. # lazily determine how to convert abs files
  45. case `uname -s` in
  46. MINGW*)
  47. file_conv=mingw
  48. ;;
  49. CYGWIN*)
  50. file_conv=cygwin
  51. ;;
  52. *)
  53. file_conv=wine
  54. ;;
  55. esac
  56. fi
  57. case $file_conv/,$2, in
  58. *,$file_conv,*)
  59. ;;
  60. mingw/*)
  61. file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
  62. ;;
  63. cygwin/*)
  64. file=`cygpath -m "$file" || echo "$file"`
  65. ;;
  66. wine/*)
  67. file=`winepath -w "$file" || echo "$file"`
  68. ;;
  69. esac
  70. ;;
  71. esac
  72. }
  73. # func_cl_wrapper cl arg...
  74. # Adjust compile command to suit cl
  75. func_cl_wrapper ()
  76. {
  77. # Assume a capable shell
  78. lib_path=
  79. shared=:
  80. linker_opts=
  81. for arg
  82. do
  83. if test -n "$eat"; then
  84. eat=
  85. else
  86. case $1 in
  87. -o)
  88. # configure might choose to run compile as `compile cc -o foo foo.c'.
  89. eat=1
  90. case $2 in
  91. *.o | *.[oO][bB][jJ])
  92. func_file_conv "$2"
  93. set x "$@" -Fo"$file"
  94. shift
  95. ;;
  96. *)
  97. func_file_conv "$2"
  98. set x "$@" -Fe"$file"
  99. shift
  100. ;;
  101. esac
  102. ;;
  103. -I*)
  104. func_file_conv "${1#-I}" mingw
  105. set x "$@" -I"$file"
  106. shift
  107. ;;
  108. -l*)
  109. lib=${1#-l}
  110. found=no
  111. save_IFS=$IFS
  112. IFS=';'
  113. for dir in $lib_path $LIB
  114. do
  115. IFS=$save_IFS
  116. if $shared && test -f "$dir/$lib.dll.lib"; then
  117. found=yes
  118. set x "$@" "$dir/$lib.dll.lib"
  119. break
  120. fi
  121. if test -f "$dir/$lib.lib"; then
  122. found=yes
  123. set x "$@" "$dir/$lib.lib"
  124. break
  125. fi
  126. done
  127. IFS=$save_IFS
  128. test "$found" != yes && set x "$@" "$lib.lib"
  129. shift
  130. ;;
  131. -L*)
  132. func_file_conv "${1#-L}"
  133. if test -z "$lib_path"; then
  134. lib_path=$file
  135. else
  136. lib_path="$lib_path;$file"
  137. fi
  138. linker_opts="$linker_opts -LIBPATH:$file"
  139. ;;
  140. -static)
  141. shared=false
  142. ;;
  143. -Wl,*)
  144. arg=${1#-Wl,}
  145. save_ifs="$IFS"; IFS=','
  146. for flag in $arg; do
  147. IFS="$save_ifs"
  148. linker_opts="$linker_opts $flag"
  149. done
  150. IFS="$save_ifs"
  151. ;;
  152. -Xlinker)
  153. eat=1
  154. linker_opts="$linker_opts $2"
  155. ;;
  156. -*)
  157. set x "$@" "$1"
  158. shift
  159. ;;
  160. *.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
  161. func_file_conv "$1"
  162. set x "$@" -Tp"$file"
  163. shift
  164. ;;
  165. *.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
  166. func_file_conv "$1" mingw
  167. set x "$@" "$file"
  168. shift
  169. ;;
  170. *)
  171. set x "$@" "$1"
  172. shift
  173. ;;
  174. esac
  175. fi
  176. shift
  177. done
  178. if test -n "$linker_opts"; then
  179. linker_opts="-link$linker_opts"
  180. fi
  181. exec "$@" $linker_opts
  182. exit 1
  183. }
  184. eat=
  185. case $1 in
  186. '')
  187. echo "$0: No command. Try \`$0 --help' for more information." 1>&2
  188. exit 1;
  189. ;;
  190. -h | --h*)
  191. cat <<\EOF
  192. Usage: compile [--help] [--version] PROGRAM [ARGS]
  193. Wrapper for compilers which do not understand `-c -o'.
  194. Remove `-o dest.o' from ARGS, run PROGRAM with the remaining
  195. arguments, and rename the output as expected.
  196. If you are trying to build a whole package this is not the
  197. right script to run: please start by reading the file `INSTALL'.
  198. Report bugs to <bug-automake@gnu.org>.
  199. EOF
  200. exit $?
  201. ;;
  202. -v | --v*)
  203. echo "compile $scriptversion"
  204. exit $?
  205. ;;
  206. cl | *[/\\]cl | cl.exe | *[/\\]cl.exe )
  207. func_cl_wrapper "$@" # Doesn't return...
  208. ;;
  209. esac
  210. ofile=
  211. cfile=
  212. for arg
  213. do
  214. if test -n "$eat"; then
  215. eat=
  216. else
  217. case $1 in
  218. -o)
  219. # configure might choose to run compile as `compile cc -o foo foo.c'.
  220. # So we strip `-o arg' only if arg is an object.
  221. eat=1
  222. case $2 in
  223. *.o | *.obj)
  224. ofile=$2
  225. ;;
  226. *)
  227. set x "$@" -o "$2"
  228. shift
  229. ;;
  230. esac
  231. ;;
  232. *.c)
  233. cfile=$1
  234. set x "$@" "$1"
  235. shift
  236. ;;
  237. *)
  238. set x "$@" "$1"
  239. shift
  240. ;;
  241. esac
  242. fi
  243. shift
  244. done
  245. if test -z "$ofile" || test -z "$cfile"; then
  246. # If no `-o' option was seen then we might have been invoked from a
  247. # pattern rule where we don't need one. That is ok -- this is a
  248. # normal compilation that the losing compiler can handle. If no
  249. # `.c' file was seen then we are probably linking. That is also
  250. # ok.
  251. exec "$@"
  252. fi
  253. # Name of file we expect compiler to create.
  254. cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
  255. # Create the lock directory.
  256. # Note: use `[/\\:.-]' here to ensure that we don't use the same name
  257. # that we are using for the .o file. Also, base the name on the expected
  258. # object file name, since that is what matters with a parallel build.
  259. lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
  260. while true; do
  261. if mkdir "$lockdir" >/dev/null 2>&1; then
  262. break
  263. fi
  264. sleep 1
  265. done
  266. # FIXME: race condition here if user kills between mkdir and trap.
  267. trap "rmdir '$lockdir'; exit 1" 1 2 15
  268. # Run the compile.
  269. "$@"
  270. ret=$?
  271. if test -f "$cofile"; then
  272. test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
  273. elif test -f "${cofile}bj"; then
  274. test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
  275. fi
  276. rmdir "$lockdir"
  277. exit $ret
  278. # Local Variables:
  279. # mode: shell-script
  280. # sh-indentation: 2
  281. # eval: (add-hook 'write-file-hooks 'time-stamp)
  282. # time-stamp-start: "scriptversion="
  283. # time-stamp-format: "%:y-%02m-%02d.%02H"
  284. # time-stamp-time-zone: "UTC"
  285. # time-stamp-end: "; # UTC"
  286. # End: