Skip to content

Commit 23e07d1

Browse files
committed
[flang][driver] Make the -J option less restrictive so we would not have to struggle with autoconf
There are autoconf-configured projects for which the generated Makefile is invoking flang with more than one -J option, each one specifying the same directory. Although only one module directory should be specified (by either -J or -module-dir), it should not really matter how many times this same directory has been specified. Apparently, other compilers understand it that way, hence autoconf's configure script may generate a Makefile with the repetitive -J's. For example, when trying to build the ABINIT [1] project (which can be configured by either CMake or the configure script) when configured by autoconf, it fails to build as such: ``` make[3]: Entering directory 'src/98_main' mpifort -DHAVE_CONFIG_H -I. -I../../../src/98_main -I../.. -I../../src/incs -I../../../src/incs -Ifallbacks/exports/include -Jbuild/mods -Jbuild/mods -c -o abinit-abinit.o `test -f 'abinit.F90' || echo '../../../src/98_main/'`abinit.F90 error: Only one '-module-dir/-J' option allowed make[3]: *** [Makefile:3961: abinit-abinit.o] Error 1 ``` This patch solves the problem. [1] https://github.com/abinit/abinit.git
1 parent cebb7c0 commit 23e07d1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Support/raw_ostream.h"
4141
#include "llvm/TargetParser/Host.h"
4242
#include "llvm/TargetParser/Triple.h"
43+
#include <algorithm>
4344
#include <cstdlib>
4445
#include <memory>
4546
#include <optional>
@@ -832,12 +833,15 @@ static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
832833
// -J/module-dir option
833834
auto moduleDirList =
834835
args.getAllArgValues(clang::driver::options::OPT_module_dir);
835-
// User can only specify -J/-module-dir once
836+
// User can only specify one -J/-module-dir directory
836837
// https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html
838+
std::sort(moduleDirList.begin(), moduleDirList.end());
839+
moduleDirList.erase(std::unique(moduleDirList.begin(), moduleDirList.end()),
840+
moduleDirList.end());
837841
if (moduleDirList.size() > 1) {
838842
const unsigned diagID =
839843
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
840-
"Only one '-module-dir/-J' option allowed");
844+
"Only one '-module-dir/-J' directory allowed");
841845
diags.Report(diagID);
842846
}
843847
if (moduleDirList.size() == 1)

flang/test/Driver/use-module-error.f90

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
!--------------------------
44
! FLANG DRIVER (flang-new)
55
!--------------------------
6+
! RUN: %flang -fsyntax-only -J %S/Inputs/ %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
7+
! RUN: %flang -fsyntax-only -J %S/Inputs/ -J %S/Inputs/ %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
8+
! RUN: %flang -fsyntax-only -module-dir %S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
9+
! RUN: %flang -fsyntax-only -module-dir %S/Inputs/module-dir -module-dir %S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
10+
! RUN: %flang -fsyntax-only -module-dir %S/Inputs/module-dir -J%S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
611
! RUN: not %flang -fsyntax-only -J %S/Inputs/module-dir -J %S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
712
! RUN: not %flang -fsyntax-only -J %S/Inputs/module-dir -module-dir %S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
813
! RUN: not %flang -fsyntax-only -module-dir %S/Inputs/module-dir -J%S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
914

1015
!-----------------------------------------
1116
! FRONTEND FLANG DRIVER (flang-new -fc1)
1217
!-----------------------------------------
18+
! RUN: %flang_fc1 -fsyntax-only -J %S/Inputs/ %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
19+
! RUN: %flang_fc1 -fsyntax-only -J %S/Inputs/ -J %S/Inputs/ %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
20+
! RUN: %flang_fc1 -fsyntax-only -module-dir %S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
21+
! RUN: %flang_fc1 -fsyntax-only -module-dir %S/Inputs/module-dir -module-dir %S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
22+
! RUN: %flang_fc1 -fsyntax-only -module-dir %S/Inputs/module-dir -J%S/Inputs/module-dir %s 2>&1 | FileCheck %s --allow-empty --check-prefix=SINGLEINCLUDE
1323
! RUN: not %flang_fc1 -fsyntax-only -J %S/Inputs/module-dir -J %S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
1424
! RUN: not %flang_fc1 -fsyntax-only -J %S/Inputs/module-dir -module-dir %S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
1525
! RUN: not %flang_fc1 -fsyntax-only -module-dir %S/Inputs/module-dir -J%S/Inputs/ %s 2>&1 | FileCheck %s --check-prefix=DOUBLEINCLUDE
1626

17-
! DOUBLEINCLUDE:error: Only one '-module-dir/-J' option allowed
27+
! DOUBLEINCLUDE:error: Only one '-module-dir/-J' directory allowed
28+
! SINGLEINCLUDE-NOT:error: Only one '-module-dir/-J' directory allowed
1829

1930
program too_many_module_dirs
2031
end

0 commit comments

Comments
 (0)