• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: MIT
2# Copyright 2020 Google LLC
3#
4# Use of this source code is governed by an MIT-style
5# license that can be found in the LICENSE file or at
6# https://opensource.org/licenses/MIT.
7
8
9# Use 'make help' to list available targets.
10#
11# Define V=1 to enable "verbose" mode, showing all executed commands.
12#
13# Define USE_SHARED_LIB=1 to link the fsverity binary to the shared library
14# libfsverity.so rather than to the static library libfsverity.a.
15#
16# Define PREFIX to override the installation prefix, like './configure --prefix'
17# in autotools-based projects (default: /usr/local)
18#
19# Define BINDIR to override where to install binaries, like './configure
20# --bindir' in autotools-based projects (default: PREFIX/bin)
21#
22# Define INCDIR to override where to install headers, like './configure
23# --includedir' in autotools-based projects (default: PREFIX/include)
24#
25# Define LIBDIR to override where to install libraries, like './configure
26# --libdir' in autotools-based projects (default: PREFIX/lib)
27#
28# Define DESTDIR to override the installation destination directory
29# (default: empty string)
30#
31# You can also specify custom CC, CFLAGS, CPPFLAGS, LDFLAGS, and/or PKGCONF.
32#
33##############################################################################
34
35cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
36	      then echo $(1); fi)
37
38# Support building with MinGW for minimal Windows fsverity.exe, but not for
39# libfsverity. fsverity.exe will be statically linked.
40ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
41MINGW = 1
42endif
43
44CFLAGS ?= -O2
45
46override CFLAGS := -Wall -Wundef				\
47	$(call cc-option,-Wdeclaration-after-statement)		\
48	$(call cc-option,-Wimplicit-fallthrough)		\
49	$(call cc-option,-Wmissing-field-initializers)		\
50	$(call cc-option,-Wmissing-prototypes)			\
51	$(call cc-option,-Wstrict-prototypes)			\
52	$(call cc-option,-Wunused-parameter)			\
53	$(call cc-option,-Wvla)					\
54	$(CFLAGS)
55
56override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
57
58ifneq ($(V),1)
59QUIET_CC        = @echo '  CC      ' $@;
60QUIET_CCLD      = @echo '  CCLD    ' $@;
61QUIET_AR        = @echo '  AR      ' $@;
62QUIET_LN        = @echo '  LN      ' $@;
63QUIET_GEN       = @echo '  GEN     ' $@;
64endif
65USE_SHARED_LIB  ?=
66PREFIX          ?= /usr/local
67BINDIR          ?= $(PREFIX)/bin
68INCDIR          ?= $(PREFIX)/include
69LIBDIR          ?= $(PREFIX)/lib
70DESTDIR         ?=
71ifneq ($(MINGW),1)
72PKGCONF         ?= pkg-config
73else
74PKGCONF         := false
75EXEEXT          := .exe
76endif
77FSVERITY        := fsverity$(EXEEXT)
78
79# Rebuild if a user-specified setting that affects the build changed.
80.build-config: FORCE
81	@flags=$$(							\
82		echo 'CC=$(CC)';					\
83		echo 'CFLAGS=$(CFLAGS)';				\
84		echo 'CPPFLAGS=$(CPPFLAGS)';				\
85		echo 'LDFLAGS=$(LDFLAGS)';				\
86		echo 'LDLIBS=$(LDLIBS)';				\
87		echo 'USE_SHARED_LIB=$(USE_SHARED_LIB)';		\
88	);								\
89	if [ "$$flags" != "`cat $@ 2>/dev/null`" ]; then		\
90		[ -e $@ ] && echo "Rebuilding due to new settings";	\
91		echo "$$flags" > $@;					\
92	fi
93
94DEFAULT_TARGETS :=
95COMMON_HEADERS  := $(wildcard common/*.h)
96LDLIBS          := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo -lcrypto)
97CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
98
99# If we are dynamically linking, when running tests we need to override
100# LD_LIBRARY_PATH as no RPATH is set
101ifdef USE_SHARED_LIB
102RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY)
103else
104RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./$(FSVERITY)
105endif
106
107##############################################################################
108
109#### Library
110
111SOVERSION       := 0
112LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
113LIB_SRC         := $(wildcard lib/*.c)
114ifeq ($(MINGW),1)
115LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
116endif
117LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
118STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
119SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
120
121# Compile static library object files
122$(STATIC_LIB_OBJ): %.o: %.c $(LIB_HEADERS) .build-config
123	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) $<
124
125# Compile shared library object files
126$(SHARED_LIB_OBJ): %.shlib.o: %.c $(LIB_HEADERS) .build-config
127	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) -fPIC $<
128
129# Create static library
130libfsverity.a:$(STATIC_LIB_OBJ)
131	$(QUIET_AR) $(AR) cr $@ $+
132
133DEFAULT_TARGETS += libfsverity.a
134
135# Create shared library
136libfsverity.so.$(SOVERSION):$(SHARED_LIB_OBJ)
137	$(QUIET_CCLD) $(CC) -o $@ -Wl,-soname=$@ -shared $+ \
138		$(CFLAGS) $(LDFLAGS) $(LDLIBS)
139
140DEFAULT_TARGETS += libfsverity.so.$(SOVERSION)
141
142# Create the symlink libfsverity.so => libfsverity.so.$(SOVERSION)
143libfsverity.so:libfsverity.so.$(SOVERSION)
144	$(QUIET_LN) ln -sf $+ $@
145
146DEFAULT_TARGETS += libfsverity.so
147
148##############################################################################
149
150#### Programs
151
152ALL_PROG_SRC      := $(wildcard programs/*.c)
153ALL_PROG_OBJ      := $(ALL_PROG_SRC:.c=.o)
154ALL_PROG_HEADERS  := $(wildcard programs/*.h) $(COMMON_HEADERS)
155PROG_COMMON_SRC   := programs/utils.c
156PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
157FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
158		     programs/cmd_digest.o	\
159		     programs/cmd_sign.o	\
160		     programs/fsverity.o
161ifneq ($(MINGW),1)
162FSVERITY_PROG_OBJ += \
163		     programs/cmd_enable.o	\
164		     programs/cmd_measure.o
165endif
166TEST_PROG_SRC     := $(wildcard programs/test_*.c)
167TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT))
168
169# Compile program object files
170$(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
171	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $<
172
173# Link the fsverity program
174ifdef USE_SHARED_LIB
175$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so
176	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
177		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
178else
179$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a
180	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
181endif
182
183DEFAULT_TARGETS += $(FSVERITY)
184
185# Link the test programs
186$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
187	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
188
189##############################################################################
190
191SPECIAL_TARGETS := all test_programs check install uninstall help clean
192
193FORCE:
194
195.PHONY: $(SPECIAL_TARGETS) FORCE
196
197.DEFAULT_GOAL = all
198
199all:$(DEFAULT_TARGETS)
200
201test_programs:$(TEST_PROGRAMS)
202
203# This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
204# want to run the full tests.
205check:$(FSVERITY) test_programs
206	for prog in $(TEST_PROGRAMS); do \
207		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
208	done
209	$(RUN_FSVERITY) --help > /dev/null
210	$(RUN_FSVERITY) --version > /dev/null
211	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \
212		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
213	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \
214		--block-size=512 --salt=12345678 \
215		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
216	$(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \
217		--block-size=512 --salt=12345678 > /dev/null
218	rm -f fsverity.sig
219	@echo "All tests passed!"
220
221install:all
222	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
223	install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR)
224	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
225	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
226	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
227	install -m644 include/libfsverity.h $(DESTDIR)$(INCDIR)
228	sed -e "s|@PREFIX@|$(PREFIX)|" \
229		-e "s|@LIBDIR@|$(LIBDIR)|" \
230		-e "s|@INCDIR@|$(INCDIR)|" \
231		lib/libfsverity.pc.in \
232		> $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
233	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
234
235uninstall:
236	rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY)
237	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
238	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
239	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
240	rm -f $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
241	rm -f $(DESTDIR)$(INCDIR)/libfsverity.h
242
243help:
244	@echo "Available targets:"
245	@echo "------------------"
246	@for target in $(DEFAULT_TARGETS) $(TEST_PROGRAMS) $(SPECIAL_TARGETS); \
247	do \
248		echo $$target; \
249	done
250
251clean:
252	rm -f $(DEFAULT_TARGETS) $(TEST_PROGRAMS) \
253		lib/*.o programs/*.o .build-config fsverity.sig
254