CMakeLists.txt: allow disabling Python support (#2051)

* CMakeLists.txt: allow disabling Python support

In some cases, even if Swig is found and Python3 is found, it may not
be desirable to build Python support in avrdude, so this commit adds
a FORCE_DISABLE_PYTHON_SUPPORTT option to be able to explicitly 
disable the Python support.

To preserve existing behavior, this option defaults to disabled (OFF).

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Co-authored-by: Xiaofan Chen <xiaofanc@gmail.com>
This commit is contained in:
Thomas Petazzoni
2026-05-03 14:43:48 +02:00
committed by GitHub
parent 3ad284db2c
commit 5307a62d25
2 changed files with 26 additions and 11 deletions

View File

@@ -34,6 +34,7 @@ option(USE_EXTERNAL_LIBS "Use external libraries from AVRDUDE GitHub repositorie
option(USE_LIBUSBWIN32 "Prefer libusb-win32 over libusb" OFF)
option(DEBUG_CMAKE "Enable debugging output for this CMake project" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(FORCE_DISABLE_PYTHON_SUPPORT "Option to explicitly disable Python support" OFF)
if(WIN32)
# Prefer static libraries over DLLs on Windows
@@ -281,15 +282,31 @@ if(HAVE_LINUXGPIO)
endif()
# -------------------------------------
# Find SWIG
find_package(SWIG 4.0 COMPONENTS python)
if(SWIG_FOUND)
# Find SWIG/Python3 if needed
if(NOT FORCE_DISABLE_PYTHON_SUPPORT)
find_package(SWIG 4.0 COMPONENTS python)
if(SWIG_FOUND)
set(Python3_FIND_STRATEGY LOCATION)
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
find_package(Python3 COMPONENTS Interpreter Development)
if(PYTHON3_FOUND)
if(Python3_FOUND)
set(HAVE_SWIG 1)
message(STATUS "Python support enabled")
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import site; print(site.getsitepackages()[0])"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
message(STATUS "Found SWIG but no Python3 header/library; cannot use SWIG")
message(STATUS "SWIG found, but Python3 headers/library missing; Python support disabled")
endif()
else()
message(STATUS "SWIG not found; Python support disabled")
endif()
else()
message(STATUS "Python support explicitly disabled via FORCE_DISABLE_PYTHON_SUPPORT")
endif()
# =====================================

View File

@@ -419,8 +419,6 @@ install(FILES "avrdude.1"
)
if(HAVE_SWIG)
execute_process(COMMAND ${Python3_EXECUTABLE} -c "import site; print(site.getsitepackages()[0])"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
install(TARGETS swig_avrdude DESTINATION ${PYTHON_SITE_PACKAGES})
install(FILES ${CMAKE_BINARY_DIR}/src/swig_avrdude.py DESTINATION ${PYTHON_SITE_PACKAGES})
install(DIRECTORY python/ DESTINATION ${CMAKE_INSTALL_DATADIR}/avrdude FILES_MATCHING PATTERN "*.ui")