Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExtraEclipseCDT4Generator.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmExtraEclipseCDT4Generator.h"
4
5
#include <algorithm>
6
#include <cassert>
7
#include <cstdio>
8
#include <map>
9
#include <memory>
10
#include <sstream>
11
#include <utility>
12
13
#include <cmext/string_view>
14
15
#include "cmsys/RegularExpression.hxx"
16
17
#include "cmGeneratedFileStream.h"
18
#include "cmGeneratorExpression.h"
19
#include "cmGeneratorTarget.h"
20
#include "cmGlobalGenerator.h"
21
#include "cmList.h"
22
#include "cmLocalGenerator.h"
23
#include "cmMakefile.h"
24
#include "cmMessageType.h"
25
#include "cmSourceFile.h"
26
#include "cmSourceGroup.h"
27
#include "cmState.h"
28
#include "cmStateTypes.h"
29
#include "cmStringAlgorithms.h"
30
#include "cmSystemTools.h"
31
#include "cmValue.h"
32
#include "cmXMLWriter.h"
33
#include "cmake.h"
34
35
static void AppendAttribute(cmXMLWriter& xml, char const* keyval)
36
0
{
37
0
  xml.StartElement("attribute");
38
0
  xml.Attribute("key", keyval);
39
0
  xml.Attribute("value", keyval);
40
0
  xml.EndElement();
41
0
}
42
43
template <typename T>
44
void AppendDictionary(cmXMLWriter& xml, char const* key, T const& value)
45
0
{
46
0
  xml.StartElement("dictionary");
47
0
  xml.Element("key", key);
48
0
  xml.Element("value", value);
49
0
  xml.EndElement();
50
0
}
Unexecuted instantiation: void AppendDictionary<char [6]>(cmXMLWriter&, char const*, char const (&) [6])
Unexecuted instantiation: void AppendDictionary<char [5]>(cmXMLWriter&, char const*, char const (&) [5])
Unexecuted instantiation: void AppendDictionary<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(cmXMLWriter&, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: void AppendDictionary<char [47]>(cmXMLWriter&, char const*, char const (&) [47])
Unexecuted instantiation: void AppendDictionary<char [4]>(cmXMLWriter&, char const*, char const (&) [4])
Unexecuted instantiation: void AppendDictionary<char [1]>(cmXMLWriter&, char const*, char const (&) [1])
51
52
cmExtraEclipseCDT4Generator::cmExtraEclipseCDT4Generator()
53
0
{
54
0
  this->IsOutOfSourceBuild = false;
55
0
  this->GenerateSourceProject = false;
56
0
  this->SupportsVirtualFolders = true;
57
0
  this->GenerateLinkedResources = true;
58
0
  this->SupportsGmakeErrorParser = true;
59
0
  this->SupportsMachO64Parser = true;
60
0
  this->CEnabled = false;
61
0
  this->CXXEnabled = false;
62
0
}
63
64
cmExternalMakefileProjectGeneratorFactory*
65
cmExtraEclipseCDT4Generator::GetFactory()
66
35
{
67
35
  static cmExternalMakefileProjectGeneratorSimpleFactory<
68
35
    cmExtraEclipseCDT4Generator>
69
35
    factory("Eclipse CDT4",
70
35
            "Generates Eclipse CDT 4.0 project files (deprecated).");
71
72
35
  if (factory.GetSupportedGlobalGenerators().empty()) {
73
// TODO: Verify if __CYGWIN__ should be checked.
74
// #if defined(_WIN32) && !defined(__CYGWIN__)
75
#if defined(_WIN32)
76
    factory.AddSupportedGlobalGenerator("NMake Makefiles");
77
    factory.AddSupportedGlobalGenerator("MinGW Makefiles");
78
// factory.AddSupportedGlobalGenerator("MSYS Makefiles");
79
#endif
80
1
    factory.AddSupportedGlobalGenerator("Ninja");
81
1
    factory.AddSupportedGlobalGenerator("Unix Makefiles");
82
1
  }
83
84
35
  return &factory;
85
35
}
86
87
void cmExtraEclipseCDT4Generator::EnableLanguage(
88
  std::vector<std::string> const& languages, cmMakefile* /*unused*/,
89
  bool /*optional*/)
90
0
{
91
0
  for (std::string const& l : languages) {
92
0
    if (l == "CXX") {
93
0
      this->Natures.insert("org.eclipse.cdt.core.ccnature");
94
0
      this->Natures.insert("org.eclipse.cdt.core.cnature");
95
0
      this->CXXEnabled = true;
96
0
    } else if (l == "C") {
97
0
      this->Natures.insert("org.eclipse.cdt.core.cnature");
98
0
      this->CEnabled = true;
99
0
    } else if (l == "Java") {
100
0
      this->Natures.insert("org.eclipse.jdt.core.javanature");
101
0
    }
102
0
  }
103
0
}
104
105
void cmExtraEclipseCDT4Generator::Generate()
106
0
{
107
0
  auto const& lg = this->GlobalGenerator->GetLocalGenerators()[0];
108
0
  cmMakefile const* mf = lg->GetMakefile();
109
110
0
  std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
111
0
  cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
112
0
  if (regex.find(eclipseVersion)) {
113
0
    unsigned int majorVersion = 0;
114
0
    unsigned int minorVersion = 0;
115
0
    int res =
116
0
      sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion, &minorVersion);
117
0
    if (res == 2) {
118
0
      int version = majorVersion * 1000 + minorVersion;
119
0
      if (version < 3006) // 3.6 is Helios
120
0
      {
121
0
        this->SupportsVirtualFolders = false;
122
0
        this->SupportsMachO64Parser = false;
123
0
      }
124
0
      if (version < 3007) // 3.7 is Indigo
125
0
      {
126
0
        this->SupportsGmakeErrorParser = false;
127
0
      }
128
0
    }
129
0
  }
130
131
  // TODO: Decide if these are local or member variables
132
0
  this->HomeDirectory = lg->GetSourceDirectory();
133
0
  this->HomeOutputDirectory = lg->GetBinaryDirectory();
134
135
0
  this->GenerateLinkedResources =
136
0
    mf->IsOn("CMAKE_ECLIPSE_GENERATE_LINKED_RESOURCES");
137
138
0
  this->IsOutOfSourceBuild =
139
0
    (this->HomeDirectory != this->HomeOutputDirectory);
140
141
0
  this->GenerateSourceProject =
142
0
    (this->IsOutOfSourceBuild &&
143
0
     mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
144
145
0
  if (!this->GenerateSourceProject &&
146
0
      (mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"))) {
147
0
    mf->IssueMessage(
148
0
      MessageType::WARNING,
149
0
      "ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
150
0
      "but this variable is not supported anymore since CMake 2.8.7.\n"
151
0
      "Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
152
0
  }
153
154
0
  if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
155
0
                                    this->HomeDirectory)) {
156
0
    mf->IssueMessage(MessageType::WARNING,
157
0
                     "The build directory is a subdirectory "
158
0
                     "of the source directory.\n"
159
0
                     "This is not supported well by Eclipse. It is strongly "
160
0
                     "recommended to use a build directory which is a "
161
0
                     "sibling of the source directory.");
162
0
  }
163
164
  // NOTE: This is not good, since it pollutes the source tree. However,
165
  //       Eclipse doesn't allow CVS/SVN to work when the .project is not in
166
  //       the cvs/svn root directory. Hence, this is provided as an option.
167
0
  if (this->GenerateSourceProject) {
168
    // create .project file in the source tree
169
0
    this->CreateSourceProjectFile();
170
0
  }
171
172
  // create a .project file
173
0
  this->CreateProjectFile();
174
175
  // create a .cproject file
176
0
  this->CreateCProjectFile();
177
178
  // create resource settings
179
0
  this->CreateSettingsResourcePrefsFile();
180
0
}
181
182
void cmExtraEclipseCDT4Generator::CreateSettingsResourcePrefsFile()
183
0
{
184
0
  auto const& lg = this->GlobalGenerator->GetLocalGenerators()[0];
185
0
  cmMakefile* mf = lg->GetMakefile();
186
187
0
  std::string const filename =
188
0
    this->HomeOutputDirectory + "/.settings/org.eclipse.core.resources.prefs";
189
190
0
  cmGeneratedFileStream fout(filename);
191
0
  if (!fout) {
192
0
    return;
193
0
  }
194
195
0
  fout << "eclipse.preferences.version=1\n";
196
0
  cmValue encoding = mf->GetDefinition("CMAKE_ECLIPSE_RESOURCE_ENCODING");
197
0
  if (encoding) {
198
0
    fout << "encoding/<project>=" << *encoding << '\n';
199
0
  }
200
0
}
201
202
void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
203
0
{
204
0
  assert(this->HomeDirectory != this->HomeOutputDirectory);
205
206
  // set up the project name: <project>-Source@<baseSourcePathName>
207
0
  auto const& lg = this->GlobalGenerator->GetLocalGenerators()[0];
208
0
  std::string name = cmExtraEclipseCDT4Generator::GenerateProjectName(
209
0
    lg->GetProjectName(), "Source",
210
0
    cmExtraEclipseCDT4Generator::GetPathBasename(this->HomeDirectory));
211
212
0
  std::string const filename = this->HomeDirectory + "/.project";
213
0
  cmGeneratedFileStream fout(filename);
214
0
  if (!fout) {
215
0
    return;
216
0
  }
217
218
0
  cmXMLWriter xml(fout);
219
0
  xml.StartDocument("UTF-8");
220
0
  xml.StartElement("projectDescription");
221
0
  xml.Element("name", name);
222
0
  xml.Element("comment", "");
223
0
  xml.Element("projects", "");
224
0
  xml.Element("buildSpec", "");
225
0
  xml.Element("natures", "");
226
0
  xml.StartElement("linkedResources");
227
228
0
  if (this->SupportsVirtualFolders) {
229
0
    this->CreateLinksToSubprojects(xml, this->HomeDirectory);
230
0
    this->SrcLinkedResources.clear();
231
0
  }
232
233
0
  xml.EndElement(); // linkedResources
234
0
  xml.EndElement(); // projectDescription
235
0
  xml.EndDocument();
236
0
}
237
238
void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
239
                                            char const* envVar,
240
                                            cmLocalGenerator& lg)
