Blame 0049-Rework-some-makefile-bits-to-make-overriding-some-op.patch

38cfe28
From 998f617cec92d526e1fadb745673ceef63fa1483 Mon Sep 17 00:00:00 2001
38cfe28
From: Peter Jones <pjones@redhat.com>
38cfe28
Date: Thu, 10 Oct 2019 15:45:10 -0400
38cfe28
Subject: [PATCH 49/86] Rework some makefile bits to make overriding some
38cfe28
 options simpler.
38cfe28
38cfe28
This reworks a lot of defaults.mk to make some variables more regularly
38cfe28
partially overridden on the command line, making e.g. suppression of
38cfe28
-Werror easier when warranted.  It also makes having per-compiler
38cfe28
versions of each thing simpler.
38cfe28
38cfe28
The following variables can be overridden, though in some cases only
38cfe28
partially:
38cfe28
38cfe28
name			kind of things in it
38cfe28
COMPILER		gcc
38cfe28
CROSS_COMPILE		arm-linux-gnu-
38cfe28
CC			$(CROSS_COMPILE)$(COMPILER)
38cfe28
DEBUGINFO		-g
38cfe28
OPTIMIZE		-O2 -flto
38cfe28
WARNINGS		-Wfoo -Wno-bar
38cfe28
ERRORS			-Werror -Wno-error=cpp
38cfe28
CPPFLAGS		-D and -I
38cfe28
CFLAGS			$(OPTIMIZE) $(DEBUGINFO) $(WARNINGS) $(ERRORS)
38cfe28
LDFLAGS			linker options (add-only)
38cfe28
CCLDFLAGS		-Wl, prefixed linker options (add-only and it's
38cfe28
			normally machine generated from LDFLAGS
38cfe28
HOSTCC			$(COMPILER)
38cfe28
HOST_CPPFLAGS		$(CPPFLAGS) (add-only)
38cfe28
HOST_CFLAGS		$(CFLAGS) (add-only)
38cfe28
HOST_CCLDFLAGS		$(CCLDFLAGS) (add-only)
38cfe28
38cfe28
This resolves issue #31
38cfe28
38cfe28
Signed-off-by: Peter Jones <pjones@redhat.com>
38cfe28
---
38cfe28
 src/Makefile            |  5 ++-
38cfe28
 src/include/defaults.mk | 83 +++++++++++++++++++++++++++--------------
38cfe28
 src/include/gcc.specs   | 11 +-----
38cfe28
 src/include/rules.mk    | 51 +++++++++++++------------
38cfe28
 src/test/Makefile       |  2 +-
38cfe28
 5 files changed, 87 insertions(+), 65 deletions(-)
38cfe28
38cfe28
diff --git a/src/Makefile b/src/Makefile
38cfe28
index e5f10d4c7bf..1d0e217c89b 100644
38cfe28
--- a/src/Makefile
38cfe28
+++ b/src/Makefile
38cfe28
@@ -51,10 +51,11 @@ include/efivar/efivar-guids.h : makeguids guids.txt
38cfe28
 	./makeguids guids.txt guids.bin names.bin \
38cfe28
 		guid-symbols.c include/efivar/efivar-guids.h
38cfe28
 
38cfe28
-makeguids : CPPFLAGS+=-DEFIVAR_BUILD_ENVIRONMENT -march=native
38cfe28
+makeguids : CPPFLAGS=$(HOST_CPPFLAGS)
38cfe28
 makeguids : LIBS=dl
38cfe28
+makeguids : CCLD=$(HOSTCCLD)
38cfe28
+makeguids : CFLAGS=$(HOST_CFLAGS)
38cfe28
 makeguids : $(MAKEGUIDS_SOURCES)
38cfe28
-makeguids : CCLD=$(CCLD_FOR_BUILD)
38cfe28
 
38cfe28
 guids.o : guids.S | guids.bin names.bin
38cfe28
 
38cfe28
diff --git a/src/include/defaults.mk b/src/include/defaults.mk
38cfe28
index 7290187ceba..df738feddea 100644
38cfe28
--- a/src/include/defaults.mk
38cfe28
+++ b/src/include/defaults.mk
38cfe28
@@ -8,19 +8,63 @@ BINDIR	?= $(EXEC_PREFIX)/bin
38cfe28
 PCDIR	?= $(LIBDIR)/pkgconfig
38cfe28
 DESTDIR	?=
38cfe28
 
38cfe28
-INSTALL ?= install
38cfe28
 CROSS_COMPILE	?=
38cfe28
 COMPILER ?= gcc
38cfe28
-PKG_CONFIG = $(shell if [ -e "$$(env $(CROSS_COMPILE)pkg-config 2>&1)" ]; then echo $(CROSS_COMPILE)pkg-config ; else echo pkg-config ; fi)
38cfe28
-CC_FOR_BUILD	?= cc
38cfe28
-CC	:= $(if $(filter default,$(origin CC)),$(CROSS_COMPILE)$(COMPILER),$(CC))
38cfe28
-CCLD_FOR_BUILD	?= $(CC_FOR_BUILD)
38cfe28
-CCLD	:= $(if $(filter undefined,$(origin CCLD)),$(CC),$(CCLD))
38cfe28
+$(call set-if-undefined,CC,$(CROSS_COMPILE)$(COMPILER))
38cfe28
+$(call set-if-undefined,CCLD,$(CC))
38cfe28
+$(call set-if-undefined,HOSTCC,$(COMPILER))
38cfe28
+$(call set-if-undefined,HOSTCCLD,$(HOSTCC))
38cfe28
+
38cfe28
 OPTIMIZE ?= -O2 -flto
38cfe28
-CFLAGS	?= $(OPTIMIZE) -g3
38cfe28
-CFLAGS	:= $(CFLAGS)
38cfe28
+DEBUGINFO ?= -g3
38cfe28
+WARNINGS_GCC ?= -Wmaybe-uninitialized \
38cfe28
+		-Wno-nonnull-compare
38cfe28
+WARNINGS_CCC_ANALYZER ?= $(WARNINGS_GCC)
38cfe28
+WARNINGS ?= -Wall -Wextra \
38cfe28
+	    -Wno-address-of-packed-member \
38cfe28
+	    $(call family,WARNINGS)
38cfe28
+ERRORS ?= -Werror -Wno-error=cpp $(call family,ERRORS)
38cfe28
+CPPFLAGS ?=
38cfe28
+override _CPPFLAGS := $(CPPFLAGS)
38cfe28
+override CPPFLAGS = $(_CPPFLAGS) -DLIBEFIVAR_VERSION=$(VERSION) \
38cfe28
+	    -D_GNU_SOURCE \
38cfe28
+	    -I$(TOPDIR)/src/include/
38cfe28
+CFLAGS ?= $(OPTIMIZE) $(DEBUGINFO) $(WARNINGS) $(ERRORS)
38cfe28
+CFLAGS_GCC ?= -specs=$(TOPDIR)/src/include/gcc.specs \
38cfe28
+	      -fno-merge-constants
38cfe28
+override _CFLAGS := $(CFLAGS)
38cfe28
+override CFLAGS = $(_CFLAGS) \
38cfe28
+		  -std=gnu11 \
38cfe28
+		  -funsigned-char \
38cfe28
+		  -fvisibility=hidden \
38cfe28
+		  $(call family,CFLAGS) \
38cfe28
+		  $(call pkg-config-cflags)
38cfe28
+LDFLAGS_CLANG ?= --fatal-warnings -pie -z relro
38cfe28
 LDFLAGS ?=
38cfe28
-LDFLAGS := $(LDFLAGS)
38cfe28
+override _LDFLAGS := $(LDFLAGS)
38cfe28
+override LDFLAGS = $(_LDFLAGS) \
38cfe28
+		   --add-needed \
38cfe28
+		   --build-id \
38cfe28
+		   --no-allow-shlib-undefined \
38cfe28
+		   --no-undefined-version \
38cfe28
+		   -z now \
38cfe28
+		   -z muldefs \
38cfe28
+		   $(call family,LDFLAGS)
38cfe28
+CCLDFLAGS ?=
38cfe28
+override _CCLDFLAGS := $(CCLDFLAGS)
38cfe28
+override CCLDFLAGS = $(CFLAGS) -L. $(_CCLDFLAGS) \
38cfe28
+		     $(call add-prefix,-Wl,$(LDFLAGS)) \
38cfe28
+		     $(call pkg-config-ccldflags)
38cfe28
+HOST_CPPFLAGS ?= $(CPPFLAGS)
38cfe28
+override _HOST_CPPFLAGS := $(HOST_CPPFLAGS)
38cfe28
+override HOST_CPPFLAGS = $(_HOST_CPPFLAGS) \
38cfe28
+			 -DEFIVAR_BUILD_ENVIRONMENT -march=native
38cfe28
+HOST_CFLAGS ?= $(CFLAGS)
38cfe28
+override _HOST_CFLAGS := $(HOST_CFLAGS)
38cfe28
+override HOST_CFLAGS = $(_HOST_CFLAGS)
38cfe28
+
38cfe28
+PKG_CONFIG = $(shell if [ -e "$$(env $(CROSS_COMPILE)pkg-config 2>&1)" ]; then echo $(CROSS_COMPILE)pkg-config ; else echo pkg-config ; fi)
38cfe28
+INSTALL ?= install
38cfe28
 AR	:= $(CROSS_COMPILE)$(COMPILER)-ar
38cfe28
 NM	:= $(CROSS_COMPILE)$(COMPILER)-nm
38cfe28
 RANLIB	:= $(CROSS_COMPILE)$(COMPILER)-ranlib
38cfe28
@@ -29,26 +73,7 @@ ABIDIFF := abidiff
38cfe28
 
38cfe28
 PKGS	=
38cfe28
 
38cfe28
-CPPFLAGS += -DLIBEFIVAR_VERSION=$(VERSION)
38cfe28
-
38cfe28
-clang_cflags = -D_GNU_SOURCE -std=gnu11 -Wno-address-of-packed-member \
38cfe28
-	       -funsigned-char -Wall -Wno-nonnull-compare \
38cfe28
-	       -Werror -Wno-error=cpp
38cfe28
-gcc_cflags = -specs=$(TOPDIR)/src/include/gcc.specs
38cfe28
-cflags	= $(CFLAGS) -I${TOPDIR}/src/include/ \
38cfe28
-	$(if $(findstring clang,$(CC)),$(clang_cflags),) \
38cfe28
-	$(if $(findstring ccc-analyzer,$(CC)),$(clang_cflags),) \
38cfe28
-	$(if $(findstring gcc,$(CC)),$(gcc_cflags),) \
38cfe28
-	$(call pkg-config-cflags)
38cfe28
-clang_ccldflags =
38cfe28
-gcc_ccldflags =
38cfe28
-ccldflags = $(cflags) -L. $(CCLDFLAGS) $(LDFLAGS) \
38cfe28
-	-Wl,-z,muldefs \
38cfe28
-	$(if $(findstring clang,$(CCLD)),$(clang_ccldflags),) \
38cfe28
-	$(if $(findstring ccc-analyzer,$(CCLD)),$(clang_ccldflags),) \
38cfe28
-	$(if $(findstring gcc,$(CCLD)),$(gcc_ccldflags),) \
38cfe28
-	$(call pkg-config-ldflags)
38cfe28
-SOFLAGS=-shared
38cfe28
+SOFLAGS=-shared $(call family,SOFLAGS)
38cfe28
 LDLIBS=$(foreach lib,$(LIBS),-l$(lib)) $(call pkg-config-ldlibs)
38cfe28
 
38cfe28
 COMMIT_ID=$(shell git log -1 --pretty=%H 2>/dev/null || echo master)
38cfe28
diff --git a/src/include/gcc.specs b/src/include/gcc.specs
38cfe28
index 9d2b145ee6e..ef28e2bb51a 100644
38cfe28
--- a/src/include/gcc.specs
38cfe28
+++ b/src/include/gcc.specs
38cfe28
@@ -1,14 +1,5 @@
38cfe28
-*cpp:
38cfe28
-+ -D_GNU_SOURCE
38cfe28
-
38cfe28
-*efivar_cpp_options:
38cfe28
- -Wall -Wno-nonnull-compare -Werror -Wno-error=cpp -std=gnu11 -Wextra -funsigned-char
38cfe28
-
38cfe28
-*cpp_options:
38cfe28
-+ %(efivar_cpp_options)
38cfe28
-
38cfe28
 *cc1_options:
38cfe28
-+ %(efivar_cpp_options) -Wmaybe-uninitialized -fno-merge-constants -funsigned-char -fvisibility=hidden %{!r:%{!fpie:%{!fPIE:%{!fpic:%{!fPIC:%{!fno-pic:-fPIE}}}}}} -grecord-gcc-switches
38cfe28
++ %{!r:%{!fpie:%{!fPIE:%{!fpic:%{!fPIC:%{!fno-pic:-fPIE}}}}}} -grecord-gcc-switches
38cfe28
 
38cfe28
 *self_spec:
38cfe28
 + %{!shared:%{!static:%{!r:-pie}}} %{static:-Wl,-no-fatal-warnings -Wl,-static -static -Wl,-z,relro,-z,now} -grecord-gcc-switches
38cfe28
diff --git a/src/include/rules.mk b/src/include/rules.mk
38cfe28
index 1783dda573b..ff0ff4ef61b 100644
38cfe28
--- a/src/include/rules.mk
38cfe28
+++ b/src/include/rules.mk
38cfe28
@@ -4,20 +4,32 @@ default : all
38cfe28
 
38cfe28
 include $(TOPDIR)/src/include/version.mk
38cfe28
 
38cfe28
+comma:= ,
38cfe28
+empty:=
38cfe28
+space:= $(empty) $(empty)
38cfe28
+
38cfe28
+set-if-undefined = $(call eval,$(1) := $(if $(filter default undefined,$(origin $(1))),$(2),$($(1))))
38cfe28
+add-prefix = $(subst $(space),$(empty),$(1)$(foreach x,$(2),$(comma)$(x)))
38cfe28
+
38cfe28
+FAMILY_SUFFIXES = $(if $(findstring clang,$(CC)),CLANG,) \
38cfe28
+		  $(if $(findstring ccc-analyzer,$(CC)),CCC_ANALYZER,) \
38cfe28
+		  $(if $(findstring gcc,$(CC)),GCC,)
38cfe28
+family = $(foreach FAMILY_SUFFIX,$(FAMILY_SUFFIXES),$($(1)_$(FAMILY_SUFFIX)))
38cfe28
+
38cfe28
 %.a :
38cfe28
 	$(AR) -cvqs $@ $^
38cfe28
 
38cfe28
 % : %.c
38cfe28
 
38cfe28
 % : %.c
38cfe28
-	$(CCLD) $(ccldflags) $(CPPFLAGS) -o $@ $^ $(LDLIBS)
38cfe28
+	$(CCLD) $(CCLDFLAGS) $(CPPFLAGS) -o $@ $^ $(LDLIBS)
38cfe28
 
38cfe28
-%-static : ccldflags+=-static
38cfe28
+%-static : CCLDFLAGS+=-static
38cfe28
 %-static : %.c
38cfe28
-	$(CCLD) $(ccldflags) $(CPPFLAGS) -o $@ $^ $(LDLIBS)
38cfe28
+	$(CCLD) $(CCLDFLAGS) $(CPPFLAGS) -o $@ $^ $(LDLIBS)
38cfe28
 
38cfe28
 %.so :
38cfe28
-	$(CCLD) $(ccldflags) $(CPPFLAGS) $(SOFLAGS) \
38cfe28
+	$(CCLD) $(CCLDFLAGS) $(CPPFLAGS) $(SOFLAGS) \
38cfe28
 	  -Wl,-soname,$@.1 \
38cfe28
 	  -Wl,--version-script=$(MAP) \
38cfe28
 	  -o $@ $^ $(LDLIBS)
38cfe28
@@ -35,22 +47,22 @@ include $(TOPDIR)/src/include/version.mk
38cfe28
 		$<
38cfe28
 
38cfe28
 %.o : %.c
38cfe28
-	$(CC) $(cflags) -fPIC $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
+	$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
 
38cfe28
 %.static.o : %.c
38cfe28
-	$(CC) $(cflags) -fPIE $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
+	$(CC) $(CFLAGS) -fPIE $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
 
38cfe28
 %.o : %.S
38cfe28
-	$(CC) $(cflags) -fPIC $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
+	$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
 
38cfe28
 %.static.o : %.S
38cfe28
-	$(CC) $(cflags) -fPIE $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
+	$(CC) $(CFLAGS) -fPIE $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^)
38cfe28
 
