1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5include common.mk
6
7LIBDIR ?= lib
8PRELOADNAME = libminijailpreload.so
9PRELOADPATH = \"/$(LIBDIR)/$(PRELOADNAME)\"
10CPPFLAGS += -DPRELOADPATH="$(PRELOADPATH)"
11
12ifneq ($(HAVE_SECUREBITS_H),no)
13CPPFLAGS += -DHAVE_SECUREBITS_H
14endif
15ifneq ($(USE_seccomp),yes)
16CPPFLAGS += -DUSE_SECCOMP_SOFTFAIL
17endif
18
19CFLAGS += -Wextra -Wno-missing-field-initializers
20CXXFLAGS += -Wextra -Wno-missing-field-initializers
21
22USE_SYSTEM_GTEST ?= no
23ifeq ($(USE_SYSTEM_GTEST),no)
24GTEST_CXXFLAGS := -std=gnu++11
25GTEST_MAIN := gtest_main.a
26GTEST_LIBS := gtest.a
27else
28GTEST_CXXFLAGS := $(gtest-config --cxxflags)
29GTEST_MAIN := -lgtest -lgtest_main
30GTEST_LIBS := $(gtest-config --libs)
31endif
32
33all: CC_BINARY(minijail0) CC_LIBRARY(libminijail.so) \
34	CC_LIBRARY(libminijailpreload.so)
35
36parse_seccomp_policy: CXX_BINARY(parse_seccomp_policy)
37
38tests: TEST(CXX_BINARY(libminijail_unittest)) \
39	TEST(CXX_BINARY(syscall_filter_unittest))
40
41
42CC_BINARY(minijail0): LDLIBS += -lcap -ldl
43CC_BINARY(minijail0): libconstants.gen.o libsyscalls.gen.o libminijail.o \
44		syscall_filter.o signal_handler.o bpf.o util.o elfparse.o \
45		syscall_wrapper.o minijail0.o
46clean: CLEAN(minijail0)
47
48
49CC_LIBRARY(libminijail.so): LDLIBS += -lcap
50CC_LIBRARY(libminijail.so): libminijail.o syscall_filter.o signal_handler.o \
51		bpf.o util.o syscall_wrapper.o libconstants.gen.o \
52		libsyscalls.gen.o
53clean: CLEAN(libminijail.so)
54
55
56CXX_BINARY(libminijail_unittest): CXXFLAGS += -Wno-write-strings \
57						$(GTEST_CXXFLAGS)
58CXX_BINARY(libminijail_unittest): LDLIBS += -lcap $(GTEST_MAIN)
59ifeq ($(USE_SYSTEM_GTEST),no)
60CXX_BINARY(libminijail_unittest): $(GTEST_MAIN)
61endif
62CXX_BINARY(libminijail_unittest): libminijail_unittest.o libminijail.o \
63		syscall_filter.o signal_handler.o bpf.o util.o \
64		syscall_wrapper.o libconstants.gen.o libsyscalls.gen.o
65clean: CLEAN(libminijail_unittest)
66
67
68CC_LIBRARY(libminijailpreload.so): LDLIBS += -lcap -ldl
69CC_LIBRARY(libminijailpreload.so): libminijailpreload.o libminijail.o \
70		libconstants.gen.o libsyscalls.gen.o syscall_filter.o \
71		signal_handler.o bpf.o util.o syscall_wrapper.o
72clean: CLEAN(libminijailpreload.so)
73
74
75CXX_BINARY(syscall_filter_unittest): CXXFLAGS += -Wno-write-strings \
76						$(GTEST_CXXFLAGS)
77CXX_BINARY(syscall_filter_unittest): LDLIBS += -lcap $(GTEST_MAIN)
78ifeq ($(USE_SYSTEM_GTEST),no)
79CXX_BINARY(syscall_filter_unittest): $(GTEST_MAIN)
80endif
81CXX_BINARY(syscall_filter_unittest): syscall_filter_unittest.o \
82		syscall_filter.o bpf.o util.o libconstants.gen.o \
83		libsyscalls.gen.o
84clean: CLEAN(syscall_filter_unittest)
85
86
87CXX_BINARY(parse_seccomp_policy): parse_seccomp_policy.o syscall_filter.o \
88		bpf.o util.o libconstants.gen.o libsyscalls.gen.o
89clean: CLEAN(parse_policy)
90
91
92libsyscalls.gen.o: CPPFLAGS += -I$(SRC)
93
94libsyscalls.gen.o.depends: libsyscalls.gen.c
95
96# Only regenerate libsyscalls.gen.c if the Makefile or header changes.
97# NOTE! This will not detect if the file is not appropriate for the target.
98libsyscalls.gen.c: $(SRC)/Makefile $(SRC)/libsyscalls.h
99	@printf "Generating target-arch specific $@...\n"
100	$(QUIET)$(SRC)/gen_syscalls.sh "$(CC)" "$@"
101	@printf "$@ done.\n"
102clean: CLEAN(libsyscalls.gen.c)
103
104$(eval $(call add_object_rules,libsyscalls.gen.o,CC,c,CFLAGS))
105
106libconstants.gen.o: CPPFLAGS += -I$(SRC)
107
108libconstants.gen.o.depends: libconstants.gen.c
109
110# Only regenerate libconstants.gen.c if the Makefile or header changes.
111# NOTE! This will not detect if the file is not appropriate for the target.
112libconstants.gen.c: $(SRC)/Makefile $(SRC)/libconstants.h
113	@printf "Generating target-arch specific $@...\n"
114	$(QUIET)$(SRC)/gen_constants.sh "$(CC)" "$@"
115	@printf "$@ done.\n"
116clean: CLEAN(libconstants.gen.c)
117
118$(eval $(call add_object_rules,libconstants.gen.o,CC,c,CFLAGS))
119
120
121################################################################################
122# Google Test
123
124ifeq ($(USE_SYSTEM_GTEST),no)
125# Points to the root of Google Test, relative to where this file is.
126# Remember to tweak this if you move this file.
127GTEST_DIR = googletest-release-1.8.0/googletest
128
129# Flags passed to the preprocessor.
130# Set Google Test's header directory as a system directory, such that
131# the compiler doesn't generate warnings in Google Test headers.
132CPPFLAGS += -isystem $(GTEST_DIR)/include
133
134# Flags passed to the C++ compiler.
135GTEST_CXXFLAGS += -pthread
136
137# All Google Test headers.  Usually you shouldn't change this
138# definition.
139GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
140		$(GTEST_DIR)/include/gtest/internal/*.h
141
142# House-keeping build targets.
143clean: clean_gtest
144
145clean_gtest:
146	rm -f gtest.a gtest_main.a *.o
147
148# Builds gtest.a and gtest_main.a.
149
150# Usually you shouldn't tweak such internal variables, indicated by a
151# trailing _.
152GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
153
154# For simplicity and to avoid depending on Google Test's
155# implementation details, the dependencies specified below are
156# conservative and not optimized.  This is fine as Google Test
157# compiles fast and for ordinary users its source rarely changes.
158gtest-all.o : $(GTEST_SRCS_)
159	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) $(GTEST_CXXFLAGS) -c \
160		$(GTEST_DIR)/src/gtest-all.cc -o $@
161
162gtest_main.o : $(GTEST_SRCS_)
163	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) $(GTEST_CXXFLAGS) -c \
164		$(GTEST_DIR)/src/gtest_main.cc -o $@
165
166gtest.a : gtest-all.o
167	$(AR) $(ARFLAGS) $@ $^
168
169gtest_main.a : gtest-all.o gtest_main.o
170	$(AR) $(ARFLAGS) $@ $^
171
172endif
173################################################################################
174