makefile.nimf 3.6 KB

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