1 //===--- ModuleAssistant.cpp - Module map generation manager --*- C++ -*---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the module generation entry point function,
11 // createModuleMap, a Module class for representing a module,
12 // and various implementation functions for doing the underlying
13 // work, described below.
14 //
15 // The "Module" class represents a module, with members for storing the module
16 // name, associated header file names, and sub-modules, and an "output"
17 // function that recursively writes the module definitions.
18 //
19 // The "createModuleMap" function implements the top-level logic of the
20 // assistant mode.  It calls a loadModuleDescriptions function to walk
21 // the header list passed to it and creates a tree of Module objects
22 // representing the module hierarchy, represented by a "Module" object,
23 // the "RootModule".  This root module may or may not represent an actual
24 // module in the module map, depending on the "--root-module" option passed
25 // to modularize.  It then calls a writeModuleMap function to set up the
26 // module map file output and walk the module tree, outputting the module
27 // map file using a stream obtained and managed by an
28 // llvm::ToolOutputFile object.
29 //
30 //===----------------------------------------------------------------------===//
31 
32 #include "Modularize.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/ToolOutputFile.h"
37 #include <vector>
38 
39 // Local definitions:
40 
41 namespace {
42 
43 // Internal class definitions:
44 
45 // Represents a module.
46 class Module {
47 public:
48   Module(llvm::StringRef Name, bool Problem);
49   Module();
50   ~Module();
51   bool output(llvm::raw_fd_ostream &OS, int Indent);
52   Module *findSubModule(llvm::StringRef SubName);
53 
54 public:
55   std::string Name;
56   std::vector<std::string> HeaderFileNames;
57   std::vector<Module *> SubModules;
58   bool IsProblem;
59 };
60 
61 } // end anonymous namespace.
62 
63 // Module functions:
64 
65 // Constructors.
66 Module::Module(llvm::StringRef Name, bool Problem)
67   : Name(Name), IsProblem(Problem) {}
68 Module::Module() : IsProblem(false) {}
69 
70 // Destructor.
71 Module::~Module() {
72   // Free submodules.
73   while (!SubModules.empty()) {
74     Module *last = SubModules.back();
75     SubModules.pop_back();
76     delete last;
77   }
78 }
79 
80 // Write a module hierarchy to the given output stream.
81 bool Module::output(llvm::raw_fd_ostream &OS, int Indent) {
82   // If this is not the nameless root module, start a module definition.
83   if (Name.size() != 0) {
84     OS.indent(Indent);
85     OS << "module " << Name << " {\n";
86     Indent += 2;
87   }
88 
89   // Output submodules.
90   for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
91     if (!(*I)->output(OS, Indent))
92       return false;
93   }
94 
95   // Output header files.
96   for (auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E;
97        ++I) {
98     OS.indent(Indent);
99     if (IsProblem || strstr((*I).c_str(), ".inl"))
100       OS << "exclude header \"" << *I << "\"\n";
101     else
102       OS << "header \"" << *I << "\"\n";
103   }
104 
105   // If this module has header files, output export directive.
106   if (HeaderFileNames.size() != 0) {
107     OS.indent(Indent);
108     OS << "export *\n";
109   }
110 
111   // If this is not the nameless root module, close the module definition.
112   if (Name.size() != 0) {
113     Indent -= 2;
114     OS.indent(Indent);
115     OS << "}\n";
116   }
117 
118   return true;
119 }
120 
121 // Lookup a sub-module.
122 Module *Module::findSubModule(llvm::StringRef SubName) {
123   for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
124     if ((*I)->Name == SubName)
125       return *I;
126   }
127   return nullptr;
128 }
129 
130 // Implementation functions:
131 
132 // Reserved keywords in module.modulemap syntax.
133 // Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp,
134 // such as in ModuleMapParser::consumeToken().
135 static const char *const ReservedNames[] = {
136   "config_macros", "export",   "module", "conflict", "framework",
137   "requires",      "exclude",  "header", "private",  "explicit",
138   "link",          "umbrella", "extern", "use",      nullptr // Flag end.
139 };
140 
141 // Convert module name to a non-keyword.
142 // Prepends a '_' to the name if and only if the name is a keyword.
143 static std::string
144 ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) {
145   std::string SafeName = MightBeReservedName;
146   for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) {
147     if (MightBeReservedName == ReservedNames[Index]) {
148       SafeName.insert(0, "_");
149       break;
150     }
151   }
152   return SafeName;
153 }
154 
155 // Convert module name to a non-keyword.
156 // Prepends a '_' to the name if and only if the name is a keyword.
157 static std::string
158 ensureVaidModuleName(llvm::StringRef MightBeInvalidName) {
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;
164   return SafeName;
165 }
166 
167 // Add one module, given a header file path.
168 static bool addModuleDescription(Module *RootModule,
169                                  llvm::StringRef HeaderFilePath,
170                                  llvm::StringRef HeaderPrefix,
171                                  DependencyMap &Dependencies,
172                                  bool IsProblemFile) {
173   Module *CurrentModule = RootModule;
174   DependentsVector &FileDependents = Dependencies[HeaderFilePath];
175   std::string FilePath;
176   // Strip prefix.
177   // HeaderFilePath should be compared to natively-canonicalized Prefix.
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);
183   else
184     FilePath = HeaderFilePath;
185   int Count = FileDependents.size();
186   // Headers that go into modules must not depend on other files being
187   // included first.  If there are any dependents, warn user and omit.
188   if (Count != 0) {
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";
193     return true;
194   }
195   // Make canonical.
196   std::replace(FilePath.begin(), FilePath.end(), '\\', '/');
197   // Insert module into tree, using subdirectories as submodules.
198   for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
199                                        E = llvm::sys::path::end(FilePath);
200        I != E; ++I) {
201     if ((*I)[0] == '.')
202       continue;
203     std::string Stem = llvm::sys::path::stem(*I);
204     Stem = ensureNoCollisionWithReservedName(Stem);
205     Stem = ensureVaidModuleName(Stem);
206     Module *SubModule = CurrentModule->findSubModule(Stem);
207     if (!SubModule) {
208       SubModule = new Module(Stem, IsProblemFile);
209       CurrentModule->SubModules.push_back(SubModule);
210     }
211     CurrentModule = SubModule;
212   }
213   // Add header file name to headers.
214   CurrentModule->HeaderFileNames.push_back(FilePath);
215   return true;
216 }
217 
218 // Create the internal module tree representation.
219 static Module *loadModuleDescriptions(
220     llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
221     llvm::ArrayRef<std::string> ProblemFileNames,
222     DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
223 
224   // Create root module.
225   auto *RootModule = new Module(RootModuleName, false);
226 
227   llvm::SmallString<256> CurrentDirectory;
228   llvm::sys::fs::current_path(CurrentDirectory);
229 
230   // If no header prefix, use current directory.
231   if (HeaderPrefix.size() == 0)
232     HeaderPrefix = CurrentDirectory;
233 
234   // Walk the header file names and output the module map.
235   for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
236                                              E = HeaderFileNames.end();
237        I != E; ++I) {
238     std::string Header(*I);
239     bool IsProblemFile = false;
240     for (auto &ProblemFile : ProblemFileNames) {
241       if (ProblemFile == Header) {
242         IsProblemFile = true;
243         break;
244       }
245     }
246     // Add as a module.
247     if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
248       return nullptr;
249   }
250 
251   return RootModule;
252 }
253 
254 // Kick off the writing of the module map.
255 static bool writeModuleMap(llvm::StringRef ModuleMapPath,
256                            llvm::StringRef HeaderPrefix, Module *RootModule) {
257   llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
258   llvm::sys::path::remove_filename(HeaderDirectory);
259   llvm::SmallString<256> FilePath;
260 
261   // Get the module map file path to be used.
262   if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
263     FilePath = HeaderPrefix;
264     // Prepend header file name prefix if it's not absolute.
265     llvm::sys::path::append(FilePath, ModuleMapPath);
266     llvm::sys::path::native(FilePath);
267   } else {
268     FilePath = ModuleMapPath;
269     llvm::sys::path::native(FilePath);
270   }
271 
272   // Set up module map output file.
273   std::error_code EC;
274   llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::F_Text);
275   if (EC) {
276     llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
277                  << EC.message() << "\n";
278     return false;
279   }
280 
281   // Get output stream from tool output buffer/manager.
282   llvm::raw_fd_ostream &OS = Out.os();
283 
284   // Output file comment.
285   OS << "// " << ModuleMapPath << "\n";
286   OS << "// Generated by: " << CommandLine << "\n\n";
287 
288   // Write module hierarchy from internal representation.
289   if (!RootModule->output(OS, 0))
290     return false;
291 
292   // Tell ToolOutputFile that we want to keep the file.
293   Out.keep();
294 
295   return true;
296 }
297 
298 // Global functions:
299 
300 // Module map generation entry point.
301 bool createModuleMap(llvm::StringRef ModuleMapPath,
302                      llvm::ArrayRef<std::string> HeaderFileNames,
303                      llvm::ArrayRef<std::string> ProblemFileNames,
304                      DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
305                      llvm::StringRef RootModuleName) {
306   // Load internal representation of modules.
307   std::unique_ptr<Module> RootModule(
308     loadModuleDescriptions(
309       RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
310       HeaderPrefix));
311   if (!RootModule.get())
312     return false;
313 
314   // Write module map file.
315   return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
316 }
317