Skip to content

Commit 6213f60

Browse files
committed
Add support building python module in CMake.
Bump required minimal cmake version for pybind11.
1 parent 52b4f66 commit 6213f60

17 files changed

+1390
-3
lines changed

CMakeLists.txt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
#Tiny Object Loader Cmake configuration file.
22
#This configures the Cmake system with multiple properties, depending
33
#on the platform and configuration it is set to build in.
4-
cmake_minimum_required(VERSION 3.2)
5-
project(tinyobjloader)
4+
cmake_minimum_required(VERSION 3.16)
5+
project(tinyobjloader CXX)
66
set(TINYOBJLOADER_SOVERSION 2)
77
set(TINYOBJLOADER_VERSION 2.0.0-rc.10)
8+
set(PY_TARGET "aaa")
89

910
#optional double precision support
1011
option(TINYOBJLOADER_USE_DOUBLE "Build library with double precision instead of single (float)" OFF)
12+
option(TINYOBJLOADER_WITH_PYTHON "Build Python module(for developer). Use pyproject.toml/setup.py to build Python module for end-users" OFF)
13+
option(TINYOBJLOADER_PREFER_LOCAL_PYTHON_INSTALLATION
14+
"Prefer locally-installed Python interpreter than system or conda/brew installed Python. Please specify your Python interpreter with `Python3_EXECUTABLE` cmake option if you enable this option."
15+
OFF)
16+
17+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
18+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/sanitizers)
19+
find_package(Sanitizers) # Address sanitizer (-DSANITIZE_ADDRESS=ON)
20+
1121

1222
if(TINYOBJLOADER_USE_DOUBLE)
1323
set(LIBRARY_NAME ${PROJECT_NAME}_double)
1424
else()
1525
set(LIBRARY_NAME ${PROJECT_NAME})
1626
endif()
1727

18-
1928
#Folder Shortcuts
2029
set(TINYOBJLOADEREXAMPLES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/examples)
2130

@@ -46,7 +55,36 @@ set(TINYOBJLOADER_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR})
4655

4756
option(TINYOBJLOADER_BUILD_TEST_LOADER "Build Example Loader Application" OFF)
4857

58+
set(CMAKE_CXX_STANDARD 11)
59+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
60+
set(CMAKE_CXX_EXTENSIONS OFF)
61+
62+
# Build standalone .so for Python binding(for developer)
63+
if (TINYOBJLOADER_WITH_PYTHON)
64+
65+
if(TINYOBJLOADER_PREFER_LOCAL_PYTHON_INSTALLATION)
66+
#message(STATUS "Local Python")
67+
set(Python3_FIND_FRAMEWORK NEVER) # Do not search framework python
68+
set(Python3_FIND_STRATEGY LOCATION)
69+
set(Python3_FIND_REGISTRY NEVER) # Windows only
70+
else()
71+
set(Python3_FIND_FRAMEWORK LAST
72+
)# Prefer Brew/Conda to Apple framework python
73+
endif()
74+
75+
find_package(
76+
Python3
77+
COMPONENTS Interpreter Development
78+
REQUIRED)
79+
80+
find_package(pybind11 CONFIG REQUIRED)
81+
82+
endif()
83+
84+
85+
4986
add_library(${LIBRARY_NAME} ${tinyobjloader-Source})
87+
add_sanitizers(${LIBRARY_NAME})
5088

5189
if(BUILD_SHARED_LIBS)
5290
set_target_properties(${LIBRARY_NAME} PROPERTIES
@@ -85,6 +123,24 @@ if(TINYOBJLOADER_BUILD_OBJ_STICHER)
85123
)
86124
endif()
87125

126+
if (TINYOBJLOADER_WITH_PYTHON)
127+
# pybind11 method:
128+
pybind11_add_module(${PY_TARGET} ${CMAKE_SOURCE_DIR}/python/bindings.cc )
129+
130+
add_sanitizers(${PY_TARGET})
131+
set_target_properties(${PY_TARGET} PROPERTIES OUTPUT_NAME "tinyobjloader")
132+
133+
# copy .so to jdepp/
134+
add_custom_command(
135+
TARGET ${PY_TARGET}
136+
POST_BUILD
137+
COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${PY_TARGET}>"
138+
"${CMAKE_SOURCE_DIR}/python/$<TARGET_FILE_NAME:${PY_TARGET}>"
139+
COMMENT "copying tinyobjloader native python module file to python/"
140+
VERBATIM)
141+
142+
endif()
143+
88144
#Write CMake package config files
89145
include(CMakePackageConfigHelpers)
90146

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
curdir=`pwd`
2+
3+
builddir=${curdir}/build_python_module
4+
5+
rm -rf ${builddir}
6+
mkdir ${builddir}
7+
8+
# set path to pybind11
9+
# If you install pybind11 through pip, its usually installed to <site-package path>/pybind11.
10+
pybind11_path=`python -c "import site; print (site.getsitepackages()[0])"`
11+
echo ${pybind11_path}
12+
13+
CC=clang CXX=clang++ \
14+
pybind11_DIR=${pybind11_path}/pybind11 \
15+
cmake \
16+
-B${builddir} \
17+
-DCMAKE_VERBOSE_MAKEFILE=1 \
18+
-DTINYOBJLOADER_WITH_PYTHON=1
19+
20+
cd ${curdir}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# macOS paths usually start with /Users/*. Unfortunately, clang-cl interprets
2+
# paths starting with /U as macro undefines, so we need to put a -- before the
3+
# input file path to force it to be treated as a path. CMake's compilation rules
4+
# should be tweaked accordingly, but until that's done, and to support older
5+
# CMake versions, overriding compilation rules works well enough. This file will
6+
# be included by cmake after the default compilation rules have already been set
7+
# up, so we can just modify them instead of duplicating them entirely.
8+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
9+
string(REPLACE "-c <SOURCE>" "-c -- <SOURCE>" CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}")

cmake/aarch64-linux-gnu.toolchain

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(CMAKE_SYSTEM_NAME Linux)
2+
set(CMAKE_SYSTEM_PROCESSOR aarch64)
3+
set(CMAKE_C_COMPILER_TARGET aarch64-linux-gnu)
4+
5+
set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu/)
6+
7+
# Sync with GitHub Actions config
8+
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
9+
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
10+
11+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
12+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
13+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
14+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

0 commit comments

Comments
 (0)