Skip to content

Commit de38c4e

Browse files
authored
[flang][driver] Introduce FCC_OVERRIDE_OPTIONS. (#140556)
This PR add functionality to change `flang` command line using environment variable `FCC_OVERRIDE_OPTIONS`. It is quite similar to what `CCC_OVERRIDE_OPTIONS` does for clang. The `FCC_OVERRIDE_OPTIONS` seemed like the most obvious name to me but I am open to other ideas. The `applyOverrideOptions` now takes an extra argument that is the name of the environment variable. Previously `CCC_OVERRIDE_OPTIONS` was hardcoded.
1 parent 1984c75 commit de38c4e

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ llvm::Error expandResponseFiles(SmallVectorImpl<const char *> &Args,
879879
/// See applyOneOverrideOption.
880880
void applyOverrideOptions(SmallVectorImpl<const char *> &Args,
881881
const char *OverrideOpts,
882-
llvm::StringSet<> &SavedStrings,
882+
llvm::StringSet<> &SavedStrings, StringRef EnvVar,
883883
raw_ostream *OS = nullptr);
884884

885885
} // end namespace driver

clang/lib/Driver/Driver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7204,9 +7204,10 @@ static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
72047204
///
72057205
/// '#': Silence information about the changes to the command line arguments.
72067206
///
7207-
/// '^': Add FOO as a new argument at the beginning of the command line.
7207+
/// '^FOO': Add FOO as a new argument at the beginning of the command line
7208+
/// right after the name of the compiler executable.
72087209
///
7209-
/// '+': Add FOO as a new argument at the end of the command line.
7210+
/// '+FOO': Add FOO as a new argument at the end of the command line.
72107211
///
72117212
/// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
72127213
/// line.
@@ -7294,7 +7295,7 @@ static void applyOneOverrideOption(raw_ostream &OS,
72947295
void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
72957296
const char *OverrideStr,
72967297
llvm::StringSet<> &SavedStrings,
7297-
raw_ostream *OS) {
7298+
StringRef EnvVar, raw_ostream *OS) {
72987299
if (!OS)
72997300
OS = &llvm::nulls();
73007301

@@ -7303,7 +7304,7 @@ void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args,
73037304
OS = &llvm::nulls();
73047305
}
73057306

7306-
*OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
7307+
*OS << "### " << EnvVar << ": " << OverrideStr << "\n";
73077308

73087309
// This does not need to be efficient.
73097310

clang/tools/driver/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
305305
if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
306306
// FIXME: Driver shouldn't take extra initial argument.
307307
driver::applyOverrideOptions(Args, OverrideStr, SavedStrings,
308-
&llvm::errs());
308+
"CCC_OVERRIDE_OPTIONS", &llvm::errs());
309309
}
310310

311311
std::string Path = GetExecutablePath(ToolContext.Path, CanonicalPrefixes);

flang/docs/FlangDriver.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,31 @@ nvfortran defines `-fast` as
614614
- `-Mcache_align`: there is no equivalent flag in Flang or Clang.
615615
- `-Mflushz`: flush-to-zero mode - when `-ffast-math` is specified, Flang will
616616
link to `crtfastmath.o` to ensure denormal numbers are flushed to zero.
617+
618+
619+
## FCC_OVERRIDE_OPTIONS
620+
621+
The environment variable `FCC_OVERRIDE_OPTIONS` can be used to edit flang's
622+
command line arguments. The value of this variable is a space-separated list of
623+
edits to perform. The edits are applied in the order in which they appear in
624+
`FCC_OVERRIDE_OPTIONS`. Each edit should be one of the following form:
625+
626+
- `#`: Silence information about the changes to the command line arguments.
627+
628+
- `^FOO`: Add `FOO` as a new argument at the beginning of the command line right
629+
after the name of the compiler executable.
630+
631+
- `+FOO`: Add `FOO` as a new argument at the end of the command line.
632+
633+
- `s/XXX/YYY/`: Substitute the regular expression `XXX` with `YYY` in the
634+
command line.
635+
636+
- `xOPTION`: Removes all instances of the literal argument `OPTION`.
637+
638+
- `XOPTION`: Removes all instances of the literal argument `OPTION`, and the
639+
following argument.
640+
641+
- `Ox`: Removes all flags matching `O` or `O[sz0-9]` and adds `Ox` at the end
642+
of the command line.
643+
644+
This environment variable does not affect the options added by the config files.

flang/test/Driver/Inputs/config-7.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Werror

flang/test/Driver/fcc_override.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: env FCC_OVERRIDE_OPTIONS="#+-Os +-Oz +-O +-O3 +-Oignore +a +b +c xb Xa Omagic ^-### " %flang --target=x86_64-unknown-linux-gnu %s -O2 b -O3 2>&1 | FileCheck %s
2+
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror +-g" %flang --target=x86_64-unknown-linux-gnu -Werror %s -c -### 2>&1 | FileCheck %s -check-prefix=RM-WERROR
3+
! RUN: env FCC_OVERRIDE_OPTIONS="x-Werror" %flang --config=%S/Inputs/config-7.cfg -### %s -c 2>&1 | FileCheck %s -check-prefix=CONF
4+
5+
! CHECK: "-fc1"
6+
! CHECK-NOT: "-Oignore"
7+
! CHECK: "-Omagic"
8+
! CHECK-NOT: "-Oignore"
9+
10+
! RM-WERROR: ### FCC_OVERRIDE_OPTIONS: x-Werror +-g
11+
! RM-WERROR-NEXT: ### Deleting argument -Werror
12+
! RM-WERROR-NEXT: ### Adding argument -g at end
13+
! RM-WERROR-NOT: "-Werror"
14+
15+
! Test that FCC_OVERRIDE_OPTIONS does not affect the options from config files.
16+
! CONF: ### FCC_OVERRIDE_OPTIONS: x-Werror
17+
! CONF: "-Werror"

flang/tools/flang-driver/driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ int main(int argc, const char **argv) {
111111
}
112112
}
113113

114+
llvm::StringSet<> savedStrings;
115+
// Handle FCC_OVERRIDE_OPTIONS, used for editing a command line behind the
116+
// scenes.
117+
if (const char *overrideStr = ::getenv("FCC_OVERRIDE_OPTIONS"))
118+
clang::driver::applyOverrideOptions(args, overrideStr, savedStrings,
119+
"FCC_OVERRIDE_OPTIONS", &llvm::errs());
120+
114121
// Not in the frontend mode - continue in the compiler driver mode.
115122

116123
// Create DiagnosticsEngine for the compiler driver

0 commit comments

Comments
 (0)