Skip to content

Commit 7538949

Browse files
authored
Split out CommandLine enum into a separate static library, allow removing it from stdlib (swiftlang#39591)
This is for the 'freestanding' build to stop assuming the platform has argc/argv. - Introduce a new sub-library, libswiftCommandLineSupport.a - Move stubs/CommandLine.cpp into this library - Conditionally embed it into libswiftCore - Conditionally embed it into libswiftPrivateLibcExtras if not in libswiftCore to support testing - Add SWIFT_STDLIB_HAS_COMMANDLINE CMake (and build-script) flag
1 parent 737962e commit 7538949

File tree

16 files changed

+70
-19
lines changed

16 files changed

+70
-19
lines changed

lib/Immediate/Immediate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ int swift::RunImmediately(CompilerInstance &CI,
239239
// This must be done here, before any library loading has been done, to avoid
240240
// racing with the static initializers in user code.
241241
// Setup interpreted process arguments.
242-
using ArgOverride = void (*)(const char **, int);
242+
using ArgOverride = void (* SWIFT_CC(swift))(const char **, int);
243243
#if defined(_WIN32)
244244
auto stdlib = loadSwiftRuntime(Context.SearchPathOpts.RuntimeLibraryPaths);
245245
if (!stdlib) {

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ option(SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
127127
"Build stdlib without a custom implementation of MetadataAllocator, relying on malloc+free instead."
128128
FALSE)
129129

130+
option(SWIFT_STDLIB_HAS_COMMANDLINE
131+
"Build stdlib with the CommandLine enum and support for argv/argc."
132+
TRUE)
133+
130134
option(SWIFT_BUILD_TEST_SUPPORT_MODULES
131135
"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."
132136
"${SWIFT_BUILD_TEST_SUPPORT_MODULES_default}")

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ function(_add_target_variant_swift_compile_flags
289289
list(APPEND result "-D" "SWIFT_RUNTIME_OS_VERSIONING")
290290
endif()
291291

292+
if(SWIFT_STDLIB_HAS_COMMANDLINE)
293+
list(APPEND result "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")
294+
endif()
295+
292296
if(SWIFT_STDLIB_HAS_STDIN)
293297
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
294298
endif()

stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ if(SWIFT_STDLIB_HAS_ENVIRON)
33
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
44
endif()
55

6+
set(swift_private_libc_extras_incorporate_object_libraries)
7+
if(SWIFT_STDLIB_HAS_COMMANDLINE)
8+
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")
9+
else()
10+
set(swift_private_libc_extras_incorporate_object_libraries "swiftCommandLineSupport")
11+
endif()
12+
613
add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
714
# This file should be listed the first. Module name is inferred from the
815
# filename.
@@ -25,5 +32,6 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
2532
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
2633
SWIFT_MODULE_DEPENDS_HAIKU Glibc
2734
SWIFT_MODULE_DEPENDS_WINDOWS CRT WinSDK
35+
INCORPORATE_OBJECT_LIBRARIES ${swift_private_libc_extras_incorporate_object_libraries}
2836
INSTALL_IN_COMPONENT stdlib-experimental
2937
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ public enum ProcessTerminationStatus : CustomStringConvertible {
5757
}
5858
}
5959

60+
#if !SWIFT_STDLIB_HAS_COMMANDLINE
61+
@_silgen_name("_swift_stdlib_getUnsafeArgvArgc")
62+
internal func _swift_stdlib_getUnsafeArgvArgc(_: UnsafeMutablePointer<Int32>) -> UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>
63+
64+
public enum CommandLine {
65+
public static var arguments: [String] = {
66+
var argc: Int32 = 0
67+
var unsafeArgv = _swift_stdlib_getUnsafeArgvArgc(&argc)
68+
return (0 ..< Int(argc)).map { String(cString: unsafeArgv[$0]!) }
69+
}()
70+
}
71+
#endif // !SWIFT_STDLIB_HAS_COMMANDLINE
6072

6173
#if os(Windows)
6274
public func spawnChild(_ args: [String])

stdlib/public/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if(CXX_SUPPORTS_EXIT_TIME_DESTRUCTORS_WARNING)
5555
endif()
5656

5757
add_subdirectory(SwiftShims)
58+
add_subdirectory(CommandLineSupport)
5859

5960
# This static library is shared across swiftCore and swiftReflection
6061
if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_swift_target_library(swiftCommandLineSupport
2+
STATIC DONT_EMBED_BITCODE NOSWIFTRT
3+
CommandLine.cpp
4+
C_COMPILE_FLAGS ${SWIFT_RUNTIME_CXX_FLAGS}
5+
LINK_FLAGS ${SWIFT_RUNTIME_LINK_FLAGS}
6+
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
7+
INSTALL_IN_COMPONENT never_install
8+
)

stdlib/public/stubs/CommandLine.cpp renamed to stdlib/public/CommandLineSupport/CommandLine.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
static char **_swift_stdlib_ProcessOverrideUnsafeArgv = nullptr;
4040
static int _swift_stdlib_ProcessOverrideUnsafeArgc = 0;
4141

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

54-
SWIFT_RUNTIME_STDLIB_API
55+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
5556
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
5657
assert(outArgLen != nullptr);
5758

@@ -64,7 +65,7 @@ char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
6465
return *_NSGetArgv();
6566
}
6667
#elif defined(__linux__) || defined(__CYGWIN__)
67-
SWIFT_RUNTIME_STDLIB_API
68+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
6869
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
6970
assert(outArgLen != nullptr);
7071

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

101-
SWIFT_RUNTIME_STDLIB_API
102+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
102103
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
103104
assert(outArgLen != nullptr);
104105

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

168-
SWIFT_RUNTIME_STDLIB_API
169+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
169170
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
170171
assert(outArgLen != nullptr);
171172

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

219-
SWIFT_RUNTIME_STDLIB_API
220+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
220221
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
221222
assert(outArgLen != nullptr);
222223

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

260-
SWIFT_RUNTIME_STDLIB_API
261+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
261262
char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
262263
assert(outArgLen != nullptr);
263264
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
@@ -289,7 +290,7 @@ char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
289290
return argv_copy;
290291
}
291292
#else // Add your favorite OS's command line arg grabber here.
292-
SWIFT_RUNTIME_STDLIB_API
293+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
293294
char **_swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
294295
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
295296
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;

stdlib/public/SwiftShims/RuntimeStubs.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ SWIFT_RUNTIME_STDLIB_API
3131
__swift_ssize_t
3232
swift_stdlib_readLine_stdin(unsigned char * _Nullable * _Nonnull LinePtr);
3333

34-
SWIFT_RUNTIME_STDLIB_API
35-
char * _Nullable * _Nonnull
36-
_swift_stdlib_getUnsafeArgvArgc(int * _Nonnull outArgLen);
37-
38-
SWIFT_RUNTIME_STDLIB_API
39-
void
40-
_swift_stdlib_overrideUnsafeArgvArgc(char * _Nullable * _Nonnull argv, int argc);
41-
4234
SWIFT_END_NULLABILITY_ANNOTATIONS
4335

4436
#ifdef __cplusplus

stdlib/public/core/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ if(SWIFT_CHECK_ESSENTIAL_STDLIB)
302302
target_link_libraries(swift_stdlib_essential ${RUNTIME_DEPENDENCY})
303303
endif()
304304

305+
set(swift_core_incorporate_object_libraries)
306+
list(APPEND swift_core_incorporate_object_libraries swiftRuntime)
307+
list(APPEND swift_core_incorporate_object_libraries swiftLLVMSupport)
308+
list(APPEND swift_core_incorporate_object_libraries swiftDemangling)
309+
list(APPEND swift_core_incorporate_object_libraries swiftStdlibStubs)
310+
if(SWIFT_STDLIB_HAS_COMMANDLINE)
311+
list(APPEND swift_core_incorporate_object_libraries swiftCommandLineSupport)
312+
endif()
313+
305314
set(swiftCore_common_options
306315
IS_STDLIB IS_STDLIB_CORE
307316
${SWIFTLIB_SOURCES}
@@ -314,7 +323,7 @@ set(swiftCore_common_options
314323
PRIVATE_LINK_LIBRARIES
315324
${swift_core_private_link_libraries}
316325
INCORPORATE_OBJECT_LIBRARIES
317-
swiftRuntime swiftLLVMSupport swiftDemangling swiftStdlibStubs
326+
${swift_core_incorporate_object_libraries}
318327
FRAMEWORK_DEPENDS
319328
${swift_core_framework_depends})
320329

0 commit comments

Comments
 (0)