Skip to content

Commit 30e2446

Browse files
committed
rewite some parts of CMakeLists.txt
Signed-off-by: Nicolas Rol <[email protected]>
1 parent cf68d37 commit 30e2446

File tree

3 files changed

+132
-172
lines changed

3 files changed

+132
-172
lines changed

Diff for: metrix-simulator/CMakeLists.txt

+104-39
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
#
1+
#
22
# Copyright (c) 2021, RTE (http://www.rte-france.com)
33
# See AUTHORS.txt
44
# All rights reserved.
55
# This Source Code Form is subject to the terms of the Mozilla Public
66
# License, v. 2.0. If a copy of the MPL was not distributed with this
77
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
88
# SPDX-License-Identifier: MPL-2.0
9-
#
9+
#
1010

11+
# Minimal CMake required version
1112
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
1213

14+
# Include helper functions for creating config files
1315
include(CMakePackageConfigHelpers)
1416

15-
project(MetrixSimulator
17+
# Project description
18+
project(MetrixSimulator
1619
VERSION 7.4.0
1720
LANGUAGES C CXX)
1821

22+
# Explicitly activate policies
1923
cmake_policy(SET CMP0074 NEW) # Use <package>_ROOT variables to find package
24+
if(POLICY CMP0167)
25+
# The new version of this policy removes the FindBoost.cmake and uses the official Boost Cmake support
26+
# However it's only available after the version 1.70+ so, since we ask for version 1.66+, we need the old policy
27+
cmake_policy(SET CMP0167 OLD)
28+
endif()
2029

21-
message("Build directory : ${CMAKE_BINARY_DIR}")
22-
message("Source directory : ${CMAKE_SOURCE_DIR}")
30+
# Show build and source directories
31+
message("-- Build directory : ${CMAKE_BINARY_DIR}")
32+
message("-- Source directory : ${CMAKE_SOURCE_DIR}")
2333

34+
# Enable C++11 standards
2435
set(CMAKE_CXX_STANDARD 11)
2536
set(CMAKE_CXX_STANDARD_REQUIRED true)
2637

27-
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
28-
set(INSTALL_CMAKE_DIR cmake CACHE PATH "Installation directory for cmake files")
29-
38+
# Specify some parameters depending on if it's run on the compiler
3039
if(MSVC)
3140
add_definitions(-D_WIN32_WINNT=0x0A00)
3241
add_compile_definitions("_CRT_SECURE_NO_WARNINGS")
@@ -46,29 +55,69 @@ else()
4655
set(CMAKE_C_FLAGS_RELEASE "-O3")
4756
endif(MSVC)
4857

58+
# Add custom cmake modules to the path
59+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
60+
61+
# Specify where the cmake files will be installed
62+
set(INSTALL_CMAKE_DIR cmake CACHE PATH "Installation directory for cmake files")
63+
64+
# ==================================== Boost =================================================
4965
set(Boost_USE_STATIC_LIBS ON)
5066
set(Boost_USE_MULTITHREADED ON)
5167
find_package(Boost 1.66.0 REQUIRED COMPONENTS system program_options log filesystem)
5268

69+
# ================================ Sirius_solver =============================================
70+
# Add an option to use or not shared library for sirius solver
5371
option(USE_SIRIUS_SHARED "Use shared library for sirius solver" OFF)
5472
if (USE_SIRIUS_SHARED)
55-
set(SIRIUS_NAME sirius_solver)
73+
message("-- Use shared sirius_solver in ${sirius_solver_DIR}")
74+
set(SIRIUS_NAME sirius_solver)
5675
else()
57-
set(SIRIUS_NAME sirius_solver_static)
76+
message("-- Use static sirius_solver in ${sirius_solver_static_DIR}")
77+
set(SIRIUS_NAME sirius_solver_static)
5878
endif()
5979

80+
# Define already existing sirius installation path
6081
set(${SIRIUS_NAME}_ROOT ${CMAKE_CURRENT_BINARY_DIR}/external/sirius_solver CACHE PATH "Path where a Sirius installation already exists")
82+
83+
# Find the sirius_solver package
6184
find_package(${SIRIUS_NAME} REQUIRED)
6285

63-
# SUITESPARSE
64-
set(SUITESPARSE_HOME ${CMAKE_CURRENT_BINARY_DIR}/external/suitesparse CACHE PATH "Path where an SuiteSparse installation already exists")
65-
find_package(SuiteSparse REQUIRED)
86+
# ================================= SuiteSparse ==============================================
87+
# Add suitesparse cmake directory to the path
88+
set(SUITESPARSE_DIR ${CMAKE_CURRENT_BINARY_DIR}/external/suitesparse CACHE PATH "Path where an SuiteSparse installation already exists")
89+
list(APPEND CMAKE_PREFIX_PATH "${SUITESPARSE_DIR}/lib/cmake")
90+
91+
# Find the required SuiteSparse packages
92+
find_package(AMD REQUIRED)
93+
find_package(COLAMD REQUIRED)
94+
find_package(BTF REQUIRED)
95+
find_package(KLU REQUIRED)
6696

97+
# Find the actual library name
98+
find_library(SUITESPARSECONFIG_LIB
99+
NAMES suitesparseconfig SuiteSparse_config
100+
PATHS ${SUITESPARSE_DIR}/lib
101+
REQUIRED
102+
)
103+
104+
# Create an "imported" targer if it does not already exists
105+
if(NOT TARGET SuiteSparse::config)
106+
add_library(SuiteSparse::config UNKNOWN IMPORTED)
107+
set_target_properties(SuiteSparse::config PROPERTIES
108+
IMPORTED_LOCATION "${SUITESPARSECONFIG_LIB}"
109+
INTERFACE_INCLUDE_DIRECTORIES "${SUITESPARSE_DIR}/include"
110+
)
111+
endif()
112+
113+
# ================================= The rest ==============================================
114+
# Add the log directory to the build
67115
add_subdirectory(log)
68116

69117
message(STATUS "METRIX")
70118

71-
set(METRIX_HEADERS
119+
# Define required headers
120+
set(METRIX_HEADERS
72121
src/calcul.h
73122
src/err/error.h
74123
src/parametres.h
@@ -92,17 +141,18 @@ set(METRIX_HEADERS
92141
src/margin_variations_compute.h
93142
)
94143

95-
set(METRIX_SOURCES
144+
# Define required source files
145+
set(METRIX_SOURCES
96146
src/calculmacrofonctions.cpp
97-
src/err/error.cpp
98-
src/reseau.cpp
99-
src/calculrepports.cpp
100-
src/calculecrirecontraintesdodu.cpp
101-
src/connexite.cpp
102-
src/err/IoDico.cpp
147+
src/err/error.cpp
148+
src/reseau.cpp
149+
src/calculrepports.cpp
150+
src/calculecrirecontraintesdodu.cpp
151+
src/connexite.cpp
152+
src/err/IoDico.cpp
103153
src/calculeEmpilementGroupes.cpp
104154
src/metrix.cpp
105-
src/metrix2assess.cpp
155+
src/metrix2assess.cpp
106156
src/config/configuration.cpp
107157
src/config/input_configuration.cpp
108158
src/config/variant_configuration.cpp
@@ -115,38 +165,40 @@ set(METRIX_SOURCES
115165
src/margin_variations_compute.cpp
116166
)
117167

168+
# Define binary executable file name
118169
set(target "metrix-simulator")
119170

171+
# Generate a version.h file
120172
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/config/version.h)
121173

122-
add_executable(${target}
123-
${METRIX_HEADERS} ${METRIX_SOURCES})
174+
# Create the executable
175+
add_executable(${target}
176+
${METRIX_HEADERS} ${METRIX_SOURCES})
124177

178+
# Add the required directories for the executable
125179
target_include_directories(${target}
126180
PRIVATE
127181
metrix::log
128182
src
129183
${SIRIUS_NAME}
130-
SuiteSparse::SuiteSparse_KLU
131-
SuiteSparse::SuiteSparse_AMD
132-
SuiteSparse::SuiteSparse_COLAMD
133-
SuiteSparse::SuiteSparse_BTF
134-
SuiteSparse::SuiteSparse_Config
135184
${Boost_INCLUDE_DIRS}
136185
)
186+
187+
# Add the required libraries
137188
target_link_libraries(${target}
138189
PRIVATE
139190
metrix::log
140191
${SIRIUS_NAME}
141-
SuiteSparse::SuiteSparse_KLU
142-
SuiteSparse::SuiteSparse_AMD
143-
SuiteSparse::SuiteSparse_COLAMD
144-
SuiteSparse::SuiteSparse_BTF
145-
SuiteSparse::SuiteSparse_Config
192+
SuiteSparse::config
193+
SuiteSparse::AMD
194+
SuiteSparse::COLAMD
195+
SuiteSparse::BTF
196+
SuiteSparse::KLU
146197
${Boost_LIBRARIES}
147198
$<$<BOOL:${MSVC}>:msvcrt.lib>
148199
)
149200

201+
# Checks whether the shared library for Sirius is available and installs it
150202
if (USE_SIRIUS_SHARED)
151203
# Export sirius library if using shared
152204
get_target_property(sirius_PATH_LIB_RELEASE sirius_solver IMPORTED_LOCATION_RELEASE)
@@ -162,12 +214,13 @@ if (USE_SIRIUS_SHARED)
162214
install(FILES ${sirius_PATH_LIB} DESTINATION lib)
163215
endif()
164216

217+
# Option to enable code coverage (disabled by default)
165218
set(CODE_COVERAGE FALSE CACHE BOOL "Enable code coverage")
166219
if (CODE_COVERAGE)
167-
message(STATUS "CODE COVERAGE")
168-
set(CMAKE_BUILD_TYPE "Debug")
169-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
170-
include(cmake/CodeCoverage)
220+
message(STATUS "CODE COVERAGE") # Output message for code coverage
221+
set(CMAKE_BUILD_TYPE "Debug") # Switch to Debug build type
222+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") # Add current source to module path
223+
include(cmake/CodeCoverage) # Include custom code coverage script
171224
CODE_COVERAGE(NAME code-coverage
172225
OUTPUT_DIR coverage
173226
ROOT_DIR src
@@ -176,24 +229,36 @@ if (CODE_COVERAGE)
176229
)
177230
endif()
178231

232+
# Install SuiteSparse config files
233+
install(FILES "${SUITESPARSECONFIG_LIB}" DESTINATION lib)
234+
235+
# Install the executable to bin directory
179236
install(TARGETS ${target} EXPORT targets RUNTIME DESTINATION bin)
237+
# Install the CMake configuration files
180238
install(EXPORT targets
181239
FILE metrix-simulator-config.cmake
182240
DESTINATION ${INSTALL_CMAKE_DIR}
183241
)
242+
# Install 'etc' directory to the root of install
184243
install(DIRECTORY ${PROJECT_SOURCE_DIR}/etc DESTINATION .)
185244

245+
# Option to run a reduced scope of tests
186246
option(METRIX_RUN_ALL_TESTS "Run reduced scope of tests" ON)
187247

248+
# Enable CTest for running tests
188249
enable_testing()
189-
add_subdirectory(tests)
190250

251+
# Add the tests subdirectory, which contains the tests
252+
add_subdirectory(tests)
191253

254+
# Generate a basic package version file for the installed package
192255
write_basic_package_version_file(
193256
"${CMAKE_CURRENT_BINARY_DIR}/metrix-simulator-config.cmake"
194257
VERSION ${MetrixSimulator_VERSION}
195258
COMPATIBILITY SameMajorVersion
196259
)
260+
261+
# Install the generated version file to the CMake install directory
197262
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/metrix-simulator-config.cmake"
198263
DESTINATION ${INSTALL_CMAKE_DIR}
199264
)

Diff for: metrix-simulator/cmake/FindSuiteSparse.cmake

-122
This file was deleted.

0 commit comments

Comments
 (0)