241
0
{
242
0
  cmMakefile* mf = lg.GetMakefile();
243
244
  // get the variables from the environment and from the cache and then
245
  // figure out which one to use:
246
247
0
  std::string envVarValue;
248
0
  bool const envVarSet = cmSystemTools::GetEnv(envVar, envVarValue);
249
250
0
  std::string cacheEntryName = cmStrCat("CMAKE_ECLIPSE_ENVVAR_", envVar);
251
0
  cmValue cacheValue = lg.GetState()->GetInitializedCacheValue(cacheEntryName);
252
253
  // now we have both, decide which one to use
254
0
  std::string valueToUse;
255
0
  if (!envVarSet && !cacheValue) {
256
    // nothing known, do nothing
257
0
    valueToUse.clear();
258
0
  } else if (envVarSet && !cacheValue) {
259
    // The variable is in the env, but not in the cache. Use it and put it
260
    // in the cache
261
0
    valueToUse = envVarValue;
262
0
    mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
263
0
                           cmStateEnums::STRING, true);
264
0
    mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
265
0
  } else if (!envVarSet && cacheValue) {
266
    // It is already in the cache, but not in the env, so use it from the cache
267
0
    valueToUse = *cacheValue;
268
0
  } else {
269
    // It is both in the cache and in the env.
270
    // Use the version from the env. except if the value from the env is
271
    // completely contained in the value from the cache (for the case that we
272
    // now have a PATH without MSVC dirs in the env. but had the full PATH with
273
    // all MSVC dirs during the cmake run which stored the var in the cache:
274
0
    valueToUse = *cacheValue;
275
0
    if (valueToUse.find(envVarValue) == std::string::npos) {
276
0
      valueToUse = envVarValue;
277
0
      mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
278
0
                             cmStateEnums::STRING, true);
279
0
      mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
280
0
    }
281
0
  }
282
283
0
  if (!valueToUse.empty()) {
284
0
    out << envVar << "=" << valueToUse << "|";
285
0
  }
286
0
}
287
288
void cmExtraEclipseCDT4Generator::CreateProjectFile()
289
0
{
290
0
  auto const& lg = this->GlobalGenerator->GetLocalGenerators()[0];
291
0
  cmMakefile* mf = lg->GetMakefile();
292
293
0
  std::string const filename = this->HomeOutputDirectory + "/.project";
294
295
0
  cmGeneratedFileStream fout(filename);
296
0
  if (!fout) {
297
0
    return;
298
0
  }
299
300
0
  std::string compilerId = mf->GetSafeDefinition("CMAKE_C_COMPILER_ID");
301
0
  if (compilerId.empty()) // no C compiler, try the C++ compiler:
302
0
  {
303
0
    compilerId = mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
304
0
  }
305
306
0
  cmXMLWriter xml(fout);
307
308
0
  xml.StartDocument("UTF-8");
309
0
  xml.StartElement("projectDescription");
310
311
0
  xml.Element("name",
312
0
              cmExtraEclipseCDT4Generator::GenerateProjectName(
313
0
                lg->GetProjectName(),
314
0
                mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
315
0
                cmExtraEclipseCDT4Generator::GetPathBasename(
316
0
                  this->HomeOutputDirectory)));
317
318
0
  xml.Element("comment", "");
319
0
  xml.Element("projects", "");
320
321
0
  xml.StartElement("buildSpec");
322
0
  xml.StartElement("buildCommand");
323
0
  xml.Element("name", "org.eclipse.cdt.make.core.makeBuilder");
324
0
  xml.Element("triggers", "clean,full,incremental,");
325
0
  xml.StartElement("arguments");
326
327
  // use clean target
328
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.cleanBuildTarget", "clean");
329
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.enableCleanBuild", "true");
330
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.append_environment",
331
0
                   "true");
