summaryrefslogtreecommitdiff
path: root/indra/cmake/00-Common.cmake
blob: 254097b491017ea904c378852d11605d9c8e14d3 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- cmake -*-
#
# Compilation options shared by all Second Life components.

#*****************************************************************************
#   It's important to realize that CMake implicitly concatenates
#   CMAKE_CXX_FLAGS with (e.g.) CMAKE_CXX_FLAGS_RELEASE for Release builds. So
#   set switches in CMAKE_CXX_FLAGS that should affect all builds, but in
#   CMAKE_CXX_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELWITHDEBINFO for switches
#   that should affect only that build variant.
#
#   Also realize that CMAKE_CXX_FLAGS may already be partially populated on
#   entry to this file.
#
#   Additionally CMAKE_C_FLAGS is prepended to CMAKE_CXX_FLAGS_RELEASE and
#   CMAKE_CXX_FLAGS_RELWITHDEBINFO which risks having flags overriden by cmake
#   inserting additional options that are part of the build config type.
#*****************************************************************************
include_guard()

include(Variables)
include(Linking)
include(UnixInstall)

# We go to some trouble to set LL_BUILD to the set of relevant compiler flags.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{LL_BUILD}")

if (DARWIN)
  # LL_BUILD carries a literal -mmacosx-version-min=<ver> from
  # viewer-build-variables. We derive the macOS deployment target in
  # Variables.cmake (clamping up to the SDK's supported minimum when needed)
  # and let CMAKE_OSX_DEPLOYMENT_TARGET emit the flag to both compiler and
  # linker. Strip the verbatim flag here so it can't conflict with the
  # clamped deployment target.
  string(REGEX REPLACE "-mmacosx-version-min=[0-9.]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif (DARWIN)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} $ENV{LL_BUILD_RELEASE}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} $ENV{LL_BUILD_RELWITHDEBINFO}")

# Given that, all the flags you see added below are flags NOT present in
# https://bitbucket.org/lindenlab/viewer-build-variables/src/tip/variables.
# Before adding new ones here, it's important to ask: can this flag really be
# applied to the viewer only, or should/must it be applied to all 3p libraries
# as well?

# Portable compilation flags.
add_compile_definitions(ADDRESS_SIZE=${ADDRESS_SIZE})
# Because older versions of Boost.Bind dumped placeholders _1, _2 et al. into
# the global namespace, Boost now requires either BOOST_BIND_NO_PLACEHOLDERS
# to avoid that or BOOST_BIND_GLOBAL_PLACEHOLDERS to state that we require it
# -- which we do. Without one or the other, we get a ton of Boost warnings.
add_compile_definitions(BOOST_BIND_GLOBAL_PLACEHOLDERS)

if(CMAKE_OSX_ARCHITECTURES MATCHES arm64 OR CMAKE_SYSTEM_PROCESSOR MATCHES aarch64)
add_compile_definitions(GLM_FORCE_DEFAULT_ALIGNED_GENTYPES=1 GLM_FORCE_NEON=1 GLM_ENABLE_EXPERIMENTAL=1)
elseif($ENV{MSYSTEM_CARCH} MATCHES aarch64)
add_compile_definitions(GLM_FORCE_DEFAULT_ALIGNED_GENTYPES=1 GLM_ENABLE_EXPERIMENTAL=1)
else()
# Force enable SSE2 instructions in GLM per the manual
# https://github.com/g-truc/glm/blob/master/manual.md#section2_10
add_compile_definitions(GLM_FORCE_DEFAULT_ALIGNED_GENTYPES=1 GLM_FORCE_SSE2=1 GLM_ENABLE_EXPERIMENTAL=1)
endif()

# Configure crash reporting
set(RELEASE_CRASH_REPORTING OFF CACHE BOOL "Enable use of crash reporting in release builds")
set(NON_RELEASE_CRASH_REPORTING OFF CACHE BOOL "Enable use of crash reporting in developer builds")

if(RELEASE_CRASH_REPORTING)
  add_compile_definitions( LL_SEND_CRASH_REPORTS=1)
endif()

if(NON_RELEASE_CRASH_REPORTING)
  add_compile_definitions( LL_SEND_CRASH_REPORTS=1)
endif()

set(USE_LTO OFF CACHE BOOL "Enable Link Time Optimization")
if(USE_LTO)
  set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
