This repository has been archived by the owner on May 12, 2023. It is now read-only.
forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
150 lines (121 loc) · 5.24 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
cmake_minimum_required(VERSION 3.11)
project(SofaPython3 VERSION 1.0)
# Detect if SofaPython3 is a subproject of another project (eg. when compiled within Sofa)
if (NOT "${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}")
set(SP3_COMPILED_AS_SUBPROJECT 1)
message(STATUS "${PROJECT_NAME} is compiled as a subproject to the external project '${CMAKE_PROJECT_NAME}'.")
if("${CMAKE_PROJECT_NAME}" STREQUAL "Sofa")
set(SP3_COMPILED_AS_SOFA_SUBPROJECT 1)
endif()
else()
unset(SP3_COMPILED_AS_SUBPROJECT)
endif ()
# CMAKE TOOLS
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
include(SofaPython3Tools)
if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
if(PLUGIN_SOFAPYTHON)
message(FATAL_ERROR "SofaPython3 cannot be built alongside SofaPython. Please set PLUGIN_SOFAPYTHON to OFF")
endif()
endif()
# OPTIONS
if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
option(SP3_BUILD_TEST "Compile the automatic tests for SofaPython3, along with the gtest library." ${SOFA_BUILD_TESTS})
else()
option(SP3_BUILD_TEST "Compile the automatic tests for SofaPython3, along with the gtest library." ON)
endif()
set(SP3_PYTHON_PACKAGES_DIRECTORY
"lib/site-packages"
CACHE PATH
"Path to the directory containing the python packages defined in the binding projects. (default to lib/site-packages)"
)
# BUILD OPTIONS
if (NOT SP3_COMPILED_AS_SUBPROJECT)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "Install path prefix, prepended onto install directories." FORCE)
endif()
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(SofaFramework REQUIRED)
find_package(SofaSimulation REQUIRED)
find_package(SofaGeneral REQUIRED)
unset(PYTHON_EXECUTABLE)
find_package(Python 3.7 COMPONENTS Interpreter Development REQUIRED)
set(PYTHON_INCLUDE_DIRS ${Python_INCLUDE_DIR})
set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
set(PYBIND11_PYTHON_VERSION ${Python_VERSION})
find_package(pybind11 QUIET)
if(NOT pybind11_FOUND)#
message("-- Cannot build the python module 'Sofa', missing the pybind software to generate the bindings.")
return()
endif()
message("-- Python include dirs: ${PYTHON_INCLUDE_DIRS}")
message("-- Python libraries: ${PYTHON_LIBRARIES}")
message("-- Python library: ${PYTHON_LIBRARY}")
set(HEADER_FILES
src/SofaPython3/config.h
src/SofaPython3/initModule.h
src/SofaPython3/PythonEnvironment.h
src/SofaPython3/SceneLoaderPY3.h
src/SofaPython3/DataCache.h
src/SofaPython3/DataHelper.h
src/SofaPython3/PythonFactory.h
)
set(SOURCE_FILES
src/SofaPython3/initModule.cpp
src/SofaPython3/PythonEnvironment.cpp
src/SofaPython3/SceneLoaderPY3.cpp
src/SofaPython3/DataCache.cpp
src/SofaPython3/DataHelper.cpp
src/SofaPython3/PythonFactory.cpp
)
set(EXTRA_FILES
SofaPython3Config.cmake.in
README.md
)
if(SP3_BUILD_TEST)
list(APPEND HEADER_FILES
src/SofaPython3/PythonTest.h
)
list(APPEND SOURCE_FILES
src/SofaPython3/PythonTest.cpp
)
endif(SP3_BUILD_TEST)
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${EXTRA_FILES})
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SOFAPYTHON3")
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DSOFA_HAVE_SOFAPYTHON3")
if (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
# Sized deallocaion is not enabled by default under clang after c++14
set(CMAKE_CXX_FLAGS "-fsized-deallocation")
endif ()
target_link_libraries(${PROJECT_NAME} PRIVATE SofaCore SofaSimulationCore SofaSimulationGraph SofaComponentGeneral)
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
# dlopen() is used on Linux for a workaround (see PythonEnvironnement.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE dl)
endif()
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/>")
target_include_directories(${PROJECT_NAME} PUBLIC "$<INSTALL_INTERFACE:include>")
target_link_libraries(${PROJECT_NAME} PUBLIC "${PYTHON_LIBRARY}" )
target_include_directories(${PROJECT_NAME} PUBLIC "${PYTHON_INCLUDE_DIR}")
## When installing, copy the content of the example directory into module/SofaPython3
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/examples DESTINATION modules/SofaPython3 COMPONENT resources)
## When installing, copy the target into module/SofaPython3
install(TARGETS ${PROJECT_NAME} DESTINATION modules/SofaPython3)
# The signature of sofa_create_package is
# sofa_create_package(package_name, version, the_targets, <include_subdir>)
# This function assumes that there is a FooConfig.cmake.in file template in the source directory.
# This function is an all in one replacement for install(TARGET), install(EXPORT), and also add
# the example directory in the INSTALL set.
sofa_create_package(${PROJECT_NAME} ${PROJECT_VERSION} SofaPython3 "SofaPython3")
set(EXAMPLES_FILES
examples/example-forcefield.py
examples/example-scriptcontroller.py
)
add_custom_target(Examples SOURCES ${EXAMPLES_FILES})
### Python binding
add_subdirectory(bindings)