How to install component libraries like boost?

The are at least 2 ways to do it.

1) One is to set NO_CHECK_REQUIRED_COMPONENTS_MACRO

It is recommended to set this option here https://cmake.org/cmake/help/latest/guide/importing-exporting/index.html?highlight=components#adding-components

This result is this cmake config package installed:

Claus-iMac:MathFunctionsComponents$ tree root/
root/
├── include
│   └── MathFunctions
│       ├── Addition.h
│       ├── MathFunctions.h
│       └── SquareRoot.h
└── lib
    ├── cmake
    │   └── MathFunctions
    │       ├── MathFunctionsAdditionTargets-debug.cmake
    │       ├── MathFunctionsAdditionTargets.cmake
    │       ├── MathFunctionsConfig.cmake
    │       ├── MathFunctionsConfigVersion.cmake
    │       ├── MathFunctionsMathFunctionsTargets-debug.cmake
    │       ├── MathFunctionsMathFunctionsTargets.cmake
    │       ├── MathFunctionsSquareRootTargets-debug.cmake
    │       ├── MathFunctionsSquareRootTargets.cmake
    │       ├── MathFunctionsTargets-debug.cmake
    │       └── MathFunctionsTargets.cmake
    ├── libAddition-d.a
    ├── libMathFunctions-d.a
    └── libSquareRoot-d.a

5 directories, 16 files
Claus-iMac:MathFunctionsComponents$ 

2) The other way is to NOT use NO_CHECK_REQUIRED_COMPONENTS_MACRO

I would prefer to have only one MathFunctionsTargets.cmake installed like this:

+ cmake -S test -B build/test -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=root -DTEST_INSTALLED_VERSION=1
-- The CXX compiler identification is GNU 10.2.0
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/g++-10 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    TEST_INSTALLED_VERSION

-- Build files have been written to: /Users/clausklein/Downloads/cmake/Help/guide/importing-exporting/MathFunctionsComponents/build/test
+ cmake --build build/test
[2/2] Linking CXX executable myexe
+ cmake --build build/test --target test
[0/1] Running tests...
Test project /Users/clausklein/Downloads/cmake/Help/guide/importing-exporting/MathFunctionsComponents/build/test
    Start 1: myexe
1/1 Test #1: myexe ............................   Passed    0.32 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.32 sec

Claus-iMac:MathFunctionsComponents$ tree root/
root/
├── include
│   └── MathFunctions
│       ├── Addition.h
│       ├── MathFunctions.h
│       └── SquareRoot.h
└── lib
    ├── cmake
    │   └── MathFunctions
    │       ├── MathFunctionsConfig.cmake
    │       ├── MathFunctionsConfigVersion.cmake
    │       ├── MathFunctionsTargets-debug.cmake
    │       └── MathFunctionsTargets.cmake
    ├── libAddition-d.a
    ├── libMathFunctions-d.a
    └── libSquareRoot-d.a

5 directories, 10 files
Claus-iMac:MathFunctionsComponents$ 

see my result feature request: add support to install a component library like boost · Issue #22 · TheLartians/PackageProject.cmake · GitHub

It woks, I had to change my Config.cmake.in file!

Result in this:


####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was Config.cmake.in                            ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file)
  set(${_var} "${_file}")
  if(NOT EXISTS "${_file}")
    message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  endif()
endmacro()

macro(check_required_components _NAME)
  foreach(comp ${${_NAME}_FIND_COMPONENTS})
    if(NOT ${_NAME}_${comp}_FOUND)
      if(${_NAME}_FIND_REQUIRED_${comp})
        set(${_NAME}_FOUND FALSE)
      endif()
    endif()
  endforeach()
endmacro()

####################################################################################

if(COMPONET_TARGETS_ENABLED)
  set(_supported_components Addition SquareRoot MathFunctions)

  foreach(_comp ${MathFunctions_FIND_COMPONENTS})
    if(NOT _comp IN_LIST _supported_components)
      set(MathFunctions_FOUND False)
      set(MathFunctions_NOT_FOUND_MESSAGE "Unsupported component: ${_comp}")
    endif()
    include("${CMAKE_CURRENT_LIST_DIR}/MathFunctions${_comp}Targets.cmake")
  endforeach()
else()
  include("${CMAKE_CURRENT_LIST_DIR}/MathFunctionsTargets.cmake")
endif()