Skip to content

Commit

Permalink
Merge pull request apple#42 from FoundationDB/eng/reverse-header-sepa…
Browse files Browse the repository at this point in the history
…rate-gen-target

[interop] Use a separate target for Swift-to-C++ header generation
  • Loading branch information
hyp authored Dec 6, 2022
2 parents b7b531f + 7f41097 commit 0cf635d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
35 changes: 13 additions & 22 deletions cmake/SwiftToCXXInterop.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
include(CompilerChecks)
include(FindSwiftLibs)

# Generates a C++ compatibility header for the given target.
# FIXME: Can we remove the headerName and match the Swift module name?
function(generate_cxx_compat_header target headerName)
function(add_swift_to_cxx_header_gen_target target_name header_target_name header_path)
cmake_parse_arguments(ARG "" "" "SOURCES;FLAGS" ${ARGN})

# Verify toolchain support.
get_filename_component(SwiftBinPath ${CMAKE_Swift_COMPILER} DIRECTORY)
set (SwiftInteropVersionFile ${SwiftBinPath}/../lib/swift/swiftToCxx/experimental-interoperability-version.json)
Expand All @@ -17,36 +17,27 @@ function(generate_cxx_compat_header target headerName)
message(FATAL_ERROR "Swift: reverse interop is required, but not supported. Update your toolchain.")
endif()

set(destpath ${CMAKE_CURRENT_BINARY_DIR}/include/SwiftModules)
message(STATUS "Swift: C++ compatibility headers for ${target} will be generated in ${destpath}")

# Generate a directory into which the C++ headers will go.
file(MAKE_DIRECTORY ${destpath})

# Generate the C++ compatibility header for the Swift module.
target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:Swift>:SHELL: -Xfrontend -emit-clang-header-path -Xfrontend ${destpath}/${headerName}>")
# Note: do not generate Stdlib bindings yet (C++20 only).
if (${headerName} STREQUAL "Flow")
target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:Swift>:SHELL: -Xfrontend -clang-header-expose-decls=has-expose-attr>")
endif()
endfunction()

function(add_swift_to_cxx_header_gen_target target_name header_target_name header_path source_path)
cmake_parse_arguments(ARG "" "" "FLAGS" ${ARGN})