332
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.stopOnError", "true");
333
334
  // set the make command
335
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.enabledIncrementalBuild",
336
0
                   "true");
337
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.build.command",
338
0
                   cmExtraEclipseCDT4Generator::GetEclipsePath(
339
0
                     mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM")));
340
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.contents",
341
0
                   "org.eclipse.cdt.make.core.activeConfigSettings");
342
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.inc", "all");
343
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.build.arguments",
344
0
                   mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS"));
345
0
  AppendDictionary(
346
0
    xml, "org.eclipse.cdt.make.core.buildLocation",
347
0
    cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
348
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.useDefaultBuildCmd",
349
0
                   "false");
350
351
  // set project specific environment
352
0
  std::ostringstream environment;
353
0
  environment << "VERBOSE=1|CMAKE_NO_VERBOSE=1|"; // verbose Makefile output
354
  // set vsvars32.bat environment available at CMake time,
355
  //   but not necessarily when eclipse is open
356
0
  if (compilerId == "MSVC") {
357
0
    AddEnvVar(environment, "PATH", *lg);
358
0
    AddEnvVar(environment, "INCLUDE", *lg);
359
0
    AddEnvVar(environment, "LIB", *lg);
360
0
    AddEnvVar(environment, "LIBPATH", *lg);
361
0
  } else if (compilerId == "Intel") {
362
    // if the env.var is set, use this one and put it in the cache
363
    // if the env.var is not set, but the value is in the cache,
364
    // use it from the cache:
365
0
    AddEnvVar(environment, "INTEL_LICENSE_FILE", *lg);
366
0
  }
367
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.environment",
368
0
                   environment.str());
369
370
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.enableFullBuild", "true");
371
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.auto", "all");
372
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.enableAutoBuild", "false");
373
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.clean",
374
0
                   "clean");
375
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.fullBuildTarget", "all");
376
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.buildArguments", "");
377
0
  AppendDictionary(
378
0
    xml, "org.eclipse.cdt.make.core.build.location",
379
0
    cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
380
0
  AppendDictionary(xml, "org.eclipse.cdt.make.core.autoBuildTarget", "all");
381
382
  // set error parsers
383
0
  std::ostringstream errorOutputParser;
384
385
0
  if (compilerId == "MSVC") {
386
0
    errorOutputParser << "org.eclipse.cdt.core.VCErrorParser;";
387
0
  } else if (compilerId == "Intel") {
388
0
    errorOutputParser << "org.eclipse.cdt.core.ICCErrorParser;";
389
0
  }
390
391
0
  if (this->SupportsGmakeErrorParser) {
392
0
    errorOutputParser << "org.eclipse.cdt.core.GmakeErrorParser;";
393
0
  } else {
394
0
    errorOutputParser << "org.eclipse.cdt.core.MakeErrorParser;";
395
0
  }
396
397
0
  errorOutputParser << "org.eclipse.cdt.core.GCCErrorParser;"
398
0
                       "org.eclipse.cdt.core.GASErrorParser;"
399
0
                       "org.eclipse.cdt.core.GLDErrorParser;";
400
0
  AppendDictionary(xml, "org.eclipse.cdt.core.errorOutputParser",
401
0
                   errorOutputParser.str());
402
403
0
  xml.EndElement(); // arguments
404
0
  xml.EndElement(); // buildCommand
405
0
  xml.StartElement("buildCommand");
406
0
  xml.Element("name", "org.eclipse.cdt.make.core.ScannerConfigBuilder");
407
0
  xml.StartElement("arguments");
408
0
  xml.EndElement(); // arguments
409
0
  xml.EndElement(); // buildCommand
410
0
  xml.EndElement(); // buildSpec
411
412
  // set natures for c/c++ projects
413
0
  xml.StartElement("natures");
414
0
  xml.Element("nature", "org.eclipse.cdt.make.core.makeNature");
415
0
  xml.Element("nature", "org.eclipse.cdt.make.core.ScannerConfigNature");
416
417
0
  for (std::string const& n : this->Natures) {
418
0
    xml.Element("nature", n);
419
0
  }
420
421
0
  if (cmValue extraNaturesProp =
422
0
        mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_NATURES")) {
423
0
    cmList extraNatures{ *extraNaturesProp };
424
0
    for (std::string const& n : extraNatures) {
425
0
      xml.Element("nature", n);
426
0
    }
427
0
  }
428
429
0
  xml.EndElement(); // natures
430
431
0
  xml.StartElement("linkedResources");
432
  // create linked resources
433
0
  if (this->IsOutOfSourceBuild) {
434
    // create a linked resource to CMAKE_SOURCE_DIR
435
    // (this is not done anymore for each project because of
436
    // https://gitlab.kitware.com/cmake/cmake/-/issues/9978 and because I found
437
    // it actually quite confusing in bigger projects with many directories and
438
    // projects, Alex
439
440
0
    std::string sourceLinkedResourceName = "[Source directory]";
441
0
    std::string linkSourceDirectory =
442
0
      cmExtraEclipseCDT4Generator::GetEclipsePath(
443
0
        lg->GetCurrentSourceDirectory());
444
    // .project dir can't be subdir of a linked resource dir
445
0
    if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
446
0
                                       linkSourceDirectory)) {
447
0
      cmExtraEclipseCDT4Generator::AppendLinkedResource(
448
0
        xml, sourceLinkedResourceName,
449
0
        cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
450
0
        LinkToFolder);
451
0
      this->SrcLinkedResources.push_back(std::move(sourceLinkedResourceName));
452
0
    }
453
0
  }
454
455
0
  if (this->SupportsVirtualFolders) {
456
0
    this->CreateLinksToSubprojects(xml, this->HomeOutputDirectory);
457
458
0
    this->CreateLinksForTargets(xml);
459
0
  }
460
461
0
  xml.EndElement(); // linkedResources
