1#-------------------------------------------------------------------------
2# drawElements CMake utilities
3# ----------------------------
4#
5# Copyright 2014 The Android Open Source Project
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#      http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19#-------------------------------------------------------------------------
20
21# \note Always include this file in main project file, with NO_POLICY_SCOPE
22#       AFTER project(name) statement.
23#
24# project(deproject)
25# include(delibs/cmake/Defs.cmake NO_POLICY_SCOPE)
26
27cmake_policy(VERSION 2.6)
28
29# \todo [pyry] More intelligent detection, perhaps use some script?
30
31# cmake files can use DE_DEFS variable to check that this file has been included
32set(DE_DEFS 1)
33
34macro (DE_MAKE_ENV_BOOL BASE VALUE)
35	if (${BASE} STREQUAL ${BASE}_${VALUE})
36		set(${BASE}_IS_${VALUE} 1)
37	else ()
38		set(${BASE}_IS_${VALUE} 0)
39	endif ()
40endmacro ()
41
42# Add build type RelWithAsserts
43set(CMAKE_CXX_FLAGS_RELWITHASSERTS ${CMAKE_CXX_FLAGS_RELEASE})
44set(CMAKE_C_FLAGS_RELWITHASSERTS ${CMAKE_C_FLAGS_RELEASE})
45set(CMAKE_EXE_LINKER_FLAGS_RELWITHASSERTS ${CMAKE_EXE_LINKER_FLAGS_RELEASE})
46set(CMAKE_SHARED_LINKER_FLAGS_RELWITHASSERTS ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})
47
48# Os detection
49if (NOT DEFINED DE_OS)
50	if (WIN32)
51		set(DE_OS "DE_OS_WIN32")
52	elseif (APPLE)
53		set(DE_OS "DE_OS_OSX")
54	elseif (UNIX)
55		set(DE_OS "DE_OS_UNIX")
56	else ()
57		set(DE_OS "DE_OS_VANILLA")
58	endif ()
59endif ()
60
61# DE_OS_IS_{PLATFORM} definitions
62DE_MAKE_ENV_BOOL("DE_OS" "VANILLA")
63DE_MAKE_ENV_BOOL("DE_OS" "WIN32")
64DE_MAKE_ENV_BOOL("DE_OS" "UNIX")
65DE_MAKE_ENV_BOOL("DE_OS" "WINCE")
66DE_MAKE_ENV_BOOL("DE_OS" "OSX")
67DE_MAKE_ENV_BOOL("DE_OS" "ANDROID")
68DE_MAKE_ENV_BOOL("DE_OS" "IOS")
69
70# Prevent mixed compile with GCC and Clang
71if (NOT ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") EQUAL ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"))
72	message(FATAL_ERROR "CMake C and CXX compilers do not match. Both or neither must be GNU.")
73elseif (NOT ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") EQUAL ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
74	message(FATAL_ERROR "CMake C and CXX compilers do not match. Both or neither must be Clang.")
75endif ()
76
77# Compiler detection
78if (NOT DEFINED DE_COMPILER)
79	# \note " x" postfix is to work around bug in CMake that causes
80	#       "MSVC" to translate to something completely different
81	if (("${CMAKE_C_COMPILER_ID} x" MATCHES "MSVC") OR MSVC)
82		set(DE_COMPILER "DE_COMPILER_MSC")
83	elseif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU")
84		set(DE_COMPILER "DE_COMPILER_GCC")
85	elseif ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
86		set(DE_COMPILER "DE_COMPILER_CLANG")
87
88	# Guess based on OS
89	elseif (DE_OS_IS_WIN32)
90		set(DE_COMPILER "DE_COMPILER_MSC")
91	elseif (DE_OS_IS_UNIX OR DE_OS_IS_ANDROID)
92		set(DE_COMPILER "DE_COMPILER_GCC")
93	elseif (DE_OS_IS_OSX OR DE_OS_IS_IOS)
94		set(DE_COMPILER "DE_COMPILER_CLANG")
95
96	else ()
97		set(DE_COMPILER "DE_COMPILER_VANILLA")
98	endif ()
99endif ()
100
101# DE_COMPILER_IS_{COMPILER} definitions
102DE_MAKE_ENV_BOOL("DE_COMPILER" "VANILLA")
103DE_MAKE_ENV_BOOL("DE_COMPILER" "MSC")
104DE_MAKE_ENV_BOOL("DE_COMPILER" "GCC")
105DE_MAKE_ENV_BOOL("DE_COMPILER" "CLANG")
106
107# Pointer size detection
108if (NOT DEFINED DE_PTR_SIZE)
109	if (DEFINED CMAKE_SIZEOF_VOID_P)
110		set(DE_PTR_SIZE ${CMAKE_SIZEOF_VOID_P})
111	else ()
112		set(DE_PTR_SIZE 4)
113	endif ()
114endif ()
115
116# CPU detection
117if (NOT DEFINED DE_CPU)
118	if (DE_PTR_SIZE EQUAL 8)
119		set(DE_CPU "DE_CPU_X86_64")
120	else ()
121		set(DE_CPU "DE_CPU_X86")
122	endif ()
123endif ()
124
125# DE_CPU_IS_{CPU} definitions
126DE_MAKE_ENV_BOOL("DE_CPU" "VANILLA")
127DE_MAKE_ENV_BOOL("DE_CPU" "X86")
128DE_MAKE_ENV_BOOL("DE_CPU" "ARM")
129DE_MAKE_ENV_BOOL("DE_CPU" "ARM_64")
130
131# \note [petri] Re-wrote in this ugly manner, because CMake 2.6 seems to
132#               barf about the parenthesis in the previous way. Ugh.
133#if (NOT ((DE_PTR_SIZE EQUAL 4) OR (DE_PTR_SIZE EQUAL 8)))
134if (DE_PTR_SIZE EQUAL 4)
135elseif (DE_PTR_SIZE EQUAL 8)
136else ()
137	message(FATAL_ERROR "DE_PTR_SIZE (${DE_PTR_SIZE}) is invalid")
138endif ()
139
140# Debug definitions
141if (NOT DEFINED DE_DEBUG)
142	if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithAsserts")
143		set(DE_DEBUG 1)
144	else ()
145		set(DE_DEBUG 0)
146	endif ()
147endif ()
148
149# Android API version
150if (DE_OS_IS_ANDROID AND NOT DEFINED DE_ANDROID_API)
151	set(DE_ANDROID_API 5)
152endif ()
153
154message(STATUS "DE_OS          = ${DE_OS}")
155message(STATUS "DE_COMPILER    = ${DE_COMPILER}")
156message(STATUS "DE_CPU         = ${DE_CPU}")
157message(STATUS "DE_PTR_SIZE    = ${DE_PTR_SIZE}")
158message(STATUS "DE_DEBUG       = ${DE_DEBUG}")
159if (DE_OS_IS_ANDROID)
160	message(STATUS "DE_ANDROID_API = ${DE_ANDROID_API}")
161endif ()
162
163# Expose definitions
164if (DE_DEBUG)
165	add_definitions(-DDE_DEBUG)
166endif ()
167
168add_definitions("-DDE_OS=${DE_OS}")
169add_definitions("-DDE_COMPILER=${DE_COMPILER}")
170add_definitions("-DDE_CPU=${DE_CPU}")
171add_definitions("-DDE_PTR_SIZE=${DE_PTR_SIZE}")
172
173if (DE_OS_IS_ANDROID)
174	add_definitions("-DDE_ANDROID_API=${DE_ANDROID_API}")
175endif ()
176