[clang] [Clang][Driver] Improve config file handling on Darwin (PR #111306)
Carlo Cabrera via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 6 11:02:03 PDT 2024
https://github.com/carlocab created https://github.com/llvm/llvm-project/pull/111306
On Darwin, the default target triple contains the version of the kernel:
❯ clang --print-target-triple
arm64-apple-darwin24.0.0
❯ uname -r
24.0.0
This makes writing config files for the target triple rather cumbersome,
because they require including the full version of the kernel in the
filename when one generally cares only about the major version if at
all.
Let's improve this by also checking for major-versioned and unversioned
`.cfg` files if we weren't able to find a `.cfg` file for the full
triple when targetting Darwin.
>From 7ab9f9ebb858618f6eb92bb058afca0cb0f56269 Mon Sep 17 00:00:00 2001
From: Carlo Cabrera <30379873+carlocab at users.noreply.github.com>
Date: Mon, 7 Oct 2024 01:53:37 +0800
Subject: [PATCH] [Clang][Driver] Improve config file handling on Darwin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
On Darwin, the default target triple contains the version of the kernel:
❯ clang --print-target-triple
arm64-apple-darwin24.0.0
❯ uname -r
24.0.0
This makes writing config files for the target triple rather cumbersome,
because they require including the full version of the kernel in the
filename when one generally cares only about the major version if at
all.
Let's improve this by also checking for major-versioned and unversioned
`.cfg` files if we weren't able to find a `.cfg` file for the full
triple when targetting Darwin.
---
clang/lib/Driver/Driver.cpp | 19 +++++++++++++++++++
clang/test/Driver/config-file-darwin.c | 24 ++++++++++++++++++++++++
2 files changed, 43 insertions(+)
create mode 100644 clang/test/Driver/config-file-darwin.c
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 2aaa52072b03d2..e9f241228d4aab 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 <cstdlib> // ::getenv
+#include <cstring>
#include <map>
#include <memory>
#include <optional>
@@ -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 00000000000000..c24eb7ad19fe67
--- /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
More information about the cfe-commits
mailing list