Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- ModuleAssistant.cpp - Module map generation manager --*- C++ -*---===// |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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 |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 28 | // llvm::ToolOutputFile object. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 29 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 31 | |
| 32 | #include "Modularize.h" |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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: |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 48 | Module(llvm::StringRef Name, bool Problem); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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; |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 58 | bool IsProblem; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // end anonymous namespace. |
| 62 | |
| 63 | // Module functions: |
| 64 | |
| 65 | // Constructors. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 66 | Module::Module(llvm::StringRef Name, bool Problem) |
| 67 | : Name(Name), IsProblem(Problem) {} |
| 68 | Module::Module() : IsProblem(false) {} |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 69 | |
| 70 | // Destructor. |
| 71 | Module::~Module() { |
| 72 | // Free submodules. |
Alexander Kornienko | bf87a8b | 2015-01-22 13:14:29 +0000 | [diff] [blame] | 73 | while (!SubModules.empty()) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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. |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 90 | for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 91 | if (!(*I)->output(OS, Indent)) |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // Output header files. |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 96 | for (auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E; |
| 97 | ++I) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 98 | OS.indent(Indent); |
John Thompson | 1c158c1 | 2016-03-21 23:05:14 +0000 | [diff] [blame] | 99 | if (IsProblem || strstr((*I).c_str(), ".inl")) |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 100 | OS << "exclude header \"" << *I << "\"\n"; |
| 101 | else |
| 102 | OS << "header \"" << *I << "\"\n"; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 123 | for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 124 | if ((*I)->Name == SubName) |
| 125 | return *I; |
| 126 | } |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 127 | return nullptr; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Implementation functions: |
| 131 | |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 132 | // Reserved keywords in module.modulemap syntax. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 133 | // Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp, |
| 134 | // such as in ModuleMapParser::consumeToken(). |
Craig Topper | 45857d4 | 2015-10-18 05:14:41 +0000 | [diff] [blame] | 135 | static const char *const ReservedNames[] = { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 136 | "config_macros", "export", "module", "conflict", "framework", |
| 137 | "requires", "exclude", "header", "private", "explicit", |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 138 | "link", "umbrella", "extern", "use", nullptr // Flag end. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Alp Toker | 58983f1 | 2014-01-09 01:39:49 +0000 | [diff] [blame] | 141 | // Convert module name to a non-keyword. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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; |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 146 | for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 147 | if (MightBeReservedName == ReservedNames[Index]) { |
| 148 | SafeName.insert(0, "_"); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | return SafeName; |
| 153 | } |
| 154 | |
John Thompson | 1c158c1 | 2016-03-21 23:05:14 +0000 | [diff] [blame] | 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 | |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 167 | // Add one module, given a header file path. |
| 168 | static bool addModuleDescription(Module *RootModule, |
| 169 | llvm::StringRef HeaderFilePath, |
| 170 | llvm::StringRef HeaderPrefix, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 171 | DependencyMap &Dependencies, |
| 172 | bool IsProblemFile) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 173 | Module *CurrentModule = RootModule; |
| 174 | DependentsVector &FileDependents = Dependencies[HeaderFilePath]; |
| 175 | std::string FilePath; |
| 176 | // Strip prefix. |
NAKAMURA Takumi | bf5391d | 2013-10-16 01:42:33 +0000 | [diff] [blame] | 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); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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," |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 191 | " meaning the module.modulemap won't compile." |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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); |
John Thompson | 1c158c1 | 2016-03-21 23:05:14 +0000 | [diff] [blame] | 205 | Stem = ensureVaidModuleName(Stem); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 206 | Module *SubModule = CurrentModule->findSubModule(Stem); |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 207 | if (!SubModule) { |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 208 | SubModule = new Module(Stem, IsProblemFile); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 221 | llvm::ArrayRef<std::string> ProblemFileNames, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 222 | DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) { |
| 223 | |
| 224 | // Create root module. |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 225 | auto *RootModule = new Module(RootModuleName, false); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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) { |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 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 | } |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 246 | // Add as a module. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 247 | if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile)) |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 248 | return nullptr; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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. |
Rafael Espindola | b14bd53 | 2014-08-25 18:17:00 +0000 | [diff] [blame] | 273 | std::error_code EC; |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 274 | llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::F_Text); |
Rafael Espindola | b14bd53 | 2014-08-25 18:17:00 +0000 | [diff] [blame] | 275 | if (EC) { |
| 276 | llvm::errs() << Argv0 << ": error opening " << FilePath << ":" |
| 277 | << EC.message() << "\n"; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 292 | // Tell ToolOutputFile that we want to keep the file. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 303 | llvm::ArrayRef<std::string> ProblemFileNames, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 304 | DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, |
| 305 | llvm::StringRef RootModuleName) { |
| 306 | // Load internal representation of modules. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame] | 307 | std::unique_ptr<Module> RootModule( |
| 308 | loadModuleDescriptions( |
| 309 | RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies, |
| 310 | HeaderPrefix)); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 311 | if (!RootModule.get()) |
| 312 | return false; |
| 313 | |
| 314 | // Write module map file. |
| 315 | return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get()); |
| 316 | } |