Swift: cxx_std_NN on linked libraries should propagate to swift compile options
Given a simple project like so, the cxx_std_20
compile feature on CxxDep
should translate to -Xcc -std=c++20
for the swift targets that link CxxDep
:
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(P LANGUAGES Swift CXX)
add_library(CxxDep INTERFACE)
target_include_directories(CxxDep INTERFACE Sources/CxxDep/include)
target_compile_features(CxxDep INTERFACE cxx_std_20)
add_executable(SwiftExe Sources/SwiftExe/main.swift)
target_compile_options(SwiftExe PRIVATE -cxx-interoperability-mode=default)
target_link_libraries(SwiftExe PRIVATE CxxDep)
Sources/CxxDep/include/module.modulemap
module CxxDep [system] {
requires cplusplus
module A {
header "A.h"
export *
}
}
Sources/CxxDep/include/A.h
#ifndef CXXDEP_A_H
#define CXXDEP_A_H
#if __cplusplus < 202002L
#error "need c++20"
#endif
#endif
Sources/SwiftExe/main.swift
import CxxDep
print("hi")
This should succeed in compiling like so:
cmake -B build -G Ninja
cmake --build build
However with CMake 3.27 and Swift 5.9, it does not. I need to use a manual way of setting the C++ version for any linked c++ modules:
target_compile_options(CxxDep INTERFACE "SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xcc -std=c++20>")
The swift package manager has a top level package setting for this: https://developer.apple.com/documentation/packagedescription/package/cxxlanguagestandard
In my opinion it would make sense for the cxx_std_NN properties to propagate to swift targets to get feature parity there.