462
0
  xml.EndElement(); // projectDescription
463
0
}
464
465
void cmExtraEclipseCDT4Generator::WriteGroups(
466
  SourceGroupVector const& sourceGroups,
467
  cmSourceGroupFiles const& sourceGroupFiles, std::string& linkName,
468
  cmXMLWriter& xml)
469
0
{
470
0
  for (auto const& sg : sourceGroups) {
471
0
    std::string linkName3 = cmStrCat(linkName, '/', sg->GetFullName());
472
473
0
    std::replace(linkName3.begin(), linkName3.end(), '\\', '/');
474
475
0
    cmExtraEclipseCDT4Generator::AppendLinkedResource(
476
0
      xml, linkName3, "virtual:/virtual", VirtualFolder);
477
0
    SourceGroupVector const& children = sg->GetGroupChildren();
478
0
    if (!children.empty()) {
479
0
      this->WriteGroups(children, sourceGroupFiles, linkName, xml);
480
0
    }
481
0
    std::vector<cmSourceFile const*> const& sourceFiles =
482
0
      sourceGroupFiles.GetSourceFiles(sg.get());
483
0
    for (cmSourceFile const* file : sourceFiles) {
484
0
      std::string const& fullPath = file->GetFullPath();
485
486
0
      if (!cmSystemTools::FileIsDirectory(fullPath)) {
487
0
        std::string linkName4 =
488
0
          cmStrCat(linkName3, '/', cmSystemTools::GetFilenameName(fullPath));
489
0
        cmExtraEclipseCDT4Generator::AppendLinkedResource(
490
0
          xml, linkName4,
491
0
          cmExtraEclipseCDT4Generator::GetEclipsePath(fullPath), LinkToFile);
492
0
      }
493
0
    }
494
0
  }
495
0
}
496
497
void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml)
498
0
{
499
0
  std::string linkName = "[Targets]";
500
0
  cmExtraEclipseCDT4Generator::AppendLinkedResource(
501
0
    xml, linkName, "virtual:/virtual", VirtualFolder);
502
503
0
  for (auto const& lg : this->GlobalGenerator->GetLocalGenerators()) {
504
0
    cmMakefile* makefile = lg->GetMakefile();
505
0
    auto const& targets = lg->GetGeneratorTargets();
506
507
0
    for (auto const& target : targets) {
508
0
      std::string linkName2 = cmStrCat(linkName, '/');
509
0
      switch (target->GetType()) {
510
0
        case cmStateEnums::EXECUTABLE:
511
0
        case cmStateEnums::STATIC_LIBRARY:
512
0
        case cmStateEnums::SHARED_LIBRARY:
513
0
        case cmStateEnums::MODULE_LIBRARY:
514
0
        case cmStateEnums::OBJECT_LIBRARY: {
515
0
          char const* prefix =
516
0
            (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
517
0
                                                           : "[lib] ");
518
0
          linkName2 += prefix;
519
0
          linkName2 += target->GetName();
520
0
          cmExtraEclipseCDT4Generator::AppendLinkedResource(
521
0
            xml, linkName2, "virtual:/virtual", VirtualFolder);
522
0
          if (!this->GenerateLinkedResources) {
523
0
            break; // skip generating the linked resources to the source files
524
0
          }
525
          // get the files from the source lists then add them to the groups
526
0
          cmSourceGroupFiles sourceGroupFiles;
527
0
          std::vector<cmSourceFile*> files;
528
0
          target->GetSourceFiles(
529
0
            files, makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
530
0
          for (cmSourceFile* sf : files) {
531
            // Add the file to the list of sources.
532
0
            sourceGroupFiles.Add(lg->FindSourceGroup(sf->ResolveFullPath()),
533
0
                                 sf);
534
0
          }
535
536
0
          this->WriteGroups(makefile->GetSourceGroups(), sourceGroupFiles,
537
0
                            linkName2, xml);
538
0
        } break;
539
        // ignore all others:
540
0
        default:
541
0
          break;
542
0
      }
543
0
    }
544
0
  }
545
0
}
546
547
void cmExtraEclipseCDT4Generator::CreateLinksToSubprojects(
548
  cmXMLWriter& xml, std::string const& baseDir)
549
0
{
550
0
  if (!this->GenerateLinkedResources) {
551
0
    return;
552
0
  }
553
554
  // for each sub project create a linked resource to the source dir
555
  // - only if it is an out-of-source build
556
0
  cmExtraEclipseCDT4Generator::AppendLinkedResource(
557
0
    xml, "[Subprojects]", "virtual:/virtual", VirtualFolder);
558
559
0
  for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
560
0
    std::string linkSourceDirectory =
561
0
      cmExtraEclipseCDT4Generator::GetEclipsePath(
562
0
        it.second[0]->GetCurrentSourceDirectory());
563
    // a linked resource must not point to a parent directory of .project or
564
    // .project itself
565
0
    if ((baseDir != linkSourceDirectory) &&
566
0
        !cmSystemTools::IsSubDirectory(baseDir, linkSourceDirectory)) {
567
0
      std::string linkName = cmStrCat("[Subprojects]/", it.first);
568
0
      cmExtraEclipseCDT4Generator::AppendLinkedResource(
569
0
        xml, linkName,
570
0
        cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
571
0
        LinkToFolder);
572
      // Don't add it to the srcLinkedResources, because listing multiple
573
      // directories confuses the Eclipse indexer (#13596).
574
0
    }
575
0
  }
576
0
}
577
578
void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
579
  cmXMLWriter& xml, std::vector<std::string> const& includeDirs,
580
  std::set<std::string>& emittedDirs)
