playing with em/pnacl platforms
[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 # PREFIXES AND EXTENSIONS
241 EC := .ec
242 S := .sym
243 I := .imp
244 B := .bowl
245 C := .c
246 ifndef O
247 O := .o
248 endif
249 A := .a
250 E := $(if $(WINDOWS_TARGET),.exe,)
251 SO := $(if $(WINDOWS_TARGET),.dll,$(if $(OSX_TARGET),.dylib,.so))
252 LP := $(if $(WINDOWS_TARGET),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
253 HOST_E := $(if $(WINDOWS_HOST),.exe,)
254 HOST_SO := $(if $(WINDOWS_HOST),.dll,$(if $(OSX_HOST),.dylib,.so))
255 HOST_LP := $(if $(WINDOWS_HOST),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
256 .SUFFIXES: .c .ec .sym .imp .bowl $(O) $(A)
257
258 # TARGET VERSION
259 VER := $(if $(LINUX_TARGET),$(if $(LINUX_HOST),$(if $(VERSION),.$(VERSION),),),)
260
261 # SUPER TOOLS
262 ifdef CCACHE
263    CCACHE_COMPILE := ccache$(space)
264 ifdef DISTCC
265    DISTCC_COMPILE := distcc$(space)
266 endif
267 else
268 ifdef DISTCC
269    DISTCC_COMPILE := distcc$(space)
270 endif
271 endif
272
273 _CPP = $(if $(findstring $(space),$(CPP)),"$(CPP)",$(CPP))
274
275 _SYSROOT = $(if $(SYSROOT),$(space)--sysroot=$(SYSROOT),)
276
277 # SHELL COMMANDS
278 ifdef WINDOWS_HOST
279 ifndef MSYSCON
280    WIN_SHELL_COMMANDS := defined
281 endif
282 endif
283 ifdef WIN_SHELL_COMMANDS
284    nullerror = 2>NUL
285    echo = $(if $(1),echo $(1),echo.)
286    cat = $(if $(1),type $(1))
287    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%%)")
288 #   cpq = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))" > nul 2>&1)
289    cpq = $(if $(1),@cmd /c "for %%I in ($(call for_list,$(1))) do copy /by %%I $(call sys_path,$(2))" > nul 2>&1)
290 #   cpv = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))")
291    cpv = $(if $(1),@cmd /c "for %%I in ($(call for_list,$(1))) do copy /by %%I $(call sys_path,$(2))")
292 #   rmq = $(if $(1),-del /f /q $(1) > nul 2>&1)
293 #   rmq = $(if $(1),-del /f /q $(call sys_path,$(1)) > nul 2>&1)
294    rmq = $(if $(1),-del /f /q $(call sys_path,$(call sys_path_list,$(1))) > nul 2>&1)
295 #   rmq = $(if $(1),-del /f /q $(foreach path,$(1),$(each_quote_path)) $(call sys_path,$(1)) > nul 2>&1)
296    rmrq = $(if $(1),-rmdir /q /s $(call sys_path,$(1)) > nul 2>&1)
297    mkdirq = $(if $(1),-mkdir $(call sys_path,$(1)) > nul 2>&1)
298    rmdirq = $(if $(1),-rmdir /q $(call sys_path,$(1)) > nul 2>&1)
299 else
300    nullerror = 2>/dev/null
301    echo = $(if $(1),echo "$(1)",echo)
302    cat = $(if $(1),cat $(1))
303    touch = $(if $(1),touch $(1))
304    cpq = $(if $(1),cp $(1) $(2))
305    cpv = cp $(1) $(2)
306    rmq = $(if $(1),-rm -f $(1))
307    rmrq = $(if $(1),-rm -f -r $(1))
308    mkdirq = $(if $(1),-mkdir -p $(1))
309    rmdirq = $(if $(1),-rmdir $(1))
310 endif
311
312 # potential common use variables
313 numbers := 0 1 2 3 4 5 6 7 8 9
314
315 # potential common use functions
316 reverselist = $(if $(1),$(call reverselist,$(strip $(wordlist 2,$(words $(1)),$(1))))) $(firstword $(1))
317 dirlistfromlocation = $(strip $(subst $(slash),$(space),$(subst $(backslash),$(space),$(1))))
318 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)))))))))))
319 hasnumbers = $(if $(filter $(numbers),$(call spacenumbers,$(1))),$(1),)
320 isanumber = $(if $(filter-out $(numbers),$(call spacenumbers,$(1))),,$(1))
321
322 # location version utility functions (lv_*)
323 lv_issimplever = $(if $(call isanumber,$(firstword $(call spacenumbers,$(subst .,,$(1))))),$(1),)
324 lv_isversionver = $(if $(call lv_issimplever,$(1:v%=%)),$(1),$(if $(call lv_issimplever,$(1:ver%=%)),$(1),$(if $(call lv_issimplever,$(1:version%=%)),$(1),)))
325 lv_isreleasever = $(if $(call lv_issimplever,$(1:r%=%)),$(1),$(if $(call lv_issimplever,$(1:rel%=%)),$(1),$(if $(call lv_issimplever,$(1:release%=%)),$(1),)))
326 lv_isbuildver = $(if $(call lv_issimplever,$(1:b%=%)),$(1),$(if $(call lv_issimplever,$(1:bld%=%)),$(1),$(if $(call lv_issimplever,$(1:build%=%)),$(1),)))
327 lv_iscomplexver = $(if $(call lv_isversionver,$(1)),$(1),$(if $(call lv_isreleasever,$(1)),$(1),$(if $(call lv_isbuildver,$(1)),$(1),)))
328 lv_isver = $(if $(call lv_issimplever,$(1)),$(1),$(if $(call lv_iscomplexver,$(1)),$(1),))
329 lv_possibleverorver = $(if $(findstring -,$(1)),$(if $(call hasnumbers,$(1)),$(1),),$(if $(call lv_isver,$(1)),$(1),))
330 lv_termslistfromdir = $(strip $(subst -,$(space),$(1)))
331 lv_verfromtermlist = $(if $(1)$(2),$(if $(1),$(1)$(if $(2),-,),)$(call lv_verfromtermlist,$(firstword $(2)),$(wordlist 2,$(words $(2)),$(2))),)
332 lv_termwalker = $(if $(firstword $(1)),$(if $(call lv_isver,$(firstword $(1))),$(call lv_verfromtermlist,,$(1)),$(call lv_termwalker,$(wordlist 2,$(words $(1)),$(1)))),)
333 lv_version = $(if $(call lv_possibleverorver,$(1)),$(call lv_termwalker,$(call lv_termslistfromdir,$(1))),)
334 lv_dirwalker = $(if $(firstword $(1)),$(if $(call lv_version,$(firstword $(1))),$(call lv_version,$(firstword $(1))),$(call lv_dirwalker,$(wordlist 2,$(words $(1)),$(1)))),)
335 locationversion = $(call shwspace,$(call lv_dirwalker,$(call reverselist,$(subst $(space)$(space),$(space),$(call dirlistfromlocation,$(call hidspace,$(1)))))))
336
337 # SOURCE CODE REPOSITORY VERSION
338 ifndef REPOSITORY_VER
339    # TODO: support other VCS
340    ifndef GIT_REPOSITORY
341       ifndef GIT
342          GIT := git
343       endif
344       ifeq ($(shell $(GIT) --version $(nullerror)),)
345          export GIT_NA := $(GIT)NotAvailable
346       else
347          ifneq ($(shell $(GIT) log -n 1 --format="%%%%" $(nullerror)),)
348             export GIT_REPOSITORY := yes
349             export REPOSITORY_VER := $(shell $(GIT) describe --tags --dirty=" (dirty)" --always)
350          endif
351       endif
352    endif
353    ifndef REPOSITORY_VER
354       DIR_VER := $(call locationversion,$(CURDIR))
355       ifneq ($(DIR_VER),)
356          export REPOSITORY_VER := $(DIR_VER)
357       endif
358    endif
359    ifndef REPOSITORY_VER
360       export REPOSITORY_VER := unknown
361    endif
362 endif
363
364 # COMPILER OPTIONS
365 ECSLIBOPT := $(if $(STATIC_LIBRARY_TARGET),-staticlib,$(if $(SHARED_LIBRARY_TARGET),-dynamiclib,))
366 FVISIBILITY := $(if $(WINDOWS_TARGET),,-fvisibility=hidden)
367 FPIC := $(if $(WINDOWS_TARGET),,-fPIC)
368 EXECUTABLE := $(if $(WINDOWS_TARGET),$(if $(EXECUTABLE_TARGET),$(CONSOLE),),)
369 INSTALLNAME := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-install_name $(LP)$(MODULE)$(SO),),)
370
371 # LINKER OPTIONS
372 SHAREDLIB := $(if $(SHARED_LIBRARY_TARGET),$(if $(OSX_TARGET),-dynamiclib -single_module -multiply_defined suppress,-shared),)
373 LINKOPT :=
374 STRIPOPT := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-x, -u -r), -x --strip-unneeded --remove-section=.comment --remove-section=.note)
375 HOST_SODESTDIR := $(if $(WINDOWS_HOST),obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
376 SODESTDIR := $(if $(WINDOWS_TARGET),obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
377
378 # EXCLUDED_LIBS TOOL
379 _L = $(if $(filter $(1),$(EXCLUDED_LIBS)),,-l$(1))
380
381 # DEBIAN
382 ifdef DEBIAN_PACKAGE
383 CFLAGS += $(CPPFLAGS)
384 endif
385
386 ifdef DEBUG
387 CFLAGS += -D_DEBUG
388 endif
389
390 # COMMON LIBRARIES DETECTION
391 ifdef WINDOWS_TARGET
392  ifdef OPENSSL_CONF
393   _OPENSSL_CONF = $(call hidspace,$(call slash_path,$(OPENSSL_CONF)))
394   OPENSSL_INCLUDE_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/include,$(_OPENSSL_CONF)))
395   OPENSSL_LIB_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/lib,$(_OPENSSL_CONF)))
396   OPENSSL_BIN_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/bin,$(_OPENSSL_CONF)))
397  else
398 # to avoid issues with empty -L/-I
399   OPENSSL_INCLUDE_DIR = .
400   OPENSSL_LIB_DIR = .
401   OPENSSL_BIN_DIR = .
402  endif
403 endif