makefile.nimf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #? stdtmpl(subsChar='?') | standard
  2. #proc generateMakefile(c: ConfigData): string =
  3. # result = "# Generated from niminst\n" &
  4. # "# Template is in tools/niminst/makefile.nimf\n" &
  5. # "# To regenerate run ``niminst csource`` or ``koch csource``\n"
  6. CC ??= gcc
  7. CFLAGS += -Ic_code ?{c.ccompiler.flags}
  8. LDFLAGS += ?{c.linker.flags}
  9. binDir = ?{firstBinPath(c).toUnix}
  10. koch := $(shell sh -c 'test -s ../koch.nim && echo "yes"')
  11. ifeq ($(koch),yes)
  12. binDir = ../bin
  13. endif
  14. target := ?{"$(binDir)/" & toLowerAscii(c.name)}
  15. ucpu := $(shell sh -c 'uname -m | tr "[:upper:]" "[:lower:]"')
  16. ifeq ($(OS),Windows_NT)
  17. uos := windows
  18. else
  19. uos := $(shell sh -c 'uname | tr "[:upper:]" "[:lower:]"')
  20. endif
  21. ifeq ($(uos),linux)
  22. myos = linux
  23. ## add -lrt to avoid "undefined reference to `clock_gettime'" with glibc<2.17
  24. LDFLAGS += -ldl -lm -lrt
  25. endif
  26. ifeq ($(uos),dragonfly)
  27. myos = freebsd
  28. LDFLAGS += -lm
  29. endif
  30. ifeq ($(uos),freebsd)
  31. myos= freebsd
  32. CC = clang
  33. LDFLAGS += -lm
  34. endif
  35. ifeq ($(uos),openbsd)
  36. myos = openbsd
  37. LDFLAGS += -lm
  38. endif
  39. ifeq ($(uos),netbsd)
  40. myos = netbsd
  41. LDFLAGS += -lm
  42. endif
  43. ifeq ($(uos),darwin)
  44. myos = macosx
  45. CC = clang
  46. LDFLAGS += -ldl -lm
  47. ifeq ($(HOSTTYPE),x86_64)
  48. ucpu = amd64
  49. endif
  50. endif
  51. ifeq ($(uos),aix)
  52. myos = aix
  53. LDFLAGS += -dl -lm
  54. endif
  55. ifeq ($(uos),solaris)
  56. myos = solaris
  57. LDFLAGS += -ldl -lm -lsocket -lnsl
  58. endif
  59. ifeq ($(uos),sun)
  60. myos = solaris
  61. LDFLAGS += -ldl -lm -lsocket -lnsl
  62. endif
  63. ifeq ($(uos),haiku)
  64. myos = haiku
  65. endif
  66. ifeq ($(uos),windows)
  67. myos = windows
  68. target := $(target).exe
  69. endif
  70. ifndef myos
  71. $(error unknown operating system: $(uos))
  72. endif
  73. ifeq ($(ucpu),i386)
  74. mycpu = i386
  75. endif
  76. ifeq ($(ucpu),i486)
  77. mycpu = i386
  78. endif
  79. ifeq ($(ucpu),i586)
  80. mycpu = i386
  81. endif
  82. ifeq ($(ucpu),i686)
  83. mycpu = i386
  84. endif
  85. ifeq ($(ucpu),bepc)
  86. mycpu = i386
  87. endif
  88. ifeq ($(ucpu),i86pc)
  89. mycpu = i386
  90. endif
  91. ifeq ($(ucpu),amd64)
  92. mycpu = amd64
  93. endif
  94. ifeq ($(ucpu),x86-64)
  95. mycpu = amd64
  96. endif
  97. ifeq ($(ucpu),x86_64)
  98. mycpu = amd64
  99. endif
  100. ifeq ($(ucpu),parisc64)
  101. mycpu = hppa
  102. endif
  103. ifeq ($(ucpu),s390x)
  104. mycpu = s390x
  105. endif
  106. ifeq ($(ucpu),sparc)
  107. mycpu = sparc
  108. endif
  109. ifeq ($(ucpu),sparc64)
  110. mycpu = sparc64
  111. endif
  112. ifeq ($(ucpu),sun)
  113. mycpu = sparc
  114. endif
  115. ifeq ($(ucpu),ppc64le)
  116. mycpu = powerpc64el
  117. endif
  118. ifeq ($(ucpu),ppc64)
  119. mycpu = powerpc64
  120. ifeq ($(myos),linux)
  121. CFLAGS += -m64
  122. LDFLAGS += -m64
  123. endif
  124. endif
  125. ifeq ($(ucpu),powerpc)
  126. mycpu = powerpc
  127. ifeq ($(myos),freebsd)
  128. mycpu = $(shell sh -c 'uname -p | tr "[:upper:]" "[:lower:]"')
  129. CFLAGS += -m64
  130. LDFLAGS += -m64
  131. ifeq ($(mycpu),powerpc64le)
  132. mycpu = powerpc64el
  133. endif
  134. endif
  135. endif
  136. ifeq ($(ucpu),ppc)
  137. mycpu = powerpc
  138. endif
  139. ifneq (,$(filter $(ucpu), mips mips64))
  140. mycpu = $(shell /bin/sh -c '"$(CC)" -dumpmachine | sed "s/-.*//"')
  141. ifeq (,$(filter $(mycpu), mips mipsel mips64 mips64el))
  142. $(error unknown MIPS target: $(mycpu))
  143. endif
  144. endif
  145. ifeq ($(ucpu),arm)
  146. mycpu = arm
  147. endif
  148. ifeq ($(ucpu),armeb)
  149. mycpu = arm
  150. endif
  151. ifeq ($(ucpu),armel)
  152. mycpu = arm
  153. endif
  154. ifeq ($(ucpu),armv6l)
  155. mycpu = arm
  156. endif
  157. ifeq ($(ucpu),armv7l)
  158. mycpu = arm
  159. endif
  160. ifeq ($(ucpu),armv7hl)
  161. mycpu = arm
  162. endif
  163. ifeq ($(ucpu),armv8l)
  164. mycpu = arm
  165. endif
  166. ifeq ($(ucpu),aarch64)
  167. mycpu = arm64
  168. endif
  169. ifeq ($(ucpu),arm64)
  170. mycpu = arm64
  171. endif
  172. ifeq ($(ucpu),riscv64)
  173. mycpu = riscv64
  174. endif
  175. ifeq ($(ucpu),e2k)
  176. mycpu = e2k
  177. endif
  178. ifeq ($(ucpu),loongarch64)
  179. mycpu = loongarch64
  180. endif
  181. ifndef mycpu
  182. $(error unknown CPU architecture: $(ucpu) See makefile.nimf)
  183. endif
  184. # for osA in 1..c.oses.len:
  185. ifeq ($(myos),?{c.oses[osA-1]})
  186. # for cpuA in 1..c.cpus.len:
  187. ifeq ($(mycpu),?{c.cpus[cpuA-1]})
  188. # var oFiles = ""
  189. # for ff in c.cfiles[osA][cpuA].items:
  190. # oFiles.add(" " & changeFileExt(ff.toUnix, "o"))
  191. # end for
  192. oFiles =?oFiles
  193. endif
  194. # end for
  195. endif
  196. # end for
  197. ifeq ($(strip $(oFiles)),)
  198. $(error no C code generated for: [$(myos): $(mycpu)])
  199. endif
  200. $(target): $(oFiles)
  201. @mkdir -p $(binDir)
  202. $(CC) -o $@ $^ $(LDFLAGS)
  203. @echo "SUCCESS"
  204. .PHONY: clean
  205. clean:
  206. rm -f $(oFiles) ?{"$(binDir)/" & toLowerAscii(c.name)}