Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "tools/gn/qt_creator_writer.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <sstream> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 | |
| 15 #include "tools/gn/builder.h" | |
| 16 #include "tools/gn/config_values_extractors.h" | |
| 17 #include "tools/gn/deps_iterator.h" | |
| 18 #include "tools/gn/filesystem_utils.h" | |
| 19 #include "tools/gn/label.h" | |
| 20 #include "tools/gn/loader.h" | |
| 21 | |
| 22 namespace { | |
| 23 const char kProjectDirName[] = "qtcreator_project"; | |
| 24 const char kProjectName[] = "all"; | |
| 25 const char kMainProjectFileSuffix[] = ".creator"; | |
| 26 const char kSourcesFileSuffix[] = ".files"; | |
| 27 const char kIncludesFileSuffix[] = ".includes"; | |
| 28 const char kDefinesFileSuffix[] = ".config"; | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 bool QtCreatorWriter::RunAndWriteFile(const BuildSettings* build_settings, | |
| 33 const Builder* builder, | |
| 34 Err* err, | |
| 35 const std::string& root_target) { | |
| 36 base::FilePath project_dir = | |
| 37 build_settings->GetFullPath(build_settings->build_dir()) | |
| 38 .AppendASCII(kProjectDirName); | |
| 39 if (!base::DirectoryExists(project_dir)) { | |
| 40 base::File::Error error; | |
| 41 if (!base::CreateDirectoryAndGetError(project_dir, &error)) { | |
| 42 *err = | |
| 43 Err(Location(), "Could not create the QtCreator project directory '" + | |
| 44 FilePathToUTF8(project_dir) + "': " + | |
| 45 base::File::ErrorToString(error)); | |
| 46 return false; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 base::FilePath project_prefix = project_dir.AppendASCII(kProjectName); | |
| 51 QtCreatorWriter gen(build_settings, builder, project_prefix, root_target); | |
| 52 gen.Run(); | |
| 53 if (gen.err_.has_error()) { | |
| 54 *err = gen.err_; | |
| 55 return false; | |
| 56 } | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 QtCreatorWriter::QtCreatorWriter(const BuildSettings* build_settings, | |
| 61 const Builder* builder, | |
| 62 const base::FilePath& project_prefix, | |
| 63 const std::string& root_target_name) | |
| 64 : build_settings_(build_settings), | |
| 65 builder_(builder), | |
| 66 project_prefix_(project_prefix), | |
| 67 root_target_name_(root_target_name) {} | |
| 68 | |
| 69 QtCreatorWriter::~QtCreatorWriter() {} | |
| 70 | |
| 71 void QtCreatorWriter::CollectDeps(const Target* target) { | |
| 72 for (const auto& dep : target->GetDeps(Target::DEPS_ALL)) { | |
| 73 const Target* dep_target = dep.ptr; | |
| 74 if (targets_.count(dep_target)) | |
| 75 continue; | |
| 76 targets_.insert(dep_target); | |
| 77 CollectDeps(dep_target); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 bool QtCreatorWriter::DiscoverTargets() { | |
| 82 auto all_targets = builder_->GetAllResolvedTargets(); | |
| 83 | |
| 84 if (root_target_name_.empty()) { | |
| 85 targets_ = std::set<const Target*>(all_targets.begin(), all_targets.end()); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 const Target* root_target = nullptr; | |
| 90 for (const Target* target : all_targets) { | |
| 91 if (target->label().name() == root_target_name_) { | |
| 92 root_target = target; | |
| 93 break; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 if (!root_target) { | |
| 98 err_ = Err(Location(), "Target '" + root_target_name_ + "' not found."); | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 targets_.insert(root_target); | |
| 103 CollectDeps(root_target); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void QtCreatorWriter::AddToSources(const Target::FileList& files) { | |
| 108 for (const SourceFile& file : files) { | |
| 109 const std::string& file_path = | |
| 110 FilePathToUTF8(build_settings_->GetFullPath(file)); | |
| 111 sources_.insert(file_path); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void QtCreatorWriter::HandleTarget(const Target* target) { | |
| 116 SourceFile build_file = Loader::BuildFileForLabel(target->label()); | |
| 117 sources_.insert(FilePathToUTF8(build_settings_->GetFullPath(build_file))); | |
| 118 AddToSources(target->settings()->import_manager().GetImportedFiles()); | |
| 119 | |
| 120 AddToSources(target->sources()); | |
| 121 AddToSources(target->public_headers()); | |
| 122 AddToSources(target->inputs()); | |
| 123 | |
| 124 for (ConfigValuesIterator it(target); !it.done(); it.Next()) { | |
| 125 SourceFile precompiled_source = it.cur().precompiled_source(); | |
| 126 if (!precompiled_source.is_null()) { | |
| 127 sources_.insert( | |
| 128 FilePathToUTF8(build_settings_->GetFullPath(precompiled_source))); | |
| 129 } | |
| 130 | |
| 131 for (const SourceDir& include_dir : it.cur().include_dirs()) { | |
| 132 includes_.insert( | |
| 133 FilePathToUTF8(build_settings_->GetFullPath(include_dir))); | |
| 134 } | |
| 135 | |
| 136 for (std::string define : it.cur().defines()) { | |
| 137 size_t equal_pos = define.find('='); | |
| 138 if (equal_pos != std::string::npos) | |
| 139 define[equal_pos] = ' '; | |
| 140 define.insert(0, "#define "); | |
| 141 defines_.insert(define); | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void QtCreatorWriter::GenerateFile(const char* suffix, | |
| 147 std::set<std::string>* items) { | |
|
brucedawson
2016/05/09 18:56:49
Should be const std::set, and perhaps then a refer
Krzysztof Olczyk
2016/05/10 08:11:18
Done.
| |
| 148 const base::FilePath file_path = project_prefix_.AddExtension(suffix); | |
| 149 std::ostringstream output; | |
| 150 for (const std::string& item : *items) | |
| 151 output << item << std::endl; | |
| 152 WriteFileIfChanged(file_path, output.str(), &err_); | |
| 153 } | |
| 154 | |
| 155 void QtCreatorWriter::Run() { | |
| 156 if (!DiscoverTargets()) | |
| 157 return; | |
| 158 | |
| 159 for (const Target* target : targets_) { | |
| 160 if (target->toolchain()->label() != | |
| 161 builder_->loader()->GetDefaultToolchain()) | |
| 162 continue; | |
| 163 HandleTarget(target); | |
| 164 } | |
| 165 | |
| 166 std::set<std::string> empty_list; | |
| 167 | |
| 168 GenerateFile(kMainProjectFileSuffix, &empty_list); | |
| 169 GenerateFile(kSourcesFileSuffix, &sources_); | |
| 170 GenerateFile(kIncludesFileSuffix, &includes_); | |
| 171 GenerateFile(kDefinesFileSuffix, &defines_); | |
| 172 } | |
| OLD | NEW |