Skip to content

[Backtracing] Add support for looking up paths for auxiliary executables #63964

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 8 commits into from
Mar 3, 2023
Merged
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
79 changes: 79 additions & 0 deletions include/swift/Runtime/Paths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//===--- 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_getRuntimeLibraryPath();

/// 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. The string is owned by the runtime
/// and should not be freed.
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. This string
/// should be released with `free()` when no longer required.
SWIFT_RUNTIME_EXPORT
char *
swift_copyAuxiliaryExecutablePath(const char *name);

#endif // SWIFT_RUNTIME_PATHS_H
1 change: 1 addition & 0 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 @@ -24,6 +25,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 @@ -124,6 +131,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