Skip to content

Compile for Mac #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Build folders
build/
build_osx/
cmake-build-debug/
cmake-build-release/

# IDEs
.idea/

#######################################
## C++
#######################################
Expand Down Expand Up @@ -259,3 +268,6 @@ cython_debug/
#######################################
# get rid of output folder
_build/

# macos
.DS_Store
88 changes: 64 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ cmake_minimum_required(VERSION 3.16.)
project(diffCheck VERSION 1.3.0 LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 17)

# Force usage of libc++ and proper visibility on macOS
# For some reason, without this, there will be a segfault in test
if(APPLE)
add_compile_options(-stdlib=libc++ -fvisibility=hidden -Wall)
add_link_options(-stdlib=libc++)
endif()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

include(external_tools)
Expand Down Expand Up @@ -40,28 +47,38 @@ endif()
set(SHARED_LIB_NAME diffCheck)

file(GLOB_RECURSE SOURCES_LIB
src/diffCheck.hh # diffCheck interface
src/diffCheck/*.cc src/diffCheck/*.hh # diffCheck src
)

add_library(${SHARED_LIB_NAME} SHARED ${SOURCES_LIB})
src/diffCheck.hh # diffCheck interface
src/diffCheck/*.cc src/diffCheck/*.hh # diffCheck src
)

# For some reason, on macOS, we need to compile the library as static
# I wonder if it's a good idea to also compile the library as static on Windows
# if (APPLE)
# add_library(${SHARED_LIB_NAME} STATIC ${SOURCES_LIB})
# else()
add_library(${SHARED_LIB_NAME} STATIC ${SOURCES_LIB})
# endif()

if (WIN32)
set_target_properties(${SHARED_LIB_NAME} PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS TRUE
)
endif()
set_target_properties(${SHARED_LIB_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin # for dll
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib # for lib
)

set_target_properties(${SHARED_LIB_NAME} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

target_include_directories(${SHARED_LIB_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src
)

#set the MD_DynamicRelease flag for MSVC since we are compiling with /MD for py wrap
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
)

# set the MD_DynamicRelease flag for MSVC since we are compiling with /MD for py wrap
if (WIN32)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

#--------------------------------------------------------------------------
# 3rd party
Expand All @@ -73,14 +90,15 @@ add_subdirectory(deps/eigen)
target_link_libraries(${SHARED_LIB_NAME} PUBLIC Eigen3::Eigen)

# Open3D (pre-built binaries) ---------------------------------------------
download_submodule_project(open3d)
set(Open3D_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/open3d/win/0_18/CMake)
# download_submodule_project(open3d)
if (WIN32)
set(Open3D_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/open3d/win/0_18/CMake)
elseif (APPLE)
set(Open3D_DIR ${CMAKE_CURRENT_SOURCE_DIR}/deps/open3d/mac/open3d-devel-darwin-arm64-0.18.0/lib/cmake/Open3D)
endif()
find_package(Open3D 0.18.0 REQUIRED)

# print the version debug or release of the package
message(STATUS "Open3D version: ${Open3D_VERSION}"
"Open3D include dir: ${Open3D_INCLUDE_DIRS}"
"Open3D library dir: ${Open3D_LIBRARIES}")
# find_package(Open3D HINTS ${CMAKE_INSTALL_PREFIX}/lib/cmake)

# link the release version of the open3d library
target_link_libraries(${SHARED_LIB_NAME} PUBLIC Open3D::Open3D)
Expand All @@ -97,6 +115,9 @@ if(WIN32)
endif()
endif()




# Boost (header only) -----------------------------------------------------
download_submodule_project(boost)
target_include_directories(${SHARED_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/deps/boost/win/1_89/include/boost-1_85)
Expand Down Expand Up @@ -128,15 +149,18 @@ target_link_libraries(${APP_NAME_EXE} ${SHARED_LIB_NAME})

target_include_directories(${APP_NAME_EXE}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src
)
)

#--------------------------------------------------------------------------
# pybind11
#--------------------------------------------------------------------------
add_definitions(-D_GLIBCXX_DEBUG)

if (BUILD_PYTHON_MODULE)
set(PYBINDMODULE_NAME diffcheck_bindings)
set(PYPI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/gh/diffCheck/diffCheck)
set(TARGET_DLL_PYPI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/gh/diffCheck/diffCheck/dlls)
set(TARGET_SO_PYPI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/gh/diffCheck/diffCheck)
set(SPHINX_DOC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/doc)

download_submodule_project(pybind11)
Expand All @@ -151,27 +175,43 @@ if (BUILD_PYTHON_MODULE)

set(PYBIND11_PYTHON_VERSION 3.9.10)

pybind11_add_module(${PYBINDMODULE_NAME} src/diffCheckBindings.cc)
pybind11_add_module(${PYBINDMODULE_NAME}
MODULE
src/diffCheckBindings.cc
)

target_link_libraries(${PYBINDMODULE_NAME} PUBLIC ${SHARED_LIB_NAME})
target_include_directories(${PYBINDMODULE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(${PYBINDMODULE_NAME} PUBLIC ${SHARED_LIB_NAME})

# copy the pyd file to the pypi directory
add_custom_command(TARGET ${PYBINDMODULE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:${PYBINDMODULE_NAME}>
${PYPI_DIR}
)
copy_dlls(${TARGET_DLL_PYPI_DIR} ${PYBINDMODULE_NAME})
# copy the pyd/dlls for the sphinx documentation
add_custom_command(TARGET ${PYBINDMODULE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:${PYBINDMODULE_NAME}>
${SPHINX_DOC_DIR}
)
copy_dlls(${SPHINX_DOC_DIR} ${PYBINDMODULE_NAME})

if (WIN32)
copy_dlls(${TARGET_DLL_PYPI_DIR} ${PYBINDMODULE_NAME})
copy_dlls(${SPHINX_DOC_DIR} ${PYBINDMODULE_NAME})
elseif (APPLE)
copy_so_files(${TARGET_SO_PYPI_DIR} ${PYBINDMODULE_NAME})
copy_so_files(${SPHINX_DOC_DIR} ${PYBINDMODULE_NAME})
endif()
endif()

# install the diffCheck shared library to the system path
install(TARGETS ${SHARED_LIB_NAME}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
)

#--------------------------------------------------------------------------
# Tests
#--------------------------------------------------------------------------
Expand Down
39 changes: 39 additions & 0 deletions cmake/activate_conda.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/zsh
########################################################################
# check if conda is available
# check that diff_check environment is available
# activate it
########################################################################

# Check if conda command is available
echo "Checking if conda command is available..."
if ! command -v conda >/dev/null 2>&1; then
echo "conda command not found, you won't be able to build the python wrap for diffcheck. Please install Anaconda or Miniconda first."
exit 1
fi
echo "Conda command is available."

# Check if the diff_check environment is available
if ! conda env list | grep -q 'diff_check'; then
echo "diff_check environment not found, you should create one by running:"
echo "$ conda env create -f environment.mac.yml"
exit 1
else
echo "diff_check environment is available, updating it now..."
if conda env update --name diff_check --file environment.mac.yml --prune; then
echo "Environment updated successfully."
else
echo "Failed to update diff_check environment, please check the environment.mac.yml file."
exit 1
fi
fi
echo "diff_check environment is up to date."

# Check if a different environment than diff_check is activated, if so deactivate it
active_env_name=$(conda info --envs | awk '/\*/ {print $1}')
if [[ "$active_env_name" != "diff_check" && -n "$active_env_name" ]]; then
echo "You should deactivate $active_env_name first with 'conda deactivate' and 'conda activate diff_check' before running this script."
exit 1
fi

echo "You can start the cmake config now ..."
5 changes: 5 additions & 0 deletions cmake/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# activate the conda diff_check environment otherwise the python wrap won't work
call cmake/activate_conda.sh

#configure the project
conda run --name diff_check --no-capture-output cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
10 changes: 10 additions & 0 deletions cmake/external_tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,14 @@ function (copy_dlls directory_to_copy_dlls post_build_target)
${directory_to_copy_dlls}
)
endforeach()
endfunction()

# ------------------------------------------------------------------------------
function (copy_so_files directory_to_copy_so_files post_build_target)
message (STATUS "Erasing old SO files and copy new ones to ${directory_to_copy_so_files}")
file(GLOB files ${directory_to_copy_so_files}/*.so)
foreach(file ${files})
message(STATUS "Removing ${file}")
file(REMOVE ${file})
endforeach()
endfunction()
27 changes: 19 additions & 8 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ copy_dlls(${TEST_OUT_DIR_BINARY} ${CPP_UNIT_TESTS})
# ------------------------------------------------------------------------------
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

# find python executable
message(STATUS "Python3_EXECUTABLE: ${Python3_EXECUTABLE}")

# find python include dir
message(STATUS "Python3_INCLUDE_DIRS: ${Python3_INCLUDE_DIRS}")

# find python library
message(STATUS "Python3_LIBRARIES: ${Python3_LIBRARIES}")

set(PYTHON_EXECUTABLE /opt/homebrew/Caskroom/miniconda/base/bin/python)

# copying pyd and dlls
set(TARGET_PYBIND_TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests/integration_tests/pybinds_tests)
add_custom_command(TARGET ${PYBINDMODULE_NAME} POST_BUILD
Expand All @@ -55,11 +66,11 @@ add_test(NAME PYBIND_UNIT_TEST
# Run all tests
# ------------------------------------------------------------------------------
# FIXME: the post build has some problems if the tests are failing MSB3073
if (RUN_TESTS)
add_custom_command(
TARGET ${CPP_UNIT_TESTS} POST_BUILD #TODO: <== this should be set to the latest test suite
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> --verbose
COMMENT "Running all tests"
)
endif()
#if (RUN_TESTS)
# add_custom_command(
# TARGET ${CPP_UNIT_TESTS} POST_BUILD #TODO: <== this should be set to the latest test suite
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
# COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> --verbose
# COMMENT "Running all tests"
# )
#endif()
2 changes: 1 addition & 1 deletion deps/eigen
Submodule eigen updated from 8b4efc to 1e6570
1 change: 1 addition & 0 deletions deps/open3d_win
Submodule open3d_win added at 7f83dd
2 changes: 1 addition & 1 deletion deps/pybind11
Submodule pybind11 updated 230 files
40 changes: 40 additions & 0 deletions dev_log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Deploy on Mac dev log
## Current status
- The pybind part can be successfully compiled and run on mac, within Grasshopper
- Some functions do not work; Grasshopper would crash without any information once the function is called (which is also very hard to debug). Based on the current experiment, these functions are problematic:
- `dfb_segmentation.DFSegmentation.associate_clusters`

## Run
To just run on Mac (using the pre-compiled stuff), please navigate to the folder `/portability_experiment`.
If you want to compile, follow the instruction below:

1. Make sure that `/deps/open3d/mac/open3d-devel-darwin-arm64-0.18.0/lib/cmake/Open3D` exist.
2. Run the cmake & build as usual (make sure that you're using python3.9!)
```
mkdir build_mac
cd build_mac
cmake .. -DRUN_TESTS=OFF
make -j
```
3. After building, you should see this file `/src/gh/diffCheck/diffCheck/diffcheck_bindings.cpython-39-darwin.so`
4. Go to `src/gh/diffCheck` and build the wheel, and you should see the `diffcheck-1.3.0-py3-none-any.whl` appear in a sub-folder `dist`.
```
cd ..
cd src/gh/diffCheck
python setup.py bdist_wheel
```
4. Install diffCheck to Grasshopper's python
```
/Users/petingo/.rhinocode/py39-rh8/python3.9 -m pip install dist/diffcheck-1.3.0-py3-none-any.whl --force-reinstall
```
5. You should be able to use diffCheck in Grasshopper's python now.

## Changes to mac build
- Everything in `diffcheck_binding` is now a part of `diffCheck` Instead of doing
```
from diffcheck_binding import xxx
```
You need to change it to
```
from diffCheck import xxx
```
7 changes: 6 additions & 1 deletion doc/development_env.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
If you develop for DF, you need to set up your development environment. This guide will help you to do that. Wether you are developing for the `c++` or `python` part of the project, you will find the necessary information here.

## Prepare your environment

### Windows
Before to start, especially if you used diffCheck as an end-user before you will need to:

1. Make sure to have `camke` installed on your machine. You can download it [here](https://cmake.org/download/).
Expand Down Expand Up @@ -50,6 +50,11 @@ Before to start, especially if you used diffCheck as an end-user before you will
For your info the packages is installed in `C:\Users\andre\.rhinocode\py39-rh8\Lib\site-packages`.
```

### Mac
```
/Users/petingo/.rhinocode/py39-rh8/python3.9 -m pip install -e "/Users/petingo/p/diffCheck/src/gh/diffCheck"
```

That's it you are now a contributor to the diffCheck! We raccomand to not download anymore from yak package but rather use the source code in the repository. If you want the latest diffCheck, checkout and pull the main.

---
Expand Down
Loading
Loading