summaryrefslogtreecommitdiffstats
path: root/Source/cmExtraQbsGenerator.cxx
blob: c15f8dae75505f00160915054a44055185672f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "cmExtraQbsGenerator.h"

#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmGeneratedFileStream.h"
#include "cmSourceFile.h"

cmExtraQbsGenerator::cmExtraQbsGenerator()
{
#if defined(_WIN32)
  this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  this->SupportedGlobalGenerators.push_back("NMake Makefiles");
#endif
  this->SupportedGlobalGenerators.push_back("Ninja");
  this->SupportedGlobalGenerators.push_back("Unix Makefiles");
}

cmExtraQbsGenerator::~cmExtraQbsGenerator() {}

void cmExtraQbsGenerator::GetDocumentation(cmDocumentationEntry &entry,
                                           const std::string &) const
{
  entry.Name = this->GetName();
  entry.Brief = "Generates Qbs project files.";
}

void cmExtraQbsGenerator::Generate()
{
  for (std::map<std::string, std::vector<cmLocalGenerator *> >::const_iterator
       it = this->GlobalGenerator->GetProjectMap().begin();
       it != this->GlobalGenerator->GetProjectMap().end(); ++it)
    {
    // create a project file
    this->CreateProjectFile(it->first, it->second);
    }
}

void cmExtraQbsGenerator::CreateProjectFile(
        const std::string &name,
        const std::vector<cmLocalGenerator *> &lgs)
{
  const cmMakefile *mf = lgs[0]->GetMakefile();
  std::string outputDir = mf->GetCurrentBinaryDirectory();

  const std::string filename = outputDir + "/" + name + ".qbs";

  this->CreateNewProjectFile(name, lgs, filename);
}

void cmExtraQbsGenerator::CreateNewProjectFile(
    const std::string &projectName, const std::vector<cmLocalGenerator *> &lgs,
    const std::string &filename)
{
  cmGeneratedFileStream fout(filename.c_str());
  if (!fout)
    {
    return;
    }

  fout << "import qbs\n"
       << "import qbs.File\n\n"
       << "Project {\n"
       << "\tname:\"" << projectName << "\"\n";
  std::vector<cmLocalGenerator *>::const_iterator itr = lgs.begin();
  for (; itr != lgs.end(); ++itr)
    {
    cmLocalGenerator *lg = (*itr);
    this->AppendSubProject(fout, lg);
    }
  fout << "}\n";
}

void cmExtraQbsGenerator::AppendSubProject(cmGeneratedFileStream &fout,
                                           cmLocalGenerator *lg)
{
  const cmMakefile *mk = lg->GetMakefile();
  if (!mk || mk->GetTargets().size() == 0)
    {
    return;
    }

  const std::string &relativePath = cmSystemTools::RelativePath(
      mk->GetHomeDirectory(), mk->GetCurrentSourceDirectory());
  fout << "\tProject {\n"
       << "\t\tname:\"" << relativePath << "\"\n";
  this->AppendProduct(fout, lg);
  fout << "\t}\n";
}

void cmExtraQbsGenerator::AppendProduct(cmGeneratedFileStream &fout,
                                        cmLocalGenerator *lg)
{
  const cmMakefile *mk = lg->GetMakefile();
  const cmTargets &ts = mk->GetTargets();
  std::string cfg = mk->GetSafeDefinition("CMAKE_BUILD_TYPE");
  cmTargets::const_iterator itr = ts.begin();
  for (; itr != ts.end(); ++itr)
    {
    const cmTarget &t = itr->second;
    this->AppendTarget(fout, lg, t, cfg);
    }
}

