-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
193 lines (163 loc) · 7.1 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
# Copyright (c) 2019-2020 Status Research & Development GmbH. Licensed under
# either of:
# - Apache License, version 2.0
# - MIT license
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
SHELL := bash # the shell used internally by Make
NIM_PARAMS := -f --outdir:build --skipParentCfg:on --skipUserCfg:on $(NIMFLAGS)
BUILD_MSG := "\\x1B[92mBuilding:\\x1B[39m"
CMAKE := cmake
CMAKE_MISSING_MSG := "CMake not installed. Aborting."
# verbosity level
V := 0
NIM_PARAMS := $(NIM_PARAMS) --verbosity:$(V)
HANDLE_OUTPUT :=
SILENT_TARGET_PREFIX := disabled
ifeq ($(V), 0)
NIM_PARAMS := $(NIM_PARAMS) --hints:off
HANDLE_OUTPUT := &>/dev/null
SILENT_TARGET_PREFIX :=
endif
USE_SYSTEM_LIBS := 0
ifneq ($(USE_SYSTEM_LIBS), 0)
NIM_PARAMS := $(NIM_PARAMS) -d:libbacktraceUseSystemLibs
endif
ECHO_AND_RUN = echo -e "\n$(CMD)\n"; $(CMD) $(MACOS_DEBUG_SYMBOLS) && ./build/$@
LIBDIR := install/usr/lib
INCLUDEDIR := install/usr/include
CFLAGS += -g -O3 -std=gnu11 -pipe -Wall -Wextra -fPIC
CXXFLAGS += -g -O3 -std=gnu++11 -pipe -Wall -Wextra -fPIC
CPPFLAGS := -I"$(CURDIR)/$(INCLUDEDIR)"
LDLIBS := -L"$(CURDIR)/$(LIBDIR)"
AR := ar
# for Mingw-w64
CC := gcc
CXX := g++
TESTS := test1 \
test2
.PHONY: all \
clean \
test \
$(TESTS)
ifeq ($(USE_SYSTEM_LIBS), 0)
LIBBACKTRACE_DEP := $(LIBDIR)/libbacktrace.a
else
LIBBACKTRACE_DEP :=
endif
all: $(LIBBACKTRACE_DEP) $(LIBDIR)/libbacktracenim.a
BUILD_CXX_LIB := 1
ifeq ($(BUILD_CXX_LIB), 1)
all: $(LIBDIR)/libbacktracenimcpp.a
endif
$(LIBDIR)/libbacktracenim.a: libbacktrace_wrapper.o | $(LIBDIR)
echo -e $(BUILD_MSG) "$@" && \
rm -f $@ && \
$(AR) rcs $@ $<
# it doesn't link to libbacktrace.a, but it needs the headers installed by that target
libbacktrace_wrapper.o: libbacktrace_wrapper.c libbacktrace_wrapper.h $(LIBBACKTRACE_DEP)
$(LIBDIR)/libbacktracenimcpp.a: libbacktrace_wrapper_cpp.o | $(LIBDIR)
echo -e $(BUILD_MSG) "$@" && \
rm -f $@ && \
$(AR) rcs $@ $<
# implicit rule doesn't kick in
libbacktrace_wrapper_cpp.o: libbacktrace_wrapper.cpp libbacktrace_wrapper.c libbacktrace_wrapper.h $(LIBBACKTRACE_DEP)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
$(LIBDIR):
mkdir -p $@
#########
# macOS #
#########
ifeq ($(shell uname), Darwin)
# TODO: disable when this issue is fixed: https://github.com/nim-lang/Nim/issues/12735
MACOS_DEBUG_SYMBOLS = && dsymutil build/$@
BUILD_MSG := "Building:"
CMAKE_MISSING_MSG := "CMake not installed. Please run 'brew install cmake'. Aborting."
#- macOS Clang needs the LLVM libunwind variant
# (GCC comes with its own, in libgcc_s.so.1, used even by Clang itself, on other platforms)
USE_VENDORED_LIBUNWIND := 1
endif # macOS
###########
# Windows #
###########
ifeq ($(OS), Windows_NT)
BUILD_MSG := "Building:"
CPPFLAGS += -D__STDC_FORMAT_MACROS -D_WIN32_WINNT=0x0600
CMAKE_ARGS := -G "MinGW Makefiles" -DCMAKE_SH="CMAKE_SH-NOTFOUND"
# the GCC one doesn't seem to work on 64 bit Windows
USE_VENDORED_LIBUNWIND := 1
LIBBACKTRACE_SED := sed -i 's/\$$[({]SHELL[)}]/"$$(SHELL)"/g' Makefile
else
LIBBACKTRACE_SED := true
endif # Windows
ifeq ($(USE_VENDORED_LIBUNWIND), 1)
# libbacktrace needs to find libunwind during compilation
export CPPFLAGS
export LDLIBS
# CMake ignores CPPFLAGS in the environment
export CFLAGS += $(CPPFLAGS)
export CXXFLAGS += $(CPPFLAGS)
#- this library doesn't support parallel builds, hence the "-j1"
#- the "Git for Windows" Bash is usually installed in a path with spaces, which messes up the Makefile. Add quotes.
$(LIBDIR)/libbacktrace.a: $(LIBDIR)/libunwind.a
else # USE_VENDORED_LIBUNWIND
export CFLAGS
export CXXFLAGS
endif # USE_VENDORED_LIBUNWIND
# We need to enable cross-compilation here, by passing "--build" and "--host"
# to "./configure". We already set CC in the environment, so it doesn't matter
# what the target host is, as long as it's a valid one.
$(LIBDIR)/libbacktrace.a:
+ echo -e $(BUILD_MSG) "$@" && \
cd vendor/libbacktrace-upstream && \
./configure --prefix="/usr" --libdir="/usr/lib" --disable-shared --enable-static \
--with-pic --build=$(./config.guess) --host=arm MAKE="$(MAKE)" $(HANDLE_OUTPUT) && \
$(LIBBACKTRACE_SED) && \
$(MAKE) -j1 DESTDIR="$(CURDIR)/install" clean all install $(HANDLE_OUTPUT)
# DESTDIR does not work on Windows for a CMake-generated Makefile
$(LIBDIR)/libunwind.a:
+ echo -e $(BUILD_MSG) "$@"; \
which $(CMAKE) &>/dev/null || { echo $(CMAKE_MISSING_MSG); exit 1; }; \
cd vendor/libunwind && \
rm -rf build && \
$(CMAKE) -S runtimes \
-DLLVM_ENABLE_RUNTIMES="libunwind" \
-DLIBUNWIND_ENABLE_SHARED=OFF -DLIBUNWIND_ENABLE_STATIC=ON \
-DLIBUNWIND_INCLUDE_DOCS=OFF \
-DCMAKE_INSTALL_PREFIX="$(CURDIR)/install/usr" \
$(CMAKE_ARGS) build $(HANDLE_OUTPUT) && \
$(MAKE) -C build VERBOSE=$(V) clean $(HANDLE_OUTPUT) && \
$(MAKE) -C build VERBOSE=$(V) unwind_static $(HANDLE_OUTPUT) && \
$(MAKE) -C build VERBOSE=$(V) install-unwind $(HANDLE_OUTPUT)
test: $(TESTS)
$(TESTS): all
$(eval CMD := nim c $(NIM_PARAMS) tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native --stackTrace:off tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:debug tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release -d:nimStackTraceOverride tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:danger tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:danger -d:nimStackTraceOverride tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release --gcc.options.debug:'-g1' -d:nimStackTraceOverride tests/[email protected]) $(ECHO_AND_RUN)
ifeq ($(shell uname), Darwin)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release --passC:-flto=thin --passL:"-flto=thin -Wl,-object_path_lto,build/[email protected]" tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release -d:nimStackTraceOverride --passC:-flto=thin --passL:"-flto=thin -Wl,-object_path_lto,build/[email protected]" tests/[email protected]) $(ECHO_AND_RUN)
else
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release --passC:-flto=auto --passL:-flto=auto tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim c $(NIM_PARAMS) --debugger:native -d:release -d:nimStackTraceOverride --passC:-flto=auto --passL:-flto=auto tests/[email protected]) $(ECHO_AND_RUN)
endif
ifeq ($(BUILD_CXX_LIB), 1)
# for the C++ backend:
$(eval CMD := nim cpp $(NIM_PARAMS) --debugger:native tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim cpp $(NIM_PARAMS) --debugger:native -d:release -d:nimStackTraceOverride tests/[email protected]) $(ECHO_AND_RUN)
$(eval CMD := nim cpp $(NIM_PARAMS) --debugger:native -d:release --gcc.cpp.options.debug:'-g1' -d:nimStackTraceOverride tests/[email protected]) $(ECHO_AND_RUN)
endif
clean:
rm -rf install build *.o
cd vendor/libbacktrace-upstream && \
{ [[ -e Makefile ]] && $(MAKE) clean $(HANDLE_OUTPUT) || true; }
cd vendor/libunwind && \
{ [[ -e Makefile ]] && $(MAKE) clean $(HANDLE_OUTPUT) || true; } && \
rm -rf build
$(SILENT_TARGET_PREFIX).SILENT: