bakefile-dllar.m4 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. AC_DEFUN([AC_BAKEFILE_CREATE_FILE_DLLAR_SH],
  2. [
  3. dnl ===================== dllar.sh begins here =====================
  4. dnl (Created by merge-scripts.py from dllar.sh
  5. dnl file do not edit here!)
  6. D='$'
  7. cat <<EOF >dllar.sh
  8. #!/bin/sh
  9. #
  10. # dllar - a tool to build both a .dll and an .a file
  11. # from a set of object (.o) files for EMX/OS2.
  12. #
  13. # Written by Andrew Zabolotny, bit@freya.etu.ru
  14. # Ported to Unix like shell by Stefan Neis, Stefan.Neis@t-online.de
  15. #
  16. # This script will accept a set of files on the command line.
  17. # All the public symbols from the .o files will be exported into
  18. # a .DEF file, then linker will be run (through gcc) against them to
  19. # build a shared library consisting of all given .o files. All libraries
  20. # (.a) will be first decompressed into component .o files then act as
  21. # described above. You can optionally give a description (-d "description")
  22. # which will be put into .DLL. To see the list of accepted options (as well
  23. # as command-line format) simply run this program without options. The .DLL
  24. # is built to be imported by name (there is no guarantee that new versions
  25. # of the library you build will have same ordinals for same symbols).
  26. #
  27. # dllar is free software; you can redistribute it and/or modify
  28. # it under the terms of the GNU General Public License as published by
  29. # the Free Software Foundation; either version 2, or (at your option)
  30. # any later version.
  31. #
  32. # dllar is distributed in the hope that it will be useful,
  33. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. # GNU General Public License for more details.
  36. #
  37. # You should have received a copy of the GNU General Public License
  38. # along with dllar; see the file COPYING. If not, write to the Free
  39. # Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  40. # 02111-1307, USA.
  41. # To successfuly run this program you will need:
  42. # - Current drive should have LFN support (HPFS, ext2, network, etc)
  43. # (Sometimes dllar generates filenames which won't fit 8.3 scheme)
  44. # - gcc
  45. # (used to build the .dll)
  46. # - emxexp
  47. # (used to create .def file from .o files)
  48. # - emximp
  49. # (used to create .a file from .def file)
  50. # - GNU text utilites (cat, sort, uniq)
  51. # used to process emxexp output
  52. # - GNU file utilities (mv, rm)
  53. # - GNU sed
  54. # - lxlite (optional, see flag below)
  55. # (used for general .dll cleanup)
  56. #
  57. flag_USE_LXLITE=1;
  58. #
  59. # helper functions
  60. # basnam, variant of basename, which does _not_ remove the path, _iff_
  61. # second argument (suffix to remove) is given
  62. basnam(){
  63. case ${D}# in
  64. 1)
  65. echo ${D}1 | sed 's/.*\\///' | sed 's/.*\\\\//'
  66. ;;
  67. 2)
  68. echo ${D}1 | sed 's/'${D}2'${D}//'
  69. ;;
  70. *)
  71. echo "error in basnam ${D}*"
  72. exit 8
  73. ;;
  74. esac
  75. }
  76. # Cleanup temporary files and output
  77. CleanUp() {
  78. cd ${D}curDir
  79. for i in ${D}inputFiles ; do
  80. case ${D}i in
  81. *!)
  82. rm -rf \`basnam ${D}i !\`
  83. ;;
  84. *)
  85. ;;
  86. esac
  87. done
  88. # Kill result in case of failure as there is just to many stupid make/nmake
  89. # things out there which doesn't do this.
  90. if @<:@ ${D}# -eq 0 @:>@; then
  91. rm -f ${D}arcFile ${D}arcFile2 ${D}defFile ${D}dllFile
  92. fi
  93. }
  94. # Print usage and exit script with rc=1.
  95. PrintHelp() {
  96. echo 'Usage: dllar.sh @<:@-o@<:@utput@:>@ output_file@:>@ @<:@-i@<:@mport@:>@ importlib_name@:>@'
  97. echo ' @<:@-name-mangler-script script.sh@:>@'
  98. echo ' @<:@-d@<:@escription@:>@ "dll descrption"@:>@ @<:@-cc "CC"@:>@ @<:@-f@<:@lags@:>@ "CFLAGS"@:>@'
  99. echo ' @<:@-ord@<:@inals@:>@@:>@ -ex@<:@clude@:>@ "symbol(s)"'
  100. echo ' @<:@-libf@<:@lags@:>@ "{INIT|TERM}{GLOBAL|INSTANCE}"@:>@ @<:@-nocrt@<:@dll@:>@@:>@ @<:@-nolxl@<:@ite@:>@@:>@'
  101. echo ' @<:@*.o@:>@ @<:@*.a@:>@'
  102. echo '*> "output_file" should have no extension.'
  103. echo ' If it has the .o, .a or .dll extension, it is automatically removed.'
  104. echo ' The import library name is derived from this and is set to "name".a,'
  105. echo ' unless overridden by -import'
  106. echo '*> "importlib_name" should have no extension.'
  107. echo ' If it has the .o, or .a extension, it is automatically removed.'
  108. echo ' This name is used as the import library name and may be longer and'
  109. echo ' more descriptive than the DLL name which has to follow the old '
  110. echo ' 8.3 convention of FAT.'
  111. echo '*> "script.sh may be given to override the output_file name by a'
  112. echo ' different name. It is mainly useful if the regular make process'
  113. echo ' of some package does not take into account OS/2 restriction of'
  114. echo ' DLL name lengths. It takes the importlib name as input and is'
  115. echo ' supposed to procude a shorter name as output. The script should'
  116. echo ' expect to get importlib_name without extension and should produce'
  117. echo ' a (max.) 8 letter name without extension.'
  118. echo '*> "cc" is used to use another GCC executable. (default: gcc.exe)'
  119. echo '*> "flags" should be any set of valid GCC flags. (default: -s -Zcrtdll)'
  120. echo ' These flags will be put at the start of GCC command line.'
  121. echo '*> -ord@<:@inals@:>@ tells dllar to export entries by ordinals. Be careful.'
  122. echo '*> -ex@<:@clude@:>@ defines symbols which will not be exported. You can define'
  123. echo ' multiple symbols, for example -ex "myfunc yourfunc _GLOBAL*".'
  124. echo ' If the last character of a symbol is "*", all symbols beginning'
  125. echo ' with the prefix before "*" will be exclude, (see _GLOBAL* above).'
  126. echo '*> -libf@<:@lags@:>@ can be used to add INITGLOBAL/INITINSTANCE and/or'
  127. echo ' TERMGLOBAL/TERMINSTANCE flags to the dynamically-linked library.'
  128. echo '*> -nocrt@<:@dll@:>@ switch will disable linking the library against emx''s'
  129. echo ' C runtime DLLs.'
  130. echo '*> -nolxl@<:@ite@:>@ switch will disable running lxlite on the resulting DLL.'
  131. echo '*> All other switches (for example -L./ or -lmylib) will be passed'
  132. echo ' unchanged to GCC at the end of command line.'
  133. echo '*> If you create a DLL from a library and you do not specify -o,'
  134. echo ' the basename for DLL and import library will be set to library name,'
  135. echo ' the initial library will be renamed to 'name'_s.a (_s for static)'
  136. echo ' i.e. "dllar gcc.a" will create gcc.dll and gcc.a, and the initial'
  137. echo ' library will be renamed into gcc_s.a.'
  138. echo '--------'
  139. echo 'Example:'
  140. echo ' dllar -o gcc290.dll libgcc.a -d "GNU C runtime library" -ord'
  141. echo ' -ex "__main __ctordtor*" -libf "INITINSTANCE TERMINSTANCE"'
  142. CleanUp
  143. exit 1
  144. }
  145. # Execute a command.
  146. # If exit code of the commnad <> 0 CleanUp() is called and we'll exit the script.
  147. # @Uses Whatever CleanUp() uses.
  148. doCommand() {
  149. echo "${D}*"
  150. eval ${D}*
  151. rcCmd=${D}?
  152. if @<:@ ${D}rcCmd -ne 0 @:>@; then
  153. echo "command failed, exit code="${D}rcCmd
  154. CleanUp
  155. exit ${D}rcCmd
  156. fi
  157. }
  158. # main routine
  159. # setup globals
  160. cmdLine=${D}*
  161. outFile=""
  162. outimpFile=""
  163. inputFiles=""
  164. renameScript=""
  165. description=""
  166. CC=gcc.exe
  167. CFLAGS="-s -Zcrtdll"
  168. EXTRA_CFLAGS=""
  169. EXPORT_BY_ORDINALS=0
  170. exclude_symbols=""
  171. library_flags=""
  172. curDir=\`pwd\`
  173. curDirS=curDir
  174. case ${D}curDirS in
  175. */)
  176. ;;
  177. *)
  178. curDirS=${D}{curDirS}"/"
  179. ;;
  180. esac
  181. # Parse commandline
  182. libsToLink=0
  183. omfLinking=0
  184. while @<:@ ${D}1 @:>@; do
  185. case ${D}1 in
  186. -ord*)
  187. EXPORT_BY_ORDINALS=1;
  188. ;;
  189. -o*)
  190. shift
  191. outFile=${D}1
  192. ;;
  193. -i*)
  194. shift
  195. outimpFile=${D}1
  196. ;;
  197. -name-mangler-script)
  198. shift
  199. renameScript=${D}1
  200. ;;
  201. -d*)
  202. shift
  203. description=${D}1
  204. ;;
  205. -f*)
  206. shift
  207. CFLAGS=${D}1
  208. ;;
  209. -c*)
  210. shift
  211. CC=${D}1
  212. ;;
  213. -h*)
  214. PrintHelp
  215. ;;
  216. -ex*)
  217. shift
  218. exclude_symbols=${D}{exclude_symbols}${D}1" "
  219. ;;
  220. -libf*)
  221. shift
  222. library_flags=${D}{library_flags}${D}1" "
  223. ;;
  224. -nocrt*)
  225. CFLAGS="-s"
  226. ;;
  227. -nolxl*)
  228. flag_USE_LXLITE=0
  229. ;;
  230. -* | /*)
  231. case ${D}1 in
  232. -L* | -l*)
  233. libsToLink=1
  234. ;;
  235. -Zomf)
  236. omfLinking=1
  237. ;;
  238. *)
  239. ;;
  240. esac
  241. EXTRA_CFLAGS=${D}{EXTRA_CFLAGS}" "${D}1
  242. ;;
  243. *.dll)
  244. EXTRA_CFLAGS="${D}{EXTRA_CFLAGS} \`basnam ${D}1 .dll\`"
  245. if @<:@ ${D}omfLinking -eq 1 @:>@; then
  246. EXTRA_CFLAGS="${D}{EXTRA_CFLAGS}.lib"
  247. else
  248. EXTRA_CFLAGS="${D}{EXTRA_CFLAGS}.a"
  249. fi
  250. ;;
  251. *)
  252. found=0;
  253. if @<:@ ${D}libsToLink -ne 0 @:>@; then
  254. EXTRA_CFLAGS=${D}{EXTRA_CFLAGS}" "${D}1
  255. else
  256. for file in ${D}1 ; do
  257. if @<:@ -f ${D}file @:>@; then
  258. inputFiles="${D}{inputFiles} ${D}file"
  259. found=1
  260. fi
  261. done
  262. if @<:@ ${D}found -eq 0 @:>@; then
  263. echo "ERROR: No file(s) found: "${D}1
  264. exit 8
  265. fi
  266. fi
  267. ;;
  268. esac
  269. shift
  270. done # iterate cmdline words
  271. #
  272. if @<:@ -z "${D}inputFiles" @:>@; then
  273. echo "dllar: no input files"
  274. PrintHelp
  275. fi
  276. # Now extract all .o files from .a files
  277. newInputFiles=""
  278. for file in ${D}inputFiles ; do
  279. case ${D}file in
  280. *.a | *.lib)
  281. case ${D}file in
  282. *.a)
  283. suffix=".a"
  284. AR="ar"
  285. ;;
  286. *.lib)
  287. suffix=".lib"
  288. AR="emxomfar"
  289. EXTRA_CFLAGS="${D}EXTRA_CFLAGS -Zomf"
  290. ;;
  291. *)
  292. ;;
  293. esac
  294. dirname=\`basnam ${D}file ${D}suffix\`"_%"
  295. mkdir ${D}dirname
  296. if @<:@ ${D}? -ne 0 @:>@; then
  297. echo "Failed to create subdirectory ./${D}dirname"
  298. CleanUp
  299. exit 8;
  300. fi
  301. # Append '!' to indicate archive
  302. newInputFiles="${D}newInputFiles ${D}{dirname}!"
  303. doCommand "cd ${D}dirname; ${D}AR x ../${D}file"
  304. cd ${D}curDir
  305. found=0;
  306. for subfile in ${D}dirname/*.o* ; do
  307. if @<:@ -f ${D}subfile @:>@; then
  308. found=1
  309. if @<:@ -s ${D}subfile @:>@; then
  310. # FIXME: This should be: is file size > 32 byte, _not_ > 0!
  311. newInputFiles="${D}newInputFiles ${D}subfile"
  312. fi
  313. fi
  314. done
  315. if @<:@ ${D}found -eq 0 @:>@; then
  316. echo "WARNING: there are no files in archive \\'${D}file\\'"
  317. fi
  318. ;;
  319. *)
  320. newInputFiles="${D}{newInputFiles} ${D}file"
  321. ;;
  322. esac
  323. done
  324. inputFiles="${D}newInputFiles"
  325. # Output filename(s).
  326. do_backup=0;
  327. if @<:@ -z ${D}outFile @:>@; then
  328. do_backup=1;
  329. set outFile ${D}inputFiles; outFile=${D}2
  330. fi
  331. # If it is an archive, remove the '!' and the '_%' suffixes
  332. case ${D}outFile in
  333. *_%!)
  334. outFile=\`basnam ${D}outFile _%!\`
  335. ;;
  336. *)
  337. ;;
  338. esac
  339. case ${D}outFile in
  340. *.dll)
  341. outFile=\`basnam ${D}outFile .dll\`
  342. ;;
  343. *.DLL)
  344. outFile=\`basnam ${D}outFile .DLL\`
  345. ;;
  346. *.o)
  347. outFile=\`basnam ${D}outFile .o\`
  348. ;;
  349. *.obj)
  350. outFile=\`basnam ${D}outFile .obj\`
  351. ;;
  352. *.a)
  353. outFile=\`basnam ${D}outFile .a\`
  354. ;;
  355. *.lib)
  356. outFile=\`basnam ${D}outFile .lib\`
  357. ;;
  358. *)
  359. ;;
  360. esac
  361. case ${D}outimpFile in
  362. *.a)
  363. outimpFile=\`basnam ${D}outimpFile .a\`
  364. ;;
  365. *.lib)
  366. outimpFile=\`basnam ${D}outimpFile .lib\`
  367. ;;
  368. *)
  369. ;;
  370. esac
  371. if @<:@ -z ${D}outimpFile @:>@; then
  372. outimpFile=${D}outFile
  373. fi
  374. defFile="${D}{outFile}.def"
  375. arcFile="${D}{outimpFile}.a"
  376. arcFile2="${D}{outimpFile}.lib"
  377. #create ${D}dllFile as something matching 8.3 restrictions,
  378. if @<:@ -z ${D}renameScript @:>@ ; then
  379. dllFile="${D}outFile"
  380. else
  381. dllFile=\`${D}renameScript ${D}outimpFile\`
  382. fi
  383. if @<:@ ${D}do_backup -ne 0 @:>@ ; then
  384. if @<:@ -f ${D}arcFile @:>@ ; then
  385. doCommand "mv ${D}arcFile ${D}{outFile}_s.a"
  386. fi
  387. if @<:@ -f ${D}arcFile2 @:>@ ; then
  388. doCommand "mv ${D}arcFile2 ${D}{outFile}_s.lib"
  389. fi
  390. fi
  391. # Extract public symbols from all the object files.
  392. tmpdefFile=${D}{defFile}_%
  393. rm -f ${D}tmpdefFile
  394. for file in ${D}inputFiles ; do
  395. case ${D}file in
  396. *!)
  397. ;;
  398. *)
  399. doCommand "emxexp -u ${D}file >> ${D}tmpdefFile"
  400. ;;
  401. esac
  402. done
  403. # Create the def file.
  404. rm -f ${D}defFile
  405. echo "LIBRARY \`basnam ${D}dllFile\` ${D}library_flags" >> ${D}defFile
  406. dllFile="${D}{dllFile}.dll"
  407. if @<:@ ! -z ${D}description @:>@; then
  408. echo "DESCRIPTION \\"${D}{description}\\"" >> ${D}defFile
  409. fi
  410. echo "EXPORTS" >> ${D}defFile
  411. doCommand "cat ${D}tmpdefFile | sort.exe | uniq.exe > ${D}{tmpdefFile}%"
  412. grep -v "^ *;" < ${D}{tmpdefFile}% | grep -v "^ *${D}" >${D}tmpdefFile
  413. # Checks if the export is ok or not.
  414. for word in ${D}exclude_symbols; do
  415. grep -v ${D}word < ${D}tmpdefFile >${D}{tmpdefFile}%
  416. mv ${D}{tmpdefFile}% ${D}tmpdefFile
  417. done
  418. if @<:@ ${D}EXPORT_BY_ORDINALS -ne 0 @:>@; then
  419. sed "=" < ${D}tmpdefFile | \\
  420. sed '
  421. N
  422. : loop
  423. s/^\\(@<:@0-9@:>@\\+\\)\\(@<:@^;@:>@*\\)\\(;.*\\)\\?/\\2 @\\1 NONAME/
  424. t loop
  425. ' > ${D}{tmpdefFile}%
  426. grep -v "^ *${D}" < ${D}{tmpdefFile}% > ${D}tmpdefFile
  427. else
  428. rm -f ${D}{tmpdefFile}%
  429. fi
  430. cat ${D}tmpdefFile >> ${D}defFile
  431. rm -f ${D}tmpdefFile
  432. # Do linking, create implib, and apply lxlite.
  433. gccCmdl="";
  434. for file in ${D}inputFiles ; do
  435. case ${D}file in
  436. *!)
  437. ;;
  438. *)
  439. gccCmdl="${D}gccCmdl ${D}file"
  440. ;;
  441. esac
  442. done
  443. doCommand "${D}CC ${D}CFLAGS -Zdll -o ${D}dllFile ${D}defFile ${D}gccCmdl ${D}EXTRA_CFLAGS"
  444. touch "${D}{outFile}.dll"
  445. doCommand "emximp -o ${D}arcFile ${D}defFile"
  446. if @<:@ ${D}flag_USE_LXLITE -ne 0 @:>@; then
  447. add_flags="";
  448. if @<:@ ${D}EXPORT_BY_ORDINALS -ne 0 @:>@; then
  449. add_flags="-ynd"
  450. fi
  451. doCommand "lxlite -cs -t: -mrn -mln ${D}add_flags ${D}dllFile"
  452. fi
  453. doCommand "emxomf -s -l ${D}arcFile"
  454. # Successful exit.
  455. CleanUp 1
  456. exit 0
  457. EOF
  458. dnl ===================== dllar.sh ends here =====================
  459. ])