-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
217 lines (160 loc) · 7.71 KB
/
Makefile
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
211
212
213
214
215
216
217
# WARNING: This makefile is intended to be invoked by LuaRocks, not manually. Only use
# it form `clean`, `format` and if your IDE is grumpy, `autogen-files` might work.
# If we don't disable suffixes, Make fails to detect our default %.o rule because it has
# extra prerequisites. Because of this, it falls back to its internal built-in recipe for
# %.o from %.cpp. This default rule doesn't have any of the header search paths, and will
# fail.
.SUFFIXES:
################################################################################
# DEFAULTS
# These are only used when this Makefile is run manually. You should only be
# doing that for `make clean`. Use `luarocks` for everything else.
CP = cp
CURL = curl
LIB_EXTENSION = so
LUAROCKS = luarocks
MKDIR = mkdir
OBJ_EXTENSION = o
UNICORN_INCDIR = /usr/include
BUSTED := $(shell $(LUAROCKS) config variables.SCRIPTS_DIR)/busted
LIBFLAG := $(shell $(LUAROCKS) config variables.LIBFLAG)
LUA := $(shell $(LUAROCKS) config variables.LUA)
LUA_DIR := $(shell $(LUAROCKS) config variables.LUA_DIR)
LUA_INCDIR = $(LUA_DIR)/include
LUA_LIBDIR = $(LUA_DIR)/lib
LUA_VERSION = $(shell $(LUA) -e 'print(_VERSION:sub(5))')
################################################################################
# Disable 64-bit integer tests for Lua <5.3
ifeq ($(LUA_VERSION),5.1)
BUSTED_FLAGS = --exclude-tags="int64only"
else ifeq ($(LUA_VERSION),5.2)
BUSTED_FLAGS = --exclude-tags="int64only"
else
BUSTED_FLAGS =
endif
IS_LUAJIT = $(shell $(LUA) -e 'if _G.jit ~= nil then print(1) else print(0) end')
ifeq ($(IS_LUAJIT),1)
# LuaJIT
DEFAULT_LUA_LIB_NAME = luajit-$(LUA_VERSION)
LUAJIT_VERSION = $(shell \
$(LUA) -e 'print(string.format("%d.%d", jit.version_num / 10000, (jit.version_num / 100) % 100))' \
)
FALLBACK_LUA_INCDIR = $(LUA_DIR)/include/luajit-$(LUAJIT_VERSION)
else
# Regular Lua
DEFAULT_LUA_LIB_NAME = lua
FALLBACK_LUA_INCDIR = $(LUA_DIR)/include
endif
ARCHITECTURE_HEADERS = $(wildcard $(UNICORN_INCDIR)/unicorn/*.h)
ARCHITECTURE_SLUGS = $(filter-out platform,$(basename $(notdir $(ARCHITECTURE_HEADERS))))
SOURCE_DIR = csrc
CONSTANT_FILES = $(foreach s,$(ARCHITECTURE_SLUGS),$(SOURCE_DIR)/$(s)_const.cpp)
CPP_TEMPLATE_SOURCES = $(wildcard $(SOURCE_DIR)/*.template)
AUTOGENERATED_CPP_FILES = $(CPP_TEMPLATE_SOURCES:.template=.cpp)
HEADER_TEMPLATE_SOURCES = $(wildcard $(SOURCE_DIR)/unicornlua/*.template)
AUTOGENERATED_HPP_FILES = $(HEADER_TEMPLATE_SOURCES:.template=.hpp)
LIB_BUILD_TARGET = $(SOURCE_DIR)/unicorn.$(LIB_EXTENSION)
LIB_CPP_SOURCES = $(wildcard $(SOURCE_DIR)/*.cpp) $(CONSTANT_FILES) $(AUTOGENERATED_CPP_FILES)
LIB_OBJECT_FILES = $(LIB_CPP_SOURCES:.cpp=.$(OBJ_EXTENSION))
TEST_EXECUTABLE = $(SOURCE_DIR)/cpp_test
TEST_CPP_SOURCES = $(wildcard tests/c/*.cpp)
TEST_LUA_SOURCES = $(wildcard tests/lua/*.lua)
TEST_HEADERS = $(wildcard tests/c/*.hpp)
TEST_CPP_OBJECT_FILES = $(TEST_CPP_SOURCES:.cpp=.$(OBJ_EXTENSION))
TEMPLATE_DATA_FILES = $(wildcard $(SOURCE_DIR)/template_data/*.lua)
# Unicorn 1.x gets put into places not on the typical linker search path, so we need to
# hardcode these additional directories it could appear in.
LIBRARY_DIRECTORIES = $(strip $(LUA_LIBDIR) $(UNICORN_LIBDIR) $(PTHREAD_LIBDIR) /usr/lib64 /usr/local/lib)
# The hardcoded version-specific paths here are fallbacks because my IDE can't find the
# Lua headers without them. Is it necessary? No. Will it cause problems? Unlikely. But
# without it, every file is a sea of red squiggles and I'm. Losing. My. Mind.
HEADER_DIRECTORIES = $(strip \
$(SOURCE_DIR) \
$(LUA_INCDIR) \
$(FALLBACK_LUA_INCDIR) \
$(UNICORN_INCDIR) \
/usr/local/include \
/usr/include/lua$(LUA_VERSION) \
/usr/local/include/lua$(LUA_VERSION))
INCLUDE_PATH_FLAGS = $(addprefix -I,$(HEADER_DIRECTORIES))
LIB_PATH_FLAGS = $(addprefix -L,$(LIBRARY_DIRECTORIES))
REQUIRED_LIBS = unicorn pthread stdc++
REQUIRED_LIBS_FLAGS = $(addprefix -l,$(REQUIRED_LIBS))
# LUALIB isn't always provided. This breaks building our tests on LuaJIT, which
# uses a filename other than liblua.a for its library. Thus, -llua won't work on
# LuaJIT (any platform) or Windows (any Lua version).
LINK_TO_LUA_FLAG = $(if $(LUALIB),-l:$(LUALIB),-l$(DEFAULT_LUA_LIB_NAME))
# https://github.com/PowerDNS/pdns/issues/4295
ifeq ($(shell uname -s),Darwin)
ifeq ($(IS_LUAJIT),1)
# This workaround isn't needed for LuaJIT 2.1+
ifeq ($(LUAJIT_VERSION),2.0)
LINK_TO_LUA_FLAG += -pagezero_size 10000 -image_base 100000000
endif
endif
endif
# On MacOS, we need to explicitly tell the compiler to use C++11 because it defaults to an
# older standard. GCC on Linux appears to work fine without it.
CXX_CMD = $(CXX) $(USER_CXX_FLAGS) $(INCLUDE_PATH_FLAGS) -std=c++11
LINK_CMD = $(LD) $(LIB_PATH_FLAGS) $(LDFLAGS)
DOCTEST_TAG = v2.4.11
DOCTEST_HEADER = tests/c/doctest.h
# Uncomment for debugging autogenerated files
# .PRECIOUS: $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES) $(CONSTANT_FILES)
# LIBRARY_DIRECTORIES is a list of the additional paths to search for libraries in at
# runtime. In Make, paths are separated by spaces. In environment variables, the paths are
# separated by colons. This means we need to replace spaces with colons.
#
# Throwaway variables to let us use spaces as an argument:
# https://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions
_empty =
_space = $(_empty) $(_empty)
LIBRARY_COLON_PATHS = $(subst $(_space),:,$(LIBRARY_DIRECTORIES))
export LUA_PATH := $(shell $(LUAROCKS) path --lr-path)
export LUA_CPATH := $(shell $(LUAROCKS) path --lr-cpath)
export PATH := $(shell $(LUAROCKS) path --lr-bin):$(PATH)
# DYLD_FALLBACK_LIBRARY_PATH is for MacOS, LD_LIBRARY_PATH is for all other *NIX systems.
export LD_LIBRARY_PATH := $(LIBRARY_COLON_PATHS):$(LD_LIBRARY_PATH)
export DYLD_FALLBACK_LIBRARY_PATH := $(LIBRARY_COLON_PATHS):$(DYLD_FALLBACK_LIBRARY_PATH)
# This must be the first rule, don't move it.
$(LIB_BUILD_TARGET): $(LIB_OBJECT_FILES)
$(LINK_CMD) $(LIBFLAG) -o $@ $^ $(REQUIRED_LIBS_FLAGS)
$(TEST_EXECUTABLE): $(TEST_CPP_OBJECT_FILES) $(LIB_OBJECT_FILES)
$(LINK_CMD) -o $@ $^ $(REQUIRED_LIBS_FLAGS) $(LINK_TO_LUA_FLAG) -lm
$(SOURCE_DIR)/%_const.cpp: $(UNICORN_INCDIR)/unicorn/%.h
$(LUA) tools/generate_constants.lua $< $@
tests/c/%.$(OBJ_EXTENSION): tests/c/%.cpp $(AUTOGENERATED_HPP_FILES) $(TEST_HEADERS) | $(DOCTEST_HEADER)
$(CXX_CMD) $(CXXFLAGS) -c -o $@ $<
%.$(OBJ_EXTENSION): %.cpp $(AUTOGENERATED_HPP_FILES)
$(CXX_CMD) $(CXXFLAGS) -c -o $@ $<
%.cpp: %.template $(TEMPLATE_DATA_FILES)
$(LUA) tools/render_template.lua -o $@ $^
%.hpp: %.template $(TEMPLATE_DATA_FILES)
$(LUA) tools/render_template.lua -o $@ $^
$(DOCTEST_HEADER):
$(CURL) -sSo $@ https://raw.githubusercontent.com/doctest/doctest/$(DOCTEST_TAG)/doctest/doctest.h
.PHONY: __install
__install: $(LIB_BUILD_TARGET)
$(CP) $^ $(INST_LIBDIR)
.PHONY: all
all: $(LIB_BUILD_TARGET) $(TEST_EXECUTABLE)
.PHONY: clean
clean:
$(RM) $(LIB_OBJECT_FILES) $(CONSTANT_FILES) $(LIB_BUILD_TARGET) \
$(TEST_EXECUTABLE) $(TEST_CPP_OBJECT_FILES) $(DOCTEST_HEADER) \
$(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES)
.PHONY: format
format:
@clang-format --Werror -i --verbose \
$(filter-out $(AUTOGENERATED_CPP_FILES),$(wildcard $(SOURCE_DIR)/*.cpp)) \
$(filter-out $(AUTOGENERATED_HPP_FILES),$(wildcard $(SOURCE_DIR)/unicornlua/*.hpp)) \
$(wildcard tests/c/*.cpp) \
$(wildcard tests/c/*.hpp)
# Convenience target for generating all templated files. This is mostly for
# making IDEs and linters shut up about "missing" files.
.PHONY: autogen-files
autogen-files: $(AUTOGENERATED_CPP_FILES) $(AUTOGENERATED_HPP_FILES) $(CONSTANT_FILES)
.PHONY: __test
__test: $(TEST_EXECUTABLE) $(TEST_LUA_SOURCES) | $(BUSTED)
$(TEST_EXECUTABLE)
$(BUSTED) $(BUSTED_FLAGS) --lua=$(LUA) --shuffle --pattern lua tests/lua