gen-gdblib-inc.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #!/usr/bin/env bash
  2. # Function to display help
  3. usage ()
  4. {
  5. echo "Script used to easily create collection of libraries needed"
  6. echo "to generate a Free Pascal IDE with debugger support."
  7. echo "Usage: Copy this script to the directory where you just compile"
  8. echo "a specific GNU debugger (for a specific target)"
  9. echo "and run ./$0 in that directory"
  10. echo "After, you will need to run a second script, copy-libs.sh"
  11. echo "with a single parameter specifying to which directory the libraries"
  12. echo "should be copied."
  13. echo "Possible parameters for this script:"
  14. echo "--forcestatic, to convert all -lname into $LINKLIB libname.a"
  15. echo "implicitlibs=\"space separated list of used system libraries\""
  16. echo "libdir=\"space separated list of library directories\""
  17. }
  18. # Try to find out which C compiler is used in Makefile
  19. # Look for CC make variable
  20. MAKE_CC=`sed -n "s:^[[:space:]]*CC[[:space:]]*=[[:space:]]*\(.*\):\1:p" \
  21. Makefile | head -1`
  22. if [ "x$MAKE_CC" != "x" ] ; then
  23. echo "Found CC=\"$MAKE_CC\" in Makefile"
  24. fi
  25. # Try to find used make executable
  26. # Try to find MAKE inside Makefile
  27. MAKE_MAKE=`sed -n "s:^[[:space:]]*MAKE[[:space:]]*=[[:space:]]*\(.*\):\1:p" \
  28. Makefile | head -1`
  29. if [ "x$MAKE_MAKE" != "x" ] ; then
  30. echo "Found MAKE=\"$MAKE_MAKE\" in Makefile"
  31. MAKE=$MAKE_MAKE
  32. else
  33. MAKE=`which gmake 2> /dev/null`
  34. if [ "x${MAKE}" == "x" ] ; then
  35. # Assume make is OK if MAKE is not found inside Makefile
  36. MAKE=make
  37. fi
  38. fi
  39. # Try to find used awk executable
  40. # Try to use AWK from Makefile
  41. MAKE_AWK=`sed -n "s:^[[:space:]]*AWK[[:space:]]*=[[:space:]]*\(.*\):\1:p" \
  42. Makefile | head -1`
  43. if [ "x$MAKE_AWK" != "x" ] ; then
  44. echo "Found AWK=\"$MAKE_AWK\""
  45. AWK=$MAKE_AWK
  46. else
  47. AWK=`which gawk 2> /dev/null`
  48. if [ "x$AWK" == "x" ] ; then
  49. #Assume awk is OK if gawk is not found
  50. AWK=awk
  51. fi
  52. fi
  53. # Set CC_is_gcc if GNU C compiler used
  54. if [ "${MAKE_CC}" != "${MAKE_CC/gcc/}" ] ; then
  55. CC_is_gcc=1
  56. echo "Found compiler is gcc"
  57. else
  58. CC_is_gcc=0
  59. fi
  60. # Possible extra option to pass when rebuilding gdb executable below
  61. MAKEOPT=
  62. if [ $CC_is_gcc -eq 1 ] ; then
  63. echo "Adding --verbose option to parse collect2 command line"
  64. LDFLAGS=--verbose
  65. else
  66. LDFLAGS=
  67. fi
  68. MAKE_EXEEXT=`sed -n "s:^[[:space:]]*EXEEXT[[:space:]]*=[[:space:]]*\(.*\):\1:p" \
  69. Makefile | head -1`
  70. if [ "x$MAKE_EXEEXT" != "x" ] ; then
  71. PATHEXT=$MAKE_EXEEXT
  72. fi
  73. if [ "${PATHEXT}" != "" ] ; then
  74. EXEEXT=.exe
  75. if [ "${DJDIR}" != "" ] ; then
  76. libdir=${DJDIR}/lib
  77. else
  78. # Do not add /lib, it is wrong, at least for msys systems
  79. libdir=
  80. fi
  81. else
  82. EXEEXT=
  83. if [ "$libdir" == "" ]; then
  84. # Do not add /lib, if -print-search-dirs can be used
  85. if [ $CC_is_gcc -eq 1 ] ; then
  86. libdir=
  87. else
  88. libdir=/lib
  89. fi
  90. fi
  91. fi
  92. force64bitcoreaddr=0
  93. CONFIGURE_ENABLE_64_BIT_BFD=`grep -w -- "--enable-64-bit-bfd" ./config.status`
  94. if [ "x$CONFIGURE_ENABLE_64_BIT_BFD" != "x" ] ; then
  95. echo "--enable-64-bit-bfd configure option found"
  96. force64bitcoreaddr=1
  97. fi
  98. if [ "$1" == "--help" ]; then
  99. usage
  100. exit
  101. fi
  102. forcestatic=0
  103. # Function to treat all command line option
  104. handle_option ()
  105. {
  106. opt_handled=0
  107. if [ "$1" == "" ] ; then
  108. return
  109. fi
  110. if [ "$1" == "--forcestatic" ]; then
  111. echo "Using only static libraries in gdblib.inc"
  112. forcestatic=1
  113. LDFLAGS="$LDFLAGS -static"
  114. opt_handled=1
  115. fi
  116. if [ "${1#implicitlibs=}" != "$1" ]; then
  117. implicitlibs=${1#implicitlibs=}
  118. echo "Also adding implicit libs \"$implicitlibs\""
  119. opt_handled=1
  120. fi
  121. if [ "${1#libdir=}" != "$1" ]; then
  122. libdir=${1#libdir=}
  123. echo "libdir is set to \"$libdir\""
  124. opt_handled=1
  125. fi
  126. }
  127. # Try to handle all command line options
  128. opt_handled=1
  129. while [ $opt_handled -eq 1 ]
  130. do
  131. handle_option "$1"
  132. if [ $opt_handled -eq 1 ] ; then
  133. shift
  134. fi
  135. done
  136. if [ "$1" != "" ]; then
  137. echo "Unrecognized option \"$1\""
  138. usage
  139. exit
  140. fi
  141. if [ "$OSTYPE" == "msys" ]; then
  142. echo "MSYS system detected"
  143. in_msys=1
  144. else
  145. in_msys=0
  146. fi
  147. echo "Deleting gdb${EXEEXT} to force recompile"
  148. rm -f gdb${EXEEXT}
  149. echo "Rebuilding gdb${EXEEXT}"
  150. ${MAKE} gdb${EXEEXT} ${MAKEOPT} LDFLAGS="$LDFLAGS" 2>&1 | tee make.log
  151. # Create gdb_get_stdin.c source file
  152. # To avoid stdin macro expansion hell.
  153. cat > gdb_get_stdin.c <<EOF
  154. #include "stdio.h"
  155. /* Missing prototypes. */
  156. FILE * gdb_get_stdin (void);
  157. FILE *
  158. gdb_get_stdin ()
  159. {
  160. return stdin;
  161. }
  162. EOF
  163. echo "Trying to compile gdb_get_stdin.c file"
  164. ${MAKE} gdb_get_stdin.o
  165. res=$?
  166. if [ $res -eq 0 ] ; then
  167. XM_ADD_FILES=gdb_get_stdin.o
  168. has_get_stdin=1
  169. else
  170. has_get_stdin=0
  171. fi
  172. # libgdb.a will not be built automatically anymore after
  173. # GDB release 7.4, so we need to explicitly generate it.
  174. if [ -f libgdb.a ] ; then
  175. rm -f libgdb.a
  176. fi
  177. echo "Rebuilding GDB library to include gdb_get_stdin.o"
  178. ${MAKE} libgdb.a ${MAKEOPT} XM_ADD_FILES=${XM_ADD_FILES}
  179. # version.c is an automatically generated file from gdb/version.in
  180. # We extract GDB version from that file.
  181. gdb_full_version=`sed -n "s:.*version.*\"\(.*\)\".*:\1:p" version.c`
  182. gdbcvs=`sed -n "s:.*version.*\"\(.*\)cvs\(.*\)\".*:\1cvs\2:p" version.c`
  183. gdb_version1=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1:p" version.c`
  184. gdb_version2=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\2:p" version.c`
  185. gdb_version=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1.\2:p" version.c`
  186. echo "found GDB full version is ${gdb_full_version}"
  187. echo "found GDB version is ${gdb_version}"
  188. if [ ${gdb_version2} -lt 10 ]; then
  189. gdbversion=GDB_V${gdb_version1}0${gdb_version2}
  190. else
  191. gdbversion=GDB_V${gdb_version1}${gdb_version2}
  192. fi
  193. echo "Using macro $gdbversion"
  194. make_log_has_collect2=`grep collect2 make.log`
  195. if [ "x$make_log_has_collect2" != "x" ] ; then
  196. find_cmd=collect2
  197. else
  198. find_cmd=cc
  199. fi
  200. cat make.log | ${AWK} -v find_cmd=$find_cmd '
  201. BEGIN {
  202. doprint=0
  203. }
  204. # We look for the compilation line
  205. # either gcc or cc
  206. $0 ~ find_cmd { doprint=1; }
  207. {
  208. if ( doprint == 1 ) {
  209. print $0
  210. }
  211. }
  212. ! /\\$/ { doprint=0; }
  213. ' | tee comp-cmd.log
  214. if [ "x$MAKE_CC" = "x" ] ; then
  215. gcccompiler=`sed -n "s:\([A-Za-z0-9_-]*gcc\) .*:\1:p" comp-cmd.log`
  216. else
  217. gcccompiler=$MAKE_CC
  218. fi
  219. if [ "$gcccompiler" != "" ]; then
  220. gcclibs=`$gcccompiler -print-search-dirs | sed -n "s#.*libraries: =\(.*\)#\1#p" `
  221. if [ "$gcclibs" != "" ]; then
  222. if [ $in_msys -eq 1 ]; then
  223. # If we are on msys, gcc is mingw, so that it uses c:/dir
  224. # while find is an msys utility that needs /c/dir path
  225. # we do this conversion below
  226. for let in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
  227. gcclibs=${gcclibs//$let:/\/$let}
  228. done
  229. for let in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
  230. gcclibs=${gcclibs//$let:/\/$let}
  231. done
  232. libdir="$libdir ${gcclibs//;/ }"
  233. else
  234. # if ; is present in gcclibs,assume this is the separator instead of :
  235. if [ "${gcclibs//;/ }" != "${gcclibs}" ]; then
  236. if [ "${gcclibs// /_}" != "${gccclibs}" ]; then
  237. # list also contains spaces, convert ' ' into '\ '
  238. gcclibs=${gcclibs// /\\ }
  239. fi
  240. libdir="$libdir ${gcclibs//;/ }"
  241. else
  242. libdir="$libdir ${gcclibs//:/ }"
  243. fi
  244. fi
  245. echo "gcc libs are \"$libdir\""
  246. fi
  247. fi
  248. # Try to locate all libraries
  249. echo Creating ./copy-libs.sh script
  250. has_libgdb=`cat comp-cmd.log | grep "libgdb\.a"`
  251. if [ "x$has_libgdb" != "x" ] ; then
  252. add_libgdb=0
  253. else
  254. add_libgdb=1
  255. fi
  256. cat comp-cmd.log | ${AWK} -v libdir="${libdir}" -v implibs="${implicitlibs}" \
  257. -v add_libgdb=${add_libgdb} '
  258. BEGIN {
  259. isdynlinker=0
  260. print "#!/usr/bin/env bash"
  261. print "# copy-libs.sh generated by awk script"
  262. print "INSTALL=`which ginstall 2> /dev/null `"
  263. print "if [ "$INSTALL" == "" ]; then"
  264. print " INSTALL=install"
  265. print "fi"
  266. print "if [ \"$1\" != \"\" ]; then"
  267. print " destdir=$1"
  268. print " $INSTALL -d ${destdir}"
  269. print "else"
  270. print " echo $0 destdir"
  271. print " echo destdir should be the location where libgdb.a"
  272. print " echo and all other archives should be copied"
  273. print " exit"
  274. print "fi"
  275. print "libdir=\"" libdir "\""
  276. print "# Copy gdblib.inc file"
  277. print "cp -p gdblib.inc ${destdir}"
  278. if (add_libgdb == 1) {
  279. print "# Adding libgdb.a"
  280. print "cp -p libgdb.a ${destdir}"
  281. }
  282. }
  283. {
  284. nb = split ($0,list);
  285. for (i=1; i<=nb; i++) {
  286. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  287. print "# Looking for static libs"
  288. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"\\1\\2 ","g",list[i]);
  289. print "cp -p " staticlib " ${destdir}";
  290. }
  291. if ( list[i] ~ /^-dynamic-linker$/ ) {
  292. i++;
  293. print "echo dynamic linker " list[i] " skipped";
  294. continue
  295. }
  296. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  297. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"\\1\\2 ","g",list[i]);
  298. print "echo " dynamiclib " found";
  299. }
  300. if ( list[i] ~ /^-l/ ) {
  301. print "#Looking for shared libs"
  302. systemlib = gensub (/-l([^ ]*)/,"lib\\1.a ","g",list[i]);
  303. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  304. print "if [ \"${systemlib}\" != \"\" ]; then";
  305. print " echo System lib found: ${systemlib}";
  306. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  307. print "else";
  308. print " echo Library " systemlib " not found, shared library assumed";
  309. print "fi";
  310. }
  311. }
  312. }
  313. END {
  314. nb = split (implibs,list);
  315. for (i=1;i<=nb; i++) {
  316. systemlib = "lib" list[i] ".a";
  317. print "echo Adding system library " systemlib;
  318. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  319. print "if [ \"${systemlib}\" != \"\" ]; then";
  320. print " echo System lib found: ${systemlib}";
  321. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  322. print "else";
  323. print " echo Library " systemlib " not found, shared library assumed";
  324. print "fi";
  325. }
  326. }
  327. ' | tee copy-libs.sh
  328. chmod u+x ./copy-libs.sh
  329. # For later
  330. echo Creating ./gdblib.inc file
  331. # Generate gdblib.inc file
  332. cat comp-cmd.log |${AWK} -v gdbcvs=${gdbcvs} \
  333. -v implibs="${implicitlibs}" -v libdir="${libdir}" \
  334. -v gdbversion=${gdbversion} -v forcestatic=${forcestatic} \
  335. -v force64bitcoreaddr=${force64bitcoreaddr} \
  336. -v has_get_stdin=${has_get_stdin} \
  337. -v add_libgdb=${add_libgdb} '
  338. BEGIN {
  339. use_mingw=0;
  340. print "{ libgdb.inc file generated by awk script }"
  341. print "{$define " gdbversion " }"
  342. if (gdbcvs) {
  343. print "{$define GDB_CVS}"
  344. }
  345. if (force64bitcoreaddr) {
  346. print "{$define GDB_CORE_ADDR_FORCE_64BITS}"
  347. }
  348. print "{$ifdef COMPILING_GDBINT_UNIT }"
  349. if (add_libgdb == 1) {
  350. print "{$LINKLIB libgdb.a} { Added here because Makefile does not use the libgdb library anymore }"
  351. }
  352. }
  353. {
  354. nb = split ($0,list);
  355. for (i=1; i<=nb; i++) {
  356. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  357. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  358. print staticlib;
  359. if ( list[i] ~ /mingw/ ) {
  360. use_mingw=1
  361. }
  362. }
  363. if ( list[i] ~ /^-dynamic-linker$/ ) {
  364. i++;
  365. print "{ Dynamic linker found " list[i] " }";
  366. continue
  367. }
  368. if ( list[i] ~ /-D__USE_MINGW_/ ) {
  369. use_mingw=1
  370. }
  371. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  372. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LINKLIB \\2} { found in \\1 \\3 }","g",list[i]);
  373. librarypath = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LIBRARYPATH \\1} { for \\2 \\3 }","g",list[i]);
  374. print dynamiclib;
  375. print librarypath;
  376. }
  377. if ( list[i] ~ /^-l/ ) {
  378. systemlib = gensub (/-l([^ ]*)/,"\\1","g",list[i]);
  379. if (forcestatic == 1) {
  380. systemlib="lib" systemlib ".a"
  381. }
  382. if ( systemlib ~ /mingw/ ) {
  383. use_mingw=1
  384. }
  385. print "{$LINKLIB " systemlib "} { with -l gcc option}";
  386. }
  387. }
  388. }
  389. END {
  390. print "{ List implicit libraries }"
  391. nb = split (implibs,list);
  392. for (i=1;i<=nb; i++) {
  393. if ( list[i] ~ /lib.*\.a/ ) {
  394. lib=list[i];
  395. } else {
  396. if ( forcestatic == 1 ) {
  397. lib="lib" list[i] ".a";
  398. } else {
  399. lib=list[i];
  400. }
  401. }
  402. print "{$LINKLIB " lib "} { implicit library } "
  403. }
  404. print "{$endif COMPILING_GDBINT_UNIT }"
  405. print "{ List library dirs }"
  406. nb = split (libdir,list);
  407. for (i=1;i<=nb; i++) {
  408. dir=list[i];
  409. print "{$LIBRARYPATH " dir "} { library path } "
  410. print "{$OBJECTPATH " dir "} { library path } "
  411. }
  412. print "{$undef NotImplemented}"
  413. if ( use_mingw == 1 ) {
  414. print "{$define USE_MINGW_GDB}"
  415. }
  416. if ( has_get_stdin == 1 ) {
  417. print "{$define LIBGDB_HAS_GET_STDIN}"
  418. }
  419. }
  420. ' | tee gdblib.inc