blob: 33dca788de5c68fc4e069184c884bff346b85bd4 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- ModuleAssistant.cpp - Module map generation manager --*- C++ -*---===//
John Thompson5ab4f112013-10-15 13:52:33 +00002//
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//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
John Thompson5ab4f112013-10-15 13:52:33 +00009//
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
Reid Kleckner3fc649c2017-09-23 01:03:17 +000028// llvm::ToolOutputFile object.
John Thompson5ab4f112013-10-15 13:52:33 +000029//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000030//===----------------------------------------------------------------------===//
John Thompson5ab4f112013-10-15 13:52:33 +000031
32#include "Modularize.h"
John Thompson5ab4f112013-10-15 13:52:33 +000033#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
41namespace {
42
43// Internal class definitions:
44
45// Represents a module.
46class Module {
47public:
John Thompson4018c622015-07-10 00:37:25 +000048 Module(llvm::StringRef Name, bool Problem);
John Thompson5ab4f112013-10-15 13:52:33 +000049 Module();
50 ~Module();
51 bool output(llvm::raw_fd_ostream &OS, int Indent);
52 Module *findSubModule(llvm::StringRef SubName);
53
54public:
55 std::string Name;
56 std::vector<std::string> HeaderFileNames;
57 std::vector<Module *> SubModules;
John Thompson4018c622015-07-10 00:37:25 +000058 bool IsProblem;
John Thompson5ab4f112013-10-15 13:52:33 +000059};
60
61} // end anonymous namespace.
62
63// Module functions:
64
65// Constructors.
John Thompson4018c622015-07-10 00:37:25 +000066Module::Module(llvm::StringRef Name, bool Problem)
67 : Name(Name), IsProblem(Problem) {}
68Module::Module() : IsProblem(false) {}
John Thompson5ab4f112013-10-15 13:52:33 +000069
70// Destructor.
71Module::~Module() {
72 // Free submodules.
Alexander Kornienkobf87a8b2015-01-22 13:14:29 +000073 while (!SubModules.empty()) {
John Thompson5ab4f112013-10-15 13:52:33 +000074 Module *last = SubModules.back();
75 SubModules.pop_back();
76 delete last;
77 }
78}
79
80// Write a module hierarchy to the given output stream.
81bool 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.
Piotr Padlewski08124b12016-12-14 15:29:23 +000090 for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
John Thompson5ab4f112013-10-15 13:52:33 +000091 if (!(*I)->output(OS, Indent))
92 return false;
93 }
94
95 // Output header files.
Piotr Padlewski08124b12016-12-14 15:29:23 +000096 for (auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E;
97 ++I) {
John Thompson5ab4f112013-10-15 13:52:33 +000098 OS.indent(Indent);
John Thompson1c158c12016-03-21 23:05:14 +000099 if (IsProblem || strstr((*I).c_str(), ".inl"))
John Thompson4018c622015-07-10 00:37:25 +0000100 OS << "exclude header \"" << *I << "\"\n";
101 else
102 OS << "header \"" << *I << "\"\n";
John Thompson5ab4f112013-10-15 13:52:33 +0000103 }
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.
122Module *Module::findSubModule(llvm::StringRef SubName) {
Piotr Padlewski08124b12016-12-14 15:29:23 +0000123 for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
John Thompson5ab4f112013-10-15 13:52:33 +0000124 if ((*I)->Name == SubName)
125 return *I;
126 }
Craig Topperf61be9c2014-06-09 02:03:06 +0000127 return nullptr;
John Thompson5ab4f112013-10-15 13:52:33 +0000128}
129
130// Implementation functions:
131
John Thompson5d9862f2015-02-10 14:45:30 +0000132// Reserved keywords in module.modulemap syntax.
John Thompson5ab4f112013-10-15 13:52:33 +0000133// Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp,
134// such as in ModuleMapParser::consumeToken().
Craig Topper45857d42015-10-18 05:14:41 +0000135static const char *const ReservedNames[] = {
John Thompson5ab4f112013-10-15 13:52:33 +0000136 "config_macros", "export", "module", "conflict", "framework",
137 "requires", "exclude", "header", "private", "explicit",
Craig Topperf61be9c2014-06-09 02:03:06 +0000138 "link", "umbrella", "extern", "use", nullptr // Flag end.
John Thompson5ab4f112013-10-15 13:52:33 +0000139};
140
Alp Toker58983f12014-01-09 01:39:49 +0000141// Convert module name to a non-keyword.
John Thompson5ab4f112013-10-15 13:52:33 +0000142// Prepends a '_' to the name if and only if the name is a keyword.
143static std::string
144ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) {
145 std::string SafeName = MightBeReservedName;
Craig Topperf61be9c2014-06-09 02:03:06 +0000146 for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) {
John Thompson5ab4f112013-10-15 13:52:33 +0000147 if (MightBeReservedName == ReservedNames[Index]) {
148 SafeName.insert(0, "_");
149 break;
150 }
151 }
152 return SafeName;
153}
154
John Thompson1c158c12016-03-21 23:05:14 +0000155// Convert module name to a non-keyword.
156// Prepends a '_' to the name if and only if the name is a keyword.
157static std::string
158ensureVaidModuleName(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
John Thompson5ab4f112013-10-15 13:52:33 +0000167// Add one module, given a header file path.
168static bool addModuleDescription(Module *RootModule,
169 llvm::StringRef HeaderFilePath,
170 llvm::StringRef HeaderPrefix,
John Thompson4018c622015-07-10 00:37:25 +0000171 DependencyMap &Dependencies,
172 bool IsProblemFile) {
John Thompson5ab4f112013-10-15 13:52:33 +0000173 Module *CurrentModule = RootModule;
174 DependentsVector &FileDependents = Dependencies[HeaderFilePath];
175 std::string FilePath;
176 // Strip prefix.
NAKAMURA Takumibf5391d2013-10-16 01:42:33 +0000177 // 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);
John Thompson5ab4f112013-10-15 13:52:33 +0000183 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,"
John Thompson5d9862f2015-02-10 14:45:30 +0000191 " meaning the module.modulemap won't compile."
John Thompson5ab4f112013-10-15 13:52:33 +0000192 " 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);
John Thompson1c158c12016-03-21 23:05:14 +0000205 Stem = ensureVaidModuleName(Stem);
John Thompson5ab4f112013-10-15 13:52:33 +0000206 Module *SubModule = CurrentModule->findSubModule(Stem);
Craig Topperf61be9c2014-06-09 02:03:06 +0000207 if (!SubModule) {
John Thompson4018c622015-07-10 00:37:25 +0000208 SubModule = new Module(Stem, IsProblemFile);
John Thompson5ab4f112013-10-15 13:52:33 +0000209 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.
219static Module *loadModuleDescriptions(
220 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
John Thompson4018c622015-07-10 00:37:25 +0000221 llvm::ArrayRef<std::string> ProblemFileNames,
John Thompson5ab4f112013-10-15 13:52:33 +0000222 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
223
224 // Create root module.
Piotr Padlewski08124b12016-12-14 15:29:23 +0000225 auto *RootModule = new Module(RootModuleName, false);
John Thompson5ab4f112013-10-15 13:52:33 +0000226
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) {
John Thompson4018c622015-07-10 00:37:25 +0000238 std::string Header(*I);
239 bool IsProblemFile = false;
240 for (auto &ProblemFile : ProblemFileNames) {
241 if (ProblemFile == Header) {
242 IsProblemFile = true;
243 break;
244 }
245 }
John Thompson5ab4f112013-10-15 13:52:33 +0000246 // Add as a module.
John Thompson4018c622015-07-10 00:37:25 +0000247 if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
Craig Topperf61be9c2014-06-09 02:03:06 +0000248 return nullptr;
John Thompson5ab4f112013-10-15 13:52:33 +0000249 }
250
251 return RootModule;
252}
253
254// Kick off the writing of the module map.
255static 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.
Rafael Espindolab14bd532014-08-25 18:17:00 +0000273 std::error_code EC;
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000274 llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::F_Text);
Rafael Espindolab14bd532014-08-25 18:17:00 +0000275 if (EC) {
276 llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
277 << EC.message() << "\n";
John Thompson5ab4f112013-10-15 13:52:33 +0000278 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
Reid Kleckner3fc649c2017-09-23 01:03:17 +0000292 // Tell ToolOutputFile that we want to keep the file.
John Thompson5ab4f112013-10-15 13:52:33 +0000293 Out.keep();
294
295 return true;
296}
297
298// Global functions:
299
300// Module map generation entry point.
301bool createModuleMap(llvm::StringRef ModuleMapPath,
302 llvm::ArrayRef<std::string> HeaderFileNames,
John Thompson4018c622015-07-10 00:37:25 +0000303 llvm::ArrayRef<std::string> ProblemFileNames,
John Thompson5ab4f112013-10-15 13:52:33 +0000304 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
305 llvm::StringRef RootModuleName) {
306 // Load internal representation of modules.
John Thompson4018c622015-07-10 00:37:25 +0000307 std::unique_ptr<Module> RootModule(
308 loadModuleDescriptions(
309 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
310 HeaderPrefix));
John Thompson5ab4f112013-10-15 13:52:33 +0000311 if (!RootModule.get())
312 return false;
313
314 // Write module map file.
315 return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
316}