diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 2aaa52072b03d..e9f241228d4aa 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -95,6 +95,7 @@ #include "llvm/TargetParser/Host.h" #include "llvm/TargetParser/RISCVISAInfo.h" #include // ::getenv +#include #include #include #include @@ -1222,6 +1223,24 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) { if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) return readConfigFile(CfgFilePath, ExpCtx); + // On Darwin, try a major-versioned triple then an unversioned triple. + auto pos = Triple.find("-apple-darwin"); + auto kernelVersionStart = pos + std::strlen("-apple-darwin"); + if (pos != Triple.npos && Triple.length() > kernelVersionStart) { + // First, find the major-versioned triple (e.g. arm64-apple-darwin24). + auto T = Triple.substr(0, Triple.find(".", kernelVersionStart)); + CfgFileName = T + ".cfg"; + if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) + return readConfigFile(CfgFilePath, ExpCtx); + + // If that is not available, try an unversioned triple + // (e.g. arm64-apple-darwin). + T = Triple.substr(0, kernelVersionStart); + CfgFileName = T + ".cfg"; + if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) + return readConfigFile(CfgFilePath, ExpCtx); + } + // If we were unable to find a config file deduced from executable name, // that is not an error. return false; diff --git a/clang/test/Driver/config-file-darwin.c b/clang/test/Driver/config-file-darwin.c new file mode 100644 index 0000000000000..c24eb7ad19fe6 --- /dev/null +++ b/clang/test/Driver/config-file-darwin.c @@ -0,0 +1,24 @@ +// REQUIRES: shell + +// RUN: unset CLANG_NO_DEFAULT_CONFIG +// RUN: rm -rf %t && mkdir %t + +//--- Major-versioned config files are used when targetting *-apple-darwin* +// +// RUN: mkdir -p %t/testbin +// RUN: ln -s %clang %t/testbin/x86_64-apple-darwin24.0.0-clang +// RUN: echo "-Werror" > %t/testbin/x86_64-apple-darwin24.cfg +// 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 +// +// CHECK-MAJOR-VERSIONED: Configuration file: {{.*}}/testbin/x86_64-apple-darwin24.cfg +// CHECK-MAJOR-VERSIONED: -Werror + +//--- Unversioned config files are used when targetting *-apple-darwin* +// +// RUN: mkdir -p %t/testbin +// RUN: ln -s %clang %t/testbin/arm64-apple-darwin23.1.2-clang +// RUN: echo "-Werror" > %t/testbin/arm64-apple-darwin.cfg +// 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 +// +// CHECK-UNVERSIONED: Configuration file: {{.*}}/testbin/arm64-apple-darwin.cfg +// CHECK-UNVERSIONED: -Werror