makefile.nimf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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),sparc)
  101. mycpu = sparc
  102. endif
  103. ifeq ($(ucpu),sun)
  104. mycpu = sparc
  105. endif
  106. ifeq ($(ucpu),ppc64le)
  107. mycpu = powerpc64el
  108. endif
  109. ifeq ($(ucpu),ppc64)
  110. mycpu = powerpc64
  111. ifeq ($(myos),linux)
  112. CFLAGS += -m64
  113. LDFLAGS += -m64
  114. endif
  115. endif
  116. ifeq ($(ucpu),powerpc)
  117. mycpu = powerpc
  118. ifeq ($(myos),freebsd)
  119. mycpu = $(shell sh -c 'uname -p | tr "[:upper:]" "[:lower:]"')
  120. CFLAGS += -m64
  121. LDFLAGS += -m64
  122. ifeq ($(mycpu),powerpc64le)
  123. mycpu = powerpc64el
  124. endif
  125. endif
  126. endif
  127. ifeq ($(ucpu),ppc)
  128. mycpu = ppc
  129. endif
  130. ifneq (,$(filter $(ucpu), mips mips64))
  131. mycpu = $(shell /bin/sh -c '"$(CC)" -dumpmachine | sed "s/-.*//"')
  132. ifeq (,$(filter $(mycpu), mips mipsel mips64 mips64el))
  133. $(error unknown MIPS target: $(mycpu))
  134. endif
  135. endif
  136. ifeq ($(ucpu),arm)
  137. mycpu = arm
  138. endif
  139. ifeq ($(ucpu),armeb)
  140. mycpu = arm
  141. endif
  142. ifeq ($(ucpu),armel)
  143. mycpu = arm
  144. endif
  145. ifeq ($(ucpu),armv6l)
  146. mycpu = arm
  147. endif
  148. ifeq ($(ucpu),armv7l)
  149. mycpu = arm
  150. endif
  151. ifeq ($(ucpu),armv7hl)
  152. mycpu = arm
  153. endif
  154. ifeq ($(ucpu),armv8l)
  155. mycpu = arm
  156. endif
  157. ifeq ($(ucpu),aarch64)
  158. mycpu = arm64
  159. endif
  160. ifeq ($(ucpu),riscv64)
  161. mycpu = riscv64
  162. endif
  163. ifeq ($(ucpu),e2k)
  164. mycpu = e2k
  165. endif
  166. ifeq ($(ucpu),loongarch64)
  167. mycpu = loongarch64
  168. endif
  169. ifndef mycpu
  170. $(error unknown CPU architecture: $(ucpu) See makefile.nimf)
  171. endif
  172. # for osA in 1..c.oses.len:
  173. ifeq ($(myos),?{c.oses[osA-1]})
  174. # for cpuA in 1..c.cpus.len:
  175. ifeq ($(mycpu),?{c.cpus[cpuA-1]})
  176. # var oFiles = ""
  177. # for ff in c.cfiles[osA][cpuA].items:
  178. # oFiles.add(" " & changeFileExt(ff.toUnix, "o"))
  179. # end for
  180. oFiles =?oFiles
  181. endif
  182. # end for
  183. endif
  184. # end for
  185. ifeq ($(strip $(oFiles)),)
  186. $(error no C code generated for: [$(myos): $(mycpu)])
  187. endif
  188. $(target): $(oFiles)
  189. @mkdir -p $(binDir)
  190. $(CC) -o $@ $^ $(LDFLAGS)
  191. @echo "SUCCESS"
  192. .PHONY: clean
  193. clean:
  194. rm -f $(oFiles) ?{"$(binDir)/" & toLowerAscii(c.name)}