xref: /aosp_15_r20/external/fsverity-utils/Makefile (revision b13c0e4024008a1f948ee8189745cb3371f4ac04)
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 MANDIR to override where to install man pages, like './configure
29# --mandir' in autotools-based projects (default: PREFIX/share/man)
30#
31# Define DESTDIR to override the installation destination directory
32# (default: empty string)
33#
34# You can also specify custom CC, CFLAGS, CPPFLAGS, LDFLAGS, and/or PKGCONF.
35#
36##############################################################################
37
38cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
39	      then echo $(1); fi)
40
41# Support building with MinGW for minimal Windows fsverity.exe, but not for
42# libfsverity. fsverity.exe will be statically linked.
43ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
44MINGW = 1
45endif
46
47# Set the CFLAGS.  First give the warning-related flags (unconditionally, though
48# the user can override any of them by specifying the opposite flag); then give
49# the user-specified CFLAGS, defaulting to -O2 if none were specified.
50#
51# Use -Wno-deprecated-declarations to avoid warnings about the Engine API having
52# been deprecated in OpenSSL 3.0; the replacement isn't ready yet.
53CFLAGS ?= -O2
54override CFLAGS := -Wall -Wundef				\
55	$(call cc-option,-Wdeclaration-after-statement)		\
56	$(call cc-option,-Wimplicit-fallthrough)		\
57	$(call cc-option,-Wmissing-field-initializers)		\
58	$(call cc-option,-Wmissing-prototypes)			\
59	$(call cc-option,-Wstrict-prototypes)			\
60	$(call cc-option,-Wunused-parameter)			\
61	$(call cc-option,-Wvla)					\
62	$(call cc-option,-Wno-deprecated-declarations)		\
63	$(CFLAGS)
64
65override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
66
67ifneq ($(V),1)
68QUIET_CC        = @echo '  CC      ' $@;
69QUIET_CCLD      = @echo '  CCLD    ' $@;
70QUIET_AR        = @echo '  AR      ' $@;
71QUIET_LN        = @echo '  LN      ' $@;
72QUIET_GEN       = @echo '  GEN     ' $@;
73endif
74USE_SHARED_LIB  ?=
75PREFIX          ?= /usr/local
76BINDIR          ?= $(PREFIX)/bin
77INCDIR          ?= $(PREFIX)/include
78LIBDIR          ?= $(PREFIX)/lib
79MANDIR          ?= $(PREFIX)/share/man
80DESTDIR         ?=
81ifneq ($(MINGW),1)
82PKGCONF         ?= pkg-config
83else
84PKGCONF         := false
85EXEEXT          := .exe
86endif
87FSVERITY        := fsverity$(EXEEXT)
88
89# Rebuild if a user-specified setting that affects the build changed.
90.build-config: FORCE
91	@flags=$$(							\
92		echo 'CC=$(CC)';					\
93		echo 'CFLAGS=$(CFLAGS)';				\
94		echo 'CPPFLAGS=$(CPPFLAGS)';				\
95		echo 'LDFLAGS=$(LDFLAGS)';				\
96		echo 'LDLIBS=$(LDLIBS)';				\
97		echo 'USE_SHARED_LIB=$(USE_SHARED_LIB)';		\
98	);								\
99	if [ "$$flags" != "`cat $@ 2>/dev/null`" ]; then		\
100		[ -e $@ ] && echo "Rebuilding due to new settings";	\
101		echo "$$flags" > $@;					\
102	fi
103
104DEFAULT_TARGETS :=
105EXTRA_TARGETS   :=
106COMMON_HEADERS  := $(wildcard common/*.h)
107LDLIBS          := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo -lcrypto)
108CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
109
110# If we are dynamically linking, when running tests we need to override
111# LD_LIBRARY_PATH as no RPATH is set
112ifdef USE_SHARED_LIB
113RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY)
114else
115RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./$(FSVERITY)
116endif
117
118##############################################################################
119
120#### Library
121
122SOVERSION       := 0
123LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
124LIB_SRC         := $(wildcard lib/*.c)
125ifeq ($(MINGW),1)
126LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
127endif
128LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
129STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
130SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
131
132# Compile static library object files
133$(STATIC_LIB_OBJ): %.o: %.c $(LIB_HEADERS) .build-config
134	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) $<
135
136# Compile shared library object files
137$(SHARED_LIB_OBJ): %.shlib.o: %.c $(LIB_HEADERS) .build-config
138	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) -fPIC $<
139
140# Create static library
141libfsverity.a:$(STATIC_LIB_OBJ)
142	$(QUIET_AR) $(AR) cr $@ $+
143
144DEFAULT_TARGETS += libfsverity.a
145
146# Create shared library
147libfsverity.so.$(SOVERSION):$(SHARED_LIB_OBJ)
148	$(QUIET_CCLD) $(CC) -o $@ -Wl,-soname=$@ -shared $+ \
149		$(CFLAGS) $(LDFLAGS) $(LDLIBS)
150
151DEFAULT_TARGETS += libfsverity.so.$(SOVERSION)
152
153# Create the symlink libfsverity.so => libfsverity.so.$(SOVERSION)
154libfsverity.so:libfsverity.so.$(SOVERSION)
155	$(QUIET_LN) ln -sf $+ $@
156
157DEFAULT_TARGETS += libfsverity.so
158
159##############################################################################
160
161#### Programs
162
163ALL_PROG_SRC      := $(wildcard programs/*.c)
164ALL_PROG_OBJ      := $(ALL_PROG_SRC:.c=.o)
165ALL_PROG_HEADERS  := $(wildcard programs/*.h) $(COMMON_HEADERS)
166PROG_COMMON_SRC   := programs/utils.c
167PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
168FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
169		     programs/cmd_digest.o	\
170		     programs/cmd_sign.o	\
171		     programs/fsverity.o
172ifneq ($(MINGW),1)
173FSVERITY_PROG_OBJ += \
174		     programs/cmd_dump_metadata.o \
175		     programs/cmd_enable.o	\
176		     programs/cmd_measure.o
177endif
178TEST_PROG_SRC     := $(wildcard programs/test_*.c)
179TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT))
180
181# Compile program object files
182$(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
183	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $<
184
185# Link the fsverity program
186ifdef USE_SHARED_LIB
187$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so
188	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
189		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
190else
191$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a
192	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
193endif
194
195DEFAULT_TARGETS += $(FSVERITY)
196
197# Link the test programs
198$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
199	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
200
201EXTRA_TARGETS += $(TEST_PROGRAMS)
202
203##############################################################################
204
205#### Manual pages
206
207MAN_PAGES := $(wildcard man/*.[1-9])
208
209##############################################################################
210
211# Support for downloading and building BoringSSL.  The purpose of this is to
212# allow testing builds of fsverity-utils that link to BoringSSL instead of
213# OpenSSL, without having to use a system that uses BoringSSL natively.
214
215boringssl:
216	rm -rf boringssl boringssl.tar.gz
217	curl -s -o boringssl.tar.gz \
218		https://boringssl.googlesource.com/boringssl/+archive/refs/heads/master.tar.gz
219	mkdir boringssl
220	tar xf boringssl.tar.gz -C boringssl
221	cmake -B boringssl/build boringssl
222	$(MAKE) -C boringssl/build $(MAKEFLAGS)
223
224##############################################################################
225
226SPECIAL_TARGETS := all test_programs check install uninstall help clean
227
228FORCE:
229
230.PHONY: $(SPECIAL_TARGETS) FORCE
231
232.DEFAULT_GOAL = all
233
234all:$(DEFAULT_TARGETS)
235
236test_programs:$(TEST_PROGRAMS)
237
238# This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
239# want to run the full tests.
240check:$(FSVERITY) test_programs
241	for prog in $(TEST_PROGRAMS); do \
242		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
243	done
244	$(RUN_FSVERITY) --help > /dev/null
245	$(RUN_FSVERITY) --version > /dev/null
246	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \
247		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
248	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \
249		--block-size=512 --salt=12345678 \
250		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
251	$(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \
252		--block-size=512 --salt=12345678 > /dev/null
253	rm -f fsverity.sig
254	@echo "All tests passed!"
255
256install:all
257	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
258	install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR)
259	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
260	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
261	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
262	install -m644 include/libfsverity.h $(DESTDIR)$(INCDIR)
263	sed -e "s|@PREFIX@|$(PREFIX)|" \
264		-e "s|@LIBDIR@|$(LIBDIR)|" \
265		-e "s|@INCDIR@|$(INCDIR)|" \
266		lib/libfsverity.pc.in \
267		> $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
268	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
269	install -d $(DESTDIR)$(MANDIR)/man1
270	for page in $(MAN_PAGES); do \
271		install -m644 $$page $(DESTDIR)$(MANDIR)/man1/; \
272	done
273
274uninstall:
275	rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY)
276	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
277	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
278	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
279	rm -f $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
280	rm -f $(DESTDIR)$(INCDIR)/libfsverity.h
281	for page in $(notdir $(MAN_PAGES)); do \
282		rm -f $(DESTDIR)$(MANDIR)/man1/$$page; \
283	done
284
285help:
286	@echo "Available targets:"
287	@echo "------------------"
288	@for target in $(DEFAULT_TARGETS) $(EXTRA_TARGETS) $(SPECIAL_TARGETS); \
289	do \
290		echo $$target; \
291	done
292
293clean:
294	rm -f $(DEFAULT_TARGETS) $(EXTRA_TARGETS) \
295		lib/*.o programs/*.o .build-config fsverity.sig
296