Skip to content

find_package: Ensure root path stack and module vars are restored

FetchContent may intercept find_package() calls. If that interception logic calls FetchContent_MakeAvailable() and that call fails, it will return early from cmFindPackageCommand::InitialPass(). Use some new RAII objects to ensure we leave InitialPass() with consistent state, regardless of whether we hit that early return or not.

Original comments about `scope_exit`, no longer applicable

This MR includes a simple implementation of std::scope_exit, which is currently part of the C++ Library Fundamentals TS v3. I've added scope_exit in the cm:: namespace in this MR, following a similar pattern for other things in Utilities/cmext. Background and prior art for my implementation comes from https://crascit.com/2015/06/03/on-leaving-scope-part-2/, but the API and implementation has been adapted to more closely follow the TS. Since we need to still support C++11 though, I have provided a make_scope_exit factory function, which is not part of the TS because the TS can rely on C++17 features that we can't (CTAD).~~

~~A number of times in the past, I've wanted scope_exit or something like it in CMake, but had to make do in other ways. With the change needed in this MR, it now makes sense to just add an implementation, since it isn't much more than would be needed to robustly accomplish the goal without it. The ability to also use it in future work seems worth the effort now.

Fixes: #24595 (closed)
Topic-rename: find_package-raii-fc-early-return

Edited by Craig Scott

Merge request reports

Loading