set(target_includes_expr "$<TARGET_PROPERTY:${target_name},INCLUDE_DIRECTORIES>")
if(ARG_SOURCES)
set(target_sources ${ARG_SOURCES})
else()
get_target_property(target_sources ${target_name} SOURCES)
get_target_property(target_source_dir ${target_name} SOURCE_DIR)
list(TRANSFORM target_sources PREPEND "${target_source_dir}/")
endif()
add_custom_command(
OUTPUT
"${header_path}"
COMMAND
${CMAKE_Swift_COMPILER} -frontend -typecheck
"${source_path}"
${target_sources}
-enable-experimental-cxx-interop
-module-name "${target_name}"
-emit-clang-header-path "${header_path}"
"$<$<BOOL:${target_includes_expr}>:-I$<JOIN:${target_includes_expr},;-I>>"
${ARG_FLAGS}
DEPENDS
"${source_path}"
"${target_sources}"
COMMAND_EXPAND_LISTS
COMMENT
"Generating '${header_path}'"
Expand Down
4 changes: 0 additions & 4 deletions fdbclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ generate_modulemap("${CMAKE_BINARY_DIR}/fdbclient/include" "FDBClient" fdbclient
# TODO: the TBD validation skip is because of swift_job_run_generic, though it seems weird why we need to do that?
target_compile_options(fdbclient_swift PRIVATE "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -std=c++17 -enable-experimental-cxx-interop -Xfrontend -validate-tbd-against-ir=none -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbclient/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbclient/include/headeroverlay.yaml>")

# Ensure that C++ code in fdbclient can import Swift using a compatibility header.
include(SwiftToCXXInterop)
generate_cxx_compat_header(fdbclient_swift "FDBClient")

add_dependencies(fdbclient_swift flow_swift)
# This does not work! (see rdar://99107402)
# target_link_libraries(flow PRIVATE flow_swift)
Expand Down
16 changes: 12 additions & 4 deletions fdbserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,25 @@ add_swift_to_cxx_header_gen_target(
fdbserver_swift
fdbserver_swift_to_cxx_conformance_header
"${CMAKE_CURRENT_BINARY_DIR}/include/SwiftModules/FDBServer_CxxTypeConformances.h"
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/swift_fdbserver_cxx_swift_value_conformance.swift"
FLAGS
-Xcc -std=c++17 -enable-experimental-cxx-interop -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbclient/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbserver/include/headeroverlay.yaml
-Xcc -std=c++17 -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbclient/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbserver/include/headeroverlay.yaml
# Important: This is needed to avoid including header that depends on this generated header.
-Xcc -DFOUNDATIONDB_FDBSERVER_STREAM_SUPPORT_H
)

generate_cxx_compat_header(fdbserver_swift "FDBServer")
add_swift_to_cxx_header_gen_target(
fdbserver_swift
fdbserver_swift_header
"${CMAKE_CURRENT_BINARY_DIR}/include/SwiftModules/FDBServer"
FLAGS
-Xcc -std=c++17 -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbclient/include/headeroverlay.yaml -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/fdbserver/include/headeroverlay.yaml
)

add_dependencies(fdbserver_swift_to_cxx_conformance_header flow_actors fdbserver_actors)
add_dependencies(fdbserver_swift fdbserver_swift_to_cxx_conformance_header)
add_dependencies(fdbserver_swift_to_cxx_conformance_header flow_swift flow_swift_header flow_actors fdbserver_actors)
add_dependencies(fdbserver_swift_header fdbserver_swift_to_cxx_conformance_header)
add_dependencies(fdbserver_swift fdbserver_swift_header)

add_dependencies(fdbserver_swift flow_swift)
add_dependencies(fdbserver_swift flow_actors)
Expand Down
20 changes: 14 additions & 6 deletions flow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ target_link_directories(flow PUBLIC "${SWIFT_LINK_PATHS}")
# We need to make sure that Swift, and the concurrency library is linked
# in every module that uses flow, because we implement the Swift hooks in flow.
# TODO(swift): With upcoming CMake 3.26 we can get rid of this as it should realize
# that modules need Swift and use Swift for the linking which
# that modules need Swift and use Swift for the linking which
# will do the right thing.
target_link_options(flow PUBLIC
target_link_options(flow PUBLIC
"-lswiftCore"
"-lswift_Concurrency"
"-lswift_StringProcessing")
Expand Down Expand Up @@ -171,17 +171,25 @@ add_swift_to_cxx_header_gen_target(
flow_swift
flow_swift_checked_continuation_header
"${CMAKE_CURRENT_BINARY_DIR}/include/SwiftModules/Flow_CheckedContinuation.h"
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/FlowCheckedContinuation.swift"
FLAGS
-Xcc -std=c++17 -enable-experimental-cxx-interop -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml
-Xcc -std=c++17 -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml
# Important: This is needed to avoid including header that depends on this generated header.
-Xcc -DSWIFT_FUTURE_SUPPORT_H
)

generate_cxx_compat_header(flow_swift "Flow")
add_swift_to_cxx_header_gen_target(
flow_swift
flow_swift_header
"${CMAKE_CURRENT_BINARY_DIR}/include/SwiftModules/Flow"
FLAGS
-Xcc -std=c++17 -Xcc -DNO_INTELLISENSE -Xcc -ivfsoverlay${CMAKE_BINARY_DIR}/flow/include/headeroverlay.yaml
)

add_dependencies(flow_swift_checked_continuation_header flow_actors)
add_dependencies(flow_swift flow_swift_checked_continuation_header)
add_dependencies(flow_swift_checked_continuation_header flow_actors boost_targetProject)
add_dependencies(flow_swift_header flow_swift_checked_continuation_header)
add_dependencies(flow_swift flow_swift_header)

add_dependencies(flow_swift flow_actors)
add_dependencies(flow_swift boost_targetProject)
Expand Down

0 comments on commit 0cf635d

Please sign in to comment.