summary refs log tree commit diff stats
path: root/tools/niminst/makefile.tmpl
blob: 4a20680e005bbc412860e9269da759c8fcb49739 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#? stdtmpl(subsChar='?') | standard
#proc generateMakefile(c: ConfigData): string =
#  result = "# Generated from niminst\n" &
#           "# Template is in tools/niminst/makefile.tmpl\n" &
#           "# To regenerate run ``niminst csource`` or ``koch csource``\n"

CC = gcc
LINKER = gcc
COMP_FLAGS = $(CPPFLAGS) $(CFLAGS) ?{c.ccompiler.flags}
LINK_FLAGS = $(LDFLAGS) ?{c.linker.flags}
binDir = ?{firstBinPath(c).toUnix}

koch := $(shell sh -c 'test -s ../koch.nim && echo "yes"')
ifeq ($(koch),yes)
  binDir = ../bin
endif

ucpu := $(shell sh -c 'uname -m | tr "[:upper:]" "[:lower:]"')
uos := $(shell sh -c 'uname | tr "[:upper:]" "[:lower:]"')
uosname := $(shell sh -c 'uname -o | tr "[:upper:]" "[:lower:]"')

ifeq ($(uos),linux)
  myos = linux
  LINK_FLAGS += -ldl -lm
endif
ifeq ($(uos),dragonfly)
  myos = freebsd
  LINK_FLAGS += -lm
endif
ifeq ($(uos),freebsd)
  myos= freebsd
  CC = clang
  LINKER = clang
  LINK_FLAGS += -lm
endif
ifeq ($(uos),openbsd)
  myos = openbsd
  LINK_FLAGS += -lm
endif
ifeq ($(uos),netbsd)
  myos = netbsd
  LINK_FLAGS += -lm
endif
ifeq ($(uos),darwin)
  myos = macosx
  CC = clang
  LINKER = clang
  LINK_FLAGS += -ldl -lm
  ifeq ($(HOSTTYPE),x86_64)
    ucpu = amd64
  endif
endif
ifeq ($(uos),aix)
  myos = aix
  LINK_FLAGS += -dl -lm
endif
ifeq ($(uos),solaris)
  myos = solaris
  LINK_FLAGS += -ldl -lm -lsocket -lnsl
endif
ifeq ($(uos),sun)
  myos = solaris
  LINK_FLAGS += -ldl -lm -lsocket -lnsl
endif
ifeq ($(uos),haiku)
  myos = haiku
endif
ifndef uos
  $(error unknown operating system: $(uos))
endif

ifeq ($(uosname),android)
  myos = android
  LINK_FLAGS += -landroid-glob
endif

ifeq ($(ucpu),i386)
  mycpu = i386
endif
ifeq ($(ucpu),i486)
  mycpu = i386
endif
ifeq ($(ucpu),i586)
  mycpu = i386
endif
ifeq ($(ucpu),i686)
  mycpu = i386
endif
ifeq ($(ucpu),bepc)
  mycpu = i386
endif
ifeq ($(ucpu),i86pc)
  mycpu = i386
endif
ifeq ($(ucpu),amd64)
  mycpu = amd64
endif
ifeq ($(ucpu),x86-64)
  mycpu = amd64
endif
ifeq ($(ucpu),x86_64)
  mycpu = amd64
endif
ifeq ($(ucpu),sparc)
  mycpu = sparc
endif
ifeq ($(ucpu),sun)
  mycpu = sparc
endif
ifeq ($(ucpu),ppc64)
  mycpu = powerpc64
  ifeq ($(myos),linux)
    COMP_FLAGS += -m64
    LINK_FLAGS += -m64
  endif
endif
ifeq ($(ucpu),powerpc)
  mycpu = powerpc
endif
ifeq ($(ucpu),ppc)
  mycpu = ppc
endif
ifneq (,$(filter $(ucpu), mips mips64))
  mycpu = $(shell /bin/sh -c '"$(CC)" -dumpmachine | sed "s/-.*//"')
  ifeq (,$(filter $(mycpu), mips mipsel mips64 mips64el))
    $(error unknown MIPS target: $(mycpu))
  endif
endif
ifeq ($(ucpu),arm)
  mycpu = arm
endif
ifeq ($(ucpu),armeb)
  mycpu = arm
endif
ifeq ($(ucpu),armel)
  mycpu = arm
endif
ifeq ($(ucpu),armv6l)
  mycpu = arm
endif
ifndef ucpu
  $(error unknown processor: $(ucpu))
endif

# for osA in 1..c.oses.len:
ifeq ($(myos),?{c.oses[osA-1]})
#   for cpuA in 1..c.cpus.len:
  ifeq ($(mycpu),?{c.cpus[cpuA-1]})
#     var oFiles = ""
#     for ff in c.cfiles[osA][cpuA].items:
#       oFiles.add(" " & changeFileExt(ff.toUnix, "o"))
#     end for
    oFiles =?oFiles
  endif
#   end for
endif
# end for

ifeq ($(strip $(oFiles)),)
  $(error no C code generated for: [$(myos): $(mycpu)])
endif

%.o: %.c
	$(CC) $(COMP_FLAGS) -Ic_code -c $< -o $@

?{"$(binDir)/" & toLowerAscii(c.name)}: $(oFiles)
	@mkdir -p $(binDir)
	$(LINKER) -o $@ $^ $(LINK_FLAGS)
	@echo "SUCCESS"

.PHONY: clean

clean:
	rm -f $(oFiles) ?{"$(binDir)/" & toLowerAscii(c.name)}