unset all variables from find_package()


My task is to allow user to decide whether to use superbuild or manually specify _DIR to a library.

I’m interested in the situation when user uses CMake-GUI and at first he set USE_SUPERBUILD = ON and specify path to a package. After that he configures.
Then he decided not to use superbuild so he unchecks superbuild option.

In this case I think I need to unset all variables that were set by command find_package() and somehow delete group-variables from CMake-GUI on the screen.

I don’t see an easy way to do that and I’m not confident that my logic is acceptable.

Snippet of my code:

option(SuperBuild_HDF5 "Pull and build HDF5 from Github" ON)

if(NOT SuperBuild_HDF5)
  find_package(HDF5 REQUIRED)
  unset(HDF5_FOUND CACHE)    # wrong

Knowing the set of variables provided by a find_package call is extremely hard to do. I will say that the _FOUND variable is usually just a local variable (it certainly doesn’t make sense in the cache at least). I would recommend using imported targets rather than the variables. Most of the variables from a find_package are marked as “advanced” anyways and shouldn’t show up in the (default) UI.

In addition, the set of variables for FindHDF5 is extremely complex as well. So that’s just about the hardest case to handle here :slight_smile: .

1 Like

Thank you for response

Could you please clarify the text below, I can’t understand it (probably I don’t have much understanding what is difference between target and variable):

A custom target would be hdf5::hdf5 or the like. It’s meant to be used in target_link_libraries:

target_link_libraries(mytgt PRIVATE hdf5::hdf5)

This allows you to avoid using potentially stale variables too (as there’s no way to cache an imported target).

And to include include dir do I need also use some HDF5_INCLUDE_DIRS variable (or some other variable):
target_include_directories(mytgt PRIVATE HDF5_INCLUDE_DIRS)?

Nope, the target comes with “usage requirements” which includes the include directories already.

1 Like

thank you a lot!
Good to know that :slight_smile: