Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_package Config file with components

Tags:

cmake

i have a cmake package with a Config file supporting components, e.g.

find_package(myPack REQUIRED COMPONENTS foo bar)

in myPackConfig.cmake I typically do

foreach(comp ${myPack_FIND_COMPONENTS})
    if(TARGET myPack::${comp})
         find_package(${comp})
    endif()
endif()

i.e. loop all components and find them. But is it possible to include ALL components if none is given? E.g. when doing

find_package(myPack)

I want to have ALL components (foo, bar, baz et.c.)

thanks

like image 844
Johan Avatar asked Mar 12 '26 10:03

Johan


1 Answers

Generally, you need to iterate over list of all targets, and find ones which matches given pattern (myPack::*).

CMake has property BUILDSYSTEM_TARGETS... but it lists only non-IMPORTED targets, so it doesn't fit.

Common CMake pattern to obtain the list of created "objects" is to intercept commands created such object and force them to add objects into the list. This approach works for collecting targets list:

myPackConfig.cmake:

# ... Somewhere at the beginnning, before including other files.
set(targets_list) # It will be maintained as a list of targets created
# Replace `add_library()` call
function (add_library name)
    # Add library target name to the list
    list(APPEND targets_list ${name})
    # And perform original actions
   _add_library (${name} ${ARGN})
endfunction (add_library)

# Include `*.cmake` files created by CMake.
...

# After all 'include()'s iterate over targets list and collect needed ones.
set(all_component_targets)
foreach(t ${targets_list})
    if(t MATCHES "myPack::.*")
        list(APPEND all_component_targets ${t})
    endif()
endforeach()

# Now 'all_component_targets' contains all targets prefixed with "myPack::".

(That approach has been suggested in CMake mailing list).

like image 69
Tsyvarev Avatar answered Mar 15 '26 05:03

Tsyvarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!