summaryrefslogtreecommitdiffstats
path: root/Source/cmGlobalUnixMakefileGenerator3.cxx
blob: 4a0596c696ed67ef5bdebd407e44c0daa3074f51 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*=========================================================================

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

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  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 "cmGlobalUnixMakefileGenerator3.h"
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
#include "cmake.h"
#include "cmGeneratedFileStream.h"

cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3()
{
  // This type of makefile always requires unix style paths
  m_ForceUnixPaths = true;
  m_FindMakeProgramFile = "CMakeUnixFindMake.cmake";
}

void cmGlobalUnixMakefileGenerator3
::EnableLanguage(std::vector<std::string>const& languages, cmMakefile *mf)
{
  mf->AddDefinition("CMAKE_CFG_INTDIR",".");
  this->cmGlobalGenerator::EnableLanguage(languages, mf);
  std::string path;
  for(std::vector<std::string>::const_iterator l = languages.begin();
      l != languages.end(); ++l)
    {
    const char* lang = l->c_str();
    std::string langComp = "CMAKE_";
    langComp += lang;
    langComp += "_COMPILER";
    
    if(!mf->GetDefinition(langComp.c_str()))
      {
      cmSystemTools::Error(langComp.c_str(), " not set, after EnableLanguage");
      continue;
      }
    const char* cc = mf->GetRequiredDefinition(langComp.c_str());
    path = cmSystemTools::FindProgram(cc);
    if(path.size() == 0)
      {
      std::string message = "your ";
      message += lang;
      message += " compiler: ";
      if(cc)
        {
        message +=  cc;
        }
      else
        {
        message += "(NULL)";
        }
      message += " was not found in your path.   "
        "For CMake to correctly use try compile commands, the compiler must "
        "be in your path.   Please add the compiler to your PATH environment,"
        " and re-run CMake.";
        cmSystemTools::Error(message.c_str());
      }
    }
}

///! Create a local generator appropriate to this Global Generator
cmLocalGenerator *cmGlobalUnixMakefileGenerator3::CreateLocalGenerator()
{
  cmLocalGenerator* lg = new cmLocalUnixMakefileGenerator3;
  lg->SetGlobalGenerator(this);
  return lg;
}

//----------------------------------------------------------------------------
void cmGlobalUnixMakefileGenerator3::GetDocumentation(cmDocumentationEntry& entry) const
{
  entry.name = this->GetName();
  entry.brief = "Generates standard UNIX makefiles.";
  entry.full =
    "A hierarchy of UNIX makefiles is generated into the build tree.  Any "
    "standard UNIX-style make program can build the project through the "
    "default make target.  A \"make install\" target is also provided.";
}

//----------------------------------------------------------------------------
void cmGlobalUnixMakefileGenerator3::Generate() 
{
  // first do superclass method
  this->cmGlobalGenerator::Generate();

  // write the main makefile
  this->WriteMainMakefile();
  this->WriteMainCMakefile();

  // now write the support Makefiles
  this->WriteDependMakefile();
  this->WriteBuildMakefile();
  this->WriteCleanMakefile();
}

void cmGlobalUnixMakefileGenerator3::WriteMainMakefile()
{
  // Open the output file.  This should not be copy-if-different
  // because the check-build-system step compares the makefile time to
  // see if the build system must be regenerated.
  std::string makefileName = 
    this->GetCMakeInstance()->GetHomeOutputDirectory();
  makefileName += "/Makefile";
  cmGeneratedFileStream makefileStream(makefileName.c_str());
  if(!makefileStream)
    {
    return;
    }
 
  // get a local generator for some useful methods
  cmLocalUnixMakefileGenerator3 *lg = 
    static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
    
  // Write the do not edit header.
  lg->WriteDisclaimer(makefileStream);
  
  // Write the main entry point target.  This must be the VERY first
  // target so that make with no arguments will run it.
  // Just depend on the all target to drive the build.
  std::vector<std::string> depends;
  std::vector<std::string> no_commands;
  depends.push_back("all");

  // Write the rule.
  lg->WriteMakeRule(makefileStream,
                    "Default target executed when no arguments are "
                    "given to make.",
                    "default_target",
                    depends,
                    no_commands);

  lg->WriteMakeVariables(makefileStream);
  
  lg->WriteSpecialTargetsTop(makefileStream);

  this->WriteAllRules(lg,makefileStream);
  
  // write the target convenience rules
  unsigned int i;
  for (i = 0; i < m_LocalGenerators.size(); ++i)
    {
    lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    this->WriteConvenienceRules(makefileStream,lg);
    }

  lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  lg->WriteSpecialTargetsBottom(makefileStream);
}


