Skip to content

Commit

Permalink
Merge pull request #82 from kabiroberai/kabir/cmake
Browse files Browse the repository at this point in the history
[WIP] Add minimal CMake support
  • Loading branch information
kateinoigakukun committed Apr 22, 2024
2 parents d4e83dc + f81e56a commit c3a08eb
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 1 deletion.
57 changes: 57 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
cmake_minimum_required(VERSION 3.19.6)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

project(WasmKit LANGUAGES C Swift)

set(SWIFT_VERSION 5)
set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION})

if(CMAKE_VERSION VERSION_LESS 3.21)
get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
if(NOT parent_dir)
set(PROJECT_IS_TOP_LEVEL TRUE)
endif()
endif()

# The subdirectory into which host libraries will be installed.
set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

set(CMAKE_MACOSX_RPATH YES)

include(AddSwiftHostLibrary)

set(CMAKE_Swift_COMPILER_TARGET ${SWIFT_HOST_TRIPLE})

if("${SWIFT_HOST_MODULE_TRIPLE}" STREQUAL "")
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
if(CMAKE_Swift_COMPILER_TARGET)
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
endif()
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
string(JSON SWIFT_HOST_MODULE_TRIPLE GET "${target_info_json}" "target" "moduleTriple")
endif()
message(STATUS "Module triple: ${SWIFT_HOST_MODULE_TRIPLE}")

add_compile_definitions(
$<$<COMPILE_LANGUAGE:Swift>:WASMKIT_BUILD_USING_CMAKE>
)

include(FetchContent)

find_package(SwiftSystem CONFIG)
if(NOT SwiftSystem_FOUND)
message("-- Vending SwiftSystem")
FetchContent_Declare(SwiftSystem
GIT_REPOSITORY https://github.com/apple/swift-system
GIT_TAG 1.1.1
)
FetchContent_MakeAvailable(SwiftSystem)
endif()

add_subdirectory(Sources)
add_subdirectory(cmake/modules)
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import class Foundation.ProcessInfo

