gen-gdblib-inc.sh 14 KB

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