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 Module(
const Module &other) =
delete;
50 Module &operator=(
const Module &other) =
delete;
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;
66Module::Module(llvm::StringRef
Name,
bool Problem)
72 while (!SubModules.empty()) {
73 Module *last = SubModules.back();
74 SubModules.pop_back();
80bool Module::output(llvm::raw_fd_ostream &
OS,
int Indent) {
82 if (
Name.size() != 0) {
84 OS <<
"module " <<
Name <<
" {\n";
89 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
95 for (
auto I = HeaderFileNames.begin(),
E = HeaderFileNames.end(); I !=
E;
98 if (IsProblem || strstr((*I).c_str(),
".inl"))
99 OS <<
"exclude header \"" << *I <<
"\"\n";
101 OS <<
"header \"" << *I <<
"\"\n";
105 if (HeaderFileNames.size() != 0) {
111 if (
Name.size() != 0) {
121Module *Module::findSubModule(llvm::StringRef SubName) {
122 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
123 if ((*I)->Name == SubName)
135 "config_macros",
"export",
"module",
"conflict",
"framework",
136 "requires",
"exclude",
"header",
"private",
"explicit",
137 "link",
"umbrella",
"extern",
"use",
nullptr
144 std::string SafeName(MightBeReservedName);
145 for (
int Index = 0;
ReservedNames[Index] !=
nullptr; ++Index) {
147 SafeName.insert(0,
"_");
158 std::string SafeName(MightBeInvalidName);
159 std::replace(SafeName.begin(), SafeName.end(),
'-',
'_');
160 std::replace(SafeName.begin(), SafeName.end(),
'.',
'_');
161 if (isdigit(SafeName[0]))
162 SafeName =
"_" + SafeName;
168 llvm::StringRef HeaderFilePath,
171 bool IsProblemFile) {
174 std::string FilePath;
177 llvm::SmallString<256> NativePath, NativePrefix;
178 llvm::sys::path::native(HeaderFilePath, NativePath);
180 if (NativePath.starts_with(NativePrefix))
181 FilePath = std::string(NativePath.substr(NativePrefix.size() + 1));
183 FilePath = std::string(HeaderFilePath);
184 int Count = FileDependents.size();
188 llvm::errs() <<
"warning: " << FilePath
189 <<
" depends on other headers being included first,"
190 " meaning the module.modulemap won't compile."
191 " This header will be omitted from the module map.\n";
195 std::replace(FilePath.begin(), FilePath.end(),
'\\',
'/');
197 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
198 E = llvm::sys::path::end(FilePath);
202 std::string Stem(llvm::sys::path::stem(*I));
205 Module *SubModule = CurrentModule->findSubModule(Stem);
207 SubModule =
new Module(Stem, IsProblemFile);
208 CurrentModule->SubModules.push_back(SubModule);
210 CurrentModule = SubModule;
213 CurrentModule->HeaderFileNames.push_back(FilePath);
219 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
220 llvm::ArrayRef<std::string> ProblemFileNames,
224 auto *
RootModule =
new Module(RootModuleName,
false);
226 llvm::SmallString<256> CurrentDirectory;
227 llvm::sys::fs::current_path(CurrentDirectory);
234 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
235 E = HeaderFileNames.end();
237 std::string Header(*I);
238 bool IsProblemFile =
false;
239 for (
auto &ProblemFile : ProblemFileNames) {
240 if (ProblemFile == Header) {
241 IsProblemFile =
true;
257 llvm::sys::path::remove_filename(HeaderDirectory);
258 llvm::SmallString<256> FilePath;
261 if ((HeaderDirectory.size() == 0) && (
HeaderPrefix.size() != 0)) {
265 llvm::sys::path::native(FilePath);
268 llvm::sys::path::native(FilePath);
273 llvm::ToolOutputFile
Out(FilePath, EC, llvm::sys::fs::OF_TextWithCRLF);
275 llvm::errs() <<
Argv0 <<
": error opening " << FilePath <<
":"
276 << EC.message() <<
"\n";
281 llvm::raw_fd_ostream &
OS =
Out.os();
301 llvm::ArrayRef<std::string> HeaderFileNames,
302 llvm::ArrayRef<std::string> ProblemFileNames,
304 llvm::StringRef RootModuleName) {
308 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