Debian Codegraph @

Makefile

raw
1 # Copyright 2015 Google Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 ################################################################################
16 # User-servicable parts:
17 ################################################################################
18
19 # C/C++ compiler; to use Clang, build with `make CC=clang CXX=clang++`
20 CXX ?= g++
21 CC ?= gcc
22 # coreutils `install` command
23 INSTALL ?= install
24 # The `help2man` command
25 HELP2MAN ?= help2man
26
27 # Directories to place installed files.
28 PREFIX ?= /usr/local
29 MAN1_DIR ?= man/man1
30
31 # Default optimisation level (override by passing OPT=... in the make invocation)
32 OPT ?= -O3
33 # Default flags (can be overriden by passing CXXFLAGS=... in the make invocation)
34 CXXFLAGS ?= -g $(OPT) -Wall -Wextra -Woverloaded-virtual -pedantic -fPIC
35 # Required flags (including -std=c++17)
36 CXXFLAGS += -std=c++17 -Iinclude -Ithird_party/md5 -Ithird_party/json -Ithird_party/rapidyaml/
37 # Default C compilation flags (can be overridden by passing CFLAGS=... in the make invocation)
38 CFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -fPIC
39 # Required C compilation flags (including -std=c99)
40 CFLAGS += -std=c99 -Iinclude
41 # Default flags for linking binaries
42 LDFLAGS ?=
43 # Default flags for linking .so shared objects
44 SHARED_LDFLAGS ?= -shared
45
46 ###
47 # GoogleTest library, from the system.
48 #
49
50 GTEST_PKG := gtest_main
51
52 GTEST_FOUND := $(shell pkg-config --exists '$(GTEST_PKG)' && echo yes || echo no)
53 ifeq ($(GTEST_FOUND),yes)
54 GTEST_CXXFLAGS := $(shell pkg-config --cflags '$(GTEST_PKG)')
55 GTEST_LDFLAGS := $(shell pkg-config --libs '$(GTEST_PKG)')
56 endif
57
58 HELP2MAN_FOUND := $(shell command -v '$(HELP2MAN)' > /dev/null 2>&1 && echo yes || echo no)
59 ifneq ($(HELP2MAN_FOUND),yes)
60 $(warning help2man was not found under the name $(HELP2MAN); manpages will not be built.)
61 endif
62
63 ifeq ($(origin GTEST_ENABLED),undefined)
64 GTEST_ENABLED := $(GTEST_FOUND)
65 ifeq ($(GTEST_ENABLED),no)
66 $(warning GoogleTest package was not found on the system, some tests will be skipped.)
67 endif
68 else ifeq ($(GTEST_ENABLED),$(GTEST_FOUND))
69 # Desired state matches found state; we're fine.
70 else ifeq ($(GTEST_ENABLED),no)
71 # GoogleTest was explicitly disabled; that's fine.
72 else ifeq ($(GTEST_ENABLED),yes)
73 $(error GoogleTest was explicitly requested but is not found on the system.)
74 override GTEST_ENABLED := no
75 else
76 $(error GTEST_ENABLED was set to some invalid value, it must be unset, 'yes', or 'no')
77 endif
78
79 # --- End Google Test Integration ---
80
81 ################################################################################
82 # End of user-servicable parts
83 ################################################################################
84
85 # Disable all GNU Make builtin rules, we want to specify our own.
86 .SUFFIXES:
87
88 # Single sed invocation that finds and extracts the version from the libjsonnet.h header.
89 # Hash and Dollar characters use hex escapes to avoid angering the Makefile interpreter.
90 EXTRACT_VERSION_SED := /^[ \t]*\x23[ \t]*define[ \t]+LIB_JSONNET_VERSION[ \t]+["]v([0-9.]+(-?[a-z][a-z0-9]*)?)["][ \t]*\x24/ { s//\1/p; q; }
91
92 # Extract the library version from libjsonnet.h.
93 VERSION := $(shell sed -nE '$(EXTRACT_VERSION_SED)' include/libjsonnet.h)
94 SOVERSION := 0
95
96 ifeq ($(shell uname -s),Darwin)
97
98 SONAME_FLAG := -install_name
99 TEST_RPATH_FLAG := -Wl,-rpath,'@executable_path'
100
101 else # else assume Linux-like
102
103 SONAME_FLAG := -soname
104 TEST_RPATH_FLAG := -Wl,-rpath,'$$ORIGIN'
105
106 endif # platform switch
107
108 LIB_SRC := \
109 core/desugarer.cpp \
110 core/formatter.cpp \
111 core/lexer.cpp \
112 core/libjsonnet.cpp \
113 core/parser.cpp \
114 core/pass.cpp \
115 core/path_utils.cpp \
116 core/static_analysis.cpp \
117 core/string_utils.cpp \
118 core/vm.cpp \
119 third_party/md5/md5.cpp \
120 third_party/rapidyaml/rapidyaml.cpp
121
122 LIB_OBJ := $(addprefix .makebuild/,$(addsuffix .o,$(LIB_SRC)))
123
124 LIB_CPP_SRC := \
125 cpp/libjsonnet++.cpp
126
127 LIB_CPP_OBJ := $(LIB_OBJ) $(addprefix .makebuild/,$(addsuffix .o,$(LIB_CPP_SRC)))
128
129 BINS_SRC := \
130 cmd/utils.cpp \
131 cmd/jsonnetfmt.cpp \
132 cmd/jsonnet.cpp
133
134 BINS := \
135 jsonnet \
136 jsonnetfmt
137
138 MAN_PAGES := $(addprefix $(MAN1_DIR)/,$(addsuffix .1,$(BINS)))
139
140 LIBS := \
141 libjsonnet.so \
142 libjsonnet.so.$(SOVERSION) \
143 libjsonnet.so.$(VERSION) \
144 libjsonnet++.so \
145 libjsonnet++.so.$(SOVERSION) \
146 libjsonnet++.so.$(VERSION) \
147
148 PUBLIC_HEADERS := \
149 include/libjsonnet.h \
150 include/libjsonnet_fmt.h \
151 include/libjsonnet++.h
152
153 PLAIN_TEST_SRC := \
154 core/libjsonnet_file_test.c \
155 core/libjsonnet_native_callbacks_test.c
156 GTEST_TEST_SRC := \
157 cpp/libjsonnet_locale_test.cpp \
158 cpp/libjsonnet++_test.cpp \
159 core/libjsonnet_test.cpp \
160 core/lexer_test.cpp \
161 core/unicode_test.cpp \
162 core/parser_test.cpp
163
164 PLAIN_TEST_BINS := $(basename $(notdir $(PLAIN_TEST_SRC)))
165 GTEST_TEST_BINS := $(basename $(notdir $(GTEST_TEST_SRC)))
166 ALL_TEST_BINS := $(PLAIN_TEST_BINS) $(GTEST_TEST_BINS)
167
168 DEPS_FILES := $(addprefix .makebuild/,$(addsuffix .o.d,$(LIB_SRC) $(LIB_CPP_SRC) $(BINS_SRC) $(TEST_SRC)))
169 # Intermediate build output directories.
170 BUILD_DIRS := $(sort $(dir $(DEPS_FILES)) .makebuild/stdlib/)
171
172 ifeq ($(GTEST_ENABLED),yes)
173 ENABLED_TEST_BINS := $(ALL_TEST_BINS)
174 else
175 ENABLED_TEST_BINS := $(PLAIN_TEST_BINS)
176 endif
177
178 ################################################################################
179 # Targets / Build rules
180 ################################################################################
181
182 ALL := $(LIBS) $(BINS) $(MAN_PAGES) $(ENABLED_TEST_BINS)
183 .PHONY: default bins libs man all install dist clean
184 default: $(LIBS) $(BINS)
185 bins: $(BINS)
186 libs: $(LIBS)
187 man: $(MAN_PAGES)
188 all: $(ALL)
189
190 test: bins libs $(ENABLED_TEST_BINS)
191 ./tests.sh
192
193 dist:
194 @{ >&2 echo "The dist target is no longer supported, please don't use it." ; exit 1 ; }
195
196 clean:
197 { \
198 rm -df \
199 $(BINS) $(LIBS) $(MAN_PAGES) $(ALL_TEST_BINS) \
200 stdlib/to_c_array core/std.jsonnet.h \
201 && rm -rf .makebuild ;\
202 }
203
204 install: bins libs man
205 { \
206 $(INSTALL) -Dm 644 -t $(DESTDIR)$(PREFIX)/bin/ $(BINS) ;\
207 $(INSTALL) -Dm 644 -t $(DESTDIR)$(PREFIX)/lib/ $(LIBS) ;\
208 $(INSTALL) -Dm 644 -t $(DESTDIR)$(PREFIX)/include/ $(PUBLIC_HEADERS) ;\
209 $(INSTALL) -Dm 644 -t $(DESTDIR)$(PREFIX)/share/$(MAN1_DIR)/ $(MAN_PAGES) ;\
210 }
211
212 CC_DEPS_FLAGS = -MMD -MP -MF "$(addsuffix .d,$@)"
213
214 .makebuild/%.cpp.o: %.cpp
215 mkdir -p $(@D) && $(CXX) $(CC_DEPS_FLAGS) $(CXXFLAGS) -o $@ -c $<
216 .makebuild/%.c.o: %.c
217 mkdir -p $(@D) && $(CC) $(CC_DEPS_FLAGS) $(CFLAGS) -o $@ -c $<
218
219 %.so.$(SOVERSION): %.so.$(VERSION)
220 ln -sf $< $@
221 %.so: %.so.$(SOVERSION)
222 ln -sf $< $@
223
224 ifeq ($(HELP2MAN_FOUND),yes)
225 $(MAN_PAGES): $(MAN1_DIR)/%.1: % | $(MAN1_DIR)
226 $(HELP2MAN) --no-info --output=$@ ./$<
227
228 $(MAN1_DIR):
229 mkdir -p $@
230 else
231 .PHONY: $(MAN1_DIR) $(MAN_PAGES)
232 $(MAN1_DIR) $(MAN_PAGES):
233 @{ >&2 echo "Skipping $@: Cannot build manpages, help2man was not found." ; }
234 endif
235
236 jsonnet: .makebuild/cmd/jsonnet.cpp.o .makebuild/cmd/utils.cpp.o $(LIB_OBJ)
237 $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
238
239 jsonnetfmt: .makebuild/cmd/jsonnetfmt.cpp.o .makebuild/cmd/utils.cpp.o $(LIB_OBJ)
240 $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
241
242 libjsonnet.so.$(VERSION): $(LIB_OBJ)
243 $(CXX) $(LDFLAGS) $(LIB_OBJ) $(SHARED_LDFLAGS) -Wl,$(SONAME_FLAG),libjsonnet.so.$(SOVERSION) -o $@
244
245 libjsonnet++.so.$(VERSION): $(LIB_CPP_OBJ)
246 $(CXX) $(LDFLAGS) $(LIB_CPP_OBJ) $(SHARED_LDFLAGS) -Wl,$(SONAME_FLAG),libjsonnet++.so.$(SOVERSION) -o $@
247
248 # std.jsonnet.h is a generated file so the first build will fail if it isn't
249 # specified as an explicit dependency (after that, deps are known from compiler output)
250 .makebuild/core/desugarer.cpp.o: core/std.jsonnet.h
251
252 # Encode standard library for embedding in C
253 .makebuild/stdlib/to_c_array: stdlib/to_c_array.cpp
254 mkdir -p $(@D) && $(CXX) $(CXXFLAGS) -o "$@" $^
255
256 core/%.jsonnet.h: stdlib/%.jsonnet .makebuild/stdlib/to_c_array
257 .makebuild/stdlib/to_c_array "$<" "$@"
258
259 # Plain-C tests (link to libjsonnet.so); don't use GoogleTest (which is C++ only)
260 libjsonnet_file_test libjsonnet_native_callbacks_test: %: .makebuild/core/%.c.o libjsonnet.so
261 $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(TEST_RPATH_FLAG)
262
263 ###
264 # Tests that use GoogleTest
265 #
266
267 ifeq ($(GTEST_ENABLED),yes)
268
269 # C++ lib tests (links to libjsonnet++.so)
270 libjsonnet_locale_test libjsonnet++_test: %: .makebuild/cpp/%.cpp.o libjsonnet++.so
271 $(CXX) $(CXXFLAGS) $(GTEST_CXXFLAGS) -o $@ $^ $(LDFLAGS) $(GTEST_LDFLAGS) $(TEST_RPATH_FLAG)
272
273 # C++ tests of the C API (link to libjsonnet.so)
274 libjsonnet_test: %: .makebuild/core/%.cpp.o libjsonnet.so
275 $(CXX) $(CXXFLAGS) $(GTEST_CXXFLAGS) -o $@ $^ $(LDFLAGS) $(GTEST_LDFLAGS) $(TEST_RPATH_FLAG)
276
277 # Core component unit tests (narrow deps)
278 lexer_test: %: .makebuild/core/%.cpp.o .makebuild/core/lexer.cpp.o
279 $(CXX) $(CXXFLAGS) $(GTEST_CXXFLAGS) -o $@ $^ $(LDFLAGS) $(GTEST_LDFLAGS)
280 unicode_test: %: .makebuild/core/%.cpp.o
281 $(CXX) $(CXXFLAGS) $(GTEST_CXXFLAGS) -o $@ $^ $(LDFLAGS) $(GTEST_LDFLAGS)
282 parser_test: %: .makebuild/core/%.cpp.o .makebuild/core/parser.cpp.o .makebuild/core/lexer.cpp.o
283 $(CXX) $(CXXFLAGS) $(GTEST_CXXFLAGS) -o $@ $^ $(LDFLAGS) $(GTEST_LDFLAGS)
284
285 endif # GTEST_ENABLED
286
287 #
288 ###
289
290 -include $(DEPS_FILES)