makefile.nimf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. ucpu = $(shell sh -c 'uname -p')
  43. endif
  44. ifeq ($(uos),darwin)
  45. myos = macosx
  46. CC = clang
  47. LDFLAGS += -ldl -lm
  48. ifeq ($(HOSTTYPE),x86_64)
  49. ucpu = amd64
  50. endif
  51. endif
  52. ifeq ($(uos),aix)
  53. myos = aix
  54. LDFLAGS += -dl -lm
  55. endif
  56. ifeq ($(uos),solaris)
  57. myos = solaris
  58. LDFLAGS += -ldl -lm -lsocket -lnsl
  59. endif
  60. ifeq ($(uos),sun)
  61. myos = solaris
  62. LDFLAGS += -ldl -lm -lsocket -lnsl
  63. endif
  64. ifeq ($(uos),haiku)
  65. myos = haiku
  66. endif
  67. ifeq ($(uos),windows)
  68. myos = windows
  69. target := $(target).exe
  70. endif
  71. ifndef myos
  72. $(error unknown operating system: $(uos))
  73. endif
  74. ifeq ($(ucpu),i386)
  75. mycpu = i386
  76. endif
  77. ifeq ($(ucpu),i486)
  78. mycpu = i386
  79. endif
  80. ifeq ($(ucpu),i586)
  81. mycpu = i386
  82. endif
  83. ifeq ($(ucpu),i686)
  84. mycpu = i386
  85. endif
  86. ifeq ($(ucpu),bepc)
  87. mycpu = i386
  88. endif
  89. ifeq ($(ucpu),i86pc)
  90. mycpu = i386
  91. endif
  92. ifeq ($(ucpu),amd64)
  93. mycpu = amd64
  94. endif
  95. ifeq ($(ucpu),x86-64)
  96. mycpu = amd64
  97. endif
  98. ifeq ($(ucpu),x86_64)
  99. mycpu = amd64
  100. endif
  101. ifeq ($(ucpu),parisc64)
  102. mycpu = hppa
  103. endif
  104. ifeq ($(ucpu),s390x)
  105. mycpu = s390x
  106. endif
  107. ifeq ($(ucpu),sparc)
  108. mycpu = sparc
  109. endif
  110. ifeq ($(ucpu),sparc64)
  111. mycpu = sparc64
  112. endif
  113. ifeq ($(ucpu),sun)
  114. mycpu = sparc
  115. endif
  116. ifeq ($(ucpu),ppc64le)
  117. mycpu = powerpc64el
  118. endif
  119. ifeq ($(ucpu),ppc64)
  120. mycpu = powerpc64
  121. ifeq ($(myos),linux)
  122. CFLAGS += -m64
  123. LDFLAGS += -m64
  124. endif
  125. endif
  126. ifeq ($(ucpu),powerpc)
  127. mycpu = powerpc
  128. ifeq ($(myos),freebsd)
  129. mycpu = $(shell sh -c 'uname -p | tr "[:upper:]" "[:lower:]"')
  130. CFLAGS += -m64
  131. LDFLAGS += -m64
  132. ifeq ($(mycpu),powerpc64le)
  133. mycpu = powerpc64el
  134. endif
  135. endif
  136. endif
  137. ifeq ($(ucpu),ppc)
  138. mycpu = powerpc
  139. endif
  140. ifneq (,$(filter $(ucpu), mips mips64))
  141. mycpu = $(shell /bin/sh -c '"$(CC)" -dumpmachine | sed "s/-.*//"')
  142. ifeq (,$(filter $(mycpu), mips mipsel mips64 mips64el))
  143. $(error unknown MIPS target: $(mycpu))
  144. endif
  145. endif
  146. ifeq ($(ucpu),arm)
  147. mycpu = arm
  148. endif
  149. ifeq ($(ucpu),armeb)
  150. mycpu = arm
  151. endif
  152. ifeq ($(ucpu),armel)
  153. mycpu = arm
  154. endif
  155. ifeq ($(ucpu),armv6l)
  156. mycpu = arm
  157. endif
  158. ifeq ($(ucpu),armv7l)
  159. mycpu = arm
  160. endif
  161. ifeq ($(ucpu),armv7hl)
  162. mycpu = arm
  163. endif
  164. ifeq ($(ucpu),armv8l)
  165. mycpu = arm
  166. endif
  167. ifeq ($(ucpu),aarch64)
  168. mycpu = arm64
  169. endif
  170. ifeq ($(ucpu),arm64)
  171. mycpu = arm64
  172. endif
  173. ifeq ($(ucpu),riscv64)
  174. mycpu = riscv64
  175. endif
  176. ifeq ($(ucpu),e2k)
  177. mycpu = e2k
  178. endif
  179. ifeq ($(ucpu),loongarch64)
  180. mycpu = loongarch64
  181. endif
  182. ifndef mycpu
  183. $(error unknown CPU architecture: $(ucpu) See makefile.nimf)
  184. endif
  185. # for osA in 1..c.oses.len:
  186. ifeq ($(myos),?{c.oses[osA-1]})
  187. # for cpuA in 1..c.cpus.len:
  188. ifeq ($(mycpu),?{c.cpus[cpuA-1]})
  189. # var oFiles = ""
  190. # for ff in c.cfiles[osA][cpuA].items:
  191. # oFiles.add(" " & changeFileExt(ff.toUnix, "o"))
  192. # end for
  193. oFiles =?oFiles
  194. endif
  195. # end for
  196. endif
  197. # end for
  198. ifeq ($(strip $(oFiles)),)
  199. $(error no C code generated for: [$(myos): $(mycpu)])
  200. endif
  201. $(target): $(oFiles)
  202. @mkdir -p $(binDir)
  203. $(CC) -o $@ $^ $(LDFLAGS)
  204. @echo "SUCCESS"
  205. .PHONY: clean
  206. clean:
  207. rm -f $(oFiles) ?{"$(binDir)/" & toLowerAscii(c.name)}