Skip to content

Commit 84a0a3d

Browse files
authored
[flang][driver] Make the -J option less restrictive so we would not have to struggle with autoconf (#110010)
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 f1c2331 commit 84a0a3d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 3 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>
@@ -830,14 +831,20 @@ static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
830831
unsigned numErrorsBefore = diags.getNumErrors();
831832

832833
// -J/module-dir option
833-
auto moduleDirList =
834+
std::vector<std::string> 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, but may repeat
837+
// -J/-module-dir as long as the directory is the same each time.
836838
// https://gcc.gnu.org/onlinedocs/gfortran/Directory-Options.html
839+
std::sort(moduleDirList.begin(), moduleDirList.end());
840+
moduleDirList.erase(std::unique(moduleDirList.begin(), moduleDirList.end()),
841+
moduleDirList.end());
837842
if (moduleDirList.size() > 1) {
838843
const unsigned diagID =
839844
diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
840-
"Only one '-module-dir/-J' option allowed");
845+
"Only one '-module-dir/-J' directory allowed. "
846+
"'-module-dir/-J' may be given multiple times "
847+
"but the directory must be the same each time.");
841848
diags.Report(diagID);
842849
}
843850
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)