blob: ea7f8b145a43d338f8be4ad2d652e5dedba702a9 [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
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070014minijail0 : libsyscalls.gen.o libminijail.o syscall_filter.o bpf.o util.o \
15 minijail0.c
Elly Jonescd7a9042011-07-22 13:56:51 -040016 $(CC) $(CFLAGS) -o $@ $^ -lcap
17
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070018libminijail.so : libminijail.o syscall_filter.o bpf.o util.o libsyscalls.gen.o
Ben Chan45397012011-08-23 08:15:03 -070019 $(CC) $(CFLAGS) -shared -o $@ $^ -lcap
20
Will Drewry6ac91122011-10-21 16:38:58 -050021# Allow unittests to access what are normally internal symbols.
22libminijail_unittest.wrapper :
23 $(MAKE) $(MAKEARGS) test-clean
24 $(MAKE) $(MAKEARGS) libminijail_unittest
25 $(MAKE) $(MAKEARGS) test-clean
26
27libminijail_unittest : CFLAGS := $(filter-out -fvisibility=%,$(CFLAGS))
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080028libminijail_unittest : libminijail_unittest.o libminijail.o \
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070029 syscall_filter.o bpf.o util.o libsyscalls.gen.o
Will Drewry6ac91122011-10-21 16:38:58 -050030 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $(CFLAGS_FILE),$^) -lcap
Will Drewrydecdfdc2011-09-27 15:13:54 -050031
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032libminijailpreload.so : libminijailpreload.c libminijail.o libsyscalls.gen.o \
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070033 syscall_filter.o bpf.o util.o
Elly Jonescd7a9042011-07-22 13:56:51 -040034 $(CC) $(CFLAGS) -shared -o $@ $^ -ldl -lcap
35
Thieu Lef0ef52e2011-09-14 14:03:09 -070036libminijail.o : libminijail.c libminijail.h
Will Drewry32ac9f52011-08-18 21:36:27 -050037
Will Drewrydecdfdc2011-09-27 15:13:54 -050038libminijail_unittest.o : libminijail_unittest.c test_harness.h
39 $(CC) $(CFLAGS) -c -o $@ $<
40
Will Drewry32ac9f52011-08-18 21:36:27 -050041libsyscalls.gen.o : libsyscalls.gen.c libsyscalls.h
42
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043syscall_filter_unittest : syscall_filter_unittest.o syscall_filter.o \
44 bpf.o util.o libsyscalls.gen.o test_harness.h
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070045 $(CC) $(CFLAGS) -o $@ $^
46
47syscall_filter_unittest.o : syscall_filter_unittest.c test_harness.h
48 $(CC) $(CFLAGS) -c -o $@ $<
49
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080050syscall_filter.o : syscall_filter.c syscall_filter.h
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070051
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052bpf.o : bpf.c bpf.h
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070053
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070054util.o : util.c util.h
55
Will Drewry32ac9f52011-08-18 21:36:27 -050056# sed expression which extracts system calls that are
57# defined via asm/unistd.h. It converts them from:
58# #define __NR_read
59# to:
60# #ifdef __NR_read
61# { "read", __NR_read },
62# #endif
63# All other lines will not be emitted. The sed expression lives in its
64# own macro to allow clean line wrapping.
65define sed-multiline
Ben Chan6a824372012-07-21 16:30:52 -070066 's/#define __\(ARM_\)\?\(NR_\)\([a-z0-9_]*\)$$/#ifdef __\1\2\3\n\
67 { "\1\3", __\1\2\3 },\n#endif/g p;'
Will Drewry32ac9f52011-08-18 21:36:27 -050068endef
69
70# Generates a header file with a system call table made up of "name",
71# syscall_nr entries by including the build target <asm/unistd.h> and
72# emitting the list of defines. Use of the compiler is needed to
73# dereference the actual provider of syscall definitions.
74# E.g., asm/unistd_32.h or asm/unistd_64.h, etc.
75define gen_syscalls
76 (set -e; \
77 echo '/* GENERATED BY MAKEFILE */'; \
78 echo '#include <stddef.h>'; \
79 echo '#include <asm/unistd.h>'; \
80 echo '#include "libsyscalls.h"'; \
81 echo "const struct syscall_entry syscall_table[] = {"; \
82 echo "#include <asm/unistd.h>" | \
83 $(CC) $(CFLAGS) -dN - -E | sed -ne $(sed-multiline); \
84 echo " { NULL, -1 },"; \
85 echo "};" ) > $1
86endef
87
88# Only regenerate libsyscalls.gen.c if the Makefile or header changes.
89# NOTE! This will not detect if the file is not appropriate for the target.
90libsyscalls.gen.c : Makefile libsyscalls.h
91 @printf "Generating target-arch specific $@ . . . "
92 @$(call gen_syscalls,$@)
93 @printf "done.\n"
94
Will Drewry6ac91122011-10-21 16:38:58 -050095# Only clean up files affected by the CFLAGS change for testing.
96test-clean :
97 @rm -f libminijail.o libminijail_unittest.o libsyscalls.gen.o
98
99clean : test-clean
Will Drewry32ac9f52011-08-18 21:36:27 -0500100 @rm -f libminijail.o libminijailpreload.so minijail0
Will Drewry6ac91122011-10-21 16:38:58 -0500101 @rm -f libminijail.so
102 @rm -f libminijail_unittest
103 @rm -f libsyscalls.gen.c
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700104 @rm -f syscall_filter.o bpf.o util.o
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700105 @rm -f syscall_filter_unittest syscall_filter_unittest.o