how to build ITK 5.3.0 with Python bindings in Linux?

Hello. I would like to know if someone can help me to build ITK 5.3.0 with Python bindings in Linux. The idea is to have a reproducible build, which implies that downloading the binary packages is not an option. Thank you in advance.

When I build like this:

    cd /pkg
    pkgname="${insight-toolkit}"
    torsocks git clone https://github.com/InsightSoftwareConsortium/ITK insight-toolkit
    torsocks curl -L "https://github.com/InsightSoftwareConsortium/ITK/pull/4066/commits/93bd9fe07e250bdac948ae6f2c2cc749f165f0e9.patch" -o InsightToolkit-gcc13-fix.patch    

    cd /pkg/insight-toolkit
    git checkout v5.3.0

    patch -Np1 -i ../InsightToolkit-gcc13-fix.patch
    git submodule update --init --recursive       
  _confopts=(
    -DCMAKE_INSTALL_PREFIX:FILEPATH=/usr
    -DCMAKE_BUILD_TYPE:STRING=Release
    -DCMAKE_CXX_FLAGS:STRING="-std=c++98"
    -DITK_LEGACY_SILENT:BOOL=ON
    -DBUILD_SHARED_LIBS:BOOL=ON

    -DITK_USE_SYSTEM_LIBRARIES:BOOL=ON
    -DITK_USE_SYSTEM_EXPAT:BOOL=ON
    -DITK_USE_SYSTEM_FFTW:BOOL=ON
    -DITK_USE_SYSTEM_GDCM:BOOL=ON
    -DITK_USE_SYSTEM_HDF5:BOOL=ON
    -DITK_USE_SYSTEM_JPEG:BOOL=ON
    -DITK_USE_SYSTEM_PNG:BOOL=ON
    -DITK_USE_SYSTEM_TIFF:BOOL=ON
    -DITK_USE_SYSTEM_ZLIB:BOOL=ON

    -DBUILD_TESTING:BOOL=OFF
    -DBUILD_EXAMPLES:BOOL=OFF

    -DModule_ITKReview:BOOL=ON
    -DModule_ITKIOMINC:BOOL=ON
    -DModule_ITKIOTransformMINC:BOOL=ON
    -DModule_SimpleITKFilters:BOOL=ON

    -G Ninja
    -B build -S "${pkgname}" -Wno-dev
  ) 
  cmake "${_confopts[@]}"
  samu -j6 -C build

The compilation works well.

When I add the Python flags for CMake

pkgname=insight-toolkit
_confopts=(
    -DCMAKE_INSTALL_PREFIX:FILEPATH=/usr
    -DCMAKE_BUILD_TYPE:STRING=Release
    -DCMAKE_CXX_FLAGS:STRING="-std=c++98"
    -DITK_LEGACY_SILENT:BOOL=ON
    -DBUILD_SHARED_LIBS:BOOL=ON

    -DITK_USE_SYSTEM_LIBRARIES:BOOL=ON
    -DITK_USE_SYSTEM_EXPAT:BOOL=ON
    -DITK_USE_SYSTEM_FFTW:BOOL=ON
    -DITK_USE_SYSTEM_GDCM:BOOL=ON
    -DITK_USE_SYSTEM_HDF5:BOOL=ON
    -DITK_USE_SYSTEM_JPEG:BOOL=ON
    -DITK_USE_SYSTEM_PNG:BOOL=ON
    -DITK_USE_SYSTEM_TIFF:BOOL=ON
    -DITK_USE_SYSTEM_ZLIB:BOOL=ON

    -DBUILD_TESTING:BOOL=OFF
    -DBUILD_EXAMPLES:BOOL=OFF

    -DModule_ITKReview:BOOL=ON
    -DModule_ITKIOMINC:BOOL=ON
    -DModule_ITKIOTransformMINC:BOOL=ON
    -DModule_SimpleITKFilters:BOOL=ON

    # Python
    -DITK_WRAP_PYTHON:BOOL=ON
    -DITK_USE_SYSTEM_SWIG:BOOL=ON
    -DITK_USE_SYSTEM_CASTXML:BOOL=ON

    # Python
    -DITK_WRAP_PYTHON:BOOL=ON
    -DITK_USE_SYSTEM_SWIG:BOOL=ON
    -DITK_USE_SYSTEM_CASTXML:BOOL=ON

    -G Ninja
    -B build -S "${pkgname}" -Wno-dev
  )
  cmake "${_confopts[@]}"
  samu -j6 -C build

I get this:

