Skip to content

Commit 23f321d

Browse files
authored
Build: Use the "new" FindPython mode and binary size optimizations (#104)
* Use "new" FindPython mode - set explicit cmake versions - Use of the "new" FindPython mode remove annoying CMP0148 warning - Add INTERPROCEDURAL_OPTIMIZATION optimize binary size: libPythonRuntime.so is now 5M lighter (from 27M to 22M - Linux x64 gcc13) * macos-12 fixes * Fix GHA Windows build * Windows port to new find_python * Align python versions in CI * Enhance macOS CI * Use setup python for macOS
1 parent 25ab6eb commit 23f321d

7 files changed

Lines changed: 63 additions & 87 deletions

File tree

.github/workflows/Build.yml

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,32 @@ jobs:
156156
with:
157157
path: 'plugins/private/CloudCompare-PythonRuntime'
158158

159+
- name : setup-python
160+
uses: actions/setup-python@v5
161+
with:
162+
python-version: '3.8'
163+
cache: 'pip'
164+
165+
159166
- name: Install Dependencies
160167
run: |
161-
brew install qt@5 ninja pybind11
162-
python3 -m pip install pytest
163-
echo "CMAKE_PREFIX_PATH=/usr/local/opt/qt@5" >> $GITHUB_ENV
168+
brew install qt@5 ninja
169+
echo "CMAKE_PREFIX_PATH=$(brew --prefix qt@5)" >> $GITHUB_ENV
170+
pip install --upgrade pip
171+
pip install pytest pybind11
164172
165173
- name: Configure CMake
166-
shell: pwsh
167174
run: |
168175
mkdir build
169-
cmake `
170-
--log-level=debug `
171-
-G Ninja `
172-
-B build `
173-
-DCMAKE_BUILD_TYPE=Release `
174-
-DOPTION_BUILD_CCVIEWER=OFF `
175-
-DCMAKE_INSTALL_PREFIX=install `
176-
-DPLUGIN_PYTHON=ON `
177-
-DPLUGIN_STANDARD_QM3C2=ON `
176+
cmake \
177+
--log-level=debug \
178+
-G Ninja \
179+
-B build \
180+
-DCMAKE_BUILD_TYPE=Release \
181+
-DOPTION_BUILD_CCVIEWER=OFF \
182+
-DCMAKE_INSTALL_PREFIX=install \
183+
-DPLUGIN_PYTHON=ON \
184+
-DPLUGIN_STANDARD_QM3C2=ON \
178185
.
179186
180187
- name: Build
@@ -184,7 +191,8 @@ jobs:
184191
run: sudo cmake --install build
185192

186193
- name: Run Tests
187-
run: cmake --build build --target pytest
194+
run: |
195+
cmake --build build --target pytest
188196
189197
190198
Windows-Wheels:
@@ -199,7 +207,7 @@ jobs:
199207
with:
200208
activate-environment: CloudCompareDev
201209
auto-activate-base: false
202-
python-version: "3.7"
210+
python-version: "3.8"
203211
miniconda-version: 'latest'
204212

205213
- name: Install Dependencies
@@ -289,7 +297,7 @@ jobs:
289297
source venv/bin/activate
290298
python -m pip install --upgrade pip
291299
python -m pip install pytest
292-
echo "CMAKE_PREFIX_PATH=/usr/local/opt/qt@5" >> $GITHUB_ENV
300+
echo "CMAKE_PREFIX_PATH=$(brew --prefix qt@5)" >> $GITHUB_ENV
293301
294302
- name: pip install cccorelib
295303
working-directory: "./wrapper"

CMakeLists.txt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cmake_minimum_required(VERSION 3.15..3.27)
2+
13
include(cmake/Helpers.cmake)
24
include(cmake/CompilerWarnings.cmake)
35
include(cmake/PythonEnvHelper.cmake)
@@ -10,29 +12,23 @@ if(PLUGIN_PYTHON)
1012

1113
addplugin(NAME ${PROJECT_NAME})
1214

15+
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
16+
ensure_pybind11_cmake_module_is_in_path()
17+
find_package(pybind11 CONFIG REQUIRED)
18+
1319
option(PLUGIN_PYTHON_USE_EMBEDDED_MODULES
1420
"Should the Python wrapper libs be embedded in the plugin" ON
1521
)
1622
mark_as_advanced(PLUGIN_PYTHON_USE_EMBEDDED_MODULES)
1723

1824
if(WIN32)
1925
option(PLUGIN_PYTHON_COPY_ENV
20-
"Should the content of the current python venv be copied on install"
26+
"Should the content of the current python site-package be copied on install"
2127
ON
2228
)
2329
mark_as_advanced(PLUGIN_PYTHON_COPY_ENV)
2430
endif()
25-
26-
if(NOT PYTHON_EXECUTABLE)
27-
set_python_executable_from_current_venv()
28-
endif()
29-
30-
if(PYTHON_EXECUTABLE)
31-
ensure_pybind11_cmake_module_is_in_path()
32-
endif()
33-
34-
find_package(pybind11 CONFIG REQUIRED)
35-
31+
3632
add_subdirectory(wrapper)
3733
add_subdirectory(src)
3834

@@ -44,7 +40,7 @@ if(PLUGIN_PYTHON)
4440
endif()
4541

4642
target_link_libraries(${PROJECT_NAME} pybind11::embed)
47-
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden)
43+
set_target_properties(${PROJECT_NAME} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON CXX_VISIBILITY_PRESET hidden)
4844

4945
add_subdirectory(tests)
5046
add_subdirectory(docs)

cmake/Helpers.cmake

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# PYTHON_PREFIX & PYTHON_LIBRARY_SUFFIX come from pybind11 cmake
2-
31
# This function sets a global PYTHON_BASE_PREFIX cache variable
42
function(getset_python_base_prefix)
53
execute_process(
6-
COMMAND "${PYTHON_EXECUTABLE}" "-c"
4+
COMMAND "${Python_EXECUTABLE}" "-c"
75
"import sys;print(sys.base_prefix.replace('\\\\', '/'), end='')"
86
OUTPUT_VARIABLE PYTHON_BASE_PREFIX
97
)
@@ -13,34 +11,34 @@ function(getset_python_base_prefix)
1311
)
1412
endfunction()
1513

16-
function(copy_python_venv INSTALL_DIR)
14+
function(copy_python_env INSTALL_DIR)
1715
message(
1816
DEBUG
19-
"Venv copy:
17+
"ENV copy:
2018
PYTHON_BASE_PREFIX: ${PYTHON_BASE_PREFIX}
21-
PYTHON_PREFIX: ${PYTHON_PREFIX}
22-
INSTALL_DIR: ${INSTALL_DIR}"
19+
PYTHON_SITELIB: ${Python_SITELIB}
20+
PYTHON_STDARCH: ${Python_STDARCH}
21+
PYTHON_STDLIB: ${Python_STDLIB}
22+
PYTHON_LIBRARIES: ${Python_RUNTIME_LIBRARY_DIRS}"
2323
)
24-
message(DEBUG "COPYING venv from ${PYTHON_BASE_PREFIX} to ${INSTALL_DIR}")
24+
message(DEBUG "COPYING venv from ${Python_SITELIB}/ to ${INSTALL_DIR}/Lib/site-packages/")
2525
install(DIRECTORY "${PYTHON_BASE_PREFIX}/" DESTINATION "${INSTALL_DIR}")
2626

27-
if(NOT "${PYTHON_BASE_PREFIX}" STREQUAL "${PYTHON_PREFIX}")
28-
install(DIRECTORY "${PYTHON_PREFIX}/Lib/site-packages/"
29-
DESTINATION "${INSTALL_DIR}/Lib/site-packages/"
30-
)
31-
endif()
27+
install(DIRECTORY "${Python_SITELIB}/"
28+
DESTINATION "${INSTALL_DIR}/Lib/site-packages/"
29+
)
3230
endfunction()
3331

3432
function(copy_python_dll)
3533
message(
3634
DEBUG
37-
"Python DLL: = ${PYTHON_BASE_PREFIX}/python${PYTHON_LIBRARY_SUFFIX}.dll"
35+
"Python DLL: = ${Python_RUNTIME_LIBRARY_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.dll"
3836
)
3937
install(
40-
FILES "${PYTHON_BASE_PREFIX}/python${PYTHON_LIBRARY_SUFFIX}.dll"
38+
FILES "${Python_RUNTIME_LIBRARY_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.dll"
4139
# install the python3 base dll as well because some libs will try to
4240
# find it (PySide and PyQT for example)
43-
"${PYTHON_BASE_PREFIX}/python3.dll"
41+
"${Python_RUNTIME_LIBRARY_DIRS}/python${Python_VERSION_MAJOR}.dll"
4442
DESTINATION ${CLOUDCOMPARE_DEST_FOLDER}
4543
)
4644
endfunction()
@@ -50,7 +48,7 @@ function(manage_windows_install)
5048
getset_python_base_prefix()
5149

5250
if(PLUGIN_PYTHON_COPY_ENV)
53-
copy_python_venv(${CC_PYTHON_INSTALL_DIR})
51+
copy_python_env(${CC_PYTHON_INSTALL_DIR})
5452
endif()
5553

5654
copy_python_dll()

cmake/PythonEnvHelper.cmake

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
macro(ensure_pybind11_cmake_module_is_in_path)
22
execute_process(
3-
COMMAND "${PYTHON_EXECUTABLE}" "-m" "pybind11" "--cmake"
3+
COMMAND "${Python_EXECUTABLE}" "-m" "pybind11" "--cmake"
44
RESULT_VARIABLE _PYTHON_SUCCESS
55
OUTPUT_VARIABLE PYBIND11_CMAKE_MODULES_PATH
66
)
@@ -15,23 +15,4 @@ macro(ensure_pybind11_cmake_module_is_in_path)
1515
WARNING "Failed to get pybind11 cmake prefix path ${_PYTHON_SUCCESS}"
1616
)
1717
endif()
18-
endmacro()
19-
20-
macro(set_python_executable_from_current_venv)
21-
if(WIN32)
22-
if(DEFINED ENV{CONDA_PREFIX})
23-
list(INSERT CMAKE_PREFIX_PATH 0 "$ENV{CONDA_PREFIX}/Library/share/cmake")
24-
set(PYTHON_EXECUTABLE "$ENV{CONDA_PREFIX}/python.exe")
25-
elseif(DEFINED ENV{VIRTUAL_ENV})
26-
set(PYTHON_EXECUTABLE "$ENV{VIRTUAL_ENV}/Scripts/python.exe")
27-
endif()
28-
else()
29-
if(DEFINED ENV{CONDA_PREFIX})
30-
set(PYTHON_EXECUTABLE "$ENV{CONDA_PREFIX}/bin/python")
31-
elseif(DEFINED ENV{VIRTUAL_ENV})
32-
set(PYTHON_EXECUTABLE "$ENV{VIRTUAL_ENV}/bin/python")
33-
else()
34-
set(PYTHON_EXECUTABLE "python3")
35-
endif()
36-
endif()
37-
endmacro()
18+
endmacro()

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
if(WIN32 OR APPLE)
22
add_custom_target(
33
pytest
4-
COMMAND ${PYTHON_EXECUTABLE} -m pytest --verbose --cloudcompare_exe
4+
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --cloudcompare_exe
55
${CLOUDCOMPARE_EXECUTABLE_INSTALL_PATH} ${CMAKE_CURRENT_LIST_DIR}
66
)
77
else()
88
add_custom_target(
99
pytest
1010
COMMAND
1111
${CMAKE_COMMAND} -E env CC_PLUGIN_PATH=${CC_PLUGIN_PATH_VALUE}
12-
${PYTHON_EXECUTABLE} -m pytest --verbose --cloudcompare_exe
12+
${Python_EXECUTABLE} -m pytest --verbose --cloudcompare_exe
1313
${CLOUDCOMPARE_EXECUTABLE_INSTALL_PATH} ${CMAKE_CURRENT_LIST_DIR}
1414
)
1515
endif()

wrapper/cccorelib/CMakeLists.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.15..3.27)
22
project(pycccorelib LANGUAGES CXX)
33

44
include("${CMAKE_CURRENT_LIST_DIR}/../../cmake/Helpers.cmake")
@@ -16,14 +16,6 @@ endif()
1616
add_subdirectory(src)
1717

1818
if(CCCORELIB_PYTHON_IS_MASTER_PROJECT)
19-
if(NOT PYTHON_EXECUTABLE)
20-
set_python_executable_from_current_venv()
21-
endif()
22-
23-
if(PYTHON_EXECUTABLE)
24-
ensure_pybind11_cmake_module_is_in_path()
25-
endif()
26-
2719
include(FetchContent)
2820

2921
set(FETCHCONTENT_QUIET OFF)
@@ -49,7 +41,10 @@ if(CCCORELIB_PYTHON_IS_MASTER_PROJECT)
4941
endif()
5042

5143
if(CCCORELIB_PYTHON_IS_MASTER_PROJECT OR NOT PLUGIN_PYTHON_USE_EMBEDDED_MODULES)
44+
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
45+
ensure_pybind11_cmake_module_is_in_path()
5246
find_package(pybind11 CONFIG REQUIRED)
47+
5348
pybind11_add_module(cccorelib ${cccorelib_sources})
5449
target_link_libraries(cccorelib PRIVATE CCCoreLib)
5550

wrapper/pycc/CMakeLists.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14)
1+
cmake_minimum_required(VERSION 3.15..3.27)
22
project(pycc LANGUAGES CXX)
33

44
include("${CMAKE_CURRENT_LIST_DIR}/../../cmake/Helpers.cmake")
@@ -16,15 +16,11 @@ else()
1616
endif()
1717

1818
if(PYCC_IS_MASTER_PROJECT)
19-
message(DEBUG "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}")
20-
if(NOT PYTHON_EXECUTABLE)
21-
set_python_executable_from_current_venv()
22-
endif()
19+
message(DEBUG "Python_EXECUTABLE: ${Python_EXECUTABLE}")
2320

24-
message(DEBUG "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}")
25-
if(PYTHON_EXECUTABLE)
26-
ensure_pybind11_cmake_module_is_in_path()
27-
endif()
21+
22+
message(DEBUG "Python_EXECUTABLE: ${Python_EXECUTABLE}")
23+
ensure_pybind11_cmake_module_is_in_path()
2824

2925
include(FetchContent)
3026

@@ -51,6 +47,8 @@ if(PYCC_IS_MASTER_PROJECT)
5147
endif()
5248
endif()
5349

50+
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
51+
ensure_pybind11_cmake_module_is_in_path()
5452
find_package(pybind11 CONFIG REQUIRED)
5553

5654
add_subdirectory(src)

0 commit comments

Comments
 (0)