From 5307a62d25515cbc19591b3e0c2761987af5f8ea Mon Sep 17 00:00:00 2001 From: Thomas Petazzoni Date: Sun, 3 May 2026 14:43:48 +0200 Subject: [PATCH] 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 Co-authored-by: Xiaofan Chen --- CMakeLists.txt | 35 ++++++++++++++++++++++++++--------- src/CMakeLists.txt | 2 -- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f3fce92..5aa11011 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_package(Python3 COMPONENTS Interpreter Development) - if(PYTHON3_FOUND) - set(HAVE_SWIG 1) - else() - message(STATUS "Found SWIG but no Python3 header/library; cannot use SWIG") - endif() +# 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) + 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 "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() # ===================================== diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae5b106b..5d067e50 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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")