buildsh.nimf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. LINKER="${LD:-gcc}"
  41. COMP_FLAGS="${CPPFLAGS:-} ${CFLAGS:-} ?{c.ccompiler.flags}$extraBuildArgs"
  42. LINK_FLAGS="${LDFLAGS:-} ?{c.linker.flags}"
  43. PS4=""
  44. # add(result, "# platform detection\n")
  45. ucpu=`uname -m`
  46. uos=`uname`
  47. uosname=
  48. # add(result, "# bin dir detection\n")
  49. binDir=?{firstBinPath(c).toUnix}
  50. if [ -s ../koch.nim ]; then
  51. binDir="../bin"
  52. fi
  53. if [ ! -d $binDir ]; then
  54. mkdir $binDir
  55. fi
  56. # add(result, "# override OS, CPU and OS Name with command-line arguments\n")
  57. if [ -n "$optos" ]; then
  58. uos="$optos"
  59. fi
  60. if [ -n "$optcpu" ]; then
  61. ucpu="$optcpu"
  62. fi
  63. if [ -n "$optcpu" ]; then
  64. uosname="$optosname"
  65. fi
  66. # add(result, "# convert to lower case:\n")
  67. ucpu=`echo $ucpu | tr "[:upper:]" "[:lower:]"`
  68. uos=`echo $uos | tr "[:upper:]" "[:lower:]"`
  69. uosname=`echo $uosname | tr "[:upper:]" "[:lower:]"`
  70. case $uos in
  71. *linux* )
  72. myos="linux"
  73. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  74. ;;
  75. *dragonfly* )
  76. myos="dragonfly"
  77. LINK_FLAGS="$LINK_FLAGS -lm"
  78. ;;
  79. *freebsd* )
  80. myos="freebsd"
  81. CC="clang"
  82. LINKER="clang"
  83. LINK_FLAGS="$LINK_FLAGS -lm"
  84. ;;
  85. *openbsd* )
  86. myos="openbsd"
  87. LINK_FLAGS="$LINK_FLAGS -lm"
  88. ;;
  89. *netbsd* )
  90. myos="netbsd"
  91. LINK_FLAGS="$LINK_FLAGS -lm"
  92. ;;
  93. *darwin* )
  94. myos="macosx"
  95. CC="clang"
  96. LINKER="clang"
  97. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  98. if [ "$HOSTTYPE" = "x86_64" ] ; then
  99. ucpu="amd64"
  100. fi
  101. ;;
  102. *aix* )
  103. myos="aix"
  104. LINK_FLAGS="$LINK_FLAGS -ldl -lm"
  105. ;;
  106. *solaris* | *sun* )
  107. myos="solaris"
  108. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lsocket -lnsl"
  109. ;;
  110. *haiku* )
  111. myos="haiku"
  112. LINK_FLAGS="$LINK_FLAGS -lroot -lnetwork"
  113. ;;
  114. *mingw* | *msys* )
  115. myos="windows"
  116. ;;
  117. *android* )
  118. myos="android"
  119. LINK_FLAGS="$LINK_FLAGS -ldl -lm -lrt"
  120. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  121. ;;
  122. *)
  123. echo 2>&1 "Error: unknown operating system: $uos"
  124. exit 1
  125. ;;
  126. esac
  127. case $ucpu in
  128. *i386* | *i486* | *i586* | *i686* | *bepc* | *i86pc* )
  129. mycpu="i386" ;;
  130. *amd*64* | *x86-64* | *x86_64* )
  131. mycpu="amd64" ;;
  132. *sparc*|*sun* )
  133. mycpu="sparc"
  134. if [ "$(isainfo -b)" = "64" ]; then
  135. mycpu="sparc64"
  136. fi
  137. ;;
  138. *ppc64le* )
  139. mycpu="powerpc64el" ;;
  140. *ppc64* )
  141. if [ "$myos" = "linux" ] ; then
  142. COMP_FLAGS="$COMP_FLAGS -m64"
  143. LINK_FLAGS="$LINK_FLAGS -m64"
  144. fi
  145. mycpu="powerpc64" ;;
  146. *power*|*ppc* )
  147. mycpu="powerpc" ;;
  148. *mips* )
  149. mycpu="$("$CC" -dumpmachine | sed 's/-.*//')"
  150. case $mycpu in
  151. mips|mipsel|mips64|mips64el)
  152. ;;
  153. *)
  154. echo 2>&1 "Error: unknown MIPS target: $mycpu"
  155. exit 1
  156. esac
  157. ;;
  158. *arm*|*armv6l*|*armv71* )
  159. mycpu="arm" ;;
  160. *aarch64* )
  161. mycpu="arm64" ;;
  162. *riscv64* )
  163. mycpu="riscv64" ;;
  164. *)
  165. echo 2>&1 "Error: unknown processor: $ucpu"
  166. exit 1
  167. ;;
  168. esac
  169. case $uosname in
  170. *android* )
  171. LINK_FLAGS="$LINK_FLAGS -landroid-glob"
  172. myosname="android"
  173. myos="android"
  174. ;;
  175. esac
  176. # add(result, "# call the compiler:\n")
  177. echo \# OS: $myos
  178. echo \# CPU: $mycpu
  179. case $myos in
  180. # for osA in 1..c.oses.len:
  181. ?{c.oses[osA-1]})
  182. case $mycpu in
  183. # for cpuA in 1..c.cpus.len:
  184. ?{c.cpus[cpuA-1]})
  185. set -x
  186. # var linkCmd = ""
  187. # for ff in items(c.cfiles[osA][cpuA]):
  188. # let f = ff.toUnix
  189. $CC $COMP_FLAGS -Ic_code -c ?{f} -o ?{changeFileExt(f, "o")}
  190. # add(linkCmd, " \\\n" & changeFileExt(f, "o"))
  191. # end for
  192. $LINKER -o ?{"$binDir/" & toLowerAscii(c.name)} ?linkCmd $LINK_FLAGS
  193. ;;
  194. # end for
  195. *)
  196. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  197. exit 1
  198. ;;
  199. esac
  200. ;;
  201. # end for
  202. *)
  203. echo 2>&1 "Error: no C code generated for: [$myos: $mycpu]"
  204. exit 1
  205. ;;
  206. esac
  207. : SUCCESS