forked from CNES/aviso-lagrangian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
182 lines (155 loc) · 5.48 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# This file is part of lagrangian library.
#
# lagrangian is free software: you can redistribute it and/or modify
# it under the terms of GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# lagrangian is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of GNU Lesser General Public License
# along with lagrangian. If not, see <http://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 3.0)
include(CheckFunctionExists)
include(CheckCXXSourceRuns)
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
message(FATAL_ERROR "The build directory must be different from the \
root directory of this software.")
endif()
cmake_policy(SET CMP0048 NEW)
project(lagrangian LANGUAGES CXX)
if (POLICY CMP0063)
cmake_policy(SET CMP0063 NEW)
endif ()
if (POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif ()
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif ()
# CMake module search path
set(
CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/pybind11/tools"
"${CMAKE_MODULE_PATH}"
)
# By default, build type is set to release, with debugging information.
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RELWITHDEBINFO)
endif()
message("-- Build type: ${CMAKE_BUILD_TYPE}")
# The library must be built using C++17 compiler.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_MACOSX_RPATH 1)
include(CheckCXXCompilerFlag)
if(NOT WIN32)
check_cxx_compiler_flag("-std=c++17" HAS_CPP17_FLAG)
else()
check_cxx_compiler_flag("/std:c++17" HAS_CPP17_FLAG)
add_definitions (-D_USE_MATH_DEFINES)
add_definitions(-DBOOST_ALL_NO_LIB)
endif()
if(NOT HAS_CPP17_FLAG)
message(FATAL_ERROR "Unsupported compiler -- requires C++17 support!")
endif()
macro(check_cxx_compiler_and_linker_flags _RESULT _CXX_FLAGS _LINKER_FLAGS)
set(CMAKE_REQUIRED_FLAGS ${_CXX_FLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${_LINKER_FLAGS})
set(CMAKE_REQUIRED_QUIET FALSE)
check_cxx_source_runs("int main(int argc, char **argv) { return 0; }" ${_RESULT})
set(CMAKE_REQUIRED_FLAGS "")
set(CMAKE_REQUIRED_LIBRARIES "")
unset(_RESULT)
endmacro()
# Always use libc++ on Clang
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_cxx_compiler_and_linker_flags(
HAS_LIBCPP "-stdlib=libc++" "-stdlib=libc++")
if (HAS_LIBCPP)
string(APPEND CMAKE_CXX_FLAGS " -stdlib=libc++")
string(APPEND CMAKE_EXE_LINKER_FLAGS " -stdlib=libc++")
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -stdlib=libc++")
check_cxx_compiler_and_linker_flags(
HAS_LIBCPPABI "-stdlib=libc++" "-stdlib=libc++ -lc++abi")
if(HAS_LIBCPPABI)
string(APPEND CMAKE_EXE_LINKER_FLAGS " -lc++abi")
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -lc++abi")
endif()
endif()
check_cxx_compiler_and_linker_flags(
HAS_SIZED_DEALLOCATION "-fsized-deallocation" "")
if(HAS_SIZED_DEALLOCATION)
string(APPEND CMAKE_CXX_FLAGS " -fsized-deallocation")
endif()
endif()
if(NOT WIN32)
if(NOT CMAKE_CXX_FLAGS MATCHES "-Wall$")
string(APPEND CMAKE_CXX_FLAGS " -Wall")
endif()
if(NOT CMAKE_CXX_COMPILER MATCHES "icpc$" AND NOT CMAKE_CXX_FLAGS MATCHES "-Wpedantic$")
string(APPEND CMAKE_CXX_FLAGS " -Wpedantic")
endif()
endif()
if (MSVC)
# Disable warnings about using deprecated std::equal_to<>::result_type
add_definitions (-D_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING)
endif()
CHECK_FUNCTION_EXISTS(pow POW_FUNCTION_EXISTS)
if(NOT POW_FUNCTION_EXISTS)
unset(POW_FUNCTION_EXISTS CACHE)
list(APPEND CMAKE_REQUIRED_LIBRARIES m)
CHECK_FUNCTION_EXISTS(pow POW_FUNCTION_EXISTS)
if(POW_FUNCTION_EXISTS)
set(MATH_LIBRARY m CACHE STRING "" FORCE)
else()
message(FATAL_ERROR "Failed making the pow() function available")
endif()
endif()
# Python
find_package(PythonInterp REQUIRED)
execute_process(
COMMAND
${PYTHON_EXECUTABLE} -c [=[import os
import sysconfig
import sys
sys.stdout.write(os.path.dirname(sysconfig.get_config_h_filename()))
]=] OUTPUT_VARIABLE PYTHON_INCLUDE_DIR)
find_package(PythonLibs REQUIRED)
# Boost
find_package(Boost 1.63 REQUIRED COMPONENTS date_time)
include_directories(${Boost_INCLUDE_DIRS})
# Udunits2
find_package(UDUNITS2 REQUIRED)
include_directories(${UDUNITS2_INCLUDES})
# NetCDF
find_package(NetCDF 4.1.1 REQUIRED)
include_directories(${NETCDF_INCLUDE_DIR})
# Pybind11
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/pybind11)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# NETCDF-CXX4
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/netcdf-cxx4/cxx4)
file(GLOB_RECURSE NCXX4_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/netcdf-cxx4/cxx4/nc*.cpp")
add_library(ncxx4 STATIC ${NCXX4_SOURCES})
# Internal library
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/include)
file(GLOB_RECURSE LIB_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/lib/*.cpp")
add_library(lagrangian STATIC ${LIB_SOURCES})
# Python wrapper
file(GLOB_RECURSE WRAPPER_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/core/*.cpp")
pybind11_add_module(core ${WRAPPER_SOURCES})
target_link_libraries(core PRIVATE lagrangian ncxx4
Boost::date_time
${NETCDF_LIBRARIES}
${LIBXML2_LIBRARIES}
${UDUNITS2_LIBRARIES})