aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: c3de57131b4b83c98ff259f28d73b07e91c973c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# SPDX-License-Identifier: MIT
# Copyright 2020 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.


# Use 'make help' to list available targets.
#
# Define V=1 to enable "verbose" mode, showing all executed commands.
#
# Define USE_SHARED_LIB=1 to link the fsverity binary to the shared library
# libfsverity.so rather than to the static library libfsverity.a.
#
# Define PREFIX to override the installation prefix, like './configure --prefix'
# in autotools-based projects (default: /usr/local)
#
# Define BINDIR to override where to install binaries, like './configure
# --bindir' in autotools-based projects (default: PREFIX/bin)
#
# Define INCDIR to override where to install headers, like './configure
# --includedir' in autotools-based projects (default: PREFIX/include)
#
# Define LIBDIR to override where to install libraries, like './configure
# --libdir' in autotools-based projects (default: PREFIX/lib)
#
# Define MANDIR to override where to install man pages, like './configure
# --mandir' in autotools-based projects (default: PREFIX/share/man)
#
# Define DESTDIR to override the installation destination directory
# (default: empty string)
#
# You can also specify custom CC, CFLAGS, CPPFLAGS, LDFLAGS, and/or PKGCONF.
#
##############################################################################

cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
	      then echo $(1); fi)

# Support building with MinGW for minimal Windows fsverity.exe, but not for
# libfsverity. fsverity.exe will be statically linked.
ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
MINGW = 1
endif

# Set the CFLAGS.  First give the warning-related flags (unconditionally, though
# the user can override any of them by specifying the opposite flag); then give
# the user-specified CFLAGS, defaulting to -O2 if none were specified.
#
# Use -Wno-deprecated-declarations to avoid warnings about the Engine API having
# been deprecated in OpenSSL 3.0; the replacement isn't ready yet.
CFLAGS ?= -O2
override CFLAGS := -Wall -Wundef				\
	$(call cc-option,-Wdeclaration-after-statement)		\
	$(call cc-option,-Wimplicit-fallthrough)		\
	$(call cc-option,-Wmissing-field-initializers)		\
	$(call cc-option,-Wmissing-prototypes)			\
	$(call cc-option,-Wstrict-prototypes)			\
	$(call cc-option,-Wunused-parameter)			\
	$(call cc-option,-Wvla)					\
	$(call cc-option,-Wno-deprecated-declarations)		\
	$(CFLAGS)

override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)

ifneq ($(V),1)
QUIET_CC        = @echo '  CC      ' $@;
QUIET_CCLD      = @echo '  CCLD    ' $@;
QUIET_AR        = @echo '  AR      ' $@;
QUIET_LN        = @echo '  LN      ' $@;
QUIET_GEN       = @echo '  GEN     ' $@;
endif
USE_SHARED_LIB  ?=
PREFIX          ?= /usr/local
BINDIR          ?= $(PREFIX)/bin
INCDIR          ?= $(PREFIX)/include
LIBDIR          ?= $(PREFIX)/lib
MANDIR          ?= $(PREFIX)/share/man
DESTDIR         ?=
ifneq ($(MINGW),1)
PKGCONF         ?= pkg-config
else
PKGCONF         := false
EXEEXT          := .exe
endif
FSVERITY        := fsverity$(EXEEXT)

# Rebuild if a user-specified setting that affects the build changed.
.build-config: FORCE
	@flags=$$(							\
		echo 'CC=$(CC)';					\
		echo 'CFLAGS=$(CFLAGS)';				\
		echo 'CPPFLAGS=$(CPPFLAGS)';				\
		echo 'LDFLAGS=$(LDFLAGS)';				\
		echo 'LDLIBS=$(LDLIBS)';				\
		echo 'USE_SHARED_LIB=$(USE_SHARED_LIB)';		\
	);								\
	if [ "$$flags" != "`cat $@ 2>/dev/null`" ]; then		\
		[ -e $@ ] && echo "Rebuilding due to new settings";	\
		echo "$$flags" > $@;					\
	fi

