summaryrefslogtreecommitdiff
path: root/Source/cmLocalKdevelopGenerator.cxx
blob: 4070f22a4658aa56386c69334527206e7b43dcf2 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  Copyright (c) 2004 Alexander Neundorf, neundorf@kde.org. All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmGlobalGenerator.h"
#include "cmLocalKdevelopGenerator.h"
#include "cmMakefile.h"
#include "cmSystemTools.h"
#include "cmSourceFile.h"
#include "cmMakeDepend.h"
#include "cmCacheManager.h"
#include "cmGeneratedFileStream.h"
#include "cmake.h"
#include <cmsys/RegularExpression.hxx>


cmLocalKdevelopGenerator::cmLocalKdevelopGenerator()
  :cmLocalUnixMakefileGenerator()
{
}

cmLocalKdevelopGenerator::~cmLocalKdevelopGenerator()
{
}


void cmLocalKdevelopGenerator::Generate(bool fromTheTop)
{
  cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  if ( m_GlobalGenerator->GetCMakeInstance()->GetLocal() )
    {
    return;
    }

  bool containsTargets=false;
  std::string executable;
  cmTargets& targets=m_Makefile->GetTargets();
  for (cmTargets::const_iterator ti = targets.begin(); ti != targets.end(); ti++)
    {
    switch (ti->second.GetType())
      {
      case cmTarget::EXECUTABLE:
        executable=ti->first;
      case cmTarget::STATIC_LIBRARY:
      case cmTarget::SHARED_LIBRARY:
      case cmTarget::MODULE_LIBRARY:
      case cmTarget::UTILITY:
        containsTargets=true;
        break;
      case cmTarget::INSTALL_FILES:
      case cmTarget::INSTALL_PROGRAMS:
      default:
        break;
      }
    }

  if (containsTargets)
    {
    std::string filelistDir=m_Makefile->GetHomeOutputDirectory();
    //build the project name by taking the subdir
    std::vector<cmLocalGenerator *> lgs;
    m_GlobalGenerator->GetLocalGenerators(lgs);
    std::string projectName=lgs[0]->GetMakefile()->GetProjectName();

    std::string cmakeFilePattern("CMakeLists.txt;*.cmake;");

    if (!this->CreateFilelistFile(filelistDir, projectName, cmakeFilePattern))
      {
      return;
      }

    this->CreateProjectFile(filelistDir, projectName, executable, cmakeFilePattern);
    }
}

void cmLocalKdevelopGenerator::CreateProjectFile(const std::string& dir,
                                                 const std::string& projectname, 
                                                 const std::string& executable,
                                                 const std::string& cmakeFilePattern)
{
  std::string filename=m_Makefile->GetStartOutputDirectory();
  filename+="/";
  filename+=projectname+".kdevelop";

  if (cmSystemTools::FileExists(filename.c_str()))
    {
    this->MergeProjectFiles(dir, filename, executable, cmakeFilePattern);
    }
  else
    {
    this->CreateNewProjectFile(dir, filename, executable, cmakeFilePattern);
    }
   
}

void cmLocalKdevelopGenerator::MergeProjectFiles(const std::string& dir, 
                                                 const std::string& filename, 
                                                 const std::string& executable, 
                                                 const std::string& cmakeFilePattern)
{
  std::ifstream oldProjectFile(filename.c_str());
  if (!oldProjectFile)
    {
    this->CreateNewProjectFile(dir, filename, executable, cmakeFilePattern);
    return;
    }

  std::string tmp;
  std::vector<std::string> lines;
  while (cmSystemTools::GetLineFromStream(oldProjectFile, tmp))
    {
    lines.push_back(tmp);
    }
  oldProjectFile.close();

  cmGeneratedFileStream tempFile(filename.c_str());
  tempFile.SetAlwaysCopy(true);
  std::ostream&  fout = tempFile.GetStream();
  if(!fout)
    {
    cmSystemTools::Error("Error can not open for write: ", filename.c_str());
    return;
    }

  for (std::vector<std::string>::const_iterator it=lines.begin(); 
       it!=lines.end(); it++)
    {
    const char* line=(*it).c_str();

    if ((strstr(line, "<projectdirectory>")!=0)
        || (strstr(line, "<projectmanagement>")!=0)
        || (strstr(line, "<absoluteprojectpath>")!=0)
        || (strstr(line, "<buildtool>")!=0)
        || (strstr(line, "<builddir>")!=0))
      {
      continue;
      }

    fout<<*it<<"\n";

    if (strstr(line, "<general>"))
      {
      fout<<"  <projectmanagement>KDevCustomProject</projectmanagement>\n";
      fout<<"  <projectdirectory>"<<dir.c_str()<<"</projectdirectory>\n";   //this one is important
      fout<<"  <absoluteprojectpath>true</absoluteprojectpath>\n";          //and this one
      }

    if (strstr(line, "<build>"))
      {
      fout<<"      <buildtool>make</buildtool>\n";                                        //this one is important
      fout<<"      <builddir>"<<m_Makefile->GetStartOutputDirectory()<<"</builddir>\n";   //and this one

      }
    }
}

