John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 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::tool_output_file object. |
| 29 | // |
| 30 | //===---------------------------------------------------------------------===// |
| 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. |
| 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 Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 102 | if (IsProblem) |
| 103 | OS << "exclude header \"" << *I << "\"\n"; |
| 104 | else |
| 105 | OS << "header \"" << *I << "\"\n"; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 106 | } |
| 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. |
| 125 | Module *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 Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 132 | return nullptr; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Implementation functions: |
| 136 | |
John Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 137 | // Reserved keywords in module.modulemap syntax. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 138 | // Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp, |
| 139 | // such as in ModuleMapParser::consumeToken(). |
| 140 | static const char *ReservedNames[] = { |
| 141 | "config_macros", "export", "module", "conflict", "framework", |
| 142 | "requires", "exclude", "header", "private", "explicit", |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 143 | "link", "umbrella", "extern", "use", nullptr // Flag end. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Alp Toker | 58983f1 | 2014-01-09 01:39:49 +0000 | [diff] [blame] | 146 | // Convert module name to a non-keyword. |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 147 | // Prepends a '_' to the name if and only if the name is a keyword. |
| 148 | static std::string |
| 149 | ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) { |
| 150 | std::string SafeName = MightBeReservedName; |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 151 | for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 152 | 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. |
| 161 | static bool addModuleDescription(Module *RootModule, |
| 162 | llvm::StringRef HeaderFilePath, |
| 163 | llvm::StringRef HeaderPrefix, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 164 | DependencyMap &Dependencies, |
| 165 | bool IsProblemFile) { |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 166 | Module *CurrentModule = RootModule; |
| 167 | DependentsVector &FileDependents = Dependencies[HeaderFilePath]; |
| 168 | std::string FilePath; |
| 169 | // Strip prefix. |
NAKAMURA Takumi | bf5391d | 2013-10-16 01:42:33 +0000 | [diff] [blame] | 170 | // 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 Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 176 | 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 Thompson | 5d9862f | 2015-02-10 14:45:30 +0000 | [diff] [blame] | 184 | " meaning the module.modulemap won't compile." |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 185 | " 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 Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 199 | if (!SubModule) { |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 200 | SubModule = new Module(Stem, IsProblemFile); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 201 | 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. |
| 211 | static Module *loadModuleDescriptions( |
| 212 | llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 213 | llvm::ArrayRef<std::string> ProblemFileNames, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 214 | DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) { |
| 215 | |
| 216 | // Create root module. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 217 | Module *RootModule = new Module(RootModuleName, false); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 218 | |
| 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 Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 230 | 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 Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 238 | // Add as a module. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 239 | if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile)) |
Craig Topper | f61be9c | 2014-06-09 02:03:06 +0000 | [diff] [blame] | 240 | return nullptr; |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return RootModule; |
| 244 | } |
| 245 | |
| 246 | // Kick off the writing of the module map. |
| 247 | static 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 Espindola | b14bd53 | 2014-08-25 18:17:00 +0000 | [diff] [blame] | 265 | 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 Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 270 | 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. |
| 293 | bool createModuleMap(llvm::StringRef ModuleMapPath, |
| 294 | llvm::ArrayRef<std::string> HeaderFileNames, |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 295 | llvm::ArrayRef<std::string> ProblemFileNames, |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 296 | DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, |
| 297 | llvm::StringRef RootModuleName) { |
| 298 | // Load internal representation of modules. |
John Thompson | 4018c62 | 2015-07-10 00:37:25 +0000 | [diff] [blame^] | 299 | std::unique_ptr<Module> RootModule( |
| 300 | loadModuleDescriptions( |
| 301 | RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies, |
| 302 | HeaderPrefix)); |
John Thompson | 5ab4f11 | 2013-10-15 13:52:33 +0000 | [diff] [blame] | 303 | if (!RootModule.get()) |
| 304 | return false; |
| 305 | |
| 306 | // Write module map file. |
| 307 | return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get()); |
| 308 | } |