Fix crash in target-specific commands called from initial cache script

Fixes: #26018
This commit is contained in:
Brad King
2024-05-31 10:20:26 -04:00
parent 719b461b9c
commit 7330121467
8 changed files with 59 additions and 6 deletions

View File

@@ -85,8 +85,7 @@ bool cmTargetLinkLibrariesCommand(std::vector<std::string> const& args,
}
// Lookup the target for which libraries are specified.
cmTarget* target =
mf.GetCMakeInstance()->GetGlobalGenerator()->FindTarget(args[0]);
cmTarget* target = mf.GetGlobalGenerator()->FindTarget(args[0]);
if (!target) {
for (const auto& importedTarget : mf.GetOwnedImportedTargets()) {
if (importedTarget->GetName() == args[0]) {

View File

@@ -8,7 +8,6 @@
#include "cmStateTypes.h"
#include "cmTarget.h"
#include "cmValue.h"
#include "cmake.h"
cmTargetPropCommandBase::cmTargetPropCommandBase(cmExecutionStatus& status)
: Makefile(&status.GetMakefile())
@@ -35,9 +34,7 @@ bool cmTargetPropCommandBase::HandleArguments(
return false;
}
// Lookup the target for which property-values are specified.
this->Target =
this->Makefile->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(
args[0]);
this->Target = this->Makefile->GetGlobalGenerator()->FindTarget(args[0]);
if (!this->Target) {
this->Target = this->Makefile->FindTargetToUse(args[0]);
}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,44 @@
^CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_sources\):
Cannot specify sources for target "DoesNotExist" which is not built by this
project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_compile_definitions\):
Cannot specify compile definitions for target "DoesNotExist" which is not
built by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_compile_features\):
Cannot specify compile features for target "DoesNotExist" which is not
built by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_compile_options\):
Cannot specify compile options for target "DoesNotExist" which is not built
by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_include_directories\):
Cannot specify include directories for target "DoesNotExist" which is not
built by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_precompile_headers\):
Cannot specify precompile headers for target "DoesNotExist" which is not
built by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_link_directories\):
Cannot specify link directories for target "DoesNotExist" which is not
built by this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_link_options\):
Cannot specify link options for target "DoesNotExist" which is not built by
this project\.
+
CMake Error at [^
]*C_target-commands_initial-cache\.cmake:[0-9]+ \(target_link_libraries\):
Cannot specify link libraries for target "DoesNotExist" which is not built
by this project\.$

View File

@@ -0,0 +1 @@
loading initial cache file \.\./C_target-commands_initial-cache\.cmake

View File

@@ -0,0 +1,9 @@
target_sources(DoesNotExist PRIVATE)
target_compile_definitions(DoesNotExist PRIVATE)
target_compile_features(DoesNotExist PRIVATE)
target_compile_options(DoesNotExist PRIVATE)
target_include_directories(DoesNotExist PRIVATE)
target_precompile_headers(DoesNotExist PRIVATE)
target_link_directories(DoesNotExist PRIVATE)
target_link_options(DoesNotExist PRIVATE)
target_link_libraries(DoesNotExist PRIVATE)

View File

@@ -897,6 +897,8 @@ run_cmake_command(P_working-dir ${CMAKE_COMMAND} -DEXPECTED_WORKING_DIR=${RunCMa
file(COPY ${RunCMake_SOURCE_DIR}/C_basic_initial-cache.txt DESTINATION ${RunCMake_BINARY_DIR})
run_cmake_with_options(C_basic -C ../C_basic_initial-cache.txt)
run_cmake_with_options(C_basic_fullpath -C ${RunCMake_BINARY_DIR}/C_basic_initial-cache.txt)
file(COPY ${RunCMake_SOURCE_DIR}/C_target-commands_initial-cache.cmake DESTINATION ${RunCMake_BINARY_DIR})
run_cmake_command(C_target-commands ${CMAKE_COMMAND} -S ${RunCMake_SOURCE_DIR} -DRunCMake_TEST=C_target-commands -C ../C_target-commands_initial-cache.cmake)
set(RunCMake_TEST_OPTIONS
"-DFOO=-DBAR:BOOL=BAZ")