blob: f982f17e8af8643f169264d3aade7c6c260c8315 [file] [log] [blame]
John Thompson5ab4f112013-10-15 13:52:33 +00001//===--- 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::tool_output_file object.
29//
30//===---------------------------------------------------------------------===//
31
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.
90 for (std::vector<Module *>::iterator I = SubModules.begin(),
91 E = SubModules.end();
92 I != E; ++I) {
93 if (!(*I)->output(OS, Indent))
94 return false;
95 }
96
97 // Output header files.
98 for (std::vector<std::string>::iterator I = HeaderFileNames.begin(),
99 E = HeaderFileNames.end();
100 I != E; ++I) {
101 OS.indent(Indent);
John Thompson4018c622015-07-10 00:37:25 +0000102 if (IsProblem)
103 OS << "exclude header \"" << *I << "\"\n";
104 else
105 OS << "header \"" << *I << "\"\n";
John Thompson5ab4f112013-10-15 13:52:33 +0000106 }
107
108 // If this module has header files, output export directive.
109 if (HeaderFileNames.size() != 0) {
110 OS.indent(Indent);
111 OS << "export *\n";
112 }
113
114 // If this is not the nameless root module, close the module definition.
115 if (Name.size() != 0) {
116 Indent -= 2;
117 OS.indent(Indent);
118 OS << "}\n";
119 }
120
121 return true;
122}
123
124// Lookup a sub-module.
125Module *Module::findSubModule(llvm::StringRef SubName) {
126 for (std::vector<Module *>::iterator I = SubModules.begin(),
127 E = SubModules.end();
128 I != E; ++I) {
129 if ((*I)->Name == SubName)
130 return *I;
131 }
Craig Topperf61be9c2014-06-09 02:03:06 +0000132 return nullptr;
John Thompson5ab4f112013-10-15 13:52:33 +0000133}
134
135// Implementation functions:
136
John Thompson5d9862f2015-02-10 14:45:30 +0000137// Reserved keywords in module.modulemap syntax.
John Thompson5ab4f112013-10-15 13:52:33 +0000138// Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp,
139// such as in ModuleMapParser::consumeToken().
140static const char *ReservedNames[] = {
141 "config_macros", "export", "module", "conflict", "framework",
142 "requires", "exclude", "header", "private", "explicit",
Craig Topperf61be9c2014-06-09 02:03:06 +0000143 "link", "umbrella", "extern", "use", nullptr // Flag end.
John Thompson5ab4f112013-10-15 13:52:33 +0000144};
145
Alp Toker58983f12014-01-09 01:39:49 +0000146// Convert module name to a non-keyword.
John Thompson5ab4f112013-10-15 13:52:33 +0000147// Prepends a '_' to the name if and only if the name is a keyword.
148static std::string
149ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) {
150 std::string SafeName = MightBeReservedName;
Craig Topperf61be9c2014-06-09 02:03:06 +0000151 for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) {
John Thompson5ab4f112013-10-15 13:52:33 +0000152 if (MightBeReservedName == ReservedNames[Index]) {
153 SafeName.insert(0, "_");
154 break;
155 }
156 }
157 return SafeName;
158}
159
160// Add one module, given a header file path.
161static bool addModuleDescription(Module *RootModule,
162 llvm::StringRef HeaderFilePath,
163 llvm::StringRef HeaderPrefix,
John Thompson4018c622015-07-10 00:37:25 +0000164 DependencyMap &Dependencies,
165 bool IsProblemFile) {
John Thompson5ab4f112013-10-15 13:52:33 +0000166 Module *CurrentModule = RootModule;
167 DependentsVector &FileDependents = Dependencies[HeaderFilePath];
168 std::string FilePath;
169 // Strip prefix.
NAKAMURA Takumibf5391d2013-10-16 01:42:33 +0000170 // HeaderFilePath should be compared to natively-canonicalized Prefix.
171 llvm::SmallString<256> NativePath, NativePrefix;
172 llvm::sys::path::native(HeaderFilePath, NativePath);
173 llvm::sys::path::native(HeaderPrefix, NativePrefix);
174 if (NativePath.startswith(NativePrefix))
175 FilePath = NativePath.substr(NativePrefix.size() + 1);
John Thompson5ab4f112013-10-15 13:52:33 +0000176 else
177 FilePath = HeaderFilePath;
178 int Count = FileDependents.size();
179 // Headers that go into modules must not depend on other files being
180 // included first. If there are any dependents, warn user and omit.
181 if (Count != 0) {
182 llvm::errs() << "warning: " << FilePath
183 << " depends on other headers being included first,"
John Thompson5d9862f2015-02-10 14:45:30 +0000184 " meaning the module.modulemap won't compile."
John Thompson5ab4f112013-10-15 13:52:33 +0000185 " This header will be omitted from the module map.\n";
186 return true;
187 }
188 // Make canonical.
189 std::replace(FilePath.begin(), FilePath.end(), '\\', '/');
190 // Insert module into tree, using subdirectories as submodules.
191 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
192 E = llvm::sys::path::end(FilePath);
193 I != E; ++I) {
194 if ((*I)[0] == '.')
195 continue;
196 std::string Stem = llvm::sys::path::stem(*I);
197 Stem = ensureNoCollisionWithReservedName(Stem);
198 Module *SubModule = CurrentModule->findSubModule(Stem);
Craig Topperf61be9c2014-06-09 02:03:06 +0000199 if (!SubModule) {
John Thompson4018c622015-07-10 00:37:25 +0000200 SubModule = new Module(Stem, IsProblemFile);
John Thompson5ab4f112013-10-15 13:52:33 +0000201 CurrentModule->SubModules.push_back(SubModule);
202 }
203 CurrentModule = SubModule;
204 }
205 // Add header file name to headers.
206 CurrentModule->HeaderFileNames.push_back(FilePath);
207 return true;
208}
209
210// Create the internal module tree representation.
211static Module *loadModuleDescriptions(
212 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
John Thompson4018c622015-07-10 00:37:25 +0000213 llvm::ArrayRef<std::string> ProblemFileNames,
John Thompson5ab4f112013-10-15 13:52:33 +0000214 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
215
216 // Create root module.
John Thompson4018c622015-07-10 00:37:25 +0000217 Module *RootModule = new Module(RootModuleName, false);
John Thompson5ab4f112013-10-15 13:52:33 +0000218
219 llvm::SmallString<256> CurrentDirectory;
220 llvm::sys::fs::current_path(CurrentDirectory);
221
222 // If no header prefix, use current directory.
223 if (HeaderPrefix.size() == 0)
224 HeaderPrefix = CurrentDirectory;
225
226 // Walk the header file names and output the module map.
227 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
228 E = HeaderFileNames.end();
229 I != E; ++I) {
John Thompson4018c622015-07-10 00:37:25 +0000230 std::string Header(*I);
231 bool IsProblemFile = false;
232 for (auto &ProblemFile : ProblemFileNames) {
233 if (ProblemFile == Header) {
234 IsProblemFile = true;
235 break;
236 }
237 }
John Thompson5ab4f112013-10-15 13:52:33 +0000238 // Add as a module.
John Thompson4018c622015-07-10 00:37:25 +0000239 if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
Craig Topperf61be9c2014-06-09 02:03:06 +0000240 return nullptr;
John Thompson5ab4f112013-10-15 13:52:33 +0000241 }
242
243 return RootModule;
244}
245
246// Kick off the writing of the module map.
247static bool writeModuleMap(llvm::StringRef ModuleMapPath,
248 llvm::StringRef HeaderPrefix, Module *RootModule) {
249 llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
250 llvm::sys::path::remove_filename(HeaderDirectory);
251 llvm::SmallString<256> FilePath;
252
253 // Get the module map file path to be used.
254 if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
255 FilePath = HeaderPrefix;
256 // Prepend header file name prefix if it's not absolute.
257 llvm::sys::path::append(FilePath, ModuleMapPath);
258 llvm::sys::path::native(FilePath);
259 } else {
260 FilePath = ModuleMapPath;
261 llvm::sys::path::native(FilePath);
262 }
263
264 // Set up module map output file.
Rafael Espindolab14bd532014-08-25 18:17:00 +0000265 std::error_code EC;
266 llvm::tool_output_file Out(FilePath, EC, llvm::sys::fs::F_Text);
267 if (EC) {
268 llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
269 << EC.message() << "\n";
John Thompson5ab4f112013-10-15 13:52:33 +0000270 return false;
271 }
272
273 // Get output stream from tool output buffer/manager.
274 llvm::raw_fd_ostream &OS = Out.os();
275
276 // Output file comment.
277 OS << "// " << ModuleMapPath << "\n";
278 OS << "// Generated by: " << CommandLine << "\n\n";
279
280 // Write module hierarchy from internal representation.
281 if (!RootModule->output(OS, 0))
282 return false;
283
284 // Tell tool_output_file that we want to keep the file.
285 Out.keep();
286
287 return true;
288}
289
290// Global functions:
291
292// Module map generation entry point.
293bool createModuleMap(llvm::StringRef ModuleMapPath,
294 llvm::ArrayRef<std::string> HeaderFileNames,
John Thompson4018c622015-07-10 00:37:25 +0000295 llvm::ArrayRef<std::string> ProblemFileNames,
John Thompson5ab4f112013-10-15 13:52:33 +0000296 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
297 llvm::StringRef RootModuleName) {
298 // Load internal representation of modules.
John Thompson4018c622015-07-10 00:37:25 +0000299 std::unique_ptr<Module> RootModule(
300 loadModuleDescriptions(
301 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
302 HeaderPrefix));
John Thompson5ab4f112013-10-15 13:52:33 +0000303 if (!RootModule.get())
304 return false;
305
306 // Write module map file.
307 return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
308}