Newer
Older
Bryan O'Sullivan
committed
# -*- 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.
#*****************************************************************************
include_guard()
Bryan O'Sullivan
committed
include(Variables)
Bryan O'Sullivan
committed
# Portable compilation flags.
add_compile_definitions(
ADDRESS_SIZE=${ADDRESS_SIZE}
$<$<CONFIG:Debug>:_DEBUG>
$<$<CONFIG:Debug>:LL_DEBUG=1>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:LL_RELEASE=1>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:NDEBUG>
$<$<CONFIG:RelWithDebInfo>:LL_RELEASE_WITH_DEBUG_INFO=1>
$<$<CONFIG:Release>:LL_RELEASE_FOR_DOWNLOAD=1>
# 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")
Bryan O'Sullivan
committed
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()
Bryan O'Sullivan
committed
# Don't bother with a MinSizeRel build.
Bryan O'Sullivan
committed
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Release;Debug" CACHE STRING
"Supported build types." FORCE)
option(USE_SSE42 "Enable usage of the SSE4.2 instruction set" ON)
option(USE_AVX "Enable usage of the AVX instruction set" OFF)
option(USE_AVX2 "Enable usage of the AVX2 instruction set" OFF)
if((USE_SSE42 AND USE_AVX) OR (USE_SSE42 AND USE_AVX AND USE_AVX2) OR (USE_AVX AND USE_AVX2))
message(FATAL_ERROR "Usage of multiple SIMD flags is unsupported")
endif()
Bryan O'Sullivan
committed
# Warnings
option(DISABLE_FATAL_WARNINGS "Disable warnings as errors" ON)
Bryan O'Sullivan
committed
# Platform-specific compilation flags.
if (WINDOWS)
# Don't build DLLs.
set(BUILD_SHARED_LIBS OFF)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/MP)
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options(-m${ADDRESS_SIZE})
add_compile_definitions(
$<$<CONFIG:Debug>:_SCL_SECURE_NO_WARNINGS=1>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:_ITERATOR_DEBUG_LEVEL=0>
LL_WINDOWS=1
NOMINMAX
UNICODE
_UNICODE
_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_DEPRECATE
_WINSOCK_DEPRECATED_NO_WARNINGS
_SILENCE_CXX20_CISO646_REMOVED_WARNING
)
add_compile_options(
$<$<CONFIG:Debug>:/Od>
$<$<CONFIG:Debug>:/MDd>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:/O2>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:/MD>
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:/GS>
$<$<CONFIG:Release>:/Oi>
$<$<CONFIG:Release>:/Ot>
$<$<CONFIG:Release>:/Gy>
$<$<CONFIG:Release>:/Oy->
$<$<CONFIG:Release>:/Zc:inline>
$<$<CONFIG:Release>:/fp:fast>
/Zi
/EHsc
/permissive-
/W3
/c
/Zc:__cplusplus
/Zc:forScope
/Zc:rvalueCast
/Zc:strictStrings
/Zc:ternary
/nologo
)
add_link_options(
/DEBUG:FULL
/IGNORE:4099
/NODEFAULTLIB:LIBCMT
$<$<CONFIG:Debug>:/NODEFAULTLIB:LIBCMTD>
$<$<CONFIG:Debug>:/NODEFAULTLIB:MSVCRT>
)
if (ADDRESS_SIZE EQUAL 32)
add_compile_options(/arch:SSE2)
elseif (USE_AVX2)
add_compile_options(/arch:AVX2)
elseif (USE_AVX)
add_compile_options(/arch:AVX)
elseif (USE_SSE42)
add_compile_definitions(__SSE3__=1 __SSSE3__=1 __SSE4__=1 __SSE4_1__=1 __SSE4_2__=1)
add_compile_definitions(__SSE3__=1 __SSSE3__=1 __SSE4__=1 __SSE4_1__=1)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/Zc:externConstexpr /Zc:referenceBinding /ZH:SHA_256)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options(/Qvec /Zc:dllexportInlines- /clang:-mprefer-vector-width=128 -fno-strict-aliasing -Wno-ignored-pragma-intrinsic -Wno-unused-local-typedef)
endif()
if(FAVOR_AMD AND FAVOR_INTEL)
message(FATAL_ERROR "Cannot enable FAVOR_AMD and FAVOR_INTEL at the same time")
elseif(FAVOR_AMD)
add_compile_options(/favor:AMD64)
elseif(FAVOR_INTEL)
add_compile_options(/favor:INTEL64)
endif()
if (USE_LTO)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /LTCG")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /LTCG")
endif()
add_link_options(
$<$<CONFIG:Release>:/OPT:REF>
$<$<CONFIG:Release>:/OPT:ICF>
$<$<CONFIG:Release>:/INCREMENTAL:NO>
)
add_compile_options(/GL /Gy /Gw)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if(INCREMENTAL_LINK)
add_compile_options(-flto=thin -fwhole-program-vtables /clang:-fforce-emit-vtables)
else()
add_compile_options(-flto=full -fwhole-program-vtables /clang:-fforce-emit-vtables)
endif()
add_link_options(
$<$<CONFIG:Release>:/OPT:REF>
$<$<CONFIG:Release>:/OPT:ICF>
)
add_link_options($<$<CONFIG:Release>:/INCREMENTAL>)
add_link_options(
$<$<CONFIG:Release>:/OPT:REF>
$<$<CONFIG:Release>:/OPT:ICF>
$<$<CONFIG:Release>:/INCREMENTAL:NO>
)
endif ()
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# This is a massive hack and makes me sad. clang-cl fails to find its own builtins library :/ x64 only for now.
set(CLANG_RT_NAMES clang_rt.builtins-x86_64)
find_library(CLANG_RT NAMES ${CLANG_RT_NAMES}
PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\LLVM\\LLVM]/lib/clang/${CMAKE_CXX_COMPILER_VERSION}/lib/windows
[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\LLVM\\LLVM]/lib/clang/${CMAKE_CXX_COMPILER_VERSION}/lib/windows)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /defaultlib:\"${CLANG_RT}\"")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /defaultlib:\"${CLANG_RT}\"")
endif()
Bryan O'Sullivan
committed
string(REPLACE "/Ob2" "/Ob3" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
string(REPLACE "/Ob2" "/Ob3" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
# configure win32 API for 7 and above compatibility
set(WINVER "0x0601" CACHE STRING "Win32 API Target version (see http://msdn.microsoft.com/en-us/library/aa383745%28v=VS.85%29.aspx)")
add_compile_definitions(WINVER=${WINVER} _WIN32_WINNT=${WINVER})
Bryan O'Sullivan
committed
endif (WINDOWS)
if (LINUX)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
Bryan O'Sullivan
committed
add_compile_definitions(
LL_LINUX=1
APPID=secondlife
LL_IGNORE_SIGCHLD
_REENTRANT
$<$<CONFIG:Release>:_FORTIFY_SOURCE=2>
GDK_DISABLE_DEPRECATED
GTK_DISABLE_DEPRECATED
GSEAL_ENABLE
GTK_DISABLE_SINGLE_INCLUDES
)
add_compile_options(
$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-fstack-protector>
-fvisibility=hidden
-fexceptions
-fno-math-errno
-fno-strict-aliasing
-fsigned-char
-g
-pthread
)
add_compile_options(-mfpmath=sse -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2)
add_compile_options(-mfpmath=sse -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(-flto=thin -fwhole-program-vtables -fforce-emit-vtables)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-flto=auto -fno-fat-lto-objects)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (USE_ASAN)
add_compile_options(-fsanitize=address -fsanitize-recover=address)
elseif (USE_LEAKSAN)
add_compile_options(-fsanitize=leak)
elseif (USE_UBSAN)
add_compile_options(-fsanitize=undefined -fno-sanitize=vptr)
elseif (USE_THDSAN)
add_compile_options(-fsanitize=thread)
add_compile_options(-Og -fno-omit-frame-pointer)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND USE_LTO)
# Enable these flags so we have a read only GOT and some linking opts
add_link_options("LINKER:-z,relro" "LINKER:-z,now" "LINKER:--as-needed")
endif ()
Bryan O'Sullivan
committed
if (DARWIN)
add_compile_definitions(LL_DARWIN=1 GL_SILENCE_DEPRECATION=1)
add_link_options("LINKER:-headerpad_max_install_names" "LINKER:-search_paths_first")
add_compile_options(-O3 -gdwarf-2)
endif ()
Bryan O'Sullivan
committed
if (LINUX OR DARWIN)
add_compile_options(-Wall -Wno-sign-compare -Wno-reorder)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-unused-parameter -Wno-unused-but-set-parameter -Wno-ignored-qualifiers -Wno-unused-function)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
add_compile_options(-Wno-trigraphs -Wno-unused-local-typedef -Wno-unknown-warning-option)
endif()
Bryan O'Sullivan
committed
CHECK_CXX_COMPILER_FLAG(-Wdeprecated-copy HAS_DEPRECATED_COPY)
if (HAS_DEPRECATED_COPY)
Bryan O'Sullivan
committed
add_compile_options(-m${ADDRESS_SIZE})
option(RELEASE_SHOW_ASSERTS "Enable asserts in release builds" OFF)
if(RELEASE_SHOW_ASSERTS)
add_compile_definitions(RELEASE_SHOW_ASSERT=1)
option(ENABLE_TIMING "Enable all fast timers" ON)
if(ENABLE_TIMING)
add_compile_definitions(AL_ENABLE_ALL_TIMERS=1)