DEFAULT_TARGETS :=
EXTRA_TARGETS   :=
COMMON_HEADERS  := $(wildcard common/*.h)
LDLIBS          := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo -lcrypto)
CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)

# If we are dynamically linking, when running tests we need to override
# LD_LIBRARY_PATH as no RPATH is set
ifdef USE_SHARED_LIB
RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY)
else
RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./$(FSVERITY)
endif

##############################################################################

#### Library

SOVERSION       := 0
LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
LIB_SRC         := $(wildcard lib/*.c)
ifeq ($(MINGW),1)
LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
endif
LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)

# Compile static library object files
$(STATIC_LIB_OBJ): %.o: %.c $(LIB_HEADERS) .build-config
	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) $<

# Compile shared library object files
$(SHARED_LIB_OBJ): %.shlib.o: %.c $(LIB_HEADERS) .build-config
	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) -fPIC $<

# Create static library
libfsverity.a:$(STATIC_LIB_OBJ)
	$(QUIET_AR) $(AR) cr $@ $+

DEFAULT_TARGETS += libfsverity.a

# Create shared library
libfsverity.so.$(SOVERSION):$(SHARED_LIB_OBJ)
	$(QUIET_CCLD) $(CC) -o $@ -Wl,-soname=$@ -shared $+ \
		$(CFLAGS) $(LDFLAGS) $(LDLIBS)

DEFAULT_TARGETS += libfsverity.so.$(SOVERSION)

# Create the symlink libfsverity.so => libfsverity.so.$(SOVERSION)
libfsverity.so:libfsverity.so.$(SOVERSION)
	$(QUIET_LN) ln -sf $+ $@

DEFAULT_TARGETS += libfsverity.so

##############################################################################

#### Programs

ALL_PROG_SRC      := $(wildcard programs/*.c)
ALL_PROG_OBJ      := $(ALL_PROG_SRC:.c=.o)
ALL_PROG_HEADERS  := $(wildcard programs/*.h) $(COMMON_HEADERS)
PROG_COMMON_SRC   := programs/utils.c
PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
		     programs/cmd_digest.o	\
		     programs/cmd_sign.o	\
		     programs/fsverity.o
ifneq ($(MINGW),1)
FSVERITY_PROG_OBJ += \
		     programs/cmd_dump_metadata.o \
		     programs/cmd_enable.o	\
		     programs/cmd_measure.o
endif
TEST_PROG_SRC     := $(wildcard programs/test_*.c)
TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT))

# Compile program object files
$(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
	$(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $<

# Link the fsverity program
ifdef USE_SHARED_LIB
$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so
	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
else
$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a
	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
endif

DEFAULT_TARGETS += $(FSVERITY)

# Link the test programs
$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)

EXTRA_TARGETS += $(TEST_PROGRAMS)

##############################################################################

#### Manual pages

MAN_PAGES := $(wildcard man/*.[1-9])

##############################################################################

# Support for downloading and building BoringSSL.  The purpose of this is to
# allow testing builds of fsverity-utils that link to BoringSSL instead of
# OpenSSL, without having to use a system that uses BoringSSL natively.

boringssl:
	rm -rf boringssl boringssl.tar.gz
	curl -s -o boringssl.tar.gz \
		https://boringssl.googlesource.com/boringssl/+archive/refs/heads/master.tar.gz
	mkdir boringssl
	tar xf boringssl.tar.gz -C boringssl
	cmake -B boringssl/build boringssl
	$(MAKE) -C boringssl/build $(MAKEFLAGS)

##############################################################################

SPECIAL_TARGETS := all test_programs check install uninstall help clean

FORCE:

.PHONY: $(SPECIAL_TARGETS) FORCE

.DEFAULT_GOAL = all

all:$(DEFAULT_TARGETS)

test_programs:$(TEST_PROGRAMS)

# This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
# want to run the full tests.
check:$(FSVERITY) test_programs
	for prog in $(TEST_PROGRAMS); do \
		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
	done
	$(RUN_FSVERITY) --help > /dev/null
	$(RUN_FSVERITY) --version > /dev/null
	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \
		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \
		--block-size=512 --salt=12345678 \
		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
	$(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \
		--block-size=512 --salt=12345678 > /dev/null
	rm -f fsverity.sig
	@echo "All tests passed!"

install:all
	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
	install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR)
	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
	install -m644 include/libfsverity.h $(DESTDIR)$(INCDIR)
	sed -e "s|@PREFIX@|$(PREFIX)|" \
		-e "s|@LIBDIR@|$(LIBDIR)|" \
		-e "s|@INCDIR@|$(INCDIR)|" \
		lib/libfsverity.pc.in \
		> $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
	install -d $(DESTDIR)$(MANDIR)/man1
	for page in $(MAN_PAGES); do \
		install -m644 $$page $(DESTDIR)$(MANDIR)/man1/; \
	done

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY)
	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
	rm -f $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
	rm -f $(DESTDIR)$(INCDIR)/libfsverity.h
	for page in $(notdir $(MAN_PAGES)); do \
		rm -f $(DESTDIR)$(MANDIR)/man1/$$page; \
	done

help:
	@echo "Available targets:"
	@echo "------------------"
	@for target in $(DEFAULT_TARGETS) $(EXTRA_TARGETS) $(SPECIAL_TARGETS); \
	do \
		echo $$target; \
	done

clean:
	rm -f $(DEFAULT_TARGETS) $(EXTRA_TARGETS) \
		lib/*.o programs/*.o .build-config fsverity.sig