TheOpenSpaceProgram / new-ospgl

A space exploration game in OpenGL. Devblog: https://tatjam.github.io/index.html
MIT License
43 stars 6 forks source link

CMake improvements #13

Open jonesmz opened 4 years ago

jonesmz commented 4 years ago

Here, use this CMakeLists.txt file as inspiration

##
# // Copyright information goes here
##
#####################################################################
# Minimum cmake version requirement
#####################################################################
CMAKE_MINIMUM_REQUIRED(VERSION 3.9)

#####################################################################
# Tell CMake this is a C++ project
#####################################################################
PROJECT(OpenSpaceProgram CXX)

#####################################################################
# Generate a compile_commands.json file that can be consumed by other tools.
#####################################################################
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#####################################################################
# Build system options
#####################################################################
OPTION(OSP_BUILD_SANATIZER      "Build with the address sanatizer" OFF)
OPTION(OSP_BUILD_DOXYGEN        "Build documentation" OFF)
OPTION(OSP_BOOST_STATIC_RUNTIME "Statically link with boost runtime" ON)

#####################################################################
# Preprocessor defines for code that needs to care about these build options.
#####################################################################
IF(OSP_BUILD_SANATIZER)
  ADD_DEFINITIONS(-DOSP_BUILD_SANATIZER)
ENDIF()

IF(OSP_BUILD_DOXYGEN)
  ADD_DEFINITIONS(-DOSP_BUILD_DOXYGEN)
ENDIF()

IF(OSP_BOOST_STATIC_RUNTIME)
  ADD_DEFINITIONS(-DOSP_BOOST_STATIC_RUNTIME)
ENDIF()

IF(CMAKE_BUILD_TYPE MATCHES "Release")
  ADD_DEFINITIONS(-DOSP_BUILD_RELEASE)
ENDIF()

IF(CMAKE_BUILD_TYPE MATCHES "Debug")
  ADD_DEFINITIONS(-DOSP_BUILD_DEBUG)
ENDIF()

IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  ADD_DEFINITIONS(-DOSP_BUILD_COMPILER_CLANG)
ENDIF()

IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  ADD_DEFINITIONS(-DOSP_BUILD_COMPILER_GCC)
ENDIF()

