Skip to content

Split out CommandLine enum into a separate static library, allow removing it from stdlib #39591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Immediate/Immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int swift::RunImmediately(CompilerInstance &CI,
// This must be done here, before any library loading has been done, to avoid
// racing with the static initializers in user code.
// Setup interpreted process arguments.
using ArgOverride = void (*)(const char **, int);
using ArgOverride = void (* SWIFT_CC(swift))(const char **, int);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find - I suspect that the WASM folks would appreciate this too

#if defined(_WIN32)
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPaths);
if (!stdlib) {
Expand Down
4 changes: 4 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ option(SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
"Build stdlib without a custom implementation of MetadataAllocator, relying on malloc+free instead."
FALSE)

option(SWIFT_STDLIB_HAS_COMMANDLINE
"Build stdlib with the CommandLine enum and support for argv/argc."
TRUE)

option(SWIFT_BUILD_TEST_SUPPORT_MODULES
"Whether to build StdlibUnittest and other test support modules. Defaults to On when SWIFT_BUILD_SDK_OVERLAY is On, or when SWIFT_INCLUDE_TESTS is On."
"${SWIFT_BUILD_TEST_SUPPORT_MODULES_default}")
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ function(_add_target_variant_swift_compile_flags
list(APPEND result "-D" "SWIFT_RUNTIME_OS_VERSIONING")
endif()

if(SWIFT_STDLIB_HAS_COMMANDLINE)
list(APPEND result "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")
endif()

if(SWIFT_STDLIB_HAS_STDIN)
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
endif()
Expand Down
8 changes: 8 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ if(SWIFT_STDLIB_HAS_ENVIRON)
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
endif()

set(swift_private_libc_extras_incorporate_object_libraries)
if(SWIFT_STDLIB_HAS_COMMANDLINE)
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")
else()
set(swift_private_libc_extras_incorporate_object_libraries "swiftCommandLineSupport")
endif()

add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
# This file should be listed the first. Module name is inferred from the
# filename.
Expand All @@ -25,5 +32,6 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
SWIFT_MODULE_DEPENDS_HAIKU Glibc
SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK
INCORPORATE_OBJECT_LIBRARIES ${swift_private_libc_extras_incorporate_object_libraries}
INSTALL_IN_COMPONENT stdlib-experimental
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")
12 changes: 12 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public enum ProcessTerminationStatus : CustomStringConvertible {
}
}

#if !SWIFT_STDLIB_HAS_COMMANDLINE
@_silgen_name("_swift_stdlib_getUnsafeArgvArgc")
internal func _swift_stdlib_getUnsafeArgvArgc(_: UnsafeMutablePointer<Int32>) -> UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>

public enum CommandLine {
public static var arguments: [String] = {
var argc: Int32 = 0
var unsafeArgv = _swift_stdlib_getUnsafeArgvArgc(&argc)
return (0 ..< Int(argc)).map { String(cString: unsafeArgv[$0]!) }
}()
}
#endif // !SWIFT_STDLIB_HAS_COMMANDLINE

#if os(Windows)
public func spawnChild(_ args: [String])
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if(CXX_SUPPORTS_EXIT_TIME_DESTRUCTORS_WARNING)
endif()

add_subdirectory(SwiftShims)
add_subdirectory(CommandLineSupport)

# This static library is shared across swiftCore and swiftReflection
if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/CommandLineSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_swift_target_library(swiftCommandLineSupport
STATIC DONT_EMBED_BITCODE NOSWIFTRT
CommandLine.cpp
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CXX_FLAGS}
LINK_FLAGS ${SWIFT_RUNTIME_LINK_FLAGS}
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
INSTALL_IN_COMPONENT never_install
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
static char **_swift_stdlib_ProcessOverrideUnsafeArgv = nullptr;
static int _swift_stdlib_ProcessOverrideUnsafeArgc = 0;

SWIFT_RUNTIME_STDLIB_API
// This needs to findable by dlopen() for JIT purposes (see Immediate.cpp).
SWIFT_CC(swift) extern "C" SWIFT_ATTRIBUTE_FOR_EXPORTS
void _swift_stdlib_overrideUnsafeArgvArgc(char **argv, int argc) {
_swift_stdlib_ProcessOverrideUnsafeArgv = argv;
_swift_stdlib_ProcessOverrideUnsafeArgc = argc;
Expand All @@ -51,7 +52,7 @@ void _swift_stdlib_overrideUnsafeArgvArgc(char **argv, int argc) {
extern "C" char ***_NSGetArgv(void);
extern "C" int *_NSGetArgc(void);

SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);

Expand All @@ -64,7 +65,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
return *_NSGetArgv();
}
#elif defined(__linux__) || defined(__CYGWIN__)
SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);

Expand Down Expand Up @@ -98,7 +99,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
#elif defined(_WIN32)
#include <stdlib.h>

SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);

Expand Down Expand Up @@ -165,7 +166,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
#include <sys/types.h>
#include <unistd.h>

SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);

Expand Down Expand Up @@ -216,7 +217,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
#include <wasi/api.h>
#include <wasi/libc.h>

SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);

