# Proposal to improve ITK factory registration CMake infrastructure

**URL:** https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546
**Category:** Engineering
**Created:** [November 4, 2021, 2:45pm UTC](https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546 "2021-11-04T14:45:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jcfr](https://discourse.itk.org/user_avatar/discourse.itk.org/jcfr/32/2526_2.png) [@jcfr](https://discourse.itk.org/u/jcfr)
#### Post date: [November 4, 2021, 2:45pm UTC](https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546/1 "2021-11-04T14:45:53Z")

</div>

## Summary

The key idea of the proposal linked below was to introduce a CMake function called `itk_configure_factory` allowing to associate relevant properties to CMake targets.

Ultimately, for each target, this function will configure the header

```auto
<current_dir>/<target_name>/itk<factory_type>IOFactoryRegisterManager.h

```

and will set the following target properties:

- [COMPILE\_DEFINITIONS](https://cmake.org/cmake/help/v3.0/prop_tgt/COMPILE_DEFINITIONS.html) with value `ITK_IO_FACTORY_REGISTER_MANAGER`

- [INCLUDE\_DIRECTORIES](https://cmake.org/cmake/help/v3.0/prop_tgt/INCLUDE_DIRECTORIES.html#prop_tgt:INCLUDE_DIRECTORIES) using `target_include_directories(<target> BEFORE PUBLIC <current_dir>/<target_name>)`

## Context

While reviewing ITK [PR-2836](https://github.com/InsightSoftwareConsortium/ITK/pull/2836) discussing the addition of a new type of factory related to `FFT`, I recalled about a proposal we put together to improve how factories are registered in ITK.

The original document was created in June 2016 and is publicly available as [2016.06.24\_ITK\_Improve\_IO\_Factory](https://docs.google.com/document/d/11b4grg4mtovEiRroiijomMinKba6J25nbJaDpyiqw-4/edit#).

## Related discussion

- [Factory Registration Managers](https://discourse.itk.org/t/factory-registration-managers/573)

cc: @Tom_Birdsong @dzenanz @Niels_Dekker @Lee_Newberg @matt.mccormick

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [November 4, 2021, 6:59pm UTC](https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546/2 "2021-11-04T18:59:12Z")

</div>

Some of the things proposed in this document have been implemented in the last 5 years.

@matt.mccormick I guess we should add this to the TODO list for ITKv6.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [November 5, 2021, 5:51pm UTC](https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546/3 "2021-11-05T17:51:39Z")

</div>

Thank you for including me with this topic! Slightly related, I recently “discovered” that the current approach does create many unnecessary RegisterManager instances for some applications. Specifically, until recently, 86 `itk::ImageIOFactoryRegisterManager` instances and 83 `itk::MeshIOFactoryRegisterManager` instances were constructed automatically, when starting the [elastix](https://elastix.lumc.nl/) executable! Each time a “RegisterManager.h” is included into another cxx file, another instance is constructed.

For elastix I suppressed the creation of these RegisterManager instances, by `#undef ITK_IO_FACTORY_REGISTER_MANAGER`, to avoid an indirect `#include` (via an ITK Reader or Writer). Then I just did `#include` those “RegisterManager.h” files directly, just once, in one dedicated cxx file. Not sure if that’s an official way to do it. 🙂

> <https://github.com/SuperElastix/elastix/pull/541>
>
> Before this commit, 86 \`itk::ImageIOFactoryRegisterManager\` instances and 83 \`it…k::MeshIOFactoryRegisterManager\` instances were constructed automatically, when starting a default-configured elastix executable!

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [November 5, 2021, 6:46pm UTC](https://discourse.itk.org/t/proposal-to-improve-itk-factory-registration-cmake-infrastructure/4546/4 "2021-11-05T18:46:26Z")

</div>

@Niels_Dekker It may be beneficial to set the CMake variable ITK\_NO\_IO\_FACTORY\_REGISTER\_MANAGER. That was the way it was done in SimpleITK. You may want to look at the UseITK.cmake file and documentation in the file to determine the side effect of not having that CMake variable defined.
