1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalUnixMakefileGenerator3.cxx,v $
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"
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>
37 #include <memory> // auto_ptr
40 //----------------------------------------------------------------------------
41 // Helper function used below.
42 static std::string
cmSplitExtension(std::string
const& in
, std::string
& base
)
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
);
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;
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
;
114 // No configuration type given.
115 this->ConfigurationName
= "";
118 // Record whether some options are enabled to avoid checking many
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();
129 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
131 cmMakefileTargetGenerator
*tg
=
132 cmMakefileTargetGenerator::New(this, t
->first
, &(t
->second
));
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();
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();
180 //----------------------------------------------------------------------------
181 // writes the progreess variables and also closes out the targets
182 void cmLocalUnixMakefileGenerator3
183 ::WriteProgressVariables(unsigned long total
,
184 unsigned long ¤t
)
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
);
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());
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());
230 // always write the top makefile
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
241 std::set
<cmStdString
> emittedTargets
;
244 // write our targets, and while doing it collect up the object
246 this->WriteLocalMakefileTargets(ruleFileStream
,emittedTargets
);
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 //----------------------------------------------------------------------------
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.
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
333 std::string outNoExt
;
334 cmSplitExtension(outBase1
, outNoExt
);
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);
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";
356 targetName
+= output
;
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
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";
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())
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";
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
440 if(t
->second
.NeedRelinkBeforeInstall())
442 makeTargetName
= this->GetRelativeTargetDirectory(t
->second
);
443 makeTargetName
+= "/preinstall";
444 localName
= t
->second
.GetName();
445 localName
+= "/preinstall";
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());
475 // Write the do not edit header.
476 this->WriteDisclaimer(infoFileStream
);
478 // Setup relative path conversion tops.
480 << "# Relative path conversion top directories.\n"
481 << "SET(CMAKE_RELATIVE_PATH_TOP_SOURCE \"" << this->RelativePathTopSource
483 << "SET(CMAKE_RELATIVE_PATH_TOP_BINARY \"" << this->RelativePathTopBinary
487 // Tell the dependency scanner to use unix paths if necessary.
488 if(cmSystemTools::GetForceUnixPaths())
491 << "# Force unix paths in dependencies.\n"
492 << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
496 // Store the include search path for this directory.
498 << "# The C and CXX include file search paths:\n";
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
)
507 << " \"" << this->Convert(i
->c_str(),HOME_OUTPUT
).c_str() << "\"\n";
512 << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
514 << "SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
516 // Store the include regular expressions for this directory.
519 << "# The C and CXX include file regular expressions for "
520 << "this directory.\n";
522 << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
523 this->WriteCMakeArgument(infoFileStream
,
524 this->Makefile
->GetIncludeRegularExpression());
528 << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
529 this->WriteCMakeArgument(infoFileStream
,
530 this->Makefile
->GetComplainRegularExpression());
534 << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
536 << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
537 "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
540 //----------------------------------------------------------------------------
542 cmLocalUnixMakefileGenerator3
543 ::ConvertToFullPath(const std::string
& localPath
)
545 std::string dir
= this->Makefile
->GetStartOutputDirectory();
552 const std::string
&cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
554 return this->HomeRelativeOutputPath
;
558 //----------------------------------------------------------------------------
560 cmLocalUnixMakefileGenerator3
561 ::WriteMakeRule(std::ostream
& os
,
564 const std::vector
<std::string
>& depends
,
565 const std::vector
<std::string
>& commands
,
569 // Make sure there is a target.
570 if(!target
|| !*target
)
572 cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
579 // Write the comment describing the rule in the makefile.
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";
590 os
<< "# " << replace
.substr(lpos
) << "\n";
593 // Construct the left hand side of the rule.
595 std::string tgt
= this->Convert(replace
.c_str(),HOME_OUTPUT
,MAKEFILE
);
596 const char* space
= "";
599 // Add a space before the ":" to avoid drive letter confusion on
604 // Mark the rule as symbolic if requested.
608 this->Makefile
->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
610 os
<< tgt
.c_str() << space
<< ": " << sym
<< "\n";
617 // No dependencies. The commands will always run.
618 os
<< tgt
.c_str() << space
<< ":\n";
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
)
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
)
638 os
<< "\t" << replace
.c_str() << "\n";
642 // Add the output to the local help if requested.
645 this->LocalHelp
.push_back(target
);
649 //----------------------------------------------------------------------------
651 cmLocalUnixMakefileGenerator3
652 ::WriteMakeVariables(std::ostream
& makefileStream
)
654 this->WriteDivider(makefileStream
);
656 << "# Set environment variables for the build.\n"
658 if(this->DefineWindowsNULL
)
661 << "!IF \"$(OS)\" == \"Windows_NT\"\n"
667 if(this->WindowsShell
)
670 << "SHELL = cmd.exe\n"
676 << "# The shell in which to execute make rules.\n"
677 << "SHELL = /bin/sh\n"
681 std::string cmakecommand
=
682 this->Makefile
->GetRequiredDefinition("CMAKE_COMMAND");
684 << "# The CMake executable.\n"
685 << "CMAKE_COMMAND = "
686 << this->Convert(cmakecommand
.c_str(), FULL
, SHELL
).c_str()
690 << "# The command to remove a file.\n"
692 << this->Convert(cmakecommand
.c_str(),FULL
,SHELL
).c_str()
696 if(this->Makefile
->GetDefinition("CMAKE_EDIT_COMMAND"))
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"
707 << "# The top-level source directory on which CMake was run.\n"
708 << "CMAKE_SOURCE_DIR = "
709 << this->Convert(this->Makefile
->GetHomeDirectory(), FULL
, SHELL
)
713 << "# The top-level build directory on which CMake was run.\n"
714 << "CMAKE_BINARY_DIR = "
715 << this->Convert(this->Makefile
->GetHomeOutputDirectory(), FULL
, SHELL
)
720 //----------------------------------------------------------------------------
722 cmLocalUnixMakefileGenerator3
723 ::WriteSpecialTargetsTop(std::ostream
& makefileStream
)
725 this->WriteDivider(makefileStream
);
727 << "# Special targets provided by cmake.\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.
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
754 if((this->Makefile
->IsOn("CMAKE_VERBOSE_MAKEFILE"))
755 || (gg
->GetForceVerboseMakefiles()))
758 << "# Produce verbose output by default.\n"
762 if(this->SilentNoColon
)
764 makefileStream
<< "$(VERBOSE).SILENT\n";
768 this->WriteMakeRule(makefileStream
,
769 "Suppress display of executed commands.",
775 // Work-around for makes that drop rules that have no dependencies
777 std::string hack
= gg
->GetEmptyRuleHackDepends();
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
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
);
805 << "# Special targets to cleanup operation of make.\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
);
820 std::vector
<std::string
> no_depends
;
821 std::vector
<std::string
> commands
;
822 commands
.push_back(runRule
);
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 "
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",
843 //----------------------------------------------------------------------------
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
;
861 this->WriteMakeRule(ruleFileStream
, "Convenience name for target.",
862 helpTarget
, depends
, no_commands
, true);
867 //----------------------------------------------------------------------------
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());
893 this->cmLocalGenerator::AppendFlags(flags
, newFlags
);
896 //----------------------------------------------------------------------------
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.
905 this->Makefile
->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
906 if(!nodep
|| cmSystemTools::IsOff(nodep
))
908 depends
.push_back(ruleFileName
);
912 //----------------------------------------------------------------------------
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();
921 this->AppendCustomDepend(depends
, *i
);
925 //----------------------------------------------------------------------------
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 //----------------------------------------------------------------------------
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();
950 this->AppendCustomCommand(commands
, *i
, true);
954 //----------------------------------------------------------------------------
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.
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();
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());
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
;
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.
1011 cmd
= this->Convert(cmd
.c_str(),NONE
,SHELL
);
1012 for(unsigned int j
=1; j
< commandLine
.size(); ++j
)
1017 cmd
+= this->EscapeForShellOldStyle(commandLine
[j
].c_str());
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
);
1041 hack_cmd
+= cmd
.substr(lcurly
+1);
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 //----------------------------------------------------------------------------
1060 cmLocalUnixMakefileGenerator3
1061 ::AppendCleanCommand(std::vector
<std::string
>& commands
,
1062 const std::vector
<std::string
>& files
,
1063 cmTarget
& target
, const char* filename
)
1067 std::string cleanfile
= this->Makefile
->GetCurrentOutputDirectory();
1069 cleanfile
+= this->GetTargetDirectory(target
);
1070 cleanfile
+= "/cmake_clean";
1074 cleanfile
+= filename
;
1076 cleanfile
+= ".cmake";
1077 std::string cleanfilePath
= this->Convert(cleanfile
.c_str(), FULL
);
1078 std::ofstream
fout(cleanfilePath
.c_str());
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
)
1093 commands
.push_back(remove
);
1095 // For the main clean rule add per-language cleaning.
1098 // Get the set of source languages in the target.
1099 std::set
<cmStdString
> languages
;
1100 target
.GetLanguages(languages
);
1102 << "# Per-language clean rules from dependency scanning.\n"
1104 for(std::set
<cmStdString
>::const_iterator l
= languages
.begin();
1105 l
!= languages
.end(); ++l
)
1110 << " INCLUDE(" << this->GetTargetDirectory(target
)
1111 << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
1112 << "ENDFOREACH(lang)\n";
1117 //----------------------------------------------------------------------------
1119 cmLocalUnixMakefileGenerator3::AppendEcho(std::vector
<std::string
>& commands
,
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.
1135 color_name
= "--magenta --bold ";
1138 color_name
= "--green ";
1141 color_name
= "--red --bold ";
1144 color_name
= "--blue --bold ";
1147 color_name
= "--cyan ";
1155 // Echo one line at a time.
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.
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
);
1176 // Use cmake to echo the text in color.
1177 cmd
= "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
1179 cmd
+= this->EscapeForShell(line
.c_str());
1181 commands
.push_back(cmd
);
1184 // Reset the line to emtpy.
1187 // Terminate on end-of-string.
1195 // Append this character to the current line.
1201 //----------------------------------------------------------------------------
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
;
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
))
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())
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
, "-", "__");
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)
1242 sprintf(buffer
, "%04d", ni
);
1243 ret
= unmodified
+ buffer
;
1245 this->ShortMakeVariableMap
[ret
] = "1";
1246 this->MakeVariableMap
[unmodified
] = ret
;
1250 // if the string is greater the 32 chars it is an invalid vairable name
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());
1270 sprintf(buffer
, "%04d", ni
);
1271 ret
= str1
+ str2
+ buffer
;
1272 while(this->ShortMakeVariableMap
.count(ret
) && ni
< 1000)
1275 sprintf(buffer
, "%04d", ni
);
1276 ret
= str1
+ str2
+ buffer
;
1280 cmSystemTools::Error("Borland makefile variable length too long");
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
;
1291 //----------------------------------------------------------------------------
1292 bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo
,
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.
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
);
1331 fprintf(stdout
, "%s\n", message
.c_str());
1334 return this->ScanDependencies(dir
.c_str());
1338 // The dependencies are already up-to-date.
1343 //----------------------------------------------------------------------------
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
;
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);
1403 std::string internalRuleFileNameFull
= dir
;
1404 internalRuleFileNameFull
+= "/depend.internal";
1405 cmGeneratedFileStream
1406 internalRuleFileStream(internalRuleFileNameFull
.c_str());
1407 internalRuleFileStream
.SetCopyIfDifferent(true);
1408 if(!internalRuleFileStream
)
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();
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
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");
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()))
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");
1568 this->WriteMakeRule(ruleFileStream
,
1569 "Default target executed when no arguments are "
1576 this->WriteSpecialTargetsTop(ruleFileStream
);
1578 // Include the progress variables for the target.
1579 // Write all global targets
1580 this->WriteDivider(ruleFileStream
);
1582 << "# Targets provided globally by CMake.\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");
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();
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.
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");
1634 // Just forward to the real target so at least it will work.
1635 depends
.push_back(targetName
);
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.
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
;
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
);
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";
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);
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";
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");
1724 // At least make sure the build system is up to date.
1725 depends
.push_back("cmake_check_build_system");
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);
1735 this->WriteMakeRule(ruleFileStream
, "Prepare targets for installation.",
1736 "preinstall/fast", depends
, commands
, true);
1738 // write the depend rule, really a recompute depends rule
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
);
1749 commands
.push_back(runRule
);
1750 this->CreateCDCommand(commands
,
1751 this->Makefile
->GetHomeOutputDirectory(),
1752 this->Makefile
->GetStartOutputDirectory());
1753 this->WriteMakeRule(ruleFileStream
, "clear depends",
1755 depends
, commands
, true);
1759 //----------------------------------------------------------------------------
1760 void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile
* mf
,
1763 // Get the list of target files to check
1764 const char* infoDef
= mf
->GetDefinition("CMAKE_DEPEND_INFO_FILES");
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.
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
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
1801 << "# The set of languages for which implicit dependencies are needed:\n";
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
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
)
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_";
1833 cidVar
+= "_COMPILER_ID";
1834 const char* cid
= this->Makefile
->GetDefinition(cidVar
.c_str());
1838 << "SET(CMAKE_" << l
->first
.c_str() << "_COMPILER_ID \""
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())
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
)
1875 << " " << this->EscapeForCMake(di
->c_str()) << "\n";
1882 //----------------------------------------------------------------------------
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());
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();
1918 if ( target
.GetPropertyAsBool("MACOSX_BUNDLE") )
1920 obj
+= targetName
+ ".app/Contents/";
1924 // Emit warning here...? MACOSX_PACKAGE_LOCATION is "most useful" in a
1927 obj
+= fileTargetDirectory
;
1929 // Object names are specified relative to the current build dir.
1930 obj
= this->Convert(obj
.c_str(), START_OUTPUT
);
1937 // Start with the target directory.
1938 std::string obj
= this->GetTargetDirectory(target
);
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());
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.
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 //----------------------------------------------------------------------------
1973 cmLocalUnixMakefileGenerator3
1974 ::GetRecursiveMakeCall(const char *makefile
, const char* tgt
)
1976 // Call make on the given file.
1978 cmd
+= "$(MAKE) -f ";
1979 cmd
+= this->Convert(makefile
,NONE
,SHELL
);
1982 // Passg down verbosity level.
1983 if(this->GetMakeSilentFlag().size())
1985 cmd
+= this->GetMakeSilentFlag();
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
1993 if(this->GetPassMakeflags())
1995 cmd
+= "-$(MAKEFLAGS) ";
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);
2020 //----------------------------------------------------------------------------
2021 void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream
& os
)
2024 << "#======================================"
2025 << "=======================================\n";
2028 //----------------------------------------------------------------------------
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.
2036 for(const char* c
= s
; *c
; ++c
)
2054 //----------------------------------------------------------------------------
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())
2069 // Choose a slash direction and fix root component.
2070 const char* slash
= "/";
2071 #if defined(_WIN32) && !defined(__CYGWIN__)
2072 if(!cmSystemTools::GetForceUnixPaths())
2075 for(std::string::iterator i
= components
[0].begin();
2076 i
!= components
[0].end(); ++i
)
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.
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)))
2102 result
+= components
[i
];
2107 // Close the quoted result.
2113 //----------------------------------------------------------------------------
2115 cmLocalUnixMakefileGenerator3
2116 ::GetTargetDirectory(cmTarget
const& target
) const
2118 std::string dir
= cmake::GetCMakeFilesDirectoryPostSlash();
2119 dir
+= target
.GetName();
2124 //----------------------------------------------------------------------------
2125 cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap
const&
2126 cmLocalUnixMakefileGenerator3::GetImplicitDepends(cmTarget
const& tgt
)
2128 return this->ImplicitDepends
[tgt
.GetName()];
2131 //----------------------------------------------------------------------------
2133 cmLocalUnixMakefileGenerator3::AddImplicitDepends(cmTarget
const& tgt
,
2138 this->ImplicitDepends
[tgt
.GetName()][lang
][obj
] = src
;
2141 //----------------------------------------------------------------------------
2142 void cmLocalUnixMakefileGenerator3
2143 ::CreateCDCommand(std::vector
<std::string
>& commands
, const char *tgtDir
,
2146 // do we need to cd?
2147 if (!strcmp(tgtDir
,retDir
))
2154 // On Windows we must perform each step separately and then change
2155 // back because the shell keeps the working directory between
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);
2169 cmd
+= this->ConvertToOutputForExisting(back
.c_str());
2170 commands
.push_back(cmd
);
2174 // On UNIX we must construct a single shell command to change
2175 // directory and build because make resets the directory between
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
);
2190 void cmLocalUnixMakefileGenerator3
2191 ::GetTargetObjectFileDirectories(cmTarget
* target
,
2192 std::vector
<std::string
>& dirs
)
2194 std::string dir
= this->Makefile
->GetCurrentOutputDirectory();
2196 dir
+= this->GetTargetDirectory(*target
);
2197 dirs
.push_back(dir
);