Files
Evan Wilde b7a78568c3 CMP0215: Update NEW behavior to require CMP0157/CMP0195 to be NEW
Update CMP0215 to avoid attempting to iterate through the flags
looking for `-emit-module-path` ins the user-specified flags. We don't
do this anywhere else in the code. The existing implementation didn't
handle correctly parsing the flag so `-I blah/my-emit-module-path/` and
`-Xcc -emit-module-path ...` would both trigger it, result in the
compiler not emitting any module. Furthermore, it didn't include the
user-specified module path as part of the build graph resulting in an
incorrect build graph that would never resolve.

The only two appropriate layouts for the module are either the flat
binary module file with the name `<module-name>.swiftmodule`, or the
nested form with
`<module-name>.swiftmodule/<module-triple>.swiftmodule`. The noted
Swift-Syntax situation passes the explicit `-emit-module-path` to get to
the latter form. This form is automatically emitted by CMake when
CMP0195 is `NEW`, removing the need for the flag. The Swift project
generally recommends the nested directory structure since it gathers all
of the generated interface outputs from the compiler (textual swift
interfaces, swiftdoc, sourceinfo, and the binary swiftmodule file) in a
single place, and since it uses the module triple in the filename,
cleanly allows fat mach-o binaries on Apple platforms.

Fixes: #28021
2026-08-13 10:14:16 -04:00

248 lines
8.9 KiB
CMake

