buildsh.nimf 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. LINK_FLAGS="$LINK_FLAGS -lm"
  86. ;;
  87. *netbsd* )
  88. myos="netbsd"
  89. LINK_FLAGS="$LINK_FLAGS -lm"
  90. ;;
  91. *darwin* )
  92. myos="macosx"
  93. CC="clang"
  94. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  95. if [ "$HOSTTYPE" = "x86_64" ] ; then
  96. ucpu="amd64"
  97. fi
  98. ;;
  99. *aix* )
  100. myos="aix"
  101. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  102. ;;
  103. *solaris* | *sun* )
  104. myos="solaris"
  105. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  106. ;;
  107. *haiku* )
  108. myos="haiku"
  109. LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
  110. ;;
  111. *mingw* | *msys* )
  112. myos="windows"
  113. ;;
  114. *android* )
  115. myos="android"
  116. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  117. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  118. ;;
  119. *)
  120. echo 2>&1 "Error: unknown operating system: $uos"
  121. exit 1
  122. ;;
  123. esac
  124. case $ucpu in
  125. *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
  126. mycpu="i386" ;;
  127. *amd*64* | *x86-64* | *x86_64* )
  128. mycpu="amd64" ;;
  129. *sparc*|*sun* )
  130. mycpu="sparc"
  131. if [ "$myos" = "linux" ] ; then
  132. if [ "$(getconf LONG_BIT)" = "64" ]; then
  133. mycpu="sparc64"
  134. elif [ "$(isainfo -b)" = "64" ]; then
  135. mycpu="sparc64"
  136. fi
  137. fi
  138. ;;
  139. *ppc64le* )
  140. mycpu="powerpc64el" ;;
  141. *ppc64* )
  142. if [ "$myos" = "linux" ] ; then
  143. COMP_FLAGS="$COMP_FLAGS -m64"
  144. LINK_FLAGS="$LINK_FLAGS -m64"
  145. fi
  146. mycpu="powerpc64" ;;
  147. *power*|*ppc* )
  148. if [ "$myos" = "freebsd" ] ; then
  149. COMP_FLAGS="$COMP_FLAGS -m64"
  150. LINK_FLAGS="$LINK_FLAGS -m64"
  151. mycpu=`uname -p`
  152. else
  153. mycpu="powerpc"
  154. fi
  155. ;;
  156. *ia64*)
  157. mycpu="ia64" ;;
  158. *m68k*)
  159. mycpu="m68k" ;;
  160. *mips* )
  161. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  162. case $mycpu in
  163. mips|mipsel|mips64|mips64el)
  164. ;;
  165. *)
  166. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  167. exit 1
  168. esac
  169. ;;
  170. *alpha* )
  171. mycpu="alpha" ;;
  172. *arm*|*armv6l*|*armv71* )
  173. mycpu="arm" ;;
  174. *aarch64* )
  175. mycpu="arm64" ;;
  176. *riscv64* )
  177. mycpu="riscv64" ;;
  178. *)
  179. echo 2>&1 "Error: unknown processor: $ucpu"
  180. exit 1
  181. ;;
  182. esac
  183. case $uosname in
  184. *android* )
  185. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  186. myosname="android"
  187. myos="android"
  188. ;;
  189. esac
  190. # add(result, "# call the compiler:\n")
  191. echo \# OS: $myos
  192. echo \# CPU: $mycpu
  193. case $myos in
  194. # for osA in 1..c.oses.len:
  195. ?{c.oses[osA-1]})
  196. case $mycpu in
  197. # for cpuA in 1..c.cpus.len:
  198. ?{c.cpus[cpuA-1]})
  199. set -x
  200. # var linkCmd = ""
  201. # for ff in items(c.cfiles[osA][cpuA]):
  202. # let f = ff.toUnix
  203. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  204. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  205. # end for
  206. $CC -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  207. ;;
  208. # end for
  209. *)
  210. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  211. exit 1
  212. ;;
  213. esac
  214. ;;
  215. # end for
  216. *)
  217. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  218. exit 1
  219. ;;
  220. esac
  221. : SUCCESS