2018-08-15 00:03:32 +08:00
|
|
|
//===--- ModuleAssistant.cpp - Module map generation manager --*- C++ -*---===//
|
2013-10-15 21:52:33 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-10-15 21:52:33 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2013-10-15 21:52:33 +08:00
|
|
|
//
|
|
|
|
// This file defines the module generation entry point function,
|
|
|
|
// createModuleMap, a Module class for representing a module,
|
|
|
|
// and various implementation functions for doing the underlying
|
|
|
|
// work, described below.
|
|
|
|
//
|
|
|
|
// The "Module" class represents a module, with members for storing the module
|
|
|
|
// name, associated header file names, and sub-modules, and an "output"
|
|
|
|
// function that recursively writes the module definitions.
|
|
|
|
//
|
|
|
|
// The "createModuleMap" function implements the top-level logic of the
|
|
|
|
// assistant mode. It calls a loadModuleDescriptions function to walk
|
|
|
|
// the header list passed to it and creates a tree of Module objects
|
|
|
|
// representing the module hierarchy, represented by a "Module" object,
|
|
|
|
// the "RootModule". This root module may or may not represent an actual
|
|
|
|
// module in the module map, depending on the "--root-module" option passed
|
|
|
|
// to modularize. It then calls a writeModuleMap function to set up the
|
|
|
|
// module map file output and walk the module tree, outputting the module
|
|
|
|
// map file using a stream obtained and managed by an
|
2017-09-23 09:03:17 +08:00
|
|
|
// llvm::ToolOutputFile object.
|
2013-10-15 21:52:33 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2013-10-15 21:52:33 +08:00
|
|
|
|
|
|
|
#include "Modularize.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Local definitions:
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Internal class definitions:
|
|
|
|
|
|
|
|
// Represents a module.
|
|
|
|
class Module {
|
|
|
|
public:
|
2015-07-10 08:37:25 +08:00
|
|
|
Module(llvm::StringRef Name, bool Problem);
|
2013-10-15 21:52:33 +08:00
|
|
|
~Module();
|
|
|
|
bool output(llvm::raw_fd_ostream &OS, int Indent);
|
|
|
|
Module *findSubModule(llvm::StringRef SubName);
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string Name;
|
|
|
|
std::vector<std::string> HeaderFileNames;
|
|
|
|
std::vector<Module *> SubModules;
|
2015-07-10 08:37:25 +08:00
|
|
|
bool IsProblem;
|
2013-10-15 21:52:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
// Module functions:
|
|
|
|
|
|
|
|
// Constructors.
|
2015-07-10 08:37:25 +08:00
|
|
|
Module::Module(llvm::StringRef Name, bool Problem)
|
|
|
|
: Name(Name), IsProblem(Problem) {}
|
2013-10-15 21:52:33 +08:00
|
|
|
|
|
|
|
// Destructor.
|
|
|
|
Module::~Module() {
|
|
|
|
// Free submodules.
|
2015-01-22 21:14:29 +08:00
|
|
|
while (!SubModules.empty()) {
|
2013-10-15 21:52:33 +08:00
|
|
|
Module *last = SubModules.back();
|
|
|
|
SubModules.pop_back();
|
|
|
|
delete last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a module hierarchy to the given output stream.
|
|
|
|
bool Module::output(llvm::raw_fd_ostream &OS, int Indent) {
|
|
|
|
// If this is not the nameless root module, start a module definition.
|
|
|
|
if (Name.size() != 0) {
|
|
|
|
OS.indent(Indent);
|
|
|
|
OS << "module " << Name << " {\n";
|
|
|
|
Indent += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output submodules.
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
|
2013-10-15 21:52:33 +08:00
|
|
|
if (!(*I)->output(OS, Indent))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output header files.
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E;
|
|
|
|
++I) {
|
2013-10-15 21:52:33 +08:00
|
|
|
OS.indent(Indent);
|
2016-03-22 07:05:14 +08:00
|
|
|
if (IsProblem || strstr((*I).c_str(), ".inl"))
|
2015-07-10 08:37:25 +08:00
|
|
|
OS << "exclude header \"" << *I << "\"\n";
|
|
|
|
else
|
|
|
|
OS << "header \"" << *I << "\"\n";
|
2013-10-15 21:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this module has header files, output export directive.
|
|
|
|
if (HeaderFileNames.size() != 0) {
|
|
|
|
OS.indent(Indent);
|
|
|
|
OS << "export *\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is not the nameless root module, close the module definition.
|
|
|
|
if (Name.size() != 0) {
|
|
|
|
Indent -= 2;
|
|
|
|
OS.indent(Indent);
|
|
|
|
OS << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup a sub-module.
|
|
|
|
Module *Module::findSubModule(llvm::StringRef SubName) {
|
2016-12-14 23:29:23 +08:00
|
|
|
for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
|
2013-10-15 21:52:33 +08:00
|
|
|
if ((*I)->Name == SubName)
|
|
|
|
return *I;
|
|
|
|
}
|
2014-06-09 10:03:06 +08:00
|
|
|
return nullptr;
|
2013-10-15 21:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation functions:
|
|
|
|
|
2015-02-10 22:45:30 +08:00
|
|
|
// Reserved keywords in module.modulemap syntax.
|
2013-10-15 21:52:33 +08:00
|
|
|
// Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp,
|
|
|
|
// such as in ModuleMapParser::consumeToken().
|
2015-10-18 13:14:41 +08:00
|
|
|
static const char *const ReservedNames[] = {
|
2013-10-15 21:52:33 +08:00
|
|
|
"config_macros", "export", "module", "conflict", "framework",
|
|
|
|
"requires", "exclude", "header", "private", "explicit",
|
2014-06-09 10:03:06 +08:00
|
|
|
"link", "umbrella", "extern", "use", nullptr // Flag end.
|
2013-10-15 21:52:33 +08:00
|
|
|
};
|
|
|
|
|
2014-01-09 09:39:49 +08:00
|
|
|
// Convert module name to a non-keyword.
|
2013-10-15 21:52:33 +08:00
|
|
|
// Prepends a '_' to the name if and only if the name is a keyword.
|
|
|
|
static std::string
|
|
|
|
ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string SafeName(MightBeReservedName);
|
2014-06-09 10:03:06 +08:00
|
|
|
for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) {
|
2013-10-15 21:52:33 +08:00
|
|
|
if (MightBeReservedName == ReservedNames[Index]) {
|
|
|
|
SafeName.insert(0, "_");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SafeName;
|
|
|
|
}
|
|
|
|
|
2016-03-22 07:05:14 +08:00
|
|
|
// Convert module name to a non-keyword.
|
|
|
|
// Prepends a '_' to the name if and only if the name is a keyword.
|
|
|
|
static std::string
|
|
|
|
ensureVaidModuleName(llvm::StringRef MightBeInvalidName) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string SafeName(MightBeInvalidName);
|
2016-03-22 07:05:14 +08:00
|
|
|
std::replace(SafeName.begin(), SafeName.end(), '-', '_');
|
|
|
|
std::replace(SafeName.begin(), SafeName.end(), '.', '_');
|
|
|
|
if (isdigit(SafeName[0]))
|
|
|
|
SafeName = "_" + SafeName;
|
|
|
|
return SafeName;
|
|
|
|
}
|
|
|
|
|
2013-10-15 21:52:33 +08:00
|
|
|
// Add one module, given a header file path.
|
|
|
|
static bool addModuleDescription(Module *RootModule,
|
|
|
|
llvm::StringRef HeaderFilePath,
|
|
|
|
llvm::StringRef HeaderPrefix,
|
2015-07-10 08:37:25 +08:00
|
|
|
DependencyMap &Dependencies,
|
|
|
|
bool IsProblemFile) {
|
2013-10-15 21:52:33 +08:00
|
|
|
Module *CurrentModule = RootModule;
|
|
|
|
DependentsVector &FileDependents = Dependencies[HeaderFilePath];
|
|
|
|
std::string FilePath;
|
|
|
|
// Strip prefix.
|
2013-10-16 09:42:33 +08:00
|
|
|
// HeaderFilePath should be compared to natively-canonicalized Prefix.
|
|
|
|
llvm::SmallString<256> NativePath, NativePrefix;
|
|
|
|
llvm::sys::path::native(HeaderFilePath, NativePath);
|
|
|
|
llvm::sys::path::native(HeaderPrefix, NativePrefix);
|
|
|
|
if (NativePath.startswith(NativePrefix))
|
2020-01-29 03:23:46 +08:00
|
|
|
FilePath = std::string(NativePath.substr(NativePrefix.size() + 1));
|
2013-10-15 21:52:33 +08:00
|
|
|
else
|
2020-01-29 03:23:46 +08:00
|
|
|
FilePath = std::string(HeaderFilePath);
|
2013-10-15 21:52:33 +08:00
|
|
|
int Count = FileDependents.size();
|
|
|
|
// Headers that go into modules must not depend on other files being
|
|
|
|
// included first. If there are any dependents, warn user and omit.
|
|
|
|
if (Count != 0) {
|
|
|
|
llvm::errs() << "warning: " << FilePath
|
|
|
|
<< " depends on other headers being included first,"
|
2015-02-10 22:45:30 +08:00
|
|
|
" meaning the module.modulemap won't compile."
|
2013-10-15 21:52:33 +08:00
|
|
|
" This header will be omitted from the module map.\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Make canonical.
|
|
|
|
std::replace(FilePath.begin(), FilePath.end(), '\\', '/');
|
|
|
|
// Insert module into tree, using subdirectories as submodules.
|
|
|
|
for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
|
|
|
|
E = llvm::sys::path::end(FilePath);
|
|
|
|
I != E; ++I) {
|
|
|
|
if ((*I)[0] == '.')
|
|
|
|
continue;
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string Stem(llvm::sys::path::stem(*I));
|
2013-10-15 21:52:33 +08:00
|
|
|
Stem = ensureNoCollisionWithReservedName(Stem);
|
2016-03-22 07:05:14 +08:00
|
|
|
Stem = ensureVaidModuleName(Stem);
|
2013-10-15 21:52:33 +08:00
|
|
|
Module *SubModule = CurrentModule->findSubModule(Stem);
|
2014-06-09 10:03:06 +08:00
|
|
|
if (!SubModule) {
|
2015-07-10 08:37:25 +08:00
|
|
|
SubModule = new Module(Stem, IsProblemFile);
|
2013-10-15 21:52:33 +08:00
|
|
|
CurrentModule->SubModules.push_back(SubModule);
|
|
|
|
}
|
|
|
|
CurrentModule = SubModule;
|
|
|
|
}
|
|
|
|
// Add header file name to headers.
|
|
|
|
CurrentModule->HeaderFileNames.push_back(FilePath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the internal module tree representation.
|
|
|
|
static Module *loadModuleDescriptions(
|
|
|
|
llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
|
2015-07-10 08:37:25 +08:00
|
|
|
llvm::ArrayRef<std::string> ProblemFileNames,
|
2013-10-15 21:52:33 +08:00
|
|
|
DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
|
|
|
|
|
|
|
|
// Create root module.
|
2016-12-14 23:29:23 +08:00
|
|
|
auto *RootModule = new Module(RootModuleName, false);
|
2013-10-15 21:52:33 +08:00
|
|
|
|
|
|
|
llvm::SmallString<256> CurrentDirectory;
|
|
|
|
llvm::sys::fs::current_path(CurrentDirectory);
|
|
|
|
|
|
|
|
// If no header prefix, use current directory.
|
|
|
|
if (HeaderPrefix.size() == 0)
|
|
|
|
HeaderPrefix = CurrentDirectory;
|
|
|
|
|
|
|
|
// Walk the header file names and output the module map.
|
|
|
|
for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
|
|
|
|
E = HeaderFileNames.end();
|
|
|
|
I != E; ++I) {
|
2015-07-10 08:37:25 +08:00
|
|
|
std::string Header(*I);
|
|
|
|
bool IsProblemFile = false;
|
|
|
|
for (auto &ProblemFile : ProblemFileNames) {
|
|
|
|
if (ProblemFile == Header) {
|
|
|
|
IsProblemFile = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 21:52:33 +08:00
|
|
|
// Add as a module.
|
2015-07-10 08:37:25 +08:00
|
|
|
if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
|
2014-06-09 10:03:06 +08:00
|
|
|
return nullptr;
|
2013-10-15 21:52:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RootModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kick off the writing of the module map.
|
|
|
|
static bool writeModuleMap(llvm::StringRef ModuleMapPath,
|
|
|
|
llvm::StringRef HeaderPrefix, Module *RootModule) {
|
|
|
|
llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
|
|
|
|
llvm::sys::path::remove_filename(HeaderDirectory);
|
|
|
|
llvm::SmallString<256> FilePath;
|
|
|
|
|
|
|
|
// Get the module map file path to be used.
|
|
|
|
if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
|
|
|
|
FilePath = HeaderPrefix;
|
|
|
|
// Prepend header file name prefix if it's not absolute.
|
|
|
|
llvm::sys::path::append(FilePath, ModuleMapPath);
|
|
|
|
llvm::sys::path::native(FilePath);
|
|
|
|
} else {
|
|
|
|
FilePath = ModuleMapPath;
|
|
|
|
llvm::sys::path::native(FilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up module map output file.
|
2014-08-26 02:17:00 +08:00
|
|
|
std::error_code EC;
|
2021-04-06 19:22:41 +08:00
|
|
|
llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::OF_TextWithCRLF);
|
2014-08-26 02:17:00 +08:00
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
|
|
|
|
<< EC.message() << "\n";
|
2013-10-15 21:52:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get output stream from tool output buffer/manager.
|
|
|
|
llvm::raw_fd_ostream &OS = Out.os();
|
|
|
|
|
|
|
|
// Output file comment.
|
|
|
|
OS << "// " << ModuleMapPath << "\n";
|
|
|
|
OS << "// Generated by: " << CommandLine << "\n\n";
|
|
|
|
|
|
|
|
// Write module hierarchy from internal representation.
|
|
|
|
if (!RootModule->output(OS, 0))
|
|
|
|
return false;
|
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
// Tell ToolOutputFile that we want to keep the file.
|
2013-10-15 21:52:33 +08:00
|
|
|
Out.keep();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global functions:
|
|
|
|
|
|
|
|
// Module map generation entry point.
|
|
|
|
bool createModuleMap(llvm::StringRef ModuleMapPath,
|
|
|
|
llvm::ArrayRef<std::string> HeaderFileNames,
|
2015-07-10 08:37:25 +08:00
|
|
|
llvm::ArrayRef<std::string> ProblemFileNames,
|
2013-10-15 21:52:33 +08:00
|
|
|
DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
|
|
|
|
llvm::StringRef RootModuleName) {
|
|
|
|
// Load internal representation of modules.
|
2015-07-10 08:37:25 +08:00
|
|
|
std::unique_ptr<Module> RootModule(
|
|
|
|
loadModuleDescriptions(
|
|
|
|
RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
|
|
|
|
HeaderPrefix));
|
2013-10-15 21:52:33 +08:00
|
|
|
if (!RootModule.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Write module map file.
|
|
|
|
return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
|
|
|
|
}
|