@@ -1165,6 +1165,34 @@ bool Driver::loadConfigFiles() {
1165
1165
return false ;
1166
1166
}
1167
1167
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
+
1168
1196
bool Driver::loadDefaultConfigFiles (llvm::cl::ExpansionContext &ExpCtx) {
1169
1197
// Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
1170
1198
// value.
@@ -1176,7 +1204,7 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1176
1204
return false ;
1177
1205
1178
1206
std::string RealMode = getExecutableForDriverMode (Mode);
1179
- std::string Triple;
1207
+ llvm::Triple Triple;
1180
1208
1181
1209
// If name prefix is present, no --target= override was passed via CLOptions
1182
1210
// and the name prefix is not a valid triple, force it for backwards
@@ -1187,15 +1215,13 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1187
1215
llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix };
1188
1216
if (PrefixTriple.getArch () == llvm::Triple::UnknownArch ||
1189
1217
PrefixTriple.isOSUnknown ())
1190
- Triple = PrefixTriple. str () ;
1218
+ Triple = PrefixTriple;
1191
1219
}
1192
1220
1193
1221
// 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 ());
1199
1225
}
1200
1226
1201
1227
// Search for config files in the following order:
@@ -1210,21 +1236,21 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1210
1236
1211
1237
// Try loading <triple>-<mode>.cfg, and return if we find a match.
1212
1238
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 " ))
1215
1241
return readConfigFile (CfgFilePath, ExpCtx);
1216
1242
1217
1243
bool TryModeSuffix = !ClangNameParts.ModeSuffix .empty () &&
1218
1244
ClangNameParts.ModeSuffix != RealMode;
1219
1245
if (TryModeSuffix) {
1220
- CfgFileName = Triple + ' - ' + ClangNameParts. ModeSuffix + " .cfg " ;
1221
- if (ExpCtx. findConfigFile (CfgFileName, CfgFilePath ))
1246
+ if ( findTripleConfigFile (ExpCtx, CfgFilePath, Triple,
1247
+ " - " + ClangNameParts. ModeSuffix + " .cfg " ))
1222
1248
return readConfigFile (CfgFilePath, ExpCtx);
1223
1249
}
1224
1250
1225
1251
// Try loading <mode>.cfg, and return if loading failed. If a matching file
1226
1252
// was not found, still proceed on to try <triple>.cfg.
1227
- CfgFileName = RealMode + " .cfg" ;
1253
+ std::string CfgFileName = RealMode + " .cfg" ;
1228
1254
if (ExpCtx.findConfigFile (CfgFileName, CfgFilePath)) {
1229
1255
if (readConfigFile (CfgFilePath, ExpCtx))
1230
1256
return true ;
@@ -1236,8 +1262,7 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
1236
1262
}
1237
1263
1238
1264
// 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" ))
1241
1266
return readConfigFile (CfgFilePath, ExpCtx);
1242
1267
1243
1268
// If we were unable to find a config file deduced from executable name,
0 commit comments