1 # HOST PLATFORM DETECTION
2 ifeq ($(OS),Windows_NT)
4 WINDOWS_HOST := defined
6 _UNAME := $(shell uname)
7 UNAME_P := $(shell uname -p)
8 ifeq ($(_UNAME),FreeBSD)
9 # Using Linux platform for Unix OSes for now
10 # HOST_PLATFORM := bsd
12 HOST_PLATFORM := linux
15 ifeq ($(_UNAME),Darwin)
16 HOST_PLATFORM := apple
19 HOST_PLATFORM := linux
23 HOST_ARCH := $(UNAME_P)
27 ifndef TARGET_PLATFORM
29 TARGET_PLATFORM := $(PLATFORM)
32 ifndef TARGET_PLATFORM
34 TARGET_PLATFORM := win32
37 TARGET_PLATFORM := apple
40 # TARGET_PLATFORM := bsd
42 TARGET_PLATFORM := linux
51 PLATFORM := $(TARGET_PLATFORM)
53 ifeq ($(TARGET_PLATFORM),win32)
54 WINDOWS_TARGET := defined
56 ifeq ($(TARGET_PLATFORM),apple)
59 #ifeq ($(TARGET_PLATFORM),bsd)
60 # BSD_TARGET := defined
62 LINUX_TARGET := defined
68 ifneq ($(TARGET_PLATFORM),$(HOST_PLATFORM))
69 CROSS_TARGET := defined
73 ifeq ($(TARGET_TYPE),staticlib)
74 STATIC_LIBRARY_TARGET := defined
76 ifeq ($(TARGET_TYPE),sharedlib)
77 SHARED_LIBRARY_TARGET := defined
79 ifeq ($(TARGET_TYPE),executable)
80 EXECUTABLE_TARGET := defined
85 ifeq ($(GCC_PREFIX),i586-mingw32msvc-)
90 ifeq ($(GCC_PREFIX),i686-w64-mingw32-)
95 # Accept different things for ARCH but standardize on x32/x64
96 # This will be used for object directories
116 ifeq ($(ARCH),x86_64)
120 # Set ARCH_FLAGS only if ARCH is set
122 TARGET_ARCH := x86_64
130 ARCH_SUFFIX := .$(ARCH)
133 TARGET_ARCH := $(TARGET_ARCH)-linux-gnu
138 # On Windows/32 bit systems, pass -m32 as TDM-GCC packaged with the installer produces 64 bit executables by default
139 # Disable this if your compiler does not accept -m32
141 ifeq ($(HOST_PLATFORM),win32)
142 ifeq ($(TARGET_PLATFORM),win32)
143 ifndef ProgramFiles(x86)
154 DEBUG_SUFFIX := .debug
158 COMPILER_SUFFIX = $(ARCH_SUFFIX)
160 ifneq ($(COMPILER),default)
161 COMPILER_SUFFIX = .$(COMPILER)$(ARCH_SUFFIX)
167 esc := $(empty)
\7f$(empty)
168 space := $(empty) $(empty)
171 slash := $(empty)/$(empty)
172 backslash := $(empty)\$(empty)
173 escspace = $(subst $(space),$(backslash)$(space),$(subst $(backslash)$(space),$(space),$(1)))
174 hidspace = $(subst $(space),$(esc),$(subst $(backslash)$(space),$(esc),$(1)))
175 shwspace = $(subst $(esc),$(backslash)$(space),$(1))
176 unescp_all = $(subst $(esc),$(backslash),$(subst $(backslash),,$(subst $(backslash)$(backslash),$(esc),$(1))))
178 # HIDDEN SPACE STRING TOOLS
179 temporaty_token := _+;:;+_:;+;:_:+;+:_
180 hidden_space := $(empty)
\7f$(empty)
181 hs_hide = $(subst $(space),$(hidden_space),$(1))
182 hs_unhide = $(subst $(hidden_space),$(space),$(1))
183 hs_escape = $(subst $(hidden_space),$(backslash)$(space),$(1))
184 hs_process = $(subst $(space),$(hidden_space),$(subst $(backslash)$(space),$(hidden_space),$(1)))
185 hs_quote_all = $(foreach item,$(1),"$(call hs_unhide,$(item))")
186 hs_quote_each = $(foreach item,$(1),$(if $(findstring $(esc),$(item)),"$(call hs_unhide,$(item))",$(item)))
189 fp_unquote = $(subst $(quote),,$(1))
190 fp_opt_quotes = $(if $(findstring $(space),$(1)),"$(1)",$(1))
191 fp_no_parent_dir = $(foreach item,$(1),$(if $(findstring ..,$(item)),,$(item)))
195 # usage: $(hs_ls) | $(hs_ls_dir) | $(hs_ls_files)
197 # - for hs_ls: a list of files and directories in the current dir
198 # i.e.: fileA dir1/ fileB fileC dir2/ dir3/
199 # - for hs_ls_dir: a list of directories in the current dir
200 # i.e.: dir1 dir2 dir3
201 # - for hs_ls_file: a list of files in the current dir
202 # i.e.: fileA fileB fileC
204 # - hs_ls* functions work in current dir, you can't specify a directory
205 # - hs_ls* functions do not report hidden files and directories because wildcard doesn't
206 # you would never get such a list: .fileA .dir1/
207 hs_ls = $(subst $(temporaty_token),$(space),$(subst ./,,$(call hs_hide,$(subst $(space)./,$(temporaty_token),$(wildcard ./*/)))))
208 hs_ls_dir = $(subst /,,$(foreach item,$(hs_ls),$(if $(findstring /,$(item)),$(item),)))
209 hs_ls_file = $(foreach item,$(hs_ls),$(if $(findstring /,$(item)),,$(item)))
212 # hs_crossloop usage: $(call hs_crossloop,<list>,<command_function>)
213 # hs_crossloop will call <command_function> with the item as first parameter ($(1))
214 hs_crossloop = $(call hs_unsafe_crossloop,$(call fp_no_parent_dir,$(1)),$(2))
216 # PATH SEPARATOR STRING TOOLS
218 ifneq ($(TERM),cygwin)
220 WIN_PS_TOOLS := defined
224 slash_path = $(subst $(backslash),$(slash),$(1))
227 sys_path = $(subst $(backslash)$(backslash),$(slash),$(subst $(slash),$(backslash),$(1)))
228 quote_path = "$(call sys_path,$(call unescp_all,$(1)))"
229 each_path_quote = $(if $(findstring $(esc),$(path)),"$(call unescp_all,$(call shwspace,$(path)))",$(call unescp_all,$(path)))
230 sys_path_list = $(foreach path,$(1),$(each_path_quote))
237 # PREFIXES AND EXTENSIONS
247 E := $(if $(WINDOWS_TARGET),.exe,)
248 SO := $(if $(WINDOWS_TARGET),.dll,$(if $(OSX_TARGET),.dylib,.so))
249 LP := $(if $(WINDOWS_TARGET),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
250 HOST_E := $(if $(WINDOWS_HOST),.exe,)
251 HOST_SO := $(if $(WINDOWS_HOST),.dll,$(if $(OSX_HOST),.dylib,.so))
252 HOST_LP := $(if $(WINDOWS_HOST),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
253 .SUFFIXES: .c .ec .sym .imp .bowl $(O) $(A)
256 VER := $(if $(LINUX_TARGET),$(if $(LINUX_HOST),$(if $(VERSION),.$(VERSION),),),)
260 CCACHE_COMPILE := ccache$(space)
262 DISTCC_COMPILE := distcc$(space)
266 DISTCC_COMPILE := distcc$(space)
270 _CPP = $(if $(findstring $(space),$(CPP)),"$(CPP)",$(CPP))
272 _SYSROOT = $(if $(SYSROOT),$(space)--sysroot=$(SYSROOT),)
274 _MAKE = $(call fp_opt_quotes,$(MAKE))
278 ifneq ($(TERM),cygwin)
280 WIN_SHELL_COMMANDS := defined
285 SILENT_IS_ON := defined
288 DEBUG_IS_ON := defined
290 addtolistfile = $(if $(1),@$(call echo,$(1)) >> $(2),)
291 ifdef WIN_SHELL_COMMANDS
294 echo = $(if $(1),echo $(1))
295 touch = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do @(cd %%~pI && type nul >> %%~nxI && copy /by %%~nxI+,, > nul 2>&1 && cd %%cd%%)")
296 cp = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))"$(if $(SILENT_IS_ON), > nul,))
297 cpr = $(if $(1),xcopy /y /i /e$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(call sys_path_list,$(1))) $(call sys_path,$(2))$(if $(SILENT_IS_ON), > nul,))
298 rm = $(if $(1),-del /f$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(call sys_path_list,$(1)))$(if $(SILENT_IS_ON), > nul,)$(if $(DEBUG_IS_ON),, 2>&1))
299 rmr = $(if $(1),-rmdir /s$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,))
300 mkdir = $(if $(1),-mkdir $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,)$(if $(DEBUG_IS_ON),, 2>&1))
301 rmdir = $(if $(1),-rmdir$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,))
302 hs_unsafe_crossloop = ${if $(1),${if $(2),@cmd /c "for %%I in (${call hs_quote_each,$(1)}) do ${call $(2),%%I}",},}
305 nullerror = 2>/dev/null
306 echo = $(if $(1),echo "$(1)")
307 touch = $(if $(1),touch $(1))
308 cp = $(if $(1),cp -df$(if $(SILENT_IS_ON),,v) $(1) $(2))
309 cpr = $(if $(1),cp -dfr$(if $(SILENT_IS_ON),,v) $(1) $(2))
310 rm = $(if $(1),-rm -f$(if $(SILENT_IS_ON),,v) $(1))
311 rmr = $(if $(1),-rm -fr$(if $(SILENT_IS_ON),,v) $(1))
312 mkdir = $(if $(1),-mkdir -p$(if $(SILENT_IS_ON),,v) $(1))
313 rmdir = $(if $(1),-rmdir$(if $(SILENT_IS_ON),, -v) $(1))
314 hs_unsafe_crossloop = ${if $(1),${if $(2),for item in ${call hs_quote_each,$(1)}; do ${call $(2),"$$item"}; done,},}
317 # potential common use variables
318 numbers := 0 1 2 3 4 5 6 7 8 9
320 # potential common use functions
321 reverselist = $(if $(1),$(call reverselist,$(strip $(wordlist 2,$(words $(1)),$(1))))) $(firstword $(1))
322 dirlistfromlocation = $(strip $(subst $(slash),$(space),$(subst $(backslash),$(space),$(1))))
323 spacenumbers = $(subst 0,$(space)0$(space),$(subst 1,$(space)1$(space),$(subst 2,$(space)2$(space),$(subst 3,$(space)3$(space),$(subst 4,$(space)4$(space),$(subst 5,$(space)5$(space),$(subst 6,$(space)6$(space),$(subst 7,$(space)7$(space),$(subst 8,$(space)8$(space),$(subst 9,$(space)9$(space),$(1)))))))))))
324 hasnumbers = $(if $(filter $(numbers),$(call spacenumbers,$(1))),$(1),)
325 isanumber = $(if $(filter-out $(numbers),$(call spacenumbers,$(1))),,$(1))
327 # location version utility functions (lv_*)
328 lv_issimplever = $(if $(call isanumber,$(firstword $(call spacenumbers,$(subst .,,$(1))))),$(1),)
329 lv_isversionver = $(if $(call lv_issimplever,$(1:v%=%)),$(1),$(if $(call lv_issimplever,$(1:ver%=%)),$(1),$(if $(call lv_issimplever,$(1:version%=%)),$(1),)))
330 lv_isreleasever = $(if $(call lv_issimplever,$(1:r%=%)),$(1),$(if $(call lv_issimplever,$(1:rel%=%)),$(1),$(if $(call lv_issimplever,$(1:release%=%)),$(1),)))
331 lv_isbuildver = $(if $(call lv_issimplever,$(1:b%=%)),$(1),$(if $(call lv_issimplever,$(1:bld%=%)),$(1),$(if $(call lv_issimplever,$(1:build%=%)),$(1),)))
332 lv_iscomplexver = $(if $(call lv_isversionver,$(1)),$(1),$(if $(call lv_isreleasever,$(1)),$(1),$(if $(call lv_isbuildver,$(1)),$(1),)))
333 lv_isver = $(if $(call lv_issimplever,$(1)),$(1),$(if $(call lv_iscomplexver,$(1)),$(1),))
334 lv_possibleverorver = $(if $(findstring -,$(1)),$(if $(call hasnumbers,$(1)),$(1),),$(if $(call lv_isver,$(1)),$(1),))
335 lv_termslistfromdir = $(strip $(subst -,$(space),$(1)))
336 lv_verfromtermlist = $(if $(1)$(2),$(if $(1),$(1)$(if $(2),-,),)$(call lv_verfromtermlist,$(firstword $(2)),$(wordlist 2,$(words $(2)),$(2))),)
337 lv_termwalker = $(if $(firstword $(1)),$(if $(call lv_isver,$(firstword $(1))),$(call lv_verfromtermlist,,$(1)),$(call lv_termwalker,$(wordlist 2,$(words $(1)),$(1)))),)
338 lv_version = $(if $(call lv_possibleverorver,$(1)),$(call lv_termwalker,$(call lv_termslistfromdir,$(1))),)
339 lv_dirwalker = $(if $(firstword $(1)),$(if $(call lv_version,$(firstword $(1))),$(call lv_version,$(firstword $(1))),$(call lv_dirwalker,$(wordlist 2,$(words $(1)),$(1)))),)
340 locationversion = $(call shwspace,$(call lv_dirwalker,$(call reverselist,$(subst $(space)$(space),$(space),$(call dirlistfromlocation,$(call hidspace,$(1)))))))
342 # SOURCE CODE REPOSITORY VERSION
343 ifndef REPOSITORY_VER
344 # TODO: support other VCS
345 ifndef GIT_REPOSITORY
349 ifeq ($(shell $(GIT) --version $(nullerror)),)
350 export GIT_NA := $(GIT)NotAvailable
352 ifneq ($(shell $(GIT) log -n 1 --format="%%%%" $(nullerror)),)
353 export GIT_REPOSITORY := yes
354 export REPOSITORY_VER := $(shell $(GIT) describe --tags --dirty=" (dirty)" --always)
358 ifndef REPOSITORY_VER
359 DIR_VER := $(call locationversion,$(CURDIR))
361 export REPOSITORY_VER := $(DIR_VER)
364 ifndef REPOSITORY_VER
365 export REPOSITORY_VER := unknown
370 ECSLIBOPT := $(if $(STATIC_LIBRARY_TARGET),-staticlib,$(if $(SHARED_LIBRARY_TARGET),-dynamiclib,))
371 FVISIBILITY := $(if $(WINDOWS_TARGET),,-fvisibility=hidden)
372 FPIC := $(if $(WINDOWS_TARGET),,-fPIC)
373 EXECUTABLE := $(if $(WINDOWS_TARGET),$(if $(EXECUTABLE_TARGET),$(CONSOLE),),)
374 INSTALLNAME := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-install_name $(LP)$(MODULE)$(SO),),)
377 SHAREDLIB := $(if $(SHARED_LIBRARY_TARGET),$(if $(OSX_TARGET),-dynamiclib -single_module -multiply_defined suppress,-shared),)
379 STRIPOPT := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-x, -u -r), -x --strip-unneeded --remove-section=.comment --remove-section=.note)
380 HOST_SODESTDIR := $(if $(WINDOWS_HOST),obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
381 SODESTDIR := $(if $(WINDOWS_TARGET),obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
384 _L = $(if $(filter $(1),$(EXCLUDED_LIBS)),,-l$(1))
388 CFLAGS += $(CPPFLAGS)
395 # COMMON LIBRARIES DETECTION
398 _OPENSSL_CONF = $(call hidspace,$(call slash_path,$(OPENSSL_CONF)))
399 OPENSSL_INCLUDE_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/include,$(_OPENSSL_CONF)))
400 OPENSSL_LIB_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/lib,$(_OPENSSL_CONF)))
401 OPENSSL_BIN_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/bin,$(_OPENSSL_CONF)))
403 # to avoid issues with empty -L/-I
404 OPENSSL_INCLUDE_DIR = .