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