From: Yann Collet Date: Wed, 28 Oct 2020 16:39:15 +0000 (-0700) Subject: minor Makefile refactor X-Git-Tag: v1.4.7~41^2~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6ecf1568f147b13b30646aa5ee7342763382c38;p=thirdparty%2Fzstd.git minor Makefile refactor hopefully improving readability --- diff --git a/lib/Makefile b/lib/Makefile index a549b82bd..c6197e2d1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -15,12 +15,9 @@ default: lib-release $(V)$(VERBOSE).SILENT: # When cross-compiling from linux to windows, -# you might need to specify this as "Windows." -# Fedora build fails without it. -# -# Note: mingw-w64 build from linux to windows -# does not fail on other tested distros (ubuntu, debian) -# even without manually specifying the TARGET_SYSTEM. +# one might need to specify TARGET_SYSTEM as "Windows." +# Building from Fedora fails without it. +# (but Ubuntu and Debian don't need to set anything) TARGET_SYSTEM ?= $(OS) # Version numbers @@ -35,8 +32,8 @@ LIBVER := $(shell echo $(LIBVER_SCRIPT)) VERSION?= $(LIBVER) CCVER := $(shell $(CC) --version) -# This is a helper variable that configures a bunch of other variables to new, -# space-optimized defaults. +# ZSTD_LIB_MINIFY is a helper variable that +# configures a bunch of other variables to space-optimized defaults. ZSTD_LIB_MINIFY ?= 0 ifneq ($(ZSTD_LIB_MINIFY), 0) HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0) @@ -177,9 +174,7 @@ vpath %.c $(ZSTD_SUBDIR) UNAME := $(shell uname) -BUILD_DIR ?= 0 - -ifeq ($(BUILD_DIR),0) +ifndef BUILD_DIR ifeq ($(UNAME), Darwin) HASH ?= md5 else ifeq ($(UNAME), FreeBSD) @@ -195,7 +190,7 @@ ifeq ($(HAVE_HASH),0) $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags) BUILD_DIR := obj/generic_noconf endif -endif # $(BUILD_DIR) == 0 +endif # BUILD_DIR # macOS linker doesn't support -soname, and use different extension @@ -212,27 +207,31 @@ else SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) endif +SET_CACHE_DIRECTORY = \ + $(MAKE) --no-print-directory $@ \ + BUILD_DIR=obj/$(HASH_DIR) \ + CPPFLAGS="$(CPPFLAGS)" \ + CFLAGS="$(CFLAGS)" \ + LDFLAGS="$(LDFLAGS)" -.PHONY: lib-all all clean install uninstall -default: lib-release +.PHONY: lib-all all clean install uninstall # alias lib-all: all all: lib -ifeq ($(BUILD_DIR),0) -# determine a BUILD_DIR +.PHONY: libzstd.a # must be run every time + +ifndef BUILD_DIR +# determine BUILD_DIR from compilation flags -.PHONY: libzstd.a # not the actual recipe libzstd.a: - $(MAKE) --no-print-directory $@ \ - BUILD_DIR=obj/$(HASH_DIR) \ - CPPFLAGS="$(CPPFLAGS)" + $(SET_CACHE_DIRECTORY) else -# $(BUILD_DIR) is defined +# BUILD_DIR is defined ZSTD_STATLIB_DIR := $(BUILD_DIR)/static ZSTD_STATLIB := $(ZSTD_STATLIB_DIR)/libzstd.a @@ -243,7 +242,6 @@ $(ZSTD_STATLIB): $(ZSTD_STATLIB_OBJ) @echo compiling static library $(AR) $(ARFLAGS) $@ $^ -.PHONY: libzstd.a # must be run every time libzstd.a: $(ZSTD_STATLIB) ln -sf $< $@ @@ -256,25 +254,21 @@ $(LIBZSTD): $(ZSTD_FILES) @echo compiling dynamic library $(LIBVER) $(CC) $(FLAGS) -DZSTD_DLL_EXPORT=1 -Wl,--out-implib,dll\libzstd.dll.a -shared $^ -o $@ -else +else # not Windows LIBZSTD = libzstd.$(SHARED_EXT_VER) +.PHONY: $(LIBZSTD) # must be run every time $(LIBZSTD): CFLAGS += -fPIC $(LIBZSTD): LDFLAGS += -shared -fvisibility=hidden -ifeq ($(BUILD_DIR),0) -# determine a BUILD_DIR +ifndef BUILD_DIR +# determine BUILD_DIR from compilation flags -.PHONY: $(LIBZSTD) # not the actual recipe $(LIBZSTD): - $(MAKE) --no-print-directory $@ \ - BUILD_DIR=obj/$(HASH_DIR) \ - CPPFLAGS="$(CPPFLAGS)" \ - CFLAGS="$(CFLAGS)" \ - LDFLAGS="$(LDFLAGS)" + $(SET_CACHE_DIRECTORY) else -# $(BUILD_DIR) is defined +# BUILD_DIR is defined ZSTD_DYNLIB_DIR := $(BUILD_DIR)/dynamic ZSTD_DYNLIB := $(ZSTD_DYNLIB_DIR)/$(LIBZSTD) @@ -288,11 +282,10 @@ $(ZSTD_DYNLIB): $(ZSTD_DYNLIB_OBJ) ln -sf $@ libzstd.$(SHARED_EXT_MAJOR) ln -sf $@ libzstd.$(SHARED_EXT) -.PHONY: $(LIBZSTD) # must be run every time $(LIBZSTD): $(ZSTD_DYNLIB) ln -sf $< $@ -endif # if BUILD_DIR +endif # ifndef BUILD_DIR endif # if windows .PHONY: libzstd diff --git a/programs/Makefile b/programs/Makefile index e9ba02cd5..3443014ce 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -98,9 +98,8 @@ ZSTD_ALL_SRC := $(ZSTDLIB_LOCAL_SRC) $(ZSTD_CLI_SRC) ZSTD_ALL_OBJ := $(ZSTD_ALL_SRC:.c=.o) UNAME := $(shell uname) -BUILD_DIR ?= 0 -ifeq ($(BUILD_DIR),0) +ifndef BUILD_DIR ifeq ($(UNAME), Darwin) HASH ?= md5 else ifeq ($(UNAME), FreeBSD) @@ -116,7 +115,7 @@ ifeq ($(HAVE_HASH),0) $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags) BUILD_DIR := obj/generic_noconf endif -endif # $(BUILD_DIR) == 0 +endif # BUILD_DIR # Define *.exe as extension for Windows systems ifneq (,$(filter Windows%,$(OS))) @@ -193,6 +192,13 @@ ifeq ($(BACKTRACE), 1) endif endif +SET_CACHE_DIRECTORY = \ + $(MAKE) --no-print-directory $@ \ + BUILD_DIR=obj/$(HASH_DIR) \ + CPPFLAGS="$(CPPFLAGS)" \ + CFLAGS="$(CFLAGS)" \ + LDFLAGS="$(LDFLAGS)" + .PHONY: all all: zstd @@ -200,15 +206,6 @@ all: zstd .PHONY: allVariants allVariants: zstd zstd-compress zstd-decompress zstd-small zstd-nolegacy zstd-dictBuilder -ZSTD_OBJ := $(addprefix $(BUILD_DIR)/, $(ZSTD_ALL_OBJ)) -$(BUILD_DIR)/zstd : $(ZSTD_OBJ) - @echo "$(THREAD_MSG)" - @echo "$(ZLIB_MSG)" - @echo "$(LZMA_MSG)" - @echo "$(LZ4_MSG)" - @echo LINK $@ - $(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS) - .PHONY: zstd # must always be run zstd : CPPFLAGS += $(THREAD_CPP) $(ZLIBCPP) $(LZMACPP) $(LZ4CPP) zstd : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) $(DEBUGFLAGS_LD) @@ -217,24 +214,29 @@ ifneq (,$(filter Windows%,$(OS))) zstd : $(RES_FILE) endif -ifeq ($(BUILD_DIR),0) -# generate a BUILD_DIR from flags +ifndef BUILD_DIR +# generate BUILD_DIR from flags zstd: - $(MAKE) --no-print-directory $@ \ - BUILD_DIR=obj/$(HASH_DIR) \ - CPPFLAGS="$(CPPFLAGS)" \ - CFLAGS="$(CFLAGS)" \ - LDFLAGS="$(LDFLAGS)" + $(SET_CACHE_DIRECTORY) else # BUILD_DIR is defined +ZSTD_OBJ := $(addprefix $(BUILD_DIR)/, $(ZSTD_ALL_OBJ)) +$(BUILD_DIR)/zstd : $(ZSTD_OBJ) + @echo "$(THREAD_MSG)" + @echo "$(ZLIB_MSG)" + @echo "$(LZMA_MSG)" + @echo "$(LZ4_MSG)" + @echo LINK $@ + $(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS) + zstd : $(BUILD_DIR)/zstd ln -sf $< $@ @echo zstd build completed -endif +endif # BUILD_DIR .PHONY: zstd-release