2
0

libzstd.mk 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # ################################################################
  2. # Copyright (c) Yann Collet, Facebook, Inc.
  3. # All rights reserved.
  4. #
  5. # This source code is licensed under both the BSD-style license (found in the
  6. # LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. # in the COPYING file in the root directory of this source tree).
  8. # You may select, at your option, one of the above-listed licenses.
  9. # ################################################################
  10. ##################################################################
  11. # Input Variables
  12. ##################################################################
  13. # Zstd lib directory
  14. LIBZSTD ?= ./
  15. # Legacy support
  16. ZSTD_LEGACY_SUPPORT ?= 5
  17. ZSTD_LEGACY_MULTITHREADED_API ?= 0
  18. # Build size optimizations
  19. HUF_FORCE_DECOMPRESS_X1 ?= 0
  20. HUF_FORCE_DECOMPRESS_X2 ?= 0
  21. ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 0
  22. ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG ?= 0
  23. ZSTD_NO_INLINE ?= 0
  24. ZSTD_STRIP_ERROR_STRINGS ?= 0
  25. # Assembly support
  26. ZSTD_NO_ASM ?= 0
  27. ##################################################################
  28. # libzstd helpers
  29. ##################################################################
  30. VOID ?= /dev/null
  31. # Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/)
  32. NUM_SYMBOL := \#
  33. # define silent mode as default (verbose mode with V=1 or VERBOSE=1)
  34. $(V)$(VERBOSE).SILENT:
  35. # When cross-compiling from linux to windows,
  36. # one might need to specify TARGET_SYSTEM as "Windows."
  37. # Building from Fedora fails without it.
  38. # (but Ubuntu and Debian don't need to set anything)
  39. TARGET_SYSTEM ?= $(OS)
  40. # Version numbers
  41. LIBVER_SRC := $(LIBZSTD)/zstd.h
  42. LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
  43. LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
  44. LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
  45. LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
  46. LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
  47. LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
  48. LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
  49. LIBVER := $(shell echo $(LIBVER_SCRIPT))
  50. CCVER := $(shell $(CC) --version)
  51. ZSTD_VERSION?= $(LIBVER)
  52. # ZSTD_LIB_MINIFY is a helper variable that
  53. # configures a bunch of other variables to space-optimized defaults.
  54. ZSTD_LIB_MINIFY ?= 0
  55. ifneq ($(ZSTD_LIB_MINIFY), 0)
  56. HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0)
  57. ZSTD_LEGACY_SUPPORT ?= 0
  58. ZSTD_LIB_DEPRECATED ?= 0
  59. HUF_FORCE_DECOMPRESS_X1 ?= 1
  60. ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 1
  61. ZSTD_NO_INLINE ?= 1
  62. ZSTD_STRIP_ERROR_STRINGS ?= 1
  63. ifneq ($(HAVE_CC_OZ), 0)
  64. # Some compilers (clang) support an even more space-optimized setting.
  65. CFLAGS += -Oz
  66. else
  67. CFLAGS += -Os
  68. endif
  69. CFLAGS += -fno-stack-protector -fomit-frame-pointer -fno-ident \
  70. -DDYNAMIC_BMI2=0 -DNDEBUG
  71. else
  72. CFLAGS ?= -O3
  73. endif
  74. DEBUGLEVEL ?= 0
  75. CPPFLAGS += -DXXH_NAMESPACE=ZSTD_ -DDEBUGLEVEL=$(DEBUGLEVEL)
  76. ifeq ($(TARGET_SYSTEM),Windows_NT) # MinGW assumed
  77. CPPFLAGS += -D__USE_MINGW_ANSI_STDIO # compatibility with %zu formatting
  78. endif
  79. DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
  80. -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
  81. -Wstrict-prototypes -Wundef -Wpointer-arith \
  82. -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
  83. -Wredundant-decls -Wmissing-prototypes -Wc++-compat
  84. CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
  85. LDFLAGS += $(MOREFLAGS)
  86. FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
  87. ifndef ALREADY_APPENDED_NOEXECSTACK
  88. export ALREADY_APPENDED_NOEXECSTACK := 1
  89. ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; return 0; }" | $(CC) $(FLAGS) -z noexecstack -x c -Werror - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
  90. LDFLAGS += -z noexecstack
  91. endif
  92. ifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
  93. CFLAGS += -Wa,--noexecstack
  94. else ifeq ($(shell echo | $(CC) $(FLAGS) -Qunused-arguments -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
  95. # See e.g.: https://github.com/android/ndk/issues/171
  96. CFLAGS += -Qunused-arguments -Wa,--noexecstack
  97. endif
  98. endif
  99. HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
  100. GREP_OPTIONS ?=
  101. ifeq ($HAVE_COLORNEVER, 1)
  102. GREP_OPTIONS += --color=never
  103. endif
  104. GREP = grep $(GREP_OPTIONS)
  105. SED_ERE_OPT ?= -E
  106. ZSTD_COMMON_FILES := $(sort $(wildcard $(LIBZSTD)/common/*.c))
  107. ZSTD_COMPRESS_FILES := $(sort $(wildcard $(LIBZSTD)/compress/*.c))
  108. ZSTD_DECOMPRESS_FILES := $(sort $(wildcard $(LIBZSTD)/decompress/*.c))
  109. ZSTD_DICTBUILDER_FILES := $(sort $(wildcard $(LIBZSTD)/dictBuilder/*.c))
  110. ZSTD_DEPRECATED_FILES := $(sort $(wildcard $(LIBZSTD)/deprecated/*.c))
  111. ZSTD_LEGACY_FILES :=
  112. ZSTD_DECOMPRESS_AMD64_ASM_FILES := $(sort $(wildcard $(LIBZSTD)/decompress/*_amd64.S))
  113. ifneq ($(ZSTD_NO_ASM), 0)
  114. CPPFLAGS += -DZSTD_DISABLE_ASM
  115. else
  116. # Unconditionally add the ASM files they are disabled by
  117. # macros in the .S file.
  118. ZSTD_DECOMPRESS_FILES += $(ZSTD_DECOMPRESS_AMD64_ASM_FILES)
  119. endif
  120. ifneq ($(HUF_FORCE_DECOMPRESS_X1), 0)
  121. CFLAGS += -DHUF_FORCE_DECOMPRESS_X1
  122. endif
  123. ifneq ($(HUF_FORCE_DECOMPRESS_X2), 0)
  124. CFLAGS += -DHUF_FORCE_DECOMPRESS_X2
  125. endif
  126. ifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT), 0)
  127. CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
  128. endif
  129. ifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG), 0)
  130. CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
  131. endif
  132. ifneq ($(ZSTD_NO_INLINE), 0)
  133. CFLAGS += -DZSTD_NO_INLINE
  134. endif
  135. ifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0)
  136. CFLAGS += -DZSTD_STRIP_ERROR_STRINGS
  137. endif
  138. ifneq ($(ZSTD_LEGACY_MULTITHREADED_API), 0)
  139. CFLAGS += -DZSTD_LEGACY_MULTITHREADED_API
  140. endif
  141. ifneq ($(ZSTD_LEGACY_SUPPORT), 0)
  142. ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
  143. ZSTD_LEGACY_FILES += $(shell ls $(LIBZSTD)/legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
  144. endif
  145. endif
  146. CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
  147. UNAME := $(shell uname)
  148. ifndef BUILD_DIR
  149. ifeq ($(UNAME), Darwin)
  150. ifeq ($(shell md5 < /dev/null > /dev/null; echo $$?), 0)
  151. HASH ?= md5
  152. endif
  153. else ifeq ($(UNAME), FreeBSD)
  154. HASH ?= gmd5sum
  155. else ifeq ($(UNAME), NetBSD)
  156. HASH ?= md5 -n
  157. else ifeq ($(UNAME), OpenBSD)
  158. HASH ?= md5
  159. endif
  160. HASH ?= md5sum
  161. HASH_DIR = conf_$(shell echo $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(ZSTD_FILES) | $(HASH) | cut -f 1 -d " " )
  162. HAVE_HASH :=$(shell echo 1 | $(HASH) > /dev/null && echo 1 || echo 0)
  163. ifeq ($(HAVE_HASH),0)
  164. $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags)
  165. BUILD_DIR := obj/generic_noconf
  166. endif
  167. endif # BUILD_DIR
  168. ZSTD_SUBDIR := $(LIBZSTD)/common $(LIBZSTD)/compress $(LIBZSTD)/decompress $(LIBZSTD)/dictBuilder $(LIBZSTD)/legacy $(LIBZSTD)/deprecated
  169. vpath %.c $(ZSTD_SUBDIR)
  170. vpath %.S $(ZSTD_SUBDIR)