581
0
{
582
0
  for (std::string const& inc : includeDirs) {
583
0
    if (!inc.empty()) {
584
0
      std::string dir = cmSystemTools::CollapseFullPath(inc);
585
586
      // handle framework include dirs on OSX, the remainder after the
587
      // Frameworks/ part has to be stripped
588
      //   /System/Library/Frameworks/GLUT.framework/Headers
589
0
      cmsys::RegularExpression frameworkRx("(.+/Frameworks)/.+\\.framework/");
590
0
      if (frameworkRx.find(dir)) {
591
0
        dir = frameworkRx.match(1);
592
0
      }
593
594
0
      if (emittedDirs.insert(dir).second) {
595
0
        xml.StartElement("pathentry");
596
0
        xml.Attribute("include",
597
0
                      cmExtraEclipseCDT4Generator::GetEclipsePath(dir));
598
0
        xml.Attribute("kind", "inc");
599
0
        xml.Attribute("path", "");
600
0
        xml.Attribute("system", "true");
601
0
        xml.EndElement();
602
0
      }
603
0
    }
604
0
  }
605
0
}
606
607
void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
608
0
{
609
0
  std::set<std::string> emitted;
610
611
0
  auto const& lg = this->GlobalGenerator->GetLocalGenerators()[0];
612
0
  cmMakefile const* mf = lg->GetMakefile();
613
614
0
  std::string const filename = this->HomeOutputDirectory + "/.cproject";
615
616
0
  cmGeneratedFileStream fout(filename);
617
0
  if (!fout) {
618
0
    return;
619
0
  }
620
621
0
  cmXMLWriter xml(fout);
622
623
  // add header
624
0
  xml.StartDocument("UTF-8");
625
0
  xml.ProcessingInstruction("fileVersion", "4.0.0");
626
0
  xml.StartElement("cproject");
627
0
  xml.StartElement("storageModule");
628
0
  xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
629
630
0
  xml.StartElement("cconfiguration"); // noqa: spellcheck disable-line
631
0
  xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
632
633
  // Configuration settings...
634
0
  xml.StartElement("storageModule");
635
0
  xml.Attribute("buildSystemId",
636
0
                "org.eclipse.cdt.core.defaultConfigDataProvider");
637
0
  xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
638
0
  xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
639
0
  xml.Attribute("name", "Configuration");
640
0
  xml.Element("externalSettings");
641
0
  xml.StartElement("extensions");
642
643
  // TODO: refactor this out...
644
0
  std::string executableFormat =
645
0
    mf->GetSafeDefinition("CMAKE_EXECUTABLE_FORMAT");
646
0
  if (executableFormat == "ELF") {
647
0
    xml.StartElement("extension");
648
0
    xml.Attribute("id", "org.eclipse.cdt.core.ELF");
649
0
    xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
650
0
    xml.EndElement(); // extension
651
652
0
    xml.StartElement("extension");
653
0
    xml.Attribute("id", "org.eclipse.cdt.core.GNU_ELF");
654
0
    xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
655
0
    AppendAttribute(xml, "addr2line");
656
0
    AppendAttribute(xml, "c++filt");
657
0
    xml.EndElement(); // extension
658
0
  } else {
659
0
    std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
660
0
    if (systemName == "CYGWIN" || systemName == "MSYS") {
661
0
      xml.StartElement("extension");
662
0
      xml.Attribute("id", "org.eclipse.cdt.core.Cygwin_PE");
663
0
      xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
664
0
      AppendAttribute(xml, "addr2line");
665
0
      AppendAttribute(xml, "c++filt");
666
0
      AppendAttribute(xml, "cygpath");
667
0
      AppendAttribute(xml, "nm");
668
0
      xml.EndElement(); // extension
669
0
    } else if (systemName == "Windows") {
670
0
      xml.StartElement("extension");
671
0
      xml.Attribute("id", "org.eclipse.cdt.core.PE");
672
0
      xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
673
0
      xml.EndElement(); // extension
674
0
    } else if (systemName == "Darwin") {
675
0
      xml.StartElement("extension");
676
0
      xml.Attribute("id",
677
0
                    this->SupportsMachO64Parser
678
0
                      ? "org.eclipse.cdt.core.MachO64"
679
0
                      : "org.eclipse.cdt.core.MachO");
680
0
      xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
681
0
      AppendAttribute(xml, "c++filt");
682
0
      xml.EndElement(); // extension
683
0
    } else {
684
      // *** Should never get here ***
685
0
      xml.Element("error_toolchain_type");
686
0
    }
687
0
  }
688
689
0
  xml.EndElement(); // extensions
690
0
  xml.EndElement(); // storageModule
691
692
  // ???
693
0
  xml.StartElement("storageModule");
694
0
  xml.Attribute("moduleId", "org.eclipse.cdt.core.language.mapping");
695
0
  xml.Element("project-mappings");
696
0
  xml.EndElement(); // storageModule
697
698
  // ???
699
0
  xml.StartElement("storageModule");
700
0
  xml.Attribute("moduleId", "org.eclipse.cdt.core.externalSettings");
701
0
  xml.EndElement(); // storageModule
702
703
  // set the path entries (includes, libs, source dirs, etc.)
704
0
  xml.StartElement("storageModule");
705
0
  xml.Attribute("moduleId", "org.eclipse.cdt.core.pathentry");
706
707
  // for each sub project with a linked resource to the source dir:
708
  // - make it type 'src'
709
  // - and exclude it from type 'out'
710
0
  std::string excludeFromOut;
711
  /* I don't know what the pathentry kind="src" are good for, e.g.
712
   * autocompletion
713
   * works also without them. Done wrong, the indexer complains, see #12417
714
   * and #12213.
715
   * According to #13596, this entry at least limits the directories the
716
   * indexer is searching for files. So now the "src" entry contains only
717
   * the linked resource to CMAKE_SOURCE_DIR.
718
   * The CDT documentation is very terse on that:
719
   * "CDT_SOURCE: Entry kind constant describing a path entry identifying a
720
   * folder containing source code to be compiled."
721
   * Also on the cdt-dev list didn't bring any information:
722
   * http://web.archiveorange.com/archive/v/B4NlJDNIpYoOS1SbxFNy
723
   * Alex */
724
  // include subprojects directory to the src pathentry
725
  // eclipse cdt indexer uses this entries as reference to index source files
726
0
  if (this->GenerateLinkedResources) {
727
0
    xml.StartElement("pathentry");
728
0
    xml.Attribute("kind", "src");
729
0
    xml.Attribute("path", "[Subprojects]");
730
0
    xml.EndElement();
731
0
  }
732
733
0
  for (std::string const& p : this->SrcLinkedResources) {
734
0
    xml.StartElement("pathentry");
735
0
    xml.Attribute("kind", "src");
736
0
    xml.Attribute("path", p);
737
0
    xml.EndElement();
738
739
    // exclude source directory from output search path
740
    // - only if not named the same as an output directory
741
0
    if (!cmSystemTools::FileIsDirectory(
742
0
          cmStrCat(this->HomeOutputDirectory, '/', p))) {
743
0
      excludeFromOut = cmStrCat(std::move(excludeFromOut), p, "/|");
744
0
    }
745
0
  }
746
747
0
  excludeFromOut += "**/CMakeFiles/";
748
749
0
  xml.StartElement("pathentry");
750
0
  xml.Attribute("excluding", excludeFromOut);
751
0
  xml.Attribute("kind", "out");
752
0
  xml.Attribute("path", "");
753
0
  xml.EndElement();
754
755
  // add pre-processor definitions to allow eclipse to gray out sections
756
0
  emitted.clear();
757
0
  for (auto const& lgen : this->GlobalGenerator->GetLocalGenerators()) {
758
759
0
    if (cmValue cdefs =
760
0
          lgen->GetMakefile()->GetProperty("COMPILE_DEFINITIONS")) {
761
      // Expand the list.
762
0
      std::vector<std::string> defs;
763
0
      cmGeneratorExpression::Split(*cdefs, defs);
764
765
0
      for (std::string const& d : defs) {
766
0
        if (cmGeneratorExpression::Find(d) != std::string::npos) {
767
0
          continue;
768
0
        }
769
770
0
        std::string::size_type equals = d.find('=', 0);
771
0
        std::string::size_type enddef = d.length();
772
773
0
        std::string def;
774
0
        std::string val;
775
0
        if (equals != std::string::npos && equals < enddef) {
776
          // we have -DFOO=BAR
777
0
          def = d.substr(0, equals);
778
0
          val = d.substr(equals + 1, enddef - equals + 1);
779
0
        } else {
780
          // we have -DFOO
781
0
          def = d;
782
0
        }
783
784
        // insert the definition if not already added.
785
0
        if (emitted.insert(def).second) {
786
0
          xml.StartElement("pathentry");
787
0
          xml.Attribute("kind", "mac");
788
0
          xml.Attribute("name", def);
789
0
          xml.Attribute("path", "");
790
0
          xml.Attribute("value", val);
791
0
          xml.EndElement();
792
0
        }
793
0
      }
794
0
    }
795
0
  }
796
  // add system defined c macros
797
0
  cmValue cDefs =
798
0
    mf->GetDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS");
799
0
  if (this->CEnabled && cDefs) {
800
    // Expand the list.
801
0
    cmList defs{ *cDefs, cmList::EmptyElements::Yes };
802
803
    // the list must contain only definition-value pairs:
804
0
    if ((defs.size() % 2) == 0) {
805
0
      auto di = defs.begin();
806
0
      while (di != defs.end()) {
807
0
        std::string def = *di;
808
0
        ++di;
809
0
        std::string val;
810
0
        if (di != defs.end()) {
811
0
          val = *di;
812
0
          ++di;
813
0
        }
814
815
        // insert the definition if not already added.
816
0
        if (emitted.insert(def).second) {
817
0
          xml.StartElement("pathentry");
818
0
          xml.Attribute("kind", "mac");
819
0
          xml.Attribute("name", def);
820
0
          xml.Attribute("path", "");
821
0
          xml.Attribute("value", val);
822
0
          xml.EndElement();
823
0
        }
824
0
      }
825
0
    }
826
0
  }
827
  // add system defined c++ macros
828
0
  cmValue cxxDefs =
829
0
    mf->GetDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS");
830
0
  if (this->CXXEnabled && cxxDefs) {
831
    // Expand the list.
832
0
    cmList defs{ *cxxDefs, cmList::EmptyElements::Yes };
833
834
    // the list must contain only definition-value pairs:
835
0
    if ((defs.size() % 2) == 0) {
836
0
      auto di = defs.begin();
837
0
      while (di != defs.end()) {
838
0
        std::string def = *di;
839
0
        ++di;
840
0
        std::string val;
841
0
        if (di != defs.end()) {
842
0
          val = *di;
843
0
          ++di;
844
0
        }
845
846
        // insert the definition if not already added.
847
0
        if (emitted.insert(def).second) {
848
0
          xml.StartElement("pathentry");
849
0
          xml.Attribute("kind", "mac");
850
0
          xml.Attribute("name", def);
851
0
          xml.Attribute("path", "");
852
0
          xml.Attribute("value", val);
853
0
          xml.EndElement();
854
0
        }
855
0
      }
856
0
    }
857
0
  }
858
859
  // include dirs
860
0
  emitted.clear();
861
0
  for (auto const& lgen : this->GlobalGenerator->GetLocalGenerators()) {
862
0
    auto const& targets = lgen->GetGeneratorTargets();
863
0
    for (auto const& target : targets) {
864
0
      if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
865
0
        continue;
866
0
      }
867
0
      std::vector<std::string> includeDirs;
868
0
      std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
869
0
      lgen->GetIncludeDirectories(includeDirs, target.get(), "C", config);
870
0
      this->AppendIncludeDirectories(xml, includeDirs, emitted);
871
0
    }
872
0
  }
