From b614185c9030a802331790c476228dc698f04f68 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 18 Mar 2021 15:18:24 -0700 Subject: [PATCH] build: add a CMake based build system (#38) This will allow using Swift System in the toolchain stack, allowing us to bootstrap a toolchain. --- CMakeLists.txt | 29 ++++++++ Sources/CMakeLists.txt | 11 +++ Sources/CSystem/CMakeLists.txt | 15 ++++ Sources/CSystem/include/module.modulemap | 5 ++ Sources/System/CMakeLists.txt | 40 ++++++++++ cmake/modules/SwiftSupport.cmake | 94 ++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Sources/CMakeLists.txt create mode 100644 Sources/CSystem/CMakeLists.txt create mode 100644 Sources/CSystem/include/module.modulemap create mode 100644 Sources/System/CMakeLists.txt create mode 100644 cmake/modules/SwiftSupport.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..aff3fe1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +#[[ +This source file is part of the Swift System open source project + +Copyright (c) 2020 Apple Inc. and the Swift System project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +cmake_minimum_required(VERSION 3.16.0) +project(swift-system + LANGUAGES C Swift) + +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) + +include(SwiftSupport) + +add_subdirectory(Sources) + +get_property(SWIFT_SYSTEM_EXPORTS GLOBAL PROPERTY SWIFT_SYSTEM_EXPORTS) +export(TARGETS ${SWIFT_SYSTEM_EXPORTS} + NAMESPACE SwiftSystem:: + FILE swift-system-config.cmake + EXPORT_LINK_INTERFACE_LIBRARIES) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt new file mode 100644 index 0000000..d788b8c --- /dev/null +++ b/Sources/CMakeLists.txt @@ -0,0 +1,11 @@ +#[[ +This source file is part of the Swift System open source project + +Copyright (c) 2020 Apple Inc. and the Swift System project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_subdirectory(CSystem) +add_subdirectory(System) diff --git a/Sources/CSystem/CMakeLists.txt b/Sources/CSystem/CMakeLists.txt new file mode 100644 index 0000000..6dba7ff --- /dev/null +++ b/Sources/CSystem/CMakeLists.txt @@ -0,0 +1,15 @@ +#[[ +This source file is part of the Swift System open source project + +Copyright (c) 2020 Apple Inc. and the Swift System project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(CSystem INTERFACE) +target_include_directories(CSystem INTERFACE + include) + + +set_property(GLOBAL APPEND PROPERTY SWIFT_SYSTEM_EXPORTS CSystem) diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap new file mode 100644 index 0000000..776f776 --- /dev/null +++ b/Sources/CSystem/include/module.modulemap @@ -0,0 +1,5 @@ +module CSystem { + header "CSystemLinux.h" + header "CSystemWindows.h" + export * +} diff --git a/Sources/System/CMakeLists.txt b/Sources/System/CMakeLists.txt new file mode 100644 index 0000000..5ab79b3 --- /dev/null +++ b/Sources/System/CMakeLists.txt @@ -0,0 +1,40 @@ +#[[ +This source file is part of the Swift System open source project + +Copyright (c) 2020 Apple Inc. and the Swift System project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(System + Errno.swift + FileDescriptor.swift + FileHelpers.swift + FileOperations.swift + FilePermissions.swift + PlatformString.swift + SystemString.swift + Util.swift + UtilConsumers.swift) +target_sources(System PRIVATE + FilePath/FilePath.swift + FilePath/FilePathComponents.swift + FilePath/FilePathComponentView.swift + FilePath/FilePathParsing.swift + FilePath/FilePathString.swift + FilePath/FilePathSyntax.swift + FilePath/FilePathWindows.swift) +target_sources(System PRIVATE + Internals/CInterop.swift + Internals/Constants.swift + Internals/Exports.swift + Internals/Mocking.swift + Internals/Syscalls.swift + Internals/WindowsSyscallAdapters.swift) +target_link_libraries(System PRIVATE + CSystem) + + +_install_target(System) +set_property(GLOBAL APPEND PROPERTY SWIFT_SYSTEM_EXPORTS System) diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake new file mode 100644 index 0000000..915993c --- /dev/null +++ b/cmake/modules/SwiftSupport.cmake @@ -0,0 +1,94 @@ +#[[ +This source file is part of the Swift System open source project + +Copyright (c) 2020 Apple Inc. and the Swift System project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +# Returns the architecture name in a variable +# +# Usage: +# get_swift_host_arch(result_var_name) +# +# Sets ${result_var_name} with the converted architecture name derived from +# CMAKE_SYSTEM_PROCESSOR. +function(get_swift_host_arch result_var_name) + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") + set("${result_var_name}" "aarch64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64") + set("${result_var_name}" "powerpc64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") + set("${result_var_name}" "powerpc64le" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") + set("${result_var_name}" "s390x" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l") + set("${result_var_name}" "armv6" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a") + set("${result_var_name}" "armv7" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64") + set("${result_var_name}" "x86_64" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64") + set("${result_var_name}" "itanium" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86") + set("${result_var_name}" "i686" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") + set("${result_var_name}" "i686" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}") + endif() +endfunction() + +# Returns the os name in a variable +# +# Usage: +# get_swift_host_os(result_var_name) +# +# +# Sets ${result_var_name} with the converted OS name derived from +# CMAKE_SYSTEM_NAME. +function(get_swift_host_os result_var_name) + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) + set(${result_var_name} macosx PARENT_SCOPE) + else() + string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc) + set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE) + endif() +endfunction() + +function(_install_target module) + get_swift_host_os(swift_os) + get_target_property(type ${module} TYPE) + + if(type STREQUAL STATIC_LIBRARY) + set(swift swift_static) + else() + set(swift swift) + endif() + + install(TARGETS ${module} + ARCHIVE DESTINATION lib/${swift}/${swift_os} + LIBRARY DESTINATION lib/${swift}/${swift_os} + RUNTIME DESTINATION bin) + if(type STREQUAL EXECUTABLE) + return() + endif() + + get_swift_host_arch(swift_arch) + get_target_property(module_name ${module} Swift_MODULE_NAME) + if(NOT module_name) + set(module_name ${module}) + endif() + + install(FILES $/${module_name}.swiftdoc + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftdoc) + install(FILES $/${module_name}.swiftmodule + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftmodule) +endfunction()