endif()

# Don't bother with a MinSizeRel or Debug builds.
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Release" CACHE STRING "Supported build types." FORCE)

# Platform-specific compilation flags.

if (NOT CMAKE_CXX_COMPILER_ID MATCHES GNU AND WINDOWS)
  # Don't build DLLs.
  set(BUILD_SHARED_LIBS OFF)

  # for "backwards compatibility", cmake sneaks in the Zm1000 option which royally
  # screws incredibuild. this hack disables it.
  # for details see: http://connect.microsoft.com/VisualStudio/feedback/details/368107/clxx-fatal-error-c1027-inconsistent-values-for-ym-between-creation-and-use-of-precompiled-headers
  # http://www.ogre3d.org/forums/viewtopic.php?f=2&t=60015
  # http://www.cmake.org/pipermail/cmake/2009-September/032143.html
  string(REPLACE "/Zm1000" " " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

  add_link_options(/LARGEADDRESSAWARE
          /NODEFAULTLIB:LIBCMT
          /IGNORE:4099)

  add_compile_definitions(
      WIN32_LEAN_AND_MEAN
      NOMINMAX
#     DOM_DYNAMIC                     # For shared library colladadom
      _CRT_SECURE_NO_WARNINGS         # Allow use of sprintf etc
      _CRT_NONSTDC_NO_DEPRECATE       # Allow use of sprintf etc
      _CRT_OBSOLETE_NO_WARNINGS
      _WINSOCK_DEPRECATED_NO_WARNINGS # Disable deprecated WinSock API warnings
      )
  add_compile_options(
          /Zo
          /GS
          /TP
          /W3
          /c
          /Zc:forScope
          /nologo
          /Oy-
          /fp:fast
          /MP
          /permissive-
      )

  # Nicky: x64 implies SSE2
  if( ADDRESS_SIZE EQUAL 32 )
    add_compile_options( /arch:SSE2 )
  endif()

  # Are we using the crummy Visual Studio KDU build workaround?
  if (NOT VS_DISABLE_FATAL_WARNINGS)
    add_compile_options(/WX)
  endif (NOT VS_DISABLE_FATAL_WARNINGS)

  #ND: When using something like buildcache (https://github.com/mbitsnbites/buildcache)
  # to make those wrappers work /Zi must be changed to /Z7, as /Zi due to it's nature is not compatible with caching
  if (${CMAKE_CXX_COMPILER_LAUNCHER} MATCHES ".*cache.*")
    add_compile_options( /Z7 )
    string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
    string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
    string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
    string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
  endif()
endif ()

if (LINUX OR CMAKE_SYSTEM_NAME MATCHES FreeBSD)
  set( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE )
  set( CMAKE_INSTALL_RPATH ${INSTALL_LIBRARY_DIR} )
  set(CMAKE_EXE_LINKER_FLAGS "-Wl,--exclude-libs,ALL")

  find_program(CCACHE_EXE ccache)
  if(CCACHE_EXE AND NOT DISABLE_CCACHE)
    set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_EXE} )
    set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_EXE} )
  endif()

   # LL_IGNORE_SIGCHLD
   # don't catch SIGCHLD in our base application class for the viewer - some of
   # our 3rd party libs may need their *own* SIGCHLD handler to work. Sigh! The
   # viewer doesn't need to catch SIGCHLD anyway.

  add_compile_definitions(
          _REENTRANT
          APPID=megapahit
          LL_IGNORE_SIGCHLD
  )

  option(ENABLE_ASAN "Enable Address Sanitizer" OFF)
  option(ENABLE_UBSAN "Enable Undefined Behavior Sanitizer" OFF)
  option(ENABLE_THREADSAN "Enable Thread Sanitizer" OFF)
  if(ENABLE_ASAN OR ENABLE_UBSAN OR ENABLE_THREADSAN)
    set(GCC_DISABLE_FATAL_WARNINGS ON) # Disable warnings as errors during sanitizer builds due to false positives

    add_compile_options(
      -U_FORTIFY_SOURCE
      -fno-omit-frame-pointer
      -fno-common
      -fsanitize-recover=all
    )

    # libwebrtc is incompatible with sanitizers
    set(DISABLE_WEBRTC ON)
    add_compile_definitions(DISABLE_WEBRTC=1)

    if(ENABLE_ASAN)
      add_compile_options(-fsanitize=address)
      add_link_options(-fsanitize=address)
    endif()

    if(ENABLE_UBSAN)
      add_compile_options(-fsanitize=undefined)
      add_link_options(-fsanitize=undefined)
    endif()

    if(ENABLE_THREADSAN)
      add_compile_options(-fsanitize=thread)
      add_link_options(-fsanitize=thread)
    endif()
  elseif(NOT (${LINUX_DISTRO} MATCHES freedesktop))
    add_compile_definitions($<$<CONFIG:Release>:_FORTIFY_SOURCE=2>)
  endif()

  if (CMAKE_SYSTEM_NAME MATCHES FreeBSD)
    add_compile_definitions(EXTERNAL_TOS)
  endif ()

  add_compile_options(
          $<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-fstack-protector>
          -fexceptions
          -fno-math-errno
          -fno-strict-aliasing
          -fsigned-char
          #-g
          -pthread
  )

  if (CMAKE_SYSTEM_NAME MATCHES x86_64)
      add_compile_options(
          -msse2
      )
  endif ()

  if (NOT BUILD_SHARED_LIBS)
      add_compile_options(
          -fvisibility=hidden
      )
  endif ()

  set(GCC_CLANG_COMPATIBLE_WARNINGS
      -Wno-parentheses
      -Wno-deprecated
      -Wno-c++20-compat
      -Wno-pessimizing-move
  )

  set(CLANG_WARNINGS
      -Wno-inconsistent-missing-override
  )

  set(GCC_WARNINGS
      ${GCC_CLANG_COMPATIBLE_WARNINGS}
      -Wno-dangling-pointer
  )

  if (CMAKE_SYSTEM_PROCESSOR MATCHES aarch64)
    set(GCC_WARNINGS
        ${GCC_WARNINGS}
        -Wno-cpp
    )
  endif ()
  add_link_options(
          -Wl,--no-keep-memory
          "LINKER:-z,relro"
          "LINKER:-z,now"
          "LINKER:--build-id"
          "LINKER:--as-needed"
          "LINKER:--no-undefined"
  )
  if (NOT GCC_DISABLE_FATAL_WARNINGS)
    add_compile_options( -Werror )
  endif (NOT GCC_DISABLE_FATAL_WARNINGS)

  if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    # ND: clang is a bit more picky than GCC, the latter seems to auto include -lstdc++ and -lm. The former not so and thus fails to link
    add_link_options(
            -lstdc++
            -lm
    )
    add_compile_options(${CLANG_WARNINGS})
  else()
    add_compile_options(${GCC_WARNINGS})
  endif()