873
  // now also the system include directories, in case we found them in
874
  // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
875
  // standard headers.
876
0
  std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
877
0
  if (this->CEnabled && !compiler.empty()) {
878
0
    std::string systemIncludeDirs =
879
0
      mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
880
0
    cmList dirs{ systemIncludeDirs };
881
0
    this->AppendIncludeDirectories(xml, dirs, emitted);
882
0
  }
883
0
  compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
884
0
  if (this->CXXEnabled && !compiler.empty()) {
885
0
    std::string systemIncludeDirs =
886
0
      mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
887
0
    cmList dirs{ systemIncludeDirs };
888
0
    this->AppendIncludeDirectories(xml, dirs, emitted);
889
0
  }
890
891
0
  xml.EndElement(); // storageModule
892
893
  // add build targets
894
0
  xml.StartElement("storageModule");
895
0
  xml.Attribute("moduleId", "org.eclipse.cdt.make.core.buildtargets");
896
0
  xml.StartElement("buildTargets");
897
0
  emitted.clear();
898
0
  std::string const& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
899
0
  std::string const& makeArgs =
900
0
    mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS");
901
902
0
  cmGlobalGenerator* generator =
903
0
    const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
904
905
0
  std::string allTarget;
906
0
  std::string cleanTarget;
907
0
  if (generator->GetAllTargetName()) {
908
0
    allTarget = generator->GetAllTargetName();
909
0
  }
910
0
  if (generator->GetCleanTargetName()) {
911
0
    cleanTarget = generator->GetCleanTargetName();
912
0
  }
913
914
  // add all executable and library targets and some of the GLOBAL
915
  // and UTILITY targets
