From de7070a35c829cb100babf04715ecd779f6250c9 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 17 Aug 2022 16:23:22 +0200 Subject: [PATCH] CMake: Add compilation targets --- CMakeLists.txt | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..820b6e3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,86 @@ +# ------------------------------------------ +# User config between these lines + +# Set project name +set(PROJECT "ruc") + +# Unit tests +option(RUC_BUILD_TESTS "Build the RUC test programs" ON) + +# ------------------------------------------ + +cmake_minimum_required(VERSION 3.16 FATAL_ERROR) +project(${PROJECT} CXX) + +# ------------------------------------------ +# Setup C++ compiler + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Compiler flags used for all build types +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic") +# -Wall = All warnings about contructions that are easily avoidable +# -Wextra = Extra warning flags not covered by -Wall +# -Wpedantic = Warnings for compiler extensions not part of the standard + +# Set default build type if not specified +set(DEFAULT_BUILD_TYPE Release) +if(EXISTS ${CMAKE_SOURCE_DIR}/.git) + set(DEFAULT_BUILD_TYPE Debug) +endif() +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE}) +endif() + +# Set build type specific compiler flags +message("--- ${CMAKE_BUILD_TYPE} ---") +if(${CMAKE_BUILD_TYPE} STREQUAL Debug) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -g -pg") + # -Og = Optimizations that do not interfere with debugging + # -g = Produce debugging information in OS's native format + # -pg = Generate profile information for analysis with gprof + # $ gprof gmon.out > profile-data.txt +elseif(${CMAKE_BUILD_TYPE} STREQUAL Release) + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") + # -O3 = Optimizations that increase compilation time and performance +endif() + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# ------------------------------------------ +# Library target + +# Define library source files +file(GLOB_RECURSE LIBRARY_SOURCES "src/*.cpp") + +add_library(${PROJECT} STATIC ${LIBRARY_SOURCES}) +target_include_directories(${PROJECT} PRIVATE + "src") + +# ------------------------------------------ +# Unit test library target + +# Define source files +file(GLOB TEST_LIBRARY_SOURCES "test/*.cpp" "src/util/timer.cpp") + +add_library(${PROJECT}-test STATIC ${TEST_LIBRARY_SOURCES}) +target_include_directories(${PROJECT}-test PRIVATE + "src" + "test") + +# ------------------------------------------ +# Unit test target + +if (RUC_BUILD_TESTS) + # Define test source files + file(GLOB_RECURSE TEST_SOURCES "test/*.cpp") + set(TEST_SOURCES ${TEST_SOURCES} ${LIBRARY_SOURCES}) + list(REMOVE_ITEM TEST_SOURCES "${CMAKE_SOURCE_DIR}/src/main.cpp") + + add_executable(${PROJECT}-unit-test ${TEST_SOURCES}) + target_include_directories(${PROJECT}-unit-test PRIVATE + "src" + "test") +endif()