From f3ebd3ce27dd706c6b873395a8813d3e42b0e557 Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Wed, 5 Jul 2023 23:56:06 -0300 Subject: [PATCH] build: automatically generate header dependencies Instead of manually specifying which source files depend on which headers, use compiler flags to automatically generate depfiles (.d), which declare the correct header (make) dependencies for each source file (.c). Use `-MMD` (which ignores system headers) to generate the dependencies and `-MP` to prevent make from complaining when a header file is removed while it is listed as a dependency in a depfile. If depfiles exist, just include them. If not, make each object file (.o) unconditionally depend on all header files in its source directory and in src/include, to ensure that rebuilds are done when needed. The latter case applies for the first build after `make clean` (which would build everything anyway) and when the compiler does not support generating depfiles. Note that both gcc and clang have supported these options for a long time. Misc: This depends on the changes from commit 5b1bd33c7 ("build: use full paths on compile/link targets", 2023-07-02) / PR #6158 to avoid issues with make dependency tracking. --- .gitignore | 1 + config.mk.in | 7 +++-- configure | 47 ++++++++++++++++++++++++++++++++- configure.ac | 7 +++++ src/etc-cleanup/Makefile | 2 -- src/fbuilder/Makefile | 2 -- src/fcopy/Makefile | 1 - src/fids/Makefile | 2 -- src/firecfg/Makefile | 7 ----- src/firejail/Makefile | 13 --------- src/firemon/Makefile | 1 - src/fldd/Makefile | 1 - src/fnet/Makefile | 1 - src/fnetfilter/Makefile | 1 - src/fsec-optimize/Makefile | 1 - src/fsec-print/Makefile | 1 - src/fseccomp/Makefile | 1 - src/fzenity/Makefile | 2 -- src/jailcheck/Makefile | 1 - src/libpostexecseccomp/Makefile | 2 -- src/libtracelog/Makefile | 2 -- src/profstats/Makefile | 2 -- src/prog.mk | 12 ++++++--- src/so.mk | 12 ++++++--- 24 files changed, 77 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index fd35dde40be..3413abd559a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.d *.o *.so *~ diff --git a/config.mk.in b/config.mk.in index 63d7fb98103..1521a263358 100644 --- a/config.mk.in +++ b/config.mk.in @@ -81,6 +81,7 @@ LDFLAGS=@LDFLAGS@ # Project variables EXTRA_CFLAGS =@EXTRA_CFLAGS@ +DEPS_CFLAGS =@DEPS_CFLAGS@ COMMON_CFLAGS = \ -ggdb -O2 -DVERSION='"$(VERSION)"' \ -Wall -Wextra $(HAVE_FATAL_WARNINGS) \ @@ -88,16 +89,18 @@ COMMON_CFLAGS = \ -fstack-protector-all -D_FORTIFY_SOURCE=2 \ -DPREFIX='"$(prefix)"' -DSYSCONFDIR='"$(sysconfdir)/firejail"' \ -DLIBDIR='"$(libdir)"' -DBINDIR='"$(bindir)"' \ - -DVARDIR='"/var/lib/firejail"' \ + -DVARDIR='"/var/lib/firejail"' PROG_CFLAGS = \ $(COMMON_CFLAGS) \ $(HAVE_GCOV) $(MANFLAGS) \ $(EXTRA_CFLAGS) \ + $(DEPS_CFLAGS) \ -fPIE SO_CFLAGS = \ $(COMMON_CFLAGS) \ + $(DEPS_CFLAGS) \ -fPIC EXTRA_LDFLAGS =@EXTRA_LDFLAGS@ @@ -105,4 +108,4 @@ PROG_LDFLAGS = -Wl,-z,relro -Wl,-z,now -fPIE -pie $(EXTRA_LDFLAGS) SO_LDFLAGS = -Wl,-z,relro -Wl,-z,now -fPIC LIBS =@LIBS@ -CLEANFILES = *.o *.gcov *.gcda *.gcno *.plist +CLEANFILES = *.d *.o *.gcov *.gcda *.gcno *.plist diff --git a/configure b/configure index 767d1da00bb..adcd6162b5e 100755 --- a/configure +++ b/configure @@ -682,6 +682,7 @@ PKG_CONFIG_PATH PKG_CONFIG HAVE_APPARMOR HAVE_IDS +DEPS_CFLAGS OBJEXT EXEEXT ac_ct_CC @@ -3270,8 +3271,51 @@ ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $ ac_compiler_gnu=$ac_cv_c_compiler_gnu -HAVE_SPECTRE="no" +DEPS_CFLAGS="" + + +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether C compiler accepts -MMD -MP" >&5 +printf %s "checking whether C compiler accepts -MMD -MP... " >&6; } +if test ${ax_cv_check_cflags___MMD__MP+y} +then : + printf %s "(cached) " >&6 +else $as_nop + + ax_check_save_flags=$CFLAGS + CFLAGS="$CFLAGS -MMD -MP" + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main (void) +{ + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + ax_cv_check_cflags___MMD__MP=yes +else $as_nop + ax_cv_check_cflags___MMD__MP=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext + CFLAGS=$ax_check_save_flags +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ax_cv_check_cflags___MMD__MP" >&5 +printf "%s\n" "$ax_cv_check_cflags___MMD__MP" >&6; } +if test "x$ax_cv_check_cflags___MMD__MP" = xyes +then : + + DEPS_CFLAGS="$DEPS_CFLAGS -MMD -MP" + +else $as_nop + : +fi + +HAVE_SPECTRE="no" { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether C compiler accepts -mindirect-branch=thunk" >&5 printf %s "checking whether C compiler accepts -mindirect-branch=thunk... " >&6; } if test ${ax_cv_check_cflags___mindirect_branch_thunk+y} @@ -5311,6 +5355,7 @@ Compile options: CPPFLAGS: $CPPFLAGS LDFLAGS: $LDFLAGS EXTRA_CFLAGS: $EXTRA_CFLAGS + DEPS_CFLAGS: $DEPS_CFLAGS EXTRA_LDFLAGS: $EXTRA_LDFLAGS LIBS: $LIBS fatal warnings: $HAVE_FATAL_WARNINGS diff --git a/configure.ac b/configure.ac index d3b05d83978..7a2c379fe31 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,12 @@ AC_CONFIG_MACRO_DIR([m4]) AC_PROG_CC +DEPS_CFLAGS="" +AC_SUBST([DEPS_CFLAGS]) +AX_CHECK_COMPILE_FLAG([-MMD -MP], [ + DEPS_CFLAGS="$DEPS_CFLAGS -MMD -MP" +]) + HAVE_SPECTRE="no" AX_CHECK_COMPILE_FLAG([-mindirect-branch=thunk], [ HAVE_SPECTRE="yes" @@ -279,6 +285,7 @@ Compile options: CPPFLAGS: $CPPFLAGS LDFLAGS: $LDFLAGS EXTRA_CFLAGS: $EXTRA_CFLAGS + DEPS_CFLAGS: $DEPS_CFLAGS EXTRA_LDFLAGS: $EXTRA_LDFLAGS LIBS: $LIBS fatal warnings: $HAVE_FATAL_WARNINGS diff --git a/src/etc-cleanup/Makefile b/src/etc-cleanup/Makefile index 23121c5215e..d9dcdbea8fd 100644 --- a/src/etc-cleanup/Makefile +++ b/src/etc-cleanup/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/etc_groups.h - include $(ROOT)/src/prog.mk diff --git a/src/fbuilder/Makefile b/src/fbuilder/Makefile index f0e374a5204..70c5fb0089f 100644 --- a/src/fbuilder/Makefile +++ b/src/fbuilder/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/syscall.h - include $(ROOT)/src/prog.mk diff --git a/src/fcopy/Makefile b/src/fcopy/Makefile index a9f706eb858..4897980e7d2 100644 --- a/src/fcopy/Makefile +++ b/src/fcopy/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/syscall.h EXTRA_OBJS = ../lib/common.o include $(ROOT)/src/prog.mk diff --git a/src/fids/Makefile b/src/fids/Makefile index 7bdbe059078..dadb17a1b9f 100644 --- a/src/fids/Makefile +++ b/src/fids/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h - include $(ROOT)/src/prog.mk diff --git a/src/firecfg/Makefile b/src/firecfg/Makefile index cc7a27c521f..6cbd1b5ba06 100644 --- a/src/firecfg/Makefile +++ b/src/firecfg/Makefile @@ -7,13 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = \ -../include/common.h \ -../include/euid_common.h \ -../include/libnetlink.h \ -../include/firejail_user.h \ -../include/pid.h - EXTRA_OBJS = ../lib/common.o ../lib/firejail_user.o include $(ROOT)/src/prog.mk diff --git a/src/firejail/Makefile b/src/firejail/Makefile index 1c4b1b5c281..e7b334cd89c 100644 --- a/src/firejail/Makefile +++ b/src/firejail/Makefile @@ -7,19 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = \ -../include/rundefs.h \ -../include/common.h \ -../include/ldd_utils.h \ -../include/euid_common.h \ -../include/pid.h \ -../include/seccomp.h \ -../include/syscall_i386.h \ -../include/syscall_x86_64.h \ -../include/firejail_user.h \ -../include/etc_groups.h - - EXTRA_OBJS = \ ../lib/common.o \ ../lib/ldd_utils.o \ diff --git a/src/firemon/Makefile b/src/firemon/Makefile index 0e39f8b018a..02e3b6eed14 100644 --- a/src/firemon/Makefile +++ b/src/firemon/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/pid.h EXTRA_OBJS = ../lib/common.o ../lib/pid.o include $(ROOT)/src/prog.mk diff --git a/src/fldd/Makefile b/src/fldd/Makefile index 246cbc18308..3e48b43ab9b 100644 --- a/src/fldd/Makefile +++ b/src/fldd/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/syscall.h ../include/ldd_utils.h EXTRA_OBJS = ../lib/common.o ../lib/ldd_utils.o include $(ROOT)/src/prog.mk diff --git a/src/fnet/Makefile b/src/fnet/Makefile index c98326dc44c..dfeefdc470d 100644 --- a/src/fnet/Makefile +++ b/src/fnet/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/libnetlink.h EXTRA_OBJS = ../lib/common.o ../lib/libnetlink.o include $(ROOT)/src/prog.mk diff --git a/src/fnetfilter/Makefile b/src/fnetfilter/Makefile index c8992c3d6a5..778de3a8bf1 100644 --- a/src/fnetfilter/Makefile +++ b/src/fnetfilter/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/syscall.h EXTRA_OBJS = ../lib/common.o include $(ROOT)/src/prog.mk diff --git a/src/fsec-optimize/Makefile b/src/fsec-optimize/Makefile index caead9d54de..babed1fdb07 100644 --- a/src/fsec-optimize/Makefile +++ b/src/fsec-optimize/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/seccomp.h ../include/syscall.h EXTRA_OBJS = ../lib/common.o ../lib/errno.o include $(ROOT)/src/prog.mk diff --git a/src/fsec-print/Makefile b/src/fsec-print/Makefile index 43cc42fed2e..bf383d2d041 100644 --- a/src/fsec-print/Makefile +++ b/src/fsec-print/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/seccomp.h ../include/syscall.h EXTRA_OBJS = ../lib/common.o ../lib/errno.o ../lib/syscall.o include $(ROOT)/src/prog.mk diff --git a/src/fseccomp/Makefile b/src/fseccomp/Makefile index 89b12f62936..66bb5844452 100644 --- a/src/fseccomp/Makefile +++ b/src/fseccomp/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/syscall.h EXTRA_OBJS = ../lib/common.o ../lib/errno.o ../lib/syscall.o include $(ROOT)/src/prog.mk diff --git a/src/fzenity/Makefile b/src/fzenity/Makefile index 3c1aa0574c8..4cebe7ac590 100644 --- a/src/fzenity/Makefile +++ b/src/fzenity/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h - include $(ROOT)/src/prog.mk diff --git a/src/jailcheck/Makefile b/src/jailcheck/Makefile index 1823ee9d075..431a21c8ee1 100644 --- a/src/jailcheck/Makefile +++ b/src/jailcheck/Makefile @@ -7,7 +7,6 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h ../include/pid.h EXTRA_OBJS = ../lib/common.o ../lib/pid.o include $(ROOT)/src/prog.mk diff --git a/src/libpostexecseccomp/Makefile b/src/libpostexecseccomp/Makefile index f66fdd0d758..a8d3fd8b74c 100644 --- a/src/libpostexecseccomp/Makefile +++ b/src/libpostexecseccomp/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) SO = $(MOD_DIR)/$(MOD).so TARGET = $(SO) -EXTRA_HDRS = ../include/seccomp.h ../include/rundefs.h - include $(ROOT)/src/so.mk diff --git a/src/libtracelog/Makefile b/src/libtracelog/Makefile index a8372616724..e9330236c26 100644 --- a/src/libtracelog/Makefile +++ b/src/libtracelog/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) SO = $(MOD_DIR)/$(MOD).so TARGET = $(SO) -EXTRA_HDRS = ../include/rundefs.h - include $(ROOT)/src/so.mk diff --git a/src/profstats/Makefile b/src/profstats/Makefile index 8f33ce1deb3..213b1476dc2 100644 --- a/src/profstats/Makefile +++ b/src/profstats/Makefile @@ -7,6 +7,4 @@ MOD_DIR = $(ROOT)/src/$(MOD) PROG = $(MOD_DIR)/$(MOD) TARGET = $(PROG) -EXTRA_HDRS = ../include/common.h - include $(ROOT)/src/prog.mk diff --git a/src/prog.mk b/src/prog.mk index debdf35724d..a639e87fc42 100644 --- a/src/prog.mk +++ b/src/prog.mk @@ -3,16 +3,22 @@ # Note: $(ROOT)/config.mk must be included before this file. # # The includer should probably define PROG and TARGET and may also want to -# define EXTRA_HDRS and EXTRA_OBJS and extend CLEANFILES. +# define EXTRA_OBJS and extend CLEANFILES. -HDRS := $(sort $(wildcard $(MOD_DIR)/*.h)) +HDRS := SRCS := $(sort $(wildcard $(MOD_DIR)/*.c)) OBJS := $(SRCS:.c=.o) +DEPS := $(sort $(wildcard $(OBJS:.o=.d))) + +ifeq ($(DEPS),) +HDRS := $(sort $(wildcard $(MOD_DIR)/*.h $(ROOT)/src/include/*.h)) +endif .PHONY: all all: $(TARGET) +-include $(DEPS) -%.o : %.c $(HDRS) $(EXTRA_HDRS) $(ROOT)/config.mk +%.o : %.c $(HDRS) $(ROOT)/config.mk $(CC) $(PROG_CFLAGS) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(PROG): $(OBJS) $(EXTRA_OBJS) $(ROOT)/config.mk diff --git a/src/so.mk b/src/so.mk index 95b52408101..ac76ffc3063 100644 --- a/src/so.mk +++ b/src/so.mk @@ -3,16 +3,22 @@ # Note: $(ROOT)/config.mk must be included before this file. # # The includer should probably define SO and TARGET and may also want to define -# EXTRA_HDRS and EXTRA_OBJS and extend CLEANFILES. +# EXTRA_OBJS and extend CLEANFILES. -HDRS := $(sort $(wildcard $(MOD_DIR)/*.h)) +HDRS := SRCS := $(sort $(wildcard $(MOD_DIR)/*.c)) OBJS := $(SRCS:.c=.o) +DEPS := $(sort $(wildcard $(OBJS:.o=.d))) + +ifeq ($(DEPS),) +HDRS := $(sort $(wildcard $(MOD_DIR)/*.h $(ROOT)/src/include/*.h)) +endif .PHONY: all all: $(TARGET) +-include $(DEPS) -%.o : %.c $(HDRS) $(EXTRA_HDRS) $(ROOT)/config.mk +%.o : %.c $(HDRS) $(ROOT)/config.mk $(CC) $(SO_CFLAGS) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(SO): $(OBJS) $(EXTRA_OBJS) $(ROOT)/config.mk