916
0
  for (auto const& lgen : this->GlobalGenerator->GetLocalGenerators()) {
917
0
    auto const& targets = lgen->GetGeneratorTargets();
918
0
    std::string subdir =
919
0
      lgen->MaybeRelativeToTopBinDir(lgen->GetCurrentBinaryDirectory());
920
0
    if (subdir == ".") {
921
0
      subdir.clear();
922
0
    }
923
924
0
    for (auto const& target : targets) {
925
0
      std::string targetName = target->GetName();
926
0
      switch (target->GetType()) {
927
0
        case cmStateEnums::GLOBAL_TARGET: {
928
          // Only add the global targets from CMAKE_BINARY_DIR,
929
          // not from the subdirs
930
0
          if (subdir.empty()) {
931
0
            cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
932
0
                                                      makeArgs, subdir, ": ");
933
0
          }
934
0
        } break;
935
0
        case cmStateEnums::UTILITY:
936
          // Add all utility targets, except the Nightly/Continuous/
937
          // Experimental-"sub"targets as e.g. NightlyStart
938
0
          if ((cmHasLiteralPrefix(targetName, "Nightly") &&
939
0
               (targetName != "Nightly")) ||
940
0
              (cmHasLiteralPrefix(targetName, "Continuous") &&
941
0
               (targetName != "Continuous")) ||
942
0
              (cmHasLiteralPrefix(targetName, "Experimental") &&
943
0
               (targetName != "Experimental"))) {
944
0
            break;
945
0
          }
946
947
0
          cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
948
0
                                                    makeArgs, subdir, ": ");
949
0
          break;
950
0
        case cmStateEnums::EXECUTABLE:
951
0
        case cmStateEnums::STATIC_LIBRARY:
952
0
        case cmStateEnums::SHARED_LIBRARY:
953
0
        case cmStateEnums::MODULE_LIBRARY:
954
0
        case cmStateEnums::OBJECT_LIBRARY: {
955
0
          char const* prefix =
956
0
            (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
957
0
                                                           : "[lib] ");
958
0
          cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
959
0
                                                    makeArgs, subdir, prefix);
960
0
          std::string fastTarget = cmStrCat(targetName, "/fast");
961
0
          cmExtraEclipseCDT4Generator::AppendTarget(xml, fastTarget, make,
962
0
                                                    makeArgs, subdir, prefix);
963
964
          // Add Build and Clean targets in the virtual folder of targets:
965
0
          if (this->SupportsVirtualFolders) {
966
0
            std::string virtDir = cmStrCat("[Targets]/", prefix, targetName);
967
0
            std::string buildArgs =
968
0
              cmStrCat("-C \"", lgen->GetBinaryDirectory(), "\" ", makeArgs);
969
0
            cmExtraEclipseCDT4Generator::AppendTarget(
970
0
              xml, "Build", make, buildArgs, virtDir, "", targetName.c_str());
971
972
0
            std::string cleanArgs =
973
0
              cmStrCat("-E chdir \"", lgen->GetObjectOutputRoot(), "\" \"",
974
0
                       cmSystemTools::GetCMakeCommand(), "\" -P \"");
975
0
            cleanArgs += lgen->GetTargetDirectory(
976
0
              target.get(), cmStateEnums::IntermediateDirKind::ObjectFiles);
977
0
            cleanArgs += "/cmake_clean.cmake\"";
978
0
            cmExtraEclipseCDT4Generator::AppendTarget(
979
0
              xml, "Clean", cmSystemTools::GetCMakeCommand(), cleanArgs,
980
0
              virtDir, "", "");
981
0
          }
982
0
        } break;
983
0
        case cmStateEnums::INTERFACE_LIBRARY:
984
0
        default:
985
0
          break;
986
0
      }
987
0
    }
988
989
    // insert the all and clean targets in every subdir
990
0
    if (!allTarget.empty()) {
991
0
      cmExtraEclipseCDT4Generator::AppendTarget(xml, allTarget, make, makeArgs,
992
0
                                                subdir, ": ");
993
0
    }
994
0
    if (!cleanTarget.empty()) {
995
0
      cmExtraEclipseCDT4Generator::AppendTarget(xml, cleanTarget, make,
996
0
                                                makeArgs, subdir, ": ");
997
0
    }
998
999
    // insert rules for compiling, preprocessing and assembling individual
1000
    // files
1001
0
    std::vector<std::string> objectFileTargets;
1002
0
    lg->GetIndividualFileTargets(objectFileTargets);
1003
0
    for (std::string const& f : objectFileTargets) {
1004
0
      char const* prefix = "[obj] ";
1005
0
      if (f.back() == 's') {
1006
0
        prefix = "[to asm] ";
1007
0
      } else if (f.back() == 'i') {
1008
0
        prefix = "[pre] ";
1009
0
      }
1010
0
      cmExtraEclipseCDT4Generator::AppendTarget(xml, f, make, makeArgs, subdir,
1011
0
                                                prefix);
1012
0
    }
1013
0
  }
1014
1015
0
  xml.EndElement(); // buildTargets
1016
0
  xml.EndElement(); // storageModule
1017
1018
0
  cmExtraEclipseCDT4Generator::AppendStorageScanners(xml, *mf);
1019
1020
  // noqa: spellcheck off
1021
0
  xml.EndElement(); // cconfiguration
1022
  // noqa: spellcheck on
1023
0
  xml.EndElement(); // storageModule
1024
1025
0
  xml.StartElement("storageModule");
1026
0
  xml.Attribute("moduleId", "cdtBuildSystem");
1027
0
  xml.Attribute("version", "4.0.0");
1028
1029
0
  xml.StartElement("project");
1030
0
  xml.Attribute("id", std::string(lg->GetProjectName()) + ".null.1");
1031
0
  xml.Attribute("name", lg->GetProjectName());
1032
0
  xml.EndElement(); // project
1033
1034
0
  xml.EndElement(); // storageModule
1035
1036
  // Append additional cproject contents without applying any XML formatting
1037
0
  if (cmValue extraCProjectContents =
1038
0
        mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_CPROJECT_CONTENTS")) {
1039
0
    fout << *extraCProjectContents;
1040
0
  }
1041
1042
0
  xml.EndElement(); // cproject
1043
0
}
1044
1045
std::string cmExtraEclipseCDT4Generator::GetEclipsePath(
1046
  std::string const& path)
1047
0
{
1048
#if defined(__CYGWIN__)
1049
  std::string cmd = "cygpath -m " + path;
1050
  std::string out;
1051
  if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out)) {
1052
    return path;
1053
  } else {
1054
    out.erase(out.find_last_of('\n'));
1055
    return out;
1056
  }
1057
#else
1058
0
  return path;
1059
0
#endif
1060
0
}
1061
1062
std::string cmExtraEclipseCDT4Generator::GetPathBasename(
1063
  std::string const& path)