[2900/4214] Generating itkNarrowBandLevelSetImageFilterPython.cpp, ../../Generators/Python/itk/itkNarrowBandLevelSetImageFilterPython.py
samu: job failed: cd /pkg/insight-toolkit/src/build/Wrapping/Typedefs/python && /usr/bin/swig -c++ -python -O -features autodoc=2 -py3 -doxygen -Werror -w302 -w303 -w312 -w314 -w361 -w362 -w350 -w383 -w384 -w389 -w394 -w395 -w467 -w508 -w509 -o /pkg/insight-toolkit/src/build/Wrapping/Modules/ITKLevelSets/itkNarrowBandThresholdSegmentationLevelSetImageFilterPython.cpp -I/usr/share/swig/4.1.1/python -I/usr/share/swig/4.1.1 -I/pkg/insight-toolkit/src/insight-toolkit/Wrapping/Generators -I/pkg/insight-toolkit/src/build/Wrapping/Typedefs/python -I/pkg/insight-toolkit/src/build/Wrapping/Typedefs -outdir /pkg/insight-toolkit/src/build/Wrapping/Generators/Python/itk /pkg/insight-toolkit/src/build/Wrapping/Typedefs/itkNarrowBandThresholdSegmentationLevelSetImageFilter.i
Deprecated command line option: -py3. Ignored, this option is no longer supported.
/pkg/insight-toolkit/src/build/Wrapping/Typedefs/itkNarrowBandThresholdSegmentationLevelSetImageFilter.i:84: Warning 401: Nothing known about base class 'itk::NarrowBandLevelSetImageFilter< itk::Image< float >,itk::Image< float >,float,itk::Image< float > >'. Ignored.
/pkg/insight-toolkit/src/build/Wrapping/Typedefs/itkNarrowBandThresholdSegmentationLevelSetImageFilter.i:84: Warning 401: Maybe you forgot to instantiate 'itk::NarrowBandLevelSetImageFilter< itk::Image< float >,itk::Image< float >,float,itk::Image< float > >' using %template.
/pkg/insight-toolkit/src/build/Wrapping/Typedefs/itkNarrowBandThresholdSegmentationLevelSetImageFilter.i:176: Warning 401: Nothing known about base class 'itk::NarrowBandLevelSetImageFilter< itk::Image< float,3 >,itk::Image< float,3 >,float,itk::Image< float,3 > >'. Ignored.
/pkg/insight-toolkit/src/build/Wrapping/Typedefs/itkNarrowBandThresholdSegmentationLevelSetImageFilter.i:176: Warning 401: Maybe you forgot to instantiate 'itk::NarrowBandLevelSetImageFilter< itk::Image< float,3 >,itk::Image< float,3 >,float,itk::Image< float,3 > >' using %template.
Deprecated command line option: -py3. Ignored, this option is no longer supported.
Deprecated command line option: -py3. Ignored, this option is no longer supported.
Deprecated command line option: -py3. Ignored, this option is no longer supported.
Deprecated command line option: -py3. Ignored, this option is no longer supported.
Deprecated command line option: -py3. Ignored, this option is no longer supported.
samu: subcommand failed

This process takes roughly 30 minutes with 6 cores.

I tried again with CMake (without @samu@) and could not catch the log. Since it takes some time to compile from scratch, I ran again without cmake "${_confopts[@]}" redirecting stderr to stdout and then to a file (which is attached):

cmake --build build 2>&1 | tee compilation.log

compilation.txt (129.5 KB)

samurai 1.2-2
clang 17.0.6-1
cmake 3.28.3-1
gcc 13.2.1-5
swig 4.1.1-2
python 3.11.8-1

Have you tried with ITK_USE_SYSTEM_SWIG=OFF and CASTXML=OFF (the default)?

1 Like

Thank you! (for your quick answer). It wants to download sources, but there is no connection (see partial_log.txt below)

partial_log.txt (6.2 KB)

I could get the files and put them there locally, but how would I indicate the path to each file? Thanks again.

my system has:
castxml 0.6.4
swig 4.1.1

-DCMAKE_CXX_FLAGS:STRING="-std=c++98" might be getting ignored, as I don’t think ITK compiles with C++ standard that old. I think that already ITK 5.0 required C++11, and that 5.3 requires C++14. 5.4 requires C++17. This is probably not the reason, but changing or removing it might be worth trying.

1 Like

Thank you, dzenanz. Unfortunately, the compilation fails also like that
partial_log.txt (120.5 KB)