void cmLocalKdevelopGenerator::CreateNewProjectFile(const std::string& dir, const std::string& filename,
                                                    const std::string& executable, const std::string& cmakeFilePattern)
{

  cmGeneratedFileStream tempFile(filename.c_str());
  tempFile.SetAlwaysCopy(true);

  std::ostream&  fout = tempFile.GetStream();
  if(!fout)
    {
    cmSystemTools::Error("Error can not open for write: ", filename.c_str());
    return;
    }

  fout<<"<?xml version = '1.0'?>\n";
  fout<<"<kdevelop>\n";
  fout<<"  <general>\n";
  fout<<"  <author></author>\n";
  fout<<"  <email></email>\n";
  fout<<"  <version>$VERSION$</version>\n";
  fout<<"  <projectmanagement>KDevCustomProject</projectmanagement>\n";
  fout<<"  <primarylanguage>C++</primarylanguage>\n";
  fout<<"  <ignoreparts/>\n";
  fout<<"  <projectdirectory>"<<dir.c_str()<<"</projectdirectory>\n";   //this one is important
  fout<<"  <absoluteprojectpath>true</absoluteprojectpath>\n";          //and this one
  fout<<"  <secondaryLanguages>\n";
  fout<<"     <language>C</language>\n";
  fout<<"  </secondaryLanguages>\n";
  fout<<"  </general>\n";
  fout<<"  <kdevcustomproject>\n";
  fout<<"    <run>\n";
  fout<<"      <mainprogram>"<<m_Makefile->GetStartOutputDirectory()<<"/"<<executable.c_str()<<"</mainprogram>\n";
  fout<<"      <directoryradio>custom</directoryradio>\n";
  fout<<"      <customdirectory>/</customdirectory>\n";
  fout<<"      <programargs></programargs>\n";
  fout<<"      <terminal>false</terminal>\n";
  fout<<"      <autocompile>true</autocompile>\n";
  fout<<"      <envvars/>\n";
  fout<<"    </run>\n";
  fout<<"    <build>\n";
  fout<<"      <buildtool>make</buildtool>\n";                                        //this one is important
  fout<<"      <builddir>"<<m_Makefile->GetStartOutputDirectory()<<"</builddir>\n";   //and this one
  fout<<"    </build>\n";
  fout<<"    <make>\n";
  fout<<"      <abortonerror>false</abortonerror>\n";
  fout<<"      <numberofjobs>1</numberofjobs>\n";
  fout<<"      <dontact>false</dontact>\n";
  fout<<"      <makebin></makebin>\n";
  fout<<"      <selectedenvironment>default</selectedenvironment>\n";
  fout<<"      <environments>\n";
  fout<<"        <default/>\n";
  fout<<"      </environments>\n";
  fout<<"    </make>\n";
  fout<<"  </kdevcustomproject>\n";
  fout<<"  <kdevfilecreate>\n";
  fout<<"    <filetypes/>\n";
  fout<<"    <useglobaltypes>\n";
  fout<<"      <type ext=\"ui\" />\n";
  fout<<"      <type ext=\"cpp\" />\n";
  fout<<"      <type ext=\"h\" />\n";
  fout<<"    </useglobaltypes>\n";
  fout<<"  </kdevfilecreate>\n";
  fout<<"  <kdevdoctreeview>\n";
  fout<<"    <projectdoc>\n";
  fout<<"      <userdocDir>html/</userdocDir>\n";
  fout<<"      <apidocDir>html/</apidocDir>\n";
  fout<<"    </projectdoc>\n";
  fout<<"    <ignoreqt_xml/>\n";
  fout<<"    <ignoredoxygen/>\n";
  fout<<"    <ignorekdocs/>\n";
  fout<<"    <ignoretocs/>\n";
  fout<<"    <ignoredevhelp/>\n";
  fout<<"  </kdevdoctreeview>\n";
  fout<<"  <cppsupportpart>\n";
  fout<<"    <filetemplates>\n";
  fout<<"      <interfacesuffix>.h</interfacesuffix>\n";
  fout<<"      <implementationsuffix>.cpp</implementationsuffix>\n";
  fout<<"    </filetemplates>\n";
  fout<<"  </cppsupportpart>\n";
  fout<<"  <kdevcppsupport>\n";
  fout<<"    <codecompletion>\n";
  fout<<"      <includeGlobalFunctions>true</includeGlobalFunctions>\n";
  fout<<"      <includeTypes>true</includeTypes>\n";
  fout<<"      <includeEnums>true</includeEnums>\n";
  fout<<"      <includeTypedefs>false</includeTypedefs>\n";
  fout<<"      <automaticCodeCompletion>true</automaticCodeCompletion>\n";
  fout<<"      <automaticArgumentsHint>true</automaticArgumentsHint>\n";
  fout<<"      <automaticHeaderCompletion>true</automaticHeaderCompletion>\n";
  fout<<"      <codeCompletionDelay>250</codeCompletionDelay>\n";
  fout<<"      <argumentsHintDelay>400</argumentsHintDelay>\n";
  fout<<"      <headerCompletionDelay>250</headerCompletionDelay>\n";
  fout<<"    </codecompletion>\n";
  fout<<"    <references/>\n";
  fout<<"  </kdevcppsupport>\n";
  fout<<"  <kdevfileview>\n";
  fout<<"    <groups>\n";
  fout<<"      <group pattern=\""<<cmakeFilePattern.c_str()<<"\" name=\"CMake\" />\n";
  fout<<"      <group pattern=\"*.h;*.hxx\" name=\"Header\" />\n";
  fout<<"      <group pattern=\"*.cpp;*.c;*.C;*.cxx\" name=\"Sources\" />\n";
  fout<<"      <group pattern=\"*.ui\" name=\"Qt Designer files\" />\n";
  fout<<"      <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  fout<<"    </groups>\n";
  fout<<"    <tree>\n";
  fout<<"      <hidepatterns>*.o,*.lo,CVS,*~,cmake*</hidepatterns>\n";
  fout<<"      <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  fout<<"    </tree>\n";
  fout<<"  </kdevfileview>\n";
  fout<<"</kdevelop>\n";
}

bool cmLocalKdevelopGenerator::CreateFilelistFile(const std::string& _dir, 
                                                  const std::string& projectname,
                                                  std::string& cmakeFilePattern)
{
  std::string filelistDir=_dir+"/";
  std::string filename=filelistDir+projectname+".kdevelop.filelist";

  std::set<cmStdString> files;

  //get all cmake files
  std::string tmp;
  const std::vector<std::string>& listFiles=m_Makefile->GetListFiles();
  for (std::vector<std::string>::const_iterator it=listFiles.begin(); it!=listFiles.end(); it++)
    {
    tmp=*it;
    cmSystemTools::ReplaceString(tmp, filelistDir.c_str(), "");
    if (tmp[0]!='/')
      {
      files.insert(tmp);
      tmp=cmSystemTools::GetFilenameName(tmp);
      //add all files which dont match the default */CMakeLists.txt;*cmake; to the file pattern
      if ((tmp!="CMakeLists.txt")
          && (strstr(tmp.c_str(), ".cmake")==0))
        {
        cmakeFilePattern+=tmp+";";
        }
      }
    }

  //get all sources
  cmTargets& targets=m_Makefile->GetTargets();
  for (cmTargets::const_iterator ti = targets.begin(); ti != targets.end(); ti++)
    {
    const std::vector<cmSourceFile*>& sources=ti->second.GetSourceFiles();
    for (std::vector<cmSourceFile*>::const_iterator it=sources.begin();
      it!=sources.end(); it++)
      {
      files.insert((*it)->GetFullPath());
      }
    const std::vector<std::string>& listFiles=m_Makefile->GetListFiles();
    for (std::vector<std::string>::const_iterator it=listFiles.begin();
      it!=listFiles.end(); it++)
      {
      files.insert(it->c_str());
      }
    }


  //check if the output file already exists and read it
  //insert all files which exist into the set of files
  std::ifstream oldFilelist(filename.c_str());
  if (oldFilelist)
    {
    while (cmSystemTools::GetLineFromStream(oldFilelist, tmp))
      {
      if (tmp[0]=='/')
        {
        continue;
        }
      std::string completePath=filelistDir+tmp;
      if (cmSystemTools::FileExists(completePath.c_str()))
        {
        files.insert(tmp);
        }
      }
    oldFilelist.close();
    }
   

  cmGeneratedFileStream tempFile(filename.c_str());
  tempFile.SetAlwaysCopy(true);
  std::ostream&  fout = tempFile.GetStream();
  if(!fout)
    {
    cmSystemTools::Error("Error can not open for write: ", filename.c_str());
    return false;
    }
   
  for (std::set<cmStdString>::const_iterator it=files.begin(); it!=files.end(); it++)
    {
    fout<< cmSystemTools::RelativePath(_dir.c_str(), it->c_str())<<"\n";
    }
  return true;
}