Skip to content

Add some utility functions for obtaining runtime-relative paths. #62462

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

Closed
wants to merge 8 commits into from
Closed
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
4 changes: 4 additions & 0 deletions include/swift/Runtime/EnvironmentVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ void initialize(void *);

extern swift::once_t initializeToken;

// Define a typedef "string" in swift::runtime::environment to make string
// environment variables work
using string = const char *;

// Declare backing variables.
#define VARIABLE(name, type, defaultValue, help) extern type name ## _variable;
#include "../../../stdlib/public/runtime/EnvironmentVariables.def"
Expand Down
77 changes: 77 additions & 0 deletions include/swift/Runtime/Paths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===--- Paths.h - Swift Runtime path utility functions ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Functions that obtain paths that might be useful within the runtime.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_RUNTIME_UTILS_H
#define SWIFT_RUNTIME_UTILS_H

#include "swift/Runtime/Config.h"

/// Return the path of the libswiftCore library.
///
/// This can be used to locate files that are installed alongside the Swift
/// runtime library.
///
/// \return A string containing the full path to libswiftCore. The string is
/// owned by the runtime and should not be freed.
SWIFT_RUNTIME_EXPORT
const char *
swift_getRuntimePath();

/// Return the path of the Swift root.
///
/// If the path to libswiftCore is `/usr/local/swift/lib/libswiftCore.dylib`,
/// this function would return `/usr/local/swift`.
///
/// The path returned here can be overridden by setting the environment variable
/// SWIFT_ROOT.
///
/// \return A string containing the full path to the Swift root directory, based
/// either on the location of the Swift runtime, or on the `SWIFT_ROOT`
/// environment variable if set.
SWIFT_RUNTIME_EXPORT
const char *
swift_getRootPath();

/// Return the path of the specified auxiliary executable.
///
/// This function will search for the auxiliary executable in the following
/// paths:
///
/// <swift-root>/libexec/swift/<platform>/<name>
/// <swift-root>/libexec/swift/<name>
/// <swift-root>/bin/<name>
/// <swift-root>/<name>
///
/// It will return the first of those that exists, but it does not test that
/// the file is indeed executable.
///
/// On Windows, it will automatically add `.exe` to the name, which means you
/// do not need to special case the name for Windows.
///
/// If you are using this function to locate a utility program for use by the
/// runtime, you should provide a way to override its location using an
/// environment variable.
///
/// If the executable cannot be found, it will return nullptr.
///
/// \param name The name of the executable to locate.
///
/// \return A string containing the full path to the executable.
SWIFT_RUNTIME_EXPORT
const char *
swift_getAuxiliaryExecutablePath(const char *name);

#endif // SWIFT_RUNTIME_PATHS_H
3 changes: 1 addition & 2 deletions include/swift/Threading/Impl/Win32/Win32Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ typedef struct _RTL_CONDITION_VARIABLE *PRTL_CONDITION_VARIABLE;
typedef PRTL_CONDITION_VARIABLE PCONDITION_VARIABLE;

// These have to be #defines, to avoid problems with <windows.h>
#define RTL_SRWLOCK_INIT \
{ 0 }
#define RTL_SRWLOCK_INIT {0}
#define SRWLOCK_INIT RTL_SRWLOCK_INIT
#define FLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)

Expand Down
6 changes: 5 additions & 1 deletion stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ function(_add_target_variant_c_compile_flags)

set(result ${${CFLAGS_RESULT_VAR_NAME}})

list(APPEND result "-DSWIFT_RUNTIME")
list(APPEND result
"-DSWIFT_RUNTIME"
"-DSWIFT_LIB_SUBDIR=\"${SWIFT_SDK_${CFLAGS_SDK}_LIB_SUBDIR}\""
"-DSWIFT_ARCH=\"${CFLAGS_ARCH}\""
)

if ("${CFLAGS_ARCH}" STREQUAL "arm64" OR
"${CFLAGS_ARCH}" STREQUAL "arm64_32")
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ set(swift_runtime_sources
MetadataLookup.cpp
Numeric.cpp
Once.cpp
Paths.cpp
Portability.cpp
ProtocolConformance.cpp
RefCount.cpp
Expand Down
15 changes: 15 additions & 0 deletions stdlib/public/runtime/EnvironmentVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

#include "swift/Runtime/Debug.h"
#include "swift/Runtime/Paths.h"
#include "swift/Runtime/EnvironmentVariables.h"

#include <string.h>
Expand All @@ -23,6 +24,12 @@ using namespace swift;

namespace {

// This is required to make the macro machinery work correctly; we can't
// declare a VARIABLE(..., const char *, ...) because then the token-pasted
// names won't work properly. It *does* mean that if you want to use std::string
// somewhere in this file, you'll have to fully qualify the name.
typedef const char *string;

// Require all environment variable names to start with SWIFT_
static constexpr bool hasSwiftPrefix(const char *str) {
const char prefix[] = "SWIFT_";
Expand Down Expand Up @@ -123,6 +130,14 @@ static uint32_t parse_uint32_t(const char *name,
return n;
}

static string parse_string(const char *name,
const char *value,
string defaultValue) {
if (!value || value[0] == 0)
return strdup(defaultValue);
return strdup(value);
}

// Print a list of all the environment variables. Lazy initialization makes
// this a bit odd, but the use of these variables in the metadata system means
// it's almost certain to run early.
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/runtime/EnvironmentVariables.def
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ VARIABLE(SWIFT_BINARY_COMPATIBILITY_VERSION, uint32_t, 0,
VARIABLE(SWIFT_DEBUG_FAILED_TYPE_LOOKUP, bool, false,
"Enable warnings when we fail to look up a type by name.")

VARIABLE(SWIFT_ROOT, string, "",
"Overrides the root directory of the Swift installation. "
"This is used to locate auxiliary files relative to the runtime "
"itself.")

#undef VARIABLE
Loading