1
0

compile 7.1 KB

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