diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index a89a872..085ace5 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -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}} diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index d547e08..b29a55d 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -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}} diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index b3adfd2..e05cffd 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -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}} diff --git a/CMakeLists.txt b/CMakeLists.txt index e6225f2..e43594a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 + $ + $ + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src/python + ) +endif() + # library interface file(GLOB_RECURSE headers_library "${TAPKEE_INCLUDE_DIR}/*.hpp") add_library(tapkee_library INTERFACE) diff --git a/src/python/tapkee.cpp b/src/python/tapkee.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/python/tapkee.pyx b/src/python/tapkee.pyx new file mode 100644 index 0000000..eb982a2 --- /dev/null +++ b/src/python/tapkee.pyx @@ -0,0 +1,4 @@ +#cython: language_level=3 + +def embed(): + return 0