//----------------------------------------------------------------------------
void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
{
  // Open the output file.  This should not be copy-if-different
  // because the check-build-system step compares the makefile time to
  // see if the build system must be regenerated.
  std::string cmakefileName = 
    this->GetCMakeInstance()->GetHomeOutputDirectory();
  cmakefileName += "/Makefile.cmake";
  cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  if(!cmakefileStream)
    {
    return;
    }

  std::string makefileName = 
    this->GetCMakeInstance()->GetHomeOutputDirectory();
  makefileName += "/Makefile";
  
  // get a local generator for some useful methods
  cmLocalUnixMakefileGenerator3 *lg = 
    static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
    
  // Write the do not edit header.
  lg->WriteDisclaimer(cmakefileStream);

  // Save the generator name
  cmakefileStream
    << "# The generator used is:\n"
    << "SET(CMAKE_DEPENDS_GENERATOR \"" << this->GetName() << "\")\n\n";

  // for each cmMakefile get its list of dependencies
  std::vector<std::string> lfiles;
  for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
    {
    lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
  
    // Get the list of files contributing to this generation step.
    lfiles.insert(lfiles.end(),lg->GetMakefile()->GetListFiles().begin(),
                  lg->GetMakefile()->GetListFiles().end());
    }
  // Sort the list and remove duplicates.
  std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  std::vector<std::string>::iterator new_end = 
    std::unique(lfiles.begin(),lfiles.end());
  lfiles.erase(new_end, lfiles.end());

  // reset lg to the first makefile
  lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);

  // Build the path to the cache file.
  std::string cache = this->GetCMakeInstance()->GetHomeOutputDirectory();
  cache += "/CMakeCache.txt";

  // Save the list to the cmake file.
  cmakefileStream
    << "# The top level Makefile was generated from the following files:\n"
    << "SET(CMAKE_MAKEFILE_DEPENDS\n"
    << "  \"" << lg->ConvertToRelativePath(cache.c_str()).c_str() << "\"\n";
  for(std::vector<std::string>::const_iterator i = lfiles.begin();
      i !=  lfiles.end(); ++i)
    {
    cmakefileStream
      << "  \"" << lg->ConvertToRelativePath(i->c_str()).c_str()
      << "\"\n";
    }
  cmakefileStream
    << "  )\n\n";

  // Build the path to the cache check file.
  std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  check += "/cmake.check_cache";

  // Set the corresponding makefile in the cmake file.
  cmakefileStream
    << "# The corresponding makefile is:\n"
    << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
    << "  \"" << lg->ConvertToRelativePath(makefileName.c_str()).c_str() << "\"\n"
    << "  \"" << lg->ConvertToRelativePath(check.c_str()).c_str() << "\"\n";

  // add in all the directory information files
  std::string tmpStr;
  for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
    {
    lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    tmpStr = lg->GetMakefile()->GetStartOutputDirectory();
    tmpStr += "/CMakeDirectoryInformation.cmake";
    cmakefileStream << "  \"" << 
      lg->Convert(tmpStr.c_str(),cmLocalGenerator::HOME_OUTPUT).c_str() << "\"\n";
    }
  cmakefileStream << "  )\n\n";

  this->WriteMainCMakefileLanguageRules(cmakefileStream);
}
  
void cmGlobalUnixMakefileGenerator3
::WriteMainCMakefileLanguageRules(cmGeneratedFileStream& cmakefileStream)
{
  cmLocalUnixMakefileGenerator3 *lg;

  // now write all the language stuff
  // Set the set of files to check for dependency integrity.
  // loop over all of the local generators to collect this
  std::set<cmStdString> checkSetLangs;
  for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
    {
    lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>& checkSet = 
      lg->GetIntegrityCheckSet();
    for(std::map<cmStdString, 
          cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
          l = checkSet.begin(); l != checkSet.end(); ++l)
      {
      checkSetLangs.insert(l->first);
      }
    }
  
  // list the languages
  cmakefileStream
    << "# The set of files whose dependency integrity should be checked:\n";
  cmakefileStream
    << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  for(std::set<cmStdString>::iterator
        l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
    {
    cmakefileStream << "  \"" << l->c_str() << "\"\n";
    }
  cmakefileStream << "  )\n";
  
  // now list the files for each language
  for(std::set<cmStdString>::iterator
        l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
    {
    cmakefileStream
      << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
    // now for each local gen get the checkset
    for (unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
      {
      lg = static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
      // get the check set for this local gen and language
      cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet = 
        lg->GetIntegrityCheckSet()[*l];
      // for each file
      for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator csIter = 
            iCheckSet.begin();
          csIter != iCheckSet.end(); ++csIter)
        {
        cmakefileStream << "  \"" << 
          lg->Convert(csIter->c_str(),cmLocalGenerator::HOME_OUTPUT).c_str() << "\"\n";
        }
      }
    cmakefileStream << "  )\n";
    }
}

void cmGlobalUnixMakefileGenerator3::WriteDependMakefile()
{
  unsigned int i;
  
  // Open the output file.  This should not be copy-if-different
  // because the check-build-system step compares the makefile time to
  // see if the build system must be regenerated.
  std::string makefileName = this->GetCMakeInstance()->GetHomeOutputDirectory();
  makefileName += "/depend.make";
  cmGeneratedFileStream makefileStream(makefileName.c_str());
  if(!makefileStream)
    {
    return;
    }
  
  // get a local generator for some useful methods
  cmLocalUnixMakefileGenerator3 *lg = 
    static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  
  // Write the do not edit header.
  lg->WriteDisclaimer(makefileStream);
  //lg->WriteMakeVariables(makefileStream);

  // add the generic dependency
  std::vector<std::string> depends;
  std::vector<std::string> no_commands;
  lg->WriteMakeRule(makefileStream, 0, "depend", depends, no_commands);

  // include the build rules
  makefileStream
    << "# Include make rules for build targets\n";
  makefileStream
    << lg->GetIncludeDirective() << " "
    << lg->ConvertToOutputForExisting("build.make").c_str()
    << "\n\n";

  // include all the target depends
  for (i = 0; i < m_LocalGenerators.size(); ++i)
    {
    cmLocalUnixMakefileGenerator3 *lg2 = 
      static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    // are any parents excluded
    bool exclude = false;
    cmLocalGenerator *lg3 = lg2;
    while (lg3)
      {
      if (lg3->GetExcludeAll())
        {
        exclude = true;
        break;
        }
      lg3 = lg3->GetParent();
      }
    lg2->WriteMainTargetIncludes(makefileStream,"depend.make","depend");
    lg2->WriteMainTargetRules(makefileStream,"depend.make","depend",!exclude);
    }
}

void cmGlobalUnixMakefileGenerator3::WriteBuildMakefile()
{
  unsigned int i;
  
  // Open the output file.  This should not be copy-if-different
  // because the check-build-system step compares the makefile time to
  // see if the build system must be regenerated.
  std::string makefileName = this->GetCMakeInstance()->GetHomeOutputDirectory();
  makefileName += "/build.make";
  cmGeneratedFileStream makefileStream(makefileName.c_str());
  if(!makefileStream)
    {
    return;
    }
  
  // get a local generator for some useful methods
  cmLocalUnixMakefileGenerator3 *lg = 
    static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  
  // Write the do not edit header.
  lg->WriteDisclaimer(makefileStream);
  lg->WriteMakeVariables(makefileStream);

  // add the generic dependency
  std::vector<std::string> depends;
  std::vector<std::string> no_commands;
  lg->WriteMakeRule(makefileStream, 0, "build", depends, no_commands);

  // include all the target depends
  for (i = 0; i < m_LocalGenerators.size(); ++i)
    {
    cmLocalUnixMakefileGenerator3 *lg2 = 
      static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    // are any parents excluded
    bool exclude = false;
    cmLocalGenerator *lg3 = lg2;
    while (lg3)
      {
      if (lg3->GetExcludeAll())
        {
        exclude = true;
        break;
        }
      lg3 = lg3->GetParent();
      }
    lg2->WriteMainTargetIncludes(makefileStream,"build.make","build");
    lg2->WriteMainTargetRules(makefileStream,"build.make","build",!exclude);
    lg2->WriteMainTargetRules(makefileStream,"build.make","requires",!exclude);
    }
}

void cmGlobalUnixMakefileGenerator3::WriteCleanMakefile()
{
  unsigned int i;
  
  // Open the output file.  This should not be copy-if-different
  // because the check-build-system step compares the makefile time to
  // see if the build system must be regenerated.
  std::string makefileName = this->GetCMakeInstance()->GetHomeOutputDirectory();
  makefileName += "/clean.make";
  cmGeneratedFileStream makefileStream(makefileName.c_str());
  if(!makefileStream)
    {
    return;
    }
  
  // get a local generator for some useful methods
  cmLocalUnixMakefileGenerator3 *lg = 
    static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[0]);
  
  // Write the do not edit header.
  lg->WriteDisclaimer(makefileStream);
  lg->WriteMakeVariables(makefileStream);

  // add the generic dependency
  std::vector<std::string> depends;
  std::vector<std::string> no_commands;
  lg->WriteMakeRule(makefileStream, 0, "clean", depends, no_commands);

  // include all the target depends
  for (i = 0; i < m_LocalGenerators.size(); ++i)
    {
    cmLocalUnixMakefileGenerator3 *lg2 = 
      static_cast<cmLocalUnixMakefileGenerator3 *>(m_LocalGenerators[i]);
    lg2->WriteMainTargetIncludes(makefileStream,"clean.make","clean");
    lg2->WriteMainTargetRules(makefileStream,"clean.make","clean",true);
    // add the directory based rules
    lg2->WriteLocalCleanRule(makefileStream);
    }
  
}

//----------------------------------------------------------------------------
void cmGlobalUnixMakefileGenerator3
::WriteAllRules(cmLocalUnixMakefileGenerator3 *lg, 
                std::ostream& makefileStream)
{
  // Write section header.
  lg->WriteDivider(makefileStream);
  makefileStream
    << "# Rules to build dependencies and targets.\n"
    << "\n";

  std::vector<std::string> depends;
  std::vector<std::string> commands;

  // Check the build system in this directory.
  depends.push_back("cmake_check_build_system");

  commands.push_back(lg->GetRecursiveMakeCall("depend.make",0));
  commands.push_back(lg->GetRecursiveMakeCall("depend.make","requires"));
  commands.push_back(lg->GetRecursiveMakeCall("build.make",0));

  // Write the rule.
  lg->WriteMakeRule(makefileStream, "The main all target", "all", depends, commands);

  // write the clean
  commands.clear();
  commands.push_back(lg->GetRecursiveMakeCall("clean.make",0));
  lg->WriteMakeRule(makefileStream, "default clean target", "clean", depends, commands);
}


//----------------------------------------------------------------------------
void
cmGlobalUnixMakefileGenerator3
::WriteConvenienceRules(std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3 *lg)
{
  std::vector<std::string> depends;  
  std::vector<std::string> tgt_depends;
  std::vector<std::string> commands;
  std::string localName;
  std::string makeTargetName;

  depends.push_back("cmake_check_build_system");
  if (lg->GetParent())
    {
    std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
    dir = lg->Convert(dir.c_str(),cmLocalGenerator::HOME_OUTPUT,cmLocalGenerator::MAKEFILE);
    localName = dir;
    localName += "/directory";
    
    // write the directory rule
    commands.clear();
    makeTargetName = dir;
    makeTargetName += "/depend";
    commands.push_back(lg->GetRecursiveMakeCall("depend.make",makeTargetName.c_str()));
    makeTargetName = dir;
    makeTargetName += "/requires";
    commands.push_back(lg->GetRecursiveMakeCall("depend.make",makeTargetName.c_str()));
    makeTargetName = dir;
    makeTargetName += "/build";
    commands.push_back(lg->GetRecursiveMakeCall("build.make",makeTargetName.c_str()));
    
    // Write the rule.
    lg->WriteMakeRule(ruleFileStream, "Convenience name for directory.",
                      localName.c_str(), depends, commands);
    }
  
  // for each target Generate the rule files for each target.
  const cmTargets& targets = lg->GetMakefile()->GetTargets();
  for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
    {
    if((t->second.GetType() == cmTarget::EXECUTABLE) ||
       (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
       (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
       (t->second.GetType() == cmTarget::MODULE_LIBRARY))
      {
      // Add a rule to build the target by name.
      localName = lg->GetRelativeTargetDirectory(t->second);

      commands.clear();
      makeTargetName = localName;
      makeTargetName += "/depend";
      commands.push_back(lg->GetRecursiveMakeCall("depend.make",makeTargetName.c_str()));
      makeTargetName = localName;
      makeTargetName += "/requires";
      commands.push_back(lg->GetRecursiveMakeCall("depend.make",makeTargetName.c_str()));
      makeTargetName = localName;
      makeTargetName += "/build";
      commands.push_back(lg->GetRecursiveMakeCall("build.make",makeTargetName.c_str()));

      // Write the rule.
      lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
                        localName.c_str(), depends, commands);
      
      // Add a target with the canonical name (no prefix, suffix or path).
      if(localName != t->second.GetName())
        {
        commands.clear();
        tgt_depends.clear();
        tgt_depends.push_back(localName);
        lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
                          t->second.GetName(), tgt_depends, commands);
        }
      }
    }
}