ITK v5.3 used swig 4.0.2 and CastXML 0.4.8, try to install those in your system if you don’t want ITK to download their own.

1 Like

Thanks. I will try in a couple of weeks.

Hi. Now running clang14 14.0.6, llvm14 14.0.6, llvm14-libs 14.0.6, swig 4.0.2 & castxml 0.4.8-1, I’m getting many of these:

In file included from /pkg/insight-toolkit/src/build/Wrapping/castxml_inputs/itkTemporalRegion.cxx:1:
In file included from /pkg/insight-toolkit/src/insight-toolkit/Modules/Core/Common/include/itkCommand.h:21:
In file included from /pkg/insight-toolkit/src/insight-toolkit/Modules/Core/Common/include/itkObject.h:31:
In file included from /pkg/insight-toolkit/src/insight-toolkit/Modules/Core/Common/include/itkLightObject.h:21:
In file included from /pkg/insight-toolkit/src/insight-toolkit/Modules/Core/Common/include/itkMacro.h:47:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/string:42:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/char_traits.h:42:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/postypes.h:40:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/cwchar:44:
/usr/include/wchar.h:422:8: error: unknown type name '_Float32'
extern _Float32 wcstof32 (const wchar_t *__restrict __nptr,
       ^

If you are using clang, it looks odd that you are seeing headers from gcc.

You should use the same compiler for all your dependencies. Check that you compiled swig and castxml with clang.

2 Likes

short: compile both CastXML and ITK with Clang 14.

I wonder if there is a way to compile CastXML without Clang, but using commit 21bfa84 from the AUR recipe https://aur.archlinux.org/castxml.git, with these parameters (and the aforementioned packages from Arch):

  CONFOPTS=(
    -DCMAKE_C_COMPILER=/usr/lib/llvm14/bin/clang
    -DCMAKE_CXX_COMPILER=/usr/lib/llvm14/bin/clang++
    -DCLANG_LINK_CLANG_DYLIB=1
    -DCLANG_RESOURCE_DIR=/usr/lib/llvm14/lib/clang/14.0.6/
    -DLLVM_DIR=/usr/lib/llvm14/lib/cmake/llvm/
    -DSPHINX_EXECUTABLE=/usr/bin/sphinx-build
    -DSPHINX_HTML=1
    -DSPHINX_MAN=1
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCastXML_INSTALL_DOC_DIR=share/doc/castxml
    -DCastXML_INSTALL_MAN_DIR=share/man
  )
  cmake . ${CONFOPTS[@]}

Allowed me to build ITK 5.3.0 with Clang with these instructions

  _confopts=(
    -DCMAKE_C_COMPILER:FILEPATH=/usr/lib/llvm14/bin/clang
    -DCMAKE_CXX_COMPILER:FILEPATH=/usr/lib/llvm14/bin/clang++
    -DCMAKE_INSTALL_PREFIX:FILEPATH=/usr
    -DCMAKE_BUILD_TYPE:STRING=Release
    -DCMAKE_CXX_FLAGS:STRING="-std=c++14"
    -DITK_LEGACY_SILENT:BOOL=ON
    -DBUILD_SHARED_LIBS:BOOL=ON

    -DITK_USE_SYSTEM_LIBRARIES:BOOL=ON
    -DITK_USE_SYSTEM_EXPAT:BOOL=ON
    -DITK_USE_SYSTEM_FFTW:BOOL=ON
    -DITK_USE_SYSTEM_GDCM:BOOL=ON
    -DITK_USE_SYSTEM_HDF5:BOOL=ON
    -DITK_USE_SYSTEM_JPEG:BOOL=ON
    -DITK_USE_SYSTEM_PNG:BOOL=ON
    -DITK_USE_SYSTEM_TIFF:BOOL=ON
    -DITK_USE_SYSTEM_ZLIB:BOOL=ON

    -DBUILD_TESTING:BOOL=OFF
    -DBUILD_EXAMPLES:BOOL=OFF

    -DModule_ITKReview:BOOL=ON
    -DModule_ITKIOMINC:BOOL=ON
    -DModule_ITKIOTransformMINC:BOOL=ON

    # Python
    -DITK_WRAP_PYTHON:BOOL=ON
    -DITK_USE_SYSTEM_SWIG:BOOL=ON
    -DITK_USE_SYSTEM_CASTXML:BOOL=ON

    -B build -S "${pkgname}" -Wno-dev
  )
cmake "${_confopts[@]}"
cmake --build build

(roughly 1 hour and 10 minutes). Whether it works is a different question. Thanks a lot!

3 Likes