Skip to content

Commit 80b4301

Browse files
committed
[Clang][Driver] Improve config file handling on Darwin
On Darwin, the default target triple contains the version of the kernel: ❯ clang --print-target-triple arm64-apple-darwin24.0.0 ❯ uname -r 24.0.0 This makes writing config files for the target triple rather cumbersome, because they require including the full version of the kernel in the filename when one generally cares only about the major version if at all. Let's improve this by also checking for major-versioned and unversioned `.cfg` files if we weren't able to find a `.cfg` file for the full triple when targetting Darwin.
1 parent 26ca8ef commit 80b4301

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#include "llvm/TargetParser/Host.h"
9696
#include "llvm/TargetParser/RISCVISAInfo.h"
9797
#include <cstdlib> // ::getenv
98+
#include <cstring>
9899
#include <map>
99100
#include <memory>
100101
#include <optional>
@@ -1222,6 +1223,24 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
12221223
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
12231224
return readConfigFile(CfgFilePath, ExpCtx);
12241225

1226+
// On Darwin, try a major-versioned triple then an unversioned triple.
1227+
auto pos = Triple.find("-apple-darwin");
1228+
auto kernelVersionStart = pos + std::strlen("-apple-darwin");
1229+
if (pos != Triple.npos && Triple.length() > kernelVersionStart) {
1230+
// First, find the major-versioned triple (e.g. arm64-apple-darwin24).
1231+
auto T = Triple.substr(0, Triple.find(".", kernelVersionStart));
1232+
CfgFileName = T + ".cfg";
1233+
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
1234+
return readConfigFile(CfgFilePath, ExpCtx);
1235+
1236+
// If that is not available, try an unversioned triple
1237+
// (e.g. arm64-apple-darwin).
1238+
T = Triple.substr(0, kernelVersionStart);
1239+
CfgFileName = T + ".cfg";
1240+
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
1241+
return readConfigFile(CfgFilePath, ExpCtx);
1242+
}
1243+
12251244
// If we were unable to find a config file deduced from executable name,
12261245
// that is not an error.
12271246
return false;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// REQUIRES: shell
2+
3+
// RUN: unset CLANG_NO_DEFAULT_CONFIG
4+
// RUN: rm -rf %t && mkdir %t
5+
6+
//--- Major-versioned config files are used when targetting *-apple-darwin*
7+
//
8+
// RUN: mkdir -p %t/testbin
9+
// RUN: ln -s %clang %t/testbin/x86_64-apple-darwin24.0.0-clang
10+
// RUN: echo "-Werror" > %t/testbin/x86_64-apple-darwin24.cfg
11+
// RUN: %t/testbin/x86_64-apple-darwin24.0.0-clang --config-system-dir= --config-user-dir= -c -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-MAJOR-VERSIONED
12+
//
13+
// CHECK-MAJOR-VERSIONED: Configuration file: {{.*}}/testbin/x86_64-apple-darwin24.cfg
14+
// CHECK-MAJOR-VERSIONED: -Werror
15+
16+
//--- Unversioned config files are used when targetting *-apple-darwin*
17+
//
18+
// RUN: mkdir -p %t/testbin
19+
// RUN: ln -s %clang %t/testbin/arm64-apple-darwin23.1.2-clang
20+
// RUN: echo "-Werror" > %t/testbin/arm64-apple-darwin.cfg
21+
// RUN: %t/testbin/arm64-apple-darwin23.1.2-clang --config-system-dir= --config-user-dir= -c -no-canonical-prefixes -### %s 2>&1 | FileCheck %s -check-prefix CHECK-UNVERSIONED
22+
//
23+
// CHECK-UNVERSIONED: Configuration file: {{.*}}/testbin/arm64-apple-darwin.cfg
24+
// CHECK-UNVERSIONED: -Werror

0 commit comments

Comments
 (0)