summaryrefslogtreecommitdiffstats
path: root/tools/gn/eclipse_writer.cc
blob: e38e24711daca5498eef1e943185c3996bfad2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/eclipse_writer.h"

#include <fstream>

#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "tools/gn/builder.h"
#include "tools/gn/config_values_extractors.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/loader.h"
#include "tools/gn/xml_element_writer.h"

namespace {

// Escapes |unescaped| for use in XML element content.
std::string EscapeForXML(const std::string& unescaped) {
  std::string result;
  result.reserve(unescaped.length());
  for (const char c : unescaped) {
    if (c == '<')
      result += "&lt;";
    else if (c == '>')
      result += "&gt;";
    else if (c == '&')
      result += "&amp;";
    else
      result.push_back(c);
  }
  return result;
}

}  // namespace

EclipseWriter::EclipseWriter(const BuildSettings* build_settings,
                             const Builder* builder,
                             std::ostream& out)
    : build_settings_(build_settings), builder_(builder), out_(out) {
  languages_.push_back("C++ Source File");
  languages_.push_back("C Source File");
  languages_.push_back("Assembly Source File");
  languages_.push_back("GNU C++");
  languages_.push_back("GNU C");
  languages_.push_back("Assembly");
}

EclipseWriter::~EclipseWriter() {}

// static
bool EclipseWriter::RunAndWriteFile(
    const BuildSettings* build_settings,
    const Builder* builder,
    Err* err) {
  base::FilePath file = build_settings->GetFullPath(build_settings->build_dir())
                            .AppendASCII("eclipse-cdt-settings.xml");
  std::ofstream file_out;
  file_out.open(FilePathToUTF8(file).c_str(),
                std::ios_base::out | std::ios_base::binary);
  if (file_out.fail()) {
    *err =
        Err(Location(), "Couldn't open eclipse-cdt-settings.xml for writing");
    return false;
  }

  EclipseWriter gen(build_settings, builder, file_out);
  gen.Run();
  return true;
}

void EclipseWriter::Run() {
  GetAllIncludeDirs();
  GetAllDefines();
  WriteCDTSettings();
}

void EclipseWriter::GetAllIncludeDirs() {
  std::vector<const Target*> targets = builder_->GetAllResolvedTargets();
  for (const Target* target : targets) {
    if (!UsesDefaultToolchain(target))
      continue;

    for (ConfigValuesIterator it(target); !it.done(); it.Next()) {
      for (const SourceDir& include_dir : it.cur().include_dirs()) {
        include_dirs_.insert(
            FilePathToUTF8(build_settings_->GetFullPath(include_dir)));
      }
    }
  }
}

void EclipseWriter::GetAllDefines() {
  std::vector<const Target*> targets = builder_->GetAllResolvedTargets();
  for (const Target* target : targets) {
    if (!UsesDefaultToolchain(target))
      continue;

    for (ConfigValuesIterator it(target); !it.done(); it.Next()) {
      for (const std::string& define : it.cur().defines()) {
        size_t equal_pos = define.find('=');
        std::string define_key;
        std::string define_value;
        if (equal_pos == std::string::npos) {
          define_key = define;
        } else {
          define_key = define.substr(0, equal_pos);
          define_value = define.substr(equal_pos + 1);
        }
        defines_[define_key] = define_value;
      }
    }
  }
}

bool EclipseWriter::UsesDefaultToolchain(const Target* target) const {
  return target->toolchain()->label() ==
         builder_->loader()->GetDefaultToolchain();
}

void EclipseWriter::WriteCDTSettings() {
  out_ << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
  XmlElementWriter cdt_properties_element(out_, "cdtprojectproperties",
                                          XmlAttributes());

  {
    const char* kIncludesSectionName =
        "org.eclipse.cdt.internal.ui.wizards.settingswizards.IncludePaths";
    scoped_ptr<XmlElementWriter> section_element =
        cdt_properties_element.SubElement(
            "section", XmlAttributes("name", kIncludesSectionName));

    section_element->SubElement(
        "language", XmlAttributes("name", "holder for library settings"));

    for (const std::string& language : languages_) {
      scoped_ptr<XmlElementWriter> language_element =
          section_element->SubElement("language",
                                      XmlAttributes("name", language));
      for (const std::string& include_dir : include_dirs_) {
        language_element
            ->SubElement("includepath",
                         XmlAttributes("workspace_path", "false"))
            ->Text(EscapeForXML(include_dir));
      }
    }
  }

  {
    const char* kMacrosSectionName =
        "org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros";
    scoped_ptr<XmlElementWriter> section_element =
        cdt_properties_element.SubElement(
            "section", XmlAttributes("name", kMacrosSectionName));

    section_element->SubElement(
        "language", XmlAttributes("name", "holder for library settings"));

    for (const std::string& language : languages_) {
      scoped_ptr<XmlElementWriter> language_element =
          section_element->SubElement("language",
                                      XmlAttributes("name", language));
      for (const auto& key_val : defines_) {
        scoped_ptr<XmlElementWriter> macro_element =
            language_element->SubElement("macro");
        macro_element->SubElement("name")->Text(EscapeForXML(key_val.first));
        macro_element->SubElement("value")->Text(EscapeForXML(key_val.second));
      }
    }
  }
}