Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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/eclipse_writer.h" | |
| 6 | |
| 7 #include <fstream> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "tools/gn/builder.h" | |
| 12 #include "tools/gn/config_values_extractors.h" | |
| 13 #include "tools/gn/filesystem_utils.h" | |
| 14 #include "tools/gn/loader.h" | |
| 15 #include "tools/gn/xml_element_writer.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Escapes |unescaped| for use in XML element content. | |
| 20 std::string EscapeForXML(const std::string& unescaped) { | |
| 21 std::string result; | |
| 22 result.reserve(unescaped.length()); | |
| 23 for (const char c : unescaped) { | |
| 24 if (c == '<') | |
| 25 result += "<"; | |
| 26 else if (c == '>') | |
| 27 result += ">"; | |
| 28 else if (c == '&') | |
| 29 result += "&"; | |
| 30 else | |
| 31 result.push_back(c); | |
| 32 } | |
| 33 return result; | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 EclipseWriter::EclipseWriter(const BuildSettings* build_settings, | |
| 39 const Builder* builder, | |
| 40 std::ostream& out) | |
| 41 : build_settings_(build_settings), builder_(builder), out_(out) { | |
| 42 languages_.push_back("C++ Source File"); | |
| 43 languages_.push_back("C Source File"); | |
| 44 languages_.push_back("Assembly Source File"); | |
| 45 languages_.push_back("GNU C++"); | |
| 46 languages_.push_back("GNU C"); | |
| 47 languages_.push_back("Assembly"); | |
| 48 | |
|
brettw
2016/02/17 18:47:00
Nit: extra blank.
| |
| 49 } | |
| 50 | |
| 51 EclipseWriter::~EclipseWriter() {} | |
| 52 | |
| 53 // static | |
| 54 bool EclipseWriter::RunAndWriteFile( | |
| 55 const BuildSettings* build_settings, | |
| 56 const Builder* builder, | |
| 57 Err* err) { | |
| 58 base::FilePath file = build_settings->GetFullPath(build_settings->build_dir()) | |
| 59 .AppendASCII("eclipse-cdt-settings.xml"); | |
| 60 std::ofstream file_out; | |
| 61 file_out.open(FilePathToUTF8(file).c_str(), | |
| 62 std::ios_base::out | std::ios_base::binary); | |
| 63 if (file_out.fail()) { | |
| 64 *err = | |
| 65 Err(Location(), "Couldn't open eclipse-cdt-settings.xml for writing"); | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 EclipseWriter gen(build_settings, builder, file_out); | |
| 70 gen.Run(); | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 void EclipseWriter::Run() { | |
| 75 GetAllIncludeDirs(); | |
| 76 GetAllDefines(); | |
| 77 WriteCDTSettings(); | |
| 78 } | |
| 79 | |
| 80 void EclipseWriter::GetAllIncludeDirs() { | |
| 81 std::vector<const Target*> targets = builder_->GetAllResolvedTargets(); | |
| 82 for (const Target* target : targets) { | |
| 83 if (!UsesDefaultToolchain(target)) | |
| 84 continue; | |
| 85 | |
| 86 for (ConfigValuesIterator it(target); !it.done(); it.Next()) { | |
| 87 for (const SourceDir& include_dir : it.cur().include_dirs()) { | |
| 88 include_dirs_.insert( | |
| 89 FilePathToUTF8(build_settings_->GetFullPath(include_dir))); | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void EclipseWriter::GetAllDefines() { | |
| 96 std::vector<const Target*> targets = builder_->GetAllResolvedTargets(); | |
| 97 for (const Target* target : targets) { | |
| 98 if (!UsesDefaultToolchain(target)) | |
| 99 continue; | |
| 100 | |
| 101 for (ConfigValuesIterator it(target); !it.done(); it.Next()) { | |
| 102 for (const std::string& define : it.cur().defines()) { | |
| 103 size_t equal_pos = define.find('='); | |
| 104 std::string define_key; | |
| 105 std::string define_value; | |
| 106 if (equal_pos == std::string::npos) { | |
|
brettw
2016/02/17 18:47:00
-DFOO
and
-DFOO=1
mean different things. I think
| |
| 107 define_key = define; | |
| 108 define_value = "1"; | |
| 109 } else { | |
| 110 define_key = define.substr(0, equal_pos); | |
| 111 define_value = define.substr(equal_pos + 1); | |
| 112 } | |
| 113 defines_[define_key] = define_value; | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool EclipseWriter::UsesDefaultToolchain(const Target* target) const { | |
| 120 return target->toolchain()->label() == | |
| 121 builder_->loader()->GetDefaultToolchain(); | |
| 122 } | |
| 123 | |
| 124 void EclipseWriter::WriteCDTSettings() { | |
| 125 out_ << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" << std::endl; | |
| 126 XmlElementWriter cdt_properties_element(out_, "cdtprojectproperties", | |
| 127 XmlAttributes()); | |
| 128 | |
| 129 { | |
| 130 const char* kIncludesSectionName = | |
| 131 "org.eclipse.cdt.internal.ui.wizards.settingswizards.IncludePaths"; | |
| 132 scoped_ptr<XmlElementWriter> section_element = | |
| 133 cdt_properties_element.SubElement( | |
| 134 "section", XmlAttributes("name", kIncludesSectionName)); | |
| 135 | |
| 136 section_element->SubElement( | |
| 137 "language", XmlAttributes("name", "holder for library settings")); | |
| 138 | |
| 139 for (const std::string& language : languages_) { | |
| 140 scoped_ptr<XmlElementWriter> language_element = | |
| 141 section_element->SubElement("language", | |
| 142 XmlAttributes("name", language)); | |
| 143 for (const std::string& include_dir : include_dirs_) { | |
| 144 language_element | |
| 145 ->SubElement("includepath", | |
| 146 XmlAttributes("workspace_path", "false")) | |
| 147 ->Text(EscapeForXML(include_dir)); | |
| 148 } | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 { | |
| 153 const char* kMacrosSectionName = | |
| 154 "org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros"; | |
| 155 scoped_ptr<XmlElementWriter> section_element = | |
| 156 cdt_properties_element.SubElement( | |
| 157 "section", XmlAttributes("name", kMacrosSectionName)); | |
| 158 | |
| 159 section_element->SubElement( | |
| 160 "language", XmlAttributes("name", "holder for library settings")); | |
| 161 | |
| 162 for (const std::string& language : languages_) { | |
| 163 scoped_ptr<XmlElementWriter> language_element = | |
| 164 section_element->SubElement("language", | |
| 165 XmlAttributes("name", language)); | |
| 166 for (auto& key_val : defines_) { | |
| 167 scoped_ptr<XmlElementWriter> macro_element = | |
| 168 language_element->SubElement("macro"); | |
| 169 macro_element->SubElement("name")->Text(EscapeForXML(key_val.first)); | |
| 170 macro_element->SubElement("value")->Text(EscapeForXML(key_val.second)); | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 } | |
| OLD | NEW |