|
| 1 | +##===----------------------------------------------------------------------===## |
| 2 | +## |
| 3 | +## This source file is part of the Swift.org open source project |
| 4 | +## |
| 5 | +## Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +## Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +## |
| 8 | +## See https://swift.org/LICENSE.txt for license information |
| 9 | +## |
| 10 | +##===----------------------------------------------------------------------===## |
| 11 | + |
| 12 | +function(emit_swift_interface target) |
| 13 | + # Generate the target-variant binary swift module when performing zippered |
| 14 | + # build |
| 15 | + # |
| 16 | + # Clean this up once CMake has nested swiftmodules in the build directory: |
| 17 | + # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10664 |
| 18 | + # https://cmake.org/cmake/help/git-stage/policy/CMP0195.html |
| 19 | + |
| 20 | + # We can't expand the Swift_MODULE_NAME target property in a generator |
| 21 | + # expression or it will fail saying that the target doesn't exist. |
| 22 | + get_target_property(module_name ${target} Swift_MODULE_NAME) |
| 23 | + if(NOT module_name) |
| 24 | + set(module_name ${target}) |
| 25 | + endif() |
| 26 | + |
| 27 | + # Account for an existing swiftmodule file generated with the previous logic |
| 28 | + if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule" |
| 29 | + AND NOT IS_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule") |
| 30 | + message(STATUS "Removing regular file ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule to support nested swiftmodule generation") |
| 31 | + file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule") |
| 32 | + endif() |
| 33 | + |
| 34 | + target_compile_options(${target} PRIVATE |
| 35 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-module-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>") |
| 36 | + if(${PROJECT_NAME}_VARIANT_MODULE_TRIPLE) |
| 37 | + target_compile_options(${target} PRIVATE |
| 38 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-module-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.swiftmodule>") |
| 39 | + endif() |
| 40 | + add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule" |
| 41 | + DEPENDS ${target}) |
| 42 | + target_sources(${target} |
| 43 | + INTERFACE |
| 44 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftmodule>) |
| 45 | + |
| 46 | + # Generate textual swift interfaces is library-evolution is enabled |
| 47 | + if(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION) |
| 48 | + target_compile_options(${target} PRIVATE |
| 49 | + $<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.swiftinterface> |
| 50 | + $<$<COMPILE_LANGUAGE:Swift>:-emit-private-module-interface-path$<SEMICOLON>${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_MODULE_TRIPLE}.private.swiftinterface>) |
| 51 | + if(${PROJECT_NAME}_VARIANT_MODULE_TRIPLE) |
| 52 | + target_compile_options(${target} PRIVATE |
| 53 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-module-interface-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.swiftinterface>" |
| 54 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-emit-variant-private-module-interface-path ${CMAKE_CURRENT_BINARY_DIR}/${module_name}.swiftmodule/${${PROJECT_NAME}_VARIANT_MODULE_TRIPLE}.private.swiftinterface>") |
| 55 | + endif() |
| 56 | + target_compile_options(${target} PRIVATE |
| 57 | + $<$<COMPILE_LANGUAGE:Swift>:-library-level$<SEMICOLON>api> |
| 58 | + $<$<COMPILE_LANGUAGE:Swift>:-Xfrontend$<SEMICOLON>-require-explicit-availability=ignore>) |
| 59 | + endif() |
| 60 | +endfunction() |
0 commit comments