32#include "llvm/ADT/SmallString.h"
33#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/Path.h"
35#include "llvm/Support/ToolOutputFile.h"
47 Module(llvm::StringRef Name,
bool Problem);
49 bool output(llvm::raw_fd_ostream &
OS,
int Indent);
50 Module *findSubModule(llvm::StringRef SubName);
54 std::vector<std::string> HeaderFileNames;
55 std::vector<Module *> SubModules;
64Module::Module(llvm::StringRef
Name,
bool Problem)
70 while (!SubModules.empty()) {
71 Module *last = SubModules.back();
72 SubModules.pop_back();
78bool Module::output(llvm::raw_fd_ostream &
OS,
int Indent) {
80 if (
Name.size() != 0) {
82 OS <<
"module " <<
Name <<
" {\n";
87 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
88 if (!(*I)->output(
OS, Indent))
93 for (
auto I = HeaderFileNames.begin(),
E = HeaderFileNames.end(); I !=
E;
96 if (IsProblem || strstr((*I).c_str(),
".inl"))
97 OS <<
"exclude header \"" << *I <<
"\"\n";
99 OS <<
"header \"" << *I <<
"\"\n";
103 if (HeaderFileNames.size() != 0) {
109 if (
Name.size() != 0) {
119Module *Module::findSubModule(llvm::StringRef SubName) {
120 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
121 if ((*I)->Name == SubName)
133 "config_macros",
"export",
"module",
"conflict",
"framework",
134 "requires",
"exclude",
"header",
"private",
"explicit",
135 "link",
"umbrella",
"extern",
"use",
nullptr
142 std::string SafeName(MightBeReservedName);
143 for (
int Index = 0;
ReservedNames[Index] !=
nullptr; ++Index) {
145 SafeName.insert(0,
"_");
156 std::string SafeName(MightBeInvalidName);
157 std::replace(SafeName.begin(), SafeName.end(),
'-',
'_');
158 std::replace(SafeName.begin(), SafeName.end(),
'.',
'_');
159 if (isdigit(SafeName[0]))
160 SafeName =
"_" + SafeName;
166 llvm::StringRef HeaderFilePath,
169 bool IsProblemFile) {
172 std::string FilePath;
175 llvm::SmallString<256> NativePath, NativePrefix;
176 llvm::sys::path::native(HeaderFilePath, NativePath);
178 if (NativePath.starts_with(NativePrefix))
179 FilePath = std::string(NativePath.substr(NativePrefix.size() + 1));
181 FilePath = std::string(HeaderFilePath);
182 int Count = FileDependents.size();
186 llvm::errs() <<
"warning: " << FilePath
187 <<
" depends on other headers being included first,"
188 " meaning the module.modulemap won't compile."
189 " This header will be omitted from the module map.\n";
193 std::replace(FilePath.begin(), FilePath.end(),
'\\',
'/');
195 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
196 E = llvm::sys::path::end(FilePath);
200 std::string Stem(llvm::sys::path::stem(*I));
203 Module *SubModule = CurrentModule->findSubModule(Stem);
205 SubModule =
new Module(Stem, IsProblemFile);
206 CurrentModule->SubModules.push_back(SubModule);
208 CurrentModule = SubModule;
211 CurrentModule->HeaderFileNames.push_back(FilePath);
217 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
218 llvm::ArrayRef<std::string> ProblemFileNames,
222 auto *
RootModule =
new Module(RootModuleName,
false);
224 llvm::SmallString<256> CurrentDirectory;
225 llvm::sys::fs::current_path(CurrentDirectory);
232 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
233 E = HeaderFileNames.end();
235 std::string Header(*I);
236 bool IsProblemFile =
false;
237 for (
auto &ProblemFile : ProblemFileNames) {
238 if (ProblemFile == Header) {
239 IsProblemFile =
true;
255 llvm::sys::path::remove_filename(HeaderDirectory);
256 llvm::SmallString<256> FilePath;
259 if ((HeaderDirectory.size() == 0) && (
HeaderPrefix.size() != 0)) {
263 llvm::sys::path::native(FilePath);
266 llvm::sys::path::native(FilePath);
271 llvm::ToolOutputFile
Out(FilePath, EC, llvm::sys::fs::OF_TextWithCRLF);
273 llvm::errs() <<
Argv0 <<
": error opening " << FilePath <<
":"
274 << EC.message() <<
"\n";
279 llvm::raw_fd_ostream &
OS =
Out.os();
299 llvm::ArrayRef<std::string> HeaderFileNames,
300 llvm::ArrayRef<std::string> ProblemFileNames,
302 llvm::StringRef RootModuleName) {
306 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
llvm::SmallString< 256U > Name
CompiledFragmentImpl & Out
static cl::opt< std::string > HeaderPrefix("prefix", cl::init(""), cl::desc("Prepend header file paths with this prefix." " If not specified," " the files are considered to be relative to the header list file."))
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
static cl::opt< std::string > ModuleMapPath("module-map-path", cl::init(""), cl::desc("Turn on module map output and specify output path or file name." " If no path is specified and if prefix option is specified," " use prefix for file path."))
Common definitions for Modularize.
llvm::SmallVector< std::string, 4 > DependentsVector
llvm::StringMap< DependentsVector > DependencyMap
static std::string ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName)
static Module * loadModuleDescriptions(llvm::StringRef RootModuleName, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix)
static std::string ensureVaidModuleName(llvm::StringRef MightBeInvalidName)
bool createModuleMap(llvm::StringRef ModuleMapPath, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, llvm::StringRef RootModuleName)
Create the module map file.
static bool writeModuleMap(llvm::StringRef ModuleMapPath, llvm::StringRef HeaderPrefix, Module *RootModule)
static bool addModuleDescription(Module *RootModule, llvm::StringRef HeaderFilePath, llvm::StringRef HeaderPrefix, DependencyMap &Dependencies, bool IsProblemFile)
static const char *const ReservedNames[]
std::vector< llvm::StringRef > CommandLine
llvm::raw_string_ostream OS