-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.mk
211 lines (178 loc) · 5.15 KB
/
common.mk
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Copyright (C) 2010--2017 Cesar Rodriguez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# R Optional (default "."). Path to the root directory of the project.
# D Optional (default "$R"). Path to the subdir where we are invoking this.
# INC Optional (default ""). Options -I and -D for the cpp.
# SRCS Optional (default "$D/*.{c,cc}"). Sources to compile.
# MSRCS Optional (default "$D/main.cc"). Sources that contain a main()
# LIBS Optional (default ""). Libraries to link to (.{a,o,so} files)
#
# defines the following variables:
# CC
# CXX
# CFLAGS, CPPFLAGS, LDFLAGS
# TARGETS
# COMPILE.cc
# LINK.cc
#
# provides the following goals:
# compile
# clean
# realclean
# vars
# c -> [oi]
# cc -> [oi]
# dot -> {svg,pdf}
.DEFAULT_GOAL := all
.PHONY: all clean realclean compile
# default values for the input variables
R ?= .
D ?= $R
SRCS ?= $(wildcard $D/*.c $D/*.cc $D/*/*.c $D/*/*.cc $D/*/*/*.c $D/*/*/*.cc)
MSRCS ?= $(wildcard $D/main.cc) $(wildcard $D/*/main.cc) $(wildcard $D/*/*/main.cc)
# compilation targets
TMP = $(SRCS:.cc=.o)
OBJS ?= $(TMP:.c=.o)
TMP2 = $(MSRCS:.cc=.o)
MOBJS ?= $(TMP2:.c=.o)
TARGETS ?= $(MOBJS:.o=)
include $R/config.mk
# == llvm ==
#LLVMCXXFLAGS := -I$(shell llvm-config-$(CONFIG_LLVM_VER) --includedir)
LLVMCXXFLAGS := $(shell llvm-config-$(CONFIG_LLVM_VER) --cppflags)
LLVMLDFLAGS := $(shell llvm-config-$(CONFIG_LLVM_VER) --ldflags)
#LLVMLIBS := $(shell llvm-config-$(CONFIG_LLVM_VER) --libs --system-libs)
LLVMLIBS := $(shell llvm-config-$(CONFIG_LLVM_VER) --libs all) -lz -lpthread -lffi -lncurses -ldl -lm
# == steroids ==
STIDROOT := $R/lib/steroids
STIDCPPFLAGS := -I $(STIDROOT)/include/
STIDLDFLAGS := -L $(STIDROOT)/src/
STIDLDLIBS := -Wl,-Bstatic -lsteroids -Wl,-Bdynamic
#STIDLDLIBS := -lsteroids
# traditional variables
ifdef CONFIG_DEBUG
CFLAGS = -Wall -std=c11 -g
CXXFLAGS = -Wall -std=c++11 -g
endif
ifdef CONFIG_RELEASE
CFLAGS = -Wall -std=c11 -O3
CXXFLAGS = -Wall -std=c++11 -O3
endif
CPPFLAGS = -I $D $(LLVMCXXFLAGS) $(STIDCPPFLAGS) $(INC)
LDFLAGS = $(LLVMLDFLAGS) $(STIDLDFLAGS) # -dead_strip -static
LDLIBS = $(LIBS) $(STIDLDLIBS) $(LLVMLIBS)
# define the toolchain
LD := ld$(CONFIG_LLVM_VER)
CC := clang-$(CONFIG_LLVM_VER)
CXX := clang++-$(CONFIG_LLVM_VER)
CPP := cpp$(CONFIG_LLVM_VER)
LEX := flex
YACC := bison
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
LINK.c = $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
LINK.cc = $(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
%.d : %.c
@echo "DEP $<"
@$(CC) -MM -MT "$*.o $*.bc $*.i $@" $(CFLAGS) $(CPPFLAGS) $< -o $@
%.d : %.cc
@echo "DEP $<"
@$(CXX) -MM -MT "$*.o $*.bc $*.i $@" $(CXXFLAGS) $(CPPFLAGS) $< -o $@
%.cc : %.l
@echo "LEX $<"
@$(LEX) -o $@ $^
%.cc : %.y
@echo "YAC $<"
@$(YACC) -o $@ $^
#%.h : %.mk
# @echo "M2H $<"
# @$R/scripts/mk2h.py < $^ > $@
# cancelling gnu make builtin rules for lex/yacc to c
# http://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_10.html#SEC104
%.c : %.l
%.c : %.y
%.o : %.c
@echo "CC $<"
@$(COMPILE.c)
%.o : %.cc
@echo "CXX $<"
@$(COMPILE.cc)
%.i : %.c
@echo "CC $<"
@$(COMPILE.c) -E
%.i : %.cc
@echo "CXX $<"
@$(COMPILE.cc) -E
%.pdf : %.dot
@echo "DOT $<"
@dot -T pdf < $< > $@
%.png : %.dot
@echo "DOT $<"
@dot -Tpng < $< > $@
%.svg : %.dot
@echo "DOT $<"
@dot -Tsvg < $< > $@
CFLAGS_:=-Wall -Wextra -std=c11 -O3
CXXFLAGS_:=-Wall -Wextra -std=c++11 -O3
%.ll : %.c
@echo "LL $<"
@$(CC) $(CFLAGS_) -S -flto $< -o $@
%.bc : %.c
@echo "BC $<"
@$(CC) $(CFLAGS_) -c -flto $< -o $@
%.ll : %.cc
@echo "LL $<"
@$(CXX) $(CXXFLAGS_) -S -flto $< -o $@
%.bc : %.cc
@echo "BC $<"
@$(CXX) $(CXXFLAGS_) -c -flto $< -o $@
%.bc : %.ll
llvm-as-$(CONFIG_LLVM_VER) $< -o $@
%.ll : %.bc
llvm-dis-$(CONFIG_LLVM_VER) $< -o $@
%.s : %.bc
llc-$(CONFIG_LLVM_VER) $< -o $@
compile: $(TARGETS)
$(TARGETS) : % : %.o $(OBJS) $(LIBS)
@echo "LD $@"
@$(LINK.cc)
vars :
@echo "R $R"
@echo "D $D"
@echo "INC $(INC)"
@echo "SRCS $(SRCS)"
@echo "MSRCS $(MSRCS)"
@echo "LIBS $(LIBS)"
@echo "======="
@echo "CC $(CC)"
@echo "CXX $(CXX)"
@echo "CFLAGS $(CFLAGS)"
@echo "CPPFLAGS $(CPPFLAGS)"
@echo "CXXFLAGS $(CXXFLAGS)"
@echo "LDFLAGS $(LDFLAGS)"
@echo "LDLIBS $(LDLIBS)"
@echo "MOBJS $(MOBJS)"
@echo "OBJS $(OBJS)"
@echo "======="
@echo "COMPILE.cc $(COMPILE.cc)"
@echo "LINK.cc $(LINK.cc)"
@echo "TARGETS $(TARGETS)"
clean :
@rm -f $(TARGETS) $(MOBJS) $(OBJS)
realclean : clean
@rm -f $(DEPS)
# dependency files
DEPS = $(patsubst %.o,%.d,$(OBJS) $(MOBJS))
$(DEPS) : $R/config.h
-include $(DEPS)