about summary refs log tree commit diff
path: root/pkgs/development/libraries/itk/generic.nix
blob: 808bac76e8613f37a4ad588d547267d6bc1c084d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{
  version,
  rev,
  sourceSha256,
}:

{
  lib,
  stdenv,
  fetchFromGitHub,
  fetchpatch,
  cmake,
  castxml,
  swig,
  expat,
  eigen,
  fftw,
  gdcm,
  hdf5-cpp,
  libjpeg,
  libminc,
  libtiff,
  libpng,
  libX11,
  libuuid,
  patchelf,
  python ? null,
  numpy ? null,
  xz,
  vtk,
  which,
  zlib,
  Cocoa,
  enablePython ? false,
}:

let
  # Python wrapper contains its own VTK support incompatible with MODULE_ITKVtkGlue
  withVtk = !enablePython;

  itkGenericLabelInterpolatorSrc = fetchFromGitHub {
    owner = "InsightSoftwareConsortium";
    repo = "ITKGenericLabelInterpolator";
    rev = "2f3768110ffe160c00c533a1450a49a16f4452d9";
    hash = "sha256-Cm3jg14MMnbr/sP+gqR2Rh25xJjoRvpmY/jP/DKH978=";
  };

  itkAdaptiveDenoisingSrc = fetchFromGitHub {
    owner = "ntustison";
    repo = "ITKAdaptiveDenoising";
    rev = "24825c8d246e941334f47968553f0ae388851f0c";
    hash = "sha256-deJbza36c0Ohf9oKpO2T4po37pkyI+2wCSeGL4r17Go=";
  };

  itkSimpleITKFiltersSrc = fetchFromGitHub {
    owner = "InsightSoftwareConsortium";
    repo = "ITKSimpleITKFilters";
    rev = "bb896868fc6480835495d0da4356d5db009592a6";
    hash = "sha256-MfaIA0xxA/pzUBSwnAevr17iR23Bo5iQO2cSyknS3o4=";
  };

  # remove after next swig update:
  swigUnstable = swig.overrideAttrs ({
    version = "4.2.1-unstable-2024-08-19";

    src = fetchFromGitHub {
      owner = "swig";
      repo = "swig";
      rev = "5ac5d90f970759fbe705fae551d0743a7c63c67e";
      hash = "sha256-32EFLHpP4l04nqrc8dt4Qsr8deTBqLt8lUlhnNnaIGU=";
    };

  });
in

