Skip to content

Commit f02f62f

Browse files
committed
[Backtracing] Add support for looking up paths for auxiliary executables.
We need to be able to locate `swift-backtrace` relative to the current location of the runtime library. This needs to work: * In a Swift build directory. * On Darwin, where we're installed in /usr/lib/swift and /usr/libexec/swift. * On Linux, where we're in /usr/lib/swift/linux and /usr/libexec/swift/linux. * On Windows, where we may be in a flat directory layout (because of limitations of Windows DLL lookups). rdar://103071801
1 parent 60ededb commit f02f62f

File tree

8 files changed

+757
-0
lines changed

8 files changed

+757
-0
lines changed

include/swift/Runtime/EnvironmentVariables.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void initialize(void *);
2424

2525
extern swift::once_t initializeToken;
2626

27+
// Define a typedef "string" in swift::runtime::environment to make string
28+
// environment variables work
29+
using string = const char *;
30+
2731
// Declare backing variables.
2832
#define VARIABLE(name, type, defaultValue, help) extern type name ## _variable;
2933
#include "../../../stdlib/public/runtime/EnvironmentVariables.def"

include/swift/Runtime/Paths.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- Paths.h - Swift Runtime path utility functions ---------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Functions that obtain paths that might be useful within the runtime.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_RUNTIME_UTILS_H
18+
#define SWIFT_RUNTIME_UTILS_H
19+
20+
#include "swift/Runtime/Config.h"
21+
22+
/// Return the path of the libswiftCore library.
23+
///
24+
/// This can be used to locate files that are installed alongside the Swift
25+
/// runtime library.
26+
///
27+
/// \return A string containing the full path to libswiftCore. The string is
28+
/// owned by the runtime and should not be freed.
29+
SWIFT_RUNTIME_EXPORT
30+
const char *
31+
swift_getRuntimePath();
32+
33+
/// Return the path of the Swift root.
34+
///
35+
/// If the path to libswiftCore is `/usr/local/swift/lib/libswiftCore.dylib`,
36+
/// this function would return `/usr/local/swift`.
37+
///
38+
/// The path returned here can be overridden by setting the environment variable
39+
/// SWIFT_ROOT.
40+
///
41+
/// \return A string containing the full path to the Swift root directory, based
42+
/// either on the location of the Swift runtime, or on the `SWIFT_ROOT`
43+
/// environment variable if set.
44+
SWIFT_RUNTIME_EXPORT
45+
const char *
46+
swift_getRootPath();
47+
48+
/// Return the path of the specified auxiliary executable.
49+
///
50+
/// This function will search for the auxiliary executable in the following
51+
/// paths:
52+
///
53+
/// <swift-root>/libexec/swift/<platform>/<name>
54+
/// <swift-root>/libexec/swift/<name>
55+
/// <swift-root>/bin/<name>
56+
/// <swift-root>/<name>
57+
///
58+
/// It will return the first of those that exists, but it does not test that
59+
/// the file is indeed executable.
60+
///
61+
/// On Windows, it will automatically add `.exe` to the name, which means you
62+
/// do not need to special case the name for Windows.
63+
///
64+
/// If you are using this function to locate a utility program for use by the
65+
/// runtime, you should provide a way to override its location using an
66+
/// environment variable.
67+
///
68+
/// If the executable cannot be found, it will return nullptr.
69+
///
70+
/// \param name The name of the executable to locate.
71+
///
72+
/// \return A string containing the full path to the executable.
73+
SWIFT_RUNTIME_EXPORT
74+
const char *
75+
swift_getAuxiliaryExecutablePath(const char *name);
76+
77+
#endif // SWIFT_RUNTIME_PATHS_H

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(swift_runtime_sources
6262
MetadataLookup.cpp
6363
Numeric.cpp
6464
Once.cpp
65+
Paths.cpp
6566
Portability.cpp
6667
ProtocolConformance.cpp
6768
RefCount.cpp

stdlib/public/runtime/EnvironmentVariables.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Runtime/Debug.h"
18+
#include "swift/Runtime/Paths.h"
1819
#include "swift/Runtime/EnvironmentVariables.h"
1920

2021
#include <string.h>
@@ -24,6 +25,12 @@ using namespace swift;
2425

2526
namespace {
2627

28+
// This is required to make the macro machinery work correctly; we can't
29+
// declare a VARIABLE(..., const char *, ...) because then the token-pasted
30+
// names won't work properly. It *does* mean that if you want to use std::string
31+
// somewhere in this file, you'll have to fully qualify the name.
32+
typedef const char *string;
33+
2734
// Require all environment variable names to start with SWIFT_
2835
static constexpr bool hasSwiftPrefix(const char *str) {
2936
const char prefix[] = "SWIFT_";
@@ -124,6 +131,14 @@ static uint32_t parse_uint32_t(const char *name,
124131
return n;
125132
}
126133

134+
static string parse_string(const char *name,
135+
const char *value,
136+
string defaultValue) {
137+
if (!value || value[0] == 0)
138+
return strdup(defaultValue);
139+
return strdup(value);
140+
}
141+
127142
// Print a list of all the environment variables. Lazy initialization makes
128143
// this a bit odd, but the use of these variables in the metadata system means
129144
// it's almost certain to run early.

stdlib/public/runtime/EnvironmentVariables.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ VARIABLE(SWIFT_BINARY_COMPATIBILITY_VERSION, uint32_t, 0,
8181
VARIABLE(SWIFT_DEBUG_FAILED_TYPE_LOOKUP, bool, false,
8282
"Enable warnings when we fail to look up a type by name.")
8383

84+
VARIABLE(SWIFT_ROOT, string, "",
85+
"Overrides the root directory of the Swift installation. "
86+
"This is used to locate auxiliary files relative to the runtime "
87+
"itself.")
88+
8489
#undef VARIABLE

0 commit comments

Comments
 (0)