Skip to content

[Clang][Driver] Improve config file handling on Darwin #111306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/RISCVISAInfo.h"
#include <cstdlib> // ::getenv
#include <cstring>
#include <map>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions clang/test/Driver/config-file-darwin.c
Original file line number Diff line number Diff line change
@@ -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
Loading