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

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