1064
0
{
1065
0
  std::string outputBasename = path;
1066
0
  while (!outputBasename.empty() &&
1067
0
         (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
1068
0
    outputBasename.resize(outputBasename.size() - 1);
1069
0
  }
1070
0
  std::string::size_type loc = outputBasename.find_last_of("/\\");
1071
0
  if (loc != std::string::npos) {
1072
0
    outputBasename = outputBasename.substr(loc + 1);
1073
0
  }
1074
1075
0
  return outputBasename;
1076
0
}
1077
1078
std::string cmExtraEclipseCDT4Generator::GenerateProjectName(
1079
  std::string const& name, std::string const& type, std::string const& path)
1080
0
{
1081
0
  return cmStrCat(name, (type.empty() ? ""_s : "-"_s), type, '@', path);
1082
0
}
1083
1084
// Helper functions
1085
void cmExtraEclipseCDT4Generator::AppendStorageScanners(
1086
  cmXMLWriter& xml, cmMakefile const& makefile)
1087
0
{
1088
  // we need the "make" and the C (or C++) compiler which are used, Alex
1089
0
  std::string const& make =
1090
0
    makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
1091
0
  std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
1092
0
  std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
1093
0
  if (compiler.empty()) {
1094
0
    compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
1095
0
    arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
1096
0
  }
1097
0
  if (compiler.empty()) // Hmm, what to do now ?
1098
0
  {
1099
0
    compiler = "gcc";
1100
0
  }
1101
1102
  // the following right now hardcodes gcc behavior :-/
1103
0
  std::string compilerArgs =
1104
0
    "-E -P -v -dD ${plugin_state_location}/${specs_file}";
1105
0
  if (!arg1.empty()) {
1106
0
    arg1 += " ";
1107
0
    compilerArgs = arg1 + compilerArgs;
1108
0
  }
1109
1110
0
  xml.StartElement("storageModule");
1111
0
  xml.Attribute("moduleId", "scannerConfiguration");
1112
1113
0
  xml.StartElement("autodiscovery");
1114
0
  xml.Attribute("enabled", "true");
1115
0
  xml.Attribute("problemReportingEnabled", "true");
1116
0
  xml.Attribute("selectedProfileId",
1117
0
                "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile");
1118
0
  xml.EndElement(); // autodiscovery
1119
1120
0
  cmExtraEclipseCDT4Generator::AppendScannerProfile(
1121
0
    xml, "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile", true,
1122
0
    "", true, "specsFile", compilerArgs, compiler, true, true);
1123
0
  cmExtraEclipseCDT4Generator::AppendScannerProfile(
1124
0
    xml, "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile", true, "",
1125
0
    true, "makefileGenerator", "-f ${project_name}_scd.mk", make, true, true);
1126
1127
0
  xml.EndElement(); // storageModule
1128
0
}
1129
1130
// The prefix is prepended before the actual name of the target. The purpose
1131
// of that is to sort the targets in the view of Eclipse, so that at first
1132
// the global/utility/all/clean targets appear ": ", then the executable
1133
// targets "[exe] ", then the libraries "[lib]", then the rules for the
1134
// object files "[obj]", then for preprocessing only "[pre] " and
1135
// finally the assembly files "[to asm] ". Note the "to" in "to asm",
1136
// without it, "asm" would be the first targets in the list, with the "to"
1137
// they are the last targets, which makes more sense.
1138
void cmExtraEclipseCDT4Generator::AppendTarget(
1139
  cmXMLWriter& xml, std::string const& target, std::string const& make,
1140
  std::string const& makeArgs, std::string const& path, char const* prefix,
1141
  char const* makeTarget)
1142
0
{
1143
0
  xml.StartElement("target");
1144
0
  xml.Attribute("name", prefix + target);
1145
0
  xml.Attribute("path", path);
1146
0
  xml.Attribute("targetID", "org.eclipse.cdt.make.MakeTargetBuilder");
1147
0
  xml.Element("buildCommand",
1148
0
              cmExtraEclipseCDT4Generator::GetEclipsePath(make));
1149
0
  xml.Element("buildArguments", makeArgs);
1150
0
  xml.Element("buildTarget", makeTarget ? makeTarget : target.c_str());
1151
0
  xml.Element("stopOnError", "true");
1152
0
  xml.Element("useDefaultCommand", "false");
1153
0
  xml.EndElement();
1154
0
}
1155
1156
void cmExtraEclipseCDT4Generator::AppendScannerProfile(
1157
  cmXMLWriter& xml, std::string const& profileID, bool openActionEnabled,
1158
  std::string const& openActionFilePath, bool pParserEnabled,
1159
  std::string const& scannerInfoProviderID,
1160
  std::string const& runActionArguments, std::string const& runActionCommand,
1161
  bool runActionUseDefault, bool sipParserEnabled)
1162
0
{
1163
0
  xml.StartElement("profile");
1164
0
  xml.Attribute("id", profileID);
1165
1166
0
  xml.StartElement("buildOutputProvider");
1167
0
  xml.StartElement("openAction");
1168
0
  xml.Attribute("enabled", openActionEnabled ? "true" : "false");
1169
0
  xml.Attribute("filePath", openActionFilePath);
1170
0
  xml.EndElement(); // openAction
1171
0
  xml.StartElement("parser");
1172
0
  xml.Attribute("enabled", pParserEnabled ? "true" : "false");
1173
0
  xml.EndElement(); // parser
1174
0
  xml.EndElement(); // buildOutputProvider
1175
1176
0
  xml.StartElement("scannerInfoProvider");
1177
0
  xml.Attribute("id", scannerInfoProviderID);
1178
0
  xml.StartElement("runAction");
1179
0
  xml.Attribute("arguments", runActionArguments);
1180
0
  xml.Attribute("command", runActionCommand);
1181
0
  xml.Attribute("useDefault", runActionUseDefault ? "true" : "false");
1182
0
  xml.EndElement(); // runAction
1183
0
  xml.StartElement("parser");
1184
0
  xml.Attribute("enabled", sipParserEnabled ? "true" : "false");
1185
0
  xml.EndElement(); // parser
1186
0
  xml.EndElement(); // scannerInfoProvider
1187
1188
0
  xml.EndElement(); // profile
1189
0
}
1190
1191
void cmExtraEclipseCDT4Generator::AppendLinkedResource(cmXMLWriter& xml,
1192
                                                       std::string const& name,
1193
                                                       std::string const& path,
1194
                                                       LinkType linkType)
1195
0
{
1196
0
  char const* locationTag = "location";
1197
0
  int typeTag = 2;
1198
0
  if (linkType == VirtualFolder) // ... and not a linked folder
1199
0
  {
1200
0
    locationTag = "locationURI";
1201
0
  }
1202
0
  if (linkType == LinkToFile) {
1203
0
    typeTag = 1;
1204
0
  }
1205
1206
0
  xml.StartElement("link");
1207
0
  xml.Element("name", name);
1208
0
  xml.Element("type", typeTag);
1209
0
  xml.Element(locationTag, path);
1210
0
  xml.EndElement();
1211
0
}