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
219 WIN_PS_TOOLS := defined
222 slash_path = $(subst $(backslash),$(slash),$(1))
225 sys_path = $(subst $(backslash)$(backslash),$(slash),$(subst $(slash),$(backslash),$(1)))
226 quote_path = "$(call sys_path,$(call unescp_all,$(1)))"
227 each_path_quote = $(if $(findstring $(esc),$(path)),"$(call unescp_all,$(call shwspace,$(path)))",$(call unescp_all,$(path)))
228 sys_path_list = $(foreach path,$(1),$(each_path_quote))
235 # PREFIXES AND EXTENSIONS
245 E := $(if $(WINDOWS_TARGET),.exe,)
246 SO := $(if $(WINDOWS_TARGET),.dll,$(if $(OSX_TARGET),.dylib,.so))
247 LP := $(if $(WINDOWS_TARGET),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
248 HOST_E := $(if $(WINDOWS_HOST),.exe,)
249 HOST_SO := $(if $(WINDOWS_HOST),.dll,$(if $(OSX_HOST),.dylib,.so))
250 HOST_LP := $(if $(WINDOWS_HOST),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
251 .SUFFIXES: .c .ec .sym .imp .bowl $(O) $(A)
254 VER := $(if $(LINUX_TARGET),$(if $(LINUX_HOST),$(if $(VERSION),.$(VERSION),),),)
258 CCACHE_COMPILE := ccache$(space)
260 DISTCC_COMPILE := distcc$(space)
264 DISTCC_COMPILE := distcc$(space)
268 _CPP = $(if $(findstring $(space),$(CPP)),"$(CPP)",$(CPP))
270 _SYSROOT = $(if $(SYSROOT),$(space)--sysroot=$(SYSROOT),)
275 WIN_SHELL_COMMANDS := defined
279 SILENT_IS_ON := defined
282 DEBUG_IS_ON := defined
284 ifdef WIN_SHELL_COMMANDS
287 echo = $(if $(1),echo $(1))
288 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%%)")
289 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,))
290 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,))
291 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))
292 rmr = $(if $(1),-rmdir /s$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,))
293 mkdir = $(if $(1),-mkdir $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,)$(if $(DEBUG_IS_ON),, 2>&1))
294 rmdir = $(if $(1),-rmdir$(if $(SILENT_IS_ON), /q,) $(call sys_path,$(1))$(if $(SILENT_IS_ON), > nul,))
295 hs_unsafe_crossloop = ${if $(1),${if $(2),@cmd /c "for %%I in (${call hs_quote_each,$(1)}) do ${call $(2),%%I}",},}
298 nullerror = 2>/dev/null
299 echo = $(if $(1),echo "$(1)")
300 touch = $(if $(1),touch $(1))
301 cp = $(if $(1),cp$(if $(SILENT_IS_ON),, -v) $(1) $(2))
302 cpr = $(if $(1),cp -prf$(if $(SILENT_IS_ON),,v) $(1) $(2))
303 rm = $(if $(1),-rm -f$(if $(SILENT_IS_ON),,v) $(1))
304 rmr = $(if $(1),-rm -fr$(if $(SILENT_IS_ON),,v) $(1))
305 mkdir = $(if $(1),-mkdir -p$(if $(SILENT_IS_ON),,v) $(1))
306 rmdir = $(if $(1),-rmdir$(if $(SILENT_IS_ON),, -v) $(1))
307 hs_unsafe_crossloop = ${if $(1),${if $(2),for item in ${call hs_quote_each,$(1)}; do ${call $(2),"$$item"}; done,},}
310 # potential common use variables
311 numbers := 0 1 2 3 4 5 6 7 8 9
313 # potential common use functions
314 reverselist = $(if $(1),$(call reverselist,$(strip $(wordlist 2,$(words $(1)),$(1))))) $(firstword $(1))
315 dirlistfromlocation = $(strip $(subst $(slash),$(space),$(subst $(backslash),$(space),$(1))))
316 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)))))))))))
317 hasnumbers = $(if $(filter $(numbers),$(call spacenumbers,$(1))),$(1),)
318 isanumber = $(if $(filter-out $(numbers),$(call spacenumbers,$(1))),,$(1))
320 # location version utility functions (lv_*)
321 lv_issimplever = $(if $(call isanumber,$(firstword $(call spacenumbers,$(subst .,,$(1))))),$(1),)
322 lv_isversionver = $(if $(call lv_issimplever,$(1:v%=%)),$(1),$(if $(call lv_issimplever,$(1:ver%=%)),$(1),$(if $(call lv_issimplever,$(1:version%=%)),$(1),)))
323 lv_isreleasever = $(if $(call lv_issimplever,$(1:r%=%)),$(1),$(if $(call lv_issimplever,$(1:rel%=%)),$(1),$(if $(call lv_issimplever,$(1:release%=%)),$(1),)))
324 lv_isbuildver = $(if $(call lv_issimplever,$(1:b%=%)),$(1),$(if $(call lv_issimplever,$(1:bld%=%)),$(1),$(if $(call lv_issimplever,$(1:build%=%)),$(1),)))
325 lv_iscomplexver = $(if $(call lv_isversionver,$(1)),$(1),$(if $(call lv_isreleasever,$(1)),$(1),$(if $(call lv_isbuildver,$(1)),$(1),)))
326 lv_isver = $(if $(call lv_issimplever,$(1)),$(1),$(if $(call lv_iscomplexver,$(1)),$(1),))
327 lv_possibleverorver = $(if $(findstring -,$(1)),$(if $(call hasnumbers,$(1)),$(1),),$(if $(call lv_isver,$(1)),$(1),))
328 lv_termslistfromdir = $(strip $(subst -,$(space),$(1)))
329 lv_verfromtermlist = $(if $(1)$(2),$(if $(1),$(1)$(if $(2),-,),)$(call lv_verfromtermlist,$(firstword $(2)),$(wordlist 2,$(words $(2)),$(2))),)
330 lv_termwalker = $(if $(firstword $(1)),$(if $(call lv_isver,$(firstword $(1))),$(call lv_verfromtermlist,,$(1)),$(call lv_termwalker,$(wordlist 2,$(words $(1)),$(1)))),)
331 lv_version = $(if $(call lv_possibleverorver,$(1)),$(call lv_termwalker,$(call lv_termslistfromdir,$(1))),)
332 lv_dirwalker = $(if $(firstword $(1)),$(if $(call lv_version,$(firstword $(1))),$(call lv_version,$(firstword $(1))),$(call lv_dirwalker,$(wordlist 2,$(words $(1)),$(1)))),)
333 locationversion = $(call shwspace,$(call lv_dirwalker,$(call reverselist,$(subst $(space)$(space),$(space),$(call dirlistfromlocation,$(call hidspace,$(1)))))))
335 # SOURCE CODE REPOSITORY VERSION
336 ifndef REPOSITORY_VER
337 # TODO: support other VCS
338 ifndef GIT_REPOSITORY
342 ifeq ($(shell $(GIT) --version $(nullerror)),)
343 export GIT_NA := $(GIT)NotAvailable
345 ifneq ($(shell $(GIT) log -n 1 --format="%%%%" $(nullerror)),)
346 export GIT_REPOSITORY := yes
347 export REPOSITORY_VER := $(shell $(GIT) describe --tags --dirty=" (dirty)" --always)
351 ifndef REPOSITORY_VER
352 DIR_VER := $(call locationversion,$(CURDIR))
354 export REPOSITORY_VER := $(DIR_VER)
357 ifndef REPOSITORY_VER
358 export REPOSITORY_VER := unknown
363 ECSLIBOPT := $(if $(STATIC_LIBRARY_TARGET),-staticlib,$(if $(SHARED_LIBRARY_TARGET),-dynamiclib,))
364 FVISIBILITY := $(if $(WINDOWS_TARGET),,-fvisibility=hidden)
365 FPIC := $(if $(WINDOWS_TARGET),,-fPIC)
366 EXECUTABLE := $(if $(WINDOWS_TARGET),$(if $(EXECUTABLE_TARGET),$(CONSOLE),),)
367 INSTALLNAME := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-install_name $(LP)$(MODULE)$(SO),),)
370 SHAREDLIB := $(if $(SHARED_LIBRARY_TARGET),$(if $(OSX_TARGET),-dynamiclib -single_module -multiply_defined suppress,-shared),)
372 STRIPOPT := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-x, -u -r), -x --strip-unneeded --remove-section=.comment --remove-section=.note)
373 HOST_SODESTDIR := $(if $(WINDOWS_HOST),obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
374 SODESTDIR := $(if $(WINDOWS_TARGET),obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
377 _L = $(if $(filter $(1),$(EXCLUDED_LIBS)),,-l$(1))
381 CFLAGS += $(CPPFLAGS)
388 # COMMON LIBRARIES DETECTION
391 _OPENSSL_CONF = $(call hidspace,$(call slash_path,$(OPENSSL_CONF)))
392 OPENSSL_INCLUDE_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/include,$(_OPENSSL_CONF)))
393 OPENSSL_LIB_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/lib,$(_OPENSSL_CONF)))
394 OPENSSL_BIN_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/bin,$(_OPENSSL_CONF)))
396 # to avoid issues with empty -L/-I
397 OPENSSL_INCLUDE_DIR = .