buildsh.nimf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. ;;
  118. *darwin* )
  119. myos="macosx"
  120. CC="clang"
  121. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  122. if [ "$HOSTTYPE" = "x86_64" ] ; then
  123. ucpu="amd64"
  124. fi
  125. ;;
  126. *aix* )
  127. myos="aix"
  128. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  129. ;;
  130. *solaris* | *sun* )
  131. myos="solaris"
  132. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  133. ;;
  134. *SunOS* )
  135. myos="solaris"
  136. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  137. isOpenIndiana="yes"
  138. ;;
  139. *haiku* )
  140. myos="haiku"
  141. LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
  142. ;;
  143. *mingw* | *msys* )
  144. myos="windows"
  145. ;;
  146. *android* )
  147. myos="android"
  148. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  149. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  150. ;;
  151. *)
  152. echo 2>&1 "Error: unknown operating system: $uos"
  153. exit 1
  154. ;;
  155. esac
  156. case $ucpu in
  157. *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
  158. if [ "$isOpenIndiana" = "yes" ] || [ `uname -o` == "illumos" ] ; then
  159. mycpu="amd64"
  160. else
  161. mycpu="i386"
  162. fi
  163. ;;
  164. *amd*64* | *x86-64* | *x86_64* )
  165. mycpu="amd64" ;;
  166. *sparc*|*sun* )
  167. mycpu="sparc"
  168. if [ "$myos" = "linux" ] ; then
  169. if [ "$(getconf LONG_BIT)" = "64" ]; then
  170. mycpu="sparc64"
  171. elif [ "$(isainfo -b)" = "64" ]; then
  172. mycpu="sparc64"
  173. fi
  174. fi
  175. ;;
  176. *ppc64le* )
  177. mycpu="powerpc64el" ;;
  178. *ppc64* )
  179. if [ "$myos" = "linux" ] ; then
  180. COMP_FLAGS="$COMP_FLAGS -m64"
  181. LINK_FLAGS="$LINK_FLAGS -m64"
  182. fi
  183. mycpu="powerpc64" ;;
  184. *power*|*ppc* )
  185. if [ "$myos" = "freebsd" ] ; then
  186. if [ "$ucpu" != "powerpc" ] ; then
  187. COMP_FLAGS="$COMP_FLAGS -m64"
  188. LINK_FLAGS="$LINK_FLAGS -m64"
  189. fi
  190. mycpu=`uname -p`
  191. case $mycpu in
  192. powerpc64le)
  193. mycpu="powerpc64el"
  194. esac
  195. else
  196. mycpu="powerpc"
  197. fi
  198. ;;
  199. *hppa*)
  200. mycpu="hppa" ;;
  201. *ia64*)
  202. mycpu="ia64" ;;
  203. *m68k*)
  204. mycpu="m68k" ;;
  205. *mips* )
  206. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  207. case $mycpu in
  208. mips|mipsel|mips64|mips64el)
  209. ;;
  210. *)
  211. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  212. exit 1
  213. esac
  214. ;;
  215. *alpha* )
  216. mycpu="alpha" ;;
  217. *aarch64*|*arm64* )
  218. mycpu="arm64" ;;
  219. *arm*|*armv6l*|*armv7l*|*armv8l* )
  220. mycpu="arm" ;;
  221. *riscv64|riscv* )
  222. mycpu="riscv64" ;;
  223. *e2k* )
  224. mycpu="e2k" ;;
  225. *loongarch64* )
  226. mycpu="loongarch64" ;;
  227. *)
  228. echo 2>&1 "Error: unknown processor: $ucpu"
  229. exit 1
  230. ;;
  231. esac
  232. case $uosname in
  233. *android* )
  234. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  235. myosname="android"
  236. myos="android"
  237. ;;
  238. esac
  239. # add(result, "# call the compiler:\n")
  240. echo \# OS: $myos
  241. echo \# CPU: $mycpu
  242. case $myos in
  243. # for osA in 1..c.oses.len:
  244. ?{c.oses[osA-1]})
  245. case $mycpu in
  246. # for cpuA in 1..c.cpus.len:
  247. ?{c.cpus[cpuA-1]})
  248. set -x
  249. # var linkCmd = ""
  250. # for ff in items(c.cfiles[osA][cpuA]):
  251. # let f = ff.toUnix
  252. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  253. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  254. # end for
  255. if [ "$parallel" -gt 0 ]; then
  256. sem --wait --id $$
  257. fi
  258. $CC -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  259. ;;
  260. # end for
  261. *)
  262. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  263. exit 1
  264. ;;
  265. esac
  266. ;;
  267. # end for
  268. *)
  269. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  270. exit 1
  271. ;;
  272. esac
  273. : SUCCESS