blob: b7022b5a17ed6b759021562bf72b90c660db12c9 [file] [log] [blame]
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Kees Cookddb79702017-04-12 16:36:45 -07005BASE_VER=0
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -07006include common.mk
7
Luis Hector Chavezf6423bb2018-10-05 14:20:29 -07008LIBDIR ?= /lib
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07009PRELOADNAME = libminijailpreload.so
Allen Webbee876072019-02-21 10:56:21 -080010PRELOADPATH = "$(LIBDIR)/$(PRELOADNAME)"
11CPPFLAGS += -DPRELOADPATH='$(PRELOADPATH)'
12
13# Defines the pivot root path used by the minimalistic-mountns profile.
14DEFAULT_PIVOT_ROOT ?= /var/empty
15CPPFLAGS += -DDEFAULT_PIVOT_ROOT='"$(DEFAULT_PIVOT_ROOT)"'
Elly Jonescd7a9042011-07-22 13:56:51 -040016
Jorge Lucangeli Obes6a600a42017-04-07 15:53:06 -040017ifeq ($(USE_seccomp),no)
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070018CPPFLAGS += -DUSE_SECCOMP_SOFTFAIL
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070019endif
Lei Zhangeee31552012-10-17 21:27:10 -070020
Mike Frysinger916c6c32018-09-27 14:17:53 -040021# Allow people to use -L and related flags.
22ALLOW_DEBUG_LOGGING ?= yes
23ifeq ($(ALLOW_DEBUG_LOGGING),yes)
24CPPFLAGS += -DALLOW_DEBUG_LOGGING
25endif
26
Luis Hector Chavezdaa03712017-09-06 08:10:33 -070027ifeq ($(USE_ASAN),yes)
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -080028CPPFLAGS += -fsanitize=address -fno-omit-frame-pointer
29LDFLAGS += -fsanitize=address -fno-omit-frame-pointer
Luis Hector Chavezdaa03712017-09-06 08:10:33 -070030USE_EXIT_ON_DIE = yes
31endif
32
33# Setting this flag can be useful for both AddressSanitizer builds and running
34# fuzzing tools, which do not expect crashes on gracefully-handled malformed
35# inputs.
36ifeq ($(USE_EXIT_ON_DIE),yes)
37CPPFLAGS += -DUSE_EXIT_ON_DIE
38endif
39
Nicole Anderson-Aubcc8cfd2020-11-10 20:33:27 +000040# Setting this flag allows duplicate syscalls definitions for seccomp filters.
41ifeq ($(ALLOW_DUPLICATE_SYSCALLS),yes)
42CPPFLAGS += -DALLOW_DUPLICATE_SYSCALLS
43endif
44
Mike Frysingere131e132017-10-04 13:27:55 -040045MJ_COMMON_FLAGS = -Wunused-parameter -Wextra -Wno-missing-field-initializers
46CFLAGS += $(MJ_COMMON_FLAGS)
47CXXFLAGS += $(MJ_COMMON_FLAGS)
Jorge Lucangeli Obesce2c3ff2017-02-01 18:04:36 -050048
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050049USE_SYSTEM_GTEST ?= no
50ifeq ($(USE_SYSTEM_GTEST),no)
Mike Frysingere1f046a2017-10-04 13:27:18 -040051GTEST_CXXFLAGS := -std=gnu++14
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050052GTEST_LIBS := gtest.a
53else
Allen Webbf8c21ea2019-06-14 09:24:10 -070054GTEST_CXXFLAGS := $(shell gtest-config --cxxflags 2>/dev/null || \
55 echo "-pthread")
56GTEST_LIBS := $(shell gtest-config --libs 2>/dev/null || \
57 echo "-lgtest -pthread -lpthread")
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050058endif
59
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040060CORE_OBJECT_FILES := libminijail.o syscall_filter.o signal_handler.o \
61 bpf.o util.o system.o syscall_wrapper.o \
62 libconstants.gen.o libsyscalls.gen.o
63
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070064all: CC_BINARY(minijail0) CC_LIBRARY(libminijail.so) \
Zach Reizner2a93cee2019-02-01 15:13:09 -080065 CC_LIBRARY(libminijailpreload.so)
Elly Jonescd7a9042011-07-22 13:56:51 -040066
Jorge Lucangeli Obesf16d6d12016-09-29 20:25:27 -040067parse_seccomp_policy: CXX_BINARY(parse_seccomp_policy)
Luis Hector Chavezcf504ca2020-01-30 21:39:13 -080068dump_constants: CXX_STATIC_BINARY(dump_constants)
Jorge Lucangeli Obesf16d6d12016-09-29 20:25:27 -040069
Jorge Lucangeli Obese0c5a762017-02-02 15:17:08 -050070tests: TEST(CXX_BINARY(libminijail_unittest)) \
Mike Frysinger4d2a81e2018-01-22 16:43:33 -050071 TEST(CXX_BINARY(minijail0_cli_unittest)) \
Mike Frysinger0b5cffa2017-08-15 18:06:18 -040072 TEST(CXX_BINARY(syscall_filter_unittest)) \
Mike Frysinger32c39922018-01-17 17:09:54 -050073 TEST(CXX_BINARY(system_unittest)) \
74 TEST(CXX_BINARY(util_unittest)) \
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050075
Will Drewry6ac91122011-10-21 16:38:58 -050076
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070077CC_BINARY(minijail0): LDLIBS += -lcap -ldl
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050078CC_BINARY(minijail0): $(CORE_OBJECT_FILES) \
79 elfparse.o minijail0.o minijail0_cli.o
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070080clean: CLEAN(minijail0)
Elly Jonescd7a9042011-07-22 13:56:51 -040081
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050082
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070083CC_LIBRARY(libminijail.so): LDLIBS += -lcap
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040084CC_LIBRARY(libminijail.so): $(CORE_OBJECT_FILES)
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070085clean: CLEAN(libminijail.so)
Ben Chan45397012011-08-23 08:15:03 -070086
Stephen Barber27c58232019-12-09 17:20:28 -080087CC_STATIC_LIBRARY(libminijail.pic.a): $(CORE_OBJECT_FILES)
88CC_STATIC_LIBRARY(libminijail.pie.a): $(CORE_OBJECT_FILES)
89clean: CLEAN(libminijail.*.a)
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050090
91CXX_BINARY(libminijail_unittest): CXXFLAGS += -Wno-write-strings \
92 $(GTEST_CXXFLAGS)
Luis Hector Chavez114a9302017-09-05 20:36:58 -070093CXX_BINARY(libminijail_unittest): LDLIBS += -lcap $(GTEST_LIBS)
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050094ifeq ($(USE_SYSTEM_GTEST),no)
Luis Hector Chavez114a9302017-09-05 20:36:58 -070095CXX_BINARY(libminijail_unittest): $(GTEST_LIBS)
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -050096endif
Luis Hector Chavez114a9302017-09-05 20:36:58 -070097CXX_BINARY(libminijail_unittest): libminijail_unittest.o $(CORE_OBJECT_FILES) \
98 testrunner.o
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -070099clean: CLEAN(libminijail_unittest)
Will Drewrydecdfdc2011-09-27 15:13:54 -0500100
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700101TEST(CXX_BINARY(libminijail_unittest)): CC_LIBRARY(libminijailpreload.so)
102
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500103
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700104CC_LIBRARY(libminijailpreload.so): LDLIBS += -lcap -ldl
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400105CC_LIBRARY(libminijailpreload.so): libminijailpreload.o $(CORE_OBJECT_FILES)
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700106clean: CLEAN(libminijailpreload.so)
Elly Jonescd7a9042011-07-22 13:56:51 -0400107
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500108
Mike Frysinger4d2a81e2018-01-22 16:43:33 -0500109CXX_BINARY(minijail0_cli_unittest): CXXFLAGS += $(GTEST_CXXFLAGS)
110CXX_BINARY(minijail0_cli_unittest): LDLIBS += -lcap $(GTEST_LIBS)
111ifeq ($(USE_SYSTEM_GTEST),no)
112CXX_BINARY(minijail0_cli_unittest): $(GTEST_LIBS)
113endif
114CXX_BINARY(minijail0_cli_unittest): minijail0_cli_unittest.o \
115 $(CORE_OBJECT_FILES) minijail0_cli.o elfparse.o testrunner.o
116clean: CLEAN(minijail0_cli_unittest)
117
118
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500119CXX_BINARY(syscall_filter_unittest): CXXFLAGS += -Wno-write-strings \
120 $(GTEST_CXXFLAGS)
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700121CXX_BINARY(syscall_filter_unittest): LDLIBS += -lcap $(GTEST_LIBS)
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500122ifeq ($(USE_SYSTEM_GTEST),no)
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700123CXX_BINARY(syscall_filter_unittest): $(GTEST_LIBS)
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500124endif
125CXX_BINARY(syscall_filter_unittest): syscall_filter_unittest.o \
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700126 $(CORE_OBJECT_FILES) testrunner.o
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700127clean: CLEAN(syscall_filter_unittest)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700128
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500129
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400130CXX_BINARY(system_unittest): CXXFLAGS += $(GTEST_CXXFLAGS)
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700131CXX_BINARY(system_unittest): LDLIBS += -lcap $(GTEST_LIBS)
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400132ifeq ($(USE_SYSTEM_GTEST),no)
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700133CXX_BINARY(system_unittest): $(GTEST_LIBS)
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400134endif
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700135CXX_BINARY(system_unittest): system_unittest.o \
136 $(CORE_OBJECT_FILES) testrunner.o
Mike Frysinger0b5cffa2017-08-15 18:06:18 -0400137clean: CLEAN(system_unittest)
138
139
Mike Frysinger32c39922018-01-17 17:09:54 -0500140CXX_BINARY(util_unittest): CXXFLAGS += $(GTEST_CXXFLAGS)
141CXX_BINARY(util_unittest): LDLIBS += -lcap $(GTEST_LIBS)
142ifeq ($(USE_SYSTEM_GTEST),no)
143CXX_BINARY(util_unittest): $(GTEST_LIBS)
144endif
145CXX_BINARY(util_unittest): util_unittest.o \
146 $(CORE_OBJECT_FILES) testrunner.o
147clean: CLEAN(util_unittest)
148
149
Jorge Lucangeli Obesf16d6d12016-09-29 20:25:27 -0400150CXX_BINARY(parse_seccomp_policy): parse_seccomp_policy.o syscall_filter.o \
151 bpf.o util.o libconstants.gen.o libsyscalls.gen.o
Jorge Lucangeli Obesa1f4e0a2017-09-05 10:06:55 -0400152clean: CLEAN(parse_seccomp_policy)
Jorge Lucangeli Obesf16d6d12016-09-29 20:25:27 -0400153
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500154
Luis Hector Chavezcf504ca2020-01-30 21:39:13 -0800155# Compiling dump_constants as a static executable makes it easy to run under
156# qemu-user, which in turn simplifies cross-compiling bpf policies.
157CXX_STATIC_BINARY(dump_constants): dump_constants.o \
Luis Hector Chavezf7b20182018-10-28 21:39:32 -0700158 libconstants.gen.o libsyscalls.gen.o
159clean: CLEAN(dump_constants)
160
161
Luis Hector Chavezcf504ca2020-01-30 21:39:13 -0800162constants.json: CXX_STATIC_BINARY(dump_constants)
Luis Hector Chavezf7b20182018-10-28 21:39:32 -0700163 ./dump_constants > $@
164clean: CLEANFILE(constants.json)
165
166
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700167libsyscalls.gen.o: CPPFLAGS += -I$(SRC)
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700168
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700169libsyscalls.gen.o.depends: libsyscalls.gen.c
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700170
Will Drewry32ac9f52011-08-18 21:36:27 -0500171# Only regenerate libsyscalls.gen.c if the Makefile or header changes.
172# NOTE! This will not detect if the file is not appropriate for the target.
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700173libsyscalls.gen.c: $(SRC)/Makefile $(SRC)/libsyscalls.h
Jorge Lucangeli Obesae57f092016-11-23 15:04:55 -0500174 @printf "Generating target-arch specific $@...\n"
Dan Willemsen2acbec52017-09-14 17:28:36 -0700175 $(QUIET)CC="$(CC)" $(SRC)/gen_syscalls.sh "$@"
Jorge Lucangeli Obesae57f092016-11-23 15:04:55 -0500176 @printf "$@ done.\n"
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700177clean: CLEAN(libsyscalls.gen.c)
Will Drewry32ac9f52011-08-18 21:36:27 -0500178
Jorge Lucangeli Obesbc9dc1c2014-08-25 09:12:36 -0700179$(eval $(call add_object_rules,libsyscalls.gen.o,CC,c,CFLAGS))
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700180
181libconstants.gen.o: CPPFLAGS += -I$(SRC)
182
183libconstants.gen.o.depends: libconstants.gen.c
184
185# Only regenerate libconstants.gen.c if the Makefile or header changes.
186# NOTE! This will not detect if the file is not appropriate for the target.
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700187libconstants.gen.c: $(SRC)/Makefile $(SRC)/libconstants.h
Jorge Lucangeli Obesae57f092016-11-23 15:04:55 -0500188 @printf "Generating target-arch specific $@...\n"
Dan Willemsen2acbec52017-09-14 17:28:36 -0700189 $(QUIET)CC="$(CC)" $(SRC)/gen_constants.sh "$@"
Jorge Lucangeli Obesae57f092016-11-23 15:04:55 -0500190 @printf "$@ done.\n"
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700191clean: CLEAN(libconstants.gen.c)
192
193$(eval $(call add_object_rules,libconstants.gen.o,CC,c,CFLAGS))
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500194
195
196################################################################################
197# Google Test
198
199ifeq ($(USE_SYSTEM_GTEST),no)
200# Points to the root of Google Test, relative to where this file is.
201# Remember to tweak this if you move this file.
Luis Héctor Chávez4f830bd2021-01-03 05:47:11 -0800202GTEST_DIR = googletest-release-1.10.0/googletest
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500203
204# Flags passed to the preprocessor.
205# Set Google Test's header directory as a system directory, such that
206# the compiler doesn't generate warnings in Google Test headers.
207CPPFLAGS += -isystem $(GTEST_DIR)/include
208
209# Flags passed to the C++ compiler.
Jorge Lucangeli Obese0c5a762017-02-02 15:17:08 -0500210GTEST_CXXFLAGS += -pthread
Jorge Lucangeli Obes9bd316b2017-01-26 17:21:16 -0500211
212# All Google Test headers. Usually you shouldn't change this
213# definition.
214GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
215 $(GTEST_DIR)/include/gtest/internal/*.h
216
217# House-keeping build targets.
218clean: clean_gtest
219
220clean_gtest:
221 rm -f gtest.a gtest_main.a *.o
222
223# Builds gtest.a and gtest_main.a.
224
225# Usually you shouldn't tweak such internal variables, indicated by a
226# trailing _.
227GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
228
229# For simplicity and to avoid depending on Google Test's
230# implementation details, the dependencies specified below are
231# conservative and not optimized. This is fine as Google Test
232# compiles fast and for ordinary users its source rarely changes.
233gtest-all.o : $(GTEST_SRCS_)
234 $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) $(GTEST_CXXFLAGS) -c \
235 $(GTEST_DIR)/src/gtest-all.cc -o $@
236
237gtest_main.o : $(GTEST_SRCS_)
238 $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) $(GTEST_CXXFLAGS) -c \
239 $(GTEST_DIR)/src/gtest_main.cc -o $@
240
241gtest.a : gtest-all.o
242 $(AR) $(ARFLAGS) $@ $^
243
244gtest_main.a : gtest-all.o gtest_main.o
245 $(AR) $(ARFLAGS) $@ $^
246
247endif
248################################################################################