6060#include " clang/Driver/Compilation.h"
6161#include " clang/Driver/InputInfo.h"
6262#include " clang/Driver/Job.h"
63+ #include " clang/Driver/ModulesDriver.h"
6364#include " clang/Driver/Options.h"
6465#include " clang/Driver/Phases.h"
6566#include " clang/Driver/SanitizerArgs.h"
8788#include " llvm/Support/FileSystem.h"
8889#include " llvm/Support/FileUtilities.h"
8990#include " llvm/Support/FormatVariadic.h"
91+ #include " llvm/Support/JSON.h"
9092#include " llvm/Support/MD5.h"
9193#include " llvm/Support/Path.h"
9294#include " llvm/Support/PrettyStackTrace.h"
@@ -1829,6 +1831,53 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
18291831 // Populate the tool chains for the offloading devices, if any.
18301832 CreateOffloadingDeviceToolChains (*C, Inputs);
18311833
1834+ modules::StdModuleManifest Manifest;
1835+ if (C->getArgs ().hasFlag (options::OPT_fmodules_driver,
1836+ options::OPT_fno_modules_driver, false )) {
1837+ Diags.Report (diag::remark_performing_driver_managed_module_build);
1838+ // TODO: Once -fmodules-driver is no longer experimental, move
1839+ // TODO: The detection logic to implicitly enable -fmodules-driver is kept
1840+ // here only for diagnostics until the feature is no longer experimental.
1841+ auto EnableOrErr = modules::shouldUseModulesDriver (Inputs, getVFS (), Diags);
1842+ if (!EnableOrErr) {
1843+ llvm::handleAllErrors (
1844+ EnableOrErr.takeError (), [&](const llvm::FileError &Err) {
1845+ Diags.Report (diag::err_cannot_open_file)
1846+ << Err.getFileName () << Err.messageWithoutFileInfo ();
1847+ });
1848+ return C;
1849+ }
1850+
1851+ // Read the standard modules manifest, and if available, add all discovered
1852+ // modules to the compilation. Compilation jobs for modules discovered from
1853+ // the manifest, which are not imported by any other source input, are
1854+ // pruned later.
1855+ const auto StdModuleManifestPath =
1856+ GetStdModuleManifestPath (*C, C->getDefaultToolChain ());
1857+ if (StdModuleManifestPath == " <NOT PRESENT>" ) {
1858+ Diags.Report (diag::remark_modules_manifest_not_found);
1859+ } else {
1860+ Diags.Report (diag::remark_using_modules_manifest)
1861+ << StdModuleManifestPath;
1862+ if (auto ManifestOrErr =
1863+ modules::readStdModuleManifest (StdModuleManifestPath, getVFS ())) {
1864+ Manifest = std::move (*ManifestOrErr);
1865+ } else {
1866+ llvm::handleAllErrors (
1867+ ManifestOrErr.takeError (),
1868+ [&](llvm::json::ParseError &Err) {
1869+ Diags.Report (diag::err_modules_manifest_failed_parse)
1870+ << Err.message ();
1871+ },
1872+ [&](llvm::FileError &Err) {
1873+ Diags.Report (diag::err_cannot_open_file)
1874+ << Err.getFileName () << Err.messageWithoutFileInfo ();
1875+ });
1876+ }
1877+ }
1878+ modules::buildStdModuleManifestInputs (Manifest, *C, Inputs);
1879+ }
1880+
18321881 // Construct the list of abstract actions to perform for this compilation. On
18331882 // MachO targets this uses the driver-driver and universal actions.
18341883 if (TC.getTriple ().isOSBinFormatMachO ())
@@ -1843,6 +1892,11 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
18431892
18441893 BuildJobs (*C);
18451894
1895+ if (C->getArgs ().hasFlag (options::OPT_fmodules_driver,
1896+ options::OPT_fno_modules_driver, false )) {
1897+ modules::planDriverManagedModuleCompilation (*C, Manifest);
1898+ }
1899+
18461900 return C;
18471901}
18481902
@@ -4320,33 +4374,6 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
43204374 }
43214375}
43224376
4323- static bool hasCXXModuleInputType (const Driver::InputList &Inputs) {
4324- const auto IsTypeCXXModule = [](const auto &Input) -> bool {
4325- const auto TypeID = Input.first ;
4326- return (TypeID == types::TY_CXXModule);
4327- };
4328- return llvm::any_of (Inputs, IsTypeCXXModule);
4329- }
4330-
4331- llvm::ErrorOr<bool >
4332- Driver::ScanInputsForCXX20ModulesUsage (const InputList &Inputs) const {
4333- const auto CXXInputs = llvm::make_filter_range (
4334- Inputs, [](const auto &Input) { return types::isCXX (Input.first ); });
4335- for (const auto &Input : CXXInputs) {
4336- StringRef Filename = Input.second ->getSpelling ();
4337- auto ErrOrBuffer = VFS->getBufferForFile (Filename);
4338- if (!ErrOrBuffer)
4339- return ErrOrBuffer.getError ();
4340- const auto Buffer = std::move (*ErrOrBuffer);
4341-
4342- if (scanInputForCXX20ModulesUsage (Buffer->getBuffer ())) {
4343- Diags.Report (diag::remark_found_cxx20_module_usage) << Filename;
4344- return true ;
4345- }
4346- }
4347- return false ;
4348- }
4349-
43504377void Driver::BuildActions (Compilation &C, DerivedArgList &Args,
43514378 const InputList &Inputs, ActionList &Actions) const {
43524379 llvm::PrettyStackTraceString CrashInfo (" Building compilation actions" );
@@ -4358,33 +4385,6 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43584385
43594386 handleArguments (C, Args, Inputs, Actions);
43604387
4361- if (Args.hasFlag (options::OPT_fmodules_driver,
4362- options::OPT_fno_modules_driver, false )) {
4363- // TODO: Move the logic for implicitly enabling explicit-module-builds out
4364- // of -fmodules-driver once it is no longer experimental.
4365- // Currently, this serves diagnostic purposes only.
4366- bool UsesCXXModules = hasCXXModuleInputType (Inputs);
4367- if (!UsesCXXModules) {
4368- const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage (Inputs);
4369- if (!ErrOrScanResult) {
4370- Diags.Report (diag::err_cannot_open_file)
4371- << ErrOrScanResult.getError ().message ();
4372- return ;
4373- }
4374- UsesCXXModules = *ErrOrScanResult;
4375- }
4376- if (UsesCXXModules || Args.hasArg (options::OPT_fmodules))
4377- BuildDriverManagedModuleBuildActions (C, Args, Inputs, Actions);
4378- return ;
4379- }
4380-
4381- BuildDefaultActions (C, Args, Inputs, Actions);
4382- }
4383-
4384- void Driver::BuildDefaultActions (Compilation &C, DerivedArgList &Args,
4385- const InputList &Inputs,
4386- ActionList &Actions) const {
4387-
43884388 bool UseNewOffloadingDriver =
43894389 C.isOffloadingHostKind (Action::OFK_OpenMP) ||
43904390 C.isOffloadingHostKind (Action::OFK_SYCL) ||
@@ -4680,12 +4680,6 @@ void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
46804680 Args.ClaimAllArgs (options::OPT_cl_ignored_Group);
46814681}
46824682
4683- void Driver::BuildDriverManagedModuleBuildActions (
4684- Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4685- ActionList &Actions) const {
4686- Diags.Report (diag::remark_performing_driver_managed_module_build);
4687- }
4688-
46894683// / Returns the canonical name for the offloading architecture when using a HIP
46904684// / or CUDA architecture.
46914685static StringRef getCanonicalArchString (Compilation &C,
0 commit comments