gen-gdblib-inc.sh 11 KB

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