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.
This commit is contained in:
Evan Wilde
2024-10-15 23:36:07 -07:00
parent 96429952bb
commit f3b5fcdc15
5 changed files with 154 additions and 2 deletions

View File

@@ -12,7 +12,12 @@
# #
# #
# gyb pulls sources from compiler sources # gyb pulls sources from compiler sources
#
# Stubs:
# Pulls in headers from compiler
# - include/swift/Basic
# - include/swift/Runtime
# - include/swift/Threading
# TODO: # TODO:
# Platform support: # Platform support:
@@ -43,6 +48,8 @@ set(SwiftCore_SWIFTC_SOURCE_DIR
include(CompilerSettings) include(CompilerSettings)
include(DefaultSettings) include(DefaultSettings)
include(PlatformInfo)
include(gyb)
option(SwiftCore_ENABLE_CRASH_REPORTER_CLIENT "Enable Apple CrashReporter integration" OFF) option(SwiftCore_ENABLE_CRASH_REPORTER_CLIENT "Enable Apple CrashReporter integration" OFF)
option(SwiftCore_ENABLE_OBJC_INTEROP "Enable runtime ObjC interop" OFF) option(SwiftCore_ENABLE_OBJC_INTEROP "Enable runtime ObjC interop" OFF)
@@ -69,3 +76,4 @@ add_subdirectory(SwiftShims/swift/shims)
add_subdirectory(Demangling) add_subdirectory(Demangling)
add_subdirectory(Threading) add_subdirectory(Threading)
add_subdirectory(runtime) add_subdirectory(runtime)
add_subdirectory(stubs)

View File

@@ -0,0 +1,42 @@
if(NOT SwiftCore_SIZEOF_POINTER)
set(SwiftCore_SIZEOF_POINTER "${CMAKE_SIZEOF_VOID_P}" CACHE STRING "Size of a pointer in bytes")
message(CONFIGURE_LOG "Stdlib Pointer size: ${CMAKE_SIZEOF_VOID_P}")
mark_as_advanced(SwiftCore_SIZEOF_POINTER)
endif()
if(NOT SwiftCore_MODULE_TRIPLE)
# TODO: This logic should migrate to CMake once CMake supports installing swiftmodules
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)
message(CONFIGURE_LOG "Swift target info: ${module_triple_command}\n"
"${target_info_json}")
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
set(SwiftCore_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
mark_as_advanced(SwiftCore_MODULE_TRIPLE)
endif()
if(NOT SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR)
# TODO: We need to separate the concept of compiler resources and the stdlib.
# Compiler-resources in the compiler-resource directory are specific to
# a given compiler. The headers in `lib/clang/include` and
# `lib/swift/clang/include` correspond with that specific copy clang and
# should not be mixed. This won't cause modularization issues because
# the one copy of clang should never be looking in the other's resource
# directory. If there are issues here, something has gone horribly wrong
# and you're looking in the wrong place.
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 resource_dir GET "${target_info_json}" "paths" "runtimeResourcePath")
cmake_path(APPEND resource_dir "clang")
message(CONFIGURE_LOG "Using Swift clang-importer headers from toolchain Swift\n"
"clang-importer header directory: ${resource_dir}")
set(SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR "${resource_dir}"
CACHE FILEPATH "Swift clang-importer resource directory")
mark_as_advanced(SwiftCore_SWIFTC_CLANGIMPORTER_RESOURCE_DIR)
endif()

View File

