Skip to content

Commit 75f7c25

Browse files
mbrockampcode-com
andcommitted
Add command-line flags for Nix-friendly resource path overrides
Add explicit flags to override Fil-C resource paths instead of relying solely on directory discovery relative to binary location: - --filc-resource-dir: Override pizfix root directory - --filc-dynamic-linker: Override ld-yolo-x86_64.so path - --filc-crt-path: Override CRT objects directory - --filc-stdfil-include: Override Fil-C runtime headers - --filc-os-include: Override kernel headers - --filc-include: Override libc headers These flags take precedence over automatic discovery but maintain backwards compatibility with existing pizfix and /opt/fil setups. This allows build systems like Nix to use wrappers with explicit paths without requiring specific directory layouts or copying resources into pizfix structures. Amp-Thread-ID: https://ampcode.com/threads/T-5bd1dd1a-c537-49c3-9b23-1b4e27ce4e81 Co-authored-by: Amp <[email protected]>
1 parent 8c2d405 commit 75f7c25

File tree

5 files changed

+90
-29
lines changed

5 files changed

+90
-29
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class Driver {
181181

182182
bool HasPizfix;
183183
bool HasOptfil;
184+
std::string PizfixRoot;
184185

185186
/// The original path to the clang executable.
186187
std::string ClangExecutable;

clang/include/clang/Driver/Options.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,24 @@ def gcc_toolchain : Joined<["--"], "gcc-toolchain=">, Flags<[NoXarchOption]>,
834834
"Flang will use the GCC installation with the largest version">;
835835
def gcc_triple_EQ : Joined<["--"], "gcc-triple=">,
836836
HelpText<"Search for the GCC installation with the specified triple.">;
837+
def filc_resource_dir : Joined<["--"], "filc-resource-dir=">, Flags<[NoXarchOption]>,
838+
Visibility<[ClangOption]>,
839+
HelpText<"Override Fil-C pizfix directory location (default: <binary>/../../../pizfix)">;
840+
def filc_dynamic_linker : Joined<["--"], "filc-dynamic-linker=">, Flags<[NoXarchOption]>,
841+
Visibility<[ClangOption]>,
842+
HelpText<"Override Fil-C dynamic linker path (default: pizfix/lib/ld-yolo-x86_64.so)">;
843+
def filc_crt_path : Joined<["--"], "filc-crt-path=">, Flags<[NoXarchOption]>,
844+
Visibility<[ClangOption]>,
845+
HelpText<"Override Fil-C CRT object files directory (default: pizfix/lib)">;
846+
def filc_stdfil_include : Joined<["--"], "filc-stdfil-include=">, Flags<[NoXarchOption]>,
847+
Visibility<[ClangOption]>,
848+
HelpText<"Override Fil-C stdfil include directory (default: pizfix/stdfil-include)">;
849+
def filc_os_include : Joined<["--"], "filc-os-include=">, Flags<[NoXarchOption]>,
850+
Visibility<[ClangOption]>,
851+
HelpText<"Override Fil-C OS include directory (default: pizfix/os-include)">;
852+
def filc_include : Joined<["--"], "filc-include=">, Flags<[NoXarchOption]>,
853+
Visibility<[ClangOption]>,
854+
HelpText<"Override Fil-C libc include directory (default: pizfix/include)">;
837855
def CC : Flag<["-"], "CC">, Visibility<[ClangOption, CC1Option]>,
838856
Group<Preprocessor_Group>,
839857
HelpText<"Include comments from within macros in preprocessed output">,

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
266266
SmallString<128> P(Dir);
267267
llvm::sys::path::append(P, "..", "..", "pizfix");
268268
HasPizfix = llvm::sys::fs::is_directory(P);
269+
if (HasPizfix)
270+
PizfixRoot = std::string(P);
269271
}
270272
if (!HasPizfix) {
271273
SmallString<128> RealPath;
@@ -1491,6 +1493,14 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
14911493
if (VFS->setCurrentWorkingDirectory(WD->getValue()))
14921494
Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
14931495

1496+
// Check for Fil-C resource directory override
1497+
if (Arg *A = Args.getLastArg(options::OPT_filc_resource_dir)) {
1498+
PizfixRoot = A->getValue();
1499+
HasPizfix = llvm::sys::fs::is_directory(PizfixRoot);
1500+
if (!HasPizfix)
1501+
Diag(diag::err_drv_no_such_file) << PizfixRoot;
1502+
}
1503+
14941504
// Check for missing include directories.
14951505
if (!Diags.isIgnored(diag::warn_missing_include_dirs, SourceLocation())) {
14961506
for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,17 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
594594

595595
auto GetYoloLibPath = [&] (const StringRef str) -> std::string {
596596
if (ToolChain.getDriver().HasPizfix) {
597-
SmallString<128> P(ToolChain.getDriver().Dir);
598-
llvm::sys::path::append(P, "..", "..", "pizfix");
599-
llvm::sys::path::append(P, "lib", str);
600-
return std::string(P);
597+
std::string BasePath;
598+
if (Arg *A = Args.getLastArg(options::OPT_filc_crt_path))
599+
BasePath = A->getValue().str();
600+
else {
601+
SmallString<128> P(ToolChain.getDriver().PizfixRoot);
602+
llvm::sys::path::append(P, "lib");
603+
BasePath = std::string(P);
604+
}
605+
SmallString<128> FullPath(BasePath);
606+
llvm::sys::path::append(FullPath, str);
607+
return std::string(FullPath);
601608
}
602609
if (ToolChain.getDriver().HasOptfil) {
603610
SmallString<128> P("/opt/fil/lib");
@@ -681,19 +688,29 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
681688
Args.AddAllArgs(CmdArgs, options::OPT_L);
682689

683690
if (ToolChain.getDriver().HasPizfix) {
691+
std::string BasePath;
692+
if (Arg *A = Args.getLastArg(options::OPT_filc_crt_path))
693+
BasePath = A->getValue().str();
694+
else {
695+
SmallString<128> P(ToolChain.getDriver().PizfixRoot);
696+
llvm::sys::path::append(P, "lib");
697+
BasePath = std::string(P);
698+
}
699+
684700
{
685-
SmallString<128> P(ToolChain.getDriver().Dir);
686-
llvm::sys::path::append(P, "..", "..", "pizfix", "lib64");
687-
CmdArgs.push_back(Args.MakeArgString("-L" + P));
688-
CmdArgs.push_back("-rpath");
689-
CmdArgs.push_back(Args.MakeArgString(P));
701+
SmallString<128> P(BasePath);
702+
llvm::sys::path::remove_filename(P);
703+
llvm::sys::path::append(P, "lib64");
704+
if (llvm::sys::fs::is_directory(P)) {
705+
CmdArgs.push_back(Args.MakeArgString("-L" + P));
706+
CmdArgs.push_back("-rpath");
707+
CmdArgs.push_back(Args.MakeArgString(P));
708+
}
690709
}
691710
{
692-
SmallString<128> P(ToolChain.getDriver().Dir);
693-
llvm::sys::path::append(P, "..", "..", "pizfix", "lib");
694-
CmdArgs.push_back(Args.MakeArgString("-L" + P));
711+
CmdArgs.push_back(Args.MakeArgString("-L" + BasePath));
695712
CmdArgs.push_back("-rpath");
696-
CmdArgs.push_back(Args.MakeArgString(P));
713+
CmdArgs.push_back(Args.MakeArgString(BasePath));
697714
}
698715
} else if (ToolChain.getDriver().HasOptfil) {
699716
CmdArgs.push_back("-L/opt/fil/lib");
@@ -738,20 +755,14 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
738755
options::OPT_r) ||
739756
Args.hasArg(options::OPT_normalcrt)) {
740757
if (ToolChain.getDriver().HasPizfix) {
741-
SmallString<128> P(ToolChain.getDriver().Dir);
742-
llvm::sys::path::append(P, "..", "..", "pizfix", "lib");
743-
llvm::sys::path::append(P, "filc_crt.o");
744-
CmdArgs.push_back(Args.MakeArgString(P));
758+
CmdArgs.push_back(Args.MakeArgString(GetYoloLibPath("filc_crt.o")));
745759
} else if (ToolChain.getDriver().HasOptfil)
746760
CmdArgs.push_back("/opt/fil/lib/filc_crt.o");
747761
else
748762
CmdArgs.push_back("/usr/lib/filc_crt.o");
749763
} else {
750764
if (ToolChain.getDriver().HasPizfix) {
751-
SmallString<128> P(ToolChain.getDriver().Dir);
752-
llvm::sys::path::append(P, "..", "..", "pizfix", "lib");
753-
llvm::sys::path::append(P, "filc_mincrt.o");
754-
CmdArgs.push_back(Args.MakeArgString(P));
765+
CmdArgs.push_back(Args.MakeArgString(GetYoloLibPath("filc_mincrt.o")));
755766
} else if (ToolChain.getDriver().HasOptfil)
756767
CmdArgs.push_back("/opt/fil/lib/filc_mincrt.o");
757768
else

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,12 @@ std::string Linux::getDynamicLinker(const ArgList &Args) const {
457457
const llvm::Triple &Triple = getTriple();
458458

459459
if ((true)) {
460+
// Check for explicit override flag
461+
if (Arg *A = Args.getLastArg(options::OPT_filc_dynamic_linker))
462+
return A->getValue().str();
463+
460464
if (getDriver().HasPizfix) {
461-
SmallString<128> P(getDriver().Dir);
462-
llvm::sys::path::append(P, "..", "..", "pizfix");
465+
SmallString<128> P(getDriver().PizfixRoot);
463466
llvm::sys::path::append(P, "lib", "ld-yolo-x86_64.so");
464467
return std::string(P);
465468
}
@@ -656,21 +659,39 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
656659

657660
if (D.HasPizfix) {
658661
{
659-
SmallString<128> P(D.Dir);
660-
llvm::sys::path::append(P, "..", "..", "pizfix", "stdfil-include");
662+
std::string P;
663+
if (Arg *A = DriverArgs.getLastArg(options::OPT_filc_stdfil_include))
664+
P = A->getValue().str();
665+
else {
666+
SmallString<128> Path(D.PizfixRoot);
667+
llvm::sys::path::append(Path, "stdfil-include");
668+
P = std::string(Path);
669+
}
661670
addSystemInclude(DriverArgs, CC1Args, P);
662671
}
663672

664673
{
665-
SmallString<128> P(D.Dir);
666-
llvm::sys::path::append(P, "..", "..", "pizfix", "os-include");
674+
std::string P;
675+
if (Arg *A = DriverArgs.getLastArg(options::OPT_filc_os_include))
676+
P = A->getValue().str();
677+
else {
678+
SmallString<128> Path(D.PizfixRoot);
679+
llvm::sys::path::append(Path, "os-include");
680+
P = std::string(Path);
681+
}
667682
addSystemInclude(DriverArgs, CC1Args, P);
668683
}
669684

670685
if (!DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)
671686
&& !DriverArgs.hasArg(options::OPT_nostdlibinc)) {
672-
SmallString<128> P(D.Dir);
673-
llvm::sys::path::append(P, "..", "..", "pizfix", "include");
687+
std::string P;
688+
if (Arg *A = DriverArgs.getLastArg(options::OPT_filc_include))
689+
P = A->getValue().str();
690+
else {
691+
SmallString<128> Path(D.PizfixRoot);
692+
llvm::sys::path::append(Path, "include");
693+
P = std::string(Path);
694+
}
674695
addSystemInclude(DriverArgs, CC1Args, P);
675696
}
676697
} else if (D.HasOptfil)

0 commit comments

Comments
 (0)