[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits

https://github.com/ldionne edited 
https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -356,6 +363,12 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public MachO {
   void addProfileRTLibs(const llvm::opt::ArgList ,
 llvm::opt::ArgStringList ) const override;
 
+  // Return the full path of the compiler-rt library on a Darwin MachO system.
+  // Those are under /lib/darwin/<...>(.dylib|.a).

ldionne wrote:

```suggestion
  // Those are under /lib/darwin/<...>.a.
```

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -223,6 +223,11 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
 // There aren't any profiling libs for embedded targets currently.
   }
 
+  // Return the full path of the compiler-rt library on a Darwin MachO system.
+  // Those are under /lib/darwin/<...>(.dylib|.a).
+  std::string getCompilerRT(const llvm::opt::ArgList , StringRef 
Component,

ldionne wrote:

I actually don't know much about these targets, they're kinda mysterious to me 
TBH, so I don't know what to document.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -223,6 +223,13 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
 // There aren't any profiling libs for embedded targets currently.
   }
 
+  // Return the full path of the compiler-rt library on a non-Darwin MachO
+  // system. Those are under
+  // /lib/darwin/macho_embedded/<...>(.dylib|.a).

ldionne wrote:

```suggestion
  // /lib/darwin/macho_embedded/<...>.a.
```

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -1303,18 +1288,55 @@ void MachO::AddLinkRuntimeLib(const ArgList , 
ArgStringList ,
   // rpaths. This is currently true from this place, but we need to be
   // careful if this function is ever called before user's rpaths are emitted.
   if (Opts & RLO_AddRPath) {
-assert(DarwinLibName.ends_with(".dylib") && "must be a dynamic library");
+assert(StringRef(P).ends_with(".dylib") && "must be a dynamic library");
 
 // Add @executable_path to rpath to support having the dylib copied with
 // the executable.
 CmdArgs.push_back("-rpath");
 CmdArgs.push_back("@executable_path");
 
-// Add the path to the resource dir to rpath to support using the dylib
-// from the default location without copying.
+// Add the compiler-rt library's directory to rpath to support using the
+// dylib from the default location without copying.
 CmdArgs.push_back("-rpath");
-CmdArgs.push_back(Args.MakeArgString(Dir));
+CmdArgs.push_back(Args.MakeArgString(llvm::sys::path::parent_path(P)));
+  }
+}
+
+std::string MachO::getCompilerRT(const ArgList &, StringRef Component,
+ FileType Type) const {
+  assert(Type != ToolChain::FT_Object &&
+ "it doesn't make sense to ask for the compiler-rt library name as an "
+ "object file");
+  SmallString<64> MachOLibName = StringRef("libclang_rt");
+  // On MachO, the builtins component is not in the library name
+  if (Component != "builtins") {
+MachOLibName += '.';
+MachOLibName += Component;
+  }
+  MachOLibName += Type == ToolChain::FT_Shared ? "_dynamic.dylib" : ".a";

ldionne wrote:

We only ever have `.a`s in there, so I think we should unconditionally use the 
`.a` extension to avoid creating confusion.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,97 @@
+// Test the output of -print-libgcc-file-name on Darwin.

ldionne wrote:

It looks like the embedded targets are already tested to some extent inside 
`clang/test/Driver/darwin-embedded.c`. I would add a new test file 
`clang/test/Driver/darwin-embedded-print-libgcc-file-name.c` and add something 
like this in it:

```
// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=armv7em-apple-darwin \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-HARD_STATIC %s
// CHECK-CLANGRT-HARD_STATIC: libclang_rt.hard_static.a

// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=armv7em-apple-darwin -msoft-float \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-SOFT_STATIC %s
// CHECK-CLANGRT-SOFT_STATIC: libclang_rt.soft_static.a

// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=armv7em-apple-darwin -fPIC \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-HARD_PIC %s
// CHECK-CLANGRT-HARD_PIC: libclang_rt.hard_pic.a

// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=armv7em-apple-darwin -msoft-float -fPIC \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-SOFT_PIC %s
// CHECK-CLANGRT-SOFT_PIC: libclang_rt.soft_pic.a
```

I'm not certain what we're testing, but we're testing the behavior that seems 
to exist today, so at least we'll have coverage for whatever we do today.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,97 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+//
+// Simulators
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS-SIMULATOR %s
+// CHECK-CLANGRT-IOS-SIMULATOR: libclang_rt.iossim.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS-SIMULATOR %s
+// CHECK-CLANGRT-WATCHOS-SIMULATOR: libclang_rt.watchossim.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS-SIMULATOR %s
+// CHECK-CLANGRT-TVOS-SIMULATOR: libclang_rt.tvossim.a
+
+//
+// Check the cc_kext variants
+//
+
+// TODO

ldionne wrote:

I also can't find how to reach those, so I think it would be reasonable to drop 
this test.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,97 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+//
+// Simulators
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS-SIMULATOR %s
+// CHECK-CLANGRT-IOS-SIMULATOR: libclang_rt.iossim.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS-SIMULATOR %s
+// CHECK-CLANGRT-WATCHOS-SIMULATOR: libclang_rt.watchossim.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos-simulator \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS-SIMULATOR %s
+// CHECK-CLANGRT-TVOS-SIMULATOR: libclang_rt.tvossim.a
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants

ldionne wrote:

```suggestion
// Check the sanitizer and profile variants
// While the driver also links in sanitizer-specific dylibs, the result of
// -print-libgcc-file-name is the path of the basic compiler-rt library.
```

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -2230,6 +2239,7 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   }
 
   if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
+initDarwinTarget();

ldionne wrote:

We should also add tests for this behavior. We were not printing the right 
directory in the embedded case before your patch, but we were printing the 
right path in the non-embedded case. I don't know if your patch fixes it. If 
your patch doesn't fix the embedded case, we could scale down the patch a bit 
and avoid touching the `OPT_print_runtime_dir` code path (like you did 
initially) just for the sake of landing this. The incorrect 
`-print-runtime-dir` result on embedded machO targets could be fixed separately.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits

https://github.com/ldionne requested changes to this pull request.


https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits

https://github.com/ldionne ready_for_review 
https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants
+//
+
+// TODO
+
+//
+// Check the dynamic library variants
+//

Xazax-hun wrote:

That makes sense to me. Thanks! 

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants
+//
+
+// TODO

Xazax-hun wrote:

As per your comment below, I added tests to make sure we always print 
`libclang_rt..a`.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits


@@ -2271,6 +2271,12 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+// The 'Darwin' toolchain is initialized only when its arguments are

Xazax-hun wrote:

Hmm, I only want to initialize the targets when this function returns false to 
avoid potential side effects when jobs are actually created or executed. This 
minimizes the risk of affecting existing code paths until the proper 
initialization is figured out in the driver. I'll look into something. 

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits


@@ -223,6 +223,11 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
 // There aren't any profiling libs for embedded targets currently.
   }
 
+  // Return the full path of the compiler-rt library on a Darwin MachO system.
+  // Those are under /lib/darwin/<...>(.dylib|.a).
+  std::string getCompilerRT(const llvm::opt::ArgList , StringRef 
Component,

Xazax-hun wrote:

Whoops, this was a copy and paste error inadvertently repeating the same 
comment twice. Now it should have the correct path in the comment. Do you think 
we should explain other differences here (apart from the path where the 
libraries can be found)? If that is the case, could you point me to some 
resources about non-Darwin MachO targets?

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits

https://github.com/Xazax-hun updated 
https://github.com/llvm/llvm-project/pull/98325

From 06d63fe5be15afddd19e64bdf59ca560bc08b3b7 Mon Sep 17 00:00:00 2001
From: Gabor Horvath 
Date: Mon, 8 Jul 2024 11:18:02 +0100
Subject: [PATCH] Fix --print-libgcc-file-name on Darwin platforms

On Darwin, --print-libgcc-file-name was returning a nonsensical result.
It would return the name of the library that would be used by the default
toolchain implementation, but that was something that didn't exist on
Darwin.

Fixing this requires initializing the Darwin toolchain before processing
`--print-libgcc-file-name`. Previously, the Darwin toolchain would only be
initialized when building the jobs for this compilation, which is too late
since `--print-libgcc-file-name` requires the toolchain to be initialized in
order to provide the right results.

rdar://90633749
---
 clang/include/clang/Driver/Driver.h   |  5 +-
 clang/lib/Driver/Driver.cpp   | 13 ++-
 clang/lib/Driver/ToolChains/Darwin.cpp| 64 
 clang/lib/Driver/ToolChains/Darwin.h  | 13 +++
 .../Driver/darwin-print-libgcc-file-name.c| 97 +++
 5 files changed, 168 insertions(+), 24 deletions(-)
 create mode 100644 clang/test/Driver/darwin-print-libgcc-file-name.c

diff --git a/clang/include/clang/Driver/Driver.h 
b/clang/include/clang/Driver/Driver.h
index cc1538372d5f8..04b46782467d6 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -628,8 +628,9 @@ class Driver {
   /// treated before building actions or binding tools.
   ///
   /// \return Whether any compilation should be built for this
-  /// invocation.
-  bool HandleImmediateArgs(const Compilation );
+  /// invocation. The compilation can only be modified when
+  /// this function returns false.
+  bool HandleImmediateArgs(Compilation );
 
   /// ConstructAction - Construct the appropriate action to do for
   /// \p Phase on the \p Input, taking in to account arguments
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 221e222bdd47d..083756dfb62f2 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2128,7 +2128,7 @@ void Driver::HandleAutocompletions(StringRef PassedFlags) 
const {
   llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
 }
 
-bool Driver::HandleImmediateArgs(const Compilation ) {
+bool Driver::HandleImmediateArgs(Compilation ) {
   // The order these options are handled in gcc is all over the place, but we
   // don't expect inconsistencies w.r.t. that to matter in practice.
 
@@ -2180,6 +2180,15 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   }
 
   const ToolChain  = C.getDefaultToolChain();
+  auto initDarwinTarget = [&]() {
+// The 'Darwin' toolchain is initialized only when its arguments are
+// computed. Get the default arguments for OFK_None to ensure that
+// initialization is performed before trying to access properties of
+// the toolchain in the functions below.
+// FIXME: Remove when darwin's toolchain is initialized during 
construction.
+const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+C.getArgsForToolChain(, Triple.getArchName(), Action::OFK_None);
+  };
 
   if (C.getArgs().hasArg(options::OPT_v))
 TC.printVerboseInfo(llvm::errs());
@@ -2230,6 +2239,7 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   }
 
   if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
+initDarwinTarget();
 if (std::optional RuntimePath = TC.getRuntimePath())
   llvm::outs() << *RuntimePath << '\n';
 else
@@ -2271,6 +2281,7 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+initDarwinTarget();
 RegisterEffectiveTriple TripleRAII(TC, Triple);
 switch (RLT) {
 case ToolChain::RLT_CompilerRT:
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index f354b0974d5f2..e530e45a9793b 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1272,23 +1272,8 @@ unsigned DarwinClang::GetDefaultDwarfVersion() const {
 void MachO::AddLinkRuntimeLib(const ArgList , ArgStringList ,
   StringRef Component, RuntimeLinkOptions Opts,
   bool IsShared) const {
-  SmallString<64> DarwinLibName = StringRef("libclang_rt.");
-  // On Darwin the builtins component is not in the library name.
-  if (Component != "builtins") {
-DarwinLibName += Component;
-if (!(Opts & RLO_IsEmbedded))
-  DarwinLibName += "_";
-  }
-
-  DarwinLibName += getOSLibraryNameSuffix();
-  DarwinLibName += IsShared ? "_dynamic.dylib" : ".a";
-  SmallString<128> Dir(getDriver().ResourceDir);
-  

[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits

https://github.com/ldionne edited 
https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits

https://github.com/ldionne edited 
https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants
+//
+
+// TODO

ldionne wrote:

I'm actually not sure what to do about sanitizers. I am figuring out which 
libraries we're actually linking by running this command and looking at what we 
link:

```
echo | xcrun --sdk  clang++ -### -fsanitize=address -xc++ -
```

For the sanitizers, I see that we link both `libclang_rt.osx.a` and 
`libclang_rt.asan_osx_dynamic.dylib`. I am not certain what we should output as 
the result of `-print-libgcc-file-name`.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -223,6 +223,11 @@ class LLVM_LIBRARY_VISIBILITY MachO : public ToolChain {
 // There aren't any profiling libs for embedded targets currently.
   }
 
+  // Return the full path of the compiler-rt library on a Darwin MachO system.
+  // Those are under /lib/darwin/<...>(.dylib|.a).
+  std::string getCompilerRT(const llvm::opt::ArgList , StringRef 
Component,

ldionne wrote:

What is the difference between the MachO `getCompilerRT` and the Darwin one? 
Probably worth having a sentence or two to make it clear what the difference is!

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants
+//
+
+// TODO
+
+//
+// Check the dynamic library variants
+//

ldionne wrote:

```suggestion

```



I think we can get rid of this. We seem to only provide dynamic libraries for 
the sanitizers. So I suspect what we do is something like:
- link in `libclang_rt..a` all the time -- that's the basic 
compiler-rt stuff
- link in `libclang_rt.__dynamic.dylib` when we have 
sanitizers enabled

I suspect that `-print-libgcc-file-name` should simply always print 
`libclang_rt..a`.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators
+
+//
+// Check the cc_kext variants
+//
+
+// TODO
+
+//
+// Check the sanitizer and profile variants
+//
+
+// TODO
+
+//
+// Check the dynamic library variants
+//
+
+// TODO

ldionne wrote:

Missing newline at the end of the file :)

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -2271,6 +2271,12 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+// The 'Darwin' toolchain is initialized only when its arguments are

ldionne wrote:

Could we do the same thing but move it way up above so that we initialize it 
early? I think the same bug exists with `-print-runtime-dir` (line 2232 above).

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Louis Dionne via cfe-commits


@@ -0,0 +1,55 @@
+// Test the output of -print-libgcc-file-name on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=x86_64-apple-macos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: libclang_rt.osx.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-ios \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: libclang_rt.ios.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-watchos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: libclang_rt.watchos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-tvos \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: libclang_rt.tvos.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=arm64-apple-driverkit \
+// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: libclang_rt.driverkit.a
+
+// TODO add simulators

ldionne wrote:

```suggestion
// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=arm64-apple-ios-simulator \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS-SIMULATOR %s
// CHECK-CLANGRT-IOS-SIMULATOR: libclang_rt.iossim.a

// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=arm64-apple-watchos-simulator \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS-SIMULATOR %s
// CHECK-CLANGRT-WATCHOS-SIMULATOR: libclang_rt.watchossim.a

 // RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
// RUN: --target=arm64-apple-tvos-simulator \
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS-SIMULATOR %s
// CHECK-CLANGRT-TVOS-SIMULATOR: libclang_rt.tvossim.a
```

I think this should do it for the simulators.

https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff f50f7a7aa0b897ef91361d7dc159f3262d142bdc 
bbd66df28f451b79dccf6c879e355e2f555e48cd -- 
clang/test/Driver/darwin-print-libgcc-file-name.c 
clang/include/clang/Driver/Driver.h clang/lib/Driver/Driver.cpp 
clang/lib/Driver/ToolChains/Darwin.cpp clang/lib/Driver/ToolChains/Darwin.h
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index c2e90a5c0e..6933d17585 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1273,7 +1273,7 @@ void MachO::AddLinkRuntimeLib(const ArgList , 
ArgStringList ,
   StringRef Component, RuntimeLinkOptions Opts,
   bool IsShared) const {
   std::string P = getCompilerRT(
-Args, Component, IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static);
+  Args, Component, IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static);
 
   // For now, allow missing resource libraries to support developers who may
   // not have compiler-rt checked out or integrated into their build (unless
@@ -1295,8 +1295,8 @@ void MachO::AddLinkRuntimeLib(const ArgList , 
ArgStringList ,
 CmdArgs.push_back("-rpath");
 CmdArgs.push_back("@executable_path");
 
-// Add the compiler-rt library's directory to rpath to support using the 
dylib
-// from the default location without copying.
+// Add the compiler-rt library's directory to rpath to support using the
+// dylib from the default location without copying.
 CmdArgs.push_back("-rpath");
 CmdArgs.push_back(Args.MakeArgString(llvm::sys::path::parent_path(P)));
   }
@@ -1315,12 +1315,13 @@ std::string MachO::getCompilerRT(const ArgList &, 
StringRef Component,
   MachOLibName += Type == ToolChain::FT_Shared ? "_dynamic.dylib" : ".a";
 
   SmallString<128> FullPath(getDriver().ResourceDir);
-  llvm::sys::path::append(FullPath, "lib", "darwin", "macho_embedded", 
MachOLibName);
+  llvm::sys::path::append(FullPath, "lib", "darwin", "macho_embedded",
+  MachOLibName);
   return std::string(FullPath);
 }
 
 std::string Darwin::getCompilerRT(const ArgList &, StringRef Component,
- FileType Type) const {
+  FileType Type) const {
   assert(Type != ToolChain::FT_Object &&
  "it doesn't make sense to ask for the compiler-rt library name as an "
  "object file");
diff --git a/clang/lib/Driver/ToolChains/Darwin.h 
b/clang/lib/Driver/ToolChains/Darwin.h
index ba5a732df1..2d570a11e2 100644
--- a/clang/lib/Driver/ToolChains/Darwin.h
+++ b/clang/lib/Driver/ToolChains/Darwin.h
@@ -225,8 +225,9 @@ public:
 
   // Return the full path of the compiler-rt library on a Darwin MachO system.
   // Those are under /lib/darwin/<...>(.dylib|.a).
-  std::string getCompilerRT(const llvm::opt::ArgList , StringRef 
Component,
-FileType Type = ToolChain::FT_Static) const 
override;
+  std::string
+  getCompilerRT(const llvm::opt::ArgList , StringRef Component,
+FileType Type = ToolChain::FT_Static) const override;
 
   /// }
   /// @name ToolChain Implementation
@@ -363,8 +364,10 @@ public:
 
   // Return the full path of the compiler-rt library on a Darwin MachO system.
   // Those are under /lib/darwin/<...>(.dylib|.a).
-  std::string getCompilerRT(const llvm::opt::ArgList , StringRef 
Component,
-FileType Type = ToolChain::FT_Static) const 
override;
+  std::string
+  getCompilerRT(const llvm::opt::ArgList , StringRef Component,
+FileType Type = ToolChain::FT_Static) const override;
+
 protected:
   /// }
   /// @name Darwin specific Toolchain functions

``




https://github.com/llvm/llvm-project/pull/98325
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix --print-libgcc-file-name on Darwin platforms (PR #98325)

2024-07-10 Thread Gábor Horváth via cfe-commits

https://github.com/Xazax-hun created 
https://github.com/llvm/llvm-project/pull/98325

On Darwin, --print-libgcc-file-name was returning a nonsensical result. It 
would return the name of the library that would be used by the default 
toolchain implementation, but that was something that didn't exist on Darwin.

Fixing this requires initializing the Darwin toolchain before processing 
`--print-libgcc-file-name`. Previously, the Darwin toolchain would only be 
initialized when building the jobs for this compilation, which is too late 
since `--print-libgcc-file-name` requires the toolchain to be initialized in 
order to provide the right results.

From bbd66df28f451b79dccf6c879e355e2f555e48cd Mon Sep 17 00:00:00 2001
From: Gabor Horvath 
Date: Mon, 8 Jul 2024 11:18:02 +0100
Subject: [PATCH] Fix --print-libgcc-file-name on Darwin platforms

On Darwin, --print-libgcc-file-name was returning a nonsensical result.
It would return the name of the library that would be used by the default
toolchain implementation, but that was something that didn't exist on
Darwin.

Fixing this requires initializing the Darwin toolchain before processing
`--print-libgcc-file-name`. Previously, the Darwin toolchain would only be
initialized when building the jobs for this compilation, which is too late
since `--print-libgcc-file-name` requires the toolchain to be initialized in
order to provide the right results.
---
 clang/include/clang/Driver/Driver.h   |  5 +-
 clang/lib/Driver/Driver.cpp   |  8 ++-
 clang/lib/Driver/ToolChains/Darwin.cpp| 61 +--
 clang/lib/Driver/ToolChains/Darwin.h  |  9 +++
 .../Driver/darwin-print-libgcc-file-name.c| 55 +
 5 files changed, 115 insertions(+), 23 deletions(-)
 create mode 100644 clang/test/Driver/darwin-print-libgcc-file-name.c

diff --git a/clang/include/clang/Driver/Driver.h 
b/clang/include/clang/Driver/Driver.h
index cc1538372d5f8..04b46782467d6 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -628,8 +628,9 @@ class Driver {
   /// treated before building actions or binding tools.
   ///
   /// \return Whether any compilation should be built for this
-  /// invocation.
-  bool HandleImmediateArgs(const Compilation );
+  /// invocation. The compilation can only be modified when
+  /// this function returns false.
+  bool HandleImmediateArgs(Compilation );
 
   /// ConstructAction - Construct the appropriate action to do for
   /// \p Phase on the \p Input, taking in to account arguments
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 221e222bdd47d..cb1bc7e36d39d 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2128,7 +2128,7 @@ void Driver::HandleAutocompletions(StringRef PassedFlags) 
const {
   llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
 }
 
-bool Driver::HandleImmediateArgs(const Compilation ) {
+bool Driver::HandleImmediateArgs(Compilation ) {
   // The order these options are handled in gcc is all over the place, but we
   // don't expect inconsistencies w.r.t. that to matter in practice.
 
@@ -2271,6 +2271,12 @@ bool Driver::HandleImmediateArgs(const Compilation ) {
   if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
 const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+// The 'Darwin' toolchain is initialized only when its arguments are
+// computed. Get the default arguments for OFK_None to ensure that
+// initialization is performed before trying to access properties of
+// the toolchain in the functions below.
+// FIXME: Remove when darwin's toolchain is initialized during 
construction.
+C.getArgsForToolChain(, Triple.getArchName(), Action::OFK_None);
 RegisterEffectiveTriple TripleRAII(TC, Triple);
 switch (RLT) {
 case ToolChain::RLT_CompilerRT:
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp 
b/clang/lib/Driver/ToolChains/Darwin.cpp
index f354b0974d5f2..c2e90a5c0e49c 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1272,23 +1272,8 @@ unsigned DarwinClang::GetDefaultDwarfVersion() const {
 void MachO::AddLinkRuntimeLib(const ArgList , ArgStringList ,
   StringRef Component, RuntimeLinkOptions Opts,
   bool IsShared) const {
-  SmallString<64> DarwinLibName = StringRef("libclang_rt.");
-  // On Darwin the builtins component is not in the library name.
-  if (Component != "builtins") {
-DarwinLibName += Component;
-if (!(Opts & RLO_IsEmbedded))
-  DarwinLibName += "_";
-  }
-
-  DarwinLibName += getOSLibraryNameSuffix();
-  DarwinLibName += IsShared ? "_dynamic.dylib" : ".a";
-  SmallString<128> Dir(getDriver().ResourceDir);
-  llvm::sys::path::append(Dir, "lib", "darwin");
-  if (Opts & RLO_IsEmbedded)
-