include(RunCMake)
# Early bailouts.
if(RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_LESS 6.1)
run_cmake(XcodeTooOld)
return()
elseif(NOT CMake_TEST_Swift)
return()
elseif(NOT RunCMake_GENERATOR MATCHES "^Ninja|^Xcode$")
run_cmake(NotSupported)
return()
endif()
if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(RunCMake_TEST_OPTIONS "-DCMAKE_CONFIGURATION_TYPES=Debug\\;Release")
endif()
block()
run_cmake(CMP0157-NEW)
run_cmake(CMP0157-WARN)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0157-OLD-build)
run_cmake(CMP0157-OLD)
if(RunCMake_GENERATOR MATCHES "Ninja.*")
set(RunCMake_TEST_NO_CLEAN 1)
# -n: dry-run to avoid actually compiling, -v: verbose to capture executed command
run_cmake_command(CMP0157-OLD-build ${CMAKE_COMMAND} --build . -- -n -v)
endif()
endblock()
# Older Xcode versions didn't support Swift static libraries.
if(NOT (RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_LESS 9.0))
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/MultiLangChain-build)
run_cmake(MultiLangChain)
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_OUTPUT_MERGE 1)
run_cmake_command(MultiLangChain-build ${CMAKE_COMMAND} --build .)
endblock()
endif()
if(RunCMake_GENERATOR MATCHES "Ninja.*")
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0195-NEW-build)
run_cmake(CMP0195-NEW)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(CMP0195-NEW-build ${CMAKE_COMMAND} --build . -- -n -v)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0195-OLD-build)
run_cmake(CMP0195-OLD)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(CMP0195-OLD-build ${CMAKE_COMMAND} --build . -- -n -v)
endblock()
endif()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SwiftSimple-build)
run_cmake(SwiftSimple)
if(RunCMake_GENERATOR_IS_MULTI_CONFIG AND
# Older Xcode versions didn't support Swift static libraries.
NOT (RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_LESS 9.0))
# Check that .swiftmodule files get their own directories
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(SwiftSimple-build-Debug ${CMAKE_COMMAND} --build . --config Debug)
run_cmake_command(SwiftSimple-build-Release ${CMAKE_COMMAND} --build . --config Release)
# Will fail if either path doesn't exist. Passing -r because Xcode
# generates .swiftmodule directories.
run_cmake_command(SwiftSimple-verify ${CMAKE_COMMAND} -E
rm -r Debug/L.swiftmodule Release/L.swiftmodule)
endif()
endblock()
if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2
OR XCODE_VERSION VERSION_GREATER_EQUAL 26)
run_cmake(SwiftPtrSize)
endif()
if(RunCMake_GENERATOR MATCHES "Ninja")
block()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
run_cmake_with_options(Win32ExecutableDisallowed)
else()
run_cmake_with_options(Win32ExecutableIgnored)
list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_SYSTEM_NAME=Darwin)
run_cmake(SwiftMultiArch)
endif()
endblock()
# Test that a second build with no changes does nothing.
block()
run_cmake(NoWorkToDo)
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/NoWorkToDo-build)
set(RunCMake_TEST_OUTPUT_MERGE 1)
run_cmake_command(NoWorkToDo-build ${CMAKE_COMMAND} --build .)
run_cmake_command(NoWorkToDo-nowork ${CMAKE_COMMAND} --build . -- -d explain)
file(WRITE ${RunCMake_TEST_BINARY_DIR}/hello.swift "//No-op change\n")
run_cmake_command(NoWorkToDo-norelink ${CMAKE_COMMAND} --build . -- -d explain)
run_cmake_command(NoWorkToDo-nowork ${CMAKE_COMMAND} --build . -- -d explain)
endblock()
# Test that intermediate static libraries are rebuilt when the public
# interface of their dependency changes
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/IncrementalSwift-build)
# Since files are modified during test, the files are created in the cmake
# file into the build directory
run_cmake(IncrementalSwift)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(IncrementalSwift-first ${CMAKE_COMMAND} --build .)
# Modify public interface of libA requiring rebuild of libB
file(WRITE ${RunCMake_TEST_BINARY_DIR}/a.swift
"public func callA() -> Float { return 32.0 }\n")
# Note: We still expect this to fail, but instead of failure at link time,
# it should fail while re-compiling libB because the function changed
run_cmake_command(IncrementalSwift-second ${CMAKE_COMMAND} --build . -- -d explain)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CompileCommands-build)
run_cmake(CompileCommands)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(CompileCommands-check ${CMAKE_COMMAND} --build .)
endblock()
block()
# Try enabling Swift with a static-library try-compile
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/StaticLibTryCompile-build)
list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY)
run_cmake(EnableSwift)
endblock()
block()
# Try enabling Swift with an executable try-compile
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ExecutableTryCompile-build)
list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_TRY_COMPILE_TARGET_TYPE=EXECUTABLE)
run_cmake(EnableSwift)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ForceResponseFile-build)
run_cmake(ForceResponseFile)
set(RunCMake_TEST_NO_CLEAN 1)
# -v: verbose to capture executed commands -n: dry-run to avoid actually compiling
run_cmake_command(ForceResponseFile-check ${CMAKE_COMMAND} --build . -- -vn)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/EmitModuleSeparately-build)
run_cmake(EmitModuleSeparately)
set(RunCMake_TEST_NO_CLEAN 1)
# -v: verbose to capture executed commands -n: dry-run to avoid actually compiling
run_cmake_command(EmitModuleSeparately-build ${CMAKE_COMMAND} --build . -- -vn)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/EmitModuleSeparatelyResponseFile-build)
run_cmake(EmitModuleSeparatelyResponseFile)
set(RunCMake_TEST_NO_CLEAN 1)
# -v: verbose to capture executed commands -n: dry-run to avoid actually compiling
run_cmake_command(EmitModuleSeparatelyResponseFile-build ${CMAKE_COMMAND} --build . -- -vn)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/EmitModuleSeparatelyDirectoryStyle-build)
run_cmake(EmitModuleSeparatelyDirectoryStyle)
set(RunCMake_TEST_NO_CLEAN 1)
# -v: verbose to capture executed commands -n: dry-run to avoid actually compiling
run_cmake_command(EmitModuleSeparatelyDirectoryStyle-build ${CMAKE_COMMAND} --build . -- -vn)
endblock()
block()
run_cmake(EmitModuleSeparatelyLinkDep)
endblock()
block()
run_cmake(EmitModuleSeparatelyLinkDepCustomPath)
endblock()
block()
run_cmake(EmitModuleSeparatelyOrdering)
endblock()
block()
run_cmake(EmitModuleSeparatelyRequiresPolicies)
endblock()
block()
if(CMAKE_SYSTEM_NAME MATCHES Windows)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ImportLibraryFlags-build)
run_cmake(ImportLibraryFlags)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(ImportLibraryFlags-check ${CMAKE_COMMAND} --build . -- -n -v)
endif()
endblock()
block()
if(CMAKE_SYSTEM_NAME MATCHES Windows)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TargetPDBFile-build)
run_cmake(TargetPDBFile)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(TargetPDBFile-build ${CMAKE_COMMAND} --build . -- -n -v)
endif()
endblock()
block()
if(CMAKE_SYSTEM_NAME MATCHES Windows)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/TargetPDBFileDwarf-build)
run_cmake(TargetPDBFileDwarf)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(TargetPDBFileDwarf-build ${CMAKE_COMMAND} --build . -- -v)
run_cmake_command(TargetPDBFileDwarf-install ${CMAKE_COMMAND} --install .
--prefix "${RunCMake_TEST_BINARY_DIR}/install")
endif()
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SwiftModuleNameHyphen-build)
run_cmake(SwiftModuleNameHyphen)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(SwiftModuleNameHyphen-build ${CMAKE_COMMAND} --build . -- -vn)
endblock()
block()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SwiftLibraryModuleCommand-build)
run_cmake(SwiftLibraryModuleCommand)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(SwiftLibraryModuleCommand-check ${CMAKE_COMMAND} --build . -- -n -v)
endblock()
run_cmake(CMP0214-NEW)
run_cmake(CMP0214-OLD)
run_cmake(CMP0214-NEW-CMP0157-OLD)
endif()
if(NOT RunCMake_GENERATOR STREQUAL "Xcode" OR
(RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_GREATER_EQUAL 15.0))
run_cmake(PackageName)
run_cmake(CMP0216-OLD)
run_cmake(CMP0216-NEW)
endif()