endif ()

if (DARWIN)
  # Use rpath loading on macos
  set(CMAKE_MACOSX_RPATH TRUE)

  set(CMAKE_CXX_LINK_FLAGS "-Wl,-headerpad_max_install_names,-search_paths_first")
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINK_FLAGS}")

  # Ensure debug symbols are always generated
  add_compile_options(-g --debug) # --debug is a clang synonym for -g that bypasses cmake behaviors

  # Silence GL deprecation warnings
  add_compile_definitions(GL_SILENCE_DEPRECATION=1)

  add_compile_options(-Wno-inconsistent-missing-override)
  if (CMAKE_OSX_ARCHITECTURES MATCHES arm64)
    add_compile_options("-Wno-#warnings")
  endif()
endif(DARWIN)

if (LINUX OR DARWIN OR CMAKE_SYSTEM_NAME MATCHES FreeBSD)
  add_compile_options(-Wall -Wno-sign-compare -Wno-trigraphs -Wno-reorder -Wno-unused-but-set-variable -Wno-unused-variable)

  if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    add_compile_options(-Wno-unused-private-field)
  endif()

  if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
    add_compile_options(-Wno-unused-local-typedef)
  endif()

  if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-Wno-stringop-truncation -Wno-stringop-overflow -Wno-parentheses -Wno-maybe-uninitialized -Wno-unused-local-typedefs)
  endif ()

  if (NOT GCC_DISABLE_FATAL_WARNINGS AND NOT CLANG_DISABLE_FATAL_WARNINGS)
    add_compile_options(-Werror)
  endif ()

  if (NOT (CMAKE_SYSTEM_PROCESSOR MATCHES aarch64))
  add_compile_options(-m${ADDRESS_SIZE})
  endif ()
endif ()