gen-gdblib-inc.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. if [ "${1//=/ }" != "$1" ]; then
  127. # Some variable set explicitly
  128. echo "Evaluating $1"
  129. export $1
  130. opt_handled=1
  131. fi
  132. }
  133. # Try to handle all command line options
  134. opt_handled=1
  135. while [ $opt_handled -eq 1 ]
  136. do
  137. handle_option "$1"
  138. if [ $opt_handled -eq 1 ] ; then
  139. shift
  140. fi
  141. done
  142. if [ "$1" != "" ]; then
  143. echo "Unrecognized option \"$1\""
  144. usage
  145. exit
  146. fi
  147. if [ "$OSTYPE" == "msys" ]; then
  148. echo "MSYS system detected"
  149. in_msys=1
  150. else
  151. in_msys=0
  152. fi
  153. echo "Deleting gdb${EXEEXT} to force recompile"
  154. rm -f gdb${EXEEXT}
  155. echo "Rebuilding gdb${EXEEXT}"
  156. ${MAKE} gdb${EXEEXT} ${MAKEOPT} LDFLAGS="$LDFLAGS" 2>&1 | tee make.log
  157. # Create gdb_get_stdin.c source file
  158. # To avoid stdin macro expansion hell.
  159. cat > gdb_get_stdin.c <<EOF
  160. #include "stdio.h"
  161. /* Missing prototypes. */
  162. FILE * gdb_get_stdin (void);
  163. FILE *
  164. gdb_get_stdin (void)
  165. {
  166. return stdin;
  167. }
  168. EOF
  169. echo "Trying to compile gdb_get_stdin.c file"
  170. ${MAKE} gdb_get_stdin.o
  171. res=$?
  172. if [ $res -eq 0 ] ; then
  173. XM_ADD_FILES=gdb_get_stdin.o
  174. has_get_stdin=1
  175. else
  176. has_get_stdin=0
  177. fi
  178. # libgdb.a will not be built automatically anymore after
  179. # GDB release 7.4, so we need to explicitly generate it.
  180. if [ -f libgdb.a ] ; then
  181. rm -f libgdb.a
  182. fi
  183. echo "Rebuilding GDB library to include gdb_get_stdin.o"
  184. ${MAKE} libgdb.a ${MAKEOPT} XM_ADD_FILES=${XM_ADD_FILES}
  185. # version.c is an automatically generated file from gdb/version.in
  186. # We extract GDB version from that file.
  187. gdb_full_version=`sed -n "s:.*version.*\"\(.*\)\".*:\1:p" version.c`
  188. gdbcvs=`sed -n "s:.*version.*\"\(.*\)cvs\(.*\)\".*:\1cvs\2:p" version.c`
  189. gdb_version1=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1:p" version.c`
  190. gdb_version2=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\2:p" version.c`
  191. gdb_version=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1.\2:p" version.c`
  192. echo "found GDB full version is ${gdb_full_version}"
  193. echo "found GDB version is ${gdb_version}"
  194. if [ ${gdb_version2} -lt 10 ]; then
  195. gdbversion=GDB_V${gdb_version1}0${gdb_version2}
  196. else
  197. gdbversion=GDB_V${gdb_version1}${gdb_version2}
  198. fi
  199. echo "Using macro $gdbversion"
  200. make_log_has_collect2=`grep collect2 make.log`
  201. if [ "x$make_log_has_collect2" != "x" ] ; then
  202. find_cmd=collect2
  203. else
  204. find_cmd=cc
  205. fi
  206. cat make.log | ${AWK} -v find_cmd=$find_cmd '
  207. BEGIN {
  208. doprint=0
  209. }
  210. # We look for the compilation line
  211. # either gcc or cc
  212. $0 ~ find_cmd { doprint=1; }
  213. {
  214. if ( doprint == 1 ) {
  215. print $0
  216. }
  217. }
  218. ! /\\$/ { doprint=0; }
  219. ' | tee comp-cmd.log
  220. if [ "x$MAKE_CC" = "x" ] ; then
  221. gcccompiler=`sed -n "s:\([A-Za-z0-9_-]*gcc\) .*:\1:p" comp-cmd.log`
  222. else
  223. gcccompiler=$MAKE_CC
  224. fi
  225. if [ "$gcccompiler" != "" ]; then
  226. gcclibs=`$gcccompiler -print-search-dirs | sed -n "s#.*libraries: =\(.*\)#\1#p" `
  227. if [ "$gcclibs" != "" ]; then
  228. if [ $in_msys -eq 1 ]; then
  229. # If we are on msys, gcc is mingw, so that it uses c:/dir
  230. # while find is an msys utility that needs /c/dir path
  231. # we do this conversion below
  232. 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
  233. gcclibs=${gcclibs//$let:/\/$let}
  234. done
  235. 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
  236. gcclibs=${gcclibs//$let:/\/$let}
  237. done
  238. libdir="$libdir ${gcclibs//;/ }"
  239. else
  240. # if ; is present in gcclibs,assume this is the separator instead of :
  241. if [ "${gcclibs//;/ }" != "${gcclibs}" ]; then
  242. if [ "${gcclibs// /_}" != "${gccclibs}" ]; then
  243. # list also contains spaces, convert ' ' into '\ '
  244. gcclibs=${gcclibs// /\\ }
  245. fi
  246. libdir="$libdir ${gcclibs//;/ }"
  247. else
  248. libdir="$libdir ${gcclibs//:/ }"
  249. fi
  250. fi
  251. echo "gcc libs are \"$libdir\""
  252. fi
  253. fi
  254. # Try to locate all libraries
  255. echo Creating ./copy-libs.sh script
  256. has_libgdb=`cat comp-cmd.log | grep "libgdb\.a"`
  257. if [ "x$has_libgdb" != "x" ] ; then
  258. add_libgdb=0
  259. else
  260. add_libgdb=1
  261. fi
  262. cat comp-cmd.log | ${AWK} -v libdir="${libdir}" -v implibs="${implicitlibs}" \
  263. -v add_libgdb=${add_libgdb} '
  264. BEGIN {
  265. isdynlinker=0
  266. print "#!/usr/bin/env bash"
  267. print "# copy-libs.sh generated by awk script"
  268. print "INSTALL=`which ginstall 2> /dev/null `"
  269. print "if [ "$INSTALL" == "" ]; then"
  270. print " INSTALL=install"
  271. print "fi"
  272. print "if [ \"$1\" != \"\" ]; then"
  273. print " destdir=$1"
  274. print " $INSTALL -d ${destdir}"
  275. print "else"
  276. print " echo $0 destdir"
  277. print " echo destdir should be the location where libgdb.a"
  278. print " echo and all other archives should be copied"
  279. print " exit"
  280. print "fi"
  281. print "libdir=\"" libdir "\""
  282. print "# Copy gdblib.inc file"
  283. print "cp -p gdblib.inc ${destdir}"
  284. if (add_libgdb == 1) {
  285. print "# Adding libgdb.a"
  286. print "cp -p libgdb.a ${destdir}"
  287. }
  288. }
  289. {
  290. nb = split ($0,list);
  291. for (i=1; i<=nb; i++) {
  292. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  293. print "# Looking for static libs"
  294. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"\\1\\2 ","g",list[i]);
  295. print "cp -p " staticlib " ${destdir}";
  296. }
  297. if ( list[i] ~ /^-dynamic-linker$/ ) {
  298. i++;
  299. print "echo dynamic linker " list[i] " skipped";
  300. continue
  301. }
  302. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  303. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"\\1\\2 ","g",list[i]);
  304. print "echo " dynamiclib " found";
  305. }
  306. if ( list[i] ~ /^-l/ ) {
  307. print "#Looking for shared libs"
  308. systemlib = gensub (/-l([^ ]*)/,"lib\\1.a ","g",list[i]);
  309. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  310. print "if [ \"${systemlib}\" != \"\" ]; then";
  311. print " echo System lib found: ${systemlib}";
  312. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  313. print "else";
  314. print " echo Library " systemlib " not found, shared library assumed";
  315. print "fi";
  316. }
  317. }
  318. }
  319. END {
  320. nb = split (implibs,list);
  321. for (i=1;i<=nb; i++) {
  322. systemlib = "lib" list[i] ".a";
  323. print "echo Adding system library " systemlib;
  324. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  325. print "if [ \"${systemlib}\" != \"\" ]; then";
  326. print " echo System lib found: ${systemlib}";
  327. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  328. print "else";
  329. print " echo Library " systemlib " not found, shared library assumed";
  330. print "fi";
  331. }
  332. }
  333. ' | tee copy-libs.sh
  334. chmod u+x ./copy-libs.sh
  335. # For later
  336. # Check if mingw executable contains
  337. # __cpu_features_init function
  338. has_cpu_features_init=`objdump -t gdb.exe | grep cpu_features_init `
  339. if [ "X$has_cpu_features_init" == "X" ] ; then
  340. mingw_no_cpu_features_init=1
  341. else
  342. mingw_no_cpu_features_init=0
  343. fi
  344. echo Creating ./gdblib.inc file
  345. # Generate gdblib.inc file
  346. cat comp-cmd.log |${AWK} -v gdbcvs=${gdbcvs} \
  347. -v implibs="${implicitlibs}" -v libdir="${libdir}" \
  348. -v gdbversion=${gdbversion} -v forcestatic=${forcestatic} \
  349. -v force64bitcoreaddr=${force64bitcoreaddr} \
  350. -v has_get_stdin=${has_get_stdin} \
  351. -v mingw_no_cpu_features_init=${mingw_no_cpu_features_init} \
  352. -v add_libgdb=${add_libgdb} '
  353. BEGIN {
  354. use_mingw=0;
  355. print "{ libgdb.inc file generated by awk script }"
  356. print "{$define " gdbversion " }"
  357. if (gdbcvs) {
  358. print "{$define GDB_CVS}"
  359. }
  360. if (force64bitcoreaddr) {
  361. print "{$define GDB_CORE_ADDR_FORCE_64BITS}"
  362. }
  363. print "{$ifdef COMPILING_GDBINT_UNIT }"
  364. if (add_libgdb == 1) {
  365. print "{$LINKLIB libgdb.a} { Added here because Makefile does not use the libgdb library anymore }"
  366. }
  367. }
  368. {
  369. nb = split ($0,list);
  370. for (i=1; i<=nb; i++) {
  371. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  372. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  373. print staticlib;
  374. if ( list[i] ~ /mingw/ ) {
  375. use_mingw=1
  376. }
  377. }
  378. if ( list[i] ~ /^-dynamic-linker$/ ) {
  379. i++;
  380. print "{ Dynamic linker found " list[i] " }";
  381. continue
  382. }
  383. if ( list[i] ~ /-D__USE_MINGW_/ ) {
  384. use_mingw=1
  385. }
  386. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  387. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LINKLIB \\2} { found in \\1 \\3 }","g",list[i]);
  388. librarypath = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LIBRARYPATH \\1} { for \\2 \\3 }","g",list[i]);
  389. print dynamiclib;
  390. print librarypath;
  391. }
  392. if ( list[i] ~ /^-l/ ) {
  393. systemlib = gensub (/-l([^ ]*)/,"\\1","g",list[i]);
  394. if (forcestatic == 1) {
  395. systemlib="lib" systemlib ".a"
  396. }
  397. if ( systemlib ~ /mingw/ ) {
  398. use_mingw=1
  399. }
  400. print "{$LINKLIB " systemlib "} { with -l gcc option}";
  401. }
  402. }
  403. }
  404. END {
  405. print "{ List implicit libraries }"
  406. nb = split (implibs,list);
  407. for (i=1;i<=nb; i++) {
  408. if ( list[i] ~ /lib.*\.a/ ) {
  409. lib=list[i];
  410. } else {
  411. if ( forcestatic == 1 ) {
  412. lib="lib" list[i] ".a";
  413. } else {
  414. lib=list[i];
  415. }
  416. }
  417. print "{$LINKLIB " lib "} { implicit library } "
  418. }
  419. print "{$endif COMPILING_GDBINT_UNIT }"
  420. print "{ List library dirs }"
  421. nb = split (libdir,list);
  422. for (i=1;i<=nb; i++) {
  423. dir=list[i];
  424. print "{$LIBRARYPATH " dir "} { library path } "
  425. print "{$OBJECTPATH " dir "} { library path } "
  426. }
  427. print "{$undef NotImplemented}"
  428. if ( use_mingw == 1 ) {
  429. print "{$define USE_MINGW_GDB}"
  430. if ( mingw_no_cpu_features_init == 1 ) {
  431. print "{$define DISABLE_CPU_FEATURES_INIT}"
  432. }
  433. }
  434. if ( has_get_stdin == 1 ) {
  435. print "{$define LIBGDB_HAS_GET_STDIN}"
  436. }
  437. }
  438. ' | tee gdblib.inc