ide: tweak compiler config support for specifying output file extensions.
[sdk] / crossplatform.mk
1 # HOST PLATFORM DETECTION
2 ifeq ($(OS),Windows_NT)
3    HOST_PLATFORM := win32
4    WINDOWS_HOST := defined
5    # IA64 / AMD64 / x86   #HKEY_LOCAL_MACHINE
6    #_PROCESSOR_ARCHITECTURE := $(shell reg Query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE)
7    #ifneq ($(findstring AMD64,$(_PROCESSOR_ARCHITECTURE)),)
8    #   HOST_ARCH := amd64
9    #   HOST_BITS := 64
10    #else
11    #   ifneq ($(findstring IA64,$(_PROCESSOR_ARCHITECTURE)),)
12    #      HOST_ARCH := ia64
13    #      HOST_BITS := 64
14    #   else
15    #      ifneq ($(findstring x86,$(_PROCESSOR_ARCHITECTURE)),)
16    #         HOST_ARCH := x86
17    #         HOST_BITS := 32
18    #      else
19    #         HOST_ARCH := unknown
20    #         HOST_BITS := unknown
21    #      endif
22    #   endif
23    #endif
24    #ifdef ProgramFiles(x86)
25    #   HOST_BITS := 64
26    #else
27    #   HOST_BITS := 32
28    #endif
29 else
30  _UNAME := $(shell uname)
31  UNAME_P := $(shell uname -p)
32  ifeq ($(_UNAME),FreeBSD)
33  # Using Linux platform for Unix OSes for now 
34  #   HOST_PLATFORM := bsd
35     BSD_HOST := defined
36     HOST_PLATFORM := linux
37     LINUX_HOST := defined
38  else
39   ifeq ($(_UNAME),Darwin)
40      HOST_PLATFORM := apple
41      OSX_HOST := defined
42   else
43      HOST_PLATFORM := linux
44      LINUX_HOST := defined
45   endif
46  endif
47  HOST_ARCH := $(UNAME_P)
48 endif
49
50 # TARGET_PLATFORM
51 ifndef TARGET_PLATFORM
52 ifdef PLATFORM
53    TARGET_PLATFORM := $(PLATFORM)
54 endif
55 endif
56 ifndef TARGET_PLATFORM
57 ifdef WINDOWS_HOST
58    TARGET_PLATFORM := win32
59 else
60 ifdef OSX_HOST
61    TARGET_PLATFORM := apple
62 else
63 #ifdef BSD_HOST
64 #   TARGET_PLATFORM := bsd
65 #else
66    TARGET_PLATFORM := linux
67 ifdef BSD_HOST
68    BSD_TARGET := defined
69 endif
70 #endif
71 endif
72 endif
73 endif
74 ifndef PLATFORM
75    PLATFORM := $(TARGET_PLATFORM)
76 endif
77 ifeq ($(TARGET_PLATFORM),win32)
78    WINDOWS_TARGET := defined
79 else
80 ifeq ($(TARGET_PLATFORM),apple)
81    OSX_TARGET := defined
82 else
83 ifeq ($(TARGET_PLATFORM),emscripten)
84    EMSCRIPTEN_TARGET := defined
85 else
86 ifeq ($(TARGET_PLATFORM),pnacl)
87    PNACL_TARGET := defined
88 else
89 #ifeq ($(TARGET_PLATFORM),bsd)
90 #   BSD_TARGET := defined
91 #else
92    LINUX_TARGET := defined
93 #endif
94 endif
95 endif
96 endif
97 endif
98
99 # CROSS_TARGET
100 ifneq ($(TARGET_PLATFORM),$(HOST_PLATFORM))
101    CROSS_TARGET := defined
102 endif
103
104 # TARGET_TYPE
105 ifeq ($(TARGET_TYPE),staticlib)
106    STATIC_LIBRARY_TARGET := defined
107 else
108 ifeq ($(TARGET_TYPE),sharedlib)
109    SHARED_LIBRARY_TARGET := defined
110 else
111 ifeq ($(TARGET_TYPE),executable)
112    EXECUTABLE_TARGET := defined
113 endif
114 endif
115 endif
116
117 ifeq ($(GCC_PREFIX),i586-mingw32msvc-)
118 export ARCH
119 ARCH := x32
120 endif
121
122 ifeq ($(GCC_PREFIX),i686-w64-mingw32-)
123 export ARCH
124 ARCH := x32
125 endif
126
127 # Accept different things for ARCH but standardize on x32/x64
128 # This will be used for object directories
129 ifdef ARCH
130  ifeq ($(ARCH),32)
131   override ARCH := x32
132  endif
133  ifeq ($(ARCH),x86)
134   override ARCH := x32
135  endif
136  ifeq ($(ARCH),i386)
137   override ARCH := x32
138  endif
139  ifeq ($(ARCH),i686)
140   override ARCH := x32
141  endif
142  ifeq ($(ARCH),64)
143   override ARCH := x64
144  endif
145  ifeq ($(ARCH),amd64)
146   override ARCH := x64
147  endif
148  ifeq ($(ARCH),x86_64)
149   override ARCH := x64
150  endif
151
152  # Set ARCH_FLAGS only if ARCH is set
153  ifeq ($(ARCH),x64)
154   TARGET_ARCH := x86_64
155   ARCH_FLAGS := -m64
156  endif
157  ifeq ($(ARCH),x32)
158   TARGET_ARCH := i386
159   ARCH_FLAGS := -m32
160  endif
161
162  ARCH_SUFFIX := .$(ARCH)
163
164  ifdef LINUX_TARGET
165   TARGET_ARCH := $(TARGET_ARCH)-linux-gnu
166  endif
167
168 endif
169
170 # On Windows/32 bit systems, pass -m32 as TDM-GCC packaged with the installer produces 64 bit executables by default
171 # Disable this if your compiler does not accept -m32
172 ifndef ARCH
173  ifeq ($(HOST_PLATFORM),win32)
174   ifeq ($(TARGET_PLATFORM),win32)
175    #ifeq ($(HOST_BITS),32)
176    ifndef ProgramFiles(x86)
177     ARCH := x32
178     TARGET_ARCH := i386
179     ARCH_FLAGS := -m32
180    endif
181   endif
182  endif
183 endif
184
185 # DEBUG SUFFIX
186 ifdef DEBUG
187 DEBUG_SUFFIX := .debug
188 endif
189
190 # COMPILER SUFFIX
191 COMPILER_SUFFIX = $(ARCH_SUFFIX)
192 ifdef COMPILER
193 ifneq ($(COMPILER),default)
194 COMPILER_SUFFIX = .$(COMPILER)$(ARCH_SUFFIX)
195 endif
196 endif
197
198 # MISC STRING TOOLS
199 empty :=
200 esc := $(empty)\7f$(empty)
201 space := $(empty) $(empty)
202 comma := ,
203 openpar := (
204 clospar := )
205 slash := $(empty)/$(empty)
206 backslash := $(empty)\$(empty)
207 escspace = $(subst $(space),$(backslash)$(space),$(subst $(backslash)$(space),$(space),$(1)))
208 hidspace = $(subst $(space),$(esc),$(subst $(backslash)$(space),$(esc),$(1)))
209 hidescspace = $(subst $(backslash)$(space),$(esc),$(1))
210 shwspace = $(subst $(esc),$(backslash)$(space),$(1))
211 shwspacequoted = $(subst $(esc),"$(space)",$(subst \$(space),$(esc),$(1)))
212 unescp_all = $(subst $(esc),$(backslash),$(subst $(backslash),,$(subst $(backslash)$(backslash),$(esc),$(1))))
213 for_list = $(call sys_path,$(call unescp_all,$(call shwspacequoted,$(subst $(space),$(comma),$(subst $(space)$(space),$(space),$(call hidescspace,$(1)))))))
214 lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
215
216 # PATH SEPARATOR STRING TOOLS
217 ifdef WINDOWS_HOST
218 ifndef MSYSCON
219    WIN_PS_TOOLS := defined
220 endif
221 endif
222 ifdef WIN_PS_TOOLS
223    psep := $(backslash)
224    slash_path = $(subst $(backslash),$(slash),$(1))
225    esc_ampersand = $(subst &,^&,$(subst ^&,&,$(1)))
226    sys_path = $(call esc_ampersand,$(subst $(backslash)$(backslash),$(slash),$(subst $(slash),$(backslash),$(1))))
227    each_quote_path = $(call quote_path,$(path))
228    quote_path = "$(call sys_path,$(call unescp_all,$(1)))"
229 #   each_path_quote = $(if $(findstring $(esc),$(path)),"$(call sys_path,$(call unescp_all,$(call shwspace,$(path))))",$(call sys_path,$(call unescp_all,$(path))))
230    each_path_quote = $(if $(findstring $(esc),$(path)),"$(call unescp_all,$(call shwspace,$(path)))",$(call unescp_all,$(path)))
231 #   sys_path_list
232    sys_path_list = $(foreach path,$(1),$(each_path_quote))
233 else
234    psep := $(slash)
235    slash_path = $(1)
236    sys_path = $(1)
237    quote_path = $(1)
238 endif
239
240 # TARGET VERSION
241 VER := $(if $(LINUX_TARGET),$(if $(LINUX_HOST),$(if $(VERSION),.$(VERSION),),),)
242
243 # SUPER TOOLS
244 ifdef CCACHE
245    CCACHE_COMPILE := ccache$(space)
246 ifdef DISTCC
247    DISTCC_COMPILE := distcc$(space)
248 endif
249 else
250 ifdef DISTCC
251    DISTCC_COMPILE := distcc$(space)
252 endif
253 endif
254
255 _CPP = $(if $(findstring $(space),$(CPP)),"$(CPP)",$(CPP))
256
257 _SYSROOT = $(if $(SYSROOT),$(space)--sysroot=$(SYSROOT),)
258
259 # SHELL COMMANDS
260 ifdef WINDOWS_HOST
261 ifndef MSYSCON
262    WIN_SHELL_COMMANDS := defined
263 endif
264 endif
265 ifdef WIN_SHELL_COMMANDS
266    nullerror = 2>NUL
267    echo = $(if $(1),echo $(1),echo.)
268    cat = $(if $(1),type $(1))
269    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%%)")
270 #   cpq = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))" > nul 2>&1)
271    cpq = $(if $(1),@cmd /c "for %%I in ($(call for_list,$(1))) do copy /by %%I $(call sys_path,$(2))" > nul 2>&1)
272 #   cpv = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))")
273    cpv = $(if $(1),@cmd /c "for %%I in ($(call for_list,$(1))) do copy /by %%I $(call sys_path,$(2))")
274 #   rmq = $(if $(1),-del /f /q $(1) > nul 2>&1)
275 #   rmq = $(if $(1),-del /f /q $(call sys_path,$(1)) > nul 2>&1)
276    rmq = $(if $(1),-del /f /q $(call sys_path,$(call sys_path_list,$(1))) > nul 2>&1)
277 #   rmq = $(if $(1),-del /f /q $(foreach path,$(1),$(each_quote_path)) $(call sys_path,$(1)) > nul 2>&1)
278    rmrq = $(if $(1),-rmdir /q /s $(call sys_path,$(1)) > nul 2>&1)
279    mkdirq = $(if $(1),-mkdir $(call sys_path,$(1)) > nul 2>&1)
280    rmdirq = $(if $(1),-rmdir /q $(call sys_path,$(1)) > nul 2>&1)
281 else
282    nullerror = 2>/dev/null
283    echo = $(if $(1),echo "$(1)",echo)
284    cat = $(if $(1),cat $(1))
285    touch = $(if $(1),touch $(1))
286    cpq = $(if $(1),cp $(1) $(2))
287    cpv = cp $(1) $(2)
288    rmq = $(if $(1),-rm -f $(1))
289    rmrq = $(if $(1),-rm -f -r $(1))
290    mkdirq = $(if $(1),-mkdir -p $(1))
291    rmdirq = $(if $(1),-rmdir $(1))
292 endif
293
294 # potential common use variables
295 numbers := 0 1 2 3 4 5 6 7 8 9
296
297 # potential common use functions
298 reverselist = $(if $(1),$(call reverselist,$(strip $(wordlist 2,$(words $(1)),$(1))))) $(firstword $(1))
299 dirlistfromlocation = $(strip $(subst $(slash),$(space),$(subst $(backslash),$(space),$(1))))
300 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)))))))))))
301 hasnumbers = $(if $(filter $(numbers),$(call spacenumbers,$(1))),$(1),)
302 isanumber = $(if $(filter-out $(numbers),$(call spacenumbers,$(1))),,$(1))
303
304 # location version utility functions (lv_*)
305 lv_issimplever = $(if $(call isanumber,$(firstword $(call spacenumbers,$(subst .,,$(1))))),$(1),)
306 lv_isversionver = $(if $(call lv_issimplever,$(1:v%=%)),$(1),$(if $(call lv_issimplever,$(1:ver%=%)),$(1),$(if $(call lv_issimplever,$(1:version%=%)),$(1),)))
307 lv_isreleasever = $(if $(call lv_issimplever,$(1:r%=%)),$(1),$(if $(call lv_issimplever,$(1:rel%=%)),$(1),$(if $(call lv_issimplever,$(1:release%=%)),$(1),)))
308 lv_isbuildver = $(if $(call lv_issimplever,$(1:b%=%)),$(1),$(if $(call lv_issimplever,$(1:bld%=%)),$(1),$(if $(call lv_issimplever,$(1:build%=%)),$(1),)))
309 lv_iscomplexver = $(if $(call lv_isversionver,$(1)),$(1),$(if $(call lv_isreleasever,$(1)),$(1),$(if $(call lv_isbuildver,$(1)),$(1),)))
310 lv_isver = $(if $(call lv_issimplever,$(1)),$(1),$(if $(call lv_iscomplexver,$(1)),$(1),))
311 lv_possibleverorver = $(if $(findstring -,$(1)),$(if $(call hasnumbers,$(1)),$(1),),$(if $(call lv_isver,$(1)),$(1),))
312 lv_termslistfromdir = $(strip $(subst -,$(space),$(1)))
313 lv_verfromtermlist = $(if $(1)$(2),$(if $(1),$(1)$(if $(2),-,),)$(call lv_verfromtermlist,$(firstword $(2)),$(wordlist 2,$(words $(2)),$(2))),)
314 lv_termwalker = $(if $(firstword $(1)),$(if $(call lv_isver,$(firstword $(1))),$(call lv_verfromtermlist,,$(1)),$(call lv_termwalker,$(wordlist 2,$(words $(1)),$(1)))),)
315 lv_version = $(if $(call lv_possibleverorver,$(1)),$(call lv_termwalker,$(call lv_termslistfromdir,$(1))),)
316 lv_dirwalker = $(if $(firstword $(1)),$(if $(call lv_version,$(firstword $(1))),$(call lv_version,$(firstword $(1))),$(call lv_dirwalker,$(wordlist 2,$(words $(1)),$(1)))),)
317 locationversion = $(call shwspace,$(call lv_dirwalker,$(call reverselist,$(subst $(space)$(space),$(space),$(call dirlistfromlocation,$(call hidspace,$(1)))))))
318
319 # SOURCE CODE REPOSITORY VERSION
320 ifndef REPOSITORY_VER
321    # TODO: support other VCS
322    ifndef GIT_REPOSITORY
323       ifndef GIT
324          GIT := git
325       endif
326       ifeq ($(shell $(GIT) --version $(nullerror)),)
327          export GIT_NA := $(GIT)NotAvailable
328       else
329          ifneq ($(shell $(GIT) log -n 1 --format="%%%%" $(nullerror)),)
330             export GIT_REPOSITORY := yes
331             export REPOSITORY_VER := $(shell $(GIT) describe --tags --dirty=" (dirty)" --always)
332          endif
333       endif
334    endif
335    ifndef REPOSITORY_VER
336       DIR_VER := $(call locationversion,$(CURDIR))
337       ifneq ($(DIR_VER),)
338          export REPOSITORY_VER := $(DIR_VER)
339       endif
340    endif
341    ifndef REPOSITORY_VER
342       export REPOSITORY_VER := unknown
343    endif
344 endif
345
346 # COMPILER OPTIONS
347 ECSLIBOPT := $(if $(STATIC_LIBRARY_TARGET),-staticlib,$(if $(SHARED_LIBRARY_TARGET),-dynamiclib,))
348 FVISIBILITY := $(if $(WINDOWS_TARGET),,-fvisibility=hidden)
349 FPIC := $(if $(WINDOWS_TARGET),,-fPIC)
350 EXECUTABLE := $(if $(WINDOWS_TARGET),$(if $(EXECUTABLE_TARGET),$(CONSOLE),),)
351 INSTALLNAME := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-install_name $(LP)$(MODULE)$(SO),),)
352
353 # LINKER OPTIONS
354 SHAREDLIB := $(if $(SHARED_LIBRARY_TARGET),$(if $(OSX_TARGET),-dynamiclib -single_module -multiply_defined suppress,-shared),)
355 LINKOPT :=
356 STRIPOPT := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-x, -u -r), -x --strip-unneeded --remove-section=.comment --remove-section=.note)
357 HOST_SODESTDIR := $(if $(WINDOWS_HOST),obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
358 SODESTDIR := $(if $(WINDOWS_TARGET),obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
359
360 # EXCLUDED_LIBS TOOL
361 _L = $(if $(filter $(1),$(EXCLUDED_LIBS)),,-l$(1))
362
363 # DEBIAN
364 ifdef DEBIAN_PACKAGE
365 CFLAGS += $(CPPFLAGS)
366 endif
367
368 ifdef DEBUG
369 CFLAGS += -D_DEBUG
370 endif
371
372 # COMMON LIBRARIES DETECTION
373 ifdef WINDOWS_TARGET
374  ifdef OPENSSL_CONF
375   _OPENSSL_CONF = $(call hidspace,$(call slash_path,$(OPENSSL_CONF)))
376   OPENSSL_INCLUDE_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/include,$(_OPENSSL_CONF)))
377   OPENSSL_LIB_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/lib,$(_OPENSSL_CONF)))
378   OPENSSL_BIN_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/bin,$(_OPENSSL_CONF)))
379  else
380 # to avoid issues with empty -L/-I
381   OPENSSL_INCLUDE_DIR = .
382   OPENSSL_LIB_DIR = .
383   OPENSSL_BIN_DIR = .
384  endif
385 endif