Passing `option`s through to `find_package`

Let’s say I have a project which wants to use spdlog, which depends on fmtlib.

The main spdlog CMakeLists.txt has a bunch of options for how to configure it, including SPDLOG_FMT_EXTERNAL. Let’s say I:

  • want to clone spdlog from source, and
  • want to provide my own fmt version and then I want to tell spdlog to use that.

What is the right way to write Findspdlog.cmake to clone the repo and to set SPDLOG_FMT_EXTERNAL so that it does the right thing?


Or, for a different example of the same idea, we could look at date and the desire to set BUILD_TZ_LIB and USE_SYSTEM_TZ_DB. How do I write Finddate.cmake to do this?

Since you want to build spdlog from source, I’d personally use FetchContent rather than trying to create a Findspdlog.cmake. I don’t know how spdlog expects to find its fmt dependency though. Are you going to have a pre-built fmt available somewhere, or are you building fmt from source as well?

FWIW, if you take the FetchContent route, then set the SPDLOG_FMT_EXTERNAL variable just before you call FetchContent_MakeAvailable(spdlog). Without looking through the spdlog sources, I think that should probably do what you’re trying to set up.