@@ -0,0 +1,47 @@
find_package(Python3 REQUIRED)
# Create a target to expand a gyb source
# target_name: Name of the target
# FLAGS list of flags passed to gyb
# DEPENDS list of dependencies
# COMMENT Custom comment
function(gyb_expand source output)
set(flags)
set(arguments)
set(multival_arguments FLAGS DEPENDS)
cmake_parse_arguments(GYB "${flags}" "${arguments}" "${multival_arguments}" ${ARGN})
get_filename_component(full_output_path ${output} ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
get_filename_component(dir "${full_output_path}" DIRECTORY)
get_filename_component(fname "${full_output_path}" NAME)
file(READ "${source}" gyb_src)
string(REGEX MATCHALL "\\\$\{[\r\n\t ]*gyb.expand\\\([\r\n\t ]*[\'\"]([^\'\"]*)[\'\"]" gyb_expand_matches "${gyb_src}")
foreach(match ${gyb_expand_matches})
string(REGEX MATCH "[\'\"]\([^\'\"]*\)[\'\"]" gyb_dep "${match}")
list(APPEND gyb_expand_deps "${CMAKE_MATCH_1}")
endforeach()
list(REMOVE_DUPLICATES gyb_expand_deps)
set(utils_dir "${PROJECT_SOURCE_DIR}/../../utils/")
set(gyb_tool "${utils_dir}/gyb")
# All the tidbits to track for changes
list(APPEND GYB_DEPENDS
"${source}"
"${utils_dir}/GYBUnicodeDataUtils.py"
"${utils_dir}/SwiftIntTypes.py"
"${utils_dir}/SwiftFloatingPointTypes.py"
"${utils_dir}/UnicodeData/GraphemeBreakProperty.txt"
"${utils_dir}/UnicodeData/GraphemeBreakTest.txt"
"${utils_dir}/gyb_stdlib_support.py")
add_custom_command(
OUTPUT "${full_output_path}"
COMMAND "${CMAKE_COMMAND}" -E make_directory "${dir}"
COMMAND "${CMAKE_COMMAND}" -E env "$<TARGET_FILE:Python3::Interpreter>" "${gyb_tool}" ${GYB_FLAGS} -o "${full_output_path}.tmp" "${source}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${full_output_path}.tmp" "${full_output_path}"
COMMAND "${CMAKE_COMMAND}" -E remove "${full_output_path}.tmp"
DEPENDS ${gyb_tool} ${gyb_tool}.py ${GYB_DEPENDS} ${gyb_expand_deps}
COMMENT "Generating GYB source ${fname} from ${source}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endfunction()

View File

@@ -0,0 +1,54 @@
add_library(swiftStdlibStubs OBJECT
Assert.cpp
GlobalObjects.cpp
LibcShims.cpp
Random.cpp
Stubs.cpp
ThreadLocalStorage.cpp
MathStubs.cpp
Unicode/UnicodeData.cpp
Unicode/UnicodeGrapheme.cpp
Unicode/UnicodeNormalization.cpp
Unicode/UnicodeScalarProps.cpp
Unicode/UnicodeWord.cpp)
if(SwiftCore_ENABLE_OBJC_INTEROP)
gyb_expand(SwiftNativeNSXXXBase.mm.gyb "SwiftNativeNSXXXBase.mm")
target_sources(swiftStdlibStubs PRIVATE
# ObjC files
Availability.mm
FoundationHelpers.mm
OptionalBridgingHelper.mm
Reflection.mm
SwiftNativeNSObject.mm
SwiftNativeNSXXXBaseARC.m
"${CMAKE_CURRENT_BINARY_DIR}/SwiftNativeNSXXXBase.mm")
endif()
target_compile_definitions(swiftStdlibStubs PRIVATE swiftCore_EXPORTS)
target_link_libraries(swiftStdlibStubs PRIVATE swiftShims)
target_include_directories(swiftStdlibStubs PRIVATE
"${PROJECT_BINARY_DIR}/include"
# FIXME: pulls in headers from the main compiler build
# Assert.cpp:
# swift/Runtime/Config.h
# swift/Runtime/Debug.h
# swift/Runtime/Portability.h
# Stubs.cpp:
# swift/Runtime/Debug.h
# GlobalObjects.cpp:
# swift/Basic/type_traits.h
# Random.cpp:
# swift/Runtime/Config.h
# UnicodeScalarProps.cpp:
# swift/Runtime/Debug.h
# ...
#
"${PROJECT_SOURCE_DIR}/../../include")
if(APPLE)
set_property(SOURCE SwiftNativeNSXXXBaseARC.m
APPEND_STRING
PROPERTY COMPILE_FLAGS "-fobjc-arc")
endif()

View File

@@ -56,7 +56,8 @@ set(CoreLibs
LLVMSupport LLVMSupport
SwiftShims SwiftShims
runtime runtime
CompatibilityOverride) CompatibilityOverride
stubs)
# Add these as we get them building # Add these as we get them building
# core # core