Skip to content

Commit

Permalink
Draft CMake for Cython
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Apr 15, 2024
1 parent 4efe427 commit 644e9b8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v3

- name: Install dependencies
run: sudo apt-get install -y libeigen3-dev libarpack2-dev libgtest-dev libfmt-dev libcxxopts-dev
run: sudo apt-get install -y libeigen3-dev libarpack2-dev libgtest-dev libfmt-dev libcxxopts-dev build-essential python3-dev cython

- name: Configure
run: cmake -B ${{github.workspace}}/build -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v3

- name: Install dependencies
run: brew install arpack eigen googletest fmt cxxopts
run: brew install arpack eigen googletest fmt cxxopts cython

- name: Configure
run: cmake -B ${{github.workspace}}/build -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
channels: conda-forge

- name: Install conda packages
run: conda install gtest eigen fmt cxxopts
run: conda install gtest eigen fmt cxxopts cython

- name: Configure
run: cmake -B ${{github.workspace}}/build -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
Expand Down
51 changes: 51 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@ if (BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# Define the macro add_cython_target
macro(add_cython_target _targetName _sourceFile _outputLang)
# Define the output file based on the desired language
set(_outputExt "cpp")
if("${_outputLang}" STREQUAL "C")
set(_outputExt "c")
endif()

# Set the output source file name
set(_generatedSource "${CMAKE_CURRENT_BINARY_DIR}/${_targetName}.${_outputExt}")

# Add a custom command to generate the C/C++ file from the Cython .pyx file
add_custom_command(
OUTPUT ${_generatedSource}
COMMAND ${CYTHON_EXECUTABLE} -${_outputLang} -o ${_generatedSource} ${_sourceFile}
DEPENDS ${_sourceFile}
COMMENT "Cythonizing ${_sourceFile} to ${_generatedSource}"
)

# Add the generated source file to the target
add_library(${_targetName} SHARED ${_generatedSource})
endmacro()

option(BUILD_PYTHON "Whether to build Python interface" ON)
if (BUILD_PYTHON)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
set(_pyx_compiled_source "${CMAKE_CURRENT_BINARY_DIR}/pytapkee.c")
set(_pyx_source "${CMAKE_CURRENT_SOURCE_DIR}/src/python/tapkee.pyx")
add_custom_command(
OUTPUT ${_pyx_compiled_source}
COMMAND ${Python3_EXECUTABLE} -m cython --cplus -o ${_pyx_compiled_source} ${_pyx_source}
DEPENDS ${_pyx_source}
COMMENT "Generating Cython files ${_pyx_compiled_source}"
)
set_source_files_properties(${_pyx_compiled_source} PROPERTIES LANGUAGE CXX)
add_library(pytapkee
SHARED
${_pyx_compiled_source}
)
set_target_properties(pytapkee PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(pytapkee PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(pytapkee PUBLIC Python3::Python)
target_include_directories(pytapkee
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/python
)
endif()

# library interface
file(GLOB_RECURSE headers_library "${TAPKEE_INCLUDE_DIR}/*.hpp")
add_library(tapkee_library INTERFACE)
Expand Down
Empty file added src/python/tapkee.cpp
Empty file.
4 changes: 4 additions & 0 deletions src/python/tapkee.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#cython: language_level=3

def embed():
return 0

0 comments on commit 644e9b8

Please sign in to comment.