From 4d712cfc25175395c9b59c1d246748dc9d63d758 Mon Sep 17 00:00:00 2001 From: Martin Duffy Date: Wed, 14 Jan 2026 11:42:58 -0500 Subject: fileapi: Fix crash on cmake_file_api() call in initial cache --- Source/cmFileAPI.cxx | 3 ++- Source/cmake.cxx | 12 +++++++++++- Source/cmake.h | 1 + Tests/RunCMake/FileAPI/InitialCache-check.cmake | 8 ++++++++ Tests/RunCMake/FileAPI/InitialCache-script.cmake | 5 +++++ Tests/RunCMake/FileAPI/InitialCache.cmake | 0 Tests/RunCMake/FileAPI/RunCMakeTest.cmake | 1 + 7 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/FileAPI/InitialCache-check.cmake create mode 100644 Tests/RunCMake/FileAPI/InitialCache-script.cmake create mode 100644 Tests/RunCMake/FileAPI/InitialCache.cmake diff --git a/Source/cmFileAPI.cxx b/Source/cmFileAPI.cxx index 1f15612..520e29d 100644 --- a/Source/cmFileAPI.cxx +++ b/Source/cmFileAPI.cxx @@ -58,7 +58,8 @@ void cmFileAPI::ReadQueries() { std::string const query_dir = cmStrCat(this->APIv1, "/query"); std::string const user_query_dir = cmStrCat(this->UserAPIv1, "/query"); - this->QueryExists = cmSystemTools::FileIsDirectory(query_dir); + this->QueryExists = + this->QueryExists || cmSystemTools::FileIsDirectory(query_dir); if (!this->UserAPIv1.empty()) { this->QueryExists = this->QueryExists || cmSystemTools::FileIsDirectory(user_query_dir); diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 3fa18ce..ed37ae4 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -691,6 +691,7 @@ bool cmake::SetCacheArgs(const std::vector& args) // Resolve script path specified on command line // relative to $PWD. auto path = cmSystemTools::CollapseFullPath(value); + state->InitializeFileAPI(); state->ReadListFile(args, path); return true; } }, @@ -2549,7 +2550,7 @@ int cmake::ActualConfigure() } #if !defined(CMAKE_BOOTSTRAP) - this->FileAPI = cm::make_unique(this); + this->InitializeFileAPI(); this->FileAPI->ReadQueries(); if (!this->GetIsInTryCompile()) { @@ -2771,6 +2772,15 @@ void cmake::StopDebuggerIfNeeded(int exitCode) #endif +void cmake::InitializeFileAPI() +{ +#ifndef CMAKE_BOOTSTRAP + if (!this->FileAPI) { + this->FileAPI = cm::make_unique(this); + } +#endif +} + // handle a command line invocation int cmake::Run(const std::vector& args, bool noconfigure) { diff --git a/Source/cmake.h b/Source/cmake.h index cfe4edd..d3ba531 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -646,6 +646,7 @@ public: #if !defined(CMAKE_BOOTSTRAP) cmFileAPI* GetFileAPI() const { return this->FileAPI.get(); } #endif + void InitializeFileAPI(); cmState* GetState() const { return this->State.get(); } void SetCurrentSnapshot(cmStateSnapshot const& snapshot) diff --git a/Tests/RunCMake/FileAPI/InitialCache-check.cmake b/Tests/RunCMake/FileAPI/InitialCache-check.cmake new file mode 100644 index 0000000..3767f15 --- /dev/null +++ b/Tests/RunCMake/FileAPI/InitialCache-check.cmake @@ -0,0 +1,8 @@ +set(expect + reply + reply/codemodel-v2-[0-9a-f]+.json + .*reply/index-[0-9.T-]+.json.* +) + +# Only need to check for existence. Other tests check the reply contents. +check_api("^${expect}$") diff --git a/Tests/RunCMake/FileAPI/InitialCache-script.cmake b/Tests/RunCMake/FileAPI/InitialCache-script.cmake new file mode 100644 index 0000000..9d8505f --- /dev/null +++ b/Tests/RunCMake/FileAPI/InitialCache-script.cmake @@ -0,0 +1,5 @@ +cmake_file_api( + QUERY + API_VERSION 1 + CODEMODEL 2 +) diff --git a/Tests/RunCMake/FileAPI/InitialCache.cmake b/Tests/RunCMake/FileAPI/InitialCache.cmake new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/FileAPI/RunCMakeTest.cmake b/Tests/RunCMake/FileAPI/RunCMakeTest.cmake index 81926af..652265c 100644 --- a/Tests/RunCMake/FileAPI/RunCMakeTest.cmake +++ b/Tests/RunCMake/FileAPI/RunCMakeTest.cmake @@ -52,6 +52,7 @@ run_cmake(ClientStateless) run_cmake(MixedStateless) run_cmake(DuplicateStateless) run_cmake(ClientStateful) +run_cmake_with_options(InitialCache -C ${RunCMake_SOURCE_DIR}/InitialCache-script.cmake) run_cmake(ProjectQueryGood) run_cmake(ProjectQueryBad) -- cgit v0.12 From bb23f659280fb5de7ed0b63d7f3e8b5c1d58c7e7 Mon Sep 17 00:00:00 2001 From: Martin Duffy Date: Wed, 14 Jan 2026 11:47:38 -0500 Subject: instrumentation: Fix crash on cmake_instrumentation() call in initial cache --- Source/cmake.cxx | 19 ++++++++++++++----- Source/cmake.h | 1 + Tests/RunCMake/Instrumentation/RunCMakeTest.cmake | 5 +++++ .../cmake-command-initial-cache-stderr.txt | 4 ++++ Tests/RunCMake/Instrumentation/initial.cmake.in | 5 +++++ .../query/cmake-command-initial-cache.cmake | 0 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 Tests/RunCMake/Instrumentation/cmake-command-initial-cache-stderr.txt create mode 100644 Tests/RunCMake/Instrumentation/initial.cmake.in create mode 100644 Tests/RunCMake/Instrumentation/query/cmake-command-initial-cache.cmake diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 1b361cb..a181aee 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -713,6 +713,7 @@ bool cmake::SetCacheArgs(std::vector const& args) // relative to $PWD. auto path = cmSystemTools::ToNormalizedPathOnDisk(value); state->InitializeFileAPI(); + state->InitializeInstrumentation(); state->ReadListFile(args, path); return true; } }, @@ -2619,11 +2620,7 @@ int cmake::ActualConfigure() #if !defined(CMAKE_BOOTSTRAP) this->InitializeFileAPI(); this->FileAPI->ReadQueries(); - - this->Instrumentation = cm::make_unique( - this->State->GetBinaryDirectory(), - cmInstrumentation::LoadQueriesAfter::No); - this->Instrumentation->ClearGeneratedQueries(); + this->InitializeInstrumentation(); if (!this->GetIsInTryCompile()) { this->TruncateOutputLog("CMakeConfigureLog.yaml"); @@ -2886,6 +2883,18 @@ void cmake::InitializeFileAPI() #endif } +void cmake::InitializeInstrumentation() +{ +#ifndef CMAKE_BOOTSTRAP + if (!this->Instrumentation) { + this->Instrumentation = cm::make_unique( + this->State->GetBinaryDirectory(), + cmInstrumentation::LoadQueriesAfter::No); + this->Instrumentation->ClearGeneratedQueries(); + } +#endif +} + // handle a command line invocation int cmake::Run(std::vector const& args, bool noconfigure) { diff --git a/Source/cmake.h b/Source/cmake.h index 55494a7..39cbf01 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -671,6 +671,7 @@ public: } #endif void InitializeFileAPI(); + void InitializeInstrumentation(); cmState* GetState() const { return this->State.get(); } void SetCurrentSnapshot(cmStateSnapshot const& snapshot) diff --git a/Tests/RunCMake/Instrumentation/RunCMakeTest.cmake b/Tests/RunCMake/Instrumentation/RunCMakeTest.cmake index 39f06a7..ae0f25e 100644 --- a/Tests/RunCMake/Instrumentation/RunCMakeTest.cmake +++ b/Tests/RunCMake/Instrumentation/RunCMakeTest.cmake @@ -12,6 +12,7 @@ function(instrument test) set(uuid "a37d1069-1972-4901-b9c9-f194aaf2b6e0") set(v1 ${RunCMake_TEST_BINARY_DIR}/.cmake/instrumentation-${uuid}/v1) set(query_dir ${CMAKE_CURRENT_LIST_DIR}/query) + configure_file(${RunCMake_SOURCE_DIR}/initial.cmake.in ${RunCMake_BINARY_DIR}/initial.cmake) # Clear previous instrumentation data # We can't use RunCMake_TEST_NO_CLEAN 0 because we preserve queries placed in the build tree after @@ -122,6 +123,10 @@ instrument(cmake-command-bad-arg NO_WARN) instrument(cmake-command-parallel-install BUILD INSTALL TEST NO_WARN INSTALL_PARALLEL DYNAMIC_QUERY CHECK_SCRIPT check-data-dir.cmake) +instrument(cmake-command-initial-cache + CONFIGURE_ARG "-C ${RunCMake_BINARY_DIR}/initial.cmake" + CHECK_SCRIPT check-data-dir.cmake +) instrument(cmake-command-resets-generated NO_WARN COPY_QUERIES_GENERATED CHECK_SCRIPT check-data-dir.cmake diff --git a/Tests/RunCMake/Instrumentation/cmake-command-initial-cache-stderr.txt b/Tests/RunCMake/Instrumentation/cmake-command-initial-cache-stderr.txt new file mode 100644 index 0000000..a1935a0 --- /dev/null +++ b/Tests/RunCMake/Instrumentation/cmake-command-initial-cache-stderr.txt @@ -0,0 +1,4 @@ +CMake Warning \(dev\) at .* + CMake's support for collecting instrumentation data is experimental. It is + meant only for experimentation and feedback to CMake developers. +This warning is for project developers. Use -Wno-dev to suppress it. diff --git a/Tests/RunCMake/Instrumentation/initial.cmake.in b/Tests/RunCMake/Instrumentation/initial.cmake.in new file mode 100644 index 0000000..0d6285d --- /dev/null +++ b/Tests/RunCMake/Instrumentation/initial.cmake.in @@ -0,0 +1,5 @@ +set(CMAKE_EXPERIMENTAL_INSTRUMENTATION @uuid@) +cmake_instrumentation( + API_VERSION 1 + DATA_VERSION 1 +) diff --git a/Tests/RunCMake/Instrumentation/query/cmake-command-initial-cache.cmake b/Tests/RunCMake/Instrumentation/query/cmake-command-initial-cache.cmake new file mode 100644 index 0000000..e69de29 -- cgit v0.12