38cfe28
 %.S: %.c
38cfe28
-	$(CC) $(cflags) $(CPPFLAGS) -S $< -o $@
38cfe28
+	$(CC) $(CFLAGS) $(CPPFLAGS) -S $< -o $@
38cfe28
 
38cfe28
 %.E: %.c
38cfe28
-	$(CC) $(cflags) $(CPPFLAGS) -E $< -o $@
38cfe28
+	$(CC) $(CFLAGS) $(CPPFLAGS) -E $< -o $@
38cfe28
 
38cfe28
 %.c : %.h
38cfe28
 
38cfe28
@@ -71,20 +83,13 @@ endef
38cfe28
 %.map : %.map.in
38cfe28
 	@$(call substitute-version,$<,$@)
38cfe28
 
38cfe28
-pkg-config-cflags = \
38cfe28
-	$(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --cflags $(PKGS); fi)
38cfe28
-pkg-config-ldflags = \
38cfe28
-	$(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --libs-only-L --libs-only-other $(PKGS) ; fi)
38cfe28
-pkg-config-ldlibs = \
38cfe28
-	$(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --libs-only-l $(PKGS) ; fi)
38cfe28
+pkg-config-cflags = $(if $(PKGS),$(shell $(PKG_CONFIG) --cflags $(PKGS)))
38cfe28
+pkg-config-ccldflags = $(if $(PKGS),$(shell $(PKG_CONFIG) --libs-only-L --libs-only-other $(PKGS)))
38cfe28
+pkg-config-ldlibs = $(if $(PKGS),$(shell $(PKG_CONFIG) --libs-only-l $(PKGS)))
38cfe28
 
38cfe28
-define deps-of
38cfe28
-	$(foreach src,$(filter %.c,$(1)),$(patsubst %.c,.%.d,$(src))) \
38cfe28
-	$(foreach src,$(filter %.S,$(1)),$(patsubst %.S,.%.d,$(src)))
38cfe28
-endef
38cfe28
+deps-of = $(foreach src,$(filter %.c,$(1)),$(patsubst %.c,.%.d,$(src))) \
38cfe28
+	  $(foreach src,$(filter %.S,$(1)),$(patsubst %.S,.%.d,$(src)))
38cfe28
 
38cfe28
-define get-config
38cfe28
-$(shell git config --local --get "efivar.$(1)")
38cfe28
-endef
38cfe28
+get-config = $(shell git config --local --get "efivar.$(1)")
38cfe28
 
38cfe28
 # vim:ft=make
38cfe28
diff --git a/src/test/Makefile b/src/test/Makefile
38cfe28
index df16c7a5f3b..7a2aa496b48 100644
38cfe28
--- a/src/test/Makefile
38cfe28
+++ b/src/test/Makefile
38cfe28
@@ -3,7 +3,7 @@ SRCDIR = $(realpath .)
38cfe28
 include $(TOPDIR)/src/include/defaults.mk
38cfe28
 include $(TOPDIR)/src/include/version.mk
38cfe28
 
38cfe28
-ccldflags += -L$(TOPDIR)/src/ -Wl,-rpath=$(TOPDIR)/src/
38cfe28
+CCLDFLAGS += -L$(TOPDIR)/src/ -Wl,-rpath=$(TOPDIR)/src/
38cfe28
 LIBS=efivar
38cfe28
 
38cfe28
 all : tester
38cfe28
-- 
38cfe28
2.24.1
38cfe28