void cmExtraQbsGenerator::AppendTarget(cmGeneratedFileStream &fout,
                                       cmLocalGenerator *lg, const cmTarget &t,
                                       const std::string &cfg)
{
  std::string type;
  bool isBuildable = true;
  switch (t.GetType())
    {
    case cmTarget::EXECUTABLE:
      type = "application";
      break;
    case cmTarget::SHARED_LIBRARY:
      type = "dynamiclibrary";
      break;
    case cmTarget::STATIC_LIBRARY:
      type = "staticlibrary";
      break;
    default:
      isBuildable = false;
      break;
    }

  if (type.empty())
    {
    fout << "\t\tProject {\n";
    }
  else
    {
    fout << "\t\tProduct {\n";
    fout << "\t\t\tdestinationDirectory: \"" << t.GetDirectory(cfg) << "\"\n";
    }
  fout << "\t\t\tname:\"" << t.GetName() << "\"\n";

  if (!type.empty())
    {
    fout << "\t\t\ttype: \"" << type << "\"\n";
    fout << "\t\t\ttargetName: \"" << t.GetName() << "\"\n";
    }

  if (isBuildable)
    {
    fout << "\t\t\tDepends { name: \"cpp\" }\n";
    cmGeneratorTarget *gt = this->GlobalGenerator->GetGeneratorTarget(&t);
    this->AppendSources(fout, gt, t, cfg);

    std::set<std::string> langs, incPaths, defs;
    t.GetLanguages(langs, cfg);
    for (std::set<std::string>::const_iterator lang = langs.begin();
         lang != langs.end();
         ++ lang)
      {
      const std::vector<std::string> &paths =
          gt->GetIncludeDirectories(cfg, *lang);
      std::copy(paths.begin(), paths.end(),
                std::inserter(incPaths, incPaths.end()));

      lg->AddCompileDefinitions(defs, &t, cfg, *lang);
      }
    this->AppendIncludePaths(fout, incPaths);
    this->AppendCompileDefinitions(fout, defs);
    }

  fout << "\t\t}\n";
}

void cmExtraQbsGenerator::AppendSources(cmGeneratedFileStream &fout,
                                        cmGeneratorTarget *gt,
                                        const cmTarget &t,
                                        const std::string &cfg)
{
  std::vector<cmSourceFile *> sources;
  gt->GetSourceFiles(sources, cfg);
  if (sources.empty())
    {
    return;
    }

  std::vector<cmSourceFile *> genSources;
  std::vector<cmSourceFile *>::const_iterator itr = sources.begin();
  fout << "\t\t\tfiles: [\n"
       << "\t\t\t\t\""
       << t.GetMakefile()->GetDefinition("CMAKE_CURRENT_LIST_FILE")
       << "\",\n";
  for (; itr != sources.end(); ++itr)
    {
    if (!(*itr)->GetPropertyAsBool("GENERATED"))
      {
      fout << "\t\t\t\t\"" << (*itr)->GetFullPath() << "\",\n";
      }
    else
      {
      genSources.push_back(*itr);
      }
    }
  fout << "\t\t\t]\n";

  if (!genSources.empty())
    {
    fout << "\t\t\tGroup {\n"
         << "\t\t\t\tname:\"Generated\"\n"
         << "\t\t\t\tfiles: [\n";
    itr = genSources.begin();
    std::string groupCondition;
    bool initialCondition = true;
    for (; itr != genSources.end(); ++itr)
      {
      const std::string &path = (*itr)->GetFullPath();
      fout << "\t\t\t\t\t\"" << path << "\",\n";
      if (initialCondition)
        {
        initialCondition = false;
        }
      else
        {
        groupCondition += "\t\t\t\t\t && ";
        }
      groupCondition += "File.exists(\"" + path + "\")\n";
      }
    fout << "\t\t\t\t]\n"
         << "\t\t\t\tcondition: " << groupCondition << "\t\t\t}\n";
  }
}

void cmExtraQbsGenerator::AppendIncludePaths(
    cmGeneratedFileStream &fout,
    const std::set<std::string> &paths)
{
  if (paths.empty())
    {
    return;
    }

  std::set<std::string>::const_iterator itr = paths.begin();
  fout << "\t\t\tcpp.includePaths: [\n";
  for (; itr != paths.end(); ++ itr)
    {
    fout << "\t\t\t\t\"" << (*itr) << "\",\n";
    }
  fout << "\t\t\t]\n";
}

void cmExtraQbsGenerator::AppendCompileDefinitions(
    cmGeneratedFileStream &fout,
    const std::set<std::string> &defs)
{
  if (defs.empty())
    {
    return;
    }

  std::set<std::string>::const_iterator itr = defs.begin();
  fout << "\t\t\tcpp.defines: [\n";
  for (; itr != defs.end(); ++ itr)
    {
    fout << "\t\t\t\t'" << (*itr) << "',\n";
    }
  fout << "\t\t\t]\n";
}