buildsh.nimf 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #? stdtmpl(subsChar='?') | standard
  2. #proc generateBuildShellScript(c: ConfigData): string =
  3. # result = "#! /bin/sh\n# Generated from niminst\n" &
  4. # "# Template is in tools/niminst/buildsh.nimf\n" &
  5. # "# To regenerate run ``niminst csource`` or ``koch csource``\n"
  6. set -e
  7. while :
  8. do
  9. case "$1" in
  10. --os)
  11. optos=$2
  12. shift 2
  13. ;;
  14. --cpu)
  15. optcpu=$2
  16. shift 2
  17. ;;
  18. --osname)
  19. optosname=$2
  20. shift 2
  21. ;;
  22. --extraBuildArgs)
  23. extraBuildArgs=" $2"
  24. shift 2
  25. ;;
  26. --) # End of all options
  27. shift
  28. break;
  29. ;;
  30. -*)
  31. echo 2>&1 "Error: Unknown option: $1" >&2
  32. exit 1
  33. ;;
  34. *) # No more options
  35. break
  36. ;;
  37. esac
  38. done
  39. CC="${CC:-gcc}"
  40. COMP_FLAGS="${CPPFLAGS:-} ${CFLAGS:-} ?{c.ccompiler.flags}$extraBuildArgs"
  41. LINK_FLAGS="${LDFLAGS:-} ?{c.linker.flags}"
  42. PS4=""
  43. # add(result, "# platform detection\n")
  44. ucpu=`uname -m`
  45. uos=`uname`
  46. uosname=
  47. # add(result, "# bin dir detection\n")
  48. binDir=?{firstBinPath(c).toUnix}
  49. if [ -s ../koch.nim ]; then
  50. binDir="../bin"
  51. fi
  52. if [ ! -d $binDir ]; then
  53. mkdir $binDir
  54. fi
  55. # add(result, "# override OS, CPU and OS Name with command-line arguments\n")
  56. if [ -n "$optos" ]; then
  57. uos="$optos"
  58. fi
  59. if [ -n "$optcpu" ]; then
  60. ucpu="$optcpu"
  61. fi
  62. if [ -n "$optcpu" ]; then
  63. uosname="$optosname"
  64. fi
  65. # add(result, "# convert to lower case:\n")
  66. ucpu=`echo $ucpu | tr "[:upper:]" "[:lower:]"`
  67. uos=`echo $uos | tr "[:upper:]" "[:lower:]"`
  68. uosname=`echo $uosname | tr "[:upper:]" "[:lower:]"`
  69. case $uos in
  70. *linux* )
  71. myos="linux"
  72. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  73. ;;
  74. *dragonfly* )
  75. myos="dragonfly"
  76. LINK_FLAGS="$LINK_FLAGS -lm"
  77. ;;
  78. *freebsd* )
  79. myos="freebsd"
  80. CC="clang"
  81. LINK_FLAGS="$LINK_FLAGS -lm"
  82. ;;
  83. *openbsd* )
  84. myos="openbsd"
  85. CC="clang"
  86. LINK_FLAGS="$LINK_FLAGS -lm"
  87. ;;
  88. *netbsd* )
  89. myos="netbsd"
  90. LINK_FLAGS="$LINK_FLAGS -lm"
  91. ;;
  92. *darwin* )
  93. myos="macosx"
  94. CC="clang"
  95. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  96. if [ "$HOSTTYPE" = "x86_64" ] ; then
  97. ucpu="amd64"
  98. fi
  99. ;;
  100. *aix* )
  101. myos="aix"
  102. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  103. ;;
  104. *solaris* | *sun* )
  105. myos="solaris"
  106. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  107. ;;
  108. *haiku* )
  109. myos="haiku"
  110. LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
  111. ;;
  112. *mingw* | *msys* )
  113. myos="windows"
  114. ;;
  115. *android* )
  116. myos="android"
  117. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  118. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  119. ;;
  120. *)
  121. echo 2>&1 "Error: unknown operating system: $uos"
  122. exit 1
  123. ;;
  124. esac
  125. case $ucpu in
  126. *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
  127. mycpu="i386" ;;
  128. *amd*64* | *x86-64* | *x86_64* )
  129. mycpu="amd64" ;;
  130. *sparc*|*sun* )
  131. mycpu="sparc"
  132. if [ "$myos" = "linux" ] ; then
  133. if [ "$(getconf LONG_BIT)" = "64" ]; then
  134. mycpu="sparc64"
  135. elif [ "$(isainfo -b)" = "64" ]; then
  136. mycpu="sparc64"
  137. fi
  138. fi
  139. ;;
  140. *ppc64le* )
  141. mycpu="powerpc64el" ;;
  142. *ppc64* )
  143. if [ "$myos" = "linux" ] ; then
  144. COMP_FLAGS="$COMP_FLAGS -m64"
  145. LINK_FLAGS="$LINK_FLAGS -m64"
  146. fi
  147. mycpu="powerpc64" ;;
  148. *power*|*ppc* )
  149. if [ "$myos" = "freebsd" ] ; then
  150. COMP_FLAGS="$COMP_FLAGS -m64"
  151. LINK_FLAGS="$LINK_FLAGS -m64"
  152. mycpu=`uname -p`
  153. case $mycpu in
  154. powerpc64le)
  155. mycpu="powerpc64el"
  156. esac
  157. else
  158. mycpu="powerpc"
  159. fi
  160. ;;
  161. *hppa*)
  162. mycpu="hppa" ;;
  163. *ia64*)
  164. mycpu="ia64" ;;
  165. *m68k*)
  166. mycpu="m68k" ;;
  167. *mips* )
  168. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  169. case $mycpu in
  170. mips|mipsel|mips64|mips64el)
  171. ;;
  172. *)
  173. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  174. exit 1
  175. esac
  176. ;;
  177. *alpha* )
  178. mycpu="alpha" ;;
  179. *aarch64*|*arm64* )
  180. mycpu="arm64" ;;
  181. *arm*|*armv6l*|*armv71* )
  182. mycpu="arm" ;;
  183. *riscv64|riscv* )
  184. mycpu="riscv64" ;;
  185. *)
  186. echo 2>&1 "Error: unknown processor: $ucpu"
  187. exit 1
  188. ;;
  189. esac
  190. case $uosname in
  191. *android* )
  192. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  193. myosname="android"
  194. myos="android"
  195. ;;
  196. esac
  197. # add(result, "# call the compiler:\n")
  198. echo \# OS: $myos
  199. echo \# CPU: $mycpu
  200. case $myos in
  201. # for osA in 1..c.oses.len:
  202. ?{c.oses[osA-1]})
  203. case $mycpu in
  204. # for cpuA in 1..c.cpus.len:
  205. ?{c.cpus[cpuA-1]})
  206. set -x
  207. # var linkCmd = ""
  208. # for ff in items(c.cfiles[osA][cpuA]):
  209. # let f = ff.toUnix
  210. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  211. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  212. # end for
  213. $CC -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  214. ;;
  215. # end for
  216. *)
  217. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  218. exit 1
  219. ;;
  220. esac
  221. ;;
  222. # end for
  223. *)
  224. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  225. exit 1
  226. ;;
  227. esac
  228. : SUCCESS