Skip to content

ENH: Consume ANTs via find_package(ANTS) instead of manual source-tree paths#598

Open
hjmjohnson wants to merge 1 commit into
BRAINSia:mainfrom
hjmjohnson:ants-find-package
Open

ENH: Consume ANTs via find_package(ANTS) instead of manual source-tree paths#598
hjmjohnson wants to merge 1 commit into
BRAINSia:mainfrom
hjmjohnson:ants-find-package

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Draft — depends on ANTsX/ANTs#1973. Do not merge until that PR lands and an ANTs revision containing it is pinned upstream.

Collapses the 20-line manual include_directories / link_directories / ANTS_LIBS ANTs detection block in BRAINSTools.cmake into find_package(ANTS CONFIG REQUIRED), using ANTs's new package-config layer from ANTsX/ANTs#1973. Net -14 lines, no call-site changes outside the two edited files.

What changes
-if(USE_ANTS)
-  if(NOT (EXISTS "${ANTs_SOURCE_DIR}" AND IS_DIRECTORY "${ANTs_SOURCE_DIR}"))
-    message(FATAL_ERROR "ANTs_SOURCE_DIR: '${ANTs_SOURCE_DIR}' does not exists")
-  endif()
-  include_directories(${BOOST_INCLUDE_DIR})
-  include_directories(${ANTs_SOURCE_DIR}/Temporary)
-  include_directories(${ANTs_SOURCE_DIR}/Tensor)
-  include_directories(${ANTs_SOURCE_DIR}/Utilities)
-  include_directories(${ANTs_SOURCE_DIR}/Examples)
-  include_directories(${ANTs_SOURCE_DIR}/ImageRegistration)
-  if(NOT (EXISTS "${ANTs_LIBRARY_DIR}" AND IS_DIRECTORY "${ANTs_LIBRARY_DIR}"))
-    message(FATAL_ERROR "ANTs_LIBRARY_DIR: '${ANTs_LIBRARY_DIR}' does not exists")
-  endif()
-  link_directories(${BRAINSTools_LIBRARY_PATH} ${BRAINSTools_CLI_ARCHIVE_OUTPUT_DIRECTORY} ${ANTs_LIBRARY_DIR})
-  set(ANTS_LIBS antsUtilities)
-endif()
+if(USE_ANTS)
+  find_package(ANTS CONFIG REQUIRED)
+  include_directories(${BOOST_INCLUDE_DIR})
+  link_directories(${BRAINSTools_LIBRARY_PATH} ${BRAINSTools_CLI_ARCHIVE_OUTPUT_DIRECTORY})
+  set(ANTS_LIBS ANTS::antsUtilities)
+endif()

The 6 public-API include directories (Examples/, Utilities/, ImageRegistration/, ImageSegmentation/, Tensor/, Temporary/) plus all transitive ITK includes now flow through ANTS::antsUtilities's INTERFACE_INCLUDE_DIRECTORIES automatically. Existing target_link_libraries(... ${ANTS_LIBS} ...) call sites (BRAINSCommonLib, BRAINSFit) are unchanged because the namespaced imported target is a drop-in for the bare library name.

SuperBuild plumbing (External_ANTs.cmake)
-set(${proj}_REPOSITORY "https://github.com/ANTsX/ANTs.git")
-## set(${proj}_REPOSITORY "https://github.com/BRAINSia/ANTs.git")
-set(${proj}_GIT_TAG
-  1766655ca9e00c0b95c765fea2d2468436c94ceb  # 20260325 - Latest master
-)
+set(${proj}_REPOSITORY "https://github.com/hjmjohnson/ANTs.git")
+set(${proj}_GIT_TAG
+  31faec49e3e82dc39f21139d70034be65dfc9a95  # 20260512 - antsconfig-cmake-modernize (ANTsX/ANTs#1973)
+)
 set(${proj}_LIBRARY_DIR ${CMAKE_INSTALL_PREFIX}/lib)
+set(ANTS_DIR ${CMAKE_INSTALL_PREFIX}/lib/cmake/ANTS)
 mark_as_superbuild(
   VARS
      ${proj}_SOURCE_DIR:PATH
      ${proj}_LIBRARY_DIR:PATH
+     ANTS_DIR:PATH
   LABELS "FIND_PACKAGE"
   )

The ANTS_DIR propagation is what lets the inner BRAINSTools build's find_package(ANTS CONFIG REQUIRED) locate the just-installed ANTSConfig.cmake without needing CMAKE_PREFIX_PATH tweaks.

Release coordination — what has to happen before this can merge
Step Status
1. ANTsX/ANTs#1973 reviewed + merged (adds ANTSConfig.cmake, ANTSTargets.cmake, ANTS::antsUtilities, public-header install, and ANTS_INCLUDE_DIRS/ANTS_LIBRARIES legacy-list vars) Draft, glance-approved by @dzenanz, pending Friday verification
2. ANTs ships a tagged release containing #1973 Pending step 1
3. Update this PR's External_ANTs.cmake ${proj}_REPOSITORY back to https://github.com/ANTsX/ANTs.git and GIT_TAG to the release SHA Will do after step 2
4. Mark this PR ready for review After step 3

Until step 3, the temporary repo pointer at hjmjohnson/ANTs keeps the SuperBuild building from the PR branch directly so the BRAINSTools side can be exercised in CI / dev environments without waiting for an ANTs release.

Verification

ANTs PR #1973's package-config was exercised end-to-end with an independent downstream consumer that mirrors BRAINSCommonLib/BRAINSFitSyN.h's exact ANTs includes:

  • Repo: https://github.com/hjmjohnson/ANTsDependantCMakeSmokeTest
  • Exercises: #include "antsUtilities.h", "itkantsRegistrationHelper.h", "ReadWriteData.h", "itkANTSAffine3DTransform.h", "ANTsVersionConfig.h"; calls ConvertToLowerCase (real symbol in libantsUtilities), instantiates itk::ANTSAffine3DTransform<double>, and itk::Image<float,3>.
  • Result: green — all the headers BRAINSTools needs are reachable through ANTS::antsUtilities after find_package(ANTS), and the link interface resolves.

Same machinery is also being exercised independently in InsightSoftwareConsortium/ITKANTsWasm#41.

Full BRAINSTools SuperBuild verification will run once CI starts on this PR; given the inner-build changes are mechanical (just swapping the same set of include directories for the same target's interface, and the same library name for the namespaced imported target), the risk is low.

…e paths

Switches the in-SuperBuild ANTs build to ANTsX/ANTs#1973's
antsconfig-cmake-modernize branch (which adds ANTSConfig.cmake to ANTs's
install tree) and collapses BRAINSTools.cmake's 20-line manual
include_directories + link_directories + ANTS_LIBS block into a single
find_package(ANTS CONFIG REQUIRED).

External_ANTs.cmake:
  * Repo: ANTsX/ANTs -> hjmjohnson/ANTs (temporarily; will revert to
    ANTsX/ANTs once #1973 merges and a tagged ANTs release ships with
    find_package() support).
  * GIT_TAG: 1766655c (2026-03-25 master) -> 31faec49
    (antsconfig-cmake-modernize HEAD).
  * Export ANTS_DIR = ${CMAKE_INSTALL_PREFIX}/lib/cmake/ANTS via
    mark_as_superbuild so the inner BRAINSTools build's
    find_package(ANTS) can locate the just-installed ANTSConfig.cmake.

BRAINSTools.cmake:
  * Replace the manual EXISTS-checks + 5x include_directories +
    link_directories(${ANTs_LIBRARY_DIR}) + set(ANTS_LIBS antsUtilities)
    block with:
        find_package(ANTS CONFIG REQUIRED)
        set(ANTS_LIBS ANTS::antsUtilities)
    ANTs's installed antsUtilities target carries the 6 public-API
    include directories (Examples/, Utilities/, ImageRegistration/,
    ImageSegmentation/, Tensor/, Temporary/) plus ITK transitive
    includes via INTERFACE_INCLUDE_DIRECTORIES, so the manual paths
    are no longer needed. Existing call sites that use ${ANTS_LIBS}
    in target_link_libraries (BRAINSCommonLib, BRAINSFit) work
    unchanged because the imported target ANTS::antsUtilities is a
    drop-in for the bare antsUtilities library name.

Diff: 23 -> 9 lines (-14 net). No call-site changes outside the two
edited files.

Net: BRAINSTools no longer needs the ANTs source tree on disk after
the SuperBuild ANTs install completes; consumes only the install
tree like any other CMake-found dependency.

Verification: ANTSConfig.cmake + the 6 INSTALL_INTERFACE include
directories + ANTS_LIBRARIES were exercised end-to-end against an
independent consumer at
https://github.com/hjmjohnson/ANTsDependantCMakeSmokeTest (built ANTs
from #1973, installed, ran a downstream find_package(ANTS) consumer
that includes the same headers BRAINSFitSyN.h uses).
hjmjohnson added a commit that referenced this pull request May 15, 2026
On macOS SDK 26.5's <__math/abs.h>, the std::abs overload set includes
both the templated floating-point form and integer-typed candidates,
making the explicit form `std::abs<double>(...)` ambiguous:

  error: call to 'abs' is ambiguous
     37 |   return std::abs<double>(input - desired)
        |          ^~~~~~~~~~~~~~~~

The explicit template argument was never load-bearing here --- inputs
are `double`, so the non-templated `std::abs(double)` overload from
<cmath> resolves unambiguously. Dropping `<double>` fixes the build
on current Apple toolchains without changing behavior on earlier
SDKs or other platforms.

Surfaced while validating #598 (find_package(ANTS)
migration) on AppleClang 21 / macOS SDK 26.5.
@hjmjohnson hjmjohnson marked this pull request as ready for review May 26, 2026 20:43

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants