summaryrefslogtreecommitdiffhomepage
path: root/cmake/opentrack-platform.cmake
blob: 04b220f653abdcf4e601eee780624c4f3f25771d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

include_guard(GLOBAL)

if(MSVC AND MSVC_VERSION LESS "1915" AND NOT ".${CMAKE_CXX_COMPILER_ID}" STREQUAL ".Clang")
    message(FATAL_ERROR "Visual Studio too old. Use Visual Studio 2017 or newer.")
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # for clang

string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING "" FORCE)

include_directories("${CMAKE_SOURCE_DIR}")

set(opentrack_maintainer-mode FALSE CACHE INTERNAL "Select if developing core code (not modules)")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_DEFAULT 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_EXTENSIONS FALSE)

set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_SKIP_INSTALL_RPATH FALSE)
set(CMAKE_SKIP_RPATH FALSE)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

set(CMAKE_MACOSX_RPATH OFF)

if(NOT WIN32 AND NOT APPLE)
    include(opentrack-pkg-config)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set(CMAKE_COMPILER_IS_GNUCXX TRUE)
    set(CMAKE_COMPILER_IS_CLANGXX TRUE)
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
    set(CMAKE_COMPILER_IS_GNUCC TRUE)
    set(CMAKE_COMPILER_IS_CLANG TRUE)
endif()

if(APPLE AND NOT CMAKE_OSX_ARCHITECTURES)
    set(CMAKE_OSX_ARCHITECTURES "x86_64")
    set(opentrack-intel TRUE)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*|i[0-9]86.*|x86.*")
    set(opentrack-intel TRUE)
elseif(MSVC AND CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "")
    set(opentrack-intel TRUE)
else()
    set(opentrack-intel FALSE)
endif()

if(CMAKE_SIZEOF_VOID_P GREATER_EQUAL 8)
    set(opentrack-64bit TRUE)
else()
    set(opentrack-64bit FALSE)
endif()

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(LINUX TRUE)
endif()

if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE)
    set(CMAKE_CXX_FLAGS "-fuse-cxa-atexit ${CMAKE_CXX_FLAGS}")

    if(LINUX) # assume binutils
        add_link_options(-Wl,--exclude-libs,ALL)
        add_link_options(-Wl,-z,relro,-z,now)
        add_link_options(-Wl,--as-needed)
    endif()
endif()

if(WIN32)
    add_definitions(-D_USE_MATH_DEFINES=1 -DSTRSAFE_NO_DEPRECATE)
endif()

if(MINGW)
    add_definitions(-DMINGW_HAS_SECURE_API)
endif()

if(MSVC)
    set(CMAKE_RC_FLAGS "/nologo /DWIN32")

    add_definitions(-DNOMINMAX)
    add_definitions(-DWIN32_LEAN_AND_MEAN)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    add_definitions(-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1)
    add_definitions(-D_SCL_SECURE_NO_WARNINGS)

    add_compile_options(-EHs-c-)
    add_definitions(-D_HAS_EXCEPTIONS=0)

    add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)
    add_definitions(-D_ENABLE_ATOMIC_ALIGNMENT_FIX)
    add_definitions(-D_SILENCE_CXX17_NEGATORS_DEPRECATION_WARNING)
    add_definitions(-D_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING)

    add_compile_options(-diagnostics:caret)
    add_compile_options(-permissive-)

    if(opentrack-64bit)
        add_link_options(-HIGHENTROPYVA)
    endif()

    add_link_options(-DYNAMICBASE -NXCOMPAT)
    add_link_options(-WX)
    add_link_options(-ignore:4020)
    add_link_options(-ignore:4217) # debug build

    #C4457: declaration of 'id' hides function parameter
    #C4456: declaration of 'i' hides previous local declaration
    #C4263 - member function does not override any base class virtual member function
    #C4264 - no override available for virtual member function from base class, function is hidden
    #C4265 - class has virtual functions, but destructor is not virtual
    #C4266 - no override available for virtual member function from base type, function is hidden
    #C4928 - illegal copy-initialization, more than one user-defined conversion has been implicitly applied
    #C4200: nonstandard extension used: zero-sized array in struct/union

    set(warns-disable 4530 4577 4789 4244 4702 4530 4244 4127 4458 4456 4251 4100 4702 4457 4200)

    foreach(i ${warns-disable})
        add_compile_options(-wd${i})
    endforeach()
endif()

if(APPLE)
    add_compile_definitions(-stdlib=libc++)
    add_link_options(-stdlib=libc++)

    add_link_options(-framework Cocoa -framework CoreFoundation -framework Carbon)
    link_libraries(objc z)
endif()

if(NOT MSVC)
    include(FindPkgConfig)
endif()