-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
0003-clang-add-conda-specific-env-var-CONDA_BUILD_SYSROOT.patch
157 lines (152 loc) · 6.06 KB
/
0003-clang-add-conda-specific-env-var-CONDA_BUILD_SYSROOT.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
From f957da489fc5d49f04051430965496d4708cf9f4 Mon Sep 17 00:00:00 2001
From: Nehal J Wani <[email protected]>
Date: Sat, 25 Aug 2018 09:20:04 -0500
Subject: [PATCH 3/9] clang: add conda specific env var CONDA_BUILD_SYSROOT
And also improve logic for finding the macos sysroot for conda
Co-authored-by: Chris Burr <[email protected]>
Co-authored-by: Vincenzo Eduardo Padulano <[email protected]>
Co-authored-by: Isuru Fernando <[email protected]>
---
clang/lib/Driver/Driver.cpp | 5 ++
clang/lib/Driver/ToolChains/Darwin.cpp | 76 +++++++++++++++++++++++++-
clang/lib/Lex/InitHeaderSearch.cpp | 6 +-
3 files changed, 84 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 8e44d5afa40e..9c66a52a9887 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1371,6 +1371,11 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
CompilerPath = Split.second;
}
}
+ if (std::optional<std::string> CondaBuildSysrootValue =
+ llvm::sys::Process::GetEnv("CONDA_BUILD_SYSROOT")) {
+ SysRoot = *CondaBuildSysrootValue;
+ }
+ // Override CONDA_BUILD_SYSROOT and consume sysroot option
if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
SysRoot = A->getValue();
if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 17d57b2f7eed..99747a4fb1aa 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -29,6 +29,18 @@
#include "llvm/TargetParser/Triple.h"
#include <cstdlib> // ::getenv
+// Extra includes for conda patch
+#include <cstdio>
+#include <memory>
+#include <optional>
+#include <string>
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Program.h"
+
using namespace clang::driver;
using namespace clang::driver::tools;
using namespace clang::driver::toolchains;
@@ -2217,15 +2229,75 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
if (!getVFS().exists(A->getValue()))
getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
} else {
+ std::string foundSDKPath = "/";
+ std::string prefixToTry = "/";
+
if (char *env = ::getenv("SDKROOT")) {
// We only use this value as the default if it is an absolute path,
// exists, and it is not the root path.
if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
StringRef(env) != "/") {
- Args.append(Args.MakeSeparateArg(
- nullptr, Opts.getOption(options::OPT_isysroot), env));
+ foundSDKPath = std::string(env);
+ }
+ }
+
+ if (foundSDKPath == "/") {
+ if (char *env = ::getenv("CONDA_BUILD_SYSROOT")) {
+ // We only use this value as the default if it is an absolute path,
+ // exists, and it is not the root path.
+ if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
+ StringRef(env) != "/") {
+ foundSDKPath = std::string(env);
+ }
}
}
+
+ // If the SDK is not found by now and it's not inside /, our only choice
+ // is to fail or ask Apple CLI utilities for help
+ if (foundSDKPath == "/" && !getVFS().exists("/usr/include/sys/types.h")) {
+
+ auto pathFromExecutable = [](llvm::ArrayRef<llvm::StringRef> Argv)
+ -> std::optional<std::string> {
+ // Inspired by clang-tools-extra/clangd/CompileCommands.cpp
+ auto Exe = llvm::sys::findProgramByName(Argv[0]);
+ if (!Exe) {
+ return std::nullopt;
+ }
+ llvm::SmallString<64> OutFile;
+ llvm::sys::fs::createTemporaryFile("darwin-adddeploymenttarget", "",
+ OutFile);
+ llvm::FileRemover OutRemover(OutFile);
+ std::optional<llvm::StringRef> Redirects[3] = {
+ /*stdin=*/{""}, /*stdout=*/{OutFile.str()}, /*stderr=*/{""}};
+ int Ret = llvm::sys::ExecuteAndWait(*Exe, Argv,
+ /*Env=*/std::nullopt, Redirects,
+ /*SecondsToWait=*/10);
+ if (Ret != 0) {
+ return std::nullopt;
+ }
+
+ auto Buf = llvm::MemoryBuffer::getFile(OutFile);
+ if (!Buf) {
+ return std::nullopt;
+ }
+ llvm::StringRef Path = Buf->get()->getBuffer().trim();
+ if (Path.empty()) {
+ return std::nullopt;
+ }
+ return Path.str();
+ };
+
+ auto XcrunPath = pathFromExecutable({"xcrun", "--show-sdk-path"});
+ if (foundSDKPath == "/" && XcrunPath &&
+ getVFS().exists(XcrunPath.value() + "/usr/include/sys/types.h")) {
+ foundSDKPath = XcrunPath.value();
+ }
+ }
+
+ if (foundSDKPath != "/") {
+ Args.append(Args.MakeSeparateArg(
+ nullptr, Opts.getOption(options::OPT_isysroot), foundSDKPath));
+ }
}
// Read the SDKSettings.json file for more information, like the SDK version
diff --git a/clang/lib/Lex/InitHeaderSearch.cpp b/clang/lib/Lex/InitHeaderSearch.cpp
index 2218db15013d..d05cfa38e4fb 100644
--- a/clang/lib/Lex/InitHeaderSearch.cpp
+++ b/clang/lib/Lex/InitHeaderSearch.cpp
@@ -24,6 +24,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"
#include <optional>
@@ -210,7 +211,10 @@ void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
[[fallthrough]];
default:
// FIXME: temporary hack: hard-coded paths.
- AddPath("/usr/local/include", System, false);
+ std::optional<std::string> CondaBuildSysrootValue =
+ llvm::sys::Process::GetEnv("CONDA_BUILD_SYSROOT");
+ if (!CondaBuildSysrootValue.has_value())
+ AddPath("/usr/local/include", System, false);
break;
}
}