forked from usgs/pestpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
177 lines (157 loc) · 5.49 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
# This is the top-level CMake file for PEST++
cmake_minimum_required(VERSION 3.9)
# Get PESTPP_VERSION from common source header
file(READ "${CMAKE_SOURCE_DIR}/src/libs/common/config_os.h" _file)
string(REGEX MATCH "PESTPP_VERSION \"([0-9\.]+)\"" _ ${_file})
set(PESTPP_VERSION ${CMAKE_MATCH_1})
if("${PESTPP_VERSION}" STREQUAL "")
message(SEND_ERROR
"Could not find PESTPP_VERSION in src/libs/common/config_os.h")
endif()
message(STATUS
"Configuring CMake ${CMAKE_VERSION} to build PESTPP ${PESTPP_VERSION}")
# This logic needs to be considered before project()
set(_change_MSVC_flags FALSE)
if(WIN32)
if(CMAKE_VERSION VERSION_LESS 3.15.0)
set(_change_MSVC_flags TRUE)
else()
# Set MSVC runtime to MultiThreaded (/MT)
cmake_policy(SET CMP0091 NEW)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif()
project(PESTPP
VERSION ${PESTPP_VERSION}
LANGUAGES CXX)
# Prevent running cmake in source dir
if("${PESTPP_SOURCE_DIR}" STREQUAL "${PESTPP_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed")
endif()
if(_change_MSVC_flags)
# Modify compile flags to change MSVC runtime from /MD to /MT
set(_re_match "([\\/\\-]M)D")
set(_re_replace "\\1T")
string(REGEX REPLACE ${_re_match} ${_re_replace}
CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE ${_re_match} ${_re_replace}
CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REGEX REPLACE ${_re_match} ${_re_replace}
CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE ${_re_match} ${_re_replace}
CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
string(REGEX REPLACE ${_re_match} ${_re_replace}
CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()
# Fortran is not required (anymore), but can be enabled using either:
# 1. ENABLE_Fortran=ON and use whatever default compiler is available
# 2. CMAKE_Fortran_COMPILER=ifort (or other compiler)
if(DEFINED ENABLE_Fortran)
set(_default_ENABLE_Fortran ${ENABLE_Fortran})
else()
if(DEFINED CMAKE_Fortran_COMPILER)
set(_default_ENABLE_Fortran ON)
else()
set(_default_ENABLE_Fortran OFF)
endif()
endif()
option(ENABLE_Fortran "Enable Fortran in PEST++" ${_default_ENABLE_Fortran})
if(ENABLE_Fortran)
enable_language(Fortran)
find_package(LAPACK REQUIRED)
message(STATUS "BLA_VENDOR=${BLA_VENDOR}")
message(STATUS "BLAS_LIBRARIES=${BLAS_LIBRARIES}")
message(STATUS "LAPACK_LIBRARIES=${LAPACK_LIBRARIES}")
set(Fortran_ENABLED TRUE)
else()
set(Fortran_ENABLED FALSE)
endif()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
message(STATUS "CMAKE_THREAD_LIBS_INIT=${CMAKE_THREAD_LIBS_INIT}")
# Ensure C++11 is used
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Use global "-fvisibility=hidden" see https://gcc.gnu.org/wiki/Visibility
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
# Global warning flags
set(PESTPP_CXX_WARN_FLAGS "${PESTPP_CXX_WARN_FLAGS}"
CACHE STRING "C++ flags used to compile PEST++ targets")
# Global definitions
if(WIN32)
add_compile_definitions(_WIN32_WINNT=0x0600)
endif()
option(BUILD_SHARED_LIBS "Build PEST++ shared (default is static, OFF)" OFF)
message(STATUS "BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}")
option(FORCE_STATIC "Force '-static' flag to link applications (default OFF)" OFF)
message(STATUS "FORCE_STATIC=${FORCE_STATIC}")
# Default install directories
include(GNUInstallDirs)
# Build main targets in src
add_subdirectory(src)
# Define an custom OS tag for CPACK_SYSTEM_NAME and (for now) local bin sub-directory
if(WIN32)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(_ostag "iwin")
else()
set(_ostag "win")
endif()
elseif(APPLE)
set(_ostag "mac")
elseif(UNIX)
set(_ostag "linux")
else()
set(_ostag "unknown")
endif()
# Detect if using a 32-bit compiler, show warning and modify _ostag
if(CMAKE_SIZEOF_VOID_P STREQUAL 4)
message(WARNING
"A 32-bit compiler has been detected. It is recommended that "
"PEST++ should be compiled using a 64-bit compiler.")
set(_ostag "${_ostag}32")
endif()
# Use a custom command to locally install binaries after compiling
# https://www.linux.com/training-tutorials/cmake-recipe-2-install-local-folder-build-dir-testing/
option(INSTALL_LOCAL "Install local executables in ./bin" ON)
if(INSTALL_LOCAL)
set(_OPTION_INSTALL_LOCAL_ALL ALL)
endif()
# Use an extra command to copy binaries into a sub-directory within bin -- please remove soon
if(WIN32)
set(_local_install_COMMAND_to_be_removed
cd "${PESTPP_SOURCE_DIR}\\bin" &&
md ${_ostag} &
copy /b pestpp-* ${_ostag}\\
)
else()
set(_local_install_COMMAND_to_be_removed
cd "${PESTPP_SOURCE_DIR}/bin" &&
rm -rf ${_ostag} &&
mkdir ${_ostag} &&
cp pestpp-* ${_ostag}/
)
endif()
add_custom_target(local_install
${_OPTION_INSTALL_LOCAL_ALL}
"${CMAKE_COMMAND}"
-D CMAKE_INSTALL_PREFIX=${PESTPP_SOURCE_DIR}
-P "${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake"
COMMAND ${_local_install_COMMAND_to_be_removed}
DEPENDS
pestpp-glm
pestpp-ies
pestpp-opt
pestpp-sen
pestpp-swp
COMMENT "Installing local executables to ${PESTPP_SOURCE_DIR}/bin"
)
# Packaging
string(TOLOWER ${PROJECT_NAME} CPACK_PACKAGE_NAME)
set(CPACK_PACKAGE_VERSION_MAJOR ${PESTPP_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PESTPP_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PESTPP_VERSION_PATCH})
set(CPACK_SYSTEM_NAME ${_ostag})
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
include(CPack)