Skip to content

Commit 95b8a48

Browse files
authored
Merge pull request #63964 from al45tair/backtracing/path-lookup
[Backtracing] Add support for looking up paths for auxiliary executables
2 parents 80bd274 + 14a0ec3 commit 95b8a48

File tree

9 files changed

+765
-0
lines changed

9 files changed

+765
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_getRuntimeLibraryPath();
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. The string is owned by the runtime
44+
/// and should not be freed.
45+
SWIFT_RUNTIME_EXPORT
46+
const char *
47+
swift_getRootPath();
48+
49+
/// Return the path of the specified auxiliary executable.
50+
///
51+
/// This function will search for the auxiliary executable in the following
52+
/// paths:
53+
///
54+
/// <swift-root>/libexec/swift/<platform>/<name>
55+
/// <swift-root>/libexec/swift/<name>
56+
/// <swift-root>/bin/<name>
57+
/// <swift-root>/<name>
58+
///
59+
/// It will return the first of those that exists, but it does not test that
60+
/// the file is indeed executable.
61+
///
62+
/// On Windows, it will automatically add `.exe` to the name, which means you
63+
/// do not need to special case the name for Windows.
64+
///
65+
/// If you are using this function to locate a utility program for use by the
66+
/// runtime, you should provide a way to override its location using an
67+
/// environment variable.
68+
///
69+
/// If the executable cannot be found, it will return nullptr.
70+
///
71+
/// \param name The name of the executable to locate.
72+
///
73+
/// \return A string containing the full path to the executable. This string
74+
/// should be released with `free()` when no longer required.
75+
SWIFT_RUNTIME_EXPORT
76+
char *
77+
swift_copyAuxiliaryExecutablePath(const char *name);
78+
79+
#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)