#####################################################################
# This variable can be used by other CMakeLists files to reference the directory structure.
#####################################################################
SET(OSP_ROOT ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")

#####################################################################
# Minimum language standard requirements.
#####################################################################
SET(CMAKE_C_STANDARD 11)
SET(CMAKE_C_EXTENSIONS OFF)
SET(CMAKE_C_STANDARD_REQUIRED ON)

SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

#####################################################################
# Static analysis options
#####################################################################
STRING(APPEND CMAKE_C_FLAGS   " -Wall -Wextra -fno-omit-frame-pointer")
STRING(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -fno-omit-frame-pointer")

#Turn on -Weverything, but then disable various noisy warnings.
STRING(APPEND CLANG_COMPILE_FLAGS " -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Wno-undefined-func-template -Wno-unused-template")
STRING(APPEND CLANG_COMPILE_FLAGS " -Wno-deprecated -Wno-documentation -Wno-documentation-deprecated-sync -Wno-documentation-unknown-command -Wno-abstract-vbase-init")
STRING(APPEND CLANG_COMPILE_FLAGS " -Wno-switch-enum -Wno-covered-switch-default -Wno-missing-prototypes -Wno-sign-conversion -Wno-float-conversion -Wno-shorten-64-to-32")
STRING(APPEND CLANG_COMPILE_FLAGS " -Wno-shadow -Wno-shadow-field-in-constructor -Wno-shadow-uncaptured-local -Wno-inconsistent-missing-destructor-override -Wno-extra-semi-stmt")
STRING(APPEND CLANG_COMPILE_FLAGS " -Wno-global-constructors -Wno-exit-time-destructors  -Wno-weak-vtables -Wno-undef -Wno-disabled-macro-expansion -Wno-used-but-marked-unused")
STRING(APPEND CLANG_COMPILE_FLAGS " -Wno-gnu-string-literal-operator-template")

STRING(APPEND GCC_COMPILE_FLAGS " -Wno-psabi")

IF(OSP_BUILD_SANATIZER)
  STRING(APPEND GCC_COMPILE_FLAGS " -fstack-protector-all -fsanitize-address-use-after-scope")
  STRING(APPEND GCC_COMPILE_FLAGS " -fsanitize=address -fsanitize-address-use-after-scope -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak")
  STRING(APPEND GCC_COMPILE_FLAGS " -fsanitize=undefined -fsanitize=shift-base -fsanitize=shift-exponent -fsanitize=integer-divide-by-zero -fsanitize=unreachable")
  STRING(APPEND GCC_COMPILE_FLAGS " -fsanitize=vla-bound -fsanitize=null -fsanitize=return -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=bounds-strict")
  STRING(APPEND GCC_COMPILE_FLAGS " -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=nonnull-attribute")
  STRING(APPEND GCC_COMPILE_FLAGS " -fsanitize=returns-nonnull-attribute -fsanitize=bool -fsanitize=enum -fsanitize=vptr -fsanitize=pointer-overflow -fsanitize=builtin")

  STRING(APPEND CLANG_COMPILE_FLAGS " -fstack-protector-all -fsanitize-address-use-after-scope")
  STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=address -fsanitize-address-use-after-scope -fsanitize=leak")
  STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=undefined -fsanitize=shift-base -fsanitize=shift-exponent -fsanitize=integer-divide-by-zero -fsanitize=unreachable")
  STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=vla-bound -fsanitize=null -fsanitize=return -fsanitize=signed-integer-overflow")
  STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=nonnull-attribute")
  STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=returns-nonnull-attribute -fsanitize=bool -fsanitize=enum -fsanitize=vptr -fsanitize=pointer-overflow -fsanitize=builtin")

  # Only activate object-size sanatizer on release builds. It has no effect on optimization level 0.
  IF(CMAKE_BUILD_TYPE MATCHES "Release")
    STRING(APPEND GCC_COMPILE_FLAGS   " -fsanitize=object-size")
    STRING(APPEND CLANG_COMPILE_FLAGS " -fsanitize=object-size")
  ENDIF()
ENDIF()

IF(CMAKE_C_COMPILER_ID MATCHES "Clang")
  STRING(APPEND CMAKE_C_FLAGS " ${CLANG_COMPILE_FLAGS}")
ENDIF()
IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  STRING(APPEND CMAKE_CXX_FLAGS " ${CLANG_COMPILE_FLAGS}")
ENDIF()

IF(CMAKE_C_COMPILER_ID MATCHES "GNU")
  STRING(APPEND CMAKE_C_FLAGS " ${GCC_COMPILE_FLAGS}")
ENDIF()
IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  STRING(APPEND CMAKE_CXX_FLAGS " ${GCC_COMPILE_FLAGS}")
ENDIF()

STRING(APPEND CMAKE_C_FLAGS_DEBUG   " -ggdb3")
STRING(APPEND CMAKE_CXX_FLAGS_DEBUG " -ggdb3")

STRING(APPEND CMAKE_C_FLAGS_DEBUG   " -gz -O0")
STRING(APPEND CMAKE_CXX_FLAGS_DEBUG " -gz -O0")

#####################################################################
# Link time optimization, results in faster code, and smaller binaries.
#####################################################################
IF(CMAKE_BUILD_TYPE MATCHES "Release")
  CMAKE_POLICY(SET CMP0069 NEW)
  INCLUDE(CheckIPOSupported)
  CHECK_IPO_SUPPORTED(RESULT lto_supported OUTPUT error)

  IF( lto_supported )
    MESSAGE(STATUS "IPO / LTO enabled")
    SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
    ADD_DEFINITIONS(-DOSP_BUILD_LTO)
  ELSE()
    MESSAGE(STATUS "IPO / LTO not supported: <${error}>")
  ENDIF()
ENDIF()

#####################################################################
# Doxygen documentation for developer reference.
#####################################################################
IF(OSP_BUILD_DOXYGEN)
  # check if Doxygen is installed
  FIND_PACKAGE(Doxygen)
  IF(DOXYGEN_FOUND)
    # set input and output files
    SET(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in)
    SET(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

    # request to configure the file
    CONFIGURE_FILE(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
    MESSAGE("Doxygen build started")

    # note the option ALL which allows to build the docs together with the application
    ADD_CUSTOM_TARGET(doc_doxygen ALL
                      COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
                      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                      COMMENT "Generating API documentation with Doxygen"
                      VERBATIM )
  ELSE(DOXYGEN_FOUND)
    MESSAGE("Doxygen need to be installed to generate the doxygen documentation")
  ENDIF(DOXYGEN_FOUND)
ENDIF()

#####################################################################
# Copy the Include What You Use mapping file (if it exists) that we have in the repository to the build directory.
#####################################################################
FILE (COPY "iwyu.imp" DESTINATION "${CMAKE_BINARY_DIR}")

#####################################################################
# Dependencies known to apply to more than one subproject.
#####################################################################
MESSAGE(STATUS "Configuring known common dependencies")

SET(Boost_USE_STATIC_LIBS   ON) # only find static libs
SET(Boost_USE_MULTITHREADED ON)

IF(OSP_BOOST_STATIC_RUNTIME)
  SET(Boost_USE_STATIC_RUNTIME ON)
ELSE()
  SET(Boost_USE_STATIC_RUNTIME OFF)
ENDIF()

FIND_PACKAGE(Boost 1.66.0 REQUIRED COMPONENTS system log program_options)

##
# If any networking stuff gets included
##
SET(OPENSSL_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(OpenSSL REQUIRED)

#####################################################################
# Subdirectories that contain code to compile.
#####################################################################
ADD_SUBDIRECTORY(3rdparty)
ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(exe)