1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMakefile.cxx,v $
6 Date: $Date: 2008-10-09 17:52:25 $
7 Version: $Revision: 1.486 $
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 "cmMakefile.h"
18 #include "cmVersion.h"
19 #include "cmCommand.h"
20 #include "cmSourceFile.h"
21 #include "cmSourceFileLocation.h"
22 #include "cmSystemTools.h"
23 #include "cmGlobalGenerator.h"
24 #include "cmLocalGenerator.h"
25 #include "cmCommands.h"
26 #include "cmCacheManager.h"
27 #include "cmFunctionBlocker.h"
28 #include "cmListFileCache.h"
29 #include "cmCommandArgumentParserHelper.h"
31 #ifdef CMAKE_BUILD_WITH_CMAKE
32 # include "cmVariableWatch.h"
34 #include "cmInstallGenerator.h"
36 #include <stdlib.h> // required for atoi
38 #include <cmsys/RegularExpression.hxx>
40 #include <cmsys/auto_ptr.hxx>
42 #include <ctype.h> // for isspace
44 // default is not to be building executables
45 cmMakefile::cmMakefile()
47 this->DefinitionStack
.push_back(DefinitionMap());
49 // Setup the default include file regular expression (match everything).
50 this->IncludeFileRegularExpression
= "^.*$";
51 // Setup the default include complaint regular expression (match nothing).
52 this->ComplainFileRegularExpression
= "^$";
53 // Source and header file extensions that we can handle
55 // Set up a list of source and header extensions
56 // these are used to find files when the extension
58 // The "c" extension MUST precede the "C" extension.
59 this->SourceFileExtensions
.push_back( "c" );
60 this->SourceFileExtensions
.push_back( "C" );
62 this->SourceFileExtensions
.push_back( "c++" );
63 this->SourceFileExtensions
.push_back( "cc" );
64 this->SourceFileExtensions
.push_back( "cpp" );
65 this->SourceFileExtensions
.push_back( "cxx" );
66 this->SourceFileExtensions
.push_back( "m" );
67 this->SourceFileExtensions
.push_back( "M" );
68 this->SourceFileExtensions
.push_back( "mm" );
70 this->HeaderFileExtensions
.push_back( "h" );
71 this->HeaderFileExtensions
.push_back( "hh" );
72 this->HeaderFileExtensions
.push_back( "h++" );
73 this->HeaderFileExtensions
.push_back( "hm" );
74 this->HeaderFileExtensions
.push_back( "hpp" );
75 this->HeaderFileExtensions
.push_back( "hxx" );
76 this->HeaderFileExtensions
.push_back( "in" );
77 this->HeaderFileExtensions
.push_back( "txx" );
79 this->DefineFlags
= " ";
80 this->LocalGenerator
= 0;
82 #if defined(CMAKE_BUILD_WITH_CMAKE)
83 this->AddSourceGroup("", "^.*$");
86 "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|f|f90|for|fpp"
87 "|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$");
88 this->AddSourceGroup("Header Files",
89 "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
90 this->AddSourceGroup("CMake Rules", "\\.rule$");
91 this->AddSourceGroup("Resources", "\\.plist$");
93 this->AddDefaultDefinitions();
95 this->PreOrder
= false;
98 cmMakefile::cmMakefile(const cmMakefile
& mf
)
100 this->Prefix
= mf
.Prefix
;
101 this->AuxSourceDirectories
= mf
.AuxSourceDirectories
;
102 this->cmStartDirectory
= mf
.cmStartDirectory
;
103 this->StartOutputDirectory
= mf
.StartOutputDirectory
;
104 this->cmHomeDirectory
= mf
.cmHomeDirectory
;
105 this->HomeOutputDirectory
= mf
.HomeOutputDirectory
;
106 this->cmCurrentListFile
= mf
.cmCurrentListFile
;
107 this->ProjectName
= mf
.ProjectName
;
108 this->Targets
= mf
.Targets
;
109 this->SourceFiles
= mf
.SourceFiles
;
110 this->Tests
= mf
.Tests
;
111 this->IncludeDirectories
= mf
.IncludeDirectories
;
112 this->LinkDirectories
= mf
.LinkDirectories
;
113 this->SystemIncludeDirectories
= mf
.SystemIncludeDirectories
;
114 this->ListFiles
= mf
.ListFiles
;
115 this->OutputFiles
= mf
.OutputFiles
;
116 this->LinkLibraries
= mf
.LinkLibraries
;
117 this->InstallGenerators
= mf
.InstallGenerators
;
118 this->IncludeFileRegularExpression
= mf
.IncludeFileRegularExpression
;
119 this->ComplainFileRegularExpression
= mf
.ComplainFileRegularExpression
;
120 this->SourceFileExtensions
= mf
.SourceFileExtensions
;
121 this->HeaderFileExtensions
= mf
.HeaderFileExtensions
;
122 this->DefineFlags
= mf
.DefineFlags
;
123 this->DefineFlagsOrig
= mf
.DefineFlagsOrig
;
125 #if defined(CMAKE_BUILD_WITH_CMAKE)
126 this->SourceGroups
= mf
.SourceGroups
;
129 this->DefinitionStack
.push_back(mf
.DefinitionStack
.back());
130 this->LocalGenerator
= mf
.LocalGenerator
;
131 this->FunctionBlockers
= mf
.FunctionBlockers
;
132 this->DataMap
= mf
.DataMap
;
133 this->MacrosMap
= mf
.MacrosMap
;
134 this->SubDirectoryOrder
= mf
.SubDirectoryOrder
;
135 this->TemporaryDefinitionKey
= mf
.TemporaryDefinitionKey
;
136 this->Properties
= mf
.Properties
;
137 this->PreOrder
= mf
.PreOrder
;
138 this->ListFileStack
= mf
.ListFileStack
;
142 //----------------------------------------------------------------------------
143 void cmMakefile::Initialize()
145 this->cmDefineRegex
.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
146 this->cmDefine01Regex
.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
147 this->cmAtVarRegex
.compile("(@[A-Za-z_0-9/.+-]+@)");
149 // Enter a policy level for this directory.
152 // By default the check is not done. It is enabled by
153 // cmListFileCache in the top level if necessary.
154 this->CheckCMP0000
= false;
157 unsigned int cmMakefile::GetCacheMajorVersion()
159 return this->GetCacheManager()->GetCacheMajorVersion();
162 unsigned int cmMakefile::GetCacheMinorVersion()
164 return this->GetCacheManager()->GetCacheMinorVersion();
167 bool cmMakefile::NeedCacheCompatibility(int major
, int minor
)
169 return this->GetCacheManager()->NeedCacheCompatibility(major
, minor
);
172 cmMakefile::~cmMakefile()
174 for(std::vector
<cmInstallGenerator
*>::iterator
175 i
= this->InstallGenerators
.begin();
176 i
!= this->InstallGenerators
.end(); ++i
)
180 for(std::vector
<cmSourceFile
*>::iterator i
= this->SourceFiles
.begin();
181 i
!= this->SourceFiles
.end(); ++i
)
185 for(std::vector
<cmTest
*>::iterator i
= this->Tests
.begin();
186 i
!= this->Tests
.end(); ++i
)
190 for(std::vector
<cmTarget
*>::iterator
191 i
= this->ImportedTargetsOwned
.begin();
192 i
!= this->ImportedTargetsOwned
.end(); ++i
)
196 for(unsigned int i
=0; i
< this->UsedCommands
.size(); i
++)
198 delete this->UsedCommands
[i
];
200 for(DataMapType::const_iterator d
= this->DataMap
.begin();
201 d
!= this->DataMap
.end(); ++d
)
208 std::list
<cmFunctionBlocker
*>::iterator pos
;
209 for (pos
= this->FunctionBlockers
.begin();
210 pos
!= this->FunctionBlockers
.end(); ++pos
)
212 cmFunctionBlocker
* b
= *pos
;
215 this->FunctionBlockers
.clear();
216 if (this->PolicyStack
.size() != 1)
218 cmSystemTools::Error("Internal CMake Error, Policy Stack has not been"
223 void cmMakefile::PrintStringVector(const char* s
,
224 const std::vector
<std::string
>& v
) const
226 std::cout
<< s
<< ": ( \n";
227 for(std::vector
<std::string
>::const_iterator i
= v
.begin();
230 std::cout
<< (*i
).c_str() << " ";
236 ::PrintStringVector(const char* s
,
237 const std::vector
<std::pair
<cmStdString
, bool> >& v
) const
239 std::cout
<< s
<< ": ( \n";
240 for(std::vector
<std::pair
<cmStdString
, bool> >::const_iterator i
241 = v
.begin(); i
!= v
.end(); ++i
)
243 std::cout
<< i
->first
.c_str() << " " << i
->second
;
249 // call print on all the classes in the makefile
250 void cmMakefile::Print()
252 // print the class lists
253 std::cout
<< "classes:\n";
255 std::cout
<< " this->Targets: ";
256 for (cmTargets::iterator l
= this->Targets
.begin();
257 l
!= this->Targets
.end(); l
++)
259 std::cout
<< l
->first
<< std::endl
;
262 std::cout
<< " this->StartOutputDirectory; " <<
263 this->StartOutputDirectory
.c_str() << std::endl
;
264 std::cout
<< " this->HomeOutputDirectory; " <<
265 this->HomeOutputDirectory
.c_str() << std::endl
;
266 std::cout
<< " this->cmStartDirectory; " <<
267 this->cmStartDirectory
.c_str() << std::endl
;
268 std::cout
<< " this->cmHomeDirectory; " <<
269 this->cmHomeDirectory
.c_str() << std::endl
;
270 std::cout
<< " this->ProjectName; "
271 << this->ProjectName
.c_str() << std::endl
;
272 this->PrintStringVector("this->IncludeDirectories;",
273 this->IncludeDirectories
);
274 this->PrintStringVector("this->LinkDirectories", this->LinkDirectories
);
275 #if defined(CMAKE_BUILD_WITH_CMAKE)
276 for( std::vector
<cmSourceGroup
>::const_iterator i
=
277 this->SourceGroups
.begin(); i
!= this->SourceGroups
.end(); ++i
)
279 std::cout
<< "Source Group: " << i
->GetName() << std::endl
;
284 bool cmMakefile::CommandExists(const char* name
) const
286 return this->GetCMakeInstance()->CommandExists(name
);
290 //----------------------------------------------------------------------------
291 void cmMakefile::IssueMessage(cmake::MessageType t
,
292 std::string
const& text
) const
294 // Collect context information.
295 cmListFileBacktrace backtrace
;
296 if(!this->CallStack
.empty())
298 if((t
== cmake::FATAL_ERROR
) || (t
== cmake::INTERNAL_ERROR
))
300 this->CallStack
.back().Status
->SetNestedError(true);
302 this->GetBacktrace(backtrace
);
306 cmListFileContext lfc
;
307 if(this->ListFileStack
.empty())
309 // We are not processing the project. Add the directory-level context.
310 lfc
.FilePath
= this->GetCurrentDirectory();
311 lfc
.FilePath
+= "/CMakeLists.txt";
315 // We are processing the project but are not currently executing a
316 // command. Add whatever context information we have.
317 lfc
.FilePath
= this->ListFileStack
.back();
320 if(!this->GetCMakeInstance()->GetIsInTryCompile())
322 lfc
.FilePath
= this->LocalGenerator
->Convert(lfc
.FilePath
.c_str(),
323 cmLocalGenerator::HOME
);
325 backtrace
.push_back(lfc
);
328 // Issue the message.
329 this->GetCMakeInstance()->IssueMessage(t
, text
, backtrace
);
332 //----------------------------------------------------------------------------
333 bool cmMakefile::GetBacktrace(cmListFileBacktrace
& backtrace
) const
335 if(this->CallStack
.empty())
339 for(CallStackType::const_reverse_iterator i
= this->CallStack
.rbegin();
340 i
!= this->CallStack
.rend(); ++i
)
342 cmListFileContext lfc
= *(*i
).Context
;
343 lfc
.FilePath
= this->LocalGenerator
->Convert(lfc
.FilePath
.c_str(),
344 cmLocalGenerator::HOME
);
345 backtrace
.push_back(lfc
);
350 //----------------------------------------------------------------------------
351 // Helper class to make sure the call stack is valid.
355 cmMakefileCall(cmMakefile
* mf
,
356 cmListFileContext
const& lfc
,
357 cmExecutionStatus
& status
): Makefile(mf
)
359 cmMakefile::CallStackEntry entry
= {&lfc
, &status
};
360 this->Makefile
->CallStack
.push_back(entry
);
364 this->Makefile
->CallStack
.pop_back();
367 cmMakefile
* Makefile
;
370 //----------------------------------------------------------------------------
371 bool cmMakefile::ExecuteCommand(const cmListFileFunction
& lff
,
372 cmExecutionStatus
&status
)
376 // quick return if blocked
377 if(this->IsFunctionBlocked(lff
,status
))
383 std::string name
= lff
.Name
;
385 // Place this call on the call stack.
386 cmMakefileCall
stack_manager(this, lff
, status
);
387 static_cast<void>(stack_manager
);
389 // Lookup the command prototype.
390 if(cmCommand
* proto
= this->GetCMakeInstance()->GetCommand(name
.c_str()))
392 // Clone the prototype.
393 cmsys::auto_ptr
<cmCommand
> pcmd(proto
->Clone());
394 pcmd
->SetMakefile(this);
396 // Decide whether to invoke the command.
397 if(pcmd
->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
398 (!this->GetCMakeInstance()->GetScriptMode() || pcmd
->IsScriptable()))
400 // if trace is one, print out invoke information
401 if(this->GetCMakeInstance()->GetTrace())
404 msg
<< lff
.FilePath
<< "(" << lff
.Line
<< "): ";
405 msg
<< lff
.Name
<< "(";
406 for(std::vector
<cmListFileArgument
>::const_iterator i
=
407 lff
.Arguments
.begin(); i
!= lff
.Arguments
.end(); ++i
)
413 cmSystemTools::Message(msg
.str().c_str());
415 // Try invoking the command.
416 if(!pcmd
->InvokeInitialPass(lff
.Arguments
,status
) ||
417 status
.GetNestedError())
419 if(!status
.GetNestedError())
421 // The command invocation requested that we report an error.
422 this->IssueMessage(cmake::FATAL_ERROR
, pcmd
->GetError());
425 if ( this->GetCMakeInstance()->GetScriptMode() )
427 cmSystemTools::SetFatalErrorOccured();
433 this->UsedCommands
.push_back(pcmd
.release());
436 else if ( this->GetCMakeInstance()->GetScriptMode()
437 && !pcmd
->IsScriptable() )
439 std::string error
= "Command ";
440 error
+= pcmd
->GetName();
441 error
+= "() is not scriptable";
442 this->IssueMessage(cmake::FATAL_ERROR
, error
);
444 cmSystemTools::SetFatalErrorOccured();
449 if(!cmSystemTools::GetFatalErrorOccured())
451 std::string error
= "Unknown CMake command \"";
454 this->IssueMessage(cmake::FATAL_ERROR
, error
);
456 cmSystemTools::SetFatalErrorOccured();
463 // Parse the given CMakeLists.txt file executing all commands
465 bool cmMakefile::ReadListFile(const char* filename_in
,
466 const char *external_in
,
467 std::string
* fullPath
)
469 std::string currentParentFile
470 = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
471 std::string currentFile
472 = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
473 this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in
);
475 // used to watch for blockers going out of scope
476 // e.g. mismatched IF statement
477 std::set
<cmFunctionBlocker
*> originalBlockers
;
479 const char* external
= 0;
480 std::string external_abs
;
482 const char* filename
= filename_in
;
483 std::string filename_abs
;
488 cmSystemTools::CollapseFullPath(external_in
,
489 this->cmStartDirectory
.c_str());
490 external
= external_abs
.c_str();
494 cmSystemTools::CollapseFullPath(filename_in
,
495 this->cmStartDirectory
.c_str());
496 filename
= filename_abs
.c_str();
500 // keep track of the current file being read
503 if(this->cmCurrentListFile
!= filename
)
505 this->cmCurrentListFile
= filename
;
509 // loop over current function blockers and record them
510 for (std::list
<cmFunctionBlocker
*>::iterator pos
511 = this->FunctionBlockers
.begin();
512 pos
!= this->FunctionBlockers
.end(); ++pos
)
514 originalBlockers
.insert(*pos
);
517 // Now read the input file
518 const char *filenametoread
= filename
;
522 filenametoread
= external
;
525 this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread
);
527 // try to see if the list file is the top most
528 // list file for a project, and if it is, then it
529 // must have a project command. If there is not
530 // one, then cmake will provide one via the
531 // cmListFileCache class.
532 bool requireProjectCommand
= false;
533 if(!external
&& this->cmStartDirectory
== this->cmHomeDirectory
)
535 if(cmSystemTools::LowerCase(
536 cmSystemTools::GetFilenameName(filename
)) == "cmakelists.txt")
538 requireProjectCommand
= true;
542 // push the listfile onto the stack
543 this->ListFileStack
.push_back(filenametoread
);
546 *fullPath
=filenametoread
;
548 cmListFile cacheFile
;
549 if( !cacheFile
.ParseFile(filenametoread
, requireProjectCommand
, this) )
551 // pop the listfile off the stack
552 this->ListFileStack
.pop_back();
557 this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile
.c_str());
558 this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile
.c_str());
561 // add this list file to the list of dependencies
562 this->ListFiles
.push_back( filenametoread
);
563 bool endScopeNicely
= true;
564 const size_t numberFunctions
= cacheFile
.Functions
.size();
565 for(size_t i
=0; i
< numberFunctions
; ++i
)
567 cmExecutionStatus status
;
568 this->ExecuteCommand(cacheFile
.Functions
[i
],status
);
569 if (status
.GetReturnInvoked() ||
570 cmSystemTools::GetFatalErrorOccured() )
572 // Exit early from processing this file.
573 endScopeNicely
= false;
578 // send scope ended to and function blockers
581 // loop over all function blockers to see if any block this command
582 for (std::list
<cmFunctionBlocker
*>::iterator pos
583 = this->FunctionBlockers
.begin();
584 pos
!= this->FunctionBlockers
.end(); ++pos
)
586 // if this blocker was not in the original then send a
587 // scope ended message
588 if (originalBlockers
.find(*pos
) == originalBlockers
.end())
590 (*pos
)->ScopeEnded(*this);
595 // If this is the directory-level CMakeLists.txt file then perform
596 // some extra checks.
597 if(this->ListFileStack
.size() == 1)
599 this->EnforceDirectoryLevelRules(endScopeNicely
);
602 this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile
.c_str());
603 this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile
.c_str());
605 // pop the listfile off the stack
606 this->ListFileStack
.pop_back();
611 //----------------------------------------------------------------------------
612 void cmMakefile::EnforceDirectoryLevelRules(bool endScopeNicely
)
614 // Enforce policy stack depth.
615 while(this->PolicyStack
.size() > 1)
619 this->IssueMessage(cmake::FATAL_ERROR
,
620 "cmake_policy PUSH without matching POP");
622 this->PopPolicy(false);
625 // Diagnose a violation of CMP0000 if necessary.
626 if(this->CheckCMP0000
)
629 msg
<< "No cmake_minimum_required command is present. "
630 << "A line of code such as\n"
631 << " cmake_minimum_required(VERSION "
632 << cmVersion::GetMajorVersion() << "."
633 << cmVersion::GetMinorVersion()
635 << "should be added at the top of the file. "
636 << "The version specified may be lower if you wish to "
637 << "support older CMake versions for this project. "
638 << "For more information run "
639 << "\"cmake --help-policy CMP0000\".";
640 switch (this->GetPolicyStatus(cmPolicies::CMP0000
))
642 case cmPolicies::WARN
:
643 // Warn because the user did not provide a mimimum required
645 this->IssueMessage(cmake::AUTHOR_WARNING
, msg
.str().c_str());
646 case cmPolicies::OLD
:
647 // OLD behavior is to use policy version 2.4 set in
650 case cmPolicies::REQUIRED_IF_USED
:
651 case cmPolicies::REQUIRED_ALWAYS
:
652 case cmPolicies::NEW
:
653 // NEW behavior is to issue an error.
654 this->IssueMessage(cmake::FATAL_ERROR
, msg
.str().c_str());
655 cmSystemTools::SetFatalErrorOccured();
661 void cmMakefile::AddCommand(cmCommand
* wg
)
663 this->GetCMakeInstance()->AddCommand(wg
);
667 void cmMakefile::SetLocalGenerator(cmLocalGenerator
* lg
)
669 this->LocalGenerator
= lg
;
672 bool cmMakefile::NeedBackwardsCompatibility(unsigned int major
,
676 if(this->LocalGenerator
)
679 this->LocalGenerator
->NeedBackwardsCompatibility(major
, minor
, patch
);
687 void cmMakefile::FinalPass()
689 // do all the variable expansions here
690 this->ExpandVariables();
692 // give all the commands a chance to do something
693 // after the file has been parsed before generation
694 for(std::vector
<cmCommand
*>::iterator i
= this->UsedCommands
.begin();
695 i
!= this->UsedCommands
.end(); ++i
)
702 // Generate the output file
703 void cmMakefile::ConfigureFinalPass()
707 = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
708 if (oldValue
&& atof(oldValue
) <= 1.2)
710 cmSystemTools::Error("You have requested backwards compatibility "
711 "with CMake version 1.2 or earlier. This version "
712 "of CMake only supports backwards compatibility "
713 "with CMake 1.4 or later. For compatibility with "
714 "1.2 or earlier please use CMake 2.0");
716 for (cmTargets::iterator l
= this->Targets
.begin();
717 l
!= this->Targets
.end(); l
++)
719 l
->second
.AnalyzeLibDependencies(*this);
723 //----------------------------------------------------------------------------
725 cmMakefile::AddCustomCommandToTarget(const char* target
,
726 const std::vector
<std::string
>& depends
,
727 const cmCustomCommandLines
& commandLines
,
728 cmTarget::CustomCommandType type
,
730 const char* workingDir
,
733 // Find the target to which to add the custom command.
734 cmTargets::iterator ti
= this->Targets
.find(target
);
735 if(ti
!= this->Targets
.end())
737 // Add the command to the appropriate build step for the target.
738 std::vector
<std::string
> no_output
;
739 cmCustomCommand
cc(no_output
, depends
, commandLines
, comment
, workingDir
);
740 cc
.SetEscapeOldStyle(escapeOldStyle
);
741 cc
.SetEscapeAllowMakeVars(true);
744 case cmTarget::PRE_BUILD
:
745 ti
->second
.GetPreBuildCommands().push_back(cc
);
747 case cmTarget::PRE_LINK
:
748 ti
->second
.GetPreLinkCommands().push_back(cc
);
750 case cmTarget::POST_BUILD
:
751 ti
->second
.GetPostBuildCommands().push_back(cc
);
757 //----------------------------------------------------------------------------
759 cmMakefile::AddCustomCommandToOutput(const std::vector
<std::string
>& outputs
,
760 const std::vector
<std::string
>& depends
,
761 const char* main_dependency
,
762 const cmCustomCommandLines
& commandLines
,
764 const char* workingDir
,
768 // Make sure there is at least one output.
771 cmSystemTools::Error("Attempt to add a custom rule with no output!");
775 // Choose a source file on which to store the custom command.
776 cmSourceFile
* file
= 0;
777 if(main_dependency
&& main_dependency
[0])
779 // The main dependency was specified. Use it unless a different
780 // custom command already used it.
781 file
= this->GetSource(main_dependency
);
782 if(file
&& file
->GetCustomCommand() && !replace
)
784 // The main dependency already has a custom command.
785 if(commandLines
== file
->GetCustomCommand()->GetCommandLines())
787 // The existing custom command is identical. Silently ignore
793 // The existing custom command is different. We need to
794 // generate a rule file for this new command.
800 // The main dependency does not have a custom command or we are
801 // allowed to replace it. Use it to store the command.
802 file
= this->GetOrCreateSource(main_dependency
);
806 // Generate a rule file if the main dependency is not available.
809 // Construct a rule file associated with the first output produced.
810 std::string outName
= outputs
[0];
813 // Check if the rule file already exists.
814 file
= this->GetSource(outName
.c_str());
815 if(file
&& file
->GetCustomCommand() && !replace
)
817 // The rule file already exists.
818 if(commandLines
!= file
->GetCustomCommand()->GetCommandLines())
820 cmSystemTools::Error("Attempt to add a custom rule to output \"",
822 "\" which already has a custom rule.");
827 // Create a cmSourceFile for the rule file.
828 file
= this->GetOrCreateSource(outName
.c_str(), true);
831 // Always create the output sources and mark them generated.
832 for(std::vector
<std::string
>::const_iterator o
= outputs
.begin();
833 o
!= outputs
.end(); ++o
)
835 if(cmSourceFile
* out
= this->GetOrCreateSource(o
->c_str(), true))
837 out
->SetProperty("GENERATED", "1");
841 // Construct a complete list of dependencies.
842 std::vector
<std::string
> depends2(depends
);
843 if(main_dependency
&& main_dependency
[0])
845 depends2
.push_back(main_dependency
);
848 // Attach the custom command to the file.
851 cmCustomCommand
* cc
=
852 new cmCustomCommand(outputs
, depends2
, commandLines
,
853 comment
, workingDir
);
854 cc
->SetEscapeOldStyle(escapeOldStyle
);
855 cc
->SetEscapeAllowMakeVars(true);
856 file
->SetCustomCommand(cc
);
860 //----------------------------------------------------------------------------
862 cmMakefile::AddCustomCommandToOutput(const char* output
,
863 const std::vector
<std::string
>& depends
,
864 const char* main_dependency
,
865 const cmCustomCommandLines
& commandLines
,
867 const char* workingDir
,
871 std::vector
<std::string
> outputs
;
872 outputs
.push_back(output
);
873 this->AddCustomCommandToOutput(outputs
, depends
, main_dependency
,
874 commandLines
, comment
, workingDir
,
875 replace
, escapeOldStyle
);
878 //----------------------------------------------------------------------------
880 cmMakefile::AddCustomCommandOldStyle(const char* target
,
881 const std::vector
<std::string
>& outputs
,
882 const std::vector
<std::string
>& depends
,
884 const cmCustomCommandLines
& commandLines
,
887 // Translate the old-style signature to one of the new-style
889 if(strcmp(source
, target
) == 0)
891 // In the old-style signature if the source and target were the
892 // same then it added a post-build rule to the target. Preserve
894 this->AddCustomCommandToTarget(target
, depends
, commandLines
,
895 cmTarget::POST_BUILD
, comment
, 0);
899 // Each output must get its own copy of this rule.
900 cmsys::RegularExpression
sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
901 "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
902 "hm|hpp|hxx|in|txx|inl)$");
903 for(std::vector
<std::string
>::const_iterator oi
= outputs
.begin();
904 oi
!= outputs
.end(); ++oi
)
906 // Get the name of this output.
907 const char* output
= oi
->c_str();
909 // Choose whether to use a main dependency.
910 if(sourceFiles
.find(source
))
912 // The source looks like a real file. Use it as the main dependency.
913 this->AddCustomCommandToOutput(output
, depends
, source
,
914 commandLines
, comment
, 0);
918 // The source may not be a real file. Do not use a main dependency.
919 const char* no_main_dependency
= 0;
920 std::vector
<std::string
> depends2
= depends
;
921 depends2
.push_back(source
);
922 this->AddCustomCommandToOutput(output
, depends2
, no_main_dependency
,
923 commandLines
, comment
, 0);
926 // If the rule was added to the source (and not a .rule file),
927 // then add the source to the target to make sure the rule is
929 std::string sname
= output
;
931 if(!this->GetSource(sname
.c_str()))
933 if (this->Targets
.find(target
) != this->Targets
.end())
935 this->Targets
[target
].AddSource(source
);
939 cmSystemTools::Error("Attempt to add a custom rule to a target "
940 "that does not exist yet for target ", target
);
947 //----------------------------------------------------------------------------
948 void cmMakefile::AddUtilityCommand(const char* utilityName
,
950 const std::vector
<std::string
>& depends
,
951 const char* workingDirectory
,
958 // Construct the command line for the custom command.
959 cmCustomCommandLine commandLine
;
960 commandLine
.push_back(command
);
963 commandLine
.push_back(arg1
);
967 commandLine
.push_back(arg2
);
971 commandLine
.push_back(arg3
);
975 commandLine
.push_back(arg4
);
977 cmCustomCommandLines commandLines
;
978 commandLines
.push_back(commandLine
);
980 // Call the real signature of this method.
981 this->AddUtilityCommand(utilityName
, excludeFromAll
, workingDirectory
,
982 depends
, commandLines
);
985 //----------------------------------------------------------------------------
987 cmMakefile::AddUtilityCommand(const char* utilityName
,
989 const char* workingDirectory
,
990 const std::vector
<std::string
>& depends
,
991 const cmCustomCommandLines
& commandLines
,
992 bool escapeOldStyle
, const char* comment
)
994 // Create a target instance for this utility.
995 cmTarget
* target
= this->AddNewTarget(cmTarget::UTILITY
, utilityName
);
998 target
->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
1002 // Use an empty comment to avoid generation of default comment.
1006 // Store the custom command in the target.
1007 std::string force
= this->GetStartOutputDirectory();
1008 force
+= cmake::GetCMakeFilesDirectory();
1010 force
+= utilityName
;
1011 const char* no_main_dependency
= 0;
1012 bool no_replace
= false;
1013 this->AddCustomCommandToOutput(force
.c_str(), depends
,
1015 commandLines
, comment
,
1016 workingDirectory
, no_replace
,
1018 cmSourceFile
* sf
= target
->AddSource(force
.c_str());
1020 // The output is not actually created so mark it symbolic.
1023 sf
->SetProperty("SYMBOLIC", "1");
1027 cmSystemTools::Error("Could not get source file entry for ",
1033 void cmMakefile::AddDefineFlag(const char* flag
)
1040 // Update the string used for the old DEFINITIONS property.
1041 this->AddDefineFlag(flag
, this->DefineFlagsOrig
);
1043 // If this is really a definition, update COMPILE_DEFINITIONS.
1044 if(this->ParseDefineFlag(flag
, false))
1049 // Add this flag that does not look like a definition.
1050 this->AddDefineFlag(flag
, this->DefineFlags
);
1053 void cmMakefile::AddDefineFlag(const char* flag
, std::string
& dflags
)
1056 std::string ret
= flag
;
1057 std::string::size_type pos
= 0;
1058 while((pos
= ret
.find('\n', pos
)) != std::string::npos
)
1064 while((pos
= ret
.find('\r', pos
)) != std::string::npos
)
1075 void cmMakefile::RemoveDefineFlag(const char* flag
)
1077 // Check the length of the flag to remove.
1078 std::string::size_type len
= strlen(flag
);
1084 // Update the string used for the old DEFINITIONS property.
1085 this->RemoveDefineFlag(flag
, len
, this->DefineFlagsOrig
);
1087 // If this is really a definition, update COMPILE_DEFINITIONS.
1088 if(this->ParseDefineFlag(flag
, true))
1093 // Remove this flag that does not look like a definition.
1094 this->RemoveDefineFlag(flag
, len
, this->DefineFlags
);
1097 void cmMakefile::RemoveDefineFlag(const char* flag
,
1098 std::string::size_type len
,
1099 std::string
& dflags
)
1101 // Remove all instances of the flag that are surrounded by
1102 // whitespace or the beginning/end of the string.
1103 for(std::string::size_type lpos
= dflags
.find(flag
, 0);
1104 lpos
!= std::string::npos
; lpos
= dflags
.find(flag
, lpos
))
1106 std::string::size_type rpos
= lpos
+ len
;
1107 if((lpos
<= 0 || isspace(dflags
[lpos
-1])) &&
1108 (rpos
>= dflags
.size() || isspace(dflags
[rpos
])))
1110 dflags
.erase(lpos
, len
);
1119 bool cmMakefile::ParseDefineFlag(std::string
const& def
, bool remove
)
1121 // Create a regular expression to match valid definitions.
1122 static cmsys::RegularExpression
1123 valid("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=.*)?$");
1125 // Make sure the definition matches.
1126 if(!valid
.find(def
.c_str()))
1131 // VS6 IDE does not support definitions with values.
1132 if((strcmp(this->LocalGenerator
->GetGlobalGenerator()->GetName(),
1133 "Visual Studio 6") == 0) &&
1134 (def
.find("=") != def
.npos
))
1139 // Definitions with non-trivial values require a policy check.
1140 static cmsys::RegularExpression
1141 trivial("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=[A-Za-z0-9_.]+)?$");
1142 if(!trivial
.find(def
.c_str()))
1144 // This definition has a non-trivial value.
1145 switch(this->GetPolicyStatus(cmPolicies::CMP0005
))
1147 case cmPolicies::WARN
:
1149 cmake::AUTHOR_WARNING
,
1150 this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0005
)
1152 case cmPolicies::OLD
:
1153 // OLD behavior is to not escape the value. We should not
1154 // convert the definition to use the property.
1156 case cmPolicies::REQUIRED_IF_USED
:
1157 case cmPolicies::REQUIRED_ALWAYS
:
1160 this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0005
)
1163 case cmPolicies::NEW
:
1164 // NEW behavior is to escape the value. Proceed to convert it
1165 // to an entry in the property.
1170 // Get the definition part after the flag.
1171 const char* define
= def
.c_str() + 2;
1175 if(const char* cdefs
= this->GetProperty("COMPILE_DEFINITIONS"))
1178 std::vector
<std::string
> defs
;
1179 cmSystemTools::ExpandListArgument(cdefs
, defs
);
1181 // Recompose the list without the definition.
1183 const char* sep
= "";
1184 for(std::vector
<std::string
>::const_iterator di
= defs
.begin();
1185 di
!= defs
.end(); ++di
)
1195 // Store the new list.
1196 this->SetProperty("COMPILE_DEFINITIONS", ndefs
.c_str());
1201 // Append the definition to the directory property.
1202 this->AppendProperty("COMPILE_DEFINITIONS", define
);
1208 void cmMakefile::AddLinkLibrary(const char* lib
,
1209 cmTarget::LinkLibraryType llt
)
1211 cmTarget::LibraryID tmp
;
1214 this->LinkLibraries
.push_back(tmp
);
1217 void cmMakefile::AddLinkLibraryForTarget(const char *target
,
1219 cmTarget::LinkLibraryType llt
)
1221 cmTargets::iterator i
= this->Targets
.find(target
);
1222 if ( i
!= this->Targets
.end())
1225 this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0,lib
);
1228 // CMake versions below 2.4 allowed linking to modules.
1229 bool allowModules
= this->NeedBackwardsCompatibility(2,2);
1230 // if it is not a static or shared library then you can not link to it
1231 if(!((tgt
->GetType() == cmTarget::STATIC_LIBRARY
) ||
1232 (tgt
->GetType() == cmTarget::SHARED_LIBRARY
) ||
1233 tgt
->IsExecutableWithExports()))
1236 e
<< "Target \"" << lib
<< "\" of type "
1237 << cmTarget::TargetTypeNames
[static_cast<int>(tgt
->GetType())]
1238 << " may not be linked into another target. "
1239 << "One may link only to STATIC or SHARED libraries, or "
1240 << "to executables with the ENABLE_EXPORTS property set.";
1241 // in older versions of cmake linking to modules was allowed
1242 if( tgt
->GetType() == cmTarget::MODULE_LIBRARY
)
1245 << "If you are developing a new project, re-organize it to avoid "
1246 << "linking to modules. "
1247 << "If you are just trying to build an existing project, "
1248 << "set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower to allow "
1249 << "linking to modules.";
1251 // if no modules are allowed then this is always an error
1253 // if we allow modules but the type is not a module then it is
1255 (allowModules
&& tgt
->GetType() != cmTarget::MODULE_LIBRARY
))
1257 this->IssueMessage(cmake::FATAL_ERROR
, e
.str().c_str());
1261 i
->second
.AddLinkLibrary( *this, target
, lib
, llt
);
1266 e
<< "Attempt to add link library \""
1267 << lib
<< "\" to target \""
1268 << target
<< "\" which is not built by this project.";
1269 cmSystemTools::Error(e
.str().c_str());
1273 void cmMakefile::AddLinkDirectoryForTarget(const char *target
,
1276 cmTargets::iterator i
= this->Targets
.find(target
);
1277 if ( i
!= this->Targets
.end())
1279 i
->second
.AddLinkDirectory( d
);
1283 cmSystemTools::Error
1284 ("Attempt to add link directories to non-existant target: ",
1285 target
, " for directory ", d
);
1289 void cmMakefile::AddLinkLibrary(const char* lib
)
1291 this->AddLinkLibrary(lib
,cmTarget::GENERAL
);
1294 void cmMakefile::AddLinkDirectory(const char* dir
)
1296 // Don't add a link directory that is already present. Yes, this
1297 // linear search results in n^2 behavior, but n won't be getting
1298 // much bigger than 20. We cannot use a set because of order
1299 // dependency of the link search path.
1305 // remove trailing slashes
1306 if(dir
[strlen(dir
)-1] == '/')
1308 std::string newdir
= dir
;
1309 newdir
= newdir
.substr(0, newdir
.size()-1);
1310 if(std::find(this->LinkDirectories
.begin(),
1311 this->LinkDirectories
.end(),
1312 newdir
.c_str()) == this->LinkDirectories
.end())
1314 this->LinkDirectories
.push_back(newdir
);
1319 if(std::find(this->LinkDirectories
.begin(),
1320 this->LinkDirectories
.end(), dir
)
1321 == this->LinkDirectories
.end())
1323 this->LinkDirectories
.push_back(dir
);
1328 void cmMakefile::InitializeFromParent()
1330 cmMakefile
*parent
= this->LocalGenerator
->GetParent()->GetMakefile();
1332 // copy the definitions
1333 this->DefinitionStack
.front() = parent
->DefinitionStack
.back();
1335 // copy include paths
1336 this->IncludeDirectories
= parent
->IncludeDirectories
;
1337 this->SystemIncludeDirectories
= parent
->SystemIncludeDirectories
;
1340 this->DefineFlags
= parent
->DefineFlags
;
1341 this->DefineFlagsOrig
= parent
->DefineFlagsOrig
;
1343 // Include transform property. There is no per-config version.
1345 const char* prop
= "IMPLICIT_DEPENDS_INCLUDE_TRANSFORM";
1346 this->SetProperty(prop
, parent
->GetProperty(prop
));
1349 // compile definitions property and per-config versions
1351 this->SetProperty("COMPILE_DEFINITIONS",
1352 parent
->GetProperty("COMPILE_DEFINITIONS"));
1353 std::vector
<std::string
> configs
;
1354 if(const char* configTypes
=
1355 this->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
1357 cmSystemTools::ExpandListArgument(configTypes
, configs
);
1359 else if(const char* buildType
=
1360 this->GetDefinition("CMAKE_BUILD_TYPE"))
1362 configs
.push_back(buildType
);
1364 for(std::vector
<std::string
>::const_iterator ci
= configs
.begin();
1365 ci
!= configs
.end(); ++ci
)
1367 std::string defPropName
= "COMPILE_DEFINITIONS_";
1368 defPropName
+= cmSystemTools::UpperCase(*ci
);
1369 this->SetProperty(defPropName
.c_str(),
1370 parent
->GetProperty(defPropName
.c_str()));
1375 this->LinkLibraries
= parent
->LinkLibraries
;
1378 this->LinkDirectories
= parent
->LinkDirectories
;
1380 // the initial project name
1381 this->ProjectName
= parent
->ProjectName
;
1383 // Copy include regular expressions.
1384 this->IncludeFileRegularExpression
= parent
->IncludeFileRegularExpression
;
1385 this->ComplainFileRegularExpression
= parent
->ComplainFileRegularExpression
;
1387 // Imported targets.
1388 this->ImportedTargets
= parent
->ImportedTargets
;
1391 void cmMakefile::ConfigureSubDirectory(cmLocalGenerator
*lg2
)
1393 // copy our variables from the child makefile
1394 lg2
->GetMakefile()->InitializeFromParent();
1395 lg2
->GetMakefile()->MakeStartDirectoriesCurrent();
1396 if (this->GetCMakeInstance()->GetDebugOutput())
1398 std::string msg
=" Entering ";
1399 msg
+= lg2
->GetMakefile()->GetCurrentDirectory();
1400 cmSystemTools::Message(msg
.c_str());
1402 // finally configure the subdir
1404 if (this->GetCMakeInstance()->GetDebugOutput())
1406 std::string msg
=" Returning to ";
1407 msg
+= this->GetCurrentDirectory();
1408 cmSystemTools::Message(msg
.c_str());
1412 void cmMakefile::AddSubDirectory(const char* sub
,
1413 bool excludeFromAll
, bool preorder
)
1415 // the source path must be made full if it isn't already
1416 std::string srcPath
= sub
;
1417 if (!cmSystemTools::FileIsFullPath(srcPath
.c_str()))
1419 srcPath
= this->GetCurrentDirectory();
1424 // binary path must be made full if it isn't already
1425 std::string binPath
= sub
;
1426 if (!cmSystemTools::FileIsFullPath(binPath
.c_str()))
1428 binPath
= this->GetCurrentOutputDirectory();
1434 this->AddSubDirectory(srcPath
.c_str(), binPath
.c_str(),
1435 excludeFromAll
, preorder
, false);
1439 void cmMakefile::AddSubDirectory(const char* srcPath
, const char *binPath
,
1440 bool excludeFromAll
, bool preorder
,
1443 std::vector
<cmLocalGenerator
*>& children
=
1444 this->LocalGenerator
->GetChildren();
1445 // has this directory already been added? If so error
1447 for (i
= 0; i
< children
.size(); ++i
)
1449 if (srcPath
== children
[i
]->GetMakefile()->GetStartDirectory())
1451 cmSystemTools::Error
1452 ("Attempt to add subdirectory multiple times for directory.\n",
1458 // create a new local generator and set its parent
1459 cmLocalGenerator
*lg2
=
1460 this->LocalGenerator
->GetGlobalGenerator()->CreateLocalGenerator();
1461 lg2
->SetParent(this->LocalGenerator
);
1462 this->LocalGenerator
->GetGlobalGenerator()->AddLocalGenerator(lg2
);
1464 // set the subdirs start dirs
1465 lg2
->GetMakefile()->SetStartDirectory(srcPath
);
1466 lg2
->GetMakefile()->SetStartOutputDirectory(binPath
);
1469 lg2
->GetMakefile()->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
1471 lg2
->GetMakefile()->SetPreOrder(preorder
);
1475 this->ConfigureSubDirectory(lg2
);
1479 void cmMakefile::AddIncludeDirectory(const char* inc
, bool before
)
1481 // if there is a newline then break it into multiple arguments
1487 // Don't add an include directory that is already present. Yes,
1488 // this linear search results in n^2 behavior, but n won't be
1489 // getting much bigger than 20. We cannot use a set because of
1490 // order dependency of the include path.
1491 std::vector
<std::string
>::iterator i
=
1492 std::find(this->IncludeDirectories
.begin(),
1493 this->IncludeDirectories
.end(), inc
);
1494 if(i
== this->IncludeDirectories
.end())
1498 // WARNING: this *is* expensive (linear time) since it's a vector
1499 this->IncludeDirectories
.insert(this->IncludeDirectories
.begin(), inc
);
1503 this->IncludeDirectories
.push_back(inc
);
1510 // if this before and already in the path then remove it
1511 this->IncludeDirectories
.erase(i
);
1512 // WARNING: this *is* expensive (linear time) since it's a vector
1513 this->IncludeDirectories
.insert(this->IncludeDirectories
.begin(), inc
);
1518 //----------------------------------------------------------------------------
1519 void cmMakefile::AddSystemIncludeDirectory(const char* dir
)
1521 this->SystemIncludeDirectories
.insert(dir
);
1524 //----------------------------------------------------------------------------
1525 bool cmMakefile::IsSystemIncludeDirectory(const char* dir
)
1527 return (this->SystemIncludeDirectories
.find(dir
) !=
1528 this->SystemIncludeDirectories
.end());
1531 void cmMakefile::AddDefinition(const char* name
, const char* value
)
1539 if (this->GetCMakeInstance())
1541 this->GetCMakeInstance()->
1542 RecordPropertyAccess(name
,cmProperty::VARIABLE
);
1546 this->TemporaryDefinitionKey
= name
;
1547 this->DefinitionStack
.back()[this->TemporaryDefinitionKey
] = value
;
1549 #ifdef CMAKE_BUILD_WITH_CMAKE
1550 cmVariableWatch
* vv
= this->GetVariableWatch();
1553 vv
->VariableAccessed(this->TemporaryDefinitionKey
,
1554 cmVariableWatch::VARIABLE_MODIFIED_ACCESS
,
1562 void cmMakefile::AddCacheDefinition(const char* name
, const char* value
,
1564 cmCacheManager::CacheEntryType type
)
1566 const char* val
= value
;
1567 cmCacheManager::CacheIterator it
=
1568 this->GetCacheManager()->GetCacheIterator(name
);
1569 if(!it
.IsAtEnd() && (it
.GetType() == cmCacheManager::UNINITIALIZED
) &&
1572 val
= it
.GetValue();
1573 if ( type
== cmCacheManager::PATH
|| type
== cmCacheManager::FILEPATH
)
1575 std::vector
<std::string
>::size_type cc
;
1576 std::vector
<std::string
> files
;
1577 std::string nvalue
= "";
1578 cmSystemTools::ExpandListArgument(val
, files
);
1579 for ( cc
= 0; cc
< files
.size(); cc
++ )
1581 files
[cc
] = cmSystemTools::CollapseFullPath(files
[cc
].c_str());
1586 nvalue
+= files
[cc
];
1589 this->GetCacheManager()->AddCacheEntry(name
, nvalue
.c_str(), doc
, type
);
1590 val
= it
.GetValue();
1594 this->GetCacheManager()->AddCacheEntry(name
, val
, doc
, type
);
1595 // if there was a definition then remove it
1596 this->DefinitionStack
.back().erase( DefinitionMap::key_type(name
));
1600 void cmMakefile::AddDefinition(const char* name
, bool value
)
1604 this->DefinitionStack
.back()
1605 .erase( DefinitionMap::key_type(name
));
1606 this->DefinitionStack
.back()
1607 .insert(DefinitionMap::value_type(name
, "ON"));
1611 this->DefinitionStack
.back()
1612 .erase( DefinitionMap::key_type(name
));
1613 this->DefinitionStack
.back()
1614 .insert(DefinitionMap::value_type(name
, "OFF"));
1616 #ifdef CMAKE_BUILD_WITH_CMAKE
1617 cmVariableWatch
* vv
= this->GetVariableWatch();
1620 vv
->VariableAccessed(name
, cmVariableWatch::VARIABLE_MODIFIED_ACCESS
,
1621 value
?"ON":"OFF", this);
1627 void cmMakefile::AddCacheDefinition(const char* name
,
1632 cmCacheManager::CacheIterator it
=
1633 this->GetCacheManager()->GetCacheIterator(name
);
1634 if(!it
.IsAtEnd() && (it
.GetType() == cmCacheManager::UNINITIALIZED
) &&
1637 val
= it
.GetValueAsBool();
1639 this->GetCacheManager()->AddCacheEntry(name
, val
, doc
);
1640 this->AddDefinition(name
, val
);
1643 void cmMakefile::RemoveDefinition(const char* name
)
1645 this->DefinitionStack
.back().erase(DefinitionMap::key_type(name
));
1646 #ifdef CMAKE_BUILD_WITH_CMAKE
1647 cmVariableWatch
* vv
= this->GetVariableWatch();
1650 vv
->VariableAccessed(name
, cmVariableWatch::VARIABLE_REMOVED_ACCESS
,
1656 void cmMakefile::RemoveCacheDefinition(const char* name
)
1658 this->GetCacheManager()->RemoveCacheEntry(name
);
1661 void cmMakefile::SetProjectName(const char* p
)
1663 this->ProjectName
= p
;
1667 void cmMakefile::AddGlobalLinkInformation(const char* name
, cmTarget
& target
)
1669 // for these targets do not add anything
1670 switch(target
.GetType())
1672 case cmTarget::UTILITY
:
1673 case cmTarget::GLOBAL_TARGET
:
1677 std::vector
<std::string
>::iterator j
;
1678 for(j
= this->LinkDirectories
.begin();
1679 j
!= this->LinkDirectories
.end(); ++j
)
1681 target
.AddLinkDirectory(j
->c_str());
1683 target
.MergeLinkLibraries( *this, name
, this->LinkLibraries
);
1687 void cmMakefile::AddLibrary(const char* lname
, cmTarget::TargetType type
,
1688 const std::vector
<std::string
> &srcs
,
1689 bool excludeFromAll
)
1691 // wrong type ? default to STATIC
1692 if ( (type
!= cmTarget::STATIC_LIBRARY
)
1693 && (type
!= cmTarget::SHARED_LIBRARY
)
1694 && (type
!= cmTarget::MODULE_LIBRARY
))
1696 type
= cmTarget::STATIC_LIBRARY
;
1699 cmTarget
* target
= this->AddNewTarget(type
, lname
);
1700 // Clear its dependencies. Otherwise, dependencies might persist
1701 // over changes in CMakeLists.txt, making the information stale and
1703 target
->ClearDependencyInformation( *this, lname
);
1706 target
->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
1708 target
->AddSources(srcs
);
1709 this->AddGlobalLinkInformation(lname
, *target
);
1712 cmTarget
* cmMakefile::AddExecutable(const char *exeName
,
1713 const std::vector
<std::string
> &srcs
,
1714 bool excludeFromAll
)
1716 cmTarget
* target
= this->AddNewTarget(cmTarget::EXECUTABLE
, exeName
);
1719 target
->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
1721 target
->AddSources(srcs
);
1722 this->AddGlobalLinkInformation(exeName
, *target
);
1726 //----------------------------------------------------------------------------
1728 cmMakefile::AddNewTarget(cmTarget::TargetType type
, const char* name
)
1730 cmTargets::iterator it
=
1731 this->Targets
.insert(cmTargets::value_type(name
, cmTarget())).first
;
1732 cmTarget
& target
= it
->second
;
1733 target
.SetType(type
, name
);
1734 target
.SetMakefile(this);
1735 this->LocalGenerator
->GetGlobalGenerator()->AddTarget(*it
);
1739 cmSourceFile
*cmMakefile::GetSourceFileWithOutput(const char *cname
)
1741 std::string name
= cname
;
1744 // look through all the source files that have custom commands
1745 // and see if the custom command has the passed source file as an output
1746 // keep in mind the possible .rule extension that may be tacked on
1747 for(std::vector
<cmSourceFile
*>::const_iterator i
=
1748 this->SourceFiles
.begin(); i
!= this->SourceFiles
.end(); ++i
)
1750 // does this source file have a custom command?
1751 if ((*i
)->GetCustomCommand())
1753 // is the output of the custom command match the source files name
1754 const std::vector
<std::string
>& outputs
=
1755 (*i
)->GetCustomCommand()->GetOutputs();
1756 for(std::vector
<std::string
>::const_iterator o
= outputs
.begin();
1757 o
!= outputs
.end(); ++o
)
1760 std::string::size_type pos
= out
.rfind(name
);
1761 // If the output matches exactly
1762 if (pos
!= out
.npos
&&
1763 pos
== out
.size() - name
.size() &&
1764 (pos
==0 || out
[pos
-1] == '/'))
1772 // otherwise return NULL
1776 #if defined(CMAKE_BUILD_WITH_CMAKE)
1777 cmSourceGroup
* cmMakefile::GetSourceGroup(const std::vector
<std::string
>&name
)
1779 cmSourceGroup
* sg
= 0;
1781 // first look for source group starting with the same as the one we wants
1782 for (std::vector
<cmSourceGroup
>::iterator sgIt
= this->SourceGroups
.begin();
1783 sgIt
!= this->SourceGroups
.end(); ++sgIt
)
1786 std::string sgName
= sgIt
->GetName();
1787 if(sgName
== name
[0])
1796 // iterate through its children to find match source group
1797 for(unsigned int i
=1; i
<name
.size(); ++i
)
1799 sg
= sg
->lookupChild(name
[i
].c_str());
1809 void cmMakefile::AddSourceGroup(const char* name
,
1814 std::vector
<std::string
> nameVector
;
1815 nameVector
.push_back(name
);
1816 AddSourceGroup(nameVector
, regex
);
1820 void cmMakefile::AddSourceGroup(const std::vector
<std::string
>& name
,
1823 cmSourceGroup
* sg
= 0;
1824 std::vector
<std::string
> currentName
;
1826 const int lastElement
= static_cast<int>(name
.size()-1);
1827 for(i
=lastElement
; i
>=0; --i
)
1829 currentName
.assign(name
.begin(), name
.begin()+i
+1);
1830 sg
= this->GetSourceGroup(currentName
);
1837 // i now contains the index of the last found component
1840 // group already exists, replace its regular expression
1843 // We only want to set the regular expression. If there are already
1844 // source files in the group, we don't want to remove them.
1845 sg
->SetGroupRegex(regex
);
1851 // group does not exists nor belong to any existing group
1852 // add its first component
1853 this->SourceGroups
.push_back(cmSourceGroup(name
[0].c_str(), regex
));
1854 sg
= this->GetSourceGroup(currentName
);
1855 i
= 0; // last component found
1858 // build the whole source group path
1859 for(++i
; i
<=lastElement
; ++i
)
1861 sg
->AddChild(cmSourceGroup(name
[i
].c_str(), 0));
1862 sg
= sg
->lookupChild(name
[i
].c_str());
1865 sg
->SetGroupRegex(regex
);
1870 void cmMakefile::AddExtraDirectory(const char* dir
)
1872 this->AuxSourceDirectories
.push_back(dir
);
1876 // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
1877 // include and library directories.
1879 void cmMakefile::ExpandVariables()
1881 // Now expand variables in the include and link strings
1882 for(std::vector
<std::string
>::iterator d
= this->IncludeDirectories
.begin();
1883 d
!= this->IncludeDirectories
.end(); ++d
)
1885 this->ExpandVariablesInString(*d
, true, true);
1887 for(std::vector
<std::string
>::iterator d
= this->LinkDirectories
.begin();
1888 d
!= this->LinkDirectories
.end(); ++d
)
1890 this->ExpandVariablesInString(*d
, true, true);
1892 for(cmTarget::LinkLibraryVectorType::iterator l
=
1893 this->LinkLibraries
.begin();
1894 l
!= this->LinkLibraries
.end(); ++l
)
1896 this->ExpandVariablesInString(l
->first
, true, true);
1900 bool cmMakefile::IsOn(const char* name
) const
1902 const char* value
= this->GetDefinition(name
);
1903 return cmSystemTools::IsOn(value
);
1906 bool cmMakefile::IsSet(const char* name
) const
1908 const char* value
= this->GetDefinition(name
);
1919 if ( cmSystemTools::IsNOTFOUND(value
) )
1927 bool cmMakefile::CanIWriteThisFile(const char* fileName
)
1929 if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
1933 // If we are doing an in-source build, than the test will always fail
1934 if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
1935 this->GetHomeOutputDirectory()) )
1937 if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
1944 // Check if this is subdirectory of the source tree but not a
1945 // subdirectory of a build tree
1946 if ( cmSystemTools::IsSubDirectory(fileName
,
1947 this->GetHomeDirectory()) &&
1948 !cmSystemTools::IsSubDirectory(fileName
,
1949 this->GetHomeOutputDirectory()) )
1956 const char* cmMakefile::GetRequiredDefinition(const char* name
) const
1958 const char* ret
= this->GetDefinition(name
);
1961 cmSystemTools::Error("Error required internal CMake variable not "
1962 "set, cmake may be not be built correctly.\n",
1963 "Missing variable is:\n",
1970 bool cmMakefile::IsDefinitionSet(const char* name
) const
1972 const char* def
= 0;
1973 DefinitionMap::const_iterator pos
=
1974 this->DefinitionStack
.back().find(name
);
1975 if(pos
!= this->DefinitionStack
.back().end())
1977 def
= (*pos
).second
.c_str();
1981 def
= this->GetCacheManager()->GetCacheValue(name
);
1983 #ifdef CMAKE_BUILD_WITH_CMAKE
1984 if(cmVariableWatch
* vv
= this->GetVariableWatch())
1988 vv
->VariableAccessed
1989 (name
, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS
,
1994 return def
?true:false;
1997 const char* cmMakefile::GetDefinition(const char* name
) const
2000 if (this->GetCMakeInstance())
2002 this->GetCMakeInstance()->
2003 RecordPropertyAccess(name
,cmProperty::VARIABLE
);
2006 const char* def
= 0;
2007 DefinitionMap::const_iterator pos
=
2008 this->DefinitionStack
.back().find(name
);
2009 if(pos
!= this->DefinitionStack
.back().end())
2011 def
= (*pos
).second
.c_str();
2015 def
= this->GetCacheManager()->GetCacheValue(name
);
2017 #ifdef CMAKE_BUILD_WITH_CMAKE
2018 cmVariableWatch
* vv
= this->GetVariableWatch();
2023 vv
->VariableAccessed(name
, cmVariableWatch::VARIABLE_READ_ACCESS
,
2028 // are unknown access allowed
2029 DefinitionMap::const_iterator pos2
=
2030 this->DefinitionStack
.back()
2031 .find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
2032 if (pos2
!= this->DefinitionStack
.back().end() &&
2033 cmSystemTools::IsOn((*pos2
).second
.c_str()))
2035 vv
->VariableAccessed(name
,
2036 cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS
, def
, this);
2040 vv
->VariableAccessed(name
,
2041 cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS
, def
, this);
2049 const char* cmMakefile::GetSafeDefinition(const char* def
) const
2051 const char* ret
= this->GetDefinition(def
);
2059 std::vector
<std::string
> cmMakefile
2060 ::GetDefinitions(int cacheonly
/* = 0 */) const
2062 std::map
<cmStdString
, int> definitions
;
2065 DefinitionMap::const_iterator it
;
2066 for ( it
= this->DefinitionStack
.back().begin();
2067 it
!= this->DefinitionStack
.back().end(); it
++ )
2069 definitions
[it
->first
] = 1;
2072 cmCacheManager::CacheIterator cit
=
2073 this->GetCacheManager()->GetCacheIterator();
2074 for ( cit
.Begin(); !cit
.IsAtEnd(); cit
.Next() )
2076 definitions
[cit
.GetName()] = 1;
2079 std::vector
<std::string
> res
;
2081 std::map
<cmStdString
, int>::iterator fit
;
2082 for ( fit
= definitions
.begin(); fit
!= definitions
.end(); fit
++ )
2084 res
.push_back(fit
->first
);
2090 const char *cmMakefile::ExpandVariablesInString(std::string
& source
)
2092 return this->ExpandVariablesInString(source
, false, false);
2095 const char *cmMakefile::ExpandVariablesInString(std::string
& source
,
2099 const char* filename
,
2104 if ( source
.empty() || source
.find_first_of("$@\\") == source
.npos
)
2106 return source
.c_str();
2109 // Special-case the @ONLY mode.
2112 if(!noEscapes
|| !removeEmpty
|| !replaceAt
)
2114 // This case should never be called. At-only is for
2115 // configure-file/string which always does no escapes.
2116 this->IssueMessage(cmake::INTERNAL_ERROR
,
2117 "ExpandVariablesInString @ONLY called "
2118 "on something with escapes.");
2121 // Store an original copy of the input.
2122 std::string input
= source
;
2124 // Start with empty output.
2127 // Look for one @VAR@ at a time.
2128 const char* in
= input
.c_str();
2129 while(this->cmAtVarRegex
.find(in
))
2131 // Get the range of the string to replace.
2132 const char* first
= in
+ this->cmAtVarRegex
.start();
2133 const char* last
= in
+ this->cmAtVarRegex
.end();
2135 // Store the unchanged part of the string now.
2136 source
.append(in
, first
-in
);
2138 // Lookup the definition of VAR.
2139 std::string
var(first
+1, last
-first
-2);
2140 if(const char* val
= this->GetDefinition(var
.c_str()))
2142 // Store the value in the output escaping as requested.
2145 source
.append(cmSystemTools::EscapeQuotes(val
));
2153 // Continue looking for @VAR@ further along the string.
2157 // Append the rest of the unchanged part of the string.
2160 return source
.c_str();
2163 // This method replaces ${VAR} and @VAR@ where VAR is looked up
2164 // with GetDefinition(), if not found in the map, nothing is expanded.
2165 // It also supports the $ENV{VAR} syntax where VAR is looked up in
2166 // the current environment variables.
2168 cmCommandArgumentParserHelper parser
;
2169 parser
.SetMakefile(this);
2170 parser
.SetLineFile(line
, filename
);
2171 parser
.SetEscapeQuotes(escapeQuotes
);
2172 parser
.SetNoEscapeMode(noEscapes
);
2173 parser
.SetReplaceAtSyntax(replaceAt
);
2174 parser
.SetRemoveEmpty(removeEmpty
);
2175 int res
= parser
.ParseString(source
.c_str(), 0);
2176 const char* emsg
= parser
.GetError();
2177 if ( res
&& !emsg
[0] )
2179 source
= parser
.GetResult();
2183 // Construct the main error message.
2184 cmOStringStream error
;
2185 error
<< "Syntax error in cmake code ";
2186 if(filename
&& line
> 0)
2188 // This filename and line number may be more specific than the
2189 // command context because one command invocation can have
2190 // arguments on multiple lines.
2192 << " " << filename
<< ":" << line
<< "\n";
2194 error
<< "when parsing string\n"
2195 << " " << source
.c_str() << "\n";
2198 // If the parser failed ("res" is false) then this is a real
2199 // argument parsing error, so the policy applies. Otherwise the
2200 // parser reported an error message without failing because the
2201 // helper implementation is unhappy, which has always reported an
2203 cmake::MessageType mtype
= cmake::FATAL_ERROR
;
2206 // This is a real argument parsing error. Use policy CMP0010 to
2207 // decide whether it is an error.
2208 switch(this->GetPolicyStatus(cmPolicies::CMP0010
))
2210 case cmPolicies::WARN
:
2212 << (this->GetPolicies()
2213 ->GetPolicyWarning(cmPolicies::CMP0010
));
2214 case cmPolicies::OLD
:
2215 // OLD behavior is to just warn and continue.
2216 mtype
= cmake::AUTHOR_WARNING
;
2218 case cmPolicies::REQUIRED_IF_USED
:
2219 case cmPolicies::REQUIRED_ALWAYS
:
2221 << (this->GetPolicies()
2222 ->GetRequiredPolicyError(cmPolicies::CMP0010
));
2223 case cmPolicies::NEW
:
2224 // NEW behavior is to report the error.
2225 cmSystemTools::SetFatalErrorOccured();
2229 this->IssueMessage(mtype
, error
.str());
2231 return source
.c_str();
2234 void cmMakefile::RemoveVariablesInString(std::string
& source
,
2239 cmsys::RegularExpression
var("(\\${[A-Za-z_0-9]*})");
2240 while (var
.find(source
))
2242 source
.erase(var
.start(),var
.end() - var
.start());
2248 cmsys::RegularExpression
varb("(\\$ENV{[A-Za-z_0-9]*})");
2249 while (varb
.find(source
))
2251 source
.erase(varb
.start(),varb
.end() - varb
.start());
2254 cmsys::RegularExpression
var2("(@[A-Za-z_0-9]*@)");
2255 while (var2
.find(source
))
2257 source
.erase(var2
.start(),var2
.end() - var2
.start());
2262 * Add the default definitions to the makefile. These values must not
2263 * be dependent on anything that isn't known when this cmMakefile instance
2266 void cmMakefile::AddDefaultDefinitions()
2268 /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
2269 With CMake must separate between target and host platform. In most cases
2270 the tests for WIN32, UNIX and APPLE will be for the target system, so an
2271 additional set of variables for the host system is required ->
2272 CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
2273 WIN32, UNIX and APPLE are now set in the platform files in
2275 To keep cmake scripts (-P) and custom language and compiler modules
2276 working, these variables are still also set here in this place, but they
2277 will be reset in CMakeSystemSpecificInformation.cmake before the platform
2278 files are executed. */
2279 #if defined(_WIN32) || defined(__CYGWIN__)
2280 this->AddDefinition("WIN32", "1");
2281 this->AddDefinition("CMAKE_HOST_WIN32", "1");
2283 this->AddDefinition("UNIX", "1");
2284 this->AddDefinition("CMAKE_HOST_UNIX", "1");
2286 // Cygwin is more like unix so enable the unix commands
2287 #if defined(__CYGWIN__)
2288 this->AddDefinition("UNIX", "1");
2289 this->AddDefinition("CMAKE_HOST_UNIX", "1");
2291 #if defined(__APPLE__)
2292 this->AddDefinition("APPLE", "1");
2293 this->AddDefinition("CMAKE_HOST_APPLE", "1");
2297 sprintf(temp
, "%d", cmVersion::GetMinorVersion());
2298 this->AddDefinition("CMAKE_MINOR_VERSION", temp
);
2299 sprintf(temp
, "%d", cmVersion::GetMajorVersion());
2300 this->AddDefinition("CMAKE_MAJOR_VERSION", temp
);
2301 sprintf(temp
, "%d", cmVersion::GetPatchVersion());
2302 this->AddDefinition("CMAKE_PATCH_VERSION", temp
);
2304 this->AddDefinition("CMAKE_FILES_DIRECTORY",
2305 cmake::GetCMakeFilesDirectory());
2308 #if defined(CMAKE_BUILD_WITH_CMAKE)
2310 * Find a source group whose regular expression matches the filename
2311 * part of the given source name. Search backward through the list of
2312 * source groups, and take the first matching group found. This way
2313 * non-inherited SOURCE_GROUP commands will have precedence over
2317 cmMakefile::FindSourceGroup(const char* source
,
2318 std::vector
<cmSourceGroup
> &groups
)
2320 // First search for a group that lists the file explicitly.
2321 for(std::vector
<cmSourceGroup
>::reverse_iterator sg
= groups
.rbegin();
2322 sg
!= groups
.rend(); ++sg
)
2324 cmSourceGroup
*result
= sg
->MatchChildrenFiles(source
);
2331 // Now search for a group whose regex matches the file.
2332 for(std::vector
<cmSourceGroup
>::reverse_iterator sg
= groups
.rbegin();
2333 sg
!= groups
.rend(); ++sg
)
2335 cmSourceGroup
*result
= sg
->MatchChildrenRegex(source
);
2343 // Shouldn't get here, but just in case, return the default group.
2344 return groups
.front();
2348 bool cmMakefile::IsFunctionBlocked(const cmListFileFunction
& lff
,
2349 cmExecutionStatus
&status
)
2351 // if there are no blockers get out of here
2352 if (this->FunctionBlockers
.begin() == this->FunctionBlockers
.end())
2357 // loop over all function blockers to see if any block this command
2358 // evaluate in reverse, this is critical for balanced IF statements etc
2359 std::list
<cmFunctionBlocker
*>::reverse_iterator pos
;
2360 for (pos
= this->FunctionBlockers
.rbegin();
2361 pos
!= this->FunctionBlockers
.rend(); ++pos
)
2363 if((*pos
)->IsFunctionBlocked(lff
, *this, status
))
2372 bool cmMakefile::ExpandArguments(
2373 std::vector
<cmListFileArgument
> const& inArgs
,
2374 std::vector
<std::string
>& outArgs
)
2376 std::vector
<cmListFileArgument
>::const_iterator i
;
2378 outArgs
.reserve(inArgs
.size());
2379 for(i
= inArgs
.begin(); i
!= inArgs
.end(); ++i
)
2381 // Expand the variables in the argument.
2383 this->ExpandVariablesInString(value
, false, false, false,
2384 i
->FilePath
, i
->Line
,
2387 // If the argument is quoted, it should be one argument.
2388 // Otherwise, it may be a list of arguments.
2391 outArgs
.push_back(value
);
2395 cmSystemTools::ExpandListArgument(value
, outArgs
);
2398 return !cmSystemTools::GetFatalErrorOccured();
2401 void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction
& lff
)
2403 // loop over all function blockers to see if any block this command
2404 std::list
<cmFunctionBlocker
*>::reverse_iterator pos
;
2405 for (pos
= this->FunctionBlockers
.rbegin();
2406 pos
!= this->FunctionBlockers
.rend(); ++pos
)
2408 if ((*pos
)->ShouldRemove(lff
, *this))
2410 cmFunctionBlocker
* b
= *pos
;
2411 this->FunctionBlockers
.remove(b
);
2420 void cmMakefile::SetHomeDirectory(const char* dir
)
2422 this->cmHomeDirectory
= dir
;
2423 cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory
);
2424 this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
2425 if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
2427 this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
2431 void cmMakefile::SetHomeOutputDirectory(const char* lib
)
2433 this->HomeOutputDirectory
= lib
;
2434 cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory
);
2435 this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
2436 if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
2438 this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
2439 this->GetHomeOutputDirectory());
2445 * Register the given cmData instance with its own name.
2447 void cmMakefile::RegisterData(cmData
* data
)
2449 std::string name
= data
->GetName();
2450 DataMapType::const_iterator d
= this->DataMap
.find(name
);
2451 if((d
!= this->DataMap
.end()) && (d
->second
!= 0) && (d
->second
!= data
))
2455 this->DataMap
[name
] = data
;
2460 * Register the given cmData instance with the given name. This can be used
2461 * to register a NULL pointer.
2463 void cmMakefile::RegisterData(const char* name
, cmData
* data
)
2465 DataMapType::const_iterator d
= this->DataMap
.find(name
);
2466 if((d
!= this->DataMap
.end()) && (d
->second
!= 0) && (d
->second
!= data
))
2470 this->DataMap
[name
] = data
;
2475 * Lookup a cmData instance previously registered with the given name. If
2476 * the instance cannot be found, return NULL.
2478 cmData
* cmMakefile::LookupData(const char* name
) const
2480 DataMapType::const_iterator d
= this->DataMap
.find(name
);
2481 if(d
!= this->DataMap
.end())
2491 //----------------------------------------------------------------------------
2492 cmSourceFile
* cmMakefile::GetSource(const char* sourceName
)
2494 cmSourceFileLocation
sfl(this, sourceName
);
2495 for(std::vector
<cmSourceFile
*>::const_iterator
2496 sfi
= this->SourceFiles
.begin();
2497 sfi
!= this->SourceFiles
.end(); ++sfi
)
2499 cmSourceFile
* sf
= *sfi
;
2500 if(sf
->Matches(sfl
))
2508 //----------------------------------------------------------------------------
2509 cmSourceFile
* cmMakefile::GetOrCreateSource(const char* sourceName
,
2512 if(cmSourceFile
* esf
= this->GetSource(sourceName
))
2518 cmSourceFile
* sf
= new cmSourceFile(this, sourceName
);
2521 sf
->SetProperty("GENERATED", "1");
2523 this->SourceFiles
.push_back(sf
);
2528 void cmMakefile::EnableLanguage(std::vector
<std::string
> const & lang
,
2531 this->AddDefinition("CMAKE_CFG_INTDIR",
2532 this->LocalGenerator
->GetGlobalGenerator()->GetCMakeCFGInitDirectory());
2533 this->LocalGenerator
->GetGlobalGenerator()->EnableLanguage(lang
, this,
2537 void cmMakefile::ExpandSourceListArguments(
2538 std::vector
<std::string
> const& arguments
,
2539 std::vector
<std::string
>& newargs
, unsigned int /* start */)
2541 // now expand the args
2543 for(i
= 0; i
< arguments
.size(); ++i
)
2545 // List expansion will have been done already.
2546 newargs
.push_back(arguments
[i
]);
2550 int cmMakefile::TryCompile(const char *srcdir
, const char *bindir
,
2551 const char *projectName
, const char *targetName
,
2552 const std::vector
<std::string
> *cmakeArgs
,
2553 std::string
*output
)
2555 // does the binary directory exist ? If not create it...
2556 if (!cmSystemTools::FileIsDirectory(bindir
))
2558 cmSystemTools::MakeDirectory(bindir
);
2561 // change to the tests directory and run cmake
2562 // use the cmake object instead of calling cmake
2563 std::string cwd
= cmSystemTools::GetCurrentWorkingDirectory();
2564 cmSystemTools::ChangeDirectory(bindir
);
2566 // make sure the same generator is used
2567 // use this program as the cmake to be run, it should not
2568 // be run that way but the cmake object requires a vailid path
2569 std::string cmakeCommand
= this->GetDefinition("CMAKE_COMMAND");
2571 cm
.SetIsInTryCompile(true);
2572 cmGlobalGenerator
*gg
= cm
.CreateGlobalGenerator
2573 (this->LocalGenerator
->GetGlobalGenerator()->GetName());
2576 cmSystemTools::Error(
2577 "Internal CMake error, TryCompile bad GlobalGenerator");
2578 // return to the original directory
2579 cmSystemTools::ChangeDirectory(cwd
.c_str());
2582 cm
.SetGlobalGenerator(gg
);
2585 cm
.SetHomeDirectory(srcdir
);
2586 cm
.SetHomeOutputDirectory(bindir
);
2587 cm
.SetStartDirectory(srcdir
);
2588 cm
.SetStartOutputDirectory(bindir
);
2589 cm
.SetCMakeCommand(cmakeCommand
.c_str());
2591 // if cmake args were provided then pass them in
2594 cm
.SetCacheArgs(*cmakeArgs
);
2596 // to save time we pass the EnableLanguage info directly
2597 gg
->EnableLanguagesFromGenerator
2598 (this->LocalGenerator
->GetGlobalGenerator());
2599 if(this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS"))
2601 cm
.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
2602 "TRUE", "", cmCacheManager::INTERNAL
);
2606 cm
.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
2607 "FALSE", "", cmCacheManager::INTERNAL
);
2609 if (cm
.Configure() != 0)
2611 cmSystemTools::Error(
2612 "Internal CMake error, TryCompile configure of cmake failed");
2613 // return to the original directory
2614 cmSystemTools::ChangeDirectory(cwd
.c_str());
2618 if (cm
.Generate() != 0)
2620 cmSystemTools::Error(
2621 "Internal CMake error, TryCompile generation of cmake failed");
2622 // return to the original directory
2623 cmSystemTools::ChangeDirectory(cwd
.c_str());
2627 // finally call the generator to actually build the resulting project
2629 this->LocalGenerator
->GetGlobalGenerator()->TryCompile(srcdir
,bindir
,
2635 cmSystemTools::ChangeDirectory(cwd
.c_str());
2639 cmake
*cmMakefile::GetCMakeInstance() const
2641 if ( this->LocalGenerator
&& this->LocalGenerator
->GetGlobalGenerator() )
2643 return this->LocalGenerator
->GetGlobalGenerator()->GetCMakeInstance();
2648 #ifdef CMAKE_BUILD_WITH_CMAKE
2649 cmVariableWatch
*cmMakefile::GetVariableWatch() const
2651 if ( this->GetCMakeInstance() &&
2652 this->GetCMakeInstance()->GetVariableWatch() )
2654 return this->GetCMakeInstance()->GetVariableWatch();
2660 void cmMakefile::AddMacro(const char* name
, const char* signature
)
2662 if ( !name
|| !signature
)
2666 this->MacrosMap
[name
] = signature
;
2669 void cmMakefile::GetListOfMacros(std::string
& macros
)
2671 StringStringMap::iterator it
;
2674 for ( it
= this->MacrosMap
.begin(); it
!= this->MacrosMap
.end(); ++it
)
2680 macros
+= it
->first
;
2685 cmCacheManager
*cmMakefile::GetCacheManager() const
2687 return this->GetCMakeInstance()->GetCacheManager();
2690 void cmMakefile::DisplayStatus(const char* message
, float s
)
2692 this->GetLocalGenerator()->GetGlobalGenerator()
2693 ->GetCMakeInstance()->UpdateProgress(message
, s
);
2696 std::string
cmMakefile::GetModulesFile(const char* filename
)
2698 std::vector
<std::string
> modulePath
;
2699 const char* def
= this->GetDefinition("CMAKE_MODULE_PATH");
2702 cmSystemTools::ExpandListArgument(def
, modulePath
);
2705 // Also search in the standard modules location.
2706 def
= this->GetDefinition("CMAKE_ROOT");
2709 std::string rootModules
= def
;
2710 rootModules
+= "/Modules";
2711 modulePath
.push_back(rootModules
);
2713 //std::string Look through the possible module directories.
2714 for(std::vector
<std::string
>::iterator i
= modulePath
.begin();
2715 i
!= modulePath
.end(); ++i
)
2717 std::string itempl
= *i
;
2718 cmSystemTools::ConvertToUnixSlashes(itempl
);
2721 if(cmSystemTools::FileExists(itempl
.c_str()))
2729 void cmMakefile::ConfigureString(const std::string
& input
,
2730 std::string
& output
, bool atOnly
,
2733 // Split input to handle one line at a time.
2734 std::string::const_iterator lineStart
= input
.begin();
2735 while(lineStart
!= input
.end())
2737 // Find the end of this line.
2738 std::string::const_iterator lineEnd
= lineStart
;
2739 while(lineEnd
!= input
.end() && *lineEnd
!= '\n')
2745 std::string
line(lineStart
, lineEnd
);
2747 // Skip the newline character.
2748 bool haveNewline
= (lineEnd
!= input
.end());
2754 // Replace #cmakedefine instances.
2755 if(this->cmDefineRegex
.find(line
))
2758 this->GetDefinition(this->cmDefineRegex
.match(1).c_str());
2759 if(!cmSystemTools::IsOff(def
))
2761 cmSystemTools::ReplaceString(line
, "#cmakedefine", "#define");
2766 output
+= "/* #undef ";
2767 output
+= this->cmDefineRegex
.match(1);
2771 else if(this->cmDefine01Regex
.find(line
))
2774 this->GetDefinition(this->cmDefine01Regex
.match(1).c_str());
2775 cmSystemTools::ReplaceString(line
, "#cmakedefine01", "#define");
2777 if(!cmSystemTools::IsOff(def
))
2796 // Move to the next line.
2797 lineStart
= lineEnd
;
2800 // Perform variable replacements.
2801 this->ExpandVariablesInString(output
, escapeQuotes
, true,
2802 atOnly
, 0, -1, true);
2805 int cmMakefile::ConfigureFile(const char* infile
, const char* outfile
,
2806 bool copyonly
, bool atOnly
, bool escapeQuotes
)
2809 if ( !this->CanIWriteThisFile(outfile
) )
2811 cmSystemTools::Error("Attempt to write file: ",
2812 outfile
, " into a source directory.");
2815 if ( !cmSystemTools::FileExists(infile
) )
2817 cmSystemTools::Error("File ", infile
, " does not exist.");
2820 std::string soutfile
= outfile
;
2821 std::string sinfile
= infile
;
2822 this->AddCMakeDependFile(infile
);
2823 cmSystemTools::ConvertToUnixSlashes(soutfile
);
2825 cmSystemTools::GetPermissions(sinfile
.c_str(), perm
);
2826 std::string::size_type pos
= soutfile
.rfind('/');
2827 if(pos
!= std::string::npos
)
2829 std::string path
= soutfile
.substr(0, pos
);
2830 cmSystemTools::MakeDirectory(path
.c_str());
2835 if ( !cmSystemTools::CopyFileIfDifferent(sinfile
.c_str(),
2843 std::string tempOutputFile
= soutfile
;
2844 tempOutputFile
+= ".tmp";
2845 std::ofstream
fout(tempOutputFile
.c_str());
2848 cmSystemTools::Error(
2849 "Could not open file for write in copy operation ",
2850 tempOutputFile
.c_str());
2851 cmSystemTools::ReportLastSystemError("");
2854 std::ifstream
fin(sinfile
.c_str());
2857 cmSystemTools::Error("Could not open file for read in copy operation ",
2862 // now copy input to output and expand variables in the
2863 // input file at the same time
2865 std::string outLine
;
2866 while( cmSystemTools::GetLineFromStream(fin
, inLine
) )
2869 this->ConfigureString(inLine
, outLine
, atOnly
, escapeQuotes
);
2870 fout
<< outLine
.c_str() << "\n";
2872 // close the files before attempting to copy
2875 if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile
.c_str(),
2882 cmSystemTools::SetPermissions(soutfile
.c_str(), perm
);
2884 cmSystemTools::RemoveFile(tempOutputFile
.c_str());
2889 void cmMakefile::SetProperty(const char* prop
, const char* value
)
2896 // handle special props
2897 std::string propname
= prop
;
2898 if ( propname
== "INCLUDE_DIRECTORIES" )
2900 std::vector
<std::string
> varArgsExpanded
;
2901 cmSystemTools::ExpandListArgument(value
, varArgsExpanded
);
2902 this->SetIncludeDirectories(varArgsExpanded
);
2906 if ( propname
== "LINK_DIRECTORIES" )
2908 std::vector
<std::string
> varArgsExpanded
;
2909 cmSystemTools::ExpandListArgument(value
, varArgsExpanded
);
2910 this->SetLinkDirectories(varArgsExpanded
);
2914 if ( propname
== "INCLUDE_REGULAR_EXPRESSION" )
2916 this->SetIncludeRegularExpression(value
);
2920 if ( propname
== "ADDITIONAL_MAKE_CLEAN_FILES" )
2922 // This property is not inherrited
2923 if ( strcmp(this->GetCurrentDirectory(),
2924 this->GetStartDirectory()) != 0 )
2930 this->Properties
.SetProperty(prop
,value
,cmProperty::DIRECTORY
);
2933 void cmMakefile::AppendProperty(const char* prop
, const char* value
)
2940 // handle special props
2941 std::string propname
= prop
;
2942 if ( propname
== "INCLUDE_DIRECTORIES" )
2944 std::vector
<std::string
> varArgsExpanded
;
2945 cmSystemTools::ExpandListArgument(value
, varArgsExpanded
);
2946 for(std::vector
<std::string
>::const_iterator vi
= varArgsExpanded
.begin();
2947 vi
!= varArgsExpanded
.end(); ++vi
)
2949 this->AddIncludeDirectory(vi
->c_str());
2954 if ( propname
== "LINK_DIRECTORIES" )
2956 std::vector
<std::string
> varArgsExpanded
;
2957 cmSystemTools::ExpandListArgument(value
, varArgsExpanded
);
2958 for(std::vector
<std::string
>::const_iterator vi
= varArgsExpanded
.begin();
2959 vi
!= varArgsExpanded
.end(); ++vi
)
2961 this->AddLinkDirectory(vi
->c_str());
2966 this->Properties
.AppendProperty(prop
,value
,cmProperty::DIRECTORY
);
2969 const char *cmMakefile::GetPropertyOrDefinition(const char* prop
)
2971 const char *ret
= this->GetProperty(prop
, cmProperty::DIRECTORY
);
2974 ret
= this->GetDefinition(prop
);
2979 const char *cmMakefile::GetProperty(const char* prop
)
2981 return this->GetProperty(prop
, cmProperty::DIRECTORY
);
2984 const char *cmMakefile::GetProperty(const char* prop
,
2985 cmProperty::ScopeType scope
)
2987 // watch for specific properties
2988 static std::string output
;
2990 if (!strcmp("PARENT_DIRECTORY",prop
))
2992 if(cmLocalGenerator
* plg
= this->LocalGenerator
->GetParent())
2994 output
= plg
->GetMakefile()->GetStartDirectory();
2996 return output
.c_str();
2998 else if (!strcmp("INCLUDE_REGULAR_EXPRESSION",prop
) )
3000 output
= this->GetIncludeRegularExpression();
3001 return output
.c_str();
3003 else if (!strcmp("LISTFILE_STACK",prop
))
3005 for (std::deque
<cmStdString
>::iterator i
= this->ListFileStack
.begin();
3006 i
!= this->ListFileStack
.end(); ++i
)
3008 if (i
!= this->ListFileStack
.begin())
3014 return output
.c_str();
3016 else if (!strcmp("VARIABLES",prop
) || !strcmp("CACHE_VARIABLES",prop
))
3019 if ( !strcmp("CACHE_VARIABLES",prop
) )
3023 std::vector
<std::string
> vars
= this->GetDefinitions(cacheonly
);
3024 for (unsigned int cc
= 0; cc
< vars
.size(); cc
++ )
3032 return output
.c_str();
3034 else if (!strcmp("MACROS",prop
))
3036 this->GetListOfMacros(output
);
3037 return output
.c_str();
3039 else if (!strcmp("DEFINITIONS",prop
))
3041 output
+= this->DefineFlagsOrig
;
3042 return output
.c_str();
3044 else if (!strcmp("INCLUDE_DIRECTORIES",prop
) )
3046 cmOStringStream str
;
3047 for (std::vector
<std::string
>::const_iterator
3048 it
= this->GetIncludeDirectories().begin();
3049 it
!= this->GetIncludeDirectories().end();
3052 if ( it
!= this->GetIncludeDirectories().begin())
3059 return output
.c_str();
3061 else if (!strcmp("LINK_DIRECTORIES",prop
))
3063 cmOStringStream str
;
3064 for (std::vector
<std::string
>::const_iterator
3065 it
= this->GetLinkDirectories().begin();
3066 it
!= this->GetLinkDirectories().end();
3069 if ( it
!= this->GetLinkDirectories().begin())
3076 return output
.c_str();
3080 const char *retVal
=
3081 this->Properties
.GetPropertyValue(prop
, scope
, chain
);
3084 if(this->LocalGenerator
->GetParent())
3086 return this->LocalGenerator
->GetParent()->GetMakefile()->
3087 GetProperty(prop
, scope
);
3089 return this->GetCMakeInstance()->GetProperty(prop
,scope
);
3095 bool cmMakefile::GetPropertyAsBool(const char* prop
)
3097 return cmSystemTools::IsOn(this->GetProperty(prop
));
3101 cmTarget
* cmMakefile::FindTarget(const char* name
)
3103 cmTargets
& tgts
= this->GetTargets();
3105 cmTargets::iterator i
= tgts
.find ( name
);
3106 if ( i
!= tgts
.end() )
3114 cmTest
* cmMakefile::CreateTest(const char* testName
)
3120 cmTest
* test
= this->GetTest(testName
);
3126 test
->SetName(testName
);
3127 test
->SetMakefile(this);
3128 this->Tests
.push_back(test
);
3132 cmTest
* cmMakefile::GetTest(const char* testName
) const
3138 std::vector
<cmTest
*>::const_iterator it
;
3139 for ( it
= this->Tests
.begin(); it
!= this->Tests
.end(); ++ it
)
3141 if ( strcmp((*it
)->GetName(), testName
) == 0 )
3149 const std::vector
<cmTest
*> *cmMakefile::GetTests() const
3151 return &this->Tests
;
3154 std::vector
<cmTest
*> *cmMakefile::GetTests()
3156 return &this->Tests
;
3159 std::string
cmMakefile::GetListFileStack()
3161 cmOStringStream tmp
;
3162 size_t depth
= this->ListFileStack
.size();
3165 std::deque
<cmStdString
>::iterator it
= this->ListFileStack
.end();
3168 if (depth
!= this->ListFileStack
.size())
3179 while (it
!= this->ListFileStack
.begin());
3185 void cmMakefile::PushScope()
3187 // Get the index of the next stack entry.
3188 std::vector
<DefinitionMap
>::size_type index
= this->DefinitionStack
.size();
3190 // Allocate a new stack entry.
3191 this->DefinitionStack
.push_back(DefinitionMap());
3193 // Copy the previous top to the new top.
3194 this->DefinitionStack
[index
] = this->DefinitionStack
[index
-1];
3197 void cmMakefile::PopScope()
3199 this->DefinitionStack
.pop_back();
3202 void cmMakefile::RaiseScope(const char *var
, const char *varDef
)
3204 if (!var
|| !strlen(var
))
3209 // multiple scopes in this directory?
3210 if (this->DefinitionStack
.size() > 1)
3214 this->DefinitionStack
[this->DefinitionStack
.size()-2][var
] = varDef
;
3218 this->DefinitionStack
[this->DefinitionStack
.size()-2].erase(var
);
3221 // otherwise do the parent (if one exists)
3222 else if (this->LocalGenerator
->GetParent())
3224 cmMakefile
*parent
= this->LocalGenerator
->GetParent()->GetMakefile();
3229 parent
->AddDefinition(var
,varDef
);
3233 parent
->RemoveDefinition(var
);
3240 // define properties
3241 void cmMakefile::DefineProperties(cmake
*cm
)
3244 ("ADDITIONAL_MAKE_CLEAN_FILES", cmProperty::DIRECTORY
,
3245 "Additional files to clean during the make clean stage.",
3246 "A list of files that will be cleaned as a part of the "
3247 "\"make clean\" stage. ");
3250 ("CLEAN_NO_CUSTOM", cmProperty::DIRECTORY
,
3251 "Should the output of custom commands be left.",
3252 "If this is true then the outputs of custom commands for this "
3253 "directory will not be removed during the \"make clean\" stage. ");
3256 ("LISTFILE_STACK", cmProperty::DIRECTORY
,
3257 "The current stack of listfiles being processed.",
3258 "This property is mainly useful when trying to debug errors "
3259 "in your CMake scripts. It returns a list of what list files "
3260 "are currently being processed, in order. So if one listfile "
3261 "does an INCLUDE command then that is effectively pushing "
3262 "the included listfile onto the stack.", false);
3265 ("TEST_INCLUDE_FILE", cmProperty::DIRECTORY
,
3266 "A cmake file that will be included when ctest is run.",
3267 "If you specify TEST_INCLUDE_FILE, that file will be "
3268 "included and processed when ctest is run on the directory.");
3271 ("COMPILE_DEFINITIONS", cmProperty::DIRECTORY
,
3272 "Preprocessor definitions for compiling a directory's sources.",
3273 "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
3274 "definitions using the syntax VAR or VAR=value. Function-style "
3275 "definitions are not supported. CMake will automatically escape "
3276 "the value correctly for the native build system (note that CMake "
3277 "language syntax may require escapes to specify some values). "
3278 "This property may be set on a per-configuration basis using the name "
3279 "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
3280 "(ex. \"COMPILE_DEFINITIONS_DEBUG\"). "
3281 "This property will be initialized in each directory by its value "
3282 "in the directory's parent.\n"
3283 "CMake will automatically drop some definitions that "
3284 "are not supported by the native build tool. "
3285 "The VS6 IDE does not support definitions with values "
3286 "(but NMake does).\n"
3287 "Dislaimer: Most native build tools have poor support for escaping "
3288 "certain values. CMake has work-arounds for many cases but some "
3289 "values may just not be possible to pass correctly. If a value "
3290 "does not seem to be escaped correctly, do not attempt to "
3291 "work-around the problem by adding escape sequences to the value. "
3292 "Your work-around may break in a future version of CMake that "
3293 "has improved escape support. Instead consider defining the macro "
3294 "in a (configured) header file. Then report the limitation.");
3297 ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::DIRECTORY
,
3298 "Per-configuration preprocessor definitions in a directory.",
3299 "This is the configuration-specific version of COMPILE_DEFINITIONS. "
3300 "This property will be initialized in each directory by its value "
3301 "in the directory's parent.\n");
3304 ("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM", cmProperty::DIRECTORY
,
3305 "Specify #include line transforms for dependencies in a directory.",
3306 "This property specifies rules to transform macro-like #include lines "
3307 "during implicit dependency scanning of C and C++ source files. "
3308 "The list of rules must be semicolon-separated with each entry of "
3309 "the form \"A_MACRO(%)=value-with-%\" (the % must be literal). "
3310 "During dependency scanning occurrences of A_MACRO(...) on #include "
3311 "lines will be replaced by the value given with the macro argument "
3312 "substituted for '%'. For example, the entry\n"
3313 " MYDIR(%)=<mydir/%>\n"
3314 "will convert lines of the form\n"
3315 " #include MYDIR(myheader.h)\n"
3317 " #include <mydir/myheader.h>\n"
3318 "allowing the dependency to be followed.\n"
3319 "This property applies to sources in all targets within a directory. "
3320 "The property value is initialized in each directory by its value "
3321 "in the directory's parent.");
3324 ("EXCLUDE_FROM_ALL", cmProperty::DIRECTORY
,
3325 "Exclude the directory from the all target of its parent.",
3326 "A property on a directory that indicates if its targets are excluded "
3327 "from the default build target. If it is not, then with a Makefile "
3328 "for example typing make will cause the targets to be built. "
3329 "The same concept applies to the default build of other generators.",
3333 ("PARENT_DIRECTORY", cmProperty::DIRECTORY
,
3334 "Source directory that added current subdirectory.",
3335 "This read-only property specifies the source directory that "
3336 "added the current source directory as a subdirectory of the build. "
3337 "In the top-level directory the value is the empty-string.", false);
3340 ("INCLUDE_REGULAR_EXPRESSION", cmProperty::DIRECTORY
,
3341 "Include file scanning regular expression.",
3342 "This read-only property specifies the regular expression used "
3343 "during dependency scanning to match include files that should "
3344 "be followed. See the include_regular_expression command.", false);
3347 ("VARIABLES", cmProperty::DIRECTORY
,
3348 "List of variables defined in the current directory.",
3349 "This read-only property specifies the list of CMake variables "
3350 "currently defined. "
3351 "It is intended for debugging purposes.", false);
3354 ("CACHE_VARIABLES", cmProperty::DIRECTORY
,
3355 "List of cache variables available in the current directory.",
3356 "This read-only property specifies the list of CMake cache "
3357 "variables currently defined. "
3358 "It is intended for debugging purposes.", false);
3361 ("MACROS", cmProperty::DIRECTORY
,
3362 "List of macro commands available in the current directory.",
3363 "This read-only property specifies the list of CMake macros "
3364 "currently defined. "
3365 "It is intended for debugging purposes. "
3366 "See the macro command.", false);
3369 ("DEFINITIONS", cmProperty::DIRECTORY
,
3370 "For CMake 2.4 compatibility only. Use COMPILE_DEFINITIONS instead.",
3371 "This read-only property specifies the list of flags given so far "
3372 "to the add_definitions command. "
3373 "It is intended for debugging purposes. "
3374 "Use the COMPILE_DEFINITIONS instead.", false);
3377 ("INCLUDE_DIRECTORIES", cmProperty::DIRECTORY
,
3378 "List of preprocessor include file search directories.",
3379 "This read-only property specifies the list of directories given "
3380 "so far to the include_directories command. "
3381 "It is intended for debugging purposes.", false);
3384 ("LINK_DIRECTORIES", cmProperty::DIRECTORY
,
3385 "List of linker search directories.",
3386 "This read-only property specifies the list of directories given "
3387 "so far to the link_directories command. "
3388 "It is intended for debugging purposes.", false);
3391 //----------------------------------------------------------------------------
3393 cmMakefile::AddImportedTarget(const char* name
, cmTarget::TargetType type
)
3395 // Create the target.
3396 cmsys::auto_ptr
<cmTarget
> target(new cmTarget
);
3397 target
->SetType(type
, name
);
3398 target
->SetMakefile(this);
3399 target
->MarkAsImported();
3401 // Add to the set of available imported targets.
3402 this->ImportedTargets
[name
] = target
.get();
3404 // Transfer ownership to this cmMakefile object.
3405 this->ImportedTargetsOwned
.push_back(target
.get());
3406 return target
.release();
3409 //----------------------------------------------------------------------------
3410 cmTarget
* cmMakefile::FindTargetToUse(const char* name
)
3412 // Look for an imported target. These take priority because they
3413 // are more local in scope and do not have to be globally unique.
3414 std::map
<cmStdString
, cmTarget
*>::const_iterator
3415 imported
= this->ImportedTargets
.find(name
);
3416 if(imported
!= this->ImportedTargets
.end())
3418 return imported
->second
;
3421 // Look for a target built in this project.
3422 return this->LocalGenerator
->GetGlobalGenerator()->FindTarget(0, name
);
3425 //----------------------------------------------------------------------------
3426 bool cmMakefile::EnforceUniqueName(std::string
const& name
, std::string
& msg
,
3429 if(cmTarget
* existing
= this->FindTargetToUse(name
.c_str()))
3431 // The name given conflicts with an existing target. Produce an
3432 // error in a compatible way.
3433 if(existing
->IsImported())
3435 // Imported targets were not supported in previous versions.
3436 // This is new code, so we can make it an error.
3438 e
<< "cannot create target \"" << name
3439 << "\" because an imported target with the same name already exists.";
3445 // target names must be globally unique
3446 switch (this->GetPolicyStatus(cmPolicies::CMP0002
))
3448 case cmPolicies::WARN
:
3449 this->IssueMessage(cmake::AUTHOR_WARNING
, this->GetPolicies()->
3450 GetPolicyWarning(cmPolicies::CMP0002
));
3451 case cmPolicies::OLD
:
3453 case cmPolicies::REQUIRED_IF_USED
:
3454 case cmPolicies::REQUIRED_ALWAYS
:
3455 this->IssueMessage(cmake::FATAL_ERROR
,
3456 this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0002
)
3459 case cmPolicies::NEW
:
3463 // The conflict is with a non-imported target.
3464 // Allow this if the user has requested support.
3466 this->LocalGenerator
->GetGlobalGenerator()->GetCMakeInstance();
3467 if(isCustom
&& existing
->GetType() == cmTarget::UTILITY
&&
3468 this != existing
->GetMakefile() &&
3469 cm
->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
3474 // Produce an error that tells the user how to work around the
3477 e
<< "cannot create target \"" << name
3478 << "\" because another target with the same name already exists. "
3479 << "The existing target is ";
3480 switch(existing
->GetType())
3482 case cmTarget::EXECUTABLE
:
3483 e
<< "an executable ";
3485 case cmTarget::STATIC_LIBRARY
:
3486 e
<< "a static library ";
3488 case cmTarget::SHARED_LIBRARY
:
3489 e
<< "a shared library ";
3491 case cmTarget::MODULE_LIBRARY
:
3492 e
<< "a module library ";
3494 case cmTarget::UTILITY
:
3495 e
<< "a custom target ";
3499 e
<< "created in source directory \""
3500 << existing
->GetMakefile()->GetCurrentDirectory() << "\". "
3501 << "See documentation for policy CMP0002 for more details.";
3509 //----------------------------------------------------------------------------
3510 cmPolicies::PolicyStatus
3511 cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id
)
3513 // Get the current setting of the policy.
3514 cmPolicies::PolicyStatus cur
= this->GetPolicyStatusInternal(id
);
3516 // If the policy is required to be set to NEW but is not, ignore the
3517 // current setting and tell the caller.
3518 if(cur
!= cmPolicies::NEW
)
3520 if(cur
== cmPolicies::REQUIRED_ALWAYS
||
3521 cur
== cmPolicies::REQUIRED_IF_USED
)
3525 cmPolicies::PolicyStatus def
= this->GetPolicies()->GetPolicyStatus(id
);
3526 if(def
== cmPolicies::REQUIRED_ALWAYS
||
3527 def
== cmPolicies::REQUIRED_IF_USED
)
3533 // The current setting is okay.
3537 //----------------------------------------------------------------------------
3538 cmPolicies::PolicyStatus
3539 cmMakefile::GetPolicyStatusInternal(cmPolicies::PolicyID id
)
3541 // Is the policy set in our stack?
3542 for(std::vector
<PolicyMap
>::reverse_iterator
3543 psi
= this->PolicyStack
.rbegin();
3544 psi
!= this->PolicyStack
.rend(); ++psi
)
3546 PolicyMap::const_iterator pse
= psi
->find(id
);
3547 if(pse
!= psi
->end())
3553 // If we have a parent directory, recurse up to it.
3554 if(this->LocalGenerator
->GetParent())
3556 cmMakefile
* parent
= this->LocalGenerator
->GetParent()->GetMakefile();
3557 return parent
->GetPolicyStatusInternal(id
);
3560 // The policy is not set. Use the default for this CMake version.
3561 return this->GetPolicies()->GetPolicyStatus(id
);
3564 bool cmMakefile::SetPolicy(const char *id
,
3565 cmPolicies::PolicyStatus status
)
3567 cmPolicies::PolicyID pid
;
3568 if (!this->GetPolicies()->GetPolicyID(id
, /* out */ pid
))
3571 e
<< "Policy \"" << id
<< "\" is not known to this version of CMake.";
3572 this->IssueMessage(cmake::FATAL_ERROR
, e
.str());
3575 return this->SetPolicy(pid
,status
);
3578 //----------------------------------------------------------------------------
3579 bool cmMakefile::SetPolicy(cmPolicies::PolicyID id
,
3580 cmPolicies::PolicyStatus status
)
3582 // A REQUIRED_ALWAYS policy may be set only to NEW.
3583 if(status
!= cmPolicies::NEW
&&
3584 this->GetPolicies()->GetPolicyStatus(id
) ==
3585 cmPolicies::REQUIRED_ALWAYS
)
3588 this->GetPolicies()->GetRequiredAlwaysPolicyError(id
);
3589 this->IssueMessage(cmake::FATAL_ERROR
, msg
.c_str());
3593 // Store the setting.
3594 this->PolicyStack
.back()[id
] = status
;
3596 // Special hook for presenting compatibility variable as soon as
3597 // the user requests it.
3598 if(id
== cmPolicies::CMP0001
&&
3599 (status
== cmPolicies::WARN
|| status
== cmPolicies::OLD
))
3601 if(!(this->GetCacheManager()
3602 ->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")))
3604 // Set it to 2.4 because that is the last version where the
3605 // variable had meaning.
3606 this->AddCacheDefinition
3607 ("CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
3608 "For backwards compatibility, what version of CMake "
3610 "syntax should this version of CMake try to support.",
3611 cmCacheManager::STRING
);
3618 bool cmMakefile::PushPolicy()
3620 // Allocate a new stack entry.
3621 this->PolicyStack
.push_back(PolicyMap());
3625 bool cmMakefile::PopPolicy(bool reportError
)
3627 if(this->PolicyStack
.size() == 1)
3631 cmSystemTools::Error("Attempt to pop the policy stack past "
3636 this->PolicyStack
.pop_back();
3640 bool cmMakefile::SetPolicyVersion(const char *version
)
3642 return this->GetCMakeInstance()->GetPolicies()->
3643 ApplyPolicyVersion(this,version
);
3646 cmPolicies
*cmMakefile::GetPolicies()
3648 if (!this->GetCMakeInstance())
3652 return this->GetCMakeInstance()->GetPolicies();