Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ elseif(USE_BACKEND STREQUAL "METAL")
set(NEURALNET_BACKEND_SOURCES
neuralnet/metalbackend.cpp
)
_swift_generate_cxx_header_target(
KataGoSwift_Swift_h
add_library(KataGoSwift STATIC
neuralnet/metalbackend.swift)
_swift_generate_cxx_header(
KataGoSwift
"${CMAKE_CURRENT_BINARY_DIR}/include/KataGoSwift/KataGoSwift-swift.h"
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/neuralnet/metalbackend.swift")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChinChangYang
Going ahead to merge, but one comment - Is the SOURCES argument here unused now after this refactor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOURCES is used: AddSwift.cmake parses it (line 13). Without SOURCES, CMake would error

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, it had:

cmake_parse_arguments(ARG "" "" "SOURCES;SEARCH_PATHS;DEPENDS" ${ARGN})

And after, it has:

cmake_parse_arguments(ARG "" "" "SEARCH_PATHS;MODULE_NAME" ${ARGN})

Which no longer includes "SOURCES"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I checked the old revision.

I did experiment with removing the SOURCES from _swift_generate_cxx_header call, and it certainly worked as well.

add_library(KataGoSwift STATIC
neuralnet/metalbackend.swift)
add_dependencies(KataGoSwift KataGoSwift_Swift_h)
target_include_directories(KataGoSwift PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
set_target_properties(KataGoSwift PROPERTIES Swift_MODULE_NAME "KataGoSwift")
target_compile_options(KataGoSwift PUBLIC
Expand Down
80 changes: 54 additions & 26 deletions cpp/external/macos/cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,74 @@
#
# See https://swift.org/LICENSE.txt for license information

include(CheckCompilerFlag)

# Generate bridging header from Swift to C++
# NOTE: This logic will eventually be upstreamed into CMake
function(_swift_generate_cxx_header_target target module header)
cmake_parse_arguments(ARG "" "" "SOURCES;SEARCH_PATHS;DEPENDS" ${ARGN})
if(NOT ARG_SOURCES)
message(FATAL_ERROR "No sources provided to 'swift_generate_cxx_header_target'")

# Generate the bridging header from Swift to C++
#
# target: the name of the target to generate headers for.
# This target must build swift source files.
# header: the name of the header file to generate.
#
# NOTE: This logic will eventually be unstreamed into CMake.
function(_swift_generate_cxx_header target header)
if(NOT TARGET ${target})
message(FATAL_ERROR "Target ${target} not defined.")
endif()

if(NOT DEFINED CMAKE_Swift_COMPILER)
message(WARNING "Swift not enabled in project. Cannot generate headers for Swift files.")
return()
endif()

cmake_parse_arguments(ARG "" "" "SEARCH_PATHS;MODULE_NAME" ${ARGN})

if(NOT ARG_MODULE_NAME)
set(target_module_name $<TARGET_PROPERTY:${target},Swift_MODULE_NAME>)
set(ARG_MODULE_NAME $<IF:$<BOOL:${target_module_name}>,${target_module_name},${target}>)
endif()

if(ARG_SEARCH_PATHS)
list(TRANSFORM ARG_SEARCH_PATHS PREPEND "-I")
string(REPLACE ";" " " EXPANDED_SEARCH_PATHS "${ARG_SEARCH_PATHS}")
endif()

if(APPLE)
if(APPLE AND CMAKE_OSX_SYSROOT)
set(SDK_FLAGS "-sdk" "${CMAKE_OSX_SYSROOT}")
elseif(WIN32)
set(SDK_FLAGS "-sdk" "$ENV{SDKROOT}")
elseif(CMAKE_SYSROOT)
set(SDK_FLAGS "-sdk" "${CMAKE_SYSROOT}")
endif()

add_custom_command(
OUTPUT
"${header}"
cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR include
OUTPUT_VARIABLE base_path)

cmake_path(APPEND base_path ${header}
OUTPUT_VARIABLE header_path)

cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR "${ARG_MODULE_NAME}.emit-module.d" OUTPUT_VARIABLE depfile_path)

set(_AllSources $<PATH:ABSOLUTE_PATH,$<TARGET_PROPERTY:${target},SOURCES>,${CMAKE_CURRENT_SOURCE_DIR}>)
set(_SwiftSources $<FILTER:${_AllSources},INCLUDE,\\.swift$>)
add_custom_command(OUTPUT ${header_path}
DEPENDS ${_SwiftSources}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND
${CMAKE_Swift_COMPILER} -frontend -typecheck
${EXPANDED_SEARCH_PATHS}
${ARG_SOURCES}
${CMAKE_Swift_COMPILER} -typecheck
${ARG_SEARCH_PATHS}
${_SwiftSources}
${SDK_FLAGS}
-module-name "${module}"
-module-name "${ARG_MODULE_NAME}"
-cxx-interoperability-mode=default
-emit-clang-header-path "${header}"
DEPENDS
${ARG_DEPENDS}
-emit-clang-header-path ${header_path}
-emit-dependencies
DEPFILE "${depfile_path}"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT
"Generating '${header}'"
)
"Generating '${header_path}'"
COMMAND_EXPAND_LISTS)

add_custom_target("${target}"
DEPENDS
"${header}"
)
# Added to public interface for dependees to find.
target_include_directories(${target} PUBLIC ${base_path})
# Added to the target to ensure target rebuilds if header changes and is used
# by sources in the target.
target_sources(${target} PRIVATE ${header_path})
endfunction()
2 changes: 1 addition & 1 deletion cpp/external/macos/cmake/modules/InitializeSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ endfunction()
function(_setup_swift_paths)
# If we haven't set the swift library search paths, do that now
if(NOT SWIFT_LIBRARY_SEARCH_PATHS)
if(APPLE)
if(CMAKE_OSX_SYSROOT)
set(SDK_FLAGS "-sdk" "${CMAKE_OSX_SYSROOT}")
endif()

Expand Down