ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Source / cmLocalUnixMakefileGenerator3.cxx
blob406ffc48a149a135ce1bd7eb886e75501606e386
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalUnixMakefileGenerator3.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-18 15:25:25 $
7 Version: $Revision: 1.235 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmLocalUnixMakefileGenerator3.h"
19 #include "cmDepends.h"
20 #include "cmGeneratedFileStream.h"
21 #include "cmGlobalUnixMakefileGenerator3.h"
22 #include "cmMakefile.h"
23 #include "cmMakefileTargetGenerator.h"
24 #include "cmSourceFile.h"
25 #include "cmake.h"
26 #include "cmVersion.h"
28 // Include dependency scanners for supported languages. Only the
29 // C/C++ scanner is needed for bootstrapping CMake.
30 #include "cmDependsC.h"
31 #ifdef CMAKE_BUILD_WITH_CMAKE
32 # include "cmDependsFortran.h"
33 # include "cmDependsJava.h"
34 # include <cmsys/Terminal.h>
35 #endif
37 #include <memory> // auto_ptr
38 #include <queue>
40 //----------------------------------------------------------------------------
41 // Helper function used below.
42 static std::string cmSplitExtension(std::string const& in, std::string& base)
44 std::string ext;
45 std::string::size_type dot_pos = in.rfind(".");
46 if(dot_pos != std::string::npos)
48 // Remove the extension first in case &base == &in.
49 ext = in.substr(dot_pos, std::string::npos);
50 base = in.substr(0, dot_pos);
52 else
54 base = in;
56 return ext;
59 //----------------------------------------------------------------------------
60 cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
62 this->SilentNoColon = false;
63 this->WindowsShell = false;
64 this->IncludeDirective = "include";
65 this->MakefileVariableSize = 0;
66 this->IgnoreLibPrefix = false;
67 this->PassMakeflags = false;
68 this->DefineWindowsNULL = false;
69 this->UnixCD = true;
70 this->ColorMakefile = false;
71 this->SkipPreprocessedSourceRules = false;
72 this->SkipAssemblySourceRules = false;
73 this->NativeEchoCommand = "@echo ";
74 this->NativeEchoWindows = true;
75 this->MakeCommandEscapeTargetTwice = false;
76 this->IsMakefileGenerator = true;
77 this->BorlandMakeCurlyHack = false;
80 //----------------------------------------------------------------------------
81 cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
85 //----------------------------------------------------------------------------
86 void cmLocalUnixMakefileGenerator3::Configure()
88 // Compute the path to use when referencing the current output
89 // directory from the top output directory.
90 this->HomeRelativeOutputPath =
91 this->Convert(this->Makefile->GetStartOutputDirectory(), HOME_OUTPUT);
92 if(this->HomeRelativeOutputPath == ".")
94 this->HomeRelativeOutputPath = "";
96 if(!this->HomeRelativeOutputPath.empty())
98 this->HomeRelativeOutputPath += "/";
100 this->cmLocalGenerator::Configure();
103 //----------------------------------------------------------------------------
104 void cmLocalUnixMakefileGenerator3::Generate()
106 // Store the configuration name that will be generated.
107 if(const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
109 // Use the build type given by the user.
110 this->ConfigurationName = config;
112 else
114 // No configuration type given.
115 this->ConfigurationName = "";
118 // Record whether some options are enabled to avoid checking many
119 // times later.
120 this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE");
121 this->SkipPreprocessedSourceRules =
122 this->Makefile->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");
123 this->SkipAssemblySourceRules =
124 this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
126 // Generate the rule files for each target.
127 cmTargets& targets = this->Makefile->GetTargets();
128 std::string empty;
129 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
131 cmMakefileTargetGenerator *tg =
132 cmMakefileTargetGenerator::New(this, t->first, &(t->second));
133 if (tg)
135 this->TargetGenerators.push_back(tg);
136 tg->WriteRuleFiles();
140 // write the local Makefile
141 this->WriteLocalMakefile();
143 // Write the cmake file with information for this directory.
144 this->WriteDirectoryInformationFile();
147 //----------------------------------------------------------------------------
148 // return info about progress actions
149 unsigned long cmLocalUnixMakefileGenerator3::GetNumberOfProgressActions()
151 unsigned long result = 0;
153 for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
154 this->TargetGenerators.begin();
155 mtgIter != this->TargetGenerators.end(); ++mtgIter)
157 result += (*mtgIter)->GetNumberOfProgressActions();
159 return result;
162 //----------------------------------------------------------------------------
163 // return info about progress actions
164 unsigned long cmLocalUnixMakefileGenerator3
165 ::GetNumberOfProgressActionsForTarget(const char *name)
167 for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
168 this->TargetGenerators.begin();
169 mtgIter != this->TargetGenerators.end(); ++mtgIter)
171 if (!strcmp(name,(*mtgIter)->GetTargetName()))
173 return (*mtgIter)->GetNumberOfProgressActions();
176 return 0;
180 //----------------------------------------------------------------------------
181 // writes the progreess variables and also closes out the targets
182 void cmLocalUnixMakefileGenerator3
183 ::WriteProgressVariables(unsigned long total,
184 unsigned long &current)
186 // delete the makefile target generator objects
187 for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
188 this->TargetGenerators.begin();
189 mtgIter != this->TargetGenerators.end(); ++mtgIter)
191 (*mtgIter)->WriteProgressVariables(total,current);
192 delete *mtgIter;
194 this->TargetGenerators.clear();
197 void cmLocalUnixMakefileGenerator3::WriteAllProgressVariable()
199 // write the top level progress for the all target
200 std::string progressFile = cmake::GetCMakeFilesDirectory();
201 progressFile += "/progress.make";
202 std::string progressFileNameFull =
203 this->ConvertToFullPath(progressFile.c_str());
204 cmGeneratedFileStream ruleFileStream(progressFileNameFull.c_str());
205 if(!ruleFileStream)
207 return;
210 cmGlobalUnixMakefileGenerator3 *gg =
211 static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
213 ruleFileStream << gg->GetNumberOfProgressActionsInAll(this) << "\n";
216 //----------------------------------------------------------------------------
217 void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
219 // generate the includes
220 std::string ruleFileName = "Makefile";
222 // Open the rule file. This should be copy-if-different because the
223 // rules may depend on this file itself.
224 std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
225 cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
226 if(!ruleFileStream)
228 return;
230 // always write the top makefile
231 if (this->Parent)
233 ruleFileStream.SetCopyIfDifferent(true);
236 // write the all rules
237 this->WriteLocalAllRules(ruleFileStream);
239 // only write local targets unless at the top Keep track of targets already
240 // listed.
241 std::set<cmStdString> emittedTargets;
242 if (this->Parent)
244 // write our targets, and while doing it collect up the object
245 // file rules
246 this->WriteLocalMakefileTargets(ruleFileStream,emittedTargets);
248 else
250 cmGlobalUnixMakefileGenerator3 *gg =
251 static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
252 gg->WriteConvenienceRules(ruleFileStream,emittedTargets);
255 bool do_preprocess_rules =
256 this->GetCreatePreprocessedSourceRules();
257 bool do_assembly_rules =
258 this->GetCreateAssemblySourceRules();
260 // now write out the object rules
261 // for each object file name
262 for (std::map<cmStdString, LocalObjectInfo>::iterator lo =
263 this->LocalObjectFiles.begin();
264 lo != this->LocalObjectFiles.end(); ++lo)
266 // Add a convenience rule for building the object file.
267 this->WriteObjectConvenienceRule(ruleFileStream,
268 "target to build an object file",
269 lo->first.c_str(), lo->second);
271 // Check whether preprocessing and assembly rules make sense.
272 // They make sense only for C and C++ sources.
273 bool lang_is_c_or_cxx = false;
274 for(std::vector<LocalObjectEntry>::const_iterator ei =
275 lo->second.begin(); ei != lo->second.end(); ++ei)
277 if(ei->Language == "C" || ei->Language == "CXX")
279 lang_is_c_or_cxx = true;
283 // Add convenience rules for preprocessed and assembly files.
284 if(lang_is_c_or_cxx && (do_preprocess_rules || do_assembly_rules))
286 std::string::size_type dot_pos = lo->first.rfind(".");
287 std::string base = lo->first.substr(0, dot_pos);
288 if(do_preprocess_rules)
290 this->WriteObjectConvenienceRule(
291 ruleFileStream, "target to preprocess a source file",
292 (base + ".i").c_str(), lo->second);
294 if(do_assembly_rules)
296 this->WriteObjectConvenienceRule(
297 ruleFileStream, "target to generate assembly for a file",
298 (base + ".s").c_str(), lo->second);
303 // add a help target as long as there isn;t a real target named help
304 if(emittedTargets.insert("help").second)
306 cmGlobalUnixMakefileGenerator3 *gg =
307 static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
308 gg->WriteHelpRule(ruleFileStream,this);
311 this->WriteSpecialTargetsBottom(ruleFileStream);
314 //----------------------------------------------------------------------------
315 void
316 cmLocalUnixMakefileGenerator3
317 ::WriteObjectConvenienceRule(std::ostream& ruleFileStream,
318 const char* comment, const char* output,
319 LocalObjectInfo const& info)
321 // If the rule includes the source file extension then create a
322 // version that has the extension removed. The help should include
323 // only the version without source extension.
324 bool inHelp = true;
325 if(info.HasSourceExtension)
327 // Remove the last extension. This should be kept.
328 std::string outBase1 = output;
329 std::string outExt1 = cmSplitExtension(outBase1, outBase1);
331 // Now remove the source extension and put back the last
332 // extension.
333 std::string outNoExt;
334 cmSplitExtension(outBase1, outNoExt);
335 outNoExt += outExt1;
337 // Add a rule to drive the rule below.
338 std::vector<std::string> depends;
339 depends.push_back(output);
340 std::vector<std::string> no_commands;
341 this->WriteMakeRule(ruleFileStream, 0,
342 outNoExt.c_str(), depends, no_commands, true, true);
343 inHelp = false;
346 // Recursively make the rule for each target using the object file.
347 std::vector<std::string> commands;
348 for(std::vector<LocalObjectEntry>::const_iterator t = info.begin();
349 t != info.end(); ++t)
351 std::string tgtMakefileName =
352 this->GetRelativeTargetDirectory(*(t->Target));
353 std::string targetName = tgtMakefileName;
354 tgtMakefileName += "/build.make";
355 targetName += "/";
356 targetName += output;
357 commands.push_back(
358 this->GetRecursiveMakeCall(tgtMakefileName.c_str(), targetName.c_str())
360 this->CreateCDCommand(commands,
361 this->Makefile->GetHomeOutputDirectory(),
362 this->Makefile->GetStartOutputDirectory());
365 // Write the rule to the makefile.
366 std::vector<std::string> no_depends;
367 this->WriteMakeRule(ruleFileStream, comment,
368 output, no_depends, commands, true, inHelp);
371 //----------------------------------------------------------------------------
372 void cmLocalUnixMakefileGenerator3
373 ::WriteLocalMakefileTargets(std::ostream& ruleFileStream,
374 std::set<cmStdString> &emitted)
376 std::vector<std::string> depends;
377 std::vector<std::string> commands;
379 // for each target we just provide a rule to cd up to the top and do a make
380 // on the target
381 cmTargets& targets = this->Makefile->GetTargets();
382 std::string localName;
383 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
385 if((t->second.GetType() == cmTarget::EXECUTABLE) ||
386 (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
387 (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
388 (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
389 (t->second.GetType() == cmTarget::UTILITY))
391 emitted.insert(t->second.GetName());
393 // for subdirs add a rule to build this specific target by name.
394 localName = this->GetRelativeTargetDirectory(t->second);
395 localName += "/rule";
396 commands.clear();
397 depends.clear();
399 // Build the target for this pass.
400 std::string makefile2 = cmake::GetCMakeFilesDirectoryPostSlash();
401 makefile2 += "Makefile2";
402 commands.push_back(this->GetRecursiveMakeCall
403 (makefile2.c_str(),localName.c_str()));
404 this->CreateCDCommand(commands,
405 this->Makefile->GetHomeOutputDirectory(),
406 this->Makefile->GetStartOutputDirectory());
407 this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
408 localName.c_str(), depends, commands, true);
410 // Add a target with the canonical name (no prefix, suffix or path).
411 if(localName != t->second.GetName())
413 commands.clear();
414 depends.push_back(localName);
415 this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
416 t->second.GetName(), depends, commands, true);
419 // Add a fast rule to build the target
420 std::string makefileName = this->GetRelativeTargetDirectory(t->second);
421 makefileName += "/build.make";
422 // make sure the makefile name is suitable for a makefile
423 std::string makeTargetName =
424 this->GetRelativeTargetDirectory(t->second);
425 makeTargetName += "/build";
426 localName = t->second.GetName();
427 localName += "/fast";
428 depends.clear();
429 commands.clear();
430 commands.push_back(this->GetRecursiveMakeCall
431 (makefileName.c_str(), makeTargetName.c_str()));
432 this->CreateCDCommand(commands,
433 this->Makefile->GetHomeOutputDirectory(),
434 this->Makefile->GetStartOutputDirectory());
435 this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
436 localName.c_str(), depends, commands, true);
438 // Add a local name for the rule to relink the target before
439 // installation.
440 if(t->second.NeedRelinkBeforeInstall())
442 makeTargetName = this->GetRelativeTargetDirectory(t->second);
443 makeTargetName += "/preinstall";
444 localName = t->second.GetName();
445 localName += "/preinstall";
446 depends.clear();
447 commands.clear();
448 commands.push_back(this->GetRecursiveMakeCall
449 (makefile2.c_str(), makeTargetName.c_str()));
450 this->CreateCDCommand(commands,
451 this->Makefile->GetHomeOutputDirectory(),
452 this->Makefile->GetStartOutputDirectory());
453 this->WriteMakeRule(ruleFileStream,
454 "Manual pre-install relink rule for target.",
455 localName.c_str(), depends, commands, true);
461 //----------------------------------------------------------------------------
462 void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
464 std::string infoFileName = this->Makefile->GetStartOutputDirectory();
465 infoFileName += cmake::GetCMakeFilesDirectory();
466 infoFileName += "/CMakeDirectoryInformation.cmake";
468 // Open the output file.
469 cmGeneratedFileStream infoFileStream(infoFileName.c_str());
470 if(!infoFileStream)
472 return;
475 // Write the do not edit header.
476 this->WriteDisclaimer(infoFileStream);
478 // Setup relative path conversion tops.
479 infoFileStream
480 << "# Relative path conversion top directories.\n"
481 << "SET(CMAKE_RELATIVE_PATH_TOP_SOURCE \"" << this->RelativePathTopSource
482 << "\")\n"
483 << "SET(CMAKE_RELATIVE_PATH_TOP_BINARY \"" << this->RelativePathTopBinary
484 << "\")\n"
485 << "\n";
487 // Tell the dependency scanner to use unix paths if necessary.
488 if(cmSystemTools::GetForceUnixPaths())
490 infoFileStream
491 << "# Force unix paths in dependencies.\n"
492 << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
493 << "\n";
496 // Store the include search path for this directory.
497 infoFileStream
498 << "# The C and CXX include file search paths:\n";
499 infoFileStream
500 << "SET(CMAKE_C_INCLUDE_PATH\n";
501 std::vector<std::string> includeDirs;
502 this->GetIncludeDirectories(includeDirs, false);
503 for(std::vector<std::string>::iterator i = includeDirs.begin();
504 i != includeDirs.end(); ++i)
506 infoFileStream
507 << " \"" << this->Convert(i->c_str(),HOME_OUTPUT).c_str() << "\"\n";
509 infoFileStream
510 << " )\n";
511 infoFileStream
512 << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
513 infoFileStream
514 << "SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
516 // Store the include regular expressions for this directory.
517 infoFileStream
518 << "\n"
519 << "# The C and CXX include file regular expressions for "
520 << "this directory.\n";
521 infoFileStream
522 << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
523 this->WriteCMakeArgument(infoFileStream,
524 this->Makefile->GetIncludeRegularExpression());
525 infoFileStream
526 << ")\n";
527 infoFileStream
528 << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
529 this->WriteCMakeArgument(infoFileStream,
530 this->Makefile->GetComplainRegularExpression());
531 infoFileStream
532 << ")\n";
533 infoFileStream
534 << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
535 infoFileStream
536 << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
537 "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
540 //----------------------------------------------------------------------------
541 std::string
542 cmLocalUnixMakefileGenerator3
543 ::ConvertToFullPath(const std::string& localPath)
545 std::string dir = this->Makefile->GetStartOutputDirectory();
546 dir += "/";
547 dir += localPath;
548 return dir;
552 const std::string &cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
554 return this->HomeRelativeOutputPath;
558 //----------------------------------------------------------------------------
559 void
560 cmLocalUnixMakefileGenerator3
561 ::WriteMakeRule(std::ostream& os,
562 const char* comment,
563 const char* target,
564 const std::vector<std::string>& depends,
565 const std::vector<std::string>& commands,
566 bool symbolic,
567 bool in_help)
569 // Make sure there is a target.
570 if(!target || !*target)
572 cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
573 comment);
574 return;
577 std::string replace;
579 // Write the comment describing the rule in the makefile.
580 if(comment)
582 replace = comment;
583 std::string::size_type lpos = 0;
584 std::string::size_type rpos;
585 while((rpos = replace.find('\n', lpos)) != std::string::npos)
587 os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
588 lpos = rpos+1;
590 os << "# " << replace.substr(lpos) << "\n";
593 // Construct the left hand side of the rule.
594 replace = target;
595 std::string tgt = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
596 const char* space = "";
597 if(tgt.size() == 1)
599 // Add a space before the ":" to avoid drive letter confusion on
600 // Windows.
601 space = " ";
604 // Mark the rule as symbolic if requested.
605 if(symbolic)
607 if(const char* sym =
608 this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
610 os << tgt.c_str() << space << ": " << sym << "\n";
614 // Write the rule.
615 if(depends.empty())
617 // No dependencies. The commands will always run.
618 os << tgt.c_str() << space << ":\n";
620 else
622 // Split dependencies into multiple rule lines. This allows for
623 // very long dependency lists even on older make implementations.
624 for(std::vector<std::string>::const_iterator dep = depends.begin();
625 dep != depends.end(); ++dep)
627 replace = *dep;
628 replace = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
629 os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
633 // Write the list of commands.
634 for(std::vector<std::string>::const_iterator i = commands.begin();
635 i != commands.end(); ++i)
637 replace = *i;
638 os << "\t" << replace.c_str() << "\n";
640 os << "\n";
642 // Add the output to the local help if requested.
643 if(in_help)
645 this->LocalHelp.push_back(target);
649 //----------------------------------------------------------------------------
650 void
651 cmLocalUnixMakefileGenerator3
652 ::WriteMakeVariables(std::ostream& makefileStream)
654 this->WriteDivider(makefileStream);
655 makefileStream
656 << "# Set environment variables for the build.\n"
657 << "\n";
658 if(this->DefineWindowsNULL)
660 makefileStream
661 << "!IF \"$(OS)\" == \"Windows_NT\"\n"
662 << "NULL=\n"
663 << "!ELSE\n"
664 << "NULL=nul\n"
665 << "!ENDIF\n";
667 if(this->WindowsShell)
669 makefileStream
670 << "SHELL = cmd.exe\n"
671 << "\n";
673 else
675 makefileStream
676 << "# The shell in which to execute make rules.\n"
677 << "SHELL = /bin/sh\n"
678 << "\n";
681 std::string cmakecommand =
682 this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
683 makefileStream
684 << "# The CMake executable.\n"
685 << "CMAKE_COMMAND = "
686 << this->Convert(cmakecommand.c_str(), FULL, SHELL).c_str()
687 << "\n"
688 << "\n";
689 makefileStream
690 << "# The command to remove a file.\n"
691 << "RM = "
692 << this->Convert(cmakecommand.c_str(),FULL,SHELL).c_str()
693 << " -E remove -f\n"
694 << "\n";
696 if(this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
698 makefileStream
699 << "# The program to use to edit the cache.\n"
700 << "CMAKE_EDIT_COMMAND = "
701 << (this->ConvertToOutputForExisting(
702 this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
703 << "\n";
706 makefileStream
707 << "# The top-level source directory on which CMake was run.\n"
708 << "CMAKE_SOURCE_DIR = "
709 << this->Convert(this->Makefile->GetHomeDirectory(), FULL, SHELL)
710 << "\n"
711 << "\n";
712 makefileStream
713 << "# The top-level build directory on which CMake was run.\n"
714 << "CMAKE_BINARY_DIR = "
715 << this->Convert(this->Makefile->GetHomeOutputDirectory(), FULL, SHELL)
716 << "\n"
717 << "\n";
720 //----------------------------------------------------------------------------
721 void
722 cmLocalUnixMakefileGenerator3
723 ::WriteSpecialTargetsTop(std::ostream& makefileStream)
725 this->WriteDivider(makefileStream);
726 makefileStream
727 << "# Special targets provided by cmake.\n"
728 << "\n";
730 std::vector<std::string> no_commands;
731 std::vector<std::string> no_depends;
733 // Special target to cleanup operation of make tool.
734 // This should be the first target except for the default_target in
735 // the interface Makefile.
736 this->WriteMakeRule(
737 makefileStream, "Disable implicit rules so canoncical targets will work.",
738 ".SUFFIXES", no_depends, no_commands, false);
740 // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
741 std::vector<std::string> depends;
742 depends.push_back(".hpux_make_needs_suffix_list");
743 this->WriteMakeRule(makefileStream, 0,
744 ".SUFFIXES", depends, no_commands, false);
746 cmGlobalUnixMakefileGenerator3* gg =
747 static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
748 // Write special target to silence make output. This must be after
749 // the default target in case VERBOSE is set (which changes the
750 // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
751 // "VERBOSE=1" to be added as a make variable which will change the
752 // name of this special target. This gives a make-time choice to
753 // the user.
754 if((this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
755 || (gg->GetForceVerboseMakefiles()))
757 makefileStream
758 << "# Produce verbose output by default.\n"
759 << "VERBOSE = 1\n"
760 << "\n";
762 if(this->SilentNoColon)
764 makefileStream << "$(VERBOSE).SILENT\n";
766 else
768 this->WriteMakeRule(makefileStream,
769 "Suppress display of executed commands.",
770 "$(VERBOSE).SILENT",
771 no_depends,
772 no_commands, false);
775 // Work-around for makes that drop rules that have no dependencies
776 // or commands.
777 std::string hack = gg->GetEmptyRuleHackDepends();
778 if(!hack.empty())
780 no_depends.push_back(hack);
782 std::string hack_cmd = gg->GetEmptyRuleHackCommand();
783 if(!hack_cmd.empty())
785 no_commands.push_back(hack_cmd);
788 // Special symbolic target that never exists to force dependers to
789 // run their rules.
790 this->WriteMakeRule
791 (makefileStream,
792 "A target that is always out of date.",
793 "cmake_force", no_depends, no_commands, true);
795 // Variables for reference by other rules.
796 this->WriteMakeVariables(makefileStream);
799 //----------------------------------------------------------------------------
800 void cmLocalUnixMakefileGenerator3
801 ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
803 this->WriteDivider(makefileStream);
804 makefileStream
805 << "# Special targets to cleanup operation of make.\n"
806 << "\n";
808 // Write special "cmake_check_build_system" target to run cmake with
809 // the --check-build-system flag.
811 // Build command to run CMake to check if anything needs regenerating.
812 std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
813 cmakefileName += "Makefile.cmake";
814 std::string runRule =
815 "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
816 runRule += " --check-build-system ";
817 runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
818 runRule += " 0";
820 std::vector<std::string> no_depends;
821 std::vector<std::string> commands;
822 commands.push_back(runRule);
823 if(this->Parent)
825 this->CreateCDCommand(commands,
826 this->Makefile->GetHomeOutputDirectory(),
827 this->Makefile->GetStartOutputDirectory());
829 this->WriteMakeRule(makefileStream,
830 "Special rule to run CMake to check the build system "
831 "integrity.\n"
832 "No rule that depends on this can have "
833 "commands that come from listfiles\n"
834 "because they might be regenerated.",
835 "cmake_check_build_system",
836 no_depends,
837 commands, true);
843 //----------------------------------------------------------------------------
844 void
845 cmLocalUnixMakefileGenerator3
846 ::WriteConvenienceRule(std::ostream& ruleFileStream,
847 const char* realTarget,
848 const char* helpTarget)
850 // A rule is only needed if the names are different.
851 if(strcmp(realTarget, helpTarget) != 0)
853 // The helper target depends on the real target.
854 std::vector<std::string> depends;
855 depends.push_back(realTarget);
857 // There are no commands.
858 std::vector<std::string> no_commands;
860 // Write the rule.
861 this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
862 helpTarget, depends, no_commands, true);
867 //----------------------------------------------------------------------------
868 std::string
869 cmLocalUnixMakefileGenerator3
870 ::GetRelativeTargetDirectory(cmTarget const& target)
872 std::string dir = this->HomeRelativeOutputPath;
873 dir += this->GetTargetDirectory(target);
874 return this->Convert(dir.c_str(),NONE,UNCHANGED);
879 //----------------------------------------------------------------------------
880 void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
881 const char* newFlags)
883 if(this->WatcomWMake && newFlags && *newFlags)
885 std::string newf = newFlags;
886 if(newf.find("\\\"") != newf.npos)
888 cmSystemTools::ReplaceString(newf, "\\\"", "\"");
889 this->cmLocalGenerator::AppendFlags(flags, newf.c_str());
890 return;
893 this->cmLocalGenerator::AppendFlags(flags, newFlags);
896 //----------------------------------------------------------------------------
897 void
898 cmLocalUnixMakefileGenerator3
899 ::AppendRuleDepend(std::vector<std::string>& depends,
900 const char* ruleFileName)
902 // Add a dependency on the rule file itself unless an option to skip
903 // it is specifically enabled by the user or project.
904 const char* nodep =
905 this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
906 if(!nodep || cmSystemTools::IsOff(nodep))
908 depends.push_back(ruleFileName);
912 //----------------------------------------------------------------------------
913 void
914 cmLocalUnixMakefileGenerator3
915 ::AppendCustomDepends(std::vector<std::string>& depends,
916 const std::vector<cmCustomCommand>& ccs)
918 for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
919 i != ccs.end(); ++i)
921 this->AppendCustomDepend(depends, *i);
925 //----------------------------------------------------------------------------
926 void
927 cmLocalUnixMakefileGenerator3
928 ::AppendCustomDepend(std::vector<std::string>& depends,
929 const cmCustomCommand& cc)
931 for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
932 d != cc.GetDepends().end(); ++d)
934 // Lookup the real name of the dependency in case it is a CMake target.
935 std::string dep = this->GetRealDependency
936 (d->c_str(), this->ConfigurationName.c_str());
937 depends.push_back(dep);
941 //----------------------------------------------------------------------------
942 void
943 cmLocalUnixMakefileGenerator3
944 ::AppendCustomCommands(std::vector<std::string>& commands,
945 const std::vector<cmCustomCommand>& ccs)
947 for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
948 i != ccs.end(); ++i)
950 this->AppendCustomCommand(commands, *i, true);
954 //----------------------------------------------------------------------------
955 void
956 cmLocalUnixMakefileGenerator3
957 ::AppendCustomCommand(std::vector<std::string>& commands,
958 const cmCustomCommand& cc, bool echo_comment)
960 // Optionally create a command to display the custom command's
961 // comment text. This is used for pre-build, pre-link, and
962 // post-build command comments. Custom build step commands have
963 // their comments generated elsewhere.
964 if(echo_comment)
966 const char* comment = cc.GetComment();
967 if(comment && *comment)
969 this->AppendEcho(commands, comment,
970 cmLocalUnixMakefileGenerator3::EchoGenerate);
974 // if the command specified a working directory use it.
975 const char* dir = this->Makefile->GetStartOutputDirectory();
976 const char* workingDir = cc.GetWorkingDirectory();
977 if(workingDir)
979 dir = workingDir;
981 bool escapeOldStyle = cc.GetEscapeOldStyle();
982 bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
984 // Add each command line to the set of commands.
985 std::vector<std::string> commands1;
986 for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
987 cl != cc.GetCommandLines().end(); ++cl)
989 // Build the command line in a single string.
990 const cmCustomCommandLine& commandLine = *cl;
991 std::string cmd = GetRealLocation(commandLine[0].c_str(),
992 this->ConfigurationName.c_str());
993 if (cmd.size())
995 cmSystemTools::ReplaceString(cmd, "/./", "/");
996 // Convert the command to a relative path only if the current
997 // working directory will be the start-output directory.
998 bool had_slash = cmd.find("/") != cmd.npos;
999 if(!workingDir)
1001 cmd = this->Convert(cmd.c_str(),START_OUTPUT);
1003 bool has_slash = cmd.find("/") != cmd.npos;
1004 if(had_slash && !has_slash)
1006 // This command was specified as a path to a file in the
1007 // current directory. Add a leading "./" so it can run
1008 // without the current directory being in the search path.
1009 cmd = "./" + cmd;
1011 cmd = this->Convert(cmd.c_str(),NONE,SHELL);
1012 for(unsigned int j=1; j < commandLine.size(); ++j)
1014 cmd += " ";
1015 if(escapeOldStyle)
1017 cmd += this->EscapeForShellOldStyle(commandLine[j].c_str());
1019 else
1021 cmd += this->EscapeForShell(commandLine[j].c_str(),
1022 escapeAllowMakeVars);
1025 if(this->BorlandMakeCurlyHack)
1027 // Borland Make has a very strange bug. If the first curly
1028 // brace anywhere in the command string is a left curly, it
1029 // must be written {{} instead of just {. Otherwise some
1030 // curly braces are removed. The hack can be skipped if the
1031 // first curly brace is the last character.
1032 std::string::size_type lcurly = cmd.find("{");
1033 if(lcurly != cmd.npos && lcurly < (cmd.size()-1))
1035 std::string::size_type rcurly = cmd.find("}");
1036 if(rcurly == cmd.npos || rcurly > lcurly)
1038 // The first curly is a left curly. Use the hack.
1039 std::string hack_cmd = cmd.substr(0, lcurly);
1040 hack_cmd += "{{}";
1041 hack_cmd += cmd.substr(lcurly+1);
1042 cmd = hack_cmd;
1046 commands1.push_back(cmd);
1050 // Setup the proper working directory for the commands.
1051 this->CreateCDCommand(commands1, dir,
1052 this->Makefile->GetHomeOutputDirectory());
1054 // push back the custom commands
1055 commands.insert(commands.end(), commands1.begin(), commands1.end());
1058 //----------------------------------------------------------------------------
1059 void
1060 cmLocalUnixMakefileGenerator3
1061 ::AppendCleanCommand(std::vector<std::string>& commands,
1062 const std::vector<std::string>& files,
1063 cmTarget& target, const char* filename)
1065 if(!files.empty())
1067 std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
1068 cleanfile += "/";
1069 cleanfile += this->GetTargetDirectory(target);
1070 cleanfile += "/cmake_clean";
1071 if(filename)
1073 cleanfile += "_";
1074 cleanfile += filename;
1076 cleanfile += ".cmake";
1077 std::string cleanfilePath = this->Convert(cleanfile.c_str(), FULL);
1078 std::ofstream fout(cleanfilePath.c_str());
1079 if(!fout)
1081 cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
1083 fout << "FILE(REMOVE\n";
1084 std::string remove = "$(CMAKE_COMMAND) -P ";
1085 remove += this->Convert(cleanfile.c_str(), START_OUTPUT, SHELL);
1086 for(std::vector<std::string>::const_iterator f = files.begin();
1087 f != files.end(); ++f)
1089 fout << "\"" << this->Convert(f->c_str(),START_OUTPUT,UNCHANGED)
1090 << "\"\n";
1092 fout << ")\n";
1093 commands.push_back(remove);
1095 // For the main clean rule add per-language cleaning.
1096 if(!filename)
1098 // Get the set of source languages in the target.
1099 std::set<cmStdString> languages;
1100 target.GetLanguages(languages);
1101 fout << "\n"
1102 << "# Per-language clean rules from dependency scanning.\n"
1103 << "FOREACH(lang";
1104 for(std::set<cmStdString>::const_iterator l = languages.begin();
1105 l != languages.end(); ++l)
1107 fout << " " << *l;
1109 fout << ")\n"
1110 << " INCLUDE(" << this->GetTargetDirectory(target)
1111 << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
1112 << "ENDFOREACH(lang)\n";
1117 //----------------------------------------------------------------------------
1118 void
1119 cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
1120 const char* text,
1121 EchoColor color)
1123 // Choose the color for the text.
1124 std::string color_name;
1125 #ifdef CMAKE_BUILD_WITH_CMAKE
1126 if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
1128 // See cmake::ExecuteEchoColor in cmake.cxx for these options.
1129 // This color set is readable on both black and white backgrounds.
1130 switch(color)
1132 case EchoNormal:
1133 break;
1134 case EchoDepend:
1135 color_name = "--magenta --bold ";
1136 break;
1137 case EchoBuild:
1138 color_name = "--green ";
1139 break;
1140 case EchoLink:
1141 color_name = "--red --bold ";
1142 break;
1143 case EchoGenerate:
1144 color_name = "--blue --bold ";
1145 break;
1146 case EchoGlobal:
1147 color_name = "--cyan ";
1148 break;
1151 #else
1152 (void)color;
1153 #endif
1155 // Echo one line at a time.
1156 std::string line;
1157 line.reserve(200);
1158 for(const char* c = text;; ++c)
1160 if(*c == '\n' || *c == '\0')
1162 // Avoid writing a blank last line on end-of-string.
1163 if(*c != '\0' || !line.empty())
1165 // Add a command to echo this line.
1166 std::string cmd;
1167 if(color_name.empty())
1169 // Use the native echo command.
1170 cmd = this->NativeEchoCommand;
1171 cmd += this->EscapeForShell(line.c_str(), false,
1172 this->NativeEchoWindows);
1174 else
1176 // Use cmake to echo the text in color.
1177 cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
1178 cmd += color_name;
1179 cmd += this->EscapeForShell(line.c_str());
1181 commands.push_back(cmd);
1184 // Reset the line to emtpy.
1185 line = "";
1187 // Terminate on end-of-string.
1188 if(*c == '\0')
1190 return;
1193 else if(*c != '\r')
1195 // Append this character to the current line.
1196 line += *c;
1201 //----------------------------------------------------------------------------
1202 std::string
1203 cmLocalUnixMakefileGenerator3
1204 ::CreateMakeVariable(const char* sin, const char* s2in)
1206 std::string s = sin;
1207 std::string s2 = s2in;
1208 std::string unmodified = s;
1209 unmodified += s2;
1210 // if there is no restriction on the length of make variables
1211 // and there are no "." charactors in the string, then return the
1212 // unmodified combination.
1213 if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
1214 && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
1216 return unmodified;
1219 // see if the variable has been defined before and return
1220 // the modified version of the variable
1221 std::map<cmStdString, cmStdString>::iterator i =
1222 this->MakeVariableMap.find(unmodified);
1223 if(i != this->MakeVariableMap.end())
1225 return i->second;
1227 // start with the unmodified variable
1228 std::string ret = unmodified;
1229 // if this there is no value for this->MakefileVariableSize then
1230 // the string must have bad characters in it
1231 if(!this->MakefileVariableSize)
1233 cmSystemTools::ReplaceString(ret, ".", "_");
1234 cmSystemTools::ReplaceString(ret, "-", "__");
1235 int ni = 0;
1236 char buffer[5];
1237 // make sure the _ version is not already used, if
1238 // it is used then add number to the end of the variable
1239 while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
1241 ++ni;
1242 sprintf(buffer, "%04d", ni);
1243 ret = unmodified + buffer;
1245 this->ShortMakeVariableMap[ret] = "1";
1246 this->MakeVariableMap[unmodified] = ret;
1247 return ret;
1250 // if the string is greater the 32 chars it is an invalid vairable name
1251 // for borland make
1252 if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
1254 int keep = this->MakefileVariableSize - 8;
1255 int size = keep + 3;
1256 std::string str1 = s;
1257 std::string str2 = s2;
1258 // we must shorten the combined string by 4 charactors
1259 // keep no more than 24 charactors from the second string
1260 if(static_cast<int>(str2.size()) > keep)
1262 str2 = str2.substr(0, keep);
1264 if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
1266 str1 = str1.substr(0, size - str2.size());
1268 char buffer[5];
1269 int ni = 0;
1270 sprintf(buffer, "%04d", ni);
1271 ret = str1 + str2 + buffer;
1272 while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
1274 ++ni;
1275 sprintf(buffer, "%04d", ni);
1276 ret = str1 + str2 + buffer;
1278 if(ni == 1000)
1280 cmSystemTools::Error("Borland makefile variable length too long");
1281 return unmodified;
1283 // once an unused variable is found
1284 this->ShortMakeVariableMap[ret] = "1";
1286 // always make an entry into the unmodified to variable map
1287 this->MakeVariableMap[unmodified] = ret;
1288 return ret;
1291 //----------------------------------------------------------------------------
1292 bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
1293 bool verbose,
1294 bool color)
1296 // read in the target info file
1297 if(!this->Makefile->ReadListFile(0, tgtInfo) ||
1298 cmSystemTools::GetErrorOccuredFlag())
1300 cmSystemTools::Error("Target DependInfo.cmake file not found");
1303 // Check if any multiple output pairs have a missing file.
1304 this->CheckMultipleOutputs(verbose);
1306 std::string dir = cmSystemTools::GetFilenamePath(tgtInfo);
1307 std::string internalDependFile = dir + "/depend.internal";
1308 std::string dependFile = dir + "/depend.make";
1310 // Check the implicit dependencies to see if they are up to date.
1311 // The build.make file may have explicit dependencies for the object
1312 // files but these will not affect the scanning process so they need
1313 // not be considered.
1314 cmDependsC checker;
1315 checker.SetVerbose(verbose);
1316 checker.SetFileComparison
1317 (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
1318 if(!checker.Check(dependFile.c_str(), internalDependFile.c_str()))
1320 // The dependencies must be regenerated.
1321 std::string targetName = cmSystemTools::GetFilenameName(dir);
1322 targetName = targetName.substr(0, targetName.length()-4);
1323 std::string message = "Scanning dependencies of target ";
1324 message += targetName;
1325 #ifdef CMAKE_BUILD_WITH_CMAKE
1326 cmSystemTools::MakefileColorEcho(
1327 cmsysTerminal_Color_ForegroundMagenta |
1328 cmsysTerminal_Color_ForegroundBold,
1329 message.c_str(), true, color);
1330 #else
1331 fprintf(stdout, "%s\n", message.c_str());
1332 #endif
1334 return this->ScanDependencies(dir.c_str());
1336 else
1338 // The dependencies are already up-to-date.
1339 return true;
1343 //----------------------------------------------------------------------------
1344 bool
1345 cmLocalUnixMakefileGenerator3
1346 ::ScanDependencies(const char* targetDir)
1348 // Read the directory information file.
1349 cmMakefile* mf = this->Makefile;
1350 bool haveDirectoryInfo = false;
1351 std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
1352 dirInfoFile += cmake::GetCMakeFilesDirectory();
1353 dirInfoFile += "/CMakeDirectoryInformation.cmake";
1354 if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
1355 !cmSystemTools::GetErrorOccuredFlag())
1357 haveDirectoryInfo = true;
1360 // Lookup useful directory information.
1361 if(haveDirectoryInfo)
1363 // Test whether we need to force Unix paths.
1364 if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
1366 if(!cmSystemTools::IsOff(force))
1368 cmSystemTools::SetForceUnixPaths(true);
1372 // Setup relative path top directories.
1373 this->RelativePathsConfigured = true;
1374 if(const char* relativePathTopSource =
1375 mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE"))
1377 this->RelativePathTopSource = relativePathTopSource;
1379 if(const char* relativePathTopBinary =
1380 mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY"))
1382 this->RelativePathTopBinary = relativePathTopBinary;
1385 else
1387 cmSystemTools::Error("Directory Information file not found");
1390 // create the file stream for the depends file
1391 std::string dir = targetDir;
1393 // Open the rule file. This should be copy-if-different because the
1394 // rules may depend on this file itself.
1395 std::string ruleFileNameFull = dir;
1396 ruleFileNameFull += "/depend.make";
1397 cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
1398 ruleFileStream.SetCopyIfDifferent(true);
1399 if(!ruleFileStream)
1401 return false;
1403 std::string internalRuleFileNameFull = dir;
1404 internalRuleFileNameFull += "/depend.internal";
1405 cmGeneratedFileStream
1406 internalRuleFileStream(internalRuleFileNameFull.c_str());
1407 internalRuleFileStream.SetCopyIfDifferent(true);
1408 if(!internalRuleFileStream)
1410 return false;
1413 this->WriteDisclaimer(ruleFileStream);
1414 this->WriteDisclaimer(internalRuleFileStream);
1416 // for each language we need to scan, scan it
1417 const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
1418 std::vector<std::string> langs;
1419 cmSystemTools::ExpandListArgument(langStr, langs);
1420 for (std::vector<std::string>::iterator li =
1421 langs.begin(); li != langs.end(); ++li)
1423 // construct the checker
1424 std::string lang = li->c_str();
1426 // Get the set of include directories.
1427 std::vector<std::string> includes;
1428 if(haveDirectoryInfo)
1430 std::string includePathVar = "CMAKE_";
1431 includePathVar += lang;
1432 includePathVar += "_INCLUDE_PATH";
1433 if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
1435 cmSystemTools::ExpandListArgument(includePath, includes);
1439 // Get the include file regular expression.
1440 std::string includeRegexScan = "^.*$";
1441 std::string includeRegexComplain = "^$";
1442 if(haveDirectoryInfo)
1444 std::string scanRegexVar = "CMAKE_";
1445 scanRegexVar += lang;
1446 scanRegexVar += "_INCLUDE_REGEX_SCAN";
1447 if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
1449 includeRegexScan = scanRegex;
1451 std::string complainRegexVar = "CMAKE_";
1452 complainRegexVar += lang;
1453 complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
1454 if(const char* complainRegex =
1455 mf->GetDefinition(complainRegexVar.c_str()))
1457 includeRegexComplain = complainRegex;
1461 // Create the scanner for this language
1462 cmDepends *scanner = 0;
1463 if(lang == "C" || lang == "CXX" || lang == "RC")
1465 std::string includeCacheFileName = dir;
1466 includeCacheFileName += "/";
1467 includeCacheFileName += lang;
1468 includeCacheFileName += ".includecache";
1470 // TODO: Handle RC (resource files) dependencies correctly.
1471 scanner = new cmDependsC(includes,
1472 includeRegexScan.c_str(),
1473 includeRegexComplain.c_str(),
1474 includeCacheFileName);
1476 #ifdef CMAKE_BUILD_WITH_CMAKE
1477 else if(lang == "Fortran")
1479 std::vector<std::string> defines;
1480 if(const char* c_defines =
1481 mf->GetDefinition("CMAKE_TARGET_DEFINITIONS"))
1483 cmSystemTools::ExpandListArgument(c_defines, defines);
1486 scanner = new cmDependsFortran(includes, defines);
1488 else if(lang == "Java")
1490 scanner = new cmDependsJava();
1492 #endif
1494 if (scanner)
1496 scanner->SetLocalGenerator(this);
1497 scanner->SetFileComparison
1498 (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
1499 scanner->SetLanguage(lang.c_str());
1500 scanner->SetTargetDirectory(dir.c_str());
1501 scanner->Write(ruleFileStream, internalRuleFileStream);
1503 // free the scanner for this language
1504 delete scanner;
1508 return true;
1511 //----------------------------------------------------------------------------
1512 void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
1514 cmMakefile* mf = this->Makefile;
1516 // Get the string listing the multiple output pairs.
1517 const char* pairs_string = mf->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
1518 if(!pairs_string)
1520 return;
1523 // Convert the string to a list and preserve empty entries.
1524 std::vector<std::string> pairs;
1525 cmSystemTools::ExpandListArgument(pairs_string, pairs, true);
1526 for(std::vector<std::string>::const_iterator i = pairs.begin();
1527 i != pairs.end(); ++i)
1529 const std::string& depender = *i;
1530 if(++i != pairs.end())
1532 const std::string& dependee = *i;
1534 // If the depender is missing then delete the dependee to make
1535 // sure both will be regenerated.
1536 if(cmSystemTools::FileExists(dependee.c_str()) &&
1537 !cmSystemTools::FileExists(depender.c_str()))
1539 if(verbose)
1541 cmOStringStream msg;
1542 msg << "Deleting primary custom command output \"" << dependee
1543 << "\" because another output \""
1544 << depender << "\" does not exist." << std::endl;
1545 cmSystemTools::Stdout(msg.str().c_str());
1547 cmSystemTools::RemoveFile(dependee.c_str());
1553 //----------------------------------------------------------------------------
1554 void cmLocalUnixMakefileGenerator3
1555 ::WriteLocalAllRules(std::ostream& ruleFileStream)
1557 this->WriteDisclaimer(ruleFileStream);
1559 // Write the main entry point target. This must be the VERY first
1560 // target so that make with no arguments will run it.
1562 // Just depend on the all target to drive the build.
1563 std::vector<std::string> depends;
1564 std::vector<std::string> no_commands;
1565 depends.push_back("all");
1567 // Write the rule.
1568 this->WriteMakeRule(ruleFileStream,
1569 "Default target executed when no arguments are "
1570 "given to make.",
1571 "default_target",
1572 depends,
1573 no_commands, true);
1576 this->WriteSpecialTargetsTop(ruleFileStream);
1578 // Include the progress variables for the target.
1579 // Write all global targets
1580 this->WriteDivider(ruleFileStream);
1581 ruleFileStream
1582 << "# Targets provided globally by CMake.\n"
1583 << "\n";
1584 cmTargets* targets = &(this->Makefile->GetTargets());
1585 cmTargets::iterator glIt;
1586 for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
1588 if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
1590 std::string targetString = "Special rule for the target " + glIt->first;
1591 std::vector<std::string> commands;
1592 std::vector<std::string> depends;
1594 const char* text = glIt->second.GetProperty("EchoString");
1595 if ( !text )
1597 text = "Running external command ...";
1599 std::set<cmStdString>::const_iterator dit;
1600 for ( dit = glIt->second.GetUtilities().begin();
1601 dit != glIt->second.GetUtilities().end();
1602 ++ dit )
1604 depends.push_back(dit->c_str());
1606 this->AppendEcho(commands, text,
1607 cmLocalUnixMakefileGenerator3::EchoGlobal);
1609 // Global targets store their rules in pre- and post-build commands.
1610 this->AppendCustomDepends(depends,
1611 glIt->second.GetPreBuildCommands());
1612 this->AppendCustomDepends(depends,
1613 glIt->second.GetPostBuildCommands());
1614 this->AppendCustomCommands(commands,
1615 glIt->second.GetPreBuildCommands());
1616 this->AppendCustomCommands(commands,
1617 glIt->second.GetPostBuildCommands());
1618 std::string targetName = glIt->second.GetName();
1619 this->WriteMakeRule(ruleFileStream, targetString.c_str(),
1620 targetName.c_str(), depends, commands, true);
1622 // Provide a "/fast" version of the target.
1623 depends.clear();
1624 if((targetName == "install")
1625 || (targetName == "install_local")
1626 || (targetName == "install_strip"))
1628 // Provide a fast install target that does not depend on all
1629 // but has the same command.
1630 depends.push_back("preinstall/fast");
1632 else
1634 // Just forward to the real target so at least it will work.
1635 depends.push_back(targetName);
1636 commands.clear();
1638 targetName += "/fast";
1639 this->WriteMakeRule(ruleFileStream, targetString.c_str(),
1640 targetName.c_str(), depends, commands, true);
1644 std::vector<std::string> depends;
1645 std::vector<std::string> commands;
1647 // Write the all rule.
1648 std::string dir;
1649 std::string recursiveTarget = this->Makefile->GetStartOutputDirectory();
1650 recursiveTarget += "/all";
1652 depends.push_back("cmake_check_build_system");
1654 std::string progressDir = this->Makefile->GetHomeOutputDirectory();
1655 progressDir += cmake::GetCMakeFilesDirectory();
1657 cmOStringStream progCmd;
1658 progCmd <<
1659 "$(CMAKE_COMMAND) -E cmake_progress_start ";
1660 progCmd << this->Convert(progressDir.c_str(),
1661 cmLocalGenerator::FULL,
1662 cmLocalGenerator::SHELL);
1664 std::string progressFile = cmake::GetCMakeFilesDirectory();
1665 progressFile += "/progress.make";
1666 std::string progressFileNameFull =
1667 this->ConvertToFullPath(progressFile.c_str());
1668 progCmd << " " << this->Convert(progressFileNameFull.c_str(),
1669 cmLocalGenerator::FULL,
1670 cmLocalGenerator::SHELL);
1671 commands.push_back(progCmd.str());
1673 std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
1674 mf2Dir += "Makefile2";
1675 commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
1676 recursiveTarget.c_str()));
1677 this->CreateCDCommand(commands,
1678 this->Makefile->GetHomeOutputDirectory(),
1679 this->Makefile->GetStartOutputDirectory());
1681 cmOStringStream progCmd;
1682 progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
1683 progCmd << this->Convert(progressDir.c_str(),
1684 cmLocalGenerator::FULL,
1685 cmLocalGenerator::SHELL);
1686 progCmd << " 0";
1687 commands.push_back(progCmd.str());
1689 this->WriteMakeRule(ruleFileStream, "The main all target", "all",
1690 depends, commands, true);
1692 // Write the clean rule.
1693 recursiveTarget = this->Makefile->GetStartOutputDirectory();
1694 recursiveTarget += "/clean";
1695 commands.clear();
1696 depends.clear();
1697 commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
1698 recursiveTarget.c_str()));
1699 this->CreateCDCommand(commands,
1700 this->Makefile->GetHomeOutputDirectory(),
1701 this->Makefile->GetStartOutputDirectory());
1702 this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
1703 depends, commands, true);
1704 commands.clear();
1705 depends.clear();
1706 depends.push_back("clean");
1707 this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
1708 depends, commands, true);
1710 // Write the preinstall rule.
1711 recursiveTarget = this->Makefile->GetStartOutputDirectory();
1712 recursiveTarget += "/preinstall";
1713 commands.clear();
1714 depends.clear();
1715 const char* noall =
1716 this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
1717 if(!noall || cmSystemTools::IsOff(noall))
1719 // Drive the build before installing.
1720 depends.push_back("all");
1722 else
1724 // At least make sure the build system is up to date.
1725 depends.push_back("cmake_check_build_system");
1727 commands.push_back
1728 (this->GetRecursiveMakeCall(mf2Dir.c_str(), recursiveTarget.c_str()));
1729 this->CreateCDCommand(commands,
1730 this->Makefile->GetHomeOutputDirectory(),
1731 this->Makefile->GetStartOutputDirectory());
1732 this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
1733 "preinstall", depends, commands, true);
1734 depends.clear();
1735 this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
1736 "preinstall/fast", depends, commands, true);
1738 // write the depend rule, really a recompute depends rule
1739 depends.clear();
1740 commands.clear();
1741 std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
1742 cmakefileName += "Makefile.cmake";
1743 std::string runRule =
1744 "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
1745 runRule += " --check-build-system ";
1746 runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
1747 cmLocalGenerator::SHELL);
1748 runRule += " 1";
1749 commands.push_back(runRule);
1750 this->CreateCDCommand(commands,
1751 this->Makefile->GetHomeOutputDirectory(),
1752 this->Makefile->GetStartOutputDirectory());
1753 this->WriteMakeRule(ruleFileStream, "clear depends",
1754 "depend",
1755 depends, commands, true);
1759 //----------------------------------------------------------------------------
1760 void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile* mf,
1761 bool verbose)
1763 // Get the list of target files to check
1764 const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
1765 if(!infoDef)
1767 return;
1769 std::vector<std::string> files;
1770 cmSystemTools::ExpandListArgument(infoDef, files);
1772 // Each depend information file corresponds to a target. Clear the
1773 // dependencies for that target.
1774 cmDepends clearer;
1775 clearer.SetVerbose(verbose);
1776 for(std::vector<std::string>::iterator l = files.begin();
1777 l != files.end(); ++l)
1779 std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
1781 // Clear the implicit dependency makefile.
1782 std::string dependFile = dir + "/depend.make";
1783 clearer.Clear(dependFile.c_str());
1785 // Remove the internal dependency check file to force
1786 // regeneration.
1787 std::string internalDependFile = dir + "/depend.internal";
1788 cmSystemTools::RemoveFile(internalDependFile.c_str());
1793 void cmLocalUnixMakefileGenerator3
1794 ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
1796 ImplicitDependLanguageMap const& implicitLangs =
1797 this->GetImplicitDepends(target);
1799 // list the languages
1800 cmakefileStream
1801 << "# The set of languages for which implicit dependencies are needed:\n";
1802 cmakefileStream
1803 << "SET(CMAKE_DEPENDS_LANGUAGES\n";
1804 for(ImplicitDependLanguageMap::const_iterator
1805 l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
1807 cmakefileStream << " \"" << l->first.c_str() << "\"\n";
1809 cmakefileStream << " )\n";
1811 // now list the files for each language
1812 cmakefileStream
1813 << "# The set of files for implicit dependencies of each language:\n";
1814 for(ImplicitDependLanguageMap::const_iterator
1815 l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
1817 cmakefileStream
1818 << "SET(CMAKE_DEPENDS_CHECK_" << l->first.c_str() << "\n";
1819 ImplicitDependFileMap const& implicitPairs = l->second;
1821 // for each file pair
1822 for(ImplicitDependFileMap::const_iterator pi = implicitPairs.begin();
1823 pi != implicitPairs.end(); ++pi)
1825 cmakefileStream << " \"" << pi->second << "\" ";
1826 cmakefileStream << "\"" << pi->first << "\"\n";
1828 cmakefileStream << " )\n";
1830 // Tell the dependency scanner what compiler is used.
1831 std::string cidVar = "CMAKE_";
1832 cidVar += l->first;
1833 cidVar += "_COMPILER_ID";
1834 const char* cid = this->Makefile->GetDefinition(cidVar.c_str());
1835 if(cid && *cid)
1837 cmakefileStream
1838 << "SET(CMAKE_" << l->first.c_str() << "_COMPILER_ID \""
1839 << cid << "\")\n";
1843 // Build a list of preprocessor definitions for the target.
1844 std::vector<std::string> defines;
1846 std::string defPropName = "COMPILE_DEFINITIONS_";
1847 defPropName += cmSystemTools::UpperCase(this->ConfigurationName);
1848 if(const char* ddefs = this->Makefile->GetProperty("COMPILE_DEFINITIONS"))
1850 cmSystemTools::ExpandListArgument(ddefs, defines);
1852 if(const char* cdefs = target.GetProperty("COMPILE_DEFINITIONS"))
1854 cmSystemTools::ExpandListArgument(cdefs, defines);
1856 if(const char* dcdefs = this->Makefile->GetProperty(defPropName.c_str()))
1858 cmSystemTools::ExpandListArgument(dcdefs, defines);
1860 if(const char* ccdefs = target.GetProperty(defPropName.c_str()))
1862 cmSystemTools::ExpandListArgument(ccdefs, defines);
1865 if(!defines.empty())
1867 cmakefileStream
1868 << "\n"
1869 << "# Preprocessor definitions for this target.\n"
1870 << "SET(CMAKE_TARGET_DEFINITIONS\n";
1871 for(std::vector<std::string>::const_iterator di = defines.begin();
1872 di != defines.end(); ++di)
1874 cmakefileStream
1875 << " " << this->EscapeForCMake(di->c_str()) << "\n";
1877 cmakefileStream
1878 << " )\n";
1882 //----------------------------------------------------------------------------
1883 std::string
1884 cmLocalUnixMakefileGenerator3
1885 ::GetObjectFileName(cmTarget& target,
1886 const cmSourceFile& source,
1887 std::string* nameWithoutTargetDir,
1888 bool* hasSourceExtension)
1890 if(const char* fileTargetDirectory =
1891 source.GetProperty("MACOSX_PACKAGE_LOCATION"))
1893 // Special handling for OSX package files.
1894 std::string objectName =
1895 this->GetObjectFileNameWithoutTarget(source, 0,
1896 hasSourceExtension);
1897 if(nameWithoutTargetDir)
1899 *nameWithoutTargetDir = objectName;
1901 objectName = cmSystemTools::GetFilenameName(objectName.c_str());
1902 std::string targetName;
1903 std::string targetNameReal;
1904 std::string targetNameImport;
1905 std::string targetNamePDB;
1906 target.GetExecutableNames(targetName, targetNameReal, targetNameImport,
1907 targetNamePDB, this->ConfigurationName.c_str());
1908 std::string obj;
1910 // Construct the full path version of the names.
1912 // If target is a MACOSX_BUNDLE target, then the package location is
1913 // relative to "${targetDir}/${targetName}.app/Contents"... else it is
1914 // relative to "${targetDir}"...
1916 obj = target.GetDirectory();
1917 obj += "/";
1918 if ( target.GetPropertyAsBool("MACOSX_BUNDLE") )
1920 obj += targetName + ".app/Contents/";
1922 else
1924 // Emit warning here...? MACOSX_PACKAGE_LOCATION is "most useful" in a
1925 // MACOSX_BUNDLE...
1927 obj += fileTargetDirectory;
1929 // Object names are specified relative to the current build dir.
1930 obj = this->Convert(obj.c_str(), START_OUTPUT);
1931 obj += "/";
1932 obj += objectName;
1933 return obj;
1935 else
1937 // Start with the target directory.
1938 std::string obj = this->GetTargetDirectory(target);
1939 obj += "/";
1941 // Get the object file name without the target directory.
1942 std::string::size_type dir_len = 0;
1943 dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
1944 dir_len += 1;
1945 dir_len += obj.size();
1946 std::string objectName =
1947 this->GetObjectFileNameWithoutTarget(source, dir_len,
1948 hasSourceExtension);
1949 if(nameWithoutTargetDir)
1951 *nameWithoutTargetDir = objectName;
1954 // Append the object name to the target directory.
1955 obj += objectName;
1956 return obj;
1960 //----------------------------------------------------------------------------
1961 void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
1964 << "# CMAKE generated file: DO NOT EDIT!\n"
1965 << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
1966 << " Generator, CMake Version "
1967 << cmVersion::GetMajorVersion() << "."
1968 << cmVersion::GetMinorVersion() << "\n\n";
1971 //----------------------------------------------------------------------------
1972 std::string
1973 cmLocalUnixMakefileGenerator3
1974 ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
1976 // Call make on the given file.
1977 std::string cmd;
1978 cmd += "$(MAKE) -f ";
1979 cmd += this->Convert(makefile,NONE,SHELL);
1980 cmd += " ";
1982 // Passg down verbosity level.
1983 if(this->GetMakeSilentFlag().size())
1985 cmd += this->GetMakeSilentFlag();
1986 cmd += " ";
1989 // Most unix makes will pass the command line flags to make down to
1990 // sub-invoked makes via an environment variable. However, some
1991 // makes do not support that, so you have to pass the flags
1992 // explicitly.
1993 if(this->GetPassMakeflags())
1995 cmd += "-$(MAKEFLAGS) ";
1998 // Add the target.
1999 if (tgt && tgt[0] != '\0')
2001 // The make target is always relative to the top of the build tree.
2002 std::string tgt2 = this->Convert(tgt, HOME_OUTPUT);
2004 // The target may have been written with windows paths.
2005 cmSystemTools::ConvertToOutputSlashes(tgt2);
2007 // Escape one extra time if the make tool requires it.
2008 if(this->MakeCommandEscapeTargetTwice)
2010 tgt2 = this->EscapeForShell(tgt2.c_str(), true, false);
2013 // The target name is now a string that should be passed verbatim
2014 // on the command line.
2015 cmd += this->EscapeForShell(tgt2.c_str(), true, false);
2017 return cmd;
2020 //----------------------------------------------------------------------------
2021 void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
2024 << "#======================================"
2025 << "=======================================\n";
2028 //----------------------------------------------------------------------------
2029 void
2030 cmLocalUnixMakefileGenerator3
2031 ::WriteCMakeArgument(std::ostream& os, const char* s)
2033 // Write the given string to the stream with escaping to get it back
2034 // into CMake through the lexical scanner.
2035 os << "\"";
2036 for(const char* c = s; *c; ++c)
2038 if(*c == '\\')
2040 os << "\\\\";
2042 else if(*c == '"')
2044 os << "\\\"";
2046 else
2048 os << *c;
2051 os << "\"";
2054 //----------------------------------------------------------------------------
2055 std::string
2056 cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
2059 // Split the path into its components.
2060 std::vector<std::string> components;
2061 cmSystemTools::SplitPath(p, components);
2063 // Return an empty path if there are no components.
2064 if(components.empty())
2066 return "\"\"";
2069 // Choose a slash direction and fix root component.
2070 const char* slash = "/";
2071 #if defined(_WIN32) && !defined(__CYGWIN__)
2072 if(!cmSystemTools::GetForceUnixPaths())
2074 slash = "\\";
2075 for(std::string::iterator i = components[0].begin();
2076 i != components[0].end(); ++i)
2078 if(*i == '/')
2080 *i = '\\';
2084 #endif
2086 // Begin the quoted result with the root component.
2087 std::string result = "\"";
2088 result += components[0];
2090 // Now add the rest of the components separated by the proper slash
2091 // direction for this platform.
2092 bool first = true;
2093 for(unsigned int i=1; i < components.size(); ++i)
2095 // Only the last component can be empty to avoid double slashes.
2096 if(components[i].length() > 0 || (i == (components.size()-1)))
2098 if(!first)
2100 result += slash;
2102 result += components[i];
2103 first = false;
2107 // Close the quoted result.
2108 result += "\"";
2110 return result;
2113 //----------------------------------------------------------------------------
2114 std::string
2115 cmLocalUnixMakefileGenerator3
2116 ::GetTargetDirectory(cmTarget const& target) const
2118 std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
2119 dir += target.GetName();
2120 dir += ".dir";
2121 return dir;
2124 //----------------------------------------------------------------------------
2125 cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap const&
2126 cmLocalUnixMakefileGenerator3::GetImplicitDepends(cmTarget const& tgt)
2128 return this->ImplicitDepends[tgt.GetName()];
2131 //----------------------------------------------------------------------------
2132 void
2133 cmLocalUnixMakefileGenerator3::AddImplicitDepends(cmTarget const& tgt,
2134 const char* lang,
2135 const char* obj,
2136 const char* src)
2138 this->ImplicitDepends[tgt.GetName()][lang][obj] = src;
2141 //----------------------------------------------------------------------------
2142 void cmLocalUnixMakefileGenerator3
2143 ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
2144 const char *retDir)
2146 // do we need to cd?
2147 if (!strcmp(tgtDir,retDir))
2149 return;
2152 if(!this->UnixCD)
2154 // On Windows we must perform each step separately and then change
2155 // back because the shell keeps the working directory between
2156 // commands.
2157 std::string cmd = "cd ";
2158 cmd += this->ConvertToOutputForExisting(tgtDir);
2159 commands.insert(commands.begin(),cmd);
2161 // Change back to the starting directory. Any trailing slash must be
2162 // removed to avoid problems with Borland Make.
2163 std::string back = retDir;
2164 if(back.size() && back[back.size()-1] == '/')
2166 back = back.substr(0, back.size()-1);
2168 cmd = "cd ";
2169 cmd += this->ConvertToOutputForExisting(back.c_str());
2170 commands.push_back(cmd);
2172 else
2174 // On UNIX we must construct a single shell command to change
2175 // directory and build because make resets the directory between
2176 // each command.
2177 std::vector<std::string>::iterator i = commands.begin();
2178 for (; i != commands.end(); ++i)
2180 std::string cmd = "cd ";
2181 cmd += this->ConvertToOutputForExisting(tgtDir);
2182 cmd += " && ";
2183 cmd += *i;
2184 *i = cmd;
2190 void cmLocalUnixMakefileGenerator3
2191 ::GetTargetObjectFileDirectories(cmTarget* target,
2192 std::vector<std::string>& dirs)
2194 std::string dir = this->Makefile->GetCurrentOutputDirectory();
2195 dir += "/";
2196 dir += this->GetTargetDirectory(*target);
2197 dirs.push_back(dir);