@@ -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+
11681196bool 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,
0 commit comments