gen-gdblib-inc.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 *
  176. gdb_get_stdin (void)
  177. {
  178. return stdin;
  179. }
  180. EOF
  181. echo "Trying to compile gdb_get_stdin.c file"
  182. ${MAKE} gdb_get_stdin.o
  183. res=$?
  184. if [ $res -eq 0 ] ; then
  185. XM_ADD_FILES=gdb_get_stdin.o
  186. has_get_stdin=1
  187. else
  188. has_get_stdin=0
  189. fi
  190. # libgdb.a will not be built automatically anymore after
  191. # GDB release 7.4, so we need to explicitly generate it.
  192. if [ -f libgdb.a ] ; then
  193. rm -f libgdb.a
  194. fi
  195. echo "Rebuilding GDB library to include gdb_get_stdin.o"
  196. ${MAKE} libgdb.a ${MAKEOPT} XM_ADD_FILES=${XM_ADD_FILES}
  197. # version.c is an automatically generated file from gdb/version.in
  198. # We extract GDB version from that file.
  199. gdb_full_version=`sed -n "s:.*version.*\"\(.*\)\".*:\1:p" version.c`
  200. gdbcvs=`sed -n "s:.*version.*\"\(.*\)cvs\(.*\)\".*:\1cvs\2:p" version.c`
  201. gdb_version1=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1:p" version.c`
  202. gdb_version2=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\2:p" version.c`
  203. gdb_version=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1.\2:p" version.c`
  204. echo "found GDB full version is ${gdb_full_version}"
  205. echo "found GDB version is ${gdb_version}"
  206. if [ ${gdb_version2} -lt 10 ]; then
  207. gdbversion=GDB_V${gdb_version1}0${gdb_version2}
  208. else
  209. gdbversion=GDB_V${gdb_version1}${gdb_version2}
  210. fi
  211. echo "Using macro $gdbversion"
  212. make_log_has_collect2=`grep collect2 make.log`
  213. if [ "x$make_log_has_collect2" != "x" ] ; then
  214. find_cmd=collect2
  215. else
  216. find_cmd=cc
  217. fi
  218. cat make.log | ${AWK} -v find_cmd=$find_cmd '
  219. BEGIN {
  220. doprint=0
  221. }
  222. # We look for the compilation line
  223. # either gcc or cc
  224. $0 ~ find_cmd { doprint=1; }
  225. {
  226. if ( doprint == 1 ) {
  227. print $0
  228. }
  229. }
  230. ! /\\$/ { doprint=0; }
  231. ' | tee comp-cmd.log
  232. if [ "x$MAKE_CC" = "x" ] ; then
  233. gcccompiler=`sed -n "s:\([A-Za-z0-9_-]*gcc\) .*:\1:p" comp-cmd.log`
  234. else
  235. gcccompiler=$MAKE_CC
  236. fi
  237. if [ "$gcccompiler" != "" ]; then
  238. gcclibs=`$gcccompiler -print-search-dirs | sed -n "s#.*libraries: =\(.*\)#\1#p" `
  239. if [ "$gcclibs" != "" ]; then
  240. if [ $in_msys -eq 1 ]; then
  241. # If we are on msys, gcc is mingw, so that it uses c:/dir
  242. # while find is an msys utility that needs /c/dir path
  243. # we do this conversion below
  244. 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
  245. gcclibs=${gcclibs//$let:/\/$let}
  246. done
  247. 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
  248. gcclibs=${gcclibs//$let:/\/$let}
  249. done
  250. libdir="$libdir ${gcclibs//;/ }"
  251. else
  252. # if ; is present in gcclibs,assume this is the separator instead of :
  253. if [ "${gcclibs//;/ }" != "${gcclibs}" ]; then
  254. if [ "${gcclibs// /_}" != "${gccclibs}" ]; then
  255. # list also contains spaces, convert ' ' into '\ '
  256. gcclibs=${gcclibs// /\\ }
  257. fi
  258. libdir="$libdir ${gcclibs//;/ }"
  259. else
  260. libdir="$libdir ${gcclibs//:/ }"
  261. fi
  262. fi
  263. echo "gcc libs are \"$libdir\""
  264. fi
  265. fi
  266. newlibdir=
  267. for dir in $libdir ; do
  268. echo "Handling dir $dir"
  269. newdir=`cd $dir 2> /dev/null && pwd -P`
  270. if [ "X$newdir" != "X" ] ; then
  271. adddir=1
  272. for rdir in $removedir ; do
  273. if [ "$rdir" == "$newdir" ] ; then
  274. adddir=0
  275. fi
  276. done
  277. if [ $adddir -eq 1 ] ; then
  278. newlibdir="$newlibdir $newdir"
  279. echo "Adding $dir as $newdir"
  280. fi
  281. else
  282. echo "$dir not found"
  283. fi
  284. done
  285. libdir="$newlibdir"
  286. # Try to locate all libraries
  287. echo Creating ./copy-libs.sh script
  288. has_libgdb=`cat comp-cmd.log | grep "libgdb\.a"`
  289. if [ "x$has_libgdb" != "x" ] ; then
  290. add_libgdb=0
  291. else
  292. add_libgdb=1
  293. fi
  294. cat comp-cmd.log | ${AWK} -v libdir="${libdir}" -v implibs="${implicitlibs}" \
  295. -v add_libgdb=${add_libgdb} '
  296. BEGIN {
  297. isdynlinker=0
  298. print "#!/usr/bin/env bash"
  299. print "# copy-libs.sh generated by awk script"
  300. print "INSTALL=`which ginstall 2> /dev/null `"
  301. print "if [ "$INSTALL" == "" ]; then"
  302. print " INSTALL=install"
  303. print "fi"
  304. print "if [ \"$1\" != \"\" ]; then"
  305. print " destdir=$1"
  306. print " $INSTALL -d ${destdir}"
  307. print "else"
  308. print " echo $0 destdir"
  309. print " echo destdir should be the location where libgdb.a"
  310. print " echo and all other archives should be copied"
  311. print " exit"
  312. print "fi"
  313. print "libdir=\"" libdir "\""
  314. print "# Copy gdblib.inc file"
  315. print "cp -p gdblib.inc ${destdir}"
  316. if (add_libgdb == 1) {
  317. print "# Adding libgdb.a"
  318. print "cp -p libgdb.a ${destdir}"
  319. }
  320. }
  321. {
  322. nb = split ($0,list);
  323. for (i=1; i<=nb; i++) {
  324. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  325. print "# Looking for static libs"
  326. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"\\1\\2 ","g",list[i]);
  327. print "cp -p " staticlib " ${destdir}";
  328. }
  329. if ( list[i] ~ /^-dynamic-linker$/ ) {
  330. i++;
  331. print "echo dynamic linker " list[i] " skipped";
  332. continue
  333. }
  334. if ( list[i] ~ /^-plugin$/ ) {
  335. i++;
  336. print "echo collect2 -plugin " list[i] " skipped";
  337. continue
  338. }
  339. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  340. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"\\1\\2 ","g",list[i]);
  341. print "echo " dynamiclib " found";
  342. }
  343. if ( list[i] ~ /^-l/ ) {
  344. print "#Looking for shared libs"
  345. systemlib = gensub (/-l([^ ]*)/,"lib\\1.a ","g",list[i]);
  346. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  347. print "if [ \"${systemlib}\" != \"\" ]; then";
  348. print " echo System lib found: ${systemlib}";
  349. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  350. print "else";
  351. print " echo Library " systemlib " not found, shared library assumed";
  352. print "fi";
  353. }
  354. }
  355. }
  356. END {
  357. nb = split (implibs,list);
  358. for (i=1;i<=nb; i++) {
  359. systemlib = "lib" list[i] ".a";
  360. print "echo Adding system library " systemlib;
  361. print "systemlib=`find $libdir -maxdepth 1 -iname " systemlib " -print 2> /dev/null `" ;
  362. print "if [ \"${systemlib}\" != \"\" ]; then";
  363. print " echo System lib found: ${systemlib}";
  364. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  365. print "else";
  366. print " echo Library " systemlib " not found, shared library assumed";
  367. print "fi";
  368. }
  369. }
  370. ' | tee copy-libs.sh
  371. chmod u+x ./copy-libs.sh
  372. # For later
  373. # Check if mingw executable contains
  374. # __cpu_features_init function
  375. if [ -f gdb.exe ] ; then
  376. has_cpu_features_init=`objdump -t gdb.exe | grep cpu_features_init `
  377. if [ "X$has_cpu_features_init" == "X" ] ; then
  378. mingw_no_cpu_features_init=1
  379. else
  380. mingw_no_cpu_features_init=0
  381. fi
  382. else
  383. mingw_no_cpu_features_init=0
  384. fi
  385. echo Creating ./gdblib.inc file
  386. # Generate gdblib.inc file
  387. cat comp-cmd.log |${AWK} -v gdbcvs=${gdbcvs} \
  388. -v implibs="${implicitlibs}" -v libdir="${libdir}" \
  389. -v gdbversion=${gdbversion} -v forcestatic=${forcestatic} \
  390. -v force64bitcoreaddr=${force64bitcoreaddr} \
  391. -v has_get_stdin=${has_get_stdin} \
  392. -v mingw_no_cpu_features_init=${mingw_no_cpu_features_init} \
  393. -v add_libgdb=${add_libgdb} '
  394. BEGIN {
  395. use_mingw=0;
  396. print "{ libgdb.inc file generated by awk script }"
  397. print "{$define " gdbversion " }"
  398. if (gdbcvs) {
  399. print "{$define GDB_CVS}"
  400. }
  401. if (force64bitcoreaddr) {
  402. print "{$define GDB_CORE_ADDR_FORCE_64BITS}"
  403. }
  404. print "{$ifdef COMPILING_GDBINT_UNIT }"
  405. if (add_libgdb == 1) {
  406. print "{$LINKLIB libgdb.a} { Added here because Makefile does not use the libgdb library anymore }"
  407. }
  408. }
  409. {
  410. nb = split ($0,list);
  411. for (i=1; i<=nb; i++) {
  412. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  413. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  414. print staticlib;
  415. if ( list[i] ~ /mingw/ ) {
  416. use_mingw=1
  417. }
  418. }
  419. if ( list[i] ~ /^-dynamic-linker$/ ) {
  420. i++;
  421. print "{ Dynamic linker found " list[i] " }";
  422. continue
  423. }
  424. if ( list[i] ~ /^-plugin$/ ) {
  425. i++;
  426. print "{ collect2 -plugin " list[i] " ignored }";
  427. continue
  428. }
  429. if ( list[i] ~ /-D__USE_MINGW_/ ) {
  430. use_mingw=1
  431. }
  432. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  433. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LINKLIB \\2} { found in \\1 \\3 }","g",list[i]);
  434. librarypath = gensub (/([^ ]*)(lib[^ ]*\.so[^ ]*)(.*)/,"{$LIBRARYPATH \\1} { for \\2 \\3 }","g",list[i]);
  435. print dynamiclib;
  436. print librarypath;
  437. }
  438. if ( list[i] ~ /^-l/ ) {
  439. systemlib = gensub (/-l([^ ]*)/,"\\1","g",list[i]);
  440. if (forcestatic == 1) {
  441. systemlib="lib" systemlib ".a"
  442. }
  443. if ( systemlib ~ /mingw/ ) {
  444. use_mingw=1
  445. }
  446. print "{$LINKLIB " systemlib "} { with -l gcc option}";
  447. }
  448. }
  449. }
  450. END {
  451. print "{ List implicit libraries }"
  452. nb = split (implibs,list);
  453. for (i=1;i<=nb; i++) {
  454. if ( list[i] ~ /lib.*\.a/ ) {
  455. lib=list[i];
  456. } else {
  457. if ( forcestatic == 1 ) {
  458. lib="lib" list[i] ".a";
  459. } else {
  460. lib=list[i];
  461. }
  462. }
  463. print "{$LINKLIB " lib "} { implicit library } "
  464. }
  465. print "{$endif COMPILING_GDBINT_UNIT }"
  466. print "{ List library dirs }"
  467. nb = split (libdir,list);
  468. for (i=1;i<=nb; i++) {
  469. dir=list[i];
  470. print "{$LIBRARYPATH " dir "} { library path } "
  471. print "{$OBJECTPATH " dir "} { library path } "
  472. }
  473. print "{$undef NotImplemented}"
  474. if ( use_mingw == 1 ) {
  475. print "{$define USE_MINGW_GDB}"
  476. if ( mingw_no_cpu_features_init == 1 ) {
  477. print "{$define DISABLE_CPU_FEATURES_INIT}"
  478. }
  479. }
  480. if ( has_get_stdin == 1 ) {
  481. print "{$define LIBGDB_HAS_GET_STDIN}"
  482. }
  483. }
  484. ' | tee gdblib.inc