buildsh.nimf 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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" ] ; 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. COMP_FLAGS="$COMP_FLAGS -m64"
  187. LINK_FLAGS="$LINK_FLAGS -m64"
  188. mycpu=`uname -p`
  189. case $mycpu in
  190. powerpc64le)
  191. mycpu="powerpc64el"
  192. esac
  193. else
  194. mycpu="powerpc"
  195. fi
  196. ;;
  197. *hppa*)
  198. mycpu="hppa" ;;
  199. *ia64*)
  200. mycpu="ia64" ;;
  201. *m68k*)
  202. mycpu="m68k" ;;
  203. *mips* )
  204. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  205. case $mycpu in
  206. mips|mipsel|mips64|mips64el)
  207. ;;
  208. *)
  209. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  210. exit 1
  211. esac
  212. ;;
  213. *alpha* )
  214. mycpu="alpha" ;;
  215. *aarch64*|*arm64* )
  216. mycpu="arm64" ;;
  217. *arm*|*armv6l*|*armv7l*|*armv8l* )
  218. mycpu="arm" ;;
  219. *riscv64|riscv* )
  220. mycpu="riscv64" ;;
  221. *)
  222. echo 2>&1 "Error: unknown processor: $ucpu"
  223. exit 1
  224. ;;
  225. esac
  226. case $uosname in
  227. *android* )
  228. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  229. myosname="android"
  230. myos="android"
  231. ;;
  232. esac
  233. # add(result, "# call the compiler:\n")
  234. echo \# OS: $myos
  235. echo \# CPU: $mycpu
  236. case $myos in
  237. # for osA in 1..c.oses.len:
  238. ?{c.oses[osA-1]})
  239. case $mycpu in
  240. # for cpuA in 1..c.cpus.len:
  241. ?{c.cpus[cpuA-1]})
  242. set -x
  243. # var linkCmd = ""
  244. # for ff in items(c.cfiles[osA][cpuA]):
  245. # let f = ff.toUnix
  246. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  247. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  248. # end for
  249. if [ "$parallel" -gt 0 ]; then
  250. sem --wait --id $$
  251. fi
  252. $CC -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  253. ;;
  254. # end for
  255. *)
  256. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  257. exit 1
  258. ;;
  259. esac
  260. ;;
  261. # end for
  262. *)
  263. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  264. exit 1
  265. ;;
  266. esac
  267. : SUCCESS