gen-gdblib-inc.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 GNU make and GNU awk
  19. MAKE=`which gmake 2> /dev/null`
  20. AWK=`which gawk 2> /dev/null`
  21. # Assume make is OK if gmake is not found
  22. if [ "${MAKE}" == "" ]; then
  23. MAKE=make
  24. fi
  25. # Assume awk is OK if gawk is not found
  26. if [ "${AWK}" == "" ]; then
  27. AWK=awk
  28. fi
  29. # Possible extra option to pass when rebuilding gdb executable below
  30. MAKEOPT=
  31. if [ "$1" == "--help" ]; then
  32. usage
  33. exit
  34. fi
  35. forcestatic=0
  36. # Function to treat all command line option
  37. handle_option ()
  38. {
  39. opt_handled=0
  40. if [ "$1" == "" ] ; then
  41. return
  42. fi
  43. if [ "$1" == "--forcestatic" ]; then
  44. echo "Using only static libraries in gdblib.inc"
  45. forcestatic=1
  46. MAKEOPT="LDFLAGS=-static"
  47. opt_handled=1
  48. fi
  49. if [ "${1#implicitlibs=}" != "$1" ]; then
  50. implicitlibs=${1#implicitlibs=}
  51. echo "Also adding implicit libs \"$implicitlibs\""
  52. opt_handled=1
  53. fi
  54. if [ "${1#libdir=}" != "$1" ]; then
  55. libdir=${1#libdir=}
  56. echo "libdir is set to \"$libdir\""
  57. opt_handled=1
  58. fi
  59. }
  60. # Try to handle all command line options
  61. opt_handled=1
  62. while [ $opt_handled -eq 1 ]
  63. do
  64. handle_option "$1"
  65. if [ $opt_handled -eq 1 ] ; then
  66. shift
  67. fi
  68. done
  69. if [ "$1" != "" ]; then
  70. echo "Unrecognized option \"$1\""
  71. usage
  72. fi
  73. if [ "${PATHEXT}" != "" ]; then
  74. EXEEXT=.exe
  75. if [ "${DJDIR}" != "" ]; then
  76. libdir=${DJDIR}/lib
  77. else
  78. libdir=/lib
  79. fi
  80. else
  81. EXEEXT=
  82. if [ "$libdir" == "" ]; then
  83. libdir=/lib
  84. fi
  85. fi
  86. if [ "$OSTYPE" == "msys" ]; then
  87. echo "MSYS system detected"
  88. in_msys=1
  89. else
  90. in_msys=0
  91. fi
  92. echo "Deleting gdb${EXEEXT} to force recompile"
  93. rm -f gdb${EXEEXT}
  94. echo "Rebuilding gdb${EXEEXT}"
  95. ${MAKE} gdb${EXEEXT} ${MAKEOPT} | tee make.log
  96. # libgdb.a will not be built automatically anymore after
  97. # GDB release 7.4, so we need to explicitly make it.
  98. echo "Rebuilding GDB library if needed"
  99. ${MAKE} libgdb.a ${MAKEOPT}
  100. # version.c is an automatically generated file from gdb/version.in
  101. # We extract GDB version from that file.
  102. gdb_full_version=`sed -n "s:.*version.*\"\(.*\)\".*:\1:p" version.c`
  103. gdbcvs=`sed -n "s:.*version.*\"\(.*\)cvs\(.*\)\".*:\1cvs\2:p" version.c`
  104. gdb_version1=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1:p" version.c`
  105. gdb_version2=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\2:p" version.c`
  106. gdb_version=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1.\2:p" version.c`
  107. echo found full version is ${gdb_full_version}
  108. echo found version is ${gdb_version}
  109. if [ ${gdb_version2} -lt 10 ]; then
  110. gdbversion=${gdb_version1}0${gdb_version2}
  111. else
  112. gdbversion=${gdb_version1}${gdb_version2}
  113. fi
  114. cat make.log | ${AWK} '
  115. BEGIN {
  116. doprint=0
  117. }
  118. # We look for the compilation line
  119. # either gcc or cc
  120. /cc / { doprint=1; }
  121. {
  122. if ( doprint == 1 ) {
  123. print $0
  124. }
  125. }
  126. ! /\\$/ { doprint=0; }
  127. ' | tee comp-cmd.log
  128. gcccompiler=`sed -n "s:\([A-Za-z0-9_-]*gcc\) .*:\1:p" comp-cmd.log`
  129. if [ "$gcccompiler" != "" ]; then
  130. gcclibs=`$gcccompiler -print-search-dirs | sed -n "s#.*libraries: =\(.*\)#\1#p" `
  131. if [ "$gcclibs" != "" ]; then
  132. if [ $in_msys -eq 1 ]; then
  133. # If we are on msys, gcc is mingw, so that it uses c:/dir
  134. # while find is an msys utility that needs /c/dir path
  135. # we do this conversion below
  136. 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
  137. gcclibs=${gcclibs//$let:/\/$let}
  138. done
  139. 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
  140. gcclibs=${gcclibs//$let:/\/$let}
  141. done
  142. libdir="$libdir ${gcclibs//;/ }"
  143. else
  144. # if ; is present in gcclibs,assume this is the separator instead of :
  145. if [ "${gcclibs//;/ }" != "${gcclibs}" ]; then
  146. if [ "${gcclibs// /_}" != "${gccclibs}" ]; then
  147. # list also contains spaces, convert ' ' into '\ '
  148. gcclibs=${gcclibs// /\\ }
  149. fi
  150. libdir="$libdir ${gcclibs//;/ }"
  151. else
  152. libdir="$libdir ${gcclibs//:/ }"
  153. fi
  154. fi
  155. echo "gcc libs are \"$libdir\""
  156. fi
  157. fi
  158. # Try to locate all libraries
  159. echo Creating ./copy-libs.sh script
  160. cat comp-cmd.log | ${AWK} -v libdir="${libdir}" -v implibs="${implicitlibs}" '
  161. BEGIN {
  162. print "#!/usr/bin/env bash"
  163. print "# copy-libs.sh generated by awk script"
  164. print "INSTALL=`which ginstall 2> /dev/null `"
  165. print "if [ "$INSTALL" == "" ]; then"
  166. print " INSTALL=install"
  167. print "fi"
  168. print "if [ \"$1\" != \"\" ]; then"
  169. print " destdir=$1"
  170. print " $INSTALL -d ${destdir}"
  171. print "else"
  172. print " echo $0 destdir"
  173. print " echo destdir should be the location where libgdb.a"
  174. print " echo and all other archives should be copied"
  175. print " exit"
  176. print "fi"
  177. print "# Copy gdblib.inc file"
  178. print "cp -p gdblib.inc ${destdir}"
  179. }
  180. {
  181. nb = split ($0,list);
  182. for (i=1; i<=nb; i++) {
  183. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  184. print "# Looking for static libs"
  185. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"\\1\\2 ","g",list[i]);
  186. print "cp -p " staticlib " ${destdir}";
  187. }
  188. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  189. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"\\1\\2 ","g",list[i]);
  190. print "echo " dynamiclib " found";
  191. }
  192. if ( list[i] ~ /^-l/ ) {
  193. print "#Looking for shared libs"
  194. systemlib = gensub (/-l([^ ]*)/,"lib\\1.a ","g",list[i]);
  195. print "systemlib=`find " libdir " -iname " systemlib " -print 2> /dev/null `" ;
  196. print "if [ \"${systemlib}\" != \"\" ]; then";
  197. print " echo System lib found: ${systemlib%%[$IFS]*}";
  198. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  199. print "else";
  200. print " echo Library " systemlib " not found, shared library assumed";
  201. print "fi";
  202. }
  203. }
  204. }
  205. END {
  206. nb = split (implibs,list);
  207. for (i=1;i<=nb; i++) {
  208. systemlib = "lib" list[i] ".a";
  209. print "echo Adding system library " systemlib;
  210. print "systemlib=`find " libdir " -iname " systemlib " -print 2> /dev/null `" ;
  211. print "if [ \"${systemlib}\" != \"\" ]; then";
  212. print " echo System lib found: ${systemlib%%[$IFS]*}";
  213. print " cp -p ${systemlib%%[$IFS]*} ${destdir}";
  214. print "else";
  215. print " echo Library " systemlib " not found, shared library assumed";
  216. print "fi";
  217. }
  218. }
  219. ' | tee copy-libs.sh
  220. chmod u+x ./copy-libs.sh
  221. # For later
  222. echo Creating ./gdblib.inc file
  223. # Generate gdblib.inc file
  224. cat comp-cmd.log |${AWK} -v gdbcvs=${gdbcvs} -v implibs="${implicitlibs}" \
  225. -v gdbversion=${gdbversion} -v forcestatic=${forcestatic} '
  226. BEGIN {
  227. use_mingw=0;
  228. print "{ libgdb.inc file generated by awk script }"
  229. print "{$define GDB_V" gdbversion " }"
  230. if (gdbcvs) {
  231. print "{$define GDB_CVS}"
  232. }
  233. print "{$ifdef COMPILING_GDBINT_UNIT }"
  234. }
  235. {
  236. nb = split ($0,list);
  237. for (i=1; i<=nb; i++) {
  238. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  239. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  240. print staticlib;
  241. if ( list[i] ~/mingw/ ) {
  242. use_mingw=1
  243. }
  244. }
  245. if ( list[i] ~ /-D__USE_MINGW_/ ) {
  246. use_mingw=1
  247. }
  248. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  249. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  250. librarypath = gensub (/([^ ]*)(lib[^ ]*\.so)/,"{$LIBRARYPATH \\1} { for \\2 }","g",list[i]);
  251. print dynamiclib;
  252. print librarypath;
  253. }
  254. if ( list[i] ~ /-l/ ) {
  255. systemlib = gensub (/-l([^ ]*)/,"\\1","g",list[i]);
  256. if (forcestatic == 1) {
  257. systemlib="lib" systemlib ".a"
  258. }
  259. print "{$LINKLIB " systemlib "} { with -l gcc option}";
  260. }
  261. }
  262. }
  263. END {
  264. nb = split (implibs,list);
  265. for (i=1;i<=nb; i++) {
  266. if ( list[i] ~ /lib.*\.a/ ) {
  267. lib=list[i];
  268. } else {
  269. if ( forcestatic == 1 ) {
  270. lib="lib" list[i] ".a";
  271. } else {
  272. lib=list[i];
  273. }
  274. }
  275. print "{$LINKLIB " lib "} { implicit library } "
  276. }
  277. print "{$endif COMPILING_GDBINT_UNIT }"
  278. print "{$undef NotImplemented}"
  279. if ( use_mingw == 1 ) {
  280. print "{$define USE_MINGW_GDB}"
  281. }
  282. }
  283. ' | tee gdblib.inc