Skip to content

Commit bfebda0

Browse files
committed
Use CMake functions
1 parent 844b687 commit bfebda0

File tree

1 file changed

+69
-30
lines changed

1 file changed

+69
-30
lines changed

CMakeLists.txt

+69-30
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,113 @@
55
cmake_minimum_required(VERSION 3.14)
66
project(matplotplusplus VERSION 1.0.2)
77
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_C_STANDARD 11)
9+
set(MATPLOT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
10+
set(MATPLOT_VERSION ${CMAKE_PROJECT_VERSION})
811

12+
#######################################################
13+
### CMake Functions ###
14+
#######################################################
915
# CMake dependencies for installer
1016
include(CMakePackageConfigHelpers)
1117
include(GNUInstallDirs)
1218

13-
# To find or download packages
19+
# Append ./cmake directory to our include paths for the find_package scripts
1420
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
15-
message("CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}")
16-
option(CPM_USE_LOCAL_PACKAGES "Try `find_package` before downloading dependencies" ON)
17-
include(cmake/CPM.cmake)
18-
19-
# Check if this is a master project or a subdirectory of another project
20-
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
21-
set(MASTER_PROJECT ON)
22-
else ()
23-
set(MASTER_PROJECT OFF)
24-
endif ()
25-
set(MATPLOT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
2621

27-
# Check if we are in debug mode
28-
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
29-
set(DEBUG_MODE ON)
30-
set(NOT_DEBUG_MODE OFF)
31-
else()
32-
set(DEBUG_MODE OFF)
33-
set(NOT_DEBUG_MODE ON)
34-
endif()
22+
# Functions to find or download packages if we can't find_package
23+
include(FetchContent)
3524

36-
# Function to link external libraries as system libraries
37-
include (cmake/LinkExternalLibraries.cmake)
25+
# Our custom cmake functions
26+
include(cmake/functions.cmake)
3827

3928
#######################################################
40-
### Options ###
29+
### Declare options ###
4130
#######################################################
31+
# Set variables with project properties
32+
set_master_project_booleans() # detect if master project / dev mode
33+
set_debug_booleans() # detect if debug
34+
set_optimization_flags() # detect and set default optimization flags
35+
set_compiler_booleans() # detect compiler
36+
4237
# What to build
4338
option(BUILD_EXAMPLES "Build examples" ${MASTER_PROJECT})
4439
option(BUILD_TESTS "Build tests" ${MASTER_PROJECT})
4540
option(BUILD_INSTALLER "Build installer target" ${MASTER_PROJECT})
4641
option(BUILD_PACKAGE "Build package" ${MASTER_PROJECT})
4742

4843
# How to build
44+
option(BUILD_WITH_PEDANTIC_WARNINGS "Use pedantic warnings. This is useful for developers because many of these warnings will be in continuous integration anyway." ${DEBUG_MODE})
4945
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
46+
option(BUILD_WITH_SANITIZERS "Use pedantic warnings." ${DEBUG_MODE})
47+
48+
# MSVC hacks
49+
option(BUILD_WITH_MSVC_HACKS "Accept utf-8 in MSVC by default." ON)
50+
option(BUILD_WITH_UTF8 "Accept utf-8 in MSVC by default." ON)
51+
option(BUILD_WITH_EXCEPTIONS "Add compiler flags to use exceptions." ON)
52+
53+
# Features
5054
option(BUILD_HIGH_RESOLUTION_WORLD_MAP "Compile the high resolution maps for geoplots" ON)
5155
option(BUILD_FOR_DOCUMENTATION_IMAGES "Bypass show() commands and save figures as .svg at destruction" OFF)
5256
option(BUILD_EXPERIMENTAL_OPENGL_BACKEND "Compile target with the experimental OpenGL backend" OFF)
53-
option(BUILD_WITH_PEDANTIC_WARNINGS "Use pedantic warnings. This is useful for developers because many of these warnings will be in continuous integration anyway." ${DEBUG_MODE})
54-
option(BUILD_WITH_UTF8 "Accept utf-8 in MSVC by default." ON)
5557

5658
# Where to find dependencies
5759
option(WITH_SYSTEM_CIMG "Use system-provided CImg.h instead of bundled" OFF)
5860
option(WITH_SYSTEM_NODESOUP "Use system-provided nodesoup instead of bundled" OFF)
5961

62+
#######################################################
63+
### Apply global options ###
64+
#######################################################
65+
# In development, we can set some options for all targets
66+
if (MASTER_PROJECT)
67+
# Maybe add sanitizers to all targets
68+
if (BUILD_WITH_SANITIZERS AND NOT EMSCRIPTEN)
69+
add_sanitizers()
70+
endif ()
71+
72+
# Allow exceptions in MSVC
73+
if (MSVC AND BUILD_WITH_EXCEPTIONS)
74+
add_compile_options(/EHsc)
75+
endif ()
76+
77+
# Allow utf-8 in MSVC
78+
if (BUILD_WITH_UTF8 AND MSVC)
79+
set(CMAKE_CXX_FLAGS "/utf-8")
80+
endif ()
81+
82+
# MSVC hack to disable windows min/max
83+
# http://www.suodenjoki.dk/us/archive/2010/min-max.htm
84+
if (BUILD_WITH_MSVC_HACKS)
85+
# Check for min in Windows.h
86+
include(CheckSymbolExists)
87+
check_symbol_exists(min "Windows.h" HAVE_WINDOWS_MINMAX)
88+
if (HAVE_WINDOWS_MINMAX)
89+
add_compile_definitions(NOMINMAX)
90+
endif ()
91+
endif ()
92+
endif()
93+
6094
#######################################################
6195
### Libraries ###
6296
#######################################################
6397
add_subdirectory(source)
6498

6599
#######################################################
66-
### Examples and tests ###
100+
### Tests ###
67101
#######################################################
68-
if (BUILD_EXAMPLES)
69-
add_subdirectory(examples)
70-
endif ()
71-
72102
if (BUILD_TESTS)
103+
include(CTest)
104+
enable_testing()
73105
add_subdirectory(test)
74106
endif ()
75107

108+
#######################################################
109+
### Examples ###
110+
#######################################################
111+
if (BUILD_EXAMPLES)
112+
add_subdirectory(examples)
113+
endif ()
114+
76115
#######################################################
77116
### Installer ###
78117
#######################################################

0 commit comments

Comments
 (0)