Jelajahi Sumber

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

Fixes: #26018
Brad King 1 tahun lalu
induk
melakukan
7330121467

+ 1 - 2
Source/cmTargetLinkLibrariesCommand.cxx

@@ -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]) {

+ 1 - 4
Source/cmTargetPropCommandBase.cxx

@@ -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]);
   }

+ 1 - 0
Tests/RunCMake/CommandLine/C_target-commands-result.txt

@@ -0,0 +1 @@
+1

+ 44 - 0
Tests/RunCMake/CommandLine/C_target-commands-stderr.txt

@@ -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\.$

+ 1 - 0
Tests/RunCMake/CommandLine/C_target-commands-stdout.txt

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

+ 0 - 0
Tests/RunCMake/CommandLine/C_target-commands.cmake


+ 9 - 0
Tests/RunCMake/CommandLine/C_target-commands_initial-cache.cmake

@@ -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)

+ 2 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -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")