Files
cmake/Tests/RunCMake/LinkerLauncher/check-special-args.cmake
T
Tyler Yankee 721eb8a5a2 cmCommonTargetGenerator: Improve launcher shell conversion
Encapsulate the shell escaping logic in `cmCommonTargetGenerator` and
represent launchers via a vector of strings (as is done with custom
commands, execute process commands, etc.) to make them easier to work
with.

This facilitates work in a future commit which will reuse this pathway.

Issue: #27598, #27402
2026-09-21 13:45:54 -04:00

43 lines
1.4 KiB
CMake

# Verifies that the launcher's own arguments arrive intact, then runs the
# launched command.
set(base 4) # CMAKE_ARGV0..3 are: cmake -P <script> --
set(num_args 4) # <phase> plus the three arguments below
set(expect_1 "")
set(expect_2 "semi;colon")
set(expect_3 "with space")
math(EXPR min_argc "${base} + ${num_args} + 1")
if(CMAKE_ARGC LESS min_argc)
message(FATAL_ERROR "too few arguments: ${CMAKE_ARGC}")
endif()
foreach(i RANGE 1 3)
math(EXPR idx "${base} + ${i}")
if(NOT "${CMAKE_ARGV${idx}}" STREQUAL "${expect_${i}}")
message(FATAL_ERROR
"launcher argument ${i}: expected [${expect_${i}}] but got [${CMAKE_ARGV${idx}}]")
endif()
endforeach()
file(APPEND "special-args.log" "${CMAKE_ARGV${base}}\n")
# Build and invoke the launched command via cmake_language(EVAL CODE) using
# bracket arguments, rather than a ";"-separated list. A list element ending
# in a backslash (e.g. MSVC's "/Fd<dir>\") would otherwise merge with the
# list's own separator when the list is later expanded, corrupting the
# command line.
set(cmd_code "execute_process(COMMAND")
math(EXPR first "${base} + ${num_args}")
math(EXPR last "${CMAKE_ARGC} - 1")
foreach(i RANGE ${first} ${last})
string(APPEND cmd_code " [==[${CMAKE_ARGV${i}}]==]")
endforeach()
string(APPEND cmd_code " RESULT_VARIABLE result)")
cmake_language(EVAL CODE "${cmd_code}")
if(NOT result EQUAL 0)
message(FATAL_ERROR "launched command failed: ${result}")
endif()