stdenv.mkDerivation {
  pname = "itk";
  inherit version;

  src = fetchFromGitHub {
    owner = "InsightSoftwareConsortium";
    repo = "ITK";
    inherit rev;
    sha256 = sourceSha256;
  };

  patches = lib.optionals (lib.versionOlder version "5.4") [
    (fetchpatch {
      name = "fix-gcc13-build";
      url = "https://github.com/InsightSoftwareConsortium/ITK/commit/9a719a0d2f5f489eeb9351b0ef913c3693147a4f.patch";
      hash = "sha256-dDyqYOzo91afR8W7k2N64X6l7t6Ws1C9iuRkWHUe0fg=";
    })
  ];

  postPatch = ''
    substituteInPlace CMake/ITKSetStandardCompilerFlags.cmake  \
      --replace "-march=corei7" ""  \
      --replace "-mtune=native" ""
    ln -sr ${itkGenericLabelInterpolatorSrc} Modules/External/ITKGenericLabelInterpolator
    ln -sr ${itkAdaptiveDenoisingSrc} Modules/External/ITKAdaptiveDenoising
    ln -sr ${itkSimpleITKFiltersSrc} Modules/External/ITKSimpleITKFilters
  '';

  cmakeFlags =
    [
      "-DBUILD_EXAMPLES=OFF"
      "-DBUILD_SHARED_LIBS=ON"
      "-DITK_FORBID_DOWNLOADS=ON"
      "-DITK_USE_SYSTEM_LIBRARIES=ON" # finds common libraries e.g. hdf5, libpng, libtiff, zlib, but not GDCM, NIFTI, MINC, etc.
      "-DITK_USE_SYSTEM_EIGEN=ON"
      "-DITK_USE_SYSTEM_EIGEN=OFF"
      "-DITK_USE_SYSTEM_GOOGLETEST=OFF" # ANTs build failure due to https://github.com/ANTsX/ANTs/issues/1489
      "-DITK_USE_SYSTEM_GDCM=ON"
      "-DITK_USE_SYSTEM_MINC=ON"
      "-DLIBMINC_DIR=${libminc}/lib/cmake"
      "-DModule_ITKMINC=ON"
      "-DModule_ITKIOMINC=ON"
      "-DModule_ITKIOTransformMINC=ON"
      "-DModule_SimpleITKFilters=ON"
      "-DModule_ITKReview=ON"
      "-DModule_MGHIO=ON"
      "-DModule_AdaptiveDenoising=ON"
      "-DModule_GenericLabelInterpolator=ON"
    ]
    ++ lib.optionals enablePython [
      "-DITK_WRAP_PYTHON=ON"
      "-DITK_USE_SYSTEM_CASTXML=ON"
      "-DITK_USE_SYSTEM_SWIG=ON"
      "-DPY_SITE_PACKAGES_PATH=${placeholder "out"}/${python.sitePackages}"
    ]
    ++ lib.optionals withVtk [ "-DModule_ITKVtkGlue=ON" ];

  nativeBuildInputs =
    [
      cmake
      xz
    ]
    ++ lib.optionals enablePython [
      castxml
      swigUnstable
      which
    ];

  buildInputs =
    [
      eigen
      libX11
      libuuid
    ]
    ++ lib.optionals stdenv.isDarwin [ Cocoa ]
    ++ lib.optionals enablePython [ python ]
    ++ lib.optionals withVtk [ vtk ];
  # Due to ITKVtkGlue=ON and the additional dependencies needed to configure VTK 9
  # (specifically libGL and libX11 on Linux),
  # it's now seemingly necessary for packages that configure ITK to
  # also include configuration deps of VTK, even if VTK is not required or available.
  # These deps were propagated from VTK 9 in https://github.com/NixOS/nixpkgs/pull/206935,
  # so we simply propagate them again from ITK.
  # This admittedly is a hack and seems like an issue with VTK 9's CMake configuration.
  propagatedBuildInputs = [
    # The dependencies we've un-vendored from ITK, such as GDCM, must be propagated,
    # otherwise other software built against ITK fails to configure since ITK headers
    # refer to these previously vendored libraries:
    expat
    fftw
    gdcm
    hdf5-cpp
    libjpeg
    libminc
    libpng
    libtiff
    zlib
  ] ++ lib.optionals withVtk vtk.propagatedBuildInputs ++ lib.optionals enablePython [ numpy ];

  postInstall = lib.optionalString enablePython ''
    substitute \
      ${./itk.egg-info} \
      $out/${python.sitePackages}/itk-${version}.egg-info \
      --subst-var-by ITK_VER "${version}"
  '';

  # remove forbidden reference to /build which occur when building the Python wrapping
  # (also remove a copy of itkTestDriver with incorrect permissions/RPATH):
  preFixup = lib.optionals enablePython ''
    rm $out/${python.sitePackages}/itk/itkTestDriver
    find $out/${python.sitePackages}/itk -type f -name '*.so*' -exec \
      patchelf {} --shrink-rpath --allowed-rpath-prefixes "$NIX_STORE" \;
  '';

  meta = {
    description = "Insight Segmentation and Registration Toolkit";
    homepage = "https://www.itk.org";
    license = lib.licenses.asl20;
    maintainers = with lib.maintainers; [ bcdarwin ];
    # aarch64-linux Python wrapping fails with "error: unknown type name '_Float128'" and similar;
    # compilation runs slowly and times out on Darwin
    platforms = with lib.platforms; if enablePython then [ "x86_64-linux" ] else unix;
  };
}