Skip to content

Commit

Permalink
test compiling Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Dec 30, 2024
1 parent af7da3c commit 8a46a34
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 33 deletions.
61 changes: 48 additions & 13 deletions .github/workflows/cmake-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,53 @@ jobs:
build:
name: "Build on ${{ matrix.platform }} - ${{ matrix.build_type }}"
strategy:
fail-fast: false
matrix:
platform: [Windows, Ubuntu, macOS]
build_type: [Debug, Release]
runs-on: "${{ matrix.platform }}-latest"
fail-fast: false
matrix:
platform: [windows-latest, ubuntu-latest, macos-latest]
build_type: [Debug, Release]
runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v1
- if: ${{ matrix.platform == 'Ubuntu' }}
name: Create Build Environment
run: sudo apt-get update || true;
sudo apt-get install libgl1-mesa-dev mesa-common-dev libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev;
# Checkout the code
- uses: actions/checkout@v3

# Set up dependencies for each platform
- name: Install Dependencies (Ubuntu)
if: runner.os == 'Linux'
run: |
sudo apt-get update || true
sudo apt-get install -y cmake ninja-build libgl1-mesa-dev mesa-common-dev \
libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev
- name: Install Dependencies (macOS)
if: runner.os == 'macOS'
run: |
brew update || true
brew install cmake ninja
- name: Install Dependencies (Windows)
if: runner.os == 'Windows'
run: |
choco install cmake --installargs '"ADD_CMAKE_TO_PATH=System"'
choco install ninja
# Configure the project
- name: Configure
run: cmake -H"." -B"build/${{ matrix.build_type }}" -DEasy3D_BUILD_TESTS=ON;
- name: "Build on ${{ matrix.platform }} - ${{ matrix.build_type }}"
run: cmake --build "build/${{ matrix.build_type }}";
run: |
cmake -S . -B build/${{ matrix.build_type }} \
-G Ninja \
-DEasy3D_BUILD_PYTHON_BINDINGS=ON \
-DEasy3D_BUILD_TUTORIALS=ON \
-DEasy3D_BUILD_TESTS=ON
# Build the project
- name: Build
run: cmake --build build/${{ matrix.build_type }}

# Print environment and debug info
- name: Print Debug Info
run: |
echo "Platform: ${{ matrix.platform }}"
echo "Build Type: ${{ matrix.build_type }}"
cmake --version
ninja --version
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ if (Easy3D_BUILD_TESTS)
endif ()

if (Easy3D_BUILD_PYTHON_BINDINGS)
# Specify the output directory for the Python package
set(Easy3D_PYTHON_PACKAGE_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/python")
add_subdirectory(python)
endif ()

Expand Down Expand Up @@ -386,6 +388,9 @@ message(STATUS "----------------------------------------------------------------

message(STATUS " Build directory : ${Easy3D_BINARY_DIR}")
message(STATUS " Installation directory : ${CMAKE_INSTALL_PREFIX}")
if (Easy3D_BUILD_PYTHON_BINDINGS)
message(STATUS " Bindings directory : ${Easy3D_PYTHON_PACKAGE_DIR}")
endif ()

message(STATUS "****************************************************************************")

Expand Down
43 changes: 25 additions & 18 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,48 @@ endif ()

target_compile_definitions(${PROJECT_NAME} PRIVATE PYBIND11_SIMPLE_GIL_SAFE_ABI) # to use the Python Stable ABI

set(MODULE_NAME "PyEasy3D") # Sets the base name of the output file
target_compile_definitions(${PROJECT_NAME} PUBLIC "PyEasy3D_NAME=${MODULE_NAME}")

# Platform-specific suffix for Python extensions
if (WIN32)
set(SUFFIX ".pyd")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(MODULE_SUFFIX ".pyd")
else()
set(SUFFIX ".so")
set(MODULE_SUFFIX ".so")
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ${SUFFIX} FOLDER "python")
# Set the target properties with the computed suffix
set_target_properties(${PROJECT_NAME} PROPERTIES
OUTPUT_NAME ${MODULE_NAME} # Sets the base name of the output file
PREFIX "" # Avoid prefixes like 'lib'
SUFFIX "${MODULE_SUFFIX}" # Use the computed suffix
FOLDER "python"
)



# The following commands are actually not necessary for generating bindings.
# They are here to make it easier for the installation of the generated Python module.

# Specify the output directory for the Python package
set(PYTHON_PACKAGE_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/python/easy3d")

# Ensure the directory exists
file(MAKE_DIRECTORY ${PYTHON_PACKAGE_DIR})
file(MAKE_DIRECTORY ${Easy3D_PYTHON_PACKAGE_DIR}/easy3d)

# Generate the __init__.py file
file(WRITE ${PYTHON_PACKAGE_DIR}/__init__.py
"# Alias for the PyEasy3D module\n"
# Generate the __init__.py file for the Python package, which imports the compiled module
file(WRITE ${Easy3D_PYTHON_PACKAGE_DIR}/easy3d/__init__.py
"# Alias for the ${MODULE_NAME} module\n"
"try:\n"
" from .PyEasy3D import *\n"
" from .${MODULE_NAME} import *\n"
"except ImportError:\n"
" from PyEasy3D import *\n"
" from ${MODULE_NAME} import *\n"
)

# Path to the compiled PyEasy3D module
set(COMPILED_MODULE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${PROJECT_NAME}.so")
# Path to the compiled module
set(COMPILED_MODULE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${MODULE_NAME}${MODULE_SUFFIX}")

# Post-build command to copy compiled PyEasy3D module to the Python package directory
# Post-build command to copy compiled module to the Python package directory
add_custom_command(
TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${PYTHON_PACKAGE_DIR}/
COMMENT "Copying PyEasy3D module to Python package"
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${Easy3D_PYTHON_PACKAGE_DIR}/easy3d/
COMMENT "Copying ${MODULE_NAME} module to Python package"
)

# Copy setup.py to the Python package directory
Expand Down
3 changes: 2 additions & 1 deletion python/bindings/easy3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ void bind_video(pybind11::module_ &m) {
#endif


PYBIND11_MODULE(PyEasy3D, root_module) {
// PyEasy3D_NAME is a preprocessor definition (defined in the CMakeList), which sets the name of the Python module.
PYBIND11_MODULE(PyEasy3D_NAME, root_module) {
root_module.doc() = "Python bindings for Easy3D";

#define EASY3D_FLAT_BINDING
Expand Down
2 changes: 1 addition & 1 deletion python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ setup(
name="easy3d",
version="@CONF_Easy3D_VERSION@",
packages=["easy3d"],
package_data={"easy3d": ["PyEasy3D.so"]},
package_data={"easy3d": ["@MODULE_NAME@@MODULE_SUFFIX@"]},
include_package_data=True,
)

0 comments on commit 8a46a34

Please sign in to comment.