let package = Package(
name: "WasmKit",
platforms: [.macOS(.v12), .iOS(.v14)],
platforms: [.macOS(.v11), .iOS(.v14)],
products: [
.library(
name: "WasmKit",
Expand Down
6 changes: 6 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_subdirectory(WasmKit)
add_subdirectory(WasmKitWASI)
add_subdirectory(SystemExtras)
add_subdirectory(WASI)
add_subdirectory(WasmTypes)
add_subdirectory(WasmParser)
12 changes: 12 additions & 0 deletions Sources/SystemExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_wasmkit_library(SystemExtras
Vendor/Exports.swift
Vendor/Utils.swift
Clock.swift
Constants.swift
FileAtOperations.swift
FileOperations.swift
Syscalls.swift
)

target_link_wasmkit_libraries(SystemExtras PUBLIC
SystemPackage)
15 changes: 15 additions & 0 deletions Sources/WASI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_wasmkit_library(WASI
Platform/SandboxPrimitives/Open.swift
Platform/SandboxPrimitives/OpenParent.swift
Platform/Directory.swift
Platform/Entry.swift
Platform/File.swift
Platform/PlatformTypes.swift
Platform/SandboxPrimitives.swift
FileSystem.swift
GuestMemorySupport.swift
WASI.swift
)

target_link_wasmkit_libraries(WASI PUBLIC
WasmTypes SystemExtras)
34 changes: 34 additions & 0 deletions Sources/WasmKit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
add_wasmkit_library(WasmKit
Types/Module.swift
ModuleParser.swift
Translator.swift
Component/CanonicalLifting.swift
Component/CanonicalLowering.swift
Component/CanonicalCall.swift
Component/CanonicalOptions.swift
Component/ComponentTypes.swift
Execution/Instructions/Reference.swift
Execution/Instructions/Control.swift
Execution/Instructions/Expression.swift
Execution/Instructions/Instruction.swift
Execution/Instructions/Parametric.swift
Execution/Instructions/Table.swift
Execution/Instructions/Memory.swift
Execution/Instructions/InstructionSupport.swift
Execution/Instructions/Numeric.swift
Execution/Instructions/Variable.swift
Execution/Types/Instances.swift
Execution/Types/Errors.swift
Execution/Types/Value.swift
Execution/Runtime/InstDispatch.swift
Execution/Runtime/Stack.swift
Execution/Runtime/Runtime.swift
Execution/Runtime/ExecutionState.swift
Execution/Runtime/Profiler.swift
Execution/Runtime/NameRegistry.swift
Execution/Runtime/Store.swift
Execution/Runtime/Function.swift
)

target_link_wasmkit_libraries(WasmKit PUBLIC
WasmParser SystemExtras WasmTypes SystemPackage)
6 changes: 6 additions & 0 deletions Sources/WasmKitWASI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_wasmkit_library(WasmKitWASI
WASIBridgeToHost+WasmKit.swift
)

target_link_wasmkit_libraries(WasmKitWASI PUBLIC
WasmKit WASI)
13 changes: 13 additions & 0 deletions Sources/WasmParser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_wasmkit_library(WasmParser
Stream/ByteStream.swift
Stream/FileHandleStream.swift
Stream/Stream.swift
InstructionCode.swift
InstructionVisitor.swift
LEB.swift
WasmParser.swift
WasmTypes.swift
)

target_link_wasmkit_libraries(WasmParser PUBLIC
WasmTypes)
4 changes: 4 additions & 0 deletions Sources/WasmTypes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_wasmkit_library(WasmTypes
GuestMemory.swift
WasmTypes.swift
)
147 changes: 147 additions & 0 deletions cmake/modules/AddSwiftHostLibrary.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

# cmake generation for Swift adds an order only dependency, which matches how C-family languages
# works. In that case, however, ninja (and presumably other generators) will rebuild on header
# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
# ABI/API of B changes.
#
# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
# seem to be being tracked.
#
# Remove once rdar://102202478 is fixed.
function(target_link_wasmkit_libraries TARGET)
target_link_libraries(${TARGET} ${ARGN})

cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
DEPENDS ${DEPENDENCY}
)
target_sources(${TARGET} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
)
endforeach()
endfunction()

# Add a new host library with the given name.
function(add_wasmkit_library name)
set(ASHL_SOURCES ${ARGN})

# Create the library target.
add_library(${name} ${ASHL_SOURCES})

# Determine where Swift modules will be built and installed.
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
set(module_base "${module_dir}/${name}.swiftmodule")
set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule")
set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface")
set(module_private_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.private.swiftinterface")
set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo")

# Add a custom target to create the module directory.
add_custom_command(
TARGET ${name}
PRE_BUILD
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
COMMENT "Generating module directory for ${name}")

# Touch the library and objects to workaround their mtime not being updated
# when there are no real changes (eg. a file was updated with a comment).
# Ideally this should be done in the driver, which could only update the
# files that have changed.
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_base}"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of library outputs workaround")

# Install the Swift module into the appropriate location.
set_target_properties(${name}
PROPERTIES Swift_MODULE_DIRECTORY ${module_dir}
)

# Configure the emission of the Swift module files.
target_compile_options("${name}" PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:
-module-name;${name};
-emit-module-path;${module_file};
-emit-module-source-info-path;${module_sourceinfo_file}
>)
if(SWIFT_MODULE_ABI_NAME_PREFIX)
# ABI name prefix. this can be used to avoid name conflicts.
target_compile_options("${name}" PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:
"SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}"
>)
endif()

if(CMAKE_VERSION VERSION_LESS 3.26.0 AND WASMKIT_ENABLE_WMO_PRE_3_26)
target_compile_options(${name} PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-wmo>)
endif()

target_compile_options(${name} PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>
)

if(LLVM_USE_LINKER)
target_link_options(${name} PRIVATE
"-use-ld=${LLVM_USE_LINKER}"
)
endif()

# NOTE: workaround for CMake not setting up include flags yet
set_target_properties(${name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
)

set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH YES
)

if(SWIFT_HOST_LIBRARIES_RPATH)
# Don't add builder's stdlib RPATH automatically.
target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath)
set_property(TARGET ${name}
PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}"
)
endif()

get_target_property(lib_type ${name} TYPE)
if(lib_type STREQUAL SHARED_LIBRARY)
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
# Allow install_name_tool to update paths (for rdar://109473564)
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " -Xlinker -headerpad_max_install_names")
endif()
endif()

if(PROJECT_IS_TOP_LEVEL)
# Install this target
install(TARGETS ${name}
EXPORT WasmKitTargets
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
RUNTIME DESTINATION bin
)

# Install the module files.
install(
DIRECTORY ${module_base}
DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
FILES_MATCHING PATTERN "*.swiftinterface"
)
else()
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
endif()
endfunction()
5 changes: 5 additions & 0 deletions cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if(PROJECT_IS_TOP_LEVEL)
export(EXPORT WasmKitTargets
FILE ${CMAKE_CURRENT_BINARY_DIR}/WasmKitConfig.cmake
NAMESPACE WasmKit::)
endif()

0 comments on commit c3a08eb

Please sign in to comment.