ecere/Desktop3D: (WIP) Attempts to fix full screen mode
[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 .SUFFIXES: .c .ec .sym .imp .bowl .o .a
199 EC := .ec
200 S := .sym
201 I := .imp
202 B := .bowl
203 C := .c
204 O := .o
205 A := .a
206 E := $(if $(WINDOWS_TARGET),.exe,)
207 SO := $(if $(WINDOWS_TARGET),.dll,$(if $(OSX_TARGET),.dylib,.so))
208 LP := $(if $(WINDOWS_TARGET),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
209 HOST_E := $(if $(WINDOWS_HOST),.exe,)
210 HOST_SO := $(if $(WINDOWS_HOST),.dll,$(if $(OSX_HOST),.dylib,.so))
211 HOST_LP := $(if $(WINDOWS_HOST),$(if $(STATIC_LIBRARY_TARGET),lib,),lib)
212
213 # TARGET VERSION
214 VER := $(if $(LINUX_TARGET),$(if $(LINUX_HOST),$(if $(VERSION),.$(VERSION),),),)
215
216 # SUPER TOOLS
217 ifdef CCACHE
218    CCACHE_COMPILE := ccache$(space)
219 ifdef DISTCC
220    DISTCC_COMPILE := distcc$(space)
221 endif
222 else
223 ifdef DISTCC
224    DISTCC_COMPILE := distcc$(space)
225 endif
226 endif
227
228 _CPP = $(if $(findstring $(space),$(CPP)),"$(CPP)",$(CPP))
229
230 _SYSROOT = $(if $(SYSROOT),$(space)--sysroot=$(SYSROOT),)
231
232 # SHELL COMMANDS
233 ifdef WINDOWS_HOST
234 ifndef MSYSCON
235    WIN_SHELL_COMMANDS := defined
236 endif
237 endif
238 ifdef WIN_SHELL_COMMANDS
239    nullerror = 2>NUL
240    echo = $(if $(1),echo $(1))
241    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%%)")
242    cpq = $(if $(1),@cmd /c "for %%I in ($(call sys_path,$(1))) do copy /by %%I $(call sys_path,$(2))" > nul 2>&1)
243    rmq = $(if $(1),-del /f /q $(call sys_path,$(call sys_path_list,$(1))) > nul 2>&1)
244    rmrq = $(if $(1),-rmdir /q /s $(call sys_path,$(1)) > nul 2>&1)
245    mkdirq = $(if $(1),-mkdir $(call sys_path,$(1)) > nul 2>&1)
246    rmdirq = $(if $(1),-rmdir /q $(call sys_path,$(1)) > nul 2>&1)
247 else
248    nullerror = 2>/dev/null
249    echo = $(if $(1),echo "$(1)")
250    touch = $(if $(1),touch $(1))
251    cpq = $(if $(1),cp $(1) $(2))
252    rmq = $(if $(1),-rm -f $(1))
253    rmrq = $(if $(1),-rm -f -r $(1))
254    mkdirq = $(if $(1),-mkdir -p $(1))
255    rmdirq = $(if $(1),-rmdir $(1))
256 endif
257
258 # potential common use variables
259 numbers := 0 1 2 3 4 5 6 7 8 9
260
261 # potential common use functions
262 reverselist = $(if $(1),$(call reverselist,$(strip $(wordlist 2,$(words $(1)),$(1))))) $(firstword $(1))
263 dirlistfromlocation = $(strip $(subst $(slash),$(space),$(subst $(backslash),$(space),$(1))))
264 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)))))))))))
265 hasnumbers = $(if $(filter $(numbers),$(call spacenumbers,$(1))),$(1),)
266 isanumber = $(if $(filter-out $(numbers),$(call spacenumbers,$(1))),,$(1))
267
268 # location version utility functions (lv_*)
269 lv_issimplever = $(if $(call isanumber,$(firstword $(call spacenumbers,$(subst .,,$(1))))),$(1),)
270 lv_isversionver = $(if $(call lv_issimplever,$(1:v%=%)),$(1),$(if $(call lv_issimplever,$(1:ver%=%)),$(1),$(if $(call lv_issimplever,$(1:version%=%)),$(1),)))
271 lv_isreleasever = $(if $(call lv_issimplever,$(1:r%=%)),$(1),$(if $(call lv_issimplever,$(1:rel%=%)),$(1),$(if $(call lv_issimplever,$(1:release%=%)),$(1),)))
272 lv_isbuildver = $(if $(call lv_issimplever,$(1:b%=%)),$(1),$(if $(call lv_issimplever,$(1:bld%=%)),$(1),$(if $(call lv_issimplever,$(1:build%=%)),$(1),)))
273 lv_iscomplexver = $(if $(call lv_isversionver,$(1)),$(1),$(if $(call lv_isreleasever,$(1)),$(1),$(if $(call lv_isbuildver,$(1)),$(1),)))
274 lv_isver = $(if $(call lv_issimplever,$(1)),$(1),$(if $(call lv_iscomplexver,$(1)),$(1),))
275 lv_possibleverorver = $(if $(findstring -,$(1)),$(if $(call hasnumbers,$(1)),$(1),),$(if $(call lv_isver,$(1)),$(1),))
276 lv_termslistfromdir = $(strip $(subst -,$(space),$(1)))
277 lv_verfromtermlist = $(if $(1)$(2),$(if $(1),$(1)$(if $(2),-,),)$(call lv_verfromtermlist,$(firstword $(2)),$(wordlist 2,$(words $(2)),$(2))),)
278 lv_termwalker = $(if $(firstword $(1)),$(if $(call lv_isver,$(firstword $(1))),$(call lv_verfromtermlist,,$(1)),$(call lv_termwalker,$(wordlist 2,$(words $(1)),$(1)))),)
279 lv_version = $(if $(call lv_possibleverorver,$(1)),$(call lv_termwalker,$(call lv_termslistfromdir,$(1))),)
280 lv_dirwalker = $(if $(firstword $(1)),$(if $(call lv_version,$(firstword $(1))),$(call lv_version,$(firstword $(1))),$(call lv_dirwalker,$(wordlist 2,$(words $(1)),$(1)))),)
281 locationversion = $(call shwspace,$(call lv_dirwalker,$(call reverselist,$(subst $(space)$(space),$(space),$(call dirlistfromlocation,$(call hidspace,$(1)))))))
282
283 # SOURCE CODE REPOSITORY VERSION
284 ifndef REPOSITORY_VER
285    # TODO: support other VCS
286    ifndef GIT_REPOSITORY
287       ifndef GIT
288          GIT := git
289       endif
290       ifeq ($(shell $(GIT) --version $(nullerror)),)
291          export GIT_NA := $(GIT)NotAvailable
292       else
293          ifneq ($(shell $(GIT) log -n 1 --format="%%%%" $(nullerror)),)
294             export GIT_REPOSITORY := yes
295             export REPOSITORY_VER := $(shell $(GIT) describe --tags --dirty="\ (dirty)" --always)
296          endif
297       endif
298    endif
299    ifndef REPOSITORY_VER
300       DIR_VER := $(call locationversion,$(CURDIR))
301       ifneq ($(DIR_VER),)
302          export REPOSITORY_VER := $(DIR_VER)
303       endif
304    endif
305    ifndef REPOSITORY_VER
306       export REPOSITORY_VER := unknown
307    endif
308 endif
309
310 # COMPILER OPTIONS
311 ECSLIBOPT := $(if $(STATIC_LIBRARY_TARGET),-staticlib,$(if $(SHARED_LIBRARY_TARGET),-dynamiclib,))
312 FVISIBILITY := $(if $(WINDOWS_TARGET),,-fvisibility=hidden)
313 FPIC := $(if $(WINDOWS_TARGET),,-fPIC)
314 EXECUTABLE := $(if $(WINDOWS_TARGET),$(if $(EXECUTABLE_TARGET),$(CONSOLE),),)
315 INSTALLNAME := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-install_name $(LP)$(MODULE)$(SO),),)
316
317 # LINKER OPTIONS
318 SHAREDLIB := $(if $(SHARED_LIBRARY_TARGET),$(if $(OSX_TARGET),-dynamiclib -single_module -multiply_defined suppress,-shared),)
319 LINKOPT :=
320 STRIPOPT := $(if $(OSX_TARGET),$(if $(SHARED_LIBRARY_TARGET),-x, -u -r), -x --strip-unneeded --remove-section=.comment --remove-section=.note)
321 HOST_SODESTDIR := $(if $(WINDOWS_HOST),obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(HOST_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
322 SODESTDIR := $(if $(WINDOWS_TARGET),obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/bin/,obj/$(TARGET_PLATFORM)$(COMPILER_SUFFIX)$(DEBUG_SUFFIX)/lib/)
323
324 # EXCLUDED_LIBS TOOL
325 _L = $(if $(filter $(1),$(EXCLUDED_LIBS)),,-l$(1))
326
327 # DEBIAN
328 ifdef DEBIAN_PACKAGE
329 CFLAGS += $(CPPFLAGS)
330 endif
331
332 ifdef DEBUG
333 CFLAGS += -D_DEBUG
334 endif
335
336 # COMMON LIBRARIES DETECTION
337 ifdef WINDOWS_TARGET
338  ifdef OPENSSL_CONF
339   _OPENSSL_CONF = $(call hidspace,$(call slash_path,$(OPENSSL_CONF)))
340   OPENSSL_INCLUDE_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/include,$(_OPENSSL_CONF)))
341   OPENSSL_LIB_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/lib,$(_OPENSSL_CONF)))
342   OPENSSL_BIN_DIR = $(call shwspace,$(subst /bin/openssl.cfg,/bin,$(_OPENSSL_CONF)))
343  else
344 # to avoid issues with empty -L/-I
345   OPENSSL_INCLUDE_DIR = .
346   OPENSSL_LIB_DIR = .
347   OPENSSL_BIN_DIR = .
348  endif
349 endif