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 ' $@; 73QUIET_PANDOC = @echo ' PANDOC ' $@; 74endif 75USE_SHARED_LIB ?= 76PREFIX ?= /usr/local 77BINDIR ?= $(PREFIX)/bin 78INCDIR ?= $(PREFIX)/include 79LIBDIR ?= $(PREFIX)/lib 80MANDIR ?= $(PREFIX)/share/man 81DESTDIR ?= 82ifneq ($(MINGW),1) 83PKGCONF ?= pkg-config 84else 85PKGCONF := false 86EXEEXT := .exe 87endif 88FSVERITY := fsverity$(EXEEXT) 89 90# Rebuild if a user-specified setting that affects the build changed. 91.build-config: FORCE 92 @flags=$$( \ 93 echo 'CC=$(CC)'; \ 94 echo 'CFLAGS=$(CFLAGS)'; \ 95 echo 'CPPFLAGS=$(CPPFLAGS)'; \ 96 echo 'LDFLAGS=$(LDFLAGS)'; \ 97 echo 'LDLIBS=$(LDLIBS)'; \ 98 echo 'USE_SHARED_LIB=$(USE_SHARED_LIB)'; \ 99 ); \ 100 if [ "$$flags" != "`cat $@ 2>/dev/null`" ]; then \ 101 [ -e $@ ] && echo "Rebuilding due to new settings"; \ 102 echo "$$flags" > $@; \ 103 fi 104 105DEFAULT_TARGETS := 106EXTRA_TARGETS := 107COMMON_HEADERS := $(wildcard common/*.h) 108LDLIBS := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo -lcrypto) 109CFLAGS += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo) 110 111# If we are dynamically linking, when running tests we need to override 112# LD_LIBRARY_PATH as no RPATH is set 113ifdef USE_SHARED_LIB 114RUN_FSVERITY = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY) 115else 116RUN_FSVERITY = $(TEST_WRAPPER_PROG) ./$(FSVERITY) 117endif 118 119############################################################################## 120 121#### Library 122 123SOVERSION := 0 124LIB_CFLAGS := $(CFLAGS) -fvisibility=hidden 125LIB_SRC := $(wildcard lib/*.c) 126ifeq ($(MINGW),1) 127LIB_SRC := $(filter-out lib/enable.c,${LIB_SRC}) 128endif 129LIB_HEADERS := $(wildcard lib/*.h) $(COMMON_HEADERS) 130STATIC_LIB_OBJ := $(LIB_SRC:.c=.o) 131SHARED_LIB_OBJ := $(LIB_SRC:.c=.shlib.o) 132 133# Compile static library object files 134$(STATIC_LIB_OBJ): %.o: %.c $(LIB_HEADERS) .build-config 135 $(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) $< 136 137# Compile shared library object files 138$(SHARED_LIB_OBJ): %.shlib.o: %.c $(LIB_HEADERS) .build-config 139 $(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(LIB_CFLAGS) -fPIC $< 140 141# Create static library 142libfsverity.a:$(STATIC_LIB_OBJ) 143 $(QUIET_AR) $(AR) cr $@ $+ 144 145DEFAULT_TARGETS += libfsverity.a 146 147# Create shared library 148libfsverity.so.$(SOVERSION):$(SHARED_LIB_OBJ) 149 $(QUIET_CCLD) $(CC) -o $@ -Wl,-soname=$@ -shared $+ \ 150 $(CFLAGS) $(LDFLAGS) $(LDLIBS) 151 152DEFAULT_TARGETS += libfsverity.so.$(SOVERSION) 153 154# Create the symlink libfsverity.so => libfsverity.so.$(SOVERSION) 155libfsverity.so:libfsverity.so.$(SOVERSION) 156 $(QUIET_LN) ln -sf $+ $@ 157 158DEFAULT_TARGETS += libfsverity.so 159 160############################################################################## 161 162#### Programs 163 164ALL_PROG_SRC := $(wildcard programs/*.c) 165ALL_PROG_OBJ := $(ALL_PROG_SRC:.c=.o) 166ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) 167PROG_COMMON_SRC := programs/utils.c 168PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) 169FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ 170 programs/cmd_digest.o \ 171 programs/cmd_sign.o \ 172 programs/fsverity.o 173ifneq ($(MINGW),1) 174FSVERITY_PROG_OBJ += \ 175 programs/cmd_dump_metadata.o \ 176 programs/cmd_enable.o \ 177 programs/cmd_measure.o 178endif 179TEST_PROG_SRC := $(wildcard programs/test_*.c) 180TEST_PROGRAMS := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT)) 181 182# Compile program object files 183$(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config 184 $(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $< 185 186# Link the fsverity program 187ifdef USE_SHARED_LIB 188$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so 189 $(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \ 190 $(CFLAGS) $(LDFLAGS) -L. -lfsverity 191else 192$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a 193 $(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS) 194endif 195 196DEFAULT_TARGETS += $(FSVERITY) 197 198# Link the test programs 199$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a 200 $(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS) 201 202EXTRA_TARGETS += $(TEST_PROGRAMS) 203 204############################################################################## 205 206#### Manual pages 207 208man/fsverity.1:man/fsverity.1.md 209 $(QUIET_PANDOC) pandoc $+ -s -t man > $@ 210 211MAN_PAGES := man/fsverity.1 212EXTRA_TARGETS += $(MAN_PAGES) 213 214############################################################################## 215 216# Support for downloading and building BoringSSL. The purpose of this is to 217# allow testing builds of fsverity-utils that link to BoringSSL instead of 218# OpenSSL, without having to use a system that uses BoringSSL natively. 219 220boringssl: 221 rm -rf boringssl boringssl.tar.gz 222 curl -s -o boringssl.tar.gz \ 223 https://boringssl.googlesource.com/boringssl/+archive/refs/heads/master.tar.gz 224 mkdir boringssl 225 tar xf boringssl.tar.gz -C boringssl 226 cmake -B boringssl/build boringssl 227 $(MAKE) -C boringssl/build $(MAKEFLAGS) 228 229############################################################################## 230 231SPECIAL_TARGETS := all test_programs check install install-man uninstall \ 232 help clean 233 234FORCE: 235 236.PHONY: $(SPECIAL_TARGETS) FORCE 237 238.DEFAULT_GOAL = all 239 240all:$(DEFAULT_TARGETS) 241 242test_programs:$(TEST_PROGRAMS) 243 244# This just runs some quick, portable tests. Use scripts/run-tests.sh if you 245# want to run the full tests. 246check:$(FSVERITY) test_programs 247 for prog in $(TEST_PROGRAMS); do \ 248 $(TEST_WRAPPER_PROG) ./$$prog || exit 1; \ 249 done 250 $(RUN_FSVERITY) --help > /dev/null 251 $(RUN_FSVERITY) --version > /dev/null 252 $(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \ 253 --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null 254 $(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \ 255 --block-size=512 --salt=12345678 \ 256 --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null 257 $(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \ 258 --block-size=512 --salt=12345678 > /dev/null 259 rm -f fsverity.sig 260 @echo "All tests passed!" 261 262install:all 263 install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR) 264 install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR) 265 install -m644 libfsverity.a $(DESTDIR)$(LIBDIR) 266 install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR) 267 ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so 268 install -m644 include/libfsverity.h $(DESTDIR)$(INCDIR) 269 sed -e "s|@PREFIX@|$(PREFIX)|" \ 270 -e "s|@LIBDIR@|$(LIBDIR)|" \ 271 -e "s|@INCDIR@|$(INCDIR)|" \ 272 lib/libfsverity.pc.in \ 273 > $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc 274 chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc 275 276install-man:$(MAN_PAGES) 277 install -d $(DESTDIR)$(MANDIR)/man1 278 install -m644 $+ $(DESTDIR)$(MANDIR)/man1/ 279 280uninstall: 281 rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY) 282 rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a 283 rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION) 284 rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so 285 rm -f $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc 286 rm -f $(DESTDIR)$(INCDIR)/libfsverity.h 287 for page in $(notdir $(MAN_PAGES)); do \ 288 rm -f $(DESTDIR)$(MANDIR)/man1/$$page; \ 289 done 290 291help: 292 @echo "Available targets:" 293 @echo "------------------" 294 @for target in $(DEFAULT_TARGETS) $(EXTRA_TARGETS) $(SPECIAL_TARGETS); \ 295 do \ 296 echo $$target; \ 297 done 298 299clean: 300 rm -f $(DEFAULT_TARGETS) $(EXTRA_TARGETS) \ 301 lib/*.o programs/*.o .build-config fsverity.sig 302