Coverage Report

Created: 2026-07-14 07:09

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