Skip to content

Commit f3b5fcd

Browse files
committed
Add stubs library to build
Adding stdlib stubs library, which uses gyb and requires knowing about some of the platform info. The stubs library pulls in several headers from the compiler repository. We should probably clean that up, but not right now. I've made a note of it.
1 parent 9642995 commit f3b5fcd

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed

Runtimes/Core/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
#
1313
#
1414
# gyb pulls sources from compiler sources
15-
15+
#
16+
# Stubs:
17+
# Pulls in headers from compiler
18+
# - include/swift/Basic
19+
# - include/swift/Runtime
20+
# - include/swift/Threading
1621

1722
# TODO:
1823
# Platform support:
@@ -43,6 +48,8 @@ set(SwiftCore_SWIFTC_SOURCE_DIR
4348

4449
include(CompilerSettings)
4550
include(DefaultSettings)
51+
include(PlatformInfo)
52+
include(gyb)
4653

4754
option(SwiftCore_ENABLE_CRASH_REPORTER_CLIENT "Enable Apple CrashReporter integration" OFF)
4855
option(SwiftCore_ENABLE_OBJC_INTEROP "Enable runtime ObjC interop" OFF)
@@ -69,3 +76,4 @@ add_subdirectory(SwiftShims/swift/shims)
6976
add_subdirectory(Demangling)
7077
add_subdirectory(Threading)
7178
add_subdirectory(runtime)
79+
add_subdirectory(stubs)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
if(NOT SwiftCore_SIZEOF_POINTER)
2+
set(SwiftCore_SIZEOF_POINTER "${CMAKE_SIZEOF_VOID_P}" CACHE STRING "Size of a pointer in bytes")
3+
message(CONFIGURE_LOG "Stdlib Pointer size: ${CMAKE_SIZEOF_VOID_P}")
4+
mark_as_advanced(SwiftCore_SIZEOF_POINTER)
5+
endif()
6+
7+
if(NOT SwiftCore_MODULE_TRIPLE)
8+
# TODO: This logic should migrate to CMake once CMake supports installing swiftmodules
9+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
10+
if(CMAKE_Swift_COMPILER_TARGET)
11+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
12+
endif()
13+
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
14+
message(CONFIGURE_LOG "Swift target info: ${module_triple_command}\n"
15+
"${target_info_json}")
16+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
17+
set(SwiftCore_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
18+
mark_as_advanced(SwiftCore_MODULE_TRIPLE)
19+
endif()
20+
21+
if(NOT SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR)
22+
# TODO: We need to separate the concept of compiler resources and the stdlib.
23+
# Compiler-resources in the compiler-resource directory are specific to
24+
# a given compiler. The headers in `lib/clang/include` and
25+
# `lib/swift/clang/include` correspond with that specific copy clang and
26+
# should not be mixed. This won't cause modularization issues because
27+
# the one copy of clang should never be looking in the other's resource
28+
# directory. If there are issues here, something has gone horribly wrong
29+
# and you're looking in the wrong place.
30+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
31+
if(CMAKE_Swift_COMPILER_TARGET)
32+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
33+
endif()
34+
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
35+
string(JSON resource_dir GET "${target_info_json}" "paths" "runtimeResourcePath")
36+
cmake_path(APPEND resource_dir "clang")
37+
message(CONFIGURE_LOG "Using Swift clang-importer headers from toolchain Swift\n"
38+
"clang-importer header directory: ${resource_dir}")
39+
set(SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR "${resource_dir}"
40+
CACHE FILEPATH "Swift clang-importer resource directory")
41+
mark_as_advanced(SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR)
42+
endif()

Runtimes/Core/cmake/modules/gyb.cmake

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
find_package(Python3 REQUIRED)
2+
3+
# Create a target to expand a gyb source
4+
# target_name: Name of the target
5+
# FLAGS list of flags passed to gyb
6+
# DEPENDS list of dependencies
7+
# COMMENT Custom comment
8+
function(gyb_expand source output)
9+
set(flags)
10+
set(arguments)
11+
set(multival_arguments FLAGS DEPENDS)
12+
cmake_parse_arguments(GYB "${flags}" "${arguments}" "${multival_arguments}" ${ARGN})
13+
14+
get_filename_component(full_output_path ${output} ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
15+
get_filename_component(dir "${full_output_path}" DIRECTORY)
16+
get_filename_component(fname "${full_output_path}" NAME)
17+
18+
file(READ "${source}" gyb_src)
19+
string(REGEX MATCHALL "\\\$\{[\r\n\t ]*gyb.expand\\\([\r\n\t ]*[\'\"]([^\'\"]*)[\'\"]" gyb_expand_matches "${gyb_src}")
20+
foreach(match ${gyb_expand_matches})
21+
string(REGEX MATCH "[\'\"]\([^\'\"]*\)[\'\"]" gyb_dep "${match}")
22+
list(APPEND gyb_expand_deps "${CMAKE_MATCH_1}")
23+
endforeach()
24+
list(REMOVE_DUPLICATES gyb_expand_deps)
25+
26+
set(utils_dir "${PROJECT_SOURCE_DIR}/../../utils/")
27+
set(gyb_tool "${utils_dir}/gyb")
28+
29+
# All the tidbits to track for changes
30+
list(APPEND GYB_DEPENDS
31+
"${source}"
32+
"${utils_dir}/GYBUnicodeDataUtils.py"
33+
"${utils_dir}/SwiftIntTypes.py"
34+
"${utils_dir}/SwiftFloatingPointTypes.py"
35+
"${utils_dir}/UnicodeData/GraphemeBreakProperty.txt"
36+
"${utils_dir}/UnicodeData/GraphemeBreakTest.txt"
37+
"${utils_dir}/gyb_stdlib_support.py")
38+
add_custom_command(
39+
OUTPUT "${full_output_path}"
40+
COMMAND "${CMAKE_COMMAND}" -E make_directory "${dir}"
41+
COMMAND "${CMAKE_COMMAND}" -E env "$<TARGET_FILE:Python3::Interpreter>" "${gyb_tool}" ${GYB_FLAGS} -o "${full_output_path}.tmp" "${source}"
42+
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${full_output_path}.tmp" "${full_output_path}"
43+
COMMAND "${CMAKE_COMMAND}" -E remove "${full_output_path}.tmp"
44+
DEPENDS ${gyb_tool} ${gyb_tool}.py ${GYB_DEPENDS} ${gyb_expand_deps}
45+
COMMENT "Generating GYB source ${fname} from ${source}"
46+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
47+
endfunction()

Runtimes/Core/stubs/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
add_library(swiftStdlibStubs OBJECT
2+
Assert.cpp
3+
GlobalObjects.cpp
4+
LibcShims.cpp
5+
Random.cpp
6+
Stubs.cpp
7+
ThreadLocalStorage.cpp
8+
MathStubs.cpp
9+
Unicode/UnicodeData.cpp
10+
Unicode/UnicodeGrapheme.cpp
11+
Unicode/UnicodeNormalization.cpp
12+
Unicode/UnicodeScalarProps.cpp
13+
Unicode/UnicodeWord.cpp)
14+
15+
if(SwiftCore_ENABLE_OBJC_INTEROP)
16+
gyb_expand(SwiftNativeNSXXXBase.mm.gyb "SwiftNativeNSXXXBase.mm")
17+
target_sources(swiftStdlibStubs PRIVATE
18+
# ObjC files
19+
Availability.mm
20+
FoundationHelpers.mm
21+
OptionalBridgingHelper.mm
22+
Reflection.mm
23+
SwiftNativeNSObject.mm
24+
SwiftNativeNSXXXBaseARC.m
25+
"${CMAKE_CURRENT_BINARY_DIR}/SwiftNativeNSXXXBase.mm")
26+
endif()
27+
28+
29+
target_compile_definitions(swiftStdlibStubs PRIVATE swiftCore_EXPORTS)
30+
target_link_libraries(swiftStdlibStubs PRIVATE swiftShims)
31+
target_include_directories(swiftStdlibStubs PRIVATE
32+
"${PROJECT_BINARY_DIR}/include"
33+
# FIXME: pulls in headers from the main compiler build
34+
# Assert.cpp:
35+
# swift/Runtime/Config.h
36+
# swift/Runtime/Debug.h
37+
# swift/Runtime/Portability.h
38+
# Stubs.cpp:
39+
# swift/Runtime/Debug.h
40+
# GlobalObjects.cpp:
41+
# swift/Basic/type_traits.h
42+
# Random.cpp:
43+
# swift/Runtime/Config.h
44+
# UnicodeScalarProps.cpp:
45+
# swift/Runtime/Debug.h
46+
# ...
47+
#
48+
"${PROJECT_SOURCE_DIR}/../../include")
49+
50+
if(APPLE)
51+
set_property(SOURCE SwiftNativeNSXXXBaseARC.m
52+
APPEND_STRING
53+
PROPERTY COMPILE_FLAGS "-fobjc-arc")
54+
endif()

Runtimes/Resync.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ set(CoreLibs
5656
LLVMSupport
5757
SwiftShims
5858
runtime
59-
CompatibilityOverride)
59+
CompatibilityOverride
60+
stubs)
6061

6162
# Add these as we get them building
6263
# core

0 commit comments

Comments
 (0)