33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/ToolOutputFile.h"
48 Module(llvm::StringRef
Name,
bool Problem);
51 bool output(llvm::raw_fd_ostream &OS,
int Indent);
52 Module *findSubModule(llvm::StringRef SubName);
56 std::vector<std::string> HeaderFileNames;
57 std::vector<Module *> SubModules;
66 Module::Module(llvm::StringRef
Name,
bool Problem)
67 : Name(Name), IsProblem(Problem) {}
68 Module::Module() : IsProblem(false) {}
73 while (!SubModules.empty()) {
74 Module *last = SubModules.back();
75 SubModules.pop_back();
81 bool Module::output(llvm::raw_fd_ostream &OS,
int Indent) {
83 if (
Name.size() != 0) {
85 OS <<
"module " <<
Name <<
" {\n";
90 for (
auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
91 if (!(*I)->output(OS, Indent))
96 for (
auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E;
99 if (IsProblem || strstr((*I).c_str(),
".inl"))
100 OS <<
"exclude header \"" << *I <<
"\"\n";
102 OS <<
"header \"" << *I <<
"\"\n";
106 if (HeaderFileNames.size() != 0) {
112 if (
Name.size() != 0) {
122 Module *Module::findSubModule(llvm::StringRef SubName) {
123 for (
auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
124 if ((*I)->Name == SubName)
136 "config_macros",
"export",
"module",
"conflict",
"framework",
137 "requires",
"exclude",
"header",
"private",
"explicit",
138 "link",
"umbrella",
"extern",
"use",
nullptr
145 std::string SafeName = MightBeReservedName;
146 for (
int Index = 0; ReservedNames[Index] !=
nullptr; ++Index) {
147 if (MightBeReservedName == ReservedNames[Index]) {
148 SafeName.insert(0,
"_");
159 std::string SafeName = MightBeInvalidName;
160 std::replace(SafeName.begin(), SafeName.end(),
'-',
'_');
161 std::replace(SafeName.begin(), SafeName.end(),
'.',
'_');
162 if (isdigit(SafeName[0]))
163 SafeName =
"_" + SafeName;
169 llvm::StringRef HeaderFilePath,
172 bool IsProblemFile) {
175 std::string FilePath;
178 llvm::SmallString<256> NativePath, NativePrefix;
179 llvm::sys::path::native(HeaderFilePath, NativePath);
180 llvm::sys::path::native(HeaderPrefix, NativePrefix);
181 if (NativePath.startswith(NativePrefix))
182 FilePath = NativePath.substr(NativePrefix.size() + 1);
184 FilePath = HeaderFilePath;
185 int Count = FileDependents.size();
189 llvm::errs() <<
"warning: " << FilePath
190 <<
" depends on other headers being included first,"
191 " meaning the module.modulemap won't compile."
192 " This header will be omitted from the module map.\n";
196 std::replace(FilePath.begin(), FilePath.end(),
'\\',
'/');
198 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
199 E = llvm::sys::path::end(FilePath);
203 std::string Stem = llvm::sys::path::stem(*I);
206 Module *SubModule = CurrentModule->findSubModule(Stem);
208 SubModule =
new Module(Stem, IsProblemFile);
209 CurrentModule->SubModules.push_back(SubModule);
211 CurrentModule = SubModule;
214 CurrentModule->HeaderFileNames.push_back(FilePath);
220 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
221 llvm::ArrayRef<std::string> ProblemFileNames,
225 auto *
RootModule =
new Module(RootModuleName,
false);
227 llvm::SmallString<256> CurrentDirectory;
228 llvm::sys::fs::current_path(CurrentDirectory);
231 if (HeaderPrefix.size() == 0)
232 HeaderPrefix = CurrentDirectory;
235 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
236 E = HeaderFileNames.end();
238 std::string Header(*I);
239 bool IsProblemFile =
false;
240 for (
auto &ProblemFile : ProblemFileNames) {
241 if (ProblemFile == Header) {
242 IsProblemFile =
true;
257 llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
258 llvm::sys::path::remove_filename(HeaderDirectory);
259 llvm::SmallString<256> FilePath;
262 if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
265 llvm::sys::path::append(FilePath, ModuleMapPath);
266 llvm::sys::path::native(FilePath);
269 llvm::sys::path::native(FilePath);
274 llvm::tool_output_file Out(FilePath, EC, llvm::sys::fs::F_Text);
276 llvm::errs() <<
Argv0 <<
": error opening " << FilePath <<
":"
277 << EC.message() <<
"\n";
282 llvm::raw_fd_ostream &OS = Out.os();
285 OS <<
"// " << ModuleMapPath <<
"\n";
286 OS <<
"// Generated by: " <<
CommandLine <<
"\n\n";
289 if (!RootModule->output(OS, 0))
302 llvm::ArrayRef<std::string> HeaderFileNames,
303 llvm::ArrayRef<std::string> ProblemFileNames,
305 llvm::StringRef RootModuleName) {
309 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
311 if (!RootModule.get())
315 return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
Common definitions for Modularize.
static bool addModuleDescription(Module *RootModule, llvm::StringRef HeaderFilePath, llvm::StringRef HeaderPrefix, DependencyMap &Dependencies, bool IsProblemFile)
llvm::SmallVector< std::string, 4 > DependentsVector
static const char *const ReservedNames[]
static bool writeModuleMap(llvm::StringRef ModuleMapPath, llvm::StringRef HeaderPrefix, Module *RootModule)
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 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)
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."))
llvm::StringMap< DependentsVector > DependencyMap
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 cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
static std::string ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName)