gen-gdblib-inc.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env bash
  2. usage ()
  3. {
  4. echo "Script used to easily create collection of libraries needed"
  5. echo "to generate a Free Pascal IDE with debugger support."
  6. echo "Usage: Copy this script to the directory where you just compile"
  7. echo "a specific GNU debugger (for a specific target)"
  8. echo "and run ./$0 in that directory"
  9. echo "After, you will need to run a second script, copy-libs.sh"
  10. echo "with a single parameter specifying to which directory the libraries"
  11. echo "should be copied."
  12. echo "Possible parameters for this script:"
  13. echo "--forcestatic, to convert all -lname into $LINKLIB libname.a"
  14. echo "implicitlibs=\"space separated list if system librairies used\""
  15. }
  16. if [ "$1" == "--help" ]; then
  17. usage
  18. exit
  19. fi
  20. if [ "$1" == "--forcestatic" ]; then
  21. echo "Using only static libraries in gdblib.inc"
  22. forcestatic=1
  23. shift
  24. else
  25. forcestatic=0
  26. fi
  27. if [ "${1#implicitlibs=}" != "$1" ]; then
  28. implicitlibs=${1#implicitlibs=}
  29. echo "Also adding implicit libs \"$implicitlibs\""
  30. shift
  31. fi
  32. if [ "$1" != "" ]; then
  33. echo "Unrecognized option \"$1\""
  34. usage
  35. fi
  36. if [ "${PATHEXT}" != "" ]; then
  37. EXEEXT=.exe
  38. if [ "${DJDIR}" != "" ]; then
  39. libdir=${DJDIR}/lib
  40. else
  41. libdir=/lib
  42. fi
  43. else
  44. EXEEXT=
  45. libdir=/lib
  46. fi
  47. if [ "$OSTYPE" == "msys" ]; then
  48. echo "MSYS system detected"
  49. in_msys=1
  50. fi
  51. echo "Deleting gdb${EXEEXT} to force recompile"
  52. rm -f gdb${EXEEXT}
  53. echo "Rebuilding gdb${EXEEXT}"
  54. MAKE=`which gmake 2> /dev/null`
  55. if [ "${MAKE}" == "" ]; then
  56. MAKE=make
  57. fi
  58. ${MAKE} gdb${EXEEXT} | tee make.log
  59. gdb_full_version=`sed -n "s:.*version.*\"\(.*\)\".*:\1:p" version.c`
  60. gdbcvs=`sed -n "s:.*version.*\"\(.*\)cvs\(.*\)\".*:\1cvs\2:p" version.c`
  61. gdb_version1=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1:p" version.c`
  62. gdb_version2=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\2:p" version.c`
  63. gdb_version=`sed -n "s:.*version.*\"\([0-9]*\)\.\([0-9]*\).*:\1.\2:p" version.c`
  64. echo found full version is ${gdb_full_version}
  65. echo found version is ${gdb_version}
  66. if [ ${gdb_version2} -lt 10 ]; then
  67. gdbversion=${gdb_version1}0${gdb_version2}
  68. else
  69. gdbversion=${gdb_version1}${gdb_version2}
  70. fi
  71. cat make.log | gawk '
  72. BEGIN {
  73. doprint=0
  74. }
  75. # We look for the compilation line
  76. # either gcc or cc
  77. /cc / { doprint=1; }
  78. {
  79. if ( doprint == 1 ) {
  80. print $0
  81. }
  82. }
  83. ! /\\$/ { doprint=0; }
  84. ' | tee comp-cmd.log
  85. gcccompiler=`sed -n "s:\([A-Za-z0-9_-]*gcc\) .*:\1:p" comp-cmd.log`
  86. if [ "$gcccompiler" != "" ]; then
  87. gcclibs=`$gcccompiler -print-search-dirs | sed -n "s#.*libraries: =\(.*\)#\1#p" `
  88. if [ "$gcclibs" != "" ]; then
  89. if [ $in_msys -eq 1 ]; then
  90. # If we are on msys, gcc is mingw, so that it uses c:/dir
  91. # while find is an msys utility that needs /c/dir path
  92. # we do this conversion below
  93. 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
  94. gcclibs=${gcclibs//$let:/\/$let}
  95. done
  96. 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
  97. gcclibs=${gcclibs//$let:/\/$let}
  98. done
  99. libdir=${gcclibs//;/ }
  100. else
  101. # if ; is present in gcclibs,assume this is the separator instead of :
  102. if [ "${gcclibs//;/ }" != "${gcclibs}" ]; then
  103. if [ "${gcclibs// /_}" != "${gccclibs}" ]; then
  104. # list also contains spaces, convert ' ' into '\ '
  105. gcclibs=${gcclibs// /\\ }
  106. fi
  107. libdir=${gcclibs//;/ }
  108. else
  109. libdir=${gcclibs//:/ }
  110. fi
  111. fi
  112. echo "gcc libs are \"$libdir\""
  113. fi
  114. fi
  115. # Try to locate all libraries
  116. echo Creating ./copy-libs.sh script
  117. cat comp-cmd.log | gawk -v libdir="${libdir}" -v implibs="${implicitlibs}" '
  118. BEGIN {
  119. print "#!/usr/bin/env bash"
  120. print "# copy-libs.sh generated by awk script"
  121. print "INSTALL=`which ginstall 2> /dev/null `"
  122. print "if [ "$INSTALL" == "" ]; then"
  123. print " INSTALL=install"
  124. print "fi"
  125. print "if [ \"$1\" != \"\" ]; then"
  126. print " destdir=$1"
  127. print " $INSTALL -d ${destdir}"
  128. print "else"
  129. print " echo $0 destdir"
  130. print " echo destdir should be the location where libgdb.a"
  131. print " echo and all other archives should be copied"
  132. print " exit"
  133. print "fi"
  134. print "# Copy gdblib.inc file"
  135. print "cp -p gdblib.inc ${destdir}"
  136. }
  137. {
  138. nb = split ($0,list);
  139. for (i=1; i<=nb; i++) {
  140. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  141. print "# Looking for static libs"
  142. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"\\1\\2 ","g",list[i]);
  143. print "cp -p " staticlib " ${destdir}";
  144. }
  145. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  146. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"\\1\\2 ","g",list[i]);
  147. print "echo " dynamiclib " found";
  148. }
  149. if ( list[i] ~ /-l/ ) {
  150. print "#Looking for shared libs"
  151. systemlib = gensub (/-l([^ ]*)/,"lib\\1.a ","g",list[i]);
  152. print "systemlib=`find " libdir " -name " systemlib " -print -quit 2> /dev/null `" ;
  153. print "if [ \"${systemlib}\" != \"\" ]; then";
  154. print " echo System lib found: ${systemlib}";
  155. print " cp -p ${systemlib} ${destdir}";
  156. print "else";
  157. print " echo Library " systemlib " not found, shared library assumed";
  158. print "fi";
  159. }
  160. }
  161. }
  162. END {
  163. nb = split (implibs,list);
  164. for (i=1;i<=nb; i++) {
  165. systemlib = "lib" list[i] ".a";
  166. print "echo Adding system library " systemlib;
  167. print "systemlib=`find " libdir " -name " systemlib " -print -quit 2> /dev/null `" ;
  168. print "if [ \"${systemlib}\" != \"\" ]; then";
  169. print " echo System lib found: ${systemlib}";
  170. print " cp -p ${systemlib} ${destdir}";
  171. print "else";
  172. print " echo Library " systemlib " not found, shared library assumed";
  173. print "fi";
  174. }
  175. }
  176. ' | tee copy-libs.sh
  177. chmod u+x ./copy-libs.sh
  178. # For later
  179. echo Creating ./gdblib.inc file
  180. # Generate gdblib.inc file
  181. cat comp-cmd.log |gawk -v gdbcvs=${gdbcvs} -v implibs="${implicitlibs}" \
  182. -v gdbversion=${gdbversion} -v forcestatic=${forcestatic} '
  183. BEGIN {
  184. use_mingw=0;
  185. print "{ libgdb.inc file generated by awk script }"
  186. print "{$define GDB_V" gdbversion " }"
  187. if (gdbcvs) {
  188. print "{$define GDB_CVS}"
  189. }
  190. print "{$ifdef COMPILING_GDBINT_UNIT }"
  191. }
  192. {
  193. nb = split ($0,list);
  194. for (i=1; i<=nb; i++) {
  195. if ( list[i] ~ /lib[^ ]*\.a/ ) {
  196. staticlib = gensub (/([^ ]*)(lib[^ ]*\.a)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  197. print staticlib;
  198. if ( list[i] ~/mingw/ ) {
  199. use_mingw=1
  200. }
  201. }
  202. if ( list[i] ~ /-D__USE_MINGW_/ ) {
  203. use_mingw=1
  204. }
  205. if ( list[i] ~ /lib[^ ]*\.so/ ) {
  206. dynamiclib = gensub (/([^ ]*)(lib[^ ]*\.so)/,"{$LINKLIB \\2} { found in \\1 }","g",list[i]);
  207. librarypath = gensub (/([^ ]*)(lib[^ ]*\.so)/,"{$LIBRARYPATH \\1} { for \\2 }","g",list[i]);
  208. print dynamiclib;
  209. print librarypath;
  210. }
  211. if ( list[i] ~ /-l/ ) {
  212. systemlib = gensub (/-l([^ ]*)/,"\\1","g",list[i]);
  213. if (forcestatic == 1) {
  214. systemlib="lib" systemlib ".a"
  215. }
  216. print "{$LINKLIB " systemlib "} { with -l gcc option}";
  217. }
  218. }
  219. }
  220. END {
  221. nb = split (implibs,list);
  222. for (i=1;i<=nb; i++) {
  223. if ( list[i] ~ /lib.*\.a/ ) {
  224. lib=list[i];
  225. } else {
  226. if ( forcestatic == 1 ) {
  227. lib="lib" list[i] ".a";
  228. } else {
  229. lib=list[i];
  230. }
  231. }
  232. print "{$LINKLIB " lib "} { implicit library } "
  233. }
  234. print "{$endif COMPILING_GDBINT_UNIT }"
  235. print "{$undef NotImplemented}"
  236. if ( use_mingw == 1 ) {
  237. print "{$define USE_MINGW_GDB}"
  238. }
  239. }
  240. ' | tee gdblib.inc