Expand Down Expand Up @@ -257,7 +258,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
#include <sys/sysctl.h>
#include <sys/exec.h>

SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
assert(outArgLen != nullptr);
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
Expand Down Expand Up @@ -289,7 +290,7 @@ char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
return argv_copy;
}
#else // Add your favorite OS's command line arg grabber here.
SWIFT_RUNTIME_STDLIB_API
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
Expand Down
8 changes: 0 additions & 8 deletions stdlib/public/SwiftShims/RuntimeStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ SWIFT_RUNTIME_STDLIB_API
__swift_ssize_t
swift_stdlib_readLine_stdin(unsigned char * _Nullable * _Nonnull LinePtr);

SWIFT_RUNTIME_STDLIB_API
char * _Nullable * _Nonnull
_swift_stdlib_getUnsafeArgvArgc(int * _Nonnull outArgLen);

SWIFT_RUNTIME_STDLIB_API
void
_swift_stdlib_overrideUnsafeArgvArgc(char * _Nullable * _Nonnull argv, int argc);

SWIFT_END_NULLABILITY_ANNOTATIONS

#ifdef __cplusplus
Expand Down
11 changes: 10 additions & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ if(SWIFT_CHECK_ESSENTIAL_STDLIB)
target_link_libraries(swift_stdlib_essential ${RUNTIME_DEPENDENCY})
endif()

set(swift_core_incorporate_object_libraries)
list(APPEND swift_core_incorporate_object_libraries swiftRuntime)
list(APPEND swift_core_incorporate_object_libraries swiftLLVMSupport)
list(APPEND swift_core_incorporate_object_libraries swiftDemangling)
list(APPEND swift_core_incorporate_object_libraries swiftStdlibStubs)
if(SWIFT_STDLIB_HAS_COMMANDLINE)
list(APPEND swift_core_incorporate_object_libraries swiftCommandLineSupport)
endif()

set(swiftCore_common_options
IS_STDLIB IS_STDLIB_CORE
${SWIFTLIB_SOURCES}
Expand All @@ -314,7 +323,7 @@ set(swiftCore_common_options
PRIVATE_LINK_LIBRARIES
${swift_core_private_link_libraries}
INCORPORATE_OBJECT_LIBRARIES
swiftRuntime swiftLLVMSupport swiftDemangling swiftStdlibStubs
${swift_core_incorporate_object_libraries}
FRAMEWORK_DEPENDS
${swift_core_framework_depends})

Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

import SwiftShims

#if SWIFT_STDLIB_HAS_COMMANDLINE

@_silgen_name("_swift_stdlib_getUnsafeArgvArgc")
internal func _swift_stdlib_getUnsafeArgvArgc(_: UnsafeMutablePointer<Int32>)
-> UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>

/// Command-line arguments for the current process.
@frozen // namespace
public enum CommandLine {
Expand Down Expand Up @@ -49,3 +55,5 @@ public enum CommandLine {
public static var arguments: [String]
= (0..<Int(argc)).map { String(cString: _unsafeArgv[$0]!) }
}

#endif // SWIFT_STDLIB_HAS_COMMANDLINE
1 change: 0 additions & 1 deletion stdlib/public/stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
set(swift_stubs_sources
Assert.cpp
CommandLine.cpp
GlobalObjects.cpp
LibcShims.cpp
Random.cpp
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// UNSUPPORTED: OS=wasi

import StdlibUnittest
import SwiftPrivateLibcExtras
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/PrintFloat.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// UNSUPPORTED: CPU=arm64_32 && OS=watchos

import StdlibUnittest
import SwiftPrivateLibcExtras
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
Expand Down
1 change: 1 addition & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,7 @@ swift-stdlib-has-environ=0
swift-runtime-static-image-inspection=1
swift-stdlib-single-threaded-runtime=1
swift-stdlib-os-versioning=0
swift-stdlib-has-commandline=0
swift-stdlib-lto=full
extra-cmake-options=
-DSWIFT_ENABLE_DISPATCH:BOOL=FALSE
Expand Down
2 changes: 2 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ KNOWN_SETTINGS=(
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
swift-stdlib-single-threaded-runtime "0" "whether to build stdlib as a single-threaded runtime only"
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
swift-stdlib-has-commandline "1" "whether to build stdlib with the CommandLine enum and support for argv/argc"
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
swift-stdlib-has-asl "" "whether the stdlib can use the asl_log API, defaults to true on Darwin, false otherwise"
Expand Down Expand Up @@ -1990,6 +1991,7 @@ for host in "${ALL_HOSTS[@]}"; do
-DSWIFT_STDLIB_HAS_DLADDR:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLADDR}")
-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION:BOOL=$(true_false "${SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION}")
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
-DSWIFT_STDLIB_HAS_COMMANDLINE:BOOL=$(true_false "${SWIFT_STDLIB_HAS_COMMANDLINE}")
-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}")
-DSWIFT_STDLIB_HAS_STDIN:BOOL=$(true_false "${SWIFT_STDLIB_HAS_STDIN}")
-DSWIFT_STDLIB_HAS_ENVIRON:BOOL=$(true_false "${SWIFT_STDLIB_HAS_ENVIRON}")
Expand Down