-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
537 lines (484 loc) · 17.2 KB
/
CMakeLists.txt
File metadata and controls
537 lines (484 loc) · 17.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
cmake_minimum_required (VERSION 3.23)
project(opm-common C CXX)
option(SIBLING_SEARCH "Search for other modules in sibling directories?" ON)
option(ENABLE_ECL_INPUT "Enable eclipse input support?" ON)
option(ENABLE_ECL_OUTPUT "Enable eclipse output support?" ON)
option(ENABLE_MOCKSIM "Build the mock simulator for io testing" ON)
option(OPM_ENABLE_PYTHON "Enable python bindings?" OFF)
option(OPM_INSTALL_PYTHON "Install python bindings?" ON)
option(OPM_ENABLE_EMBEDDED_PYTHON "Enable embedded python?" OFF)
option(OPM_ENABLE_DUNE "Enable code requiring dune-common?" ON)
macro(opm-common_prereqs_hook)
if(NOT TARGET cjson)
include(DownloadCjson)
endif()
if(TARGET fmt::fmt)
# OpmSatellites will not add the library, do it here.
list(APPEND opm-common_LIBRARIES fmt::fmt)
else()
include(DownloadFmt)
endif()
# If opm-common is configured to embed the python interpreter we must make sure
# that all downstream modules link libpython transitively. Due to the required
# integration with Python+cmake machinery provided by pybind11 this is done by
# manually adding to the opm-common_LIBRARIES variable here, and not in the
# OpmLibMain function. Here only the library dependency is implemented, the
# bulk of the python configuration is further down in the file.
if (OPM_ENABLE_PYTHON)
# Be backwards compatible.
if(PYTHON_EXECUTABLE AND NOT Python3_EXECUTABLE)
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
endif()
# We always need to search for Development as we use
# pybind11_add_module even if don't embed Python
if (OPM_ENABLE_EMBEDDED_PYTHON)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Embed Development.Module)
get_target_property(_lib_path Python3::Python IMPORTED_LOCATION)
set(PYTHON_LIBRARY ${_lib_path})
list(APPEND opm-common_LIBRARIES ${_lib_path})
else()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
endif()
if(Python3_VERSION_MINOR LESS 3)
# Python native namespace packages requires python >= 3.3
message(SEND_ERROR "OPM requires python >= 3.3 but only version ${Python3_VERSION} was found")
endif()
# Directory to install common (for opm modules) python scripts
set(OPM_PYTHON_COMMON_DIR "${CMAKE_INSTALL_DATAROOTDIR}/opm/python")
string(APPEND OPM_PROJECT_EXTRA_CODE_INTREE
"\n set(opm-common_PYTHON_COMMON_DIR ${PROJECT_SOURCE_DIR}/python)")
string(APPEND OPM_PROJECT_EXTRA_CODE_INSTALLED
"\n set(opm-common_PYTHON_COMMON_DIR ${CMAKE_INSTALL_PREFIX}/${OPM_PYTHON_COMMON_DIR})")
find_package(pybind11 CONFIG)
if (NOT pybind11_FOUND)
include(DownloadPyBind11)
endif()
endif()
if(OPM_ENABLE_DUNE)
find_package(dune-common REQUIRED)
endif()
endmacro()
macro(opm-common_config_hook)
if(ENABLE_ECL_INPUT)
string(APPEND OPM_PROJECT_EXTRA_CODE_INTREE "\n set(HAVE_ECL_INPUT 1)")
string(APPEND OPM_PROJECT_EXTRA_CODE_INSTALLED "\n set(HAVE_ECL_INPUT 1)")
set(HAVE_ECL_INPUT 1)
if(ENABLE_ECL_OUTPUT)
string(APPEND OPM_PROJECT_EXTRA_CODE_INTREE "\n set(HAVE_ECL_OUTPUT 1)")
string(APPEND OPM_PROJECT_EXTRA_CODE_INSTALLED "\n set(HAVE_ECL_OUTPUT 1)")
endif()
endif()
include(CheckIncludeFile)
check_include_file(fnmatch.h FNMATCH_H_FOUND)
if (FNMATCH_H_FOUND)
set(HAVE_FNMATCH_H 1)
endif()
if(dune-common_FOUND)
opm_need_version_of ("dune-common")
endif()
endmacro()
macro(opm-common_sources_hook)
if(ENABLE_ECL_INPUT)
# Keyword generation
include(GenerateKeywords.cmake)
# Append generated sources
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserInit.cpp)
foreach (name A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserKeywords/${name}.cpp)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserKeywords/ParserInit${name}.cpp)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/ParserKeywords/Builtin${name}.cpp)
endforeach()
if (OPM_ENABLE_EMBEDDED_PYTHON)
list(INSERT opm-common_SOURCES 0 ${PROJECT_BINARY_DIR}/python/cxx/builtin_pybind11.cpp)
endif()
endif()
set_source_files_properties(
opm/input/eclipse/Python/Python.cpp
PROPERTIES
COMPILE_FLAGS
-Wno-shadow
)
if (OPM_ENABLE_PYTHON)
# Set the path to the input docstrings.json file and the output .hpp file
set(PYTHON_DOCSTRINGS_FILE "${PROJECT_SOURCE_DIR}/python/docstrings_common.json")
set(PYTHON_DOCSTRINGS_GENERATED_HPP "${PROJECT_BINARY_DIR}/python/cxx/OpmCommonPythonDoc.hpp")
# Command to run the Python script
add_custom_command(
OUTPUT
${PYTHON_DOCSTRINGS_GENERATED_HPP}
COMMAND
$<TARGET_FILE:Python3::Interpreter>
${PROJECT_SOURCE_DIR}/python/generate_docstring_hpp.py
${PYTHON_DOCSTRINGS_FILE}
${PYTHON_DOCSTRINGS_GENERATED_HPP}
OPMCOMMONPYTHONDOC_HPP
"Opm::Common::DocStrings"
DEPENDS
${PYTHON_DOCSTRINGS_FILE}
COMMENT
"Generating OpmCommonPythonDoc.hpp from JSON file"
)
list(INSERT opm-common_SOURCES 0 ${PYTHON_DOCSTRINGS_GENERATED_HPP})
endif()
if(QuadMath_FOUND)
get_target_property(qm_defs QuadMath::QuadMath INTERFACE_COMPILE_DEFINITIONS)
if(qm_defs)
list(APPEND qm_defs HAVE_QUAD=1)
else()
set(qm_defs HAVE_QUAD=1)
endif()
get_target_property(qm_options QuadMath::QuadMath INTERFACE_COMPILE_OPTIONS)
set_source_files_properties(opm/material/components/CO2.cpp
opm/material/densead/Evaluation.cpp
PROPERTIES COMPILE_DEFINITIONS "${qm_defs}"
COMPILE_OPTIONS "${qm_options}")
endif()
endmacro()
macro(opm-common_files_hook)
if (OPM_ENABLE_PYTHON)
make_directory(${PROJECT_BINARY_DIR}/python)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${PROJECT_BINARY_DIR}/python)
set(opm-common_PYTHON_PACKAGE_VERSION ${OPM_PYTHON_PACKAGE_VERSION_TAG})
file(
COPY
${PROJECT_SOURCE_DIR}/python/README.md
${PROJECT_SOURCE_DIR}/python/MANIFEST.in
DESTINATION
${PROJECT_BINARY_DIR}/python
)
# Generate versioned setup.py
configure_file(${PROJECT_SOURCE_DIR}/python/setup.py.in
${PROJECT_BINARY_DIR}/python/setup.py.tmp)
file(
GENERATE
OUTPUT
${PROJECT_BINARY_DIR}/python/setup.py
INPUT
${PROJECT_BINARY_DIR}/python/setup.py.tmp
)
# -------------------------------------------------------------------------
# Let cmake configure some small shell scripts which can be used to simplify
# building, testing and installation of the Python extensions.
configure_file(python/setup-build.sh.in tmp/setup-build.sh)
file(
COPY
${PROJECT_BINARY_DIR}/tmp/setup-build.sh
DESTINATION
${PROJECT_BINARY_DIR}
FILE_PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
)
configure_file(python/setup-test.sh.in tmp/setup-test.sh)
file(
COPY
${PROJECT_BINARY_DIR}/tmp/setup-test.sh
DESTINATION
${PROJECT_BINARY_DIR}
FILE_PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
)
configure_file(python/setup-install.sh.in tmp/setup-install.sh)
file(
COPY
${PROJECT_BINARY_DIR}/tmp/setup-install.sh
DESTINATION
${PROJECT_BINARY_DIR}
FILE_PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
)
configure_file(python/enable-python.sh.in enable-python.sh)
endif()
endmacro()
macro(opm-common_tests_hook)
if(ENABLE_ECL_INPUT)
# Add the tests
opm_add_test(test_EclFilesComparator
CONDITION
ENABLE_ECL_INPUT AND TARGET Boost::unit_test_framework
SOURCES
tests/test_EclFilesComparator.cpp
test_util/EclFilesComparator.cpp
LIBRARIES
opmcommon
Boost::unit_test_framework
WORKING_DIRECTORY
${PROJECT_BINARY_DIR}/tests
)
opm_add_test(test_EclRegressionTest
CONDITION
ENABLE_ECL_INPUT AND TARGET Boost::unit_test_framework
SOURCES
tests/test_EclRegressionTest.cpp
test_util/EclFilesComparator.cpp
test_util/EclRegressionTest.cpp
LIBRARIES
opmcommon
Boost::unit_test_framework
WORKING_DIRECTORY
${PROJECT_BINARY_DIR}/tests
)
include(ExtraTests.cmake)
if(ENABLE_MOCKSIM AND TARGET Boost::unit_test_framework)
foreach(test test_msim test_msim_ACTIONX test_msim_EXIT)
opm_add_test(${test}
SOURCES
tests/msim/${test}.cpp
LIBRARIES
mocksim
opmcommon
Boost::unit_test_framework
WORKING_DIRECTORY
${PROJECT_BINARY_DIR}/tests
)
endforeach()
endif()
endif()
if(OPM_ENABLE_PYTHON)
add_test(
NAME
python_tests
WORKING_DIRECTORY
${PROJECT_BINARY_DIR}/python
COMMAND
${Python3_EXECUTABLE} -m unittest discover
)
endif()
endmacro()
macro(opm-common_targets_hook)
if(NOT cJSON_FOUND) # TODO: Remove once json is split out
target_sources(opmcommon PRIVATE $<TARGET_OBJECTS:cjson>)
target_include_directories(opmcommon PRIVATE ${CMAKE_BINARY_DIR}/_deps)
endif()
if(ENABLE_ECL_INPUT)
target_sources(compareECL
PRIVATE
test_util/EclFilesComparator.cpp
test_util/EclRegressionTest.cpp
)
if(ENABLE_MOCKSIM)
add_library(mocksim msim/src/msim.cpp)
opm_add_target_options(TARGET mocksim)
target_link_libraries(mocksim PUBLIC opmcommon)
target_include_directories(mocksim PUBLIC msim/include)
add_executable(msim examples/msim.cpp)
opm_add_target_options(TARGET msim)
target_link_libraries(msim PRIVATE mocksim)
endif()
list(APPEND opm-common_EXTRA_TARGETS compareECL rst_deck)
endif()
if(TARGET Boost::unit_test_framework)
foreach(test ACTIONX EmbeddedPython msim_ACTIONX PYACTION)
if(TEST ${test})
set_tests_properties(${test}
PROPERTIES
ENVIRONMENT_MODIFICATION
PYTHONPATH=path_list_prepend:${PROJECT_BINARY_DIR}/python
)
endif()
endforeach()
endif()
if(OPM_ENABLE_PYTHON)
add_custom_target(copy_python ALL
COMMAND
$<TARGET_FILE:Python3::Interpreter>
${PROJECT_SOURCE_DIR}/python/install.py
${PROJECT_SOURCE_DIR}/python
${PROJECT_BINARY_DIR}
0
)
pybind11_add_module(opmcommon_python
${PYTHON_CXX_SOURCE_FILES}
${PROJECT_BINARY_DIR}/python/cxx/builtin_pybind11.cpp)
target_link_libraries(opmcommon_python PRIVATE opmcommon pybind11::module)
opm_add_target_options(TARGET opmcommon_python)
set_target_properties(opmcommon_python
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY
python/opm
)
add_dependencies(opmcommon_python copy_python)
set_target_properties(opmcommon PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
if(OPM_ENABLE_EMBEDDED_PYTHON)
target_link_libraries(opmcommon PRIVATE $<BUILD_INTERFACE:Python3::Python>)
target_include_directories(opmcommon SYSTEM PRIVATE ${pybind11_INCLUDE_DIRS})
foreach(target opmcommon test_msim_ACTIONX EmbeddedPython PYACTION)
if(TARGET ${target})
target_compile_definitions(${target} PRIVATE EMBEDDED_PYTHON=1)
endif()
endforeach()
set_target_properties(opmcommon
PROPERTIES
EMBEDDED_PYTHON ON
EXPORT_PROPERTIES
EMBEDDED_PYTHON
)
endif()
if(dune-common_FOUND)
target_include_directories(opmcommon PRIVATE ${dune-common_INCLUDE_DIRS})
if(dune-common_VERSION VERSION_LESS 2.11)
# Explicitly link tests needing dune-common.
# To avoid pulling dune-common into the opm-common interface
target_include_directories(dunecommon INTERFACE ${dune-common_INCLUDE_DIRS})
string(REPLACE " " ";" dflags "${dune-common_CXX_FLAGS}")
list(FILTER dflags EXCLUDE REGEX std=c\\+\\+17)
target_compile_options(dunecommon INTERFACE ${dflags})
target_compile_definitions(dunecommon INTERFACE DUNE_COMMON_VERSION_MAJOR=${DUNE_COMMON_VERSION_MAJOR})
target_compile_definitions(dunecommon INTERFACE DUNE_COMMON_VERSION_MINOR=${DUNE_COMMON_VERSION_MINOR})
target_compile_definitions(dunecommon INTERFACE DUNE_COMMON_VERSION_REVISION=${DUNE_COMMON_VERSION_REVISION})
endif()
if(TARGET Boost::unit_test_framework)
foreach(src ${DUNE_TEST_SOURCE_FILES})
get_filename_component(tgt ${src} NAME_WE)
target_link_libraries(${tgt} PRIVATE dunecommon)
endforeach()
endif()
if(BUILD_EXAMPLES)
target_link_libraries(co2brinepvt PRIVATE dunecommon)
opm_add_target_options(TARGET co2brinepvt)
endif()
endif()
endmacro()
macro(opm-common_install_hook)
# Install build system files and documentation
install(
DIRECTORY
cmake
DESTINATION
${CMAKE_INSTALL_DATADIR}/opm
USE_SOURCE_PERMISSIONS
PATTERN
"OPM-CMake.md" EXCLUDE
)
install(
FILES
cmake/OPM-CMake.md
DESTINATION
${CMAKE_INSTALL_DOCDIR}
)
# Install tab completion skeleton
install(
FILES
etc/opm_bash_completion.sh.in
DESTINATION
${CMAKE_INSTALL_DATADIR}/opm/etc
)
install(
DIRECTORY
docs/man1
DESTINATION
${CMAKE_INSTALL_MANDIR}
FILES_MATCHING PATTERN
"*.1"
)
# Since the installation of Python code is nonstandard it is protected by an
# extra cmake switch, OPM_INSTALL_PYTHON. If you prefer you can still invoke
# setup.py install manually - optionally with the generated script
# setup-install.sh - and completely bypass cmake in the installation phase.
# If OPM_ENABLE_EMBEDDED_PYTHON is enabled, we also install opmcommon_python,
# to make it available in a Python console and for e.g. opm-simulators.
if (OPM_INSTALL_PYTHON OR OPM_ENABLE_EMBEDDED_PYTHON)
include(PyInstallPrefix)
install(
TARGETS
opmcommon_python
DESTINATION
${PYTHON_INSTALL_PREFIX}/opm
)
if (OPM_INSTALL_PYTHON)
install(
CODE
"execute_process(
COMMAND
$<TARGET_FILE:Python3::Interpreter>
python/install.py
${PROJECT_BINARY_DIR}/python/opm
${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}
1
)"
)
endif()
if (OPM_ENABLE_EMBEDDED_PYTHON)
install(
CODE
"execute_process(
COMMAND
$<TARGET_FILE:Python3::Interpreter>
python/install.py
${PROJECT_BINARY_DIR}/python/opm_embedded
${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX}
1
)"
)
endif()
# Need to install this Python script such that it can be used by
# opm-simulators when building against an installed opm-common
install(
PROGRAMS
python/install.py
DESTINATION
${OPM_PYTHON_COMMON_DIR}
)
endif()
# if OPM_ENABLE_EMBEDDED_PYTHON is true, then OPM_ENABLE_PYTHON is also automatically true
if (OPM_ENABLE_PYTHON OR OPM_INSTALL_PYTHON)
## Need to install this Python script such that it can be used
# by opm-simulators when building against an installed opm-common
install(
PROGRAMS
python/generate_docstring_hpp.py
DESTINATION
${OPM_PYTHON_COMMON_DIR}
)
install(
FILES
python/docstrings_common.json
DESTINATION
${OPM_PYTHON_COMMON_DIR}
)
endif()
endmacro()
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(OPM_MACROS_ROOT ${PROJECT_SOURCE_DIR})
# Output implies input
if(ENABLE_ECL_OUTPUT)
set(ENABLE_ECL_INPUT ON)
endif()
# And likewise, no input means no output
if(NOT ENABLE_ECL_INPUT)
set(ENABLE_ECL_OUTPUT OFF)
endif()
if(NOT OPM_ENABLE_PYTHON)
set(OPM_INSTALL_PYTHON OFF)
endif()
include(GNUInstallDirs)
# We need to define this variable in the installed cmake config file.
set(OPM_PROJECT_EXTRA_CODE_INSTALLED
"set(OPM_MACROS_ROOT ${CMAKE_INSTALL_FULL_DATADIR}/opm)
list(APPEND CMAKE_MODULE_PATH \${OPM_MACROS_ROOT}/cmake/Modules)
include(OpmPackage)"
)
set(OPM_PROJECT_EXTRA_CODE_INTREE
"set(OPM_MACROS_ROOT ${OPM_MACROS_ROOT})
list(APPEND CMAKE_MODULE_PATH \${OPM_MACROS_ROOT}/cmake/Modules)
include(OpmPackage)"
)
# project information is in dune.module. Read this file and set variables.
# we cannot generate dune.module since it is read by dunecontrol before
# the build starts, so it makes sense to keep the data there then.
include (OpmInit)
# Look for the opm-tests repository; if found the variable
# HAVE_OPM_TESTS will be set to true.
include(Findopm-tests)
# list of prerequisites for this particular project; this is in a
# separate file (in cmake/Modules sub-directory) because it is shared
# with the find module
include (${project}-prereqs)
if(OPM_ENABLE_EMBEDDED_PYTHON AND NOT OPM_ENABLE_PYTHON)
# This needs to be here to run before source_hook
message(WARNING "Inconsistent settings: OPM_ENABLE_PYTHON=OFF and "
"OPM_ENABLE_EMBEDDED_PYTHON=ON. OPM_ENABLE_EMBEDDED_PYTHON=ON now implies OPM_ENABLE_PYTHON=ON.")
set(OPM_ENABLE_PYTHON ON CACHE BOOL "Enable python bindings?" FORCE)
endif()
# all setup common to the OPM library modules is done here
include (OpmLibMain)