|
| 1 | +# min required cmake version |
| 2 | +cmake_minimum_required(VERSION 3.24) |
| 3 | + |
| 4 | +include(InstallRequiredSystemLibraries) |
| 5 | + |
| 6 | +# RPATH configuration for portable/relocatable installs |
| 7 | +set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) |
| 8 | +if(UNIX AND NOT APPLE) |
| 9 | + # Use $ORIGIN-based RPATH for portable installs on Linux |
| 10 | + # $ORIGIN/../lib: executables in bin/ find libs in lib/ |
| 11 | + # $ORIGIN: shared libs in lib/ find peer libs in same directory |
| 12 | + set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib;$ORIGIN") |
| 13 | + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) |
| 14 | +else() |
| 15 | + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) |
| 16 | +endif() |
| 17 | + |
| 18 | +# Define the project name (NeTrainSim) and |
| 19 | +# the programming language used (CXX for C++) |
| 20 | +set(NeTrainSim_VERSION "0.1.3" CACHE STRING "Project version" FORCE) |
| 21 | +set(NETRAINSIM_NAME "NeTrainSim" CACHE STRING "Project name" FORCE) |
| 22 | +set(NETRAINSIM_VENDOR "(C) 2022-2023 Virginia Tech Transportation Institute - Center for Sustainable Mobility." CACHE STRING "Project vendor" FORCE) |
| 23 | +# Get the current date and time |
| 24 | +string(TIMESTAMP BUILD_DATE "%Y-%m-%d %H:%M:%S") |
| 25 | +# Set the BUILD_DATE variable |
| 26 | +set(BUILD_DATE ${BUILD_DATE} CACHE STRING "Project build time" FORCE) |
| 27 | + |
| 28 | +# Extract major, minor, and patch version from NeTrainSim_VERSION |
| 29 | +string(REPLACE "." ";" VERSION_LIST ${NeTrainSim_VERSION}) |
| 30 | +list(GET VERSION_LIST 0 NeTrainSim_VERSION_MAJOR) |
| 31 | +list(GET VERSION_LIST 1 NeTrainSim_VERSION_MINOR) |
| 32 | +list(GET VERSION_LIST 2 NeTrainSim_VERSION_PATCH) |
| 33 | + |
| 34 | +project(${NETRAINSIM_NAME} VERSION ${NeTrainSim_VERSION} LANGUAGES CXX) |
| 35 | + |
| 36 | +if(CMAKE_CONFIGURATION_TYPES) |
| 37 | + set(CMAKE_CONFIGURATION_TYPES "Debug;Release;MinSizeRel;RelWithDebInfo" CACHE STRING "" FORCE) |
| 38 | +endif() |
| 39 | +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 40 | + set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE) |
| 41 | + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") |
| 42 | +endif() |
| 43 | + |
| 44 | + |
| 45 | +# Set a default build type if none was specified |
| 46 | +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 47 | + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) |
| 48 | + # Set the possible values of build type for cmake-gui |
| 49 | + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") |
| 50 | +endif() |
| 51 | + |
| 52 | +# Compiler settings |
| 53 | +# Set the C++20 standard to be used for compiling |
| 54 | +set(CMAKE_CXX_STANDARD 23) |
| 55 | +# Ensure that the selected C++ standard is a |
| 56 | +# requirement for the compiler |
| 57 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 58 | +# Enable Qt's Automatic User Interface Compiler (UIC) |
| 59 | +set(CMAKE_AUTOUIC ON) |
| 60 | +# Enable Qt's Meta-Object Compiler (MOC) which allows |
| 61 | +# the use of Qt features such as signals and slots |
| 62 | +set(CMAKE_AUTOMOC ON) |
| 63 | +# Enable Qt's Resource Compiler (RCC) for compiling |
| 64 | +# resource files into binary format |
| 65 | +set(CMAKE_AUTORCC ON) |
| 66 | + |
| 67 | +# Platform-specific compiler flags |
| 68 | +if(MSVC) |
| 69 | + # MSVC-specific flags |
| 70 | + add_compile_options(/W4 /MP) |
| 71 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 72 | + add_compile_options(/Od /Zi) |
| 73 | + elseif(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 74 | + add_compile_options(/O2) |
| 75 | + endif() |
| 76 | +else() |
| 77 | + # GCC/Clang-specific flags |
| 78 | + add_compile_options(-Wall) |
| 79 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 80 | + add_compile_options(-O0 -g) |
| 81 | + elseif(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 82 | + add_compile_options(-O3) |
| 83 | + endif() |
| 84 | +endif() |
| 85 | + |
| 86 | +# Add definitions based on build type |
| 87 | +if (CMAKE_BUILD_TYPE STREQUAL "Release") |
| 88 | + add_definitions(-DQT_NO_DEBUG_OUTPUT) |
| 89 | +endif() |
| 90 | + |
| 91 | +# ------------------------------------------------------- |
| 92 | +# -------------- BUILD TYPE SUFFIXES -------------------- |
| 93 | +# ------------------------------------------------------- |
| 94 | +# Set output name suffixes for different build configurations |
| 95 | +# This ensures libraries/executables have unique names per build type: |
| 96 | +# Debug: *d (e.g., libNeTrainSimCored.so) |
| 97 | +# Release: (none) |
| 98 | +# RelWithDebInfo: *rd |
| 99 | +# MinSizeRel: *s |
| 100 | +set(CMAKE_DEBUG_POSTFIX "d") |
| 101 | +set(CMAKE_RELEASE_POSTFIX "") |
| 102 | +set(CMAKE_RELWITHDEBINFO_POSTFIX "rd") |
| 103 | +set(CMAKE_MINSIZEREL_POSTFIX "s") |
| 104 | + |
| 105 | +# ------------------------------------------------------- |
| 106 | +# --------------------- OPTIONS ------------------------- |
| 107 | +# ------------------------------------------------------- |
| 108 | + |
| 109 | +# Option to build GUI components |
| 110 | +option(BUILD_GUI "Build the GUI components" ON) |
| 111 | +# Cache the option so it's stored between runs |
| 112 | +set(BUILD_GUI ${BUILD_GUI} CACHE BOOL "Build the GUI components" FORCE) |
| 113 | + |
| 114 | +# Option to build Server components |
| 115 | +option(BUILD_SERVER "Build the SERVER components" ON) |
| 116 | +# Cache the option so it's stored between runs |
| 117 | +set(BUILD_SERVER ${BUILD_SERVER} CACHE BOOL "Build the SERVER components" FORCE) |
| 118 | + |
| 119 | +# Option to build the installer |
| 120 | +option(BUILD_INSTALLER "Build the installer" ON) |
| 121 | +set(BUILD_INSTALLER ${BUILD_INSTALLER} CACHE BOOL "Build the INSTALLER components" FORCE) |
| 122 | + |
| 123 | +# ------------------------------------------------------- |
| 124 | +# -------------------- Qt Paths ------------------------- |
| 125 | +# ------------------------------------------------------- |
| 126 | + |
| 127 | +# Allow the user to specify the Qt binary directory |
| 128 | +if(WIN32) |
| 129 | + set(QT_BIN_DIR "C:/Qt/6.4.2/msvc2019_64/bin" CACHE PATH "Path to the Qt binary directory") |
| 130 | +elseif(UNIX AND NOT APPLE) |
| 131 | + # Derive Qt paths from Qt6_DIR (set by user or find_package) |
| 132 | + if(DEFINED Qt6_DIR) |
| 133 | + get_filename_component(QT_PREFIX_DIR "${Qt6_DIR}/../../.." ABSOLUTE) |
| 134 | + set(QT_BIN_DIR "${QT_PREFIX_DIR}/bin" CACHE PATH "Path to the Qt binary directory") |
| 135 | + set(QT_LIB_DIR "${QT_PREFIX_DIR}/lib" CACHE PATH "Path to the Qt library directory") |
| 136 | + set(QT_PLUGINS_DIR "${QT_PREFIX_DIR}/plugins" CACHE PATH "Path to the Qt plugins directory") |
| 137 | + endif() |
| 138 | +endif() |
| 139 | + |
| 140 | +# ------------------------------------------------------- |
| 141 | +# --------------------- Libraries ----------------------- |
| 142 | +# --------------- Define Default Paths ------------------ |
| 143 | +# ------------------------------------------------------- |
| 144 | + |
| 145 | +if(BUILD_GUI) |
| 146 | + if(WIN32) |
| 147 | + set(KDREPORTS_DIR "C:/Program Files/KDAB/KDReports/cmake" CACHE PATH "Path to KDReports CMake directory") |
| 148 | + elseif(APPLE) |
| 149 | + set(KDREPORTS_DIR "/usr/local/KDAB/KDReports-2.3.95/lib/cmake/KDReports-qt6" CACHE PATH "Path to KDReports CMake directory") |
| 150 | + elseif(UNIX) |
| 151 | + set(KDREPORTS_DIR "/usr/local/KDAB/KDReports-2.3.95/lib/cmake/KDReports-qt6" CACHE PATH "Path to KDReports CMake directory") |
| 152 | + endif() |
| 153 | + |
| 154 | + # Use the KDReports CMake package |
| 155 | + find_package(KDReports-qt6 REQUIRED CONFIG PATHS ${KDREPORTS_DIR}) |
| 156 | +endif() |
| 157 | + |
| 158 | +if(BUILD_SERVER) |
| 159 | + if(WIN32) |
| 160 | + # Windows-specific paths |
| 161 | + set(CONTAINER_SEARCH_PATHS "C:/Program Files/Container/cmake" CACHE PATH "Default path to container's library") |
| 162 | + set(RABBITMQ_CMAKE_DIR "C:/Program Files/rabbitmq-c/lib/cmake/rabbitmq-c" CACHE PATH "Default path to RabbitMQ-C library on Windows") |
| 163 | + elseif(APPLE) |
| 164 | + # macOS-specific paths |
| 165 | + set(CONTAINER_SEARCH_PATHS "/usr/local/lib/cmake/Container" CACHE PATH "Default path to container's library on macOS") |
| 166 | + set(RABBITMQ_CMAKE_DIR "/usr/local/lib/rabbitmq-c/cmake" CACHE PATH "Default path to RabbitMQ-C library on macOS") |
| 167 | + elseif(UNIX) |
| 168 | + # Linux-specific paths |
| 169 | + set(CONTAINER_SEARCH_PATHS "/usr/local/lib/cmake/Container" CACHE PATH "Default path to container's library on Linux") |
| 170 | + set(RABBITMQ_CMAKE_DIR "/usr/local/lib/cmake/rabbitmq-c" CACHE PATH "Default path to RabbitMQ-C library on Linux") |
| 171 | + else() |
| 172 | + message(FATAL_ERROR "Unsupported platform. Please set paths for CONTAINER_CMAKE_DIR and RABBITMQ_CMAKE_DIR manually.") |
| 173 | + endif() |
| 174 | + |
| 175 | + |
| 176 | + # Find the installed Container library |
| 177 | + set(CONTAINER_CMAKE_DIR "${CONTAINER_SEARCH_PATHS}" CACHE PATH "Path to Container library's CMake files") |
| 178 | + |
| 179 | + find_package(Container REQUIRED PATHS ${CONTAINER_CMAKE_DIR} NO_DEFAULT_PATH) |
| 180 | + |
| 181 | + if (NOT Container_FOUND) |
| 182 | + message(FATAL_ERROR "Container not found. Please specify the correct path to the Container Library cmake installation.") |
| 183 | + endif() |
| 184 | + |
| 185 | + # Check if the directory exists |
| 186 | + if(NOT EXISTS "${CONTAINER_LIB_DIR}") |
| 187 | + message(FATAL_ERROR "The specified CONTAINER_LIB_DIR does not exist: ${CONTAINER_LIB_DIR}") |
| 188 | + endif() |
| 189 | + |
| 190 | + find_package(RabbitMQ-C REQUIRED CONFIG PATHS ${RABBITMQ_CMAKE_DIR}) |
| 191 | + |
| 192 | + if (NOT RabbitMQ-C_FOUND) |
| 193 | + message(FATAL_ERROR "RabbitMQ-C not found. Please specify the correct path to the RabbitMQ-C cmake installation.") |
| 194 | + endif() |
| 195 | + |
| 196 | + # Set and cache the path to the RabbitMQ bin directory using RABBITMQ_CMAKE_DIR |
| 197 | + if(WIN32) |
| 198 | + # For Windows, use /bin |
| 199 | + set(RABBITMQ_SHRD_LIB_DIR "${RABBITMQ_CMAKE_DIR}/../../../bin" CACHE PATH "Path to the RabbitMQ-C library's bin directory") |
| 200 | + elseif(UNIX AND NOT APPLE) |
| 201 | + # For Linux, use /lib or /lib64 (adjust based on your setup) |
| 202 | + set(RABBITMQ_SHRD_LIB_DIR "${RABBITMQ_CMAKE_DIR}/../../" CACHE PATH "Path to the RabbitMQ-C library's bin directory") |
| 203 | + elseif(APPLE) |
| 204 | + # For macOS, use /lib or /lib64 (adjust based on your setup) |
| 205 | + set(RABBITMQ_SHRD_LIB_DIR "${RABBITMQ_CMAKE_DIR}/../../" CACHE PATH "Path to the RabbitMQ-C library's bin directory") |
| 206 | + endif() |
| 207 | +endif() |
| 208 | + |
| 209 | + |
| 210 | +# ------------------------------------------------------- |
| 211 | +# ---------------- RULES AND SUB PROJECTS --------------- |
| 212 | +# ------------------------------------------------------- |
| 213 | + |
| 214 | +# include src directory |
| 215 | +add_subdirectory(src) |
| 216 | + |
| 217 | + |
0 commit comments