Skip to content

Commit b614185

Browse files
authored
build: add a CMake based build system (#38)
This will allow using Swift System in the toolchain stack, allowing us to bootstrap a toolchain.
1 parent dd1da29 commit b614185

File tree

6 files changed

+194
-0
lines changed

6 files changed

+194
-0
lines changed

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
cmake_minimum_required(VERSION 3.16.0)
11+
project(swift-system
12+
LANGUAGES C Swift)
13+
14+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
15+
16+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
17+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
18+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
19+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
20+
21+
include(SwiftSupport)
22+
23+
add_subdirectory(Sources)
24+
25+
get_property(SWIFT_SYSTEM_EXPORTS GLOBAL PROPERTY SWIFT_SYSTEM_EXPORTS)
26+
export(TARGETS ${SWIFT_SYSTEM_EXPORTS}
27+
NAMESPACE SwiftSystem::
28+
FILE swift-system-config.cmake
29+
EXPORT_LINK_INTERFACE_LIBRARIES)

Sources/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_subdirectory(CSystem)
11+
add_subdirectory(System)

Sources/CSystem/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(CSystem INTERFACE)
11+
target_include_directories(CSystem INTERFACE
12+
include)
13+
14+
15+
set_property(GLOBAL APPEND PROPERTY SWIFT_SYSTEM_EXPORTS CSystem)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module CSystem {
2+
header "CSystemLinux.h"
3+
header "CSystemWindows.h"
4+
export *
5+
}

Sources/System/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(System
11+
Errno.swift
12+
FileDescriptor.swift
13+
FileHelpers.swift
14+
FileOperations.swift
15+
FilePermissions.swift
16+
PlatformString.swift
17+
SystemString.swift
18+
Util.swift
19+
UtilConsumers.swift)
20+
target_sources(System PRIVATE
21+
FilePath/FilePath.swift
22+
FilePath/FilePathComponents.swift
23+
FilePath/FilePathComponentView.swift
24+
FilePath/FilePathParsing.swift
25+
FilePath/FilePathString.swift
26+
FilePath/FilePathSyntax.swift
27+
FilePath/FilePathWindows.swift)
28+
target_sources(System PRIVATE
29+
Internals/CInterop.swift
30+
Internals/Constants.swift
31+
Internals/Exports.swift
32+
Internals/Mocking.swift
33+
Internals/Syscalls.swift
34+
Internals/WindowsSyscallAdapters.swift)
35+
target_link_libraries(System PRIVATE
36+
CSystem)
37+
38+
39+
_install_target(System)
40+
set_property(GLOBAL APPEND PROPERTY SWIFT_SYSTEM_EXPORTS System)

cmake/modules/SwiftSupport.cmake

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
# Returns the architecture name in a variable
11+
#
12+
# Usage:
13+
# get_swift_host_arch(result_var_name)
14+
#
15+
# Sets ${result_var_name} with the converted architecture name derived from
16+
# CMAKE_SYSTEM_PROCESSOR.
17+
function(get_swift_host_arch result_var_name)
18+
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
19+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
20+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
21+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
22+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
23+
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
24+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
25+
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
27+
set("${result_var_name}" "s390x" PARENT_SCOPE)
28+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
29+
set("${result_var_name}" "armv6" PARENT_SCOPE)
30+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
31+
set("${result_var_name}" "armv7" PARENT_SCOPE)
32+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
33+
set("${result_var_name}" "armv7" PARENT_SCOPE)
34+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
35+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
36+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
37+
set("${result_var_name}" "itanium" PARENT_SCOPE)
38+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
39+
set("${result_var_name}" "i686" PARENT_SCOPE)
40+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
41+
set("${result_var_name}" "i686" PARENT_SCOPE)
42+
else()
43+
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
44+
endif()
45+
endfunction()
46+
47+
# Returns the os name in a variable
48+
#
49+
# Usage:
50+
# get_swift_host_os(result_var_name)
51+
#
52+
#
53+
# Sets ${result_var_name} with the converted OS name derived from
54+
# CMAKE_SYSTEM_NAME.
55+
function(get_swift_host_os result_var_name)
56+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
57+
set(${result_var_name} macosx PARENT_SCOPE)
58+
else()
59+
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
60+
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
61+
endif()
62+
endfunction()
63+
64+
function(_install_target module)
65+
get_swift_host_os(swift_os)
66+
get_target_property(type ${module} TYPE)
67+
68+
if(type STREQUAL STATIC_LIBRARY)
69+
set(swift swift_static)
70+
else()
71+
set(swift swift)
72+
endif()
73+
74+
install(TARGETS ${module}
75+
ARCHIVE DESTINATION lib/${swift}/${swift_os}
76+
LIBRARY DESTINATION lib/${swift}/${swift_os}
77+
RUNTIME DESTINATION bin)
78+
if(type STREQUAL EXECUTABLE)
79+
return()
80+
endif()
81+
82+
get_swift_host_arch(swift_arch)
83+
get_target_property(module_name ${module} Swift_MODULE_NAME)
84+
if(NOT module_name)
85+
set(module_name ${module})
86+
endif()
87+
88+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
89+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
90+
RENAME ${swift_arch}.swiftdoc)
91+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
92+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
93+
RENAME ${swift_arch}.swiftmodule)
94+
endfunction()

0 commit comments

Comments
 (0)