Skip to content

Commit 57b80e8

Browse files
authored
[SystemZ][z/OS] Add z/OS customization file (#111182)
On z/OS, the location of the system libraries and side decks (aka equivalent to libc, etc) are not in a predefined location. The system does have a default location but sysadmins can change this and frequently do. See the -mzos-hlq* options we have for z/OS. To avoid every user needing to specify these -mzos-hlq* options, we added support for a system install default config file that is always read independent of the usual config file. The compiler will read this customization config file before reading the usual config files. The customization file is called clang.cfg and is located in: - the etc dir within the compiler installation dir. - or specified by the CLANG_CONFIG_PATH env var. This env var can either be a directory or the fill path name of the file.
1 parent 8557a57 commit 57b80e8

File tree

7 files changed

+84
-1
lines changed

7 files changed

+84
-1
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@ class Driver {
741741
/// \returns true if error occurred.
742742
bool loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx);
743743

744+
/// Tries to load options from customization file.
745+
///
746+
/// \returns true if error occurred.
747+
bool loadZOSCustomizationFile(llvm::cl::ExpansionContext &);
748+
744749
/// Read options from the specified file.
745750
///
746751
/// \param [in] FileName File to read.

clang/lib/Driver/Driver.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,34 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
10631063
//
10641064
}
10651065

1066+
bool Driver::loadZOSCustomizationFile(llvm::cl::ExpansionContext &ExpCtx) {
1067+
if (IsCLMode() || IsDXCMode() || IsFlangMode())
1068+
return false;
1069+
1070+
SmallString<128> CustomizationFile;
1071+
StringRef PathLIBEnv = StringRef(getenv("CLANG_CONFIG_PATH")).trim();
1072+
// If the env var is a directory then append "/clang.cfg" and treat
1073+
// that as the config file. Otherwise treat the env var as the
1074+
// config file.
1075+
if (!PathLIBEnv.empty()) {
1076+
llvm::sys::path::append(CustomizationFile, PathLIBEnv);
1077+
if (llvm::sys::fs::is_directory(PathLIBEnv))
1078+
llvm::sys::path::append(CustomizationFile, "/clang.cfg");
1079+
if (llvm::sys::fs::is_regular_file(CustomizationFile))
1080+
return readConfigFile(CustomizationFile, ExpCtx);
1081+
Diag(diag::err_drv_config_file_not_found) << CustomizationFile;
1082+
return true;
1083+
}
1084+
1085+
SmallString<128> BaseDir(llvm::sys::path::parent_path(Dir));
1086+
llvm::sys::path::append(CustomizationFile, BaseDir + "/etc/clang.cfg");
1087+
if (llvm::sys::fs::is_regular_file(CustomizationFile))
1088+
return readConfigFile(CustomizationFile, ExpCtx);
1089+
1090+
// If no customization file, just return
1091+
return false;
1092+
}
1093+
10661094
static void appendOneArg(InputArgList &Args, const Arg *Opt) {
10671095
// The args for config files or /clang: flags belong to different InputArgList
10681096
// objects than Args. This copies an Arg from one of those other InputArgLists
@@ -1284,11 +1312,18 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
12841312
}
12851313

12861314
// Otherwise, use the real triple as used by the driver.
1315+
llvm::Triple RealTriple =
1316+
computeTargetTriple(*this, TargetTriple, *CLOptions);
12871317
if (Triple.str().empty()) {
1288-
Triple = computeTargetTriple(*this, TargetTriple, *CLOptions);
1318+
Triple = RealTriple;
12891319
assert(!Triple.str().empty());
12901320
}
12911321

1322+
// On z/OS, start by loading the customization file before loading
1323+
// the usual default config file(s).
1324+
if (RealTriple.isOSzOS() && loadZOSCustomizationFile(ExpCtx))
1325+
return true;
1326+
12921327
// Search for config files in the following order:
12931328
// 1. <triple>-<mode>.cfg using real driver mode
12941329
// (e.g. i386-pc-linux-gnu-clang++.cfg).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-DABC=123
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-DDEF=456
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-DDEF=456

clang/test/Driver/config-zos.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// REQUIRES: shell
2+
// REQUIRES: systemz-registered-target
3+
4+
// RUN: unset CLANG_NO_DEFAULT_CONFIG
5+
// RUN: rm -rf %t && mkdir %t
6+
7+
// RUN: mkdir -p %t/testbin
8+
// RUN: mkdir -p %t/etc
9+
// RUN: ln -s %clang %t/testbin/clang
10+
// RUN: echo "-DXYZ=789" >%t/etc/clang.cfg
11+
// RUN: %t/testbin/clang --target=s390x-ibm-zos -c -### -no-canonical-prefixes %s 2>&1 | FileCheck -DDIR=%t %s
12+
// RUN: %t/testbin/clang --target=s390x-ibm-zos -c -### -no-canonical-prefixes --no-default-config %s 2>&1 | FileCheck -check-prefix=NOCONFIG %s
13+
//
14+
// CHECK: Configuration file: [[DIR]]/etc/clang.cfg
15+
// CHECK: "-D" "XYZ=789"
16+
// NOCONFIG-NOT: Configuration file: {{.*}}/etc/clang.cfg
17+
// NOCONFIG-NOT: "-D" "XYZ=789"

clang/test/Driver/config-zos1.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// REQUIRES: shell
2+
// REQUIRES: systemz-registered-target
3+
4+
// RUN: unset CLANG_NO_DEFAULT_CONFIG
5+
6+
// RUN: export CLANG_CONFIG_PATH=%S/Inputs/config-zos
7+
// RUN: %clang --target=s390x-ibm-zos -c -### %s 2>&1 | FileCheck %s
8+
// CHECK: Configuration file: {{.*}}/Inputs/config-zos/clang.cfg
9+
// CHECK: "-D" "ABC=123"
10+
11+
// RUN: export CLANG_CONFIG_PATH=%S/Inputs/config-zos/def.cfg
12+
// RUN: %clang --target=s390x-ibm-zos -c -### %s 2>&1 | FileCheck %s -check-prefix=CHECK-DEF
13+
// CHECK-DEF: Configuration file: {{.*}}/Inputs/config-zos/def.cfg
14+
// CHECK-DEF: "-D" "DEF=456"
15+
16+
// RUN: export CLANG_CONFIG_PATH=%S/Inputs/config-zos/Garbage
17+
// RUN: not %clang --target=s390x-ibm-zos -c -### %s 2>&1 | FileCheck %s -check-prefix=CHECK-ERR
18+
// CHECK-ERR: error: configuration file '{{.*}}/Inputs/config-zos/Garbage' cannot be found
19+
20+
// The directory exists but no clang.cfg in it
21+
// RUN: export CLANG_CONFIG_PATH=%S/Inputs/config-zos/tst
22+
// RUN: not %clang --target=s390x-ibm-zos -c -### %s 2>&1 | FileCheck %s -check-prefix=CHECK-ERRDIR
23+
// CHECK-ERRDIR: error: configuration file '{{.*}}/Inputs/config-zos/tst/clang.cfg' cannot be found

0 commit comments

Comments
 (0)