Skip to content
Snippets Groups Projects
Commit d37460fa authored by Sergey Kosukhin's avatar Sergey Kosukhin
Browse files

cmake: build Python interface

parent e302d840
No related branches found
No related tags found
1 merge request!56CMake support for the Python interface
...@@ -5,6 +5,15 @@ ...@@ -5,6 +5,15 @@
with section("parse"): # noqa: F821 with section("parse"): # noqa: F821
additional_commands = { additional_commands = {
"autoconf_configure": {
"pargs": 0,
"kwargs": {
"INPUTS": "+",
"OUTPUTS": "+",
"VARIABLES": "*",
"VALUES": "*",
},
},
"search_function_library": { "search_function_library": {
"pargs": 3, "pargs": 3,
"kwargs": {"OPTIONS": "*"}, "kwargs": {"OPTIONS": "*"},
......
...@@ -15,6 +15,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" ON) ...@@ -15,6 +15,7 @@ option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_TESTING "Build tests" ON) option(BUILD_TESTING "Build tests" ON)
option(MTIME_ENABLE_FORTRAN_HL "Enable the high-level Fortran interface" ON) option(MTIME_ENABLE_FORTRAN_HL "Enable the high-level Fortran interface" ON)
option(MTIME_ENABLE_PYTHON "Enable the Python interface" OFF)
option(MTIME_BUILD_EXAMPLES "Build examples" ON) option(MTIME_BUILD_EXAMPLES "Build examples" ON)
option(MTIME_BUILD_DOCS "Build documentation" OFF) option(MTIME_BUILD_DOCS "Build documentation" OFF)
...@@ -30,6 +31,13 @@ include(GNUInstallDirs) ...@@ -30,6 +31,13 @@ include(GNUInstallDirs)
add_subdirectory(src) add_subdirectory(src)
if(MTIME_ENABLE_PYTHON)
# Technically, we can produce the Python interface without the interpreter but
# we REQUIRE it for now for the consistency with the Autotools-based building:
find_package(Python REQUIRED COMPONENTS Interpreter)
add_subdirectory(python)
endif()
if(MTIME_BUILD_DOCS) if(MTIME_BUILD_DOCS)
add_subdirectory(doc) add_subdirectory(doc)
endif() endif()
......
# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
# ~~~
# autoconf_configure(INPUTS <inputs>,
# OUTPUTS <outputs>,
# VARIABLES <variables>,
# VALUES <values>)
# ~~~
# Iterates over the Autoconf templates from the <inputs> list, replaces the
# variable references from the <variables> list with the <values> (only the
# Autoconf-like @VAR@ forms) and stores the result to the respective files from
# the <outputs> list. The <inputs> and the <outputs> must have the same length.
# The <variables> and the <values> must have the same length. The function helps
# in keeping the scope of the main script free from variables needed for
# substitution only.
#
function(autoconf_configure)
cmake_parse_arguments(
PARSE_ARGV 0 ARG "" "" "INPUTS;OUTPUTS;VARIABLES;VALUES"
)
foreach(variable value IN ZIP_LISTS ARG_VARIABLES ARG_VALUES)
set(${variable} "${value}")
endforeach()
foreach(input output IN ZIP_LISTS ARG_INPUTS ARG_OUTPUTS)
configure_file(${input} ${output} @ONLY)
endforeach()
endfunction()
# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
set(module_dir_name mtime)
set(module_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/${module_dir_name})
set(module_binary_dir ${CMAKE_CURRENT_BINARY_DIR}/${module_dir_name})
add_library(mtime_py MODULE)
set_target_properties(
mtime_py
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_binary_dir}
OUTPUT_NAME __mtime
PREFIX ""
SUFFIX "${CMAKE_SHARED_MODULE_SUFFIX}"
)
target_link_libraries(mtime_py PRIVATE mtime_c)
include(AutoconfConfigure)
# Also create ${module_binary_dir}:
autoconf_configure(
INPUTS ${module_source_dir}/_mtime.py.in
OUTPUTS ${module_binary_dir}/_mtime.py
VARIABLES acx_modext
VALUES "${CMAKE_SHARED_MODULE_SUFFIX}"
)
file(
CREATE_LINK ${module_source_dir}/__init__.py ${module_binary_dir}/__init__.py
RESULT
dummy # just don't fail if something goes wrong (e.g. building in-source)
COPY_ON_ERROR SYMBOLIC
)
...@@ -54,7 +54,7 @@ set_target_properties( ...@@ -54,7 +54,7 @@ set_target_properties(
EXPORT_NAME mtime::mtime_c EXPORT_NAME mtime::mtime_c
) )
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS OR MTIME_ENABLE_PYTHON)
set_target_properties(mtime_c PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(mtime_c PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif() endif()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment