Skip to content

Conversation

@hjmjohnson
Copy link
Member

  • Relocate Python LIMITED_API logic for modularity
    o Python configuration should be done in ITKSetPytnon3Vars.cmake.
    It was previously spread around in different parts of the ITK
    package which made tracking of behaviors difficult to monitor.

  • Simplify logic for ITK_USE_PYTHON_LIMITED_API
    o Avoid using a temporary variable use_python_limited_api_default to
    set the value of ITK_USE_PYTHON_LIMITED_API.
    o Prefer to set ITK_USE_PYTHON_LIMITED_API directly.
    0 Moved Python LIMITED_API and package component logic to
    ITKSetPython3Vars.cmake for improved organization and maintainability.

  • Ensure subsequent find Python3 use same version
    o When calling find_package(Python3 multiple times
    ensure that the same PYTHON_VERSION is used
    for each call.

  • Changed Python header inclusion to use angle brackets for consistency.
    o Added include directory properties for itkPyCommand.cxx to improve
    o Python wrapping configuration.

  • Remove unused cmake variables
    o Deleted redundant ITKCommon_SYSTEM_INCLUDE_DIRS and
    o ITKCommon_SYSTEM_LIBRARY_DIRS setup for Python when ITK_WRAP_PYTHON is
    enabled.

  • These variable are no longer used anywhere in ITK:
    o ITKCommon_SYSTEM_INCLUDE_DIRS
    o ITKCommon_SYSTEM_LIBRARY_DIRS

  • Remove redundant MSVC Python linking logic
    o Removed unnecessary link_directories usage for MSVC in Python wrapping
    logic as it relies on outdated CMake features and is no longer required.
    Simplified the code for maintainability.

PR Checklist

@github-actions github-actions bot added type:Compiler Compiler support or related warnings type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots area:Python wrapping Python bindings for a class area:Core Issues affecting the Core module labels Nov 3, 2025
@hjmjohnson hjmjohnson marked this pull request as draft November 3, 2025 16:18
@hjmjohnson hjmjohnson requested a review from thewtex November 3, 2025 19:32
@hjmjohnson hjmjohnson marked this pull request as ready for review November 3, 2025 19:40
@hjmjohnson hjmjohnson requested a review from blowekamp November 3, 2025 19:49
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this Hans!

@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 8c4e211 to 0187c52 Compare November 3, 2025 21:01
@dzenanz
Copy link
Member

dzenanz commented Nov 3, 2025

It looks like you accidentally deleted CMake/itkTargetLinkLibrariesWithDynamicLookup.cmake.

@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 0187c52 to 2691336 Compare November 3, 2025 22:46
@hjmjohnson
Copy link
Member Author

It looks like you accidentally deleted CMake/itkTargetLinkLibrariesWithDynamicLookup.cmake.

Yes. This is no longer needed. The Python3_add_module() performs the necessary steps in 3.20 and greater. This correct behavior testing from 2015 predates cmake support. This is now redundant and no longer used.

Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A number of the removed features are not testing in CI, so we need to check:

  • ITKPythonPackage builds and passes tests with this branch (especially on Windows)
  • @SimonRit or @LucasGandel verify that this does not break their modules, e.g. the ITKCudaCommon, that may reference external shared libraries, especially on Windows

@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch 2 times, most recently from ef993ac to acdb6a2 Compare November 4, 2025 17:27
@hjmjohnson
Copy link
Member Author

@thewtex Thanks for pointing me in the direction of the most difficult builds. I have a windows VM that I am trying to setup to do these tests. If you have any other pointers to documentation of how to setup and test windows python I would really appreciate it.

@thewtex
Copy link
Member

thewtex commented Nov 5, 2025

@hjmjohnson hjmjohnson marked this pull request as draft November 7, 2025 16:16
@hjmjohnson
Copy link
Member Author

Finding a windows computer that can build ITK with the environments that need testing is more challenging than expected. I'm working on it.

@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from acdb6a2 to c385b1b Compare November 7, 2025 17:19
@github-actions github-actions bot added the area:ThirdParty Issues affecting the ThirdParty module label Nov 7, 2025
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from c385b1b to 318642e Compare November 8, 2025 17:01
@github-actions github-actions bot removed the area:ThirdParty Issues affecting the ThirdParty module label Nov 8, 2025
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch 3 times, most recently from 0fcc3a2 to 15925b4 Compare November 12, 2025 00:18
@SimonRit
Copy link

A number of the removed features are not testing in CI, so we need to check:

* [ ]  ITKPythonPackage builds and passes tests with this branch (especially on Windows)

* [ ]  @SimonRit or @LucasGandel verify that this does not break their modules, e.g. the ITKCudaCommon, that may reference external shared libraries, especially on Windows

Sorry, I missed your message. I tested the full compilation on Ubuntu of the wrappings with the main branches of CudaCommon and RTK. It seems to work. I'll try to do the same on Windows. Anything else I should try?

@hjmjohnson
Copy link
Member Author

Thanks, Simon.

I am working on building packages using the ITKPythonPackage system as well.

I appreciate your assistance with checking.

FYI: During setting up a Windows system, I was sidetracked with other Windows build issues that I have been trying to address.

Hans

@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 15925b4 to 5306de0 Compare November 16, 2025 14:54
@github-actions github-actions bot added the area:ThirdParty Issues affecting the ThirdParty module label Nov 16, 2025
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 5306de0 to e978470 Compare November 16, 2025 14:55
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from e978470 to 924feef Compare November 27, 2025 20:05
@github-actions github-actions bot removed the area:ThirdParty Issues affecting the ThirdParty module label Nov 27, 2025
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 924feef to 0fbcb01 Compare November 27, 2025 20:44
Visual Studio 2022 warns of ignored flags.

In Visual Studio 2012 and later versions, SSE2 is enabled by default.
https://learn.microsoft.com/en-us/cpp/build/reference/arch-x86?view=msvc-170

Originally prompted by 1000's of warnings:
cl : Command line warning D9025 : overriding '/arch:SSE' with '/arch:SSE2'

For all modern Linux packaging workflows (wheels, C++ libs, distros):
	x86_64: do NOT specify -msse2 — wasteful and redundant.
	Portability is retained, the x86-64 baseline includes SSE2
Corrected the Python wrapping `foreach` loop logic
to properly append file paths to `ITK_WRAP_PYTHON_FILES`
and improve code consistency.
Python3.11 + defines c++ compiles with
extern "C", so it does not need to be
re-stated as extern "C".
Removed unnecessary `link_directories` usage for MSVC in Python wrapping
logic as it relies on outdated CMake features and is no longer required.
Simplified the code for maintainability.
Deleted redundant ITKCommon_SYSTEM_INCLUDE_DIRS and
ITKCommon_SYSTEM_LIBRARY_DIRS setup for Python when ITK_WRAP_PYTHON is
enabled.

These variable are no longer used anywhere in ITK:
  ITKCommon_SYSTEM_INCLUDE_DIRS
  ITKCommon_SYSTEM_LIBRARY_DIRS
Deleted the unused `itkTargetLinkLibrariesWithDynamicLookup.cmake` module to simplify codebase and improve maintainability. This module is no longer relevant or referenced in the current build system.

It was previously needed for building python on Mac, but the functionality is
now included in python3_add_library() from find_package(Python3 ...)
- Relocate Python LIMITED_API logic for modularity
  o Python configuration should be done in ITKSetPytnon3Vars.cmake.
    It was previously spread around in different parts of the ITK
    package which made tracking of behaviors difficult to monitor.

- Simplify logic for ITK_USE_PYTHON_LIMITED_API
  o Avoid using a temporary variable use_python_limited_api_default to
    set the value of ITK_USE_PYTHON_LIMITED_API.
  o Prefer to set ITK_USE_PYTHON_LIMITED_API directly.
  o Moved Python LIMITED_API and package component logic to
    `ITKSetPython3Vars.cmake` for improved organization and maintainability.

- Ensure single call to find_package( Python3 ) is used to simplify
  the logic and testing.  Manually identify required Python3 COMPONENTS
  and give more descriptive error messages to help with tracking down
  the failing condition.  This is particularly helpful when multiple
  versions of Python are installed (i.e. python3.13 from homebrew,
  python3.13 from pixi, python3.13 in standard OS install, etc).

- Refactored Python LIMITED_API setup for improved modularity and reusability.
- Adjusted `itk_end_wrap_module.cmake` to properly handle LIMITED_API settings.
- Updated `ITKSetPython3Vars.cmake` to streamline Python version selection and configuration.
- Improved maintainability by centralizing Python-related configuration and ensuring consistency across CMake files.
- Fix linking of ITKCommon when including python support
- Added a check in `ITKSetPython3Vars.cmake` to ensure CMake version is >= 3.26 when `ITK_USE_PYTHON_LIMITED_API` is enabled.
- Provides clear error messaging to guide users to update CMake or disable the limited API configuration.
- Updated `ITKSetPython3Vars.cmake` to append `Python3_FIND_ABI` values only for Linux (non-Apple, non-MSVC) systems.
- Unset `Python3_FIND_ABI` for non-Linux platforms to prevent configuration issues.
- Updated `ITKSetPython3Vars.cmake` to restrict `PYTHON_VERSION_MIN` and `PYTHON_VERSION_MAX` to the version of the specified `Python3_EXECUTABLE`.
- Ensures compatibility and proper handling of Python LIMITED_API in wrapping process.
Set include directories for Python wrapping from Python3::Module
target.

Changed Python header inclusion to use angle brackets for consistency.
Added include directory properties for `itkPyCommand.cxx` to improve
Python wrapping configuration.

Add Python linking in ITKCommon when ITK_WRAP_PYTHON

- Updated `ITKCommon` to link against appropriate Python libraries when Python wrapping is enabled.
- Differentiated linking logic for LIMITED_API and non-LIMITED_API configurations.

Update Python linking and remove unused development component

Ensure `ITKCommon` links to `Python3::Module` instead of
`Python3::Python`. Adjust Python3 component configuration to exclude
unused `Development`, addressing limitations in dockcross build
environments.
@hjmjohnson hjmjohnson force-pushed the modernize-python3-module-build branch from 0fbcb01 to 4784176 Compare November 27, 2025 20:45
@hjmjohnson
Copy link
Member Author

@thewtex This builds with ITKPythonPackage (after changing ITKPythonPackage to allow for building an untagged version of ITK). It has taken me a long time to figure out how to get ITKPythonPackage to not use the itk6.0b01 tag and use my git tag for testing, but once I got ITKPythonPackage to compile my branch, all worked without a problem.

@hjmjohnson hjmjohnson marked this pull request as ready for review November 28, 2025 14:21
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tried using this. Code mostly looks good.

@dzenanz
Copy link
Member

dzenanz commented Nov 28, 2025

OK I am now trying to build this locally.

@dzenanz
Copy link
Member

dzenanz commented Nov 28, 2025

Building locally was successful, though I only tried a few configurations.

@hjmjohnson hjmjohnson merged commit 41f436a into main Nov 28, 2025
17 checks passed
@hjmjohnson hjmjohnson deleted the modernize-python3-module-build branch November 28, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Python wrapping Python bindings for a class type:Compiler Compiler support or related warnings type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants