Skip to content

Commit f0a07d9

Browse files
committed
Do not hardcode intrinsics dir
1 parent f674542 commit f0a07d9

File tree

14 files changed

+129
-46
lines changed

14 files changed

+129
-46
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7097,12 +7097,13 @@ def cpp : Flag<["-"], "cpp">, Group<f_Group>,
70977097
HelpText<"Enable predefined and command line preprocessor macros">;
70987098
def nocpp : Flag<["-"], "nocpp">, Group<f_Group>,
70997099
HelpText<"Disable predefined and command line preprocessor macros">;
7100+
7101+
// TODO: separate
71007102
def module_dir : JoinedOrSeparate<["-"], "module-dir">, MetaVarName<"<dir>">,
71017103
HelpText<"Put MODULE files in <dir>">,
71027104
DocBrief<[{This option specifies where to put .mod files for compiled modules.
71037105
It is also added to the list of directories to be searched by an USE statement.
71047106
The default is the current directory.}]>;
7105-
71067107
def module_dir_EQ : Joined<["-"], "module-dir=">, Alias<module_dir>;
71077108

71087109
def ffixed_form : Flag<["-"], "ffixed-form">, Group<f_Group>,
@@ -7243,7 +7244,8 @@ def fdo_concurrent_to_openmp_EQ : Joined<["-"], "fdo-concurrent-to-openmp=">,
72437244
def J : JoinedOrSeparate<["-"], "J">,
72447245
Flags<[RenderJoined]>, Visibility<[FlangOption, FC1Option]>,
72457246
Group<gfortran_Group>,
7246-
Alias<module_dir>;
7247+
Alias<module_dir> // TODO: remove, searchDirectoriesFromDashJ vs searchDirectoriesFromIntrModPath
7248+
;
72477249

72487250
//===----------------------------------------------------------------------===//
72497251
// FC1 Options

clang/lib/Driver/ToolChain.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ ToolChain::ToolChain(const Driver &D, const llvm::Triple &T,
103103
for (const auto &Path : getArchSpecificLibPaths())
104104
addIfExists(getFilePaths(), Path);
105105

106+
#if 0
106107
if (D.IsFlangMode()) {
107108
getIntrinsicModulePaths().append(getDefaultIntrinsicModulePaths());
108109
}
110+
#endif
109111
}
110112

113+
111114
llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
112115
ToolChain::executeToolChainProgram(StringRef Executable) const {
113116
llvm::SmallString<64> OutputFile;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,12 +957,15 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
957957
CmdArgs.push_back("-resource-dir");
958958
CmdArgs.push_back(D.ResourceDir.c_str());
959959

960+
// implicitly added by frontend
961+
// TODO: should be set by driver
962+
#if 0
960963
// Default intrinsic module dirs must be added after any user-provided -fintrinsic-modules-path
961964
for (auto && IntrModPath : TC.getIntrinsicModulePaths()) {
962965
CmdArgs.push_back("-fintrinsic-modules-path");
963966
CmdArgs.push_back(IntrModPath.c_str());
964967
}
965-
968+
#endif
966969

967970

968971
// Offloading related options

flang/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ set(FLANG_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
279279
"Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
280280
mark_as_advanced(FLANG_TOOLS_INSTALL_DIR)
281281

282-
set(FLANG_INTRINSIC_MODULES_DIR "${CMAKE_BINARY_DIR}/lib/clang/21/finclude/x86_64-unknown-linux-gnu")
282+
#set(FLANG_INTRINSIC_MODULES_DIR "${CMAKE_BINARY_DIR}/lib/clang/21/finclude/x86_64-unknown-linux-gnu")
283+
set(FLANG_INTRINSIC_MODULES_DIR "" CACHE PATH "Additional search path for modules; needed for tests if not building flang-rt in a bootstrapping build")
283284
set(FLANG_INCLUDE_DIR "${FLANG_BINARY_DIR}/include")
284285

285286
# TODO: Remove when libclangDriver is lifted out of Clang

flang/include/flang/Frontend/CompilerInvocation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CompilerInvocation : public CompilerInvocationBase {
9797
/// Semantic options
9898
// TODO: Merge with or translate to frontendOpts. We shouldn't need two sets
9999
// of options.
100-
std::string moduleDir = ".";
100+
std::string moduleDir = "."; // MK: even used?
101101

102102
std::string moduleFileSuffix = ".mod";
103103

@@ -288,7 +288,7 @@ class CompilerInvocation : public CompilerInvocationBase {
288288

289289
/// Set the Fortran options to user-specified values.
290290
/// These values are found in the preprocessor options.
291-
void setFortranOpts();
291+
void setFortranOpts( const llvm::TargetMachine &targetMachine);
292292

293293
/// Set the Semantic Options
294294
void setSemanticsOpts(Fortran::parser::AllCookedSources &);

flang/include/flang/Frontend/PreprocessorOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct PreprocessorOptions {
4545
// consider collecting them in a separate aggregate. For now we keep it here
4646
// as there is no point creating a class for just one field.
4747
std::vector<std::string> searchDirectoriesFromDashI;
48-
std::vector<std::string> searchDirectoriesFromDashJ;
48+
std::vector<std::string> searchDirectoriesFromDashJ; // MK: Is this even used?
4949
// Search directories specified by the user with -fintrinsic-modules-path
5050
std::vector<std::string> searchDirectoriesFromIntrModPath;
5151

flang/include/flang/Parser/options.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ struct Options {
2626
bool isFixedForm{false}; bool isIntrinsicMode{false};
2727
int fixedFormColumns{72};
2828
common::LanguageFeatureControl features;
29+
30+
// -J
2931
std::vector<std::string> searchDirectories;
32+
33+
// -fintrinsic-modules-path
3034
std::vector<std::string> intrinsicModuleDirectories;
35+
3136
std::vector<Predefinition> predefinitions;
3237
bool instrumentedParse{false};
3338
bool isModuleFile{false};

flang/include/flang/Semantics/semantics.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,16 @@ class SemanticsContext {
315315
const common::LangOptions &langOpts_;
316316
parser::AllCookedSources &allCookedSources_;
317317
std::optional<parser::CharBlock> location_;
318+
319+
// -J
318320
std::vector<std::string> searchDirectories_;
321+
322+
// -fintrinsic_modules_path
319323
std::vector<std::string> intrinsicModuleDirectories_;
324+
325+
// -module-dir ???
320326
std::string moduleDirectory_{"."s};
327+
321328
std::string moduleFileSuffix_{".mod"};
322329
bool underscoring_{true};
323330
bool warnOnNonstandardUsage_{false};

flang/lib/Frontend/CompilerInstance.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,17 @@ bool CompilerInstance::executeAction(FrontendAction &act) {
156156

157157
// Set some sane defaults for the frontend.
158158
invoc.setDefaultFortranOpts();
159-
// Update the fortran options based on user-based input.
160-
invoc.setFortranOpts();
161-
// Set the encoding to read all input files in based on user input.
162-
allSources->set_encoding(invoc.getFortranOpts().encoding);
159+
160+
163161
if (!setUpTargetMachine())
164162
return false;
163+
164+
// Set the encoding to read all input files in based on user input.
165+
allSources->set_encoding(invoc.getFortranOpts().encoding);
166+
167+
// Update the fortran options based on user-based input.
168+
invoc.setFortranOpts(getTargetMachine());
169+
165170
// Set options controlling lowering to FIR.
166171
invoc.setLoweringOptions();
167172

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
936936
for (const auto *currentArg : args.filtered(clang::driver::options::OPT_J))
937937
opts.searchDirectoriesFromDashJ.emplace_back(currentArg->getValue());
938938

939-
// Prepend the ordered list of -intrinsic-modules-path
939+
// Prepend the ordered list of -fintrinsic-modules-path
940940
// to the default location to search.
941941
for (const auto *currentArg :
942942
args.filtered(clang::driver::options::OPT_fintrinsic_modules_path))
@@ -1748,7 +1748,7 @@ void CompilerInvocation::setDefaultPredefinitions() {
17481748
}
17491749
}
17501750

1751-
void CompilerInvocation::setFortranOpts() {
1751+
void CompilerInvocation::setFortranOpts(const llvm::TargetMachine &targetMachine) {
17521752
auto &fortranOptions = getFortranOpts();
17531753
const auto &frontendOptions = getFrontendOpts();
17541754
const auto &preprocessorOptions = getPreprocessorOpts();
@@ -1773,15 +1773,20 @@ void CompilerInvocation::setFortranOpts() {
17731773
preprocessorOptions.searchDirectoriesFromDashI.begin(),
17741774
preprocessorOptions.searchDirectoriesFromDashI.end());
17751775

1776-
// Add the ordered list of -intrinsic-modules-path
1776+
// Add the ordered list of -fintrinsic-modules-path
17771777
#if 1
17781778
// Legacy
17791779
fortranOptions.searchDirectories.insert(
17801780
fortranOptions.searchDirectories.end(),
1781-
preprocessorOptions.searchDirectoriesFromIntrModPath.begin(),
1781+
preprocessorOptions.searchDirectoriesFromIntrModPath.begin(), // should this be searchDirectoriesFromDashJ???
17821782
preprocessorOptions.searchDirectoriesFromIntrModPath.end());
17831783
#endif
17841784

1785+
1786+
1787+
1788+
1789+
17851790
// Add the default intrinsic module directory
17861791
#if 1
17871792
// gfortran prepends this path to the usual intrinsics dir
@@ -1791,15 +1796,38 @@ void CompilerInvocation::setFortranOpts() {
17911796
preprocessorOptions.searchDirectoriesFromIntrModPath.end());
17921797
#endif
17931798

1794-
// llvm::SmallString<128> intrinsicsDir { resourceDir };
1795-
// llvm::sys::path::append(intrinsicsDir, "finclude", triple);
1796-
// fortranOptions.intrinsicModuleDirectories.emplace_back(intrinsicsDir);
1797-
// fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir(getArgv0()));
1799+
// FIXME: Assembling the list of module search paths is the driver's job.
1800+
auto triple = targetMachine.getTargetTriple().str();
1801+
// std::string mainExe = llvm::sys::fs::getMainExecutable(argv[0], nullptr);
1802+
// llvm::StringRef prefixDir = llvm::sys::path::parent_path( llvm::sys::path::parent_path(mainExe ));
1803+
llvm::SmallString<128> resourceDir(this->resourceDir);
1804+
//llvm::sys::path::append(resourceDir, /*CLANG_INSTALL_LIBDIR_BASENAME*/ "lib", "clang", FLANG_VERSION_MAJOR_STRING);
1805+
llvm::sys::path::append(resourceDir, "finclude", triple);
1806+
// llvm::errs() << resourceDir << "\n";
1807+
fortranOptions.intrinsicModuleDirectories.push_back(resourceDir.str().str());
1808+
1809+
#if 0
1810+
llvm::errs() << "\nnew intr search dirs:\n";
1811+
for (auto&& x : fortranOptions.intrinsicModuleDirectories) {
1812+
llvm::errs() << x << "\n";
1813+
}
1814+
#endif
1815+
1816+
#if 0
1817+
llvm::SmallString<128> intrinsicsDir { resourceDir };
1818+
llvm::sys::path::append(intrinsicsDir, "finclude", triple);
1819+
fortranOptions.intrinsicModuleDirectories.emplace_back(intrinsicsDir);
1820+
fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir(getArgv0()));
1821+
#endif
1822+
17981823

1824+
// FIXME: Assembling the list of module search paths is the driver's job.
1825+
#if 1
17991826
// Add the directory supplied through -J/-module-dir to the list of search
18001827
// directories
18011828
if (moduleDirJ != ".")
18021829
fortranOptions.searchDirectories.emplace_back(moduleDirJ);
1830+
#endif
18031831

18041832
if (frontendOptions.instrumentedParse)
18051833
fortranOptions.instrumentedParse = true;
@@ -1819,10 +1847,45 @@ CompilerInvocation::getSemanticsCtx(
18191847
const llvm::TargetMachine &targetMachine) {
18201848
auto &fortranOptions = getFortranOpts();
18211849

1850+
#if 0
1851+
auto &moduleDirJ = getModuleDir();
1852+
1853+
auto triple = targetMachine.getTargetTriple();
1854+
llvm::SmallVector<std::string> searchDirectories{ llvm ::ArrayRef ( fortranOptions.searchDirectories))};
1855+
if (moduleDirJ != ".")
1856+
searchDirectories.emplace_back(moduleDirJ);
1857+
1858+
llvm::SmallVector<std::string> intrinsicModuleDirectories { llvm::ArrayRef(fortranOptions.intrinsicModuleDirectories)};
1859+
1860+
// FIXME: Do not modify intrinsicIncludeDirs directly, should be treated as immutable just like argv.
1861+
// TODO: Normlize triple directory as clang does; best refactor with ToolChain::getDefaultIntrinsicModulePaths() used by the flang driver.
1862+
std::string mainExe = llvm::sys::fs::getMainExecutable(argv[0], nullptr);
1863+
llvm::StringRef prefixDir = llvm::sys::path::parent_path( llvm::sys::path::parent_path(mainExe ));
1864+
llvm::SmallString<128> resourceDir (prefixDir);
1865+
llvm::sys::path::append(resourceDir, /*CLANG_INSTALL_LIBDIR_BASENAME*/ "lib", "clang", FLANG_VERSION_MAJOR_STRING);
1866+
llvm::sys::path::append(resourceDir, "finclude", triple);
1867+
llvm::errs() << resourceDir << "\n";
1868+
intrinsicIncludeDirs.push_back(resourceDir.str().str());
1869+
1870+
#endif
1871+
1872+
18221873
auto semanticsContext = std::make_unique<semantics::SemanticsContext>(
18231874
getDefaultKinds(), fortranOptions.features, getLangOpts(),
18241875
allCookedSources);
18251876

1877+
#if 0
1878+
llvm::errs() << "\nsearch dirs:\n";
1879+
for (auto&& x : fortranOptions.searchDirectories) {
1880+
llvm::errs() << x << "\n";
1881+
}
1882+
1883+
llvm::errs() << "\nintrinsic search dirs:\n";
1884+
for (auto&& x : fortranOptions.intrinsicModuleDirectories) {
1885+
llvm::errs() << x << "\n";
1886+
}
1887+
#endif
1888+
18261889
semanticsContext->set_moduleDirectory(getModuleDir())
18271890
.set_searchDirectories(fortranOptions.searchDirectories)
18281891
.set_intrinsicModuleDirectories(fortranOptions.intrinsicModuleDirectories)

0 commit comments

Comments
 (0)