Skip to content

Commit 0cbdad4

Browse files
authoredDec 12, 2024··
[clang][Driver] Support simplified triple versions for config files (#111387)
Currently, the config file system loads the full target triple, e.g. `arm64-apple-darwin23.6.0.cfg`. This is however not very useful as this is a moving target. In the case of macOS, that target moves every ~2 months. We can improve this by adding fallbacks that simplify the version component of the triple. This pull request adds support for loading `arm64-apple-darwin23.cfg` and `arm64-apple-darwin.cfg`. See the included test for a demonstration on how it works.
1 parent 2be41e7 commit 0cbdad4

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed
 

‎clang/lib/Driver/Driver.cpp

+39-14
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,34 @@ bool Driver::loadConfigFiles() {
11651165
return false;
11661166
}
11671167

1168+
static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx,
1169+
SmallString<128> &ConfigFilePath,
1170+
llvm::Triple Triple, std::string Suffix) {
1171+
// First, try the full unmodified triple.
1172+
if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
1173+
return true;
1174+
1175+
// Don't continue if we didn't find a parsable version in the triple.
1176+
VersionTuple OSVersion = Triple.getOSVersion();
1177+
if (!OSVersion.getMinor().has_value())
1178+
return false;
1179+
1180+
std::string BaseOSName = Triple.getOSTypeName(Triple.getOS()).str();
1181+
1182+
// Next try strip the version to only include the major component.
1183+
// e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23
1184+
if (OSVersion.getMajor() != 0) {
1185+
Triple.setOSName(BaseOSName + llvm::utostr(OSVersion.getMajor()));
1186+
if (ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath))
1187+
return true;
1188+
}
1189+
1190+
// Finally, try without any version suffix at all.
1191+
// e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin
1192+
Triple.setOSName(BaseOSName);
1193+
return ExpCtx.findConfigFile(Triple.str() + Suffix, ConfigFilePath);
1194+
}
1195+
11681196
bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
11691197
// Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
11701198
// value.
@@ -1176,7 +1204,7 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
11761204
return false;
11771205

11781206
std::string RealMode = getExecutableForDriverMode(Mode);
1179-
std::string Triple;
1207+
llvm::Triple Triple;
11801208

11811209
// If name prefix is present, no --target= override was passed via CLOptions
11821210
// and the name prefix is not a valid triple, force it for backwards
@@ -1187,15 +1215,13 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
11871215
llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
11881216
if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
11891217
PrefixTriple.isOSUnknown())
1190-
Triple = PrefixTriple.str();
1218+
Triple = PrefixTriple;
11911219
}
11921220

11931221
// Otherwise, use the real triple as used by the driver.
1194-
if (Triple.empty()) {
1195-
llvm::Triple RealTriple =
1196-
computeTargetTriple(*this, TargetTriple, *CLOptions);
1197-
Triple = RealTriple.str();
1198-
assert(!Triple.empty());
1222+
if (Triple.str().empty()) {
1223+
Triple = computeTargetTriple(*this, TargetTriple, *CLOptions);
1224+
assert(!Triple.str().empty());
11991225
}
12001226

12011227
// Search for config files in the following order:
@@ -1210,21 +1236,21 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
12101236

12111237
// Try loading <triple>-<mode>.cfg, and return if we find a match.
12121238
SmallString<128> CfgFilePath;
1213-
std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
1214-
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
1239+
if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
1240+
"-" + RealMode + ".cfg"))
12151241
return readConfigFile(CfgFilePath, ExpCtx);
12161242

12171243
bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
12181244
ClangNameParts.ModeSuffix != RealMode;
12191245
if (TryModeSuffix) {
1220-
CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
1221-
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
1246+
if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple,
1247+
"-" + ClangNameParts.ModeSuffix + ".cfg"))
12221248
return readConfigFile(CfgFilePath, ExpCtx);
12231249
}
12241250

12251251
// Try loading <mode>.cfg, and return if loading failed. If a matching file
12261252
// was not found, still proceed on to try <triple>.cfg.
1227-
CfgFileName = RealMode + ".cfg";
1253+
std::string CfgFileName = RealMode + ".cfg";
12281254
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
12291255
if (readConfigFile(CfgFilePath, ExpCtx))
12301256
return true;
@@ -1236,8 +1262,7 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
12361262
}
12371263

12381264
// Try loading <triple>.cfg and return if we find a match.
1239-
CfgFileName = Triple + ".cfg";
1240-
if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
1265+
if (findTripleConfigFile(ExpCtx, CfgFilePath, Triple, ".cfg"))
12411266
return readConfigFile(CfgFilePath, ExpCtx);
12421267

12431268
// If we were unable to find a config file deduced from executable name,

‎clang/test/Driver/config-file3.c

+23
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,26 @@
226226
//
227227
// RUN: HOME=%S/Inputs/config %clang -### --config-user-dir=~ -v 2>&1 | FileCheck %s --check-prefix=CHECK-TILDE
228228
// CHECK-TILDE: User configuration file directory: {{.*}}/Inputs/config
229+
230+
//--- Fallback to stripping OS versions
231+
//
232+
// RUN: touch %t/testdmode/x86_64-apple-darwin23.6.0-clang.cfg
233+
// RUN: touch %t/testdmode/x86_64-apple-darwin23-clang.cfg
234+
// RUN: touch %t/testdmode/x86_64-apple-darwin-clang.cfg
235+
// RUN: %clang -target x86_64-apple-darwin23.6.0 --config-system-dir=%t/testdmode --config-user-dir= -no-canonical-prefixes --version 2>&1 | FileCheck %s -check-prefix DARWIN --implicit-check-not 'Configuration file:'
236+
//
237+
// DARWIN: Configuration file: {{.*}}/testdmode/x86_64-apple-darwin23.6.0-clang.cfg
238+
239+
//--- DARWIN + no full version
240+
//
241+
// RUN: rm %t/testdmode/x86_64-apple-darwin23.6.0-clang.cfg
242+
// RUN: %clang -target x86_64-apple-darwin23.6.0 --config-system-dir=%t/testdmode --config-user-dir= -no-canonical-prefixes --version 2>&1 | FileCheck %s -check-prefix DARWIN-MAJOR --implicit-check-not 'Configuration file:'
243+
//
244+
// DARWIN-MAJOR: Configuration file: {{.*}}/testdmode/x86_64-apple-darwin23-clang.cfg
245+
246+
//--- DARWIN + no version
247+
//
248+
// RUN: rm %t/testdmode/x86_64-apple-darwin23-clang.cfg
249+
// RUN: %clang -target x86_64-apple-darwin23.6.0 --config-system-dir=%t/testdmode --config-user-dir= -no-canonical-prefixes --version 2>&1 | FileCheck %s -check-prefix DARWIN-VERSIONLESS --implicit-check-not 'Configuration file:'
250+
//
251+
// DARWIN-VERSIONLESS: Configuration file: {{.*}}/testdmode/x86_64-apple-darwin-clang.cfg

0 commit comments

Comments
 (0)
Please sign in to comment.