buildsh.nimf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. --parallel)
  23. parallel=$2
  24. shift 2
  25. ;;
  26. --extraBuildArgs)
  27. extraBuildArgs=" $2"
  28. shift 2
  29. ;;
  30. -h | --help)
  31. echo "Options:"
  32. echo " --os <OS>"
  33. echo " --cpu <CPU architecture>"
  34. echo " --osname <name> Additional OS specification (used for Android)"
  35. echo " --extraBuildArgs <args> Additional arguments passed to the compiler"
  36. echo " --parallel <number> Multiprocess build. Requires GNU parallel"
  37. exit 0
  38. ;;
  39. --) # End of all options
  40. shift
  41. break;
  42. ;;
  43. -*)
  44. echo 2>&1 "Error: Unknown option: $1" >&2
  45. exit 1
  46. ;;
  47. *) # No more options
  48. break
  49. ;;
  50. esac
  51. done
  52. parallel="${parallel:-0}"
  53. CC="${CC:-gcc}"
  54. if [ "$parallel" -gt 1 ]; then
  55. if ! command -v sem > /dev/null; then
  56. echo "Error: GNU parallel is required to use --parallel"
  57. exit 1
  58. fi
  59. CC="sem -j $parallel --id $$ ${CC}"
  60. fi
  61. COMP_FLAGS="${CPPFLAGS:-} ${CFLAGS:-} ?{c.ccompiler.flags}$extraBuildArgs"
  62. LINK_FLAGS="${LDFLAGS:-} ?{c.linker.flags}"
  63. PS4=""
  64. # add(result, "# platform detection\n")
  65. ucpu=`uname -m`
  66. uos=`uname`
  67. uosname=
  68. # add(result, "# bin dir detection\n")
  69. binDir=?{firstBinPath(c).toUnix}
  70. if [ -s ../koch.nim ]; then
  71. binDir="../bin"
  72. fi
  73. if [ ! -d $binDir ]; then
  74. mkdir $binDir
  75. fi
  76. # add(result, "# override OS, CPU and OS Name with command-line arguments\n")
  77. if [ -n "$optos" ]; then
  78. uos="$optos"
  79. fi
  80. if [ -n "$optcpu" ]; then
  81. ucpu="$optcpu"
  82. fi
  83. if [ -n "$optcpu" ]; then
  84. uosname="$optosname"
  85. fi
  86. # add(result, "# convert to lower case:\n")
  87. ucpu=`echo $ucpu | tr "[:upper:]" "[:lower:]"`
  88. uos=`echo $uos | tr "[:upper:]" "[:lower:]"`
  89. uosname=`echo $uosname | tr "[:upper:]" "[:lower:]"`
  90. case $uos in
  91. *linux* )
  92. myos="linux"
  93. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  94. ;;
  95. *dragonfly* )
  96. myos="dragonfly"
  97. LINK_FLAGS="$LINK_FLAGS -lm"
  98. ;;
  99. *freebsd* )
  100. myos="freebsd"
  101. CC="clang"
  102. LINK_FLAGS="$LINK_FLAGS -lm"
  103. ;;
  104. *crossos* )
  105. myos="crossos"
  106. CC="clang"
  107. LINK_FLAGS="$LINK_FLAGS -lm"
  108. ;;
  109. *openbsd* )
  110. myos="openbsd"
  111. CC="clang"
  112. LINK_FLAGS="$LINK_FLAGS -lm"
  113. ;;
  114. *netbsd* )
  115. myos="netbsd"
  116. LINK_FLAGS="$LINK_FLAGS -lm"
  117. ucpu=`uname -p`
  118. ;;
  119. *darwin* )
  120. myos="macosx"
  121. CC="clang"
  122. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  123. if [ "$HOSTTYPE" = "x86_64" ] ; then
  124. ucpu="amd64"
  125. fi
  126. ;;
  127. *aix* )
  128. myos="aix"
  129. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  130. ;;
  131. *solaris* | *sun* )
  132. myos="solaris"
  133. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  134. ;;
  135. *SunOS* )
  136. myos="solaris"
  137. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  138. isOpenIndiana="yes"
  139. ;;
  140. *haiku* )
  141. myos="haiku"
  142. LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
  143. ;;
  144. *mingw* | *msys* )
  145. myos="windows"
  146. ;;
  147. *android* )
  148. myos="android"
  149. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  150. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  151. ;;
  152. *)
  153. echo 2>&1 "Error: unknown operating system: $uos"
  154. exit 1
  155. ;;
  156. esac
  157. case $ucpu in
  158. *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
  159. if [ "$isOpenIndiana" = "yes" ] || [ `uname -o` == "illumos" ] ; then
  160. mycpu="amd64"
  161. else
  162. mycpu="i386"
  163. fi
  164. ;;
  165. *amd*64* | *x86-64* | *x86_64* )
  166. mycpu="amd64" ;;
  167. *sparc*|*sun* )
  168. mycpu="sparc"
  169. if [ "$myos" = "linux" ] ; then
  170. if [ "$(getconf LONG_BIT)" = "64" ]; then
  171. mycpu="sparc64"
  172. elif [ "$(isainfo -b)" = "64" ]; then
  173. mycpu="sparc64"
  174. fi
  175. fi
  176. ;;
  177. *ppc64le* )
  178. mycpu="powerpc64el" ;;
  179. *ppc64* )
  180. if [ "$myos" = "linux" ] ; then
  181. COMP_FLAGS="$COMP_FLAGS -m64"
  182. LINK_FLAGS="$LINK_FLAGS -m64"
  183. fi
  184. mycpu="powerpc64" ;;
  185. *power*|*ppc* )
  186. if [ "$myos" = "freebsd" ] ; then
  187. if [ "$ucpu" != "powerpc" ] ; then
  188. COMP_FLAGS="$COMP_FLAGS -m64"
  189. LINK_FLAGS="$LINK_FLAGS -m64"
  190. fi
  191. mycpu=`uname -p`
  192. case $mycpu in
  193. powerpc64le)
  194. mycpu="powerpc64el"
  195. esac
  196. else
  197. mycpu="powerpc"
  198. fi
  199. ;;
  200. *hppa*)
  201. mycpu="hppa" ;;
  202. *ia64*)
  203. mycpu="ia64" ;;
  204. *m68k*)
  205. mycpu="m68k" ;;
  206. *mips* )
  207. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  208. case $mycpu in
  209. mips|mipsel|mips64|mips64el)
  210. ;;
  211. *)
  212. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  213. exit 1
  214. esac
  215. ;;
  216. *alpha* )
  217. mycpu="alpha" ;;
  218. *aarch64*|*arm64* )
  219. mycpu="arm64" ;;
  220. *arm*|*armv6l*|*armv7l*|*armv8l* )
  221. mycpu="arm" ;;
  222. *riscv64|riscv* )
  223. mycpu="riscv64" ;;
  224. *e2k* )
  225. mycpu="e2k" ;;
  226. *loongarch64* )
  227. mycpu="loongarch64" ;;
  228. *)
  229. echo 2>&1 "Error: unknown processor: $ucpu"
  230. exit 1
  231. ;;
  232. esac
  233. case $uosname in
  234. *android* )
  235. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  236. myosname="android"
  237. myos="android"
  238. ;;
  239. esac
  240. # add(result, "# call the compiler:\n")
  241. echo \# OS: $myos
  242. echo \# CPU: $mycpu
  243. case $myos in
  244. # for osA in 1..c.oses.len:
  245. ?{c.oses[osA-1]})
  246. case $mycpu in
  247. # for cpuA in 1..c.cpus.len:
  248. ?{c.cpus[cpuA-1]})
  249. set -x
  250. # var linkCmd = ""
  251. # for ff in items(c.cfiles[osA][cpuA]):
  252. # let f = ff.toUnix
  253. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  254. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  255. # end for
  256. if [ "$parallel" -gt 0 ]; then
  257. sem --wait --id $$
  258. fi
  259. $CC -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  260. ;;
  261. # end for
  262. *)
  263. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  264. exit 1
  265. ;;
  266. esac
  267. ;;
  268. # end for
  269. *)
  270. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  271. exit 1
  272. ;;
  273. esac
  274. : SUCCESS