blob: 7723c121f244c15ded2ce0feabcbaf50eb8c81d7 [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
Sonny Raob5d537d2011-10-21 22:17:13 +00005LIBDIR = lib
6PRELOADPATH = \"/$(LIBDIR)/libminijailpreload.so\"
Elly Jonescd7a9042011-07-22 13:56:51 -04007CFLAGS += -fPIC -Wall -Wextra -Werror -DPRELOADPATH="$(PRELOADPATH)"
Will Drewry6ac91122011-10-21 16:38:58 -05008CFLAGS += -fvisibility=internal
Elly Jonescd7a9042011-07-22 13:56:51 -04009
Ben Chan45397012011-08-23 08:15:03 -070010all : minijail0 libminijail.so libminijailpreload.so
Elly Jonescd7a9042011-07-22 13:56:51 -040011
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070012tests : libminijail_unittest.wrapper syscall_filter_unittest
Will Drewry6ac91122011-10-21 16:38:58 -050013
Will Drewry32ac9f52011-08-18 21:36:27 -050014minijail0 : libsyscalls.gen.o libminijail.o minijail0.c
Elly Jonescd7a9042011-07-22 13:56:51 -040015 $(CC) $(CFLAGS) -o $@ $^ -lcap
16
Will Drewry32ac9f52011-08-18 21:36:27 -050017libminijail.so : libminijail.o libsyscalls.gen.o
Ben Chan45397012011-08-23 08:15:03 -070018 $(CC) $(CFLAGS) -shared -o $@ $^ -lcap
19
Will Drewry6ac91122011-10-21 16:38:58 -050020# Allow unittests to access what are normally internal symbols.
21libminijail_unittest.wrapper :
22 $(MAKE) $(MAKEARGS) test-clean
23 $(MAKE) $(MAKEARGS) libminijail_unittest
24 $(MAKE) $(MAKEARGS) test-clean
25
26libminijail_unittest : CFLAGS := $(filter-out -fvisibility=%,$(CFLAGS))
Will Drewrydecdfdc2011-09-27 15:13:54 -050027libminijail_unittest : libminijail_unittest.o libminijail.o libsyscalls.gen.o
Will Drewry6ac91122011-10-21 16:38:58 -050028 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $(CFLAGS_FILE),$^) -lcap
Will Drewrydecdfdc2011-09-27 15:13:54 -050029
Will Drewry32ac9f52011-08-18 21:36:27 -050030libminijailpreload.so : libminijailpreload.c libsyscalls.gen.o libminijail.o
Elly Jonescd7a9042011-07-22 13:56:51 -040031 $(CC) $(CFLAGS) -shared -o $@ $^ -ldl -lcap
32
Thieu Lef0ef52e2011-09-14 14:03:09 -070033libminijail.o : libminijail.c libminijail.h
Will Drewry32ac9f52011-08-18 21:36:27 -050034
Will Drewrydecdfdc2011-09-27 15:13:54 -050035libminijail_unittest.o : libminijail_unittest.c test_harness.h
36 $(CC) $(CFLAGS) -c -o $@ $<
37
Will Drewry32ac9f52011-08-18 21:36:27 -050038libsyscalls.gen.o : libsyscalls.gen.c libsyscalls.h
39
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070040syscall_filter_unittest : syscall_filter_unittest.o syscall_filter.o bpf.o \
41 test_harness.h
42 $(CC) $(CFLAGS) -o $@ $^
43
44syscall_filter_unittest.o : syscall_filter_unittest.c test_harness.h
45 $(CC) $(CFLAGS) -c -o $@ $<
46
47syscall_filter.o : syscall_filter.c
48
49bpf.o : bpf.c
50
Will Drewry32ac9f52011-08-18 21:36:27 -050051# sed expression which extracts system calls that are
52# defined via asm/unistd.h. It converts them from:
53# #define __NR_read
54# to:
55# #ifdef __NR_read
56# { "read", __NR_read },
57# #endif
58# All other lines will not be emitted. The sed expression lives in its
59# own macro to allow clean line wrapping.
60define sed-multiline
61 's/#define \(__NR_\)\([a-z0-9_]*\)$$/#ifdef \1\2\n\
62 { "\2", \1\2 },\n#endif/g p;'
63endef
64
65# Generates a header file with a system call table made up of "name",
66# syscall_nr entries by including the build target <asm/unistd.h> and
67# emitting the list of defines. Use of the compiler is needed to
68# dereference the actual provider of syscall definitions.
69# E.g., asm/unistd_32.h or asm/unistd_64.h, etc.
70define gen_syscalls
71 (set -e; \
72 echo '/* GENERATED BY MAKEFILE */'; \
73 echo '#include <stddef.h>'; \
74 echo '#include <asm/unistd.h>'; \
75 echo '#include "libsyscalls.h"'; \
76 echo "const struct syscall_entry syscall_table[] = {"; \
77 echo "#include <asm/unistd.h>" | \
78 $(CC) $(CFLAGS) -dN - -E | sed -ne $(sed-multiline); \
79 echo " { NULL, -1 },"; \
80 echo "};" ) > $1
81endef
82
83# Only regenerate libsyscalls.gen.c if the Makefile or header changes.
84# NOTE! This will not detect if the file is not appropriate for the target.
85libsyscalls.gen.c : Makefile libsyscalls.h
86 @printf "Generating target-arch specific $@ . . . "
87 @$(call gen_syscalls,$@)
88 @printf "done.\n"
89
Will Drewry6ac91122011-10-21 16:38:58 -050090# Only clean up files affected by the CFLAGS change for testing.
91test-clean :
92 @rm -f libminijail.o libminijail_unittest.o libsyscalls.gen.o
93
94clean : test-clean
Will Drewry32ac9f52011-08-18 21:36:27 -050095 @rm -f libminijail.o libminijailpreload.so minijail0
Will Drewry6ac91122011-10-21 16:38:58 -050096 @rm -f libminijail.so
97 @rm -f libminijail_unittest
98 @rm -f libsyscalls.gen.c
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070099 @rm -f syscall_filter.o bpf.o
100 @rm -f syscall_filter_unittest syscall_filter_unittest.o