Skip to content
Snippets Groups Projects
00-Common.cmake 10.2 KiB
Newer Older
# -*- 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.
#*****************************************************************************
Rye Mutt's avatar
Rye Mutt committed
include(CheckCXXCompilerFlag)
Rye Mutt's avatar
Rye Mutt committed
include(SDL2)
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")
  add_compile_definitions( LL_SEND_CRASH_REPORTS=1)
  add_compile_definitions( LL_SEND_CRASH_REPORTS=1)
endif()
# Don't bother with a MinSizeRel build.
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Release;Debug" CACHE STRING
    "Supported build types." FORCE)

# SIMD config
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)
Rye Mutt's avatar
Rye Mutt committed
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()
# Warnings
option(DISABLE_FATAL_WARNINGS "Disable warnings as errors" ON)

# 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)
  else ()
    add_compile_definitions(__SSE3__=1 __SSSE3__=1 __SSE4__=1 __SSE4_1__=1)
  endif ()
  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")
      if(INCREMENTAL_LINK)
        add_link_options(/LTCG:incremental)
        set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /LTCG")
      else()
        add_link_options(/LTCG)
Rye Mutt's avatar
Rye Mutt committed
        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>
        )
    endif()
  elseif (INCREMENTAL_LINK)
    add_link_options($<$<CONFIG:Release>:/INCREMENTAL>)
  else ()
    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()

  if (NOT DISABLE_FATAL_WARNINGS)
    add_compile_options(/WX)
  endif (NOT DISABLE_FATAL_WARNINGS)
  string(REPLACE "/Ob2" "/Ob3" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
  string(REPLACE "/Ob2" "/Ob3" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
Rye Mutt's avatar
Rye Mutt committed

  # 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})
  set(CMAKE_SKIP_BUILD_RPATH TRUE)
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  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
    )
Rye Mutt's avatar
Rye Mutt committed

  if (USE_AVX2)
    add_compile_options(-mavx2)
Rye Mutt's avatar
Rye Mutt committed
  elseif (USE_AVX)
    add_compile_options(-mavx)
Rye Mutt's avatar
Rye Mutt committed
  elseif (USE_SSE42)
    add_compile_options(-mfpmath=sse -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2)
Rye Mutt's avatar
Rye Mutt committed
  else()
    add_compile_options(-mfpmath=sse -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1)
Rye Mutt's avatar
Rye Mutt committed
  endif ()
Rye Mutt's avatar
Rye Mutt committed
  if (USE_LTO)
    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)
    endif ()
Rye Mutt's avatar
Rye Mutt committed

  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)
Rye Mutt's avatar
Rye Mutt committed
      link_libraries(-lasan)
    elseif (USE_LEAKSAN)
      add_compile_options(-fsanitize=leak)
Rye Mutt's avatar
Rye Mutt committed
      link_libraries(-llsan)
    elseif (USE_UBSAN)
      add_compile_options(-fsanitize=undefined -fno-sanitize=vptr)
Rye Mutt's avatar
Rye Mutt committed
      link_libraries(-lubsan)
    elseif (USE_THDSAN)
      add_compile_options(-fsanitize=thread)
Rye Mutt's avatar
Rye Mutt committed
    endif ()
Rye Mutt's avatar
Rye Mutt committed
  if (USE_ASAN OR USE_LEAKSAN OR USE_UBSAN OR USE_THDSAN)
    add_compile_options(-Og -fno-omit-frame-pointer)
Rye Mutt's avatar
Rye Mutt committed
  else ()
    add_compile_options(-O3 -ffast-math)
Rye Mutt's avatar
Rye Mutt committed
  endif ()
Rye Mutt's avatar
Rye Mutt committed

Rye Mutt's avatar
Rye Mutt committed
  if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND USE_LTO)
    add_link_options(-fuse-ld=lld)
Rye Mutt's avatar
Rye Mutt committed
  endif()

Rye Mutt's avatar
Rye Mutt committed
  # 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 ()
  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 ()
  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)
  CHECK_CXX_COMPILER_FLAG(-Wdeprecated-copy HAS_DEPRECATED_COPY)
  if (HAS_DEPRECATED_COPY)
    add_compile_options(-Wno-deprecated-copy)
  if (NOT DISABLE_FATAL_WARNINGS)
    add_compile_options(-Werror)
  endif (NOT DISABLE_FATAL_WARNINGS)
  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)
Rye Mutt's avatar
Rye Mutt committed
option(ENABLE_TIMING "Enable all fast timers" ON)
if(ENABLE_TIMING)
  add_compile_definitions(AL_ENABLE_ALL_TIMERS=1)
Rye Mutt's avatar
Rye Mutt committed
endif()