1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalUnixMakefileGenerator3.cxx,v $
6 Date: $Date: 2008-03-18 15:51:23 $
7 Version: $Revision: 1.243 $
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"
26 #include "cmVersion.h"
27 #include "cmFileTimeComparison.h"
29 // Include dependency scanners for supported languages. Only the
30 // C/C++ scanner is needed for bootstrapping CMake.
31 #include "cmDependsC.h"
32 #ifdef CMAKE_BUILD_WITH_CMAKE
33 # include "cmDependsFortran.h"
34 # include "cmDependsJava.h"
35 # include <cmsys/Terminal.h>
38 #include <memory> // auto_ptr
41 //----------------------------------------------------------------------------
42 // Helper function used below.
43 static std::string
cmSplitExtension(std::string
const& in
, std::string
& base
)
46 std::string::size_type dot_pos
= in
.rfind(".");
47 if(dot_pos
!= std::string::npos
)
49 // Remove the extension first in case &base == &in.
50 ext
= in
.substr(dot_pos
, std::string::npos
);
51 base
= in
.substr(0, dot_pos
);
60 //----------------------------------------------------------------------------
61 cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
63 this->SilentNoColon
= false;
64 this->WindowsShell
= false;
65 this->IncludeDirective
= "include";
66 this->MakefileVariableSize
= 0;
67 this->IgnoreLibPrefix
= false;
68 this->PassMakeflags
= false;
69 this->DefineWindowsNULL
= false;
71 this->ColorMakefile
= false;
72 this->SkipPreprocessedSourceRules
= false;
73 this->SkipAssemblySourceRules
= false;
74 this->NativeEchoCommand
= "@echo ";
75 this->NativeEchoWindows
= true;
76 this->MakeCommandEscapeTargetTwice
= false;
77 this->IsMakefileGenerator
= true;
78 this->BorlandMakeCurlyHack
= false;
81 //----------------------------------------------------------------------------
82 cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
86 //----------------------------------------------------------------------------
87 void cmLocalUnixMakefileGenerator3::Configure()
89 // Compute the path to use when referencing the current output
90 // directory from the top output directory.
91 this->HomeRelativeOutputPath
=
92 this->Convert(this->Makefile
->GetStartOutputDirectory(), HOME_OUTPUT
);
93 if(this->HomeRelativeOutputPath
== ".")
95 this->HomeRelativeOutputPath
= "";
97 if(!this->HomeRelativeOutputPath
.empty())
99 this->HomeRelativeOutputPath
+= "/";
101 this->cmLocalGenerator::Configure();
104 //----------------------------------------------------------------------------
105 void cmLocalUnixMakefileGenerator3::Generate()
107 // Store the configuration name that will be generated.
108 if(const char* config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE"))
110 // Use the build type given by the user.
111 this->ConfigurationName
= config
;
115 // No configuration type given.
116 this->ConfigurationName
= "";
119 // Record whether some options are enabled to avoid checking many
121 this->ColorMakefile
= this->Makefile
->IsOn("CMAKE_COLOR_MAKEFILE");
122 this->SkipPreprocessedSourceRules
=
123 this->Makefile
->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");
124 this->SkipAssemblySourceRules
=
125 this->Makefile
->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
127 // Generate the rule files for each target.
128 cmTargets
& targets
= this->Makefile
->GetTargets();
130 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
132 cmMakefileTargetGenerator
*tg
=
133 cmMakefileTargetGenerator::New(&(t
->second
));
136 this->TargetGenerators
.push_back(tg
);
137 tg
->WriteRuleFiles();
141 // write the local Makefile
142 this->WriteLocalMakefile();
144 // Write the cmake file with information for this directory.
145 this->WriteDirectoryInformationFile();
148 //----------------------------------------------------------------------------
149 // return info about progress actions
150 unsigned long cmLocalUnixMakefileGenerator3::GetNumberOfProgressActions()
152 unsigned long result
= 0;
154 for (std::vector
<cmMakefileTargetGenerator
*>::iterator mtgIter
=
155 this->TargetGenerators
.begin();
156 mtgIter
!= this->TargetGenerators
.end(); ++mtgIter
)
158 result
+= (*mtgIter
)->GetNumberOfProgressActions();
163 //----------------------------------------------------------------------------
164 // return info about progress actions
165 unsigned long cmLocalUnixMakefileGenerator3
166 ::GetNumberOfProgressActionsForTarget(const char *name
)
168 for (std::vector
<cmMakefileTargetGenerator
*>::iterator mtgIter
=
169 this->TargetGenerators
.begin();
170 mtgIter
!= this->TargetGenerators
.end(); ++mtgIter
)
172 if (!strcmp(name
,(*mtgIter
)->GetTarget()->GetName()))
174 return (*mtgIter
)->GetNumberOfProgressActions();
181 //----------------------------------------------------------------------------
182 // writes the progreess variables and also closes out the targets
183 void cmLocalUnixMakefileGenerator3
184 ::WriteProgressVariables(unsigned long total
,
185 unsigned long ¤t
)
187 // delete the makefile target generator objects
188 for (std::vector
<cmMakefileTargetGenerator
*>::iterator mtgIter
=
189 this->TargetGenerators
.begin();
190 mtgIter
!= this->TargetGenerators
.end(); ++mtgIter
)
192 (*mtgIter
)->WriteProgressVariables(total
,current
);
195 this->TargetGenerators
.clear();
198 void cmLocalUnixMakefileGenerator3::WriteAllProgressVariable()
200 // write the top level progress for the all target
201 std::string progressFile
= cmake::GetCMakeFilesDirectory();
202 progressFile
+= "/progress.make";
203 std::string progressFileNameFull
=
204 this->ConvertToFullPath(progressFile
.c_str());
205 cmGeneratedFileStream
ruleFileStream(progressFileNameFull
.c_str());
211 cmGlobalUnixMakefileGenerator3
*gg
=
212 static_cast<cmGlobalUnixMakefileGenerator3
*>(this->GlobalGenerator
);
214 ruleFileStream
<< gg
->GetNumberOfProgressActionsInAll(this) << "\n";
217 //----------------------------------------------------------------------------
218 void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
220 // generate the includes
221 std::string ruleFileName
= "Makefile";
223 // Open the rule file. This should be copy-if-different because the
224 // rules may depend on this file itself.
225 std::string ruleFileNameFull
= this->ConvertToFullPath(ruleFileName
);
226 cmGeneratedFileStream
ruleFileStream(ruleFileNameFull
.c_str());
231 // always write the top makefile
234 ruleFileStream
.SetCopyIfDifferent(true);
237 // write the all rules
238 this->WriteLocalAllRules(ruleFileStream
);
240 // only write local targets unless at the top Keep track of targets already
242 std::set
<cmStdString
> emittedTargets
;
245 // write our targets, and while doing it collect up the object
247 this->WriteLocalMakefileTargets(ruleFileStream
,emittedTargets
);
251 cmGlobalUnixMakefileGenerator3
*gg
=
252 static_cast<cmGlobalUnixMakefileGenerator3
*>(this->GlobalGenerator
);
253 gg
->WriteConvenienceRules(ruleFileStream
,emittedTargets
);
256 bool do_preprocess_rules
=
257 this->GetCreatePreprocessedSourceRules();
258 bool do_assembly_rules
=
259 this->GetCreateAssemblySourceRules();
261 // now write out the object rules
262 // for each object file name
263 for (std::map
<cmStdString
, LocalObjectInfo
>::iterator lo
=
264 this->LocalObjectFiles
.begin();
265 lo
!= this->LocalObjectFiles
.end(); ++lo
)
267 // Add a convenience rule for building the object file.
268 this->WriteObjectConvenienceRule(ruleFileStream
,
269 "target to build an object file",
270 lo
->first
.c_str(), lo
->second
);
272 // Check whether preprocessing and assembly rules make sense.
273 // They make sense only for C and C++ sources.
274 bool lang_is_c_or_cxx
= false;
275 for(std::vector
<LocalObjectEntry
>::const_iterator ei
=
276 lo
->second
.begin(); ei
!= lo
->second
.end(); ++ei
)
278 if(ei
->Language
== "C" || ei
->Language
== "CXX")
280 lang_is_c_or_cxx
= true;
284 // Add convenience rules for preprocessed and assembly files.
285 if(lang_is_c_or_cxx
&& (do_preprocess_rules
|| do_assembly_rules
))
287 std::string::size_type dot_pos
= lo
->first
.rfind(".");
288 std::string base
= lo
->first
.substr(0, dot_pos
);
289 if(do_preprocess_rules
)
291 this->WriteObjectConvenienceRule(
292 ruleFileStream
, "target to preprocess a source file",
293 (base
+ ".i").c_str(), lo
->second
);
295 if(do_assembly_rules
)
297 this->WriteObjectConvenienceRule(
298 ruleFileStream
, "target to generate assembly for a file",
299 (base
+ ".s").c_str(), lo
->second
);
304 // add a help target as long as there isn;t a real target named help
305 if(emittedTargets
.insert("help").second
)
307 cmGlobalUnixMakefileGenerator3
*gg
=
308 static_cast<cmGlobalUnixMakefileGenerator3
*>(this->GlobalGenerator
);
309 gg
->WriteHelpRule(ruleFileStream
,this);
312 this->WriteSpecialTargetsBottom(ruleFileStream
);
315 //----------------------------------------------------------------------------
317 cmLocalUnixMakefileGenerator3
318 ::WriteObjectConvenienceRule(std::ostream
& ruleFileStream
,
319 const char* comment
, const char* output
,
320 LocalObjectInfo
const& info
)
322 // If the rule includes the source file extension then create a
323 // version that has the extension removed. The help should include
324 // only the version without source extension.
326 if(info
.HasSourceExtension
)
328 // Remove the last extension. This should be kept.
329 std::string outBase1
= output
;
330 std::string outExt1
= cmSplitExtension(outBase1
, outBase1
);
332 // Now remove the source extension and put back the last
334 std::string outNoExt
;
335 cmSplitExtension(outBase1
, outNoExt
);
338 // Add a rule to drive the rule below.
339 std::vector
<std::string
> depends
;
340 depends
.push_back(output
);
341 std::vector
<std::string
> no_commands
;
342 this->WriteMakeRule(ruleFileStream
, 0,
343 outNoExt
.c_str(), depends
, no_commands
, true, true);
347 // Recursively make the rule for each target using the object file.
348 std::vector
<std::string
> commands
;
349 for(std::vector
<LocalObjectEntry
>::const_iterator t
= info
.begin();
350 t
!= info
.end(); ++t
)
352 std::string tgtMakefileName
=
353 this->GetRelativeTargetDirectory(*(t
->Target
));
354 std::string targetName
= tgtMakefileName
;
355 tgtMakefileName
+= "/build.make";
357 targetName
+= output
;
359 this->GetRecursiveMakeCall(tgtMakefileName
.c_str(), targetName
.c_str())
361 this->CreateCDCommand(commands
,
362 this->Makefile
->GetHomeOutputDirectory(),
363 this->Makefile
->GetStartOutputDirectory());
366 // Write the rule to the makefile.
367 std::vector
<std::string
> no_depends
;
368 this->WriteMakeRule(ruleFileStream
, comment
,
369 output
, no_depends
, commands
, true, inHelp
);
372 //----------------------------------------------------------------------------
373 void cmLocalUnixMakefileGenerator3
374 ::WriteLocalMakefileTargets(std::ostream
& ruleFileStream
,
375 std::set
<cmStdString
> &emitted
)
377 std::vector
<std::string
> depends
;
378 std::vector
<std::string
> commands
;
380 // for each target we just provide a rule to cd up to the top and do a make
382 cmTargets
& targets
= this->Makefile
->GetTargets();
383 std::string localName
;
384 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
386 if((t
->second
.GetType() == cmTarget::EXECUTABLE
) ||
387 (t
->second
.GetType() == cmTarget::STATIC_LIBRARY
) ||
388 (t
->second
.GetType() == cmTarget::SHARED_LIBRARY
) ||
389 (t
->second
.GetType() == cmTarget::MODULE_LIBRARY
) ||
390 (t
->second
.GetType() == cmTarget::UTILITY
))
392 emitted
.insert(t
->second
.GetName());
394 // for subdirs add a rule to build this specific target by name.
395 localName
= this->GetRelativeTargetDirectory(t
->second
);
396 localName
+= "/rule";
400 // Build the target for this pass.
401 std::string makefile2
= cmake::GetCMakeFilesDirectoryPostSlash();
402 makefile2
+= "Makefile2";
403 commands
.push_back(this->GetRecursiveMakeCall
404 (makefile2
.c_str(),localName
.c_str()));
405 this->CreateCDCommand(commands
,
406 this->Makefile
->GetHomeOutputDirectory(),
407 this->Makefile
->GetStartOutputDirectory());
408 this->WriteMakeRule(ruleFileStream
, "Convenience name for target.",
409 localName
.c_str(), depends
, commands
, true);
411 // Add a target with the canonical name (no prefix, suffix or path).
412 if(localName
!= t
->second
.GetName())
415 depends
.push_back(localName
);
416 this->WriteMakeRule(ruleFileStream
, "Convenience name for target.",
417 t
->second
.GetName(), depends
, commands
, true);
420 // Add a fast rule to build the target
421 std::string makefileName
= this->GetRelativeTargetDirectory(t
->second
);
422 makefileName
+= "/build.make";
423 // make sure the makefile name is suitable for a makefile
424 std::string makeTargetName
=
425 this->GetRelativeTargetDirectory(t
->second
);
426 makeTargetName
+= "/build";
427 localName
= t
->second
.GetName();
428 localName
+= "/fast";
431 commands
.push_back(this->GetRecursiveMakeCall
432 (makefileName
.c_str(), makeTargetName
.c_str()));
433 this->CreateCDCommand(commands
,
434 this->Makefile
->GetHomeOutputDirectory(),
435 this->Makefile
->GetStartOutputDirectory());
436 this->WriteMakeRule(ruleFileStream
, "fast build rule for target.",
437 localName
.c_str(), depends
, commands
, true);
439 // Add a local name for the rule to relink the target before
441 if(t
->second
.NeedRelinkBeforeInstall())
443 makeTargetName
= this->GetRelativeTargetDirectory(t
->second
);
444 makeTargetName
+= "/preinstall";
445 localName
= t
->second
.GetName();
446 localName
+= "/preinstall";
449 commands
.push_back(this->GetRecursiveMakeCall
450 (makefile2
.c_str(), makeTargetName
.c_str()));
451 this->CreateCDCommand(commands
,
452 this->Makefile
->GetHomeOutputDirectory(),
453 this->Makefile
->GetStartOutputDirectory());
454 this->WriteMakeRule(ruleFileStream
,
455 "Manual pre-install relink rule for target.",
456 localName
.c_str(), depends
, commands
, true);
462 //----------------------------------------------------------------------------
463 void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
465 std::string infoFileName
= this->Makefile
->GetStartOutputDirectory();
466 infoFileName
+= cmake::GetCMakeFilesDirectory();
467 infoFileName
+= "/CMakeDirectoryInformation.cmake";
469 // Open the output file.
470 cmGeneratedFileStream
infoFileStream(infoFileName
.c_str());
476 // Write the do not edit header.
477 this->WriteDisclaimer(infoFileStream
);
479 // Setup relative path conversion tops.
481 << "# Relative path conversion top directories.\n"
482 << "SET(CMAKE_RELATIVE_PATH_TOP_SOURCE \"" << this->RelativePathTopSource
484 << "SET(CMAKE_RELATIVE_PATH_TOP_BINARY \"" << this->RelativePathTopBinary
488 // Tell the dependency scanner to use unix paths if necessary.
489 if(cmSystemTools::GetForceUnixPaths())
492 << "# Force unix paths in dependencies.\n"
493 << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
497 // Store the include search path for this directory.
499 << "# The C and CXX include file search paths:\n";
501 << "SET(CMAKE_C_INCLUDE_PATH\n";
502 std::vector
<std::string
> includeDirs
;
503 this->GetIncludeDirectories(includeDirs
, false);
504 for(std::vector
<std::string
>::iterator i
= includeDirs
.begin();
505 i
!= includeDirs
.end(); ++i
)
508 << " \"" << this->Convert(i
->c_str(),HOME_OUTPUT
).c_str() << "\"\n";
513 << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
515 << "SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
517 // Store the include regular expressions for this directory.
520 << "# The C and CXX include file regular expressions for "
521 << "this directory.\n";
523 << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
524 this->WriteCMakeArgument(infoFileStream
,
525 this->Makefile
->GetIncludeRegularExpression());
529 << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
530 this->WriteCMakeArgument(infoFileStream
,
531 this->Makefile
->GetComplainRegularExpression());
535 << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
537 << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
538 "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
541 //----------------------------------------------------------------------------
543 cmLocalUnixMakefileGenerator3
544 ::ConvertToFullPath(const std::string
& localPath
)
546 std::string dir
= this->Makefile
->GetStartOutputDirectory();
553 const std::string
&cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
555 return this->HomeRelativeOutputPath
;
559 //----------------------------------------------------------------------------
561 cmLocalUnixMakefileGenerator3
562 ::WriteMakeRule(std::ostream
& os
,
565 const std::vector
<std::string
>& depends
,
566 const std::vector
<std::string
>& commands
,
570 // Make sure there is a target.
571 if(!target
|| !*target
)
573 cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
580 // Write the comment describing the rule in the makefile.
584 std::string::size_type lpos
= 0;
585 std::string::size_type rpos
;
586 while((rpos
= replace
.find('\n', lpos
)) != std::string::npos
)
588 os
<< "# " << replace
.substr(lpos
, rpos
-lpos
) << "\n";
591 os
<< "# " << replace
.substr(lpos
) << "\n";
594 // Construct the left hand side of the rule.
596 std::string tgt
= this->Convert(replace
.c_str(),HOME_OUTPUT
,MAKEFILE
);
597 const char* space
= "";
600 // Add a space before the ":" to avoid drive letter confusion on
605 // Mark the rule as symbolic if requested.
609 this->Makefile
->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
611 os
<< tgt
.c_str() << space
<< ": " << sym
<< "\n";
618 // No dependencies. The commands will always run.
619 os
<< tgt
.c_str() << space
<< ":\n";
623 // Split dependencies into multiple rule lines. This allows for
624 // very long dependency lists even on older make implementations.
625 for(std::vector
<std::string
>::const_iterator dep
= depends
.begin();
626 dep
!= depends
.end(); ++dep
)
629 replace
= this->Convert(replace
.c_str(),HOME_OUTPUT
,MAKEFILE
);
630 os
<< tgt
.c_str() << space
<< ": " << replace
.c_str() << "\n";
634 // Write the list of commands.
635 for(std::vector
<std::string
>::const_iterator i
= commands
.begin();
636 i
!= commands
.end(); ++i
)
639 os
<< "\t" << replace
.c_str() << "\n";
643 // Add the output to the local help if requested.
646 this->LocalHelp
.push_back(target
);
650 //----------------------------------------------------------------------------
652 cmLocalUnixMakefileGenerator3
653 ::WriteMakeVariables(std::ostream
& makefileStream
)
655 this->WriteDivider(makefileStream
);
657 << "# Set environment variables for the build.\n"
659 if(this->DefineWindowsNULL
)
662 << "!IF \"$(OS)\" == \"Windows_NT\"\n"
668 if(this->WindowsShell
)
671 << "SHELL = cmd.exe\n"
677 << "# The shell in which to execute make rules.\n"
678 << "SHELL = /bin/sh\n"
682 std::string cmakecommand
=
683 this->Makefile
->GetRequiredDefinition("CMAKE_COMMAND");
685 << "# The CMake executable.\n"
686 << "CMAKE_COMMAND = "
687 << this->Convert(cmakecommand
.c_str(), FULL
, SHELL
).c_str()
691 << "# The command to remove a file.\n"
693 << this->Convert(cmakecommand
.c_str(),FULL
,SHELL
).c_str()
697 if(this->Makefile
->GetDefinition("CMAKE_EDIT_COMMAND"))
700 << "# The program to use to edit the cache.\n"
701 << "CMAKE_EDIT_COMMAND = "
702 << (this->ConvertToOutputForExisting(
703 this->Makefile
->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
708 << "# The top-level source directory on which CMake was run.\n"
709 << "CMAKE_SOURCE_DIR = "
710 << this->Convert(this->Makefile
->GetHomeDirectory(), FULL
, SHELL
)
714 << "# The top-level build directory on which CMake was run.\n"
715 << "CMAKE_BINARY_DIR = "
716 << this->Convert(this->Makefile
->GetHomeOutputDirectory(), FULL
, SHELL
)
721 //----------------------------------------------------------------------------
723 cmLocalUnixMakefileGenerator3
724 ::WriteSpecialTargetsTop(std::ostream
& makefileStream
)
726 this->WriteDivider(makefileStream
);
728 << "# Special targets provided by cmake.\n"
731 std::vector
<std::string
> no_commands
;
732 std::vector
<std::string
> no_depends
;
734 // Special target to cleanup operation of make tool.
735 // This should be the first target except for the default_target in
736 // the interface Makefile.
738 makefileStream
, "Disable implicit rules so canoncical targets will work.",
739 ".SUFFIXES", no_depends
, no_commands
, false);
741 if(!this->NMake
&& !this->WatcomWMake
&& !this->BorlandMakeCurlyHack
)
743 // turn off RCS and SCCS automatic stuff from gmake
745 << "# Remove some rules from gmake that .SUFFIXES does not remove.\n"
746 << "# This makes gmake faster as it does not try to run implicit rules\n"
747 << "# on targets that never exist.\n"
756 << "%: SCCS/s.%\n\n";
758 // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
759 std::vector
<std::string
> depends
;
760 depends
.push_back(".hpux_make_needs_suffix_list");
761 this->WriteMakeRule(makefileStream
, 0,
762 ".SUFFIXES", depends
, no_commands
, false);
764 cmGlobalUnixMakefileGenerator3
* gg
=
765 static_cast<cmGlobalUnixMakefileGenerator3
*>(this->GlobalGenerator
);
766 // Write special target to silence make output. This must be after
767 // the default target in case VERBOSE is set (which changes the
768 // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
769 // "VERBOSE=1" to be added as a make variable which will change the
770 // name of this special target. This gives a make-time choice to
772 if((this->Makefile
->IsOn("CMAKE_VERBOSE_MAKEFILE"))
773 || (gg
->GetForceVerboseMakefiles()))
776 << "# Produce verbose output by default.\n"
780 if(this->SilentNoColon
)
782 makefileStream
<< "$(VERBOSE).SILENT\n";
786 this->WriteMakeRule(makefileStream
,
787 "Suppress display of executed commands.",
793 // Work-around for makes that drop rules that have no dependencies
795 std::string hack
= gg
->GetEmptyRuleHackDepends();
798 no_depends
.push_back(hack
);
800 std::string hack_cmd
= gg
->GetEmptyRuleHackCommand();
801 if(!hack_cmd
.empty())
803 no_commands
.push_back(hack_cmd
);
806 // Special symbolic target that never exists to force dependers to
810 "A target that is always out of date.",
811 "cmake_force", no_depends
, no_commands
, true);
813 // Variables for reference by other rules.
814 this->WriteMakeVariables(makefileStream
);
817 //----------------------------------------------------------------------------
818 void cmLocalUnixMakefileGenerator3
819 ::WriteSpecialTargetsBottom(std::ostream
& makefileStream
)
821 this->WriteDivider(makefileStream
);
823 << "# Special targets to cleanup operation of make.\n"
826 // Write special "cmake_check_build_system" target to run cmake with
827 // the --check-build-system flag.
829 // Build command to run CMake to check if anything needs regenerating.
830 std::string cmakefileName
= cmake::GetCMakeFilesDirectoryPostSlash();
831 cmakefileName
+= "Makefile.cmake";
832 std::string runRule
=
833 "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
834 runRule
+= " --check-build-system ";
835 runRule
+= this->Convert(cmakefileName
.c_str(),NONE
,SHELL
);
838 std::vector
<std::string
> no_depends
;
839 std::vector
<std::string
> commands
;
840 commands
.push_back(runRule
);
843 this->CreateCDCommand(commands
,
844 this->Makefile
->GetHomeOutputDirectory(),
845 this->Makefile
->GetStartOutputDirectory());
847 this->WriteMakeRule(makefileStream
,
848 "Special rule to run CMake to check the build system "
850 "No rule that depends on this can have "
851 "commands that come from listfiles\n"
852 "because they might be regenerated.",
853 "cmake_check_build_system",
861 //----------------------------------------------------------------------------
863 cmLocalUnixMakefileGenerator3
864 ::WriteConvenienceRule(std::ostream
& ruleFileStream
,
865 const char* realTarget
,
866 const char* helpTarget
)
868 // A rule is only needed if the names are different.
869 if(strcmp(realTarget
, helpTarget
) != 0)
871 // The helper target depends on the real target.
872 std::vector
<std::string
> depends
;
873 depends
.push_back(realTarget
);
875 // There are no commands.
876 std::vector
<std::string
> no_commands
;
879 this->WriteMakeRule(ruleFileStream
, "Convenience name for target.",
880 helpTarget
, depends
, no_commands
, true);
885 //----------------------------------------------------------------------------
887 cmLocalUnixMakefileGenerator3
888 ::GetRelativeTargetDirectory(cmTarget
const& target
)
890 std::string dir
= this->HomeRelativeOutputPath
;
891 dir
+= this->GetTargetDirectory(target
);
892 return this->Convert(dir
.c_str(),NONE
,UNCHANGED
);
897 //----------------------------------------------------------------------------
898 void cmLocalUnixMakefileGenerator3::AppendFlags(std::string
& flags
,
899 const char* newFlags
)
901 if(this->WatcomWMake
&& newFlags
&& *newFlags
)
903 std::string newf
= newFlags
;
904 if(newf
.find("\\\"") != newf
.npos
)
906 cmSystemTools::ReplaceString(newf
, "\\\"", "\"");
907 this->cmLocalGenerator::AppendFlags(flags
, newf
.c_str());
911 this->cmLocalGenerator::AppendFlags(flags
, newFlags
);
914 //----------------------------------------------------------------------------
916 cmLocalUnixMakefileGenerator3
917 ::AppendRuleDepend(std::vector
<std::string
>& depends
,
918 const char* ruleFileName
)
920 // Add a dependency on the rule file itself unless an option to skip
921 // it is specifically enabled by the user or project.
923 this->Makefile
->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
924 if(!nodep
|| cmSystemTools::IsOff(nodep
))
926 depends
.push_back(ruleFileName
);
930 //----------------------------------------------------------------------------
932 cmLocalUnixMakefileGenerator3
933 ::AppendCustomDepends(std::vector
<std::string
>& depends
,
934 const std::vector
<cmCustomCommand
>& ccs
)
936 for(std::vector
<cmCustomCommand
>::const_iterator i
= ccs
.begin();
939 this->AppendCustomDepend(depends
, *i
);
943 //----------------------------------------------------------------------------
945 cmLocalUnixMakefileGenerator3
946 ::AppendCustomDepend(std::vector
<std::string
>& depends
,
947 const cmCustomCommand
& cc
)
949 for(std::vector
<std::string
>::const_iterator d
= cc
.GetDepends().begin();
950 d
!= cc
.GetDepends().end(); ++d
)
952 // Lookup the real name of the dependency in case it is a CMake target.
953 std::string dep
= this->GetRealDependency
954 (d
->c_str(), this->ConfigurationName
.c_str());
955 depends
.push_back(dep
);
959 //----------------------------------------------------------------------------
961 cmLocalUnixMakefileGenerator3
962 ::AppendCustomCommands(std::vector
<std::string
>& commands
,
963 const std::vector
<cmCustomCommand
>& ccs
)
965 for(std::vector
<cmCustomCommand
>::const_iterator i
= ccs
.begin();
968 this->AppendCustomCommand(commands
, *i
, true);
972 //----------------------------------------------------------------------------
974 cmLocalUnixMakefileGenerator3
975 ::AppendCustomCommand(std::vector
<std::string
>& commands
,
976 const cmCustomCommand
& cc
, bool echo_comment
)
978 // Optionally create a command to display the custom command's
979 // comment text. This is used for pre-build, pre-link, and
980 // post-build command comments. Custom build step commands have
981 // their comments generated elsewhere.
984 const char* comment
= cc
.GetComment();
985 if(comment
&& *comment
)
987 this->AppendEcho(commands
, comment
,
988 cmLocalUnixMakefileGenerator3::EchoGenerate
);
992 // if the command specified a working directory use it.
993 const char* dir
= this->Makefile
->GetStartOutputDirectory();
994 const char* workingDir
= cc
.GetWorkingDirectory();
999 bool escapeOldStyle
= cc
.GetEscapeOldStyle();
1000 bool escapeAllowMakeVars
= cc
.GetEscapeAllowMakeVars();
1002 // Add each command line to the set of commands.
1003 std::vector
<std::string
> commands1
;
1004 for(cmCustomCommandLines::const_iterator cl
= cc
.GetCommandLines().begin();
1005 cl
!= cc
.GetCommandLines().end(); ++cl
)
1007 // Build the command line in a single string.
1008 const cmCustomCommandLine
& commandLine
= *cl
;
1009 std::string cmd
= GetRealLocation(commandLine
[0].c_str(),
1010 this->ConfigurationName
.c_str());
1013 cmSystemTools::ReplaceString(cmd
, "/./", "/");
1014 // Convert the command to a relative path only if the current
1015 // working directory will be the start-output directory.
1016 bool had_slash
= cmd
.find("/") != cmd
.npos
;
1019 cmd
= this->Convert(cmd
.c_str(),START_OUTPUT
);
1021 bool has_slash
= cmd
.find("/") != cmd
.npos
;
1022 if(had_slash
&& !has_slash
)
1024 // This command was specified as a path to a file in the
1025 // current directory. Add a leading "./" so it can run
1026 // without the current directory being in the search path.
1029 cmd
= this->Convert(cmd
.c_str(),NONE
,SHELL
);
1030 for(unsigned int j
=1; j
< commandLine
.size(); ++j
)
1035 cmd
+= this->EscapeForShellOldStyle(commandLine
[j
].c_str());
1039 cmd
+= this->EscapeForShell(commandLine
[j
].c_str(),
1040 escapeAllowMakeVars
);
1043 if(this->BorlandMakeCurlyHack
)
1045 // Borland Make has a very strange bug. If the first curly
1046 // brace anywhere in the command string is a left curly, it
1047 // must be written {{} instead of just {. Otherwise some
1048 // curly braces are removed. The hack can be skipped if the
1049 // first curly brace is the last character.
1050 std::string::size_type lcurly
= cmd
.find("{");
1051 if(lcurly
!= cmd
.npos
&& lcurly
< (cmd
.size()-1))
1053 std::string::size_type rcurly
= cmd
.find("}");
1054 if(rcurly
== cmd
.npos
|| rcurly
> lcurly
)
1056 // The first curly is a left curly. Use the hack.
1057 std::string hack_cmd
= cmd
.substr(0, lcurly
);
1059 hack_cmd
+= cmd
.substr(lcurly
+1);
1064 commands1
.push_back(cmd
);
1068 // Setup the proper working directory for the commands.
1069 this->CreateCDCommand(commands1
, dir
,
1070 this->Makefile
->GetHomeOutputDirectory());
1072 // push back the custom commands
1073 commands
.insert(commands
.end(), commands1
.begin(), commands1
.end());
1076 //----------------------------------------------------------------------------
1078 cmLocalUnixMakefileGenerator3
1079 ::AppendCleanCommand(std::vector
<std::string
>& commands
,
1080 const std::vector
<std::string
>& files
,
1081 cmTarget
& target
, const char* filename
)
1085 std::string cleanfile
= this->Makefile
->GetCurrentOutputDirectory();
1087 cleanfile
+= this->GetTargetDirectory(target
);
1088 cleanfile
+= "/cmake_clean";
1092 cleanfile
+= filename
;
1094 cleanfile
+= ".cmake";
1095 std::string cleanfilePath
= this->Convert(cleanfile
.c_str(), FULL
);
1096 std::ofstream
fout(cleanfilePath
.c_str());
1099 cmSystemTools::Error("Could not create ", cleanfilePath
.c_str());
1101 fout
<< "FILE(REMOVE_RECURSE\n";
1102 std::string remove
= "$(CMAKE_COMMAND) -P ";
1103 remove
+= this->Convert(cleanfile
.c_str(), START_OUTPUT
, SHELL
);
1104 for(std::vector
<std::string
>::const_iterator f
= files
.begin();
1105 f
!= files
.end(); ++f
)
1107 fout
<< "\"" << this->Convert(f
->c_str(),START_OUTPUT
,UNCHANGED
)
1111 commands
.push_back(remove
);
1113 // For the main clean rule add per-language cleaning.
1116 // Get the set of source languages in the target.
1117 std::set
<cmStdString
> languages
;
1118 target
.GetLanguages(languages
);
1120 << "# Per-language clean rules from dependency scanning.\n"
1122 for(std::set
<cmStdString
>::const_iterator l
= languages
.begin();
1123 l
!= languages
.end(); ++l
)
1128 << " INCLUDE(" << this->GetTargetDirectory(target
)
1129 << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
1130 << "ENDFOREACH(lang)\n";
1135 //----------------------------------------------------------------------------
1137 cmLocalUnixMakefileGenerator3::AppendEcho(std::vector
<std::string
>& commands
,
1141 // Choose the color for the text.
1142 std::string color_name
;
1143 #ifdef CMAKE_BUILD_WITH_CMAKE
1144 if(this->GlobalGenerator
->GetToolSupportsColor() && this->ColorMakefile
)
1146 // See cmake::ExecuteEchoColor in cmake.cxx for these options.
1147 // This color set is readable on both black and white backgrounds.
1153 color_name
= "--magenta --bold ";
1156 color_name
= "--green ";
1159 color_name
= "--red --bold ";
1162 color_name
= "--blue --bold ";
1165 color_name
= "--cyan ";
1173 // Echo one line at a time.
1176 for(const char* c
= text
;; ++c
)
1178 if(*c
== '\n' || *c
== '\0')
1180 // Avoid writing a blank last line on end-of-string.
1181 if(*c
!= '\0' || !line
.empty())
1183 // Add a command to echo this line.
1185 if(color_name
.empty())
1187 // Use the native echo command.
1188 cmd
= this->NativeEchoCommand
;
1189 cmd
+= this->EscapeForShell(line
.c_str(), false,
1190 this->NativeEchoWindows
);
1194 // Use cmake to echo the text in color.
1195 cmd
= "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
1197 cmd
+= this->EscapeForShell(line
.c_str());
1199 commands
.push_back(cmd
);
1202 // Reset the line to emtpy.
1205 // Terminate on end-of-string.
1213 // Append this character to the current line.
1219 //----------------------------------------------------------------------------
1221 cmLocalUnixMakefileGenerator3
1222 ::CreateMakeVariable(const char* sin
, const char* s2in
)
1224 std::string s
= sin
;
1225 std::string s2
= s2in
;
1226 std::string unmodified
= s
;
1228 // if there is no restriction on the length of make variables
1229 // and there are no "." charactors in the string, then return the
1230 // unmodified combination.
1231 if((!this->MakefileVariableSize
&& unmodified
.find('.') == s
.npos
)
1232 && (!this->MakefileVariableSize
&& unmodified
.find('-') == s
.npos
))
1237 // see if the variable has been defined before and return
1238 // the modified version of the variable
1239 std::map
<cmStdString
, cmStdString
>::iterator i
=
1240 this->MakeVariableMap
.find(unmodified
);
1241 if(i
!= this->MakeVariableMap
.end())
1245 // start with the unmodified variable
1246 std::string ret
= unmodified
;
1247 // if this there is no value for this->MakefileVariableSize then
1248 // the string must have bad characters in it
1249 if(!this->MakefileVariableSize
)
1251 cmSystemTools::ReplaceString(ret
, ".", "_");
1252 cmSystemTools::ReplaceString(ret
, "-", "__");
1255 // make sure the _ version is not already used, if
1256 // it is used then add number to the end of the variable
1257 while(this->ShortMakeVariableMap
.count(ret
) && ni
< 1000)
1260 sprintf(buffer
, "%04d", ni
);
1261 ret
= unmodified
+ buffer
;
1263 this->ShortMakeVariableMap
[ret
] = "1";
1264 this->MakeVariableMap
[unmodified
] = ret
;
1268 // if the string is greater the 32 chars it is an invalid vairable name
1270 if(static_cast<int>(ret
.size()) > this->MakefileVariableSize
)
1272 int keep
= this->MakefileVariableSize
- 8;
1273 int size
= keep
+ 3;
1274 std::string str1
= s
;
1275 std::string str2
= s2
;
1276 // we must shorten the combined string by 4 charactors
1277 // keep no more than 24 charactors from the second string
1278 if(static_cast<int>(str2
.size()) > keep
)
1280 str2
= str2
.substr(0, keep
);
1282 if(static_cast<int>(str1
.size()) + static_cast<int>(str2
.size()) > size
)
1284 str1
= str1
.substr(0, size
- str2
.size());
1288 sprintf(buffer
, "%04d", ni
);
1289 ret
= str1
+ str2
+ buffer
;
1290 while(this->ShortMakeVariableMap
.count(ret
) && ni
< 1000)
1293 sprintf(buffer
, "%04d", ni
);
1294 ret
= str1
+ str2
+ buffer
;
1298 cmSystemTools::Error("Borland makefile variable length too long");
1301 // once an unused variable is found
1302 this->ShortMakeVariableMap
[ret
] = "1";
1304 // always make an entry into the unmodified to variable map
1305 this->MakeVariableMap
[unmodified
] = ret
;
1309 //----------------------------------------------------------------------------
1310 bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo
,
1314 // read in the target info file
1315 if(!this->Makefile
->ReadListFile(0, tgtInfo
) ||
1316 cmSystemTools::GetErrorOccuredFlag())
1318 cmSystemTools::Error("Target DependInfo.cmake file not found");
1321 // Check if any multiple output pairs have a missing file.
1322 this->CheckMultipleOutputs(verbose
);
1324 std::string dir
= cmSystemTools::GetFilenamePath(tgtInfo
);
1325 std::string internalDependFile
= dir
+ "/depend.internal";
1326 std::string dependFile
= dir
+ "/depend.make";
1328 // If the target DependInfo.cmake file has changed since the last
1329 // time dependencies were scanned then force rescanning. This may
1330 // happen when a new source file is added and CMake regenerates the
1331 // project but no other sources were touched.
1332 bool needRescan
= false;
1333 cmFileTimeComparison
* ftc
=
1334 this->GlobalGenerator
->GetCMakeInstance()->GetFileComparison();
1337 if(!ftc
->FileTimeCompare(internalDependFile
.c_str(), tgtInfo
, &result
) ||
1342 cmOStringStream msg
;
1343 msg
<< "Dependee \"" << tgtInfo
1344 << "\" is newer than depender \""
1345 << internalDependFile
<< "\"." << std::endl
;
1346 cmSystemTools::Stdout(msg
.str().c_str());
1352 // Check the implicit dependencies to see if they are up to date.
1353 // The build.make file may have explicit dependencies for the object
1354 // files but these will not affect the scanning process so they need
1355 // not be considered.
1357 checker
.SetVerbose(verbose
);
1358 checker
.SetFileComparison(ftc
);
1360 !checker
.Check(dependFile
.c_str(), internalDependFile
.c_str()))
1362 // The dependencies must be regenerated.
1363 std::string targetName
= cmSystemTools::GetFilenameName(dir
);
1364 targetName
= targetName
.substr(0, targetName
.length()-4);
1365 std::string message
= "Scanning dependencies of target ";
1366 message
+= targetName
;
1367 #ifdef CMAKE_BUILD_WITH_CMAKE
1368 cmSystemTools::MakefileColorEcho(
1369 cmsysTerminal_Color_ForegroundMagenta
|
1370 cmsysTerminal_Color_ForegroundBold
,
1371 message
.c_str(), true, color
);
1373 fprintf(stdout
, "%s\n", message
.c_str());
1376 return this->ScanDependencies(dir
.c_str());
1380 // The dependencies are already up-to-date.
1385 //----------------------------------------------------------------------------
1387 cmLocalUnixMakefileGenerator3
1388 ::ScanDependencies(const char* targetDir
)
1390 // Read the directory information file.
1391 cmMakefile
* mf
= this->Makefile
;
1392 bool haveDirectoryInfo
= false;
1393 std::string dirInfoFile
= this->Makefile
->GetStartOutputDirectory();
1394 dirInfoFile
+= cmake::GetCMakeFilesDirectory();
1395 dirInfoFile
+= "/CMakeDirectoryInformation.cmake";
1396 if(mf
->ReadListFile(0, dirInfoFile
.c_str()) &&
1397 !cmSystemTools::GetErrorOccuredFlag())
1399 haveDirectoryInfo
= true;
1402 // Lookup useful directory information.
1403 if(haveDirectoryInfo
)
1405 // Test whether we need to force Unix paths.
1406 if(const char* force
= mf
->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
1408 if(!cmSystemTools::IsOff(force
))
1410 cmSystemTools::SetForceUnixPaths(true);
1414 // Setup relative path top directories.
1415 this->RelativePathsConfigured
= true;
1416 if(const char* relativePathTopSource
=
1417 mf
->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE"))
1419 this->RelativePathTopSource
= relativePathTopSource
;
1421 if(const char* relativePathTopBinary
=
1422 mf
->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY"))
1424 this->RelativePathTopBinary
= relativePathTopBinary
;
1429 cmSystemTools::Error("Directory Information file not found");
1432 // create the file stream for the depends file
1433 std::string dir
= targetDir
;
1435 // Open the rule file. This should be copy-if-different because the
1436 // rules may depend on this file itself.
1437 std::string ruleFileNameFull
= dir
;
1438 ruleFileNameFull
+= "/depend.make";
1439 cmGeneratedFileStream
ruleFileStream(ruleFileNameFull
.c_str());
1440 ruleFileStream
.SetCopyIfDifferent(true);
1445 std::string internalRuleFileNameFull
= dir
;
1446 internalRuleFileNameFull
+= "/depend.internal";
1447 cmGeneratedFileStream
1448 internalRuleFileStream(internalRuleFileNameFull
.c_str());
1449 internalRuleFileStream
.SetCopyIfDifferent(true);
1450 if(!internalRuleFileStream
)
1455 this->WriteDisclaimer(ruleFileStream
);
1456 this->WriteDisclaimer(internalRuleFileStream
);
1458 // for each language we need to scan, scan it
1459 const char *langStr
= mf
->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
1460 std::vector
<std::string
> langs
;
1461 cmSystemTools::ExpandListArgument(langStr
, langs
);
1462 for (std::vector
<std::string
>::iterator li
=
1463 langs
.begin(); li
!= langs
.end(); ++li
)
1465 // construct the checker
1466 std::string lang
= li
->c_str();
1468 // Get the set of include directories.
1469 std::vector
<std::string
> includes
;
1470 if(haveDirectoryInfo
)
1472 std::string includePathVar
= "CMAKE_";
1473 includePathVar
+= lang
;
1474 includePathVar
+= "_INCLUDE_PATH";
1475 if(const char* includePath
= mf
->GetDefinition(includePathVar
.c_str()))
1477 cmSystemTools::ExpandListArgument(includePath
, includes
);
1481 // Get the include file regular expression.
1482 std::string includeRegexScan
= "^.*$";
1483 std::string includeRegexComplain
= "^$";
1484 if(haveDirectoryInfo
)
1486 std::string scanRegexVar
= "CMAKE_";
1487 scanRegexVar
+= lang
;
1488 scanRegexVar
+= "_INCLUDE_REGEX_SCAN";
1489 if(const char* scanRegex
= mf
->GetDefinition(scanRegexVar
.c_str()))
1491 includeRegexScan
= scanRegex
;
1493 std::string complainRegexVar
= "CMAKE_";
1494 complainRegexVar
+= lang
;
1495 complainRegexVar
+= "_INCLUDE_REGEX_COMPLAIN";
1496 if(const char* complainRegex
=
1497 mf
->GetDefinition(complainRegexVar
.c_str()))
1499 includeRegexComplain
= complainRegex
;
1503 // Create the scanner for this language
1504 cmDepends
*scanner
= 0;
1505 if(lang
== "C" || lang
== "CXX" || lang
== "RC")
1507 std::string includeCacheFileName
= dir
;
1508 includeCacheFileName
+= "/";
1509 includeCacheFileName
+= lang
;
1510 includeCacheFileName
+= ".includecache";
1512 // TODO: Handle RC (resource files) dependencies correctly.
1513 scanner
= new cmDependsC(includes
,
1514 includeRegexScan
.c_str(),
1515 includeRegexComplain
.c_str(),
1516 includeCacheFileName
);
1518 #ifdef CMAKE_BUILD_WITH_CMAKE
1519 else if(lang
== "Fortran")
1521 std::vector
<std::string
> defines
;
1522 if(const char* c_defines
=
1523 mf
->GetDefinition("CMAKE_TARGET_DEFINITIONS"))
1525 cmSystemTools::ExpandListArgument(c_defines
, defines
);
1528 scanner
= new cmDependsFortran(includes
, defines
);
1530 else if(lang
== "Java")
1532 scanner
= new cmDependsJava();
1538 scanner
->SetLocalGenerator(this);
1539 scanner
->SetFileComparison
1540 (this->GlobalGenerator
->GetCMakeInstance()->GetFileComparison());
1541 scanner
->SetLanguage(lang
.c_str());
1542 scanner
->SetTargetDirectory(dir
.c_str());
1543 scanner
->Write(ruleFileStream
, internalRuleFileStream
);
1545 // free the scanner for this language
1553 //----------------------------------------------------------------------------
1554 void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose
)
1556 cmMakefile
* mf
= this->Makefile
;
1558 // Get the string listing the multiple output pairs.
1559 const char* pairs_string
= mf
->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
1565 // Convert the string to a list and preserve empty entries.
1566 std::vector
<std::string
> pairs
;
1567 cmSystemTools::ExpandListArgument(pairs_string
, pairs
, true);
1568 for(std::vector
<std::string
>::const_iterator i
= pairs
.begin();
1569 i
!= pairs
.end(); ++i
)
1571 const std::string
& depender
= *i
;
1572 if(++i
!= pairs
.end())
1574 const std::string
& dependee
= *i
;
1576 // If the depender is missing then delete the dependee to make
1577 // sure both will be regenerated.
1578 if(cmSystemTools::FileExists(dependee
.c_str()) &&
1579 !cmSystemTools::FileExists(depender
.c_str()))
1583 cmOStringStream msg
;
1584 msg
<< "Deleting primary custom command output \"" << dependee
1585 << "\" because another output \""
1586 << depender
<< "\" does not exist." << std::endl
;
1587 cmSystemTools::Stdout(msg
.str().c_str());
1589 cmSystemTools::RemoveFile(dependee
.c_str());
1595 //----------------------------------------------------------------------------
1596 void cmLocalUnixMakefileGenerator3
1597 ::WriteLocalAllRules(std::ostream
& ruleFileStream
)
1599 this->WriteDisclaimer(ruleFileStream
);
1601 // Write the main entry point target. This must be the VERY first
1602 // target so that make with no arguments will run it.
1604 // Just depend on the all target to drive the build.
1605 std::vector
<std::string
> depends
;
1606 std::vector
<std::string
> no_commands
;
1607 depends
.push_back("all");
1610 this->WriteMakeRule(ruleFileStream
,
1611 "Default target executed when no arguments are "
1618 this->WriteSpecialTargetsTop(ruleFileStream
);
1620 // Include the progress variables for the target.
1621 // Write all global targets
1622 this->WriteDivider(ruleFileStream
);
1624 << "# Targets provided globally by CMake.\n"
1626 cmTargets
* targets
= &(this->Makefile
->GetTargets());
1627 cmTargets::iterator glIt
;
1628 for ( glIt
= targets
->begin(); glIt
!= targets
->end(); ++ glIt
)
1630 if ( glIt
->second
.GetType() == cmTarget::GLOBAL_TARGET
)
1632 std::string targetString
= "Special rule for the target " + glIt
->first
;
1633 std::vector
<std::string
> commands
;
1634 std::vector
<std::string
> depends
;
1636 const char* text
= glIt
->second
.GetProperty("EchoString");
1639 text
= "Running external command ...";
1641 std::set
<cmStdString
>::const_iterator dit
;
1642 for ( dit
= glIt
->second
.GetUtilities().begin();
1643 dit
!= glIt
->second
.GetUtilities().end();
1646 depends
.push_back(dit
->c_str());
1648 this->AppendEcho(commands
, text
,
1649 cmLocalUnixMakefileGenerator3::EchoGlobal
);
1651 // Global targets store their rules in pre- and post-build commands.
1652 this->AppendCustomDepends(depends
,
1653 glIt
->second
.GetPreBuildCommands());
1654 this->AppendCustomDepends(depends
,
1655 glIt
->second
.GetPostBuildCommands());
1656 this->AppendCustomCommands(commands
,
1657 glIt
->second
.GetPreBuildCommands());
1658 this->AppendCustomCommands(commands
,
1659 glIt
->second
.GetPostBuildCommands());
1660 std::string targetName
= glIt
->second
.GetName();
1661 this->WriteMakeRule(ruleFileStream
, targetString
.c_str(),
1662 targetName
.c_str(), depends
, commands
, true);
1664 // Provide a "/fast" version of the target.
1666 if((targetName
== "install")
1667 || (targetName
== "install_local")
1668 || (targetName
== "install_strip"))
1670 // Provide a fast install target that does not depend on all
1671 // but has the same command.
1672 depends
.push_back("preinstall/fast");
1676 // Just forward to the real target so at least it will work.
1677 depends
.push_back(targetName
);
1680 targetName
+= "/fast";
1681 this->WriteMakeRule(ruleFileStream
, targetString
.c_str(),
1682 targetName
.c_str(), depends
, commands
, true);
1686 std::vector
<std::string
> depends
;
1687 std::vector
<std::string
> commands
;
1689 // Write the all rule.
1691 std::string recursiveTarget
= this->Makefile
->GetStartOutputDirectory();
1692 recursiveTarget
+= "/all";
1694 depends
.push_back("cmake_check_build_system");
1696 std::string progressDir
= this->Makefile
->GetHomeOutputDirectory();
1697 progressDir
+= cmake::GetCMakeFilesDirectory();
1699 cmOStringStream progCmd
;
1701 "$(CMAKE_COMMAND) -E cmake_progress_start ";
1702 progCmd
<< this->Convert(progressDir
.c_str(),
1703 cmLocalGenerator::FULL
,
1704 cmLocalGenerator::SHELL
);
1706 std::string progressFile
= cmake::GetCMakeFilesDirectory();
1707 progressFile
+= "/progress.make";
1708 std::string progressFileNameFull
=
1709 this->ConvertToFullPath(progressFile
.c_str());
1710 progCmd
<< " " << this->Convert(progressFileNameFull
.c_str(),
1711 cmLocalGenerator::FULL
,
1712 cmLocalGenerator::SHELL
);
1713 commands
.push_back(progCmd
.str());
1715 std::string mf2Dir
= cmake::GetCMakeFilesDirectoryPostSlash();
1716 mf2Dir
+= "Makefile2";
1717 commands
.push_back(this->GetRecursiveMakeCall(mf2Dir
.c_str(),
1718 recursiveTarget
.c_str()));
1719 this->CreateCDCommand(commands
,
1720 this->Makefile
->GetHomeOutputDirectory(),
1721 this->Makefile
->GetStartOutputDirectory());
1723 cmOStringStream progCmd
;
1724 progCmd
<< "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
1725 progCmd
<< this->Convert(progressDir
.c_str(),
1726 cmLocalGenerator::FULL
,
1727 cmLocalGenerator::SHELL
);
1729 commands
.push_back(progCmd
.str());
1731 this->WriteMakeRule(ruleFileStream
, "The main all target", "all",
1732 depends
, commands
, true);
1734 // Write the clean rule.
1735 recursiveTarget
= this->Makefile
->GetStartOutputDirectory();
1736 recursiveTarget
+= "/clean";
1739 commands
.push_back(this->GetRecursiveMakeCall(mf2Dir
.c_str(),
1740 recursiveTarget
.c_str()));
1741 this->CreateCDCommand(commands
,
1742 this->Makefile
->GetHomeOutputDirectory(),
1743 this->Makefile
->GetStartOutputDirectory());
1744 this->WriteMakeRule(ruleFileStream
, "The main clean target", "clean",
1745 depends
, commands
, true);
1748 depends
.push_back("clean");
1749 this->WriteMakeRule(ruleFileStream
, "The main clean target", "clean/fast",
1750 depends
, commands
, true);
1752 // Write the preinstall rule.
1753 recursiveTarget
= this->Makefile
->GetStartOutputDirectory();
1754 recursiveTarget
+= "/preinstall";
1758 this->Makefile
->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
1759 if(!noall
|| cmSystemTools::IsOff(noall
))
1761 // Drive the build before installing.
1762 depends
.push_back("all");
1766 // At least make sure the build system is up to date.
1767 depends
.push_back("cmake_check_build_system");
1770 (this->GetRecursiveMakeCall(mf2Dir
.c_str(), recursiveTarget
.c_str()));
1771 this->CreateCDCommand(commands
,
1772 this->Makefile
->GetHomeOutputDirectory(),
1773 this->Makefile
->GetStartOutputDirectory());
1774 this->WriteMakeRule(ruleFileStream
, "Prepare targets for installation.",
1775 "preinstall", depends
, commands
, true);
1777 this->WriteMakeRule(ruleFileStream
, "Prepare targets for installation.",
1778 "preinstall/fast", depends
, commands
, true);
1780 // write the depend rule, really a recompute depends rule
1783 std::string cmakefileName
= cmake::GetCMakeFilesDirectoryPostSlash();
1784 cmakefileName
+= "Makefile.cmake";
1785 std::string runRule
=
1786 "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
1787 runRule
+= " --check-build-system ";
1788 runRule
+= this->Convert(cmakefileName
.c_str(),cmLocalGenerator::NONE
,
1789 cmLocalGenerator::SHELL
);
1791 commands
.push_back(runRule
);
1792 this->CreateCDCommand(commands
,
1793 this->Makefile
->GetHomeOutputDirectory(),
1794 this->Makefile
->GetStartOutputDirectory());
1795 this->WriteMakeRule(ruleFileStream
, "clear depends",
1797 depends
, commands
, true);
1801 //----------------------------------------------------------------------------
1802 void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile
* mf
,
1805 // Get the list of target files to check
1806 const char* infoDef
= mf
->GetDefinition("CMAKE_DEPEND_INFO_FILES");
1811 std::vector
<std::string
> files
;
1812 cmSystemTools::ExpandListArgument(infoDef
, files
);
1814 // Each depend information file corresponds to a target. Clear the
1815 // dependencies for that target.
1817 clearer
.SetVerbose(verbose
);
1818 for(std::vector
<std::string
>::iterator l
= files
.begin();
1819 l
!= files
.end(); ++l
)
1821 std::string dir
= cmSystemTools::GetFilenamePath(l
->c_str());
1823 // Clear the implicit dependency makefile.
1824 std::string dependFile
= dir
+ "/depend.make";
1825 clearer
.Clear(dependFile
.c_str());
1827 // Remove the internal dependency check file to force
1829 std::string internalDependFile
= dir
+ "/depend.internal";
1830 cmSystemTools::RemoveFile(internalDependFile
.c_str());
1835 void cmLocalUnixMakefileGenerator3
1836 ::WriteDependLanguageInfo(std::ostream
& cmakefileStream
, cmTarget
&target
)
1838 ImplicitDependLanguageMap
const& implicitLangs
=
1839 this->GetImplicitDepends(target
);
1841 // list the languages
1843 << "# The set of languages for which implicit dependencies are needed:\n";
1845 << "SET(CMAKE_DEPENDS_LANGUAGES\n";
1846 for(ImplicitDependLanguageMap::const_iterator
1847 l
= implicitLangs
.begin(); l
!= implicitLangs
.end(); ++l
)
1849 cmakefileStream
<< " \"" << l
->first
.c_str() << "\"\n";
1851 cmakefileStream
<< " )\n";
1853 // now list the files for each language
1855 << "# The set of files for implicit dependencies of each language:\n";
1856 for(ImplicitDependLanguageMap::const_iterator
1857 l
= implicitLangs
.begin(); l
!= implicitLangs
.end(); ++l
)
1860 << "SET(CMAKE_DEPENDS_CHECK_" << l
->first
.c_str() << "\n";
1861 ImplicitDependFileMap
const& implicitPairs
= l
->second
;
1863 // for each file pair
1864 for(ImplicitDependFileMap::const_iterator pi
= implicitPairs
.begin();
1865 pi
!= implicitPairs
.end(); ++pi
)
1867 cmakefileStream
<< " \"" << pi
->second
<< "\" ";
1868 cmakefileStream
<< "\"" << pi
->first
<< "\"\n";
1870 cmakefileStream
<< " )\n";
1872 // Tell the dependency scanner what compiler is used.
1873 std::string cidVar
= "CMAKE_";
1875 cidVar
+= "_COMPILER_ID";
1876 const char* cid
= this->Makefile
->GetDefinition(cidVar
.c_str());
1880 << "SET(CMAKE_" << l
->first
.c_str() << "_COMPILER_ID \""
1885 // Build a list of preprocessor definitions for the target.
1886 std::vector
<std::string
> defines
;
1888 std::string defPropName
= "COMPILE_DEFINITIONS_";
1889 defPropName
+= cmSystemTools::UpperCase(this->ConfigurationName
);
1890 if(const char* ddefs
= this->Makefile
->GetProperty("COMPILE_DEFINITIONS"))
1892 cmSystemTools::ExpandListArgument(ddefs
, defines
);
1894 if(const char* cdefs
= target
.GetProperty("COMPILE_DEFINITIONS"))
1896 cmSystemTools::ExpandListArgument(cdefs
, defines
);
1898 if(const char* dcdefs
= this->Makefile
->GetProperty(defPropName
.c_str()))
1900 cmSystemTools::ExpandListArgument(dcdefs
, defines
);
1902 if(const char* ccdefs
= target
.GetProperty(defPropName
.c_str()))
1904 cmSystemTools::ExpandListArgument(ccdefs
, defines
);
1907 if(!defines
.empty())
1911 << "# Preprocessor definitions for this target.\n"
1912 << "SET(CMAKE_TARGET_DEFINITIONS\n";
1913 for(std::vector
<std::string
>::const_iterator di
= defines
.begin();
1914 di
!= defines
.end(); ++di
)
1917 << " " << this->EscapeForCMake(di
->c_str()) << "\n";
1924 //----------------------------------------------------------------------------
1926 cmLocalUnixMakefileGenerator3
1927 ::GetObjectFileName(cmTarget
& target
,
1928 const cmSourceFile
& source
,
1929 std::string
* nameWithoutTargetDir
,
1930 bool* hasSourceExtension
)
1932 // Make sure we never hit this old case.
1933 if(source
.GetProperty("MACOSX_PACKAGE_LOCATION"))
1935 std::string msg
= "MACOSX_PACKAGE_LOCATION set on source file: ";
1936 msg
+= source
.GetFullPath();
1937 this->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR
,
1941 // Start with the target directory.
1942 std::string obj
= this->GetTargetDirectory(target
);
1945 // Get the object file name without the target directory.
1946 std::string::size_type dir_len
= 0;
1947 dir_len
+= strlen(this->Makefile
->GetCurrentOutputDirectory());
1949 dir_len
+= obj
.size();
1950 std::string objectName
=
1951 this->GetObjectFileNameWithoutTarget(source
, dir_len
,
1952 hasSourceExtension
);
1953 if(nameWithoutTargetDir
)
1955 *nameWithoutTargetDir
= objectName
;
1958 // Append the object name to the target directory.
1963 //----------------------------------------------------------------------------
1964 void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream
& os
)
1967 << "# CMAKE generated file: DO NOT EDIT!\n"
1968 << "# Generated by \"" << this->GlobalGenerator
->GetName() << "\""
1969 << " Generator, CMake Version "
1970 << cmVersion::GetMajorVersion() << "."
1971 << cmVersion::GetMinorVersion() << "\n\n";
1974 //----------------------------------------------------------------------------
1976 cmLocalUnixMakefileGenerator3
1977 ::GetRecursiveMakeCall(const char *makefile
, const char* tgt
)
1979 // Call make on the given file.
1981 cmd
+= "$(MAKE) -f ";
1982 cmd
+= this->Convert(makefile
,NONE
,SHELL
);
1985 // Passg down verbosity level.
1986 if(this->GetMakeSilentFlag().size())
1988 cmd
+= this->GetMakeSilentFlag();
1992 // Most unix makes will pass the command line flags to make down to
1993 // sub-invoked makes via an environment variable. However, some
1994 // makes do not support that, so you have to pass the flags
1996 if(this->GetPassMakeflags())
1998 cmd
+= "-$(MAKEFLAGS) ";
2002 if (tgt
&& tgt
[0] != '\0')
2004 // The make target is always relative to the top of the build tree.
2005 std::string tgt2
= this->Convert(tgt
, HOME_OUTPUT
);
2007 // The target may have been written with windows paths.
2008 cmSystemTools::ConvertToOutputSlashes(tgt2
);
2010 // Escape one extra time if the make tool requires it.
2011 if(this->MakeCommandEscapeTargetTwice
)
2013 tgt2
= this->EscapeForShell(tgt2
.c_str(), true, false);
2016 // The target name is now a string that should be passed verbatim
2017 // on the command line.
2018 cmd
+= this->EscapeForShell(tgt2
.c_str(), true, false);
2023 //----------------------------------------------------------------------------
2024 void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream
& os
)
2027 << "#======================================"
2028 << "=======================================\n";
2031 //----------------------------------------------------------------------------
2033 cmLocalUnixMakefileGenerator3
2034 ::WriteCMakeArgument(std::ostream
& os
, const char* s
)
2036 // Write the given string to the stream with escaping to get it back
2037 // into CMake through the lexical scanner.
2039 for(const char* c
= s
; *c
; ++c
)
2057 //----------------------------------------------------------------------------
2059 cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p
)
2062 // Split the path into its components.
2063 std::vector
<std::string
> components
;
2064 cmSystemTools::SplitPath(p
, components
);
2066 // Return an empty path if there are no components.
2067 if(components
.empty())
2072 // Choose a slash direction and fix root component.
2073 const char* slash
= "/";
2074 #if defined(_WIN32) && !defined(__CYGWIN__)
2075 if(!cmSystemTools::GetForceUnixPaths())
2078 for(std::string::iterator i
= components
[0].begin();
2079 i
!= components
[0].end(); ++i
)
2089 // Begin the quoted result with the root component.
2090 std::string result
= "\"";
2091 result
+= components
[0];
2093 // Now add the rest of the components separated by the proper slash
2094 // direction for this platform.
2096 for(unsigned int i
=1; i
< components
.size(); ++i
)
2098 // Only the last component can be empty to avoid double slashes.
2099 if(components
[i
].length() > 0 || (i
== (components
.size()-1)))
2105 result
+= components
[i
];
2110 // Close the quoted result.
2116 //----------------------------------------------------------------------------
2118 cmLocalUnixMakefileGenerator3
2119 ::GetTargetDirectory(cmTarget
const& target
) const
2121 std::string dir
= cmake::GetCMakeFilesDirectoryPostSlash();
2122 dir
+= target
.GetName();
2127 //----------------------------------------------------------------------------
2128 cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap
const&
2129 cmLocalUnixMakefileGenerator3::GetImplicitDepends(cmTarget
const& tgt
)
2131 return this->ImplicitDepends
[tgt
.GetName()];
2134 //----------------------------------------------------------------------------
2136 cmLocalUnixMakefileGenerator3::AddImplicitDepends(cmTarget
const& tgt
,
2141 this->ImplicitDepends
[tgt
.GetName()][lang
][obj
] = src
;
2144 //----------------------------------------------------------------------------
2145 void cmLocalUnixMakefileGenerator3
2146 ::CreateCDCommand(std::vector
<std::string
>& commands
, const char *tgtDir
,
2149 // do we need to cd?
2150 if (!strcmp(tgtDir
,retDir
))
2157 // On Windows we must perform each step separately and then change
2158 // back because the shell keeps the working directory between
2160 std::string cmd
= "cd ";
2161 cmd
+= this->ConvertToOutputForExisting(tgtDir
);
2162 commands
.insert(commands
.begin(),cmd
);
2164 // Change back to the starting directory. Any trailing slash must be
2165 // removed to avoid problems with Borland Make.
2166 std::string back
= retDir
;
2167 if(back
.size() && back
[back
.size()-1] == '/')
2169 back
= back
.substr(0, back
.size()-1);
2172 cmd
+= this->ConvertToOutputForExisting(back
.c_str());
2173 commands
.push_back(cmd
);
2177 // On UNIX we must construct a single shell command to change
2178 // directory and build because make resets the directory between
2180 std::vector
<std::string
>::iterator i
= commands
.begin();
2181 for (; i
!= commands
.end(); ++i
)
2183 std::string cmd
= "cd ";
2184 cmd
+= this->ConvertToOutputForExisting(tgtDir
);
2193 void cmLocalUnixMakefileGenerator3
2194 ::GetTargetObjectFileDirectories(cmTarget
* target
,
2195 std::vector
<std::string
>& dirs
)
2197 std::string dir
= this->Makefile
->GetCurrentOutputDirectory();
2199 dir
+= this->GetTargetDirectory(*target
);
2200 dirs
.push_back(dir
);