2
0

compile 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #! /bin/sh
  2. # Wrapper for compilers which do not understand '-c -o'.
  3. scriptversion=2018-03-07.03; # UTC
  4. # Copyright (C) 1999-2020 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 <https://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* | MSYS*)
  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/* | msys/*)
  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. if test -f "$dir/lib$lib.a"; then
  106. found=yes
  107. lib=$dir/lib$lib.a
  108. break
  109. fi
  110. done
  111. IFS=$save_IFS
  112. if test "$found" != yes; then
  113. lib=$lib.lib
  114. fi
  115. }
  116. # func_cl_wrapper cl arg...
  117. # Adjust compile command to suit cl
  118. func_cl_wrapper ()
  119. {
  120. # Assume a capable shell
  121. lib_path=
  122. shared=:
  123. linker_opts=
  124. for arg
  125. do
  126. if test -n "$eat"; then
  127. eat=
  128. else
  129. case $1 in
  130. -o)
  131. # configure might choose to run compile as 'compile cc -o foo foo.c'.
  132. eat=1
  133. case $2 in
  134. *.o | *.[oO][bB][jJ])
  135. func_file_conv "$2"
  136. set x "$@" -Fo"$file"
  137. shift
  138. ;;
  139. *)
  140. func_file_conv "$2"
  141. set x "$@" -Fe"$file"
  142. shift
  143. ;;
  144. esac
  145. ;;
  146. -I)
  147. eat=1
  148. func_file_conv "$2" mingw
  149. set x "$@" -I"$file"
  150. shift
  151. ;;
  152. -I*)
  153. func_file_conv "${1#-I}" mingw
  154. set x "$@" -I"$file"
  155. shift
  156. ;;
  157. -l)
  158. eat=1
  159. func_cl_dashl "$2"
  160. set x "$@" "$lib"
  161. shift
  162. ;;
  163. -l*)
  164. func_cl_dashl "${1#-l}"
  165. set x "$@" "$lib"
  166. shift
  167. ;;
  168. -L)
  169. eat=1
  170. func_cl_dashL "$2"
  171. ;;
  172. -L*)
  173. func_cl_dashL "${1#-L}"
  174. ;;
  175. -static)
  176. shared=false
  177. ;;
  178. -Wl,*)
  179. arg=${1#-Wl,}
  180. save_ifs="$IFS"; IFS=','
  181. for flag in $arg; do
  182. IFS="$save_ifs"
  183. linker_opts="$linker_opts $flag"
  184. done
  185. IFS="$save_ifs"
  186. ;;
  187. -Xlinker)
  188. eat=1
  189. linker_opts="$linker_opts $2"
  190. ;;
  191. -*)
  192. set x "$@" "$1"
  193. shift
  194. ;;
  195. *.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
  196. func_file_conv "$1"
  197. set x "$@" -Tp"$file"
  198. shift
  199. ;;
  200. *.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
  201. func_file_conv "$1" mingw
  202. set x "$@" "$file"
  203. shift
  204. ;;
  205. *)
  206. set x "$@" "$1"
  207. shift
  208. ;;
  209. esac
  210. fi
  211. shift
  212. done
  213. if test -n "$linker_opts"; then
  214. linker_opts="-link$linker_opts"
  215. fi
  216. exec "$@" $linker_opts
  217. exit 1
  218. }
  219. eat=
  220. case $1 in
  221. '')
  222. echo "$0: No command. Try '$0 --help' for more information." 1>&2
  223. exit 1;
  224. ;;
  225. -h | --h*)
  226. cat <<\EOF
  227. Usage: compile [--help] [--version] PROGRAM [ARGS]
  228. Wrapper for compilers which do not understand '-c -o'.
  229. Remove '-o dest.o' from ARGS, run PROGRAM with the remaining
  230. arguments, and rename the output as expected.
  231. If you are trying to build a whole package this is not the
  232. right script to run: please start by reading the file 'INSTALL'.
  233. Report bugs to <bug-automake@gnu.org>.
  234. EOF
  235. exit $?
  236. ;;
  237. -v | --v*)
  238. echo "compile $scriptversion"
  239. exit $?
  240. ;;
  241. cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
  242. icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
  243. func_cl_wrapper "$@" # Doesn't return...
  244. ;;
  245. esac
  246. ofile=
  247. cfile=
  248. for arg
  249. do
  250. if test -n "$eat"; then
  251. eat=
  252. else
  253. case $1 in
  254. -o)
  255. # configure might choose to run compile as 'compile cc -o foo foo.c'.
  256. # So we strip '-o arg' only if arg is an object.
  257. eat=1
  258. case $2 in
  259. *.o | *.obj)
  260. ofile=$2
  261. ;;
  262. *)
  263. set x "$@" -o "$2"
  264. shift
  265. ;;
  266. esac
  267. ;;
  268. *.c)
  269. cfile=$1
  270. set x "$@" "$1"
  271. shift
  272. ;;
  273. *)
  274. set x "$@" "$1"
  275. shift
  276. ;;
  277. esac
  278. fi
  279. shift
  280. done
  281. if test -z "$ofile" || test -z "$cfile"; then
  282. # If no '-o' option was seen then we might have been invoked from a
  283. # pattern rule where we don't need one. That is ok -- this is a
  284. # normal compilation that the losing compiler can handle. If no
  285. # '.c' file was seen then we are probably linking. That is also
  286. # ok.
  287. exec "$@"
  288. fi
  289. # Name of file we expect compiler to create.
  290. cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
  291. # Create the lock directory.
  292. # Note: use '[/\\:.-]' here to ensure that we don't use the same name
  293. # that we are using for the .o file. Also, base the name on the expected
  294. # object file name, since that is what matters with a parallel build.
  295. lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
  296. while true; do
  297. if mkdir "$lockdir" >/dev/null 2>&1; then
  298. break
  299. fi
  300. sleep 1
  301. done
  302. # FIXME: race condition here if user kills between mkdir and trap.
  303. trap "rmdir '$lockdir'; exit 1" 1 2 15
  304. # Run the compile.
  305. "$@"
  306. ret=$?
  307. if test -f "$cofile"; then
  308. test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
  309. elif test -f "${cofile}bj"; then
  310. test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
  311. fi
  312. rmdir "$lockdir"
  313. exit $ret
  314. # Local Variables:
  315. # mode: shell-script
  316. # sh-indentation: 2
  317. # eval: (add-hook 'before-save-hook 'time-stamp)
  318. # time-stamp-start: "scriptversion="
  319. # time-stamp-format: "%:y-%02m-%02d.%02H"
  320. # time-stamp-time-zone: "UTC0"
  321. # time-stamp-end: "; # UTC"
  322. # End: