1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalGenerator.cxx,v $
6 Date: $Date: 2009-09-22 20:16:55 $
7 Version: $Revision: 1.315 $
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 "cmLocalGenerator.h"
19 #include "cmComputeLinkInformation.h"
20 #include "cmGeneratedFileStream.h"
21 #include "cmGlobalGenerator.h"
22 #include "cmInstallGenerator.h"
23 #include "cmInstallFilesGenerator.h"
24 #include "cmInstallScriptGenerator.h"
25 #include "cmInstallTargetGenerator.h"
26 #include "cmMakefile.h"
27 #include "cmSourceFile.h"
29 #include "cmTestGenerator.h"
30 #include "cmVersion.h"
33 #if defined(CMAKE_BUILD_WITH_CMAKE)
34 # define CM_LG_ENCODE_OBJECT_NAMES
35 # include <cmsys/MD5.h>
38 #include <cmsys/System.h>
40 #include <ctype.h> // for isalpha
44 cmLocalGenerator::cmLocalGenerator()
46 this->Makefile
= 0; // moved to after set on global
48 this->WindowsShell
= false;
49 this->WindowsVSIDE
= false;
50 this->WatcomWMake
= false;
51 this->MinGWMake
= false;
53 this->MSYSShell
= false;
54 this->LinkScriptShell
= false;
55 this->IgnoreLibPrefix
= false;
56 this->UseRelativePaths
= false;
57 this->Configured
= false;
58 this->EmitUniversalBinaryFlags
= true;
59 this->IsMakefileGenerator
= false;
60 this->RelativePathsConfigured
= false;
61 this->PathConversionsSetup
= false;
62 this->BackwardsCompatibility
= 0;
63 this->BackwardsCompatibilityFinal
= false;
66 cmLocalGenerator::~cmLocalGenerator()
68 delete this->Makefile
;
71 //----------------------------------------------------------------------------
72 class cmLocalGeneratorCurrent
74 cmGlobalGenerator
* GG
;
77 cmLocalGeneratorCurrent(cmLocalGenerator
* lg
)
79 this->GG
= lg
->GetGlobalGenerator();
80 this->LG
= this->GG
->GetCurrentLocalGenerator();
81 this->GG
->SetCurrentLocalGenerator(lg
);
83 ~cmLocalGeneratorCurrent()
85 this->GG
->SetCurrentLocalGenerator(this->LG
);
89 //----------------------------------------------------------------------------
90 void cmLocalGenerator::Configure()
92 // Manage the global generator's current local generator.
93 cmLocalGeneratorCurrent
clg(this);
94 static_cast<void>(clg
);
96 // make sure the CMakeFiles dir is there
97 std::string filesDir
= this->Makefile
->GetStartOutputDirectory();
98 filesDir
+= cmake::GetCMakeFilesDirectory();
99 cmSystemTools::MakeDirectory(filesDir
.c_str());
101 // find & read the list file
102 this->ReadInputFile();
104 // at the end of the ReadListFile handle any old style subdirs
105 // first get all the subdirectories
106 std::vector
<cmLocalGenerator
*> subdirs
= this->GetChildren();
108 // for each subdir recurse
109 std::vector
<cmLocalGenerator
*>::iterator sdi
= subdirs
.begin();
110 for (; sdi
!= subdirs
.end(); ++sdi
)
112 if (!(*sdi
)->Configured
)
114 this->Makefile
->ConfigureSubDirectory(*sdi
);
118 // Check whether relative paths should be used for optionally
120 this->UseRelativePaths
= this->Makefile
->IsOn("CMAKE_USE_RELATIVE_PATHS");
122 this->ComputeObjectMaxPath();
124 this->Configured
= true;
127 //----------------------------------------------------------------------------
128 void cmLocalGenerator::ComputeObjectMaxPath()
130 // Choose a maximum object file name length.
131 #if defined(_WIN32) || defined(__CYGWIN__)
132 this->ObjectPathMax
= 250;
134 this->ObjectPathMax
= 1000;
136 const char* plen
= this->Makefile
->GetDefinition("CMAKE_OBJECT_PATH_MAX");
140 if(sscanf(plen
, "%u", &pmax
) == 1)
144 this->ObjectPathMax
= pmax
;
149 w
<< "CMAKE_OBJECT_PATH_MAX is set to " << pmax
150 << ", which is less than the minimum of 128. "
151 << "The value will be ignored.";
152 this->Makefile
->IssueMessage(cmake::AUTHOR_WARNING
, w
.str());
158 w
<< "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
159 << "\", which fails to parse as a positive integer. "
160 << "The value will be ignored.";
161 this->Makefile
->IssueMessage(cmake::AUTHOR_WARNING
, w
.str());
164 this->ObjectMaxPathViolations
.clear();
167 //----------------------------------------------------------------------------
168 void cmLocalGenerator::ReadInputFile()
170 // Look for the CMakeLists.txt file.
171 std::string currentStart
= this->Makefile
->GetStartDirectory();
172 currentStart
+= "/CMakeLists.txt";
173 if(cmSystemTools::FileExists(currentStart
.c_str(), true))
175 this->Makefile
->ReadListFile(currentStart
.c_str());
184 // The file is missing. Check policy CMP0014.
185 cmMakefile
* mf
= this->Parent
->GetMakefile();
187 e
<< "The source directory\n"
188 << " " << this->Makefile
->GetStartDirectory() << "\n"
189 << "does not contain a CMakeLists.txt file.";
190 switch (mf
->GetPolicyStatus(cmPolicies::CMP0014
))
192 case cmPolicies::WARN
:
193 // Print the warning.
195 << "CMake does not support this case but it used "
196 << "to work accidentally and is being allowed for "
199 << mf
->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0014
);
200 mf
->IssueMessage(cmake::AUTHOR_WARNING
, e
.str());
201 case cmPolicies::OLD
:
202 // OLD behavior does not warn.
204 case cmPolicies::REQUIRED_IF_USED
:
205 case cmPolicies::REQUIRED_ALWAYS
:
207 << mf
->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0014
);
208 case cmPolicies::NEW
:
209 // NEW behavior prints the error.
210 mf
->IssueMessage(cmake::FATAL_ERROR
, e
.str());
215 void cmLocalGenerator::SetupPathConversions()
217 // Setup the current output directory components for use by
221 cmSystemTools::CollapseFullPath(this->Makefile
->GetHomeDirectory());
222 cmSystemTools::SplitPath(outdir
.c_str(), this->HomeDirectoryComponents
);
224 cmSystemTools::CollapseFullPath(this->Makefile
->GetStartDirectory());
225 cmSystemTools::SplitPath(outdir
.c_str(), this->StartDirectoryComponents
);
227 outdir
= cmSystemTools::CollapseFullPath
228 (this->Makefile
->GetHomeOutputDirectory());
229 cmSystemTools::SplitPath(outdir
.c_str(),
230 this->HomeOutputDirectoryComponents
);
232 outdir
= cmSystemTools::CollapseFullPath
233 (this->Makefile
->GetStartOutputDirectory());
234 cmSystemTools::SplitPath(outdir
.c_str(),
235 this->StartOutputDirectoryComponents
);
239 void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator
*gg
)
241 this->GlobalGenerator
= gg
;
242 this->Makefile
= new cmMakefile
;
243 this->Makefile
->SetLocalGenerator(this);
245 // setup the home directories
246 this->Makefile
->GetProperties().SetCMakeInstance(gg
->GetCMakeInstance());
247 this->Makefile
->SetHomeDirectory(
248 gg
->GetCMakeInstance()->GetHomeDirectory());
249 this->Makefile
->SetHomeOutputDirectory(
250 gg
->GetCMakeInstance()->GetHomeOutputDirectory());
253 void cmLocalGenerator::ConfigureFinalPass()
255 this->Makefile
->ConfigureFinalPass();
258 void cmLocalGenerator::TraceDependencies()
260 // Generate the rule files for each target.
261 cmTargets
& targets
= this->Makefile
->GetTargets();
262 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
264 const char* projectFilename
= 0;
265 if (this->IsMakefileGenerator
== false) // only use of this variable
267 projectFilename
= t
->second
.GetName();
269 t
->second
.TraceDependencies(projectFilename
);
273 void cmLocalGenerator::GenerateTestFiles()
275 if ( !this->Makefile
->IsOn("CMAKE_TESTING_ENABLED") )
280 // Compute the set of configurations.
281 std::vector
<std::string
> configurationTypes
;
282 if(const char* types
=
283 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
285 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
287 const char* config
= 0;
288 if(configurationTypes
.empty())
290 config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
293 std::string file
= this->Makefile
->GetStartOutputDirectory();
295 file
+= "CTestTestfile.cmake";
297 cmGeneratedFileStream
fout(file
.c_str());
298 fout
.SetCopyIfDifferent(true);
300 fout
<< "# CMake generated Testfile for " << std::endl
301 << "# Source directory: "
302 << this->Makefile
->GetStartDirectory() << std::endl
303 << "# Build directory: "
304 << this->Makefile
->GetStartOutputDirectory() << std::endl
306 << "# This file includes the relevent testing commands "
307 << "required for " << std::endl
308 << "# testing this directory and lists subdirectories to "
309 << "be tested as well." << std::endl
;
311 const char* testIncludeFile
=
312 this->Makefile
->GetProperty("TEST_INCLUDE_FILE");
313 if ( testIncludeFile
)
315 fout
<< "INCLUDE(\"" << testIncludeFile
<< "\")" << std::endl
;
318 // Ask each test generator to write its code.
319 std::vector
<cmTestGenerator
*> const&
320 testers
= this->Makefile
->GetTestGenerators();
321 for(std::vector
<cmTestGenerator
*>::const_iterator gi
= testers
.begin();
322 gi
!= testers
.end(); ++gi
)
324 (*gi
)->Generate(fout
, config
, configurationTypes
);
326 if ( this->Children
.size())
329 for(i
= 0; i
< this->Children
.size(); ++i
)
333 this->Children
[i
]->GetMakefile()->GetStartOutputDirectory();
334 fout
<< this->Convert(outP
.c_str(),START_OUTPUT
);
335 fout
<< ")" << std::endl
;
340 //----------------------------------------------------------------------------
341 void cmLocalGenerator::GenerateInstallRules()
343 // Compute the install prefix.
344 const char* prefix
= this->Makefile
->GetDefinition("CMAKE_INSTALL_PREFIX");
345 #if defined(_WIN32) && !defined(__CYGWIN__)
346 std::string prefix_win32
;
349 if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32
))
353 const char* project_name
= this->Makefile
->GetDefinition("PROJECT_NAME");
354 if(project_name
&& project_name
[0])
356 prefix_win32
+= "/Program Files/";
357 prefix_win32
+= project_name
;
361 prefix_win32
+= "/InstalledCMakeProject";
363 prefix
= prefix_win32
.c_str();
368 prefix
= "/usr/local";
372 // Compute the set of configurations.
373 std::vector
<std::string
> configurationTypes
;
374 if(const char* types
=
375 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
377 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
379 const char* config
= 0;
380 if(configurationTypes
.empty())
382 config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
385 // Choose a default install configuration.
386 const char* default_config
= config
;
387 const char* default_order
[] = {"RELEASE", "MINSIZEREL",
388 "RELWITHDEBINFO", "DEBUG", 0};
389 for(const char** c
= default_order
; *c
&& !default_config
; ++c
)
391 for(std::vector
<std::string
>::iterator i
= configurationTypes
.begin();
392 i
!= configurationTypes
.end(); ++i
)
394 if(cmSystemTools::UpperCase(*i
) == *c
)
396 default_config
= i
->c_str();
400 if(!default_config
&& !configurationTypes
.empty())
402 default_config
= configurationTypes
[0].c_str();
406 default_config
= "Release";
409 // Create the install script file.
410 std::string file
= this->Makefile
->GetStartOutputDirectory();
411 std::string homedir
= this->Makefile
->GetHomeOutputDirectory();
412 std::string currdir
= this->Makefile
->GetCurrentOutputDirectory();
413 cmSystemTools::ConvertToUnixSlashes(file
);
414 cmSystemTools::ConvertToUnixSlashes(homedir
);
415 cmSystemTools::ConvertToUnixSlashes(currdir
);
416 int toplevel_install
= 0;
417 if ( currdir
== homedir
)
419 toplevel_install
= 1;
421 file
+= "/cmake_install.cmake";
422 cmGeneratedFileStream
fout(file
.c_str());
423 fout
.SetCopyIfDifferent(true);
426 fout
<< "# Install script for directory: "
427 << this->Makefile
->GetCurrentDirectory() << std::endl
<< std::endl
;
428 fout
<< "# Set the install prefix" << std::endl
429 << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
430 << " SET(CMAKE_INSTALL_PREFIX \"" << prefix
<< "\")" << std::endl
431 << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
432 << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
433 << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
436 // Write support code for generating per-configuration install rules.
438 "# Set the install configuration name.\n"
439 "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
441 " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
442 " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
443 " ELSE(BUILD_TYPE)\n"
444 " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config
<< "\")\n"
445 " ENDIF(BUILD_TYPE)\n"
446 " MESSAGE(STATUS \"Install configuration: "
447 "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
448 "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
451 // Write support code for dealing with component-specific installs.
453 "# Set the component getting installed.\n"
454 "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
456 " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
457 " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
459 " SET(CMAKE_INSTALL_COMPONENT)\n"
460 " ENDIF(COMPONENT)\n"
461 "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
464 // Copy user-specified install options to the install code.
465 if(const char* so_no_exe
=
466 this->Makefile
->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
469 "# Install shared libraries without execute permission?\n"
470 "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
471 " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe
<< "\")\n"
472 "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
476 // Ask each install generator to write its code.
477 std::vector
<cmInstallGenerator
*> const& installers
=
478 this->Makefile
->GetInstallGenerators();
479 for(std::vector
<cmInstallGenerator
*>::const_iterator
480 gi
= installers
.begin();
481 gi
!= installers
.end(); ++gi
)
483 (*gi
)->Generate(fout
, config
, configurationTypes
);
486 // Write rules from old-style specification stored in targets.
487 this->GenerateTargetInstallRules(fout
, config
, configurationTypes
);
489 // Include install scripts from subdirectories.
490 if(!this->Children
.empty())
492 fout
<< "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
493 fout
<< " # Include the install script for each subdirectory.\n";
494 for(std::vector
<cmLocalGenerator
*>::const_iterator
495 ci
= this->Children
.begin(); ci
!= this->Children
.end(); ++ci
)
497 if(!(*ci
)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
499 std::string odir
= (*ci
)->GetMakefile()->GetStartOutputDirectory();
500 cmSystemTools::ConvertToUnixSlashes(odir
);
501 fout
<< " INCLUDE(\"" << odir
.c_str()
502 << "/cmake_install.cmake\")" << std::endl
;
506 fout
<< "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
509 // Record the install manifest.
510 if ( toplevel_install
)
513 "IF(CMAKE_INSTALL_COMPONENT)\n"
514 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
515 "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
516 "ELSE(CMAKE_INSTALL_COMPONENT)\n"
517 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
518 "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
521 << homedir
.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
522 << "\"\")" << std::endl
;
524 << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
526 << homedir
.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
527 << "\"${file}\\n\")" << std::endl
528 << "ENDFOREACH(file)" << std::endl
;
532 //----------------------------------------------------------------------------
533 void cmLocalGenerator::GenerateTargetManifest()
535 // Collect the set of configuration types.
536 std::vector
<std::string
> configNames
;
537 if(const char* configurationTypes
=
538 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
540 cmSystemTools::ExpandListArgument(configurationTypes
, configNames
);
542 else if(const char* buildType
=
543 this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE"))
547 configNames
.push_back(buildType
);
551 // Add our targets to the manifest for each configuration.
552 cmTargets
& targets
= this->Makefile
->GetTargets();
553 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
555 cmTarget
& target
= t
->second
;
556 if(configNames
.empty())
558 target
.GenerateTargetManifest(0);
562 for(std::vector
<std::string
>::iterator ci
= configNames
.begin();
563 ci
!= configNames
.end(); ++ci
)
565 const char* config
= ci
->c_str();
566 target
.GenerateTargetManifest(config
);
572 void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname
,
574 cmSourceFile
& source
,
577 std::string objectDir
= cmSystemTools::GetFilenamePath(std::string(ofname
));
578 objectDir
= this->Convert(objectDir
.c_str(),START_OUTPUT
,SHELL
);
579 std::string objectFile
= this->Convert(ofname
,START_OUTPUT
,SHELL
);
580 std::string sourceFile
=
581 this->Convert(source
.GetFullPath().c_str(),START_OUTPUT
,SHELL
,true);
582 std::string varString
= "CMAKE_";
584 varString
+= "_COMPILE_OBJECT";
585 std::vector
<std::string
> rules
;
586 rules
.push_back(this->Makefile
->GetRequiredDefinition(varString
.c_str()));
587 varString
= "CMAKE_";
589 varString
+= "_FLAGS";
591 flags
+= this->Makefile
->GetSafeDefinition(varString
.c_str());
593 flags
+= this->GetIncludeFlags(lang
);
595 // Construct the command lines.
596 cmCustomCommandLines commandLines
;
597 std::vector
<std::string
> commands
;
598 cmSystemTools::ExpandList(rules
, commands
);
599 cmLocalGenerator::RuleVariables vars
;
600 vars
.Language
= lang
;
601 vars
.Source
= sourceFile
.c_str();
602 vars
.Object
= objectFile
.c_str();
603 vars
.ObjectDir
= objectDir
.c_str();
604 vars
.Flags
= flags
.c_str();
605 for(std::vector
<std::string
>::iterator i
= commands
.begin();
606 i
!= commands
.end(); ++i
)
608 // Expand the full command line string.
609 this->ExpandRuleVariables(*i
, vars
);
611 // Parse the string to get the custom command line.
612 cmCustomCommandLine commandLine
;
613 std::vector
<cmStdString
> cmd
= cmSystemTools::ParseArguments(i
->c_str());
614 for(std::vector
<cmStdString
>::iterator a
= cmd
.begin();
617 commandLine
.push_back(*a
);
620 // Store this command line.
621 commandLines
.push_back(commandLine
);
624 // Check for extra object-file dependencies.
625 std::vector
<std::string
> depends
;
626 const char* additionalDeps
= source
.GetProperty("OBJECT_DEPENDS");
629 cmSystemTools::ExpandListArgument(additionalDeps
, depends
);
632 // Generate a meaningful comment for the command.
633 std::string comment
= "Building ";
635 comment
+= " object ";
636 comment
+= this->Convert(ofname
, START_OUTPUT
);
638 // Add the custom command to build the object file.
639 this->Makefile
->AddCustomCommandToOutput(
642 source
.GetFullPath().c_str(),
645 this->Makefile
->GetStartOutputDirectory()
649 void cmLocalGenerator::AddBuildTargetRule(const char* llang
, cmTarget
& target
)
652 std::vector
<std::string
> objVector
;
653 // Add all the sources outputs to the depends of the target
654 std::vector
<cmSourceFile
*> const& classes
= target
.GetSourceFiles();
655 for(std::vector
<cmSourceFile
*>::const_iterator i
= classes
.begin();
656 i
!= classes
.end(); ++i
)
658 cmSourceFile
* sf
= *i
;
659 if(!sf
->GetCustomCommand() &&
660 !sf
->GetPropertyAsBool("HEADER_FILE_ONLY") &&
661 !sf
->GetPropertyAsBool("EXTERNAL_OBJECT"))
664 dir_max
+= this->Makefile
->GetCurrentOutputDirectory();
666 std::string obj
= this->GetObjectFileNameWithoutTarget(*sf
, dir_max
);
669 std::string ofname
= this->Makefile
->GetCurrentOutputDirectory();
672 objVector
.push_back(ofname
);
673 this->AddCustomCommandToCreateObject(ofname
.c_str(),
674 llang
, *(*i
), target
);
675 objs
+= this->Convert(ofname
.c_str(),START_OUTPUT
,MAKEFILE
);
680 std::string createRule
= "CMAKE_";
682 createRule
+= target
.GetCreateRuleVariable();
683 std::string targetName
= target
.GetFullName();
688 std::string linkLibs
; // should be set
689 std::string flags
; // should be set
690 std::string linkFlags
; // should be set
691 this->GetTargetFlags(linkLibs
, flags
, linkFlags
, target
);
692 cmLocalGenerator::RuleVariables vars
;
693 vars
.Language
= llang
;
694 vars
.Objects
= objs
.c_str();
695 vars
.ObjectDir
= ".";
696 vars
.Target
= targetName
.c_str();
697 vars
.LinkLibraries
= linkLibs
.c_str();
698 vars
.Flags
= flags
.c_str();
699 vars
.LinkFlags
= linkFlags
.c_str();
701 std::string langFlags
;
702 this->AddLanguageFlags(langFlags
, llang
, 0);
703 vars
.LanguageCompileFlags
= langFlags
.c_str();
705 cmCustomCommandLines commandLines
;
706 std::vector
<std::string
> rules
;
707 rules
.push_back(this->Makefile
->GetRequiredDefinition(createRule
.c_str()));
708 std::vector
<std::string
> commands
;
709 cmSystemTools::ExpandList(rules
, commands
);
710 for(std::vector
<std::string
>::iterator i
= commands
.begin();
711 i
!= commands
.end(); ++i
)
713 // Expand the full command line string.
714 this->ExpandRuleVariables(*i
, vars
);
715 // Parse the string to get the custom command line.
716 cmCustomCommandLine commandLine
;
717 std::vector
<cmStdString
> cmd
= cmSystemTools::ParseArguments(i
->c_str());
718 for(std::vector
<cmStdString
>::iterator a
= cmd
.begin();
721 commandLine
.push_back(*a
);
724 // Store this command line.
725 commandLines
.push_back(commandLine
);
727 std::string targetFullPath
= target
.GetFullPath();
728 // Generate a meaningful comment for the command.
729 std::string comment
= "Linking ";
731 comment
+= " target ";
732 comment
+= this->Convert(targetFullPath
.c_str(), START_OUTPUT
);
733 this->Makefile
->AddCustomCommandToOutput(
734 targetFullPath
.c_str(),
739 this->Makefile
->GetStartOutputDirectory()
742 (this->Makefile
->GetSource(targetFullPath
.c_str()));
746 void cmLocalGenerator
747 ::CreateCustomTargetsAndCommands(std::set
<cmStdString
> const& lang
)
749 cmTargets
&tgts
= this->Makefile
->GetTargets();
750 for(cmTargets::iterator l
= tgts
.begin();
751 l
!= tgts
.end(); l
++)
753 cmTarget
& target
= l
->second
;
754 switch(target
.GetType())
756 case cmTarget::STATIC_LIBRARY
:
757 case cmTarget::SHARED_LIBRARY
:
758 case cmTarget::MODULE_LIBRARY
:
759 case cmTarget::EXECUTABLE
:
761 const char* llang
= target
.GetLinkerLanguage();
765 ("CMake can not determine linker language for target:",
769 // if the language is not in the set lang then create custom
770 // commands to build the target
771 if(lang
.count(llang
) == 0)
773 this->AddBuildTargetRule(llang
, target
);
783 // List of variables that are replaced when
784 // rules are expanced. These variables are
785 // replaced in the form <var> with GetSafeDefinition(var).
786 // ${LANG} is replaced in the variable first with all enabled
788 static const char* ruleReplaceVars
[] =
790 "CMAKE_${LANG}_COMPILER",
791 "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
792 "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
793 "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
794 "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
795 "CMAKE_${LANG}_LINK_FLAGS",
796 "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
797 "CMAKE_${LANG}_ARCHIVE",
799 "CMAKE_CURRENT_SOURCE_DIR",
800 "CMAKE_CURRENT_BINARY_DIR",
807 cmLocalGenerator::ExpandRuleVariable(std::string
const& variable
,
808 const RuleVariables
& replaceValues
)
810 if(replaceValues
.LinkFlags
)
812 if(variable
== "LINK_FLAGS")
814 return replaceValues
.LinkFlags
;
817 if(replaceValues
.Flags
)
819 if(variable
== "FLAGS")
821 return replaceValues
.Flags
;
825 if(replaceValues
.Source
)
827 if(variable
== "SOURCE")
829 return replaceValues
.Source
;
832 if(replaceValues
.PreprocessedSource
)
834 if(variable
== "PREPROCESSED_SOURCE")
836 return replaceValues
.PreprocessedSource
;
839 if(replaceValues
.AssemblySource
)
841 if(variable
== "ASSEMBLY_SOURCE")
843 return replaceValues
.AssemblySource
;
846 if(replaceValues
.Object
)
848 if(variable
== "OBJECT")
850 return replaceValues
.Object
;
853 if(replaceValues
.ObjectDir
)
855 if(variable
== "OBJECT_DIR")
857 return replaceValues
.ObjectDir
;
860 if(replaceValues
.Objects
)
862 if(variable
== "OBJECTS")
864 return replaceValues
.Objects
;
867 if(replaceValues
.ObjectsQuoted
)
869 if(variable
== "OBJECTS_QUOTED")
871 return replaceValues
.ObjectsQuoted
;
874 if(replaceValues
.Defines
&& variable
== "DEFINES")
876 return replaceValues
.Defines
;
878 if(replaceValues
.TargetPDB
)
880 if(variable
== "TARGET_PDB")
882 return replaceValues
.TargetPDB
;
886 if(replaceValues
.Target
)
888 if(variable
== "TARGET_QUOTED")
890 std::string targetQuoted
= replaceValues
.Target
;
891 if(targetQuoted
.size() && targetQuoted
[0] != '\"')
894 targetQuoted
+= replaceValues
.Target
;
895 targetQuoted
+= '\"';
899 if(replaceValues
.LanguageCompileFlags
)
901 if(variable
== "LANGUAGE_COMPILE_FLAGS")
903 return replaceValues
.LanguageCompileFlags
;
906 if(replaceValues
.Target
)
908 if(variable
== "TARGET")
910 return replaceValues
.Target
;
913 if(variable
== "TARGET_IMPLIB")
915 return this->TargetImplib
;
917 if(variable
== "TARGET_VERSION_MAJOR")
919 if(replaceValues
.TargetVersionMajor
)
921 return replaceValues
.TargetVersionMajor
;
928 if(variable
== "TARGET_VERSION_MINOR")
930 if(replaceValues
.TargetVersionMinor
)
932 return replaceValues
.TargetVersionMinor
;
939 if(replaceValues
.Target
)
941 if(variable
== "TARGET_BASE")
943 // Strip the last extension off the target name.
944 std::string targetBase
= replaceValues
.Target
;
945 std::string::size_type pos
= targetBase
.rfind(".");
946 if(pos
!= targetBase
.npos
)
948 return targetBase
.substr(0, pos
);
957 if(replaceValues
.TargetSOName
)
959 if(variable
== "TARGET_SONAME")
961 if(replaceValues
.Language
)
963 std::string name
= "CMAKE_SHARED_LIBRARY_SONAME_";
964 name
+= replaceValues
.Language
;
966 if(this->Makefile
->GetDefinition(name
.c_str()))
968 return replaceValues
.TargetSOName
;
974 if(replaceValues
.TargetInstallNameDir
)
976 if(variable
== "TARGET_INSTALLNAME_DIR")
978 return replaceValues
.TargetInstallNameDir
;
981 if(replaceValues
.LinkLibraries
)
983 if(variable
== "LINK_LIBRARIES")
985 return replaceValues
.LinkLibraries
;
988 if(replaceValues
.Language
)
990 if(variable
== "LANGUAGE")
992 return replaceValues
.Language
;
995 if(replaceValues
.CMTarget
)
997 if(variable
== "TARGET_NAME")
999 return replaceValues
.CMTarget
->GetName();
1001 if(variable
== "TARGET_TYPE")
1003 return cmTarget::TargetTypeNames
[replaceValues
.CMTarget
->GetType()];
1006 if(replaceValues
.Output
)
1008 if(variable
== "OUTPUT")
1010 return replaceValues
.Output
;
1013 if(variable
== "CMAKE_COMMAND")
1015 const char* cmcommand
=
1016 this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
1017 return this->Convert(cmcommand
, FULL
, SHELL
);
1019 std::vector
<std::string
> enabledLanguages
;
1020 this->GlobalGenerator
->GetEnabledLanguages(enabledLanguages
);
1021 // loop over language specific replace variables
1023 while(ruleReplaceVars
[pos
])
1025 for(std::vector
<std::string
>::iterator i
= enabledLanguages
.begin();
1026 i
!= enabledLanguages
.end(); ++i
)
1028 const char* lang
= i
->c_str();
1029 std::string actualReplace
= ruleReplaceVars
[pos
];
1030 // If this is the compiler then look for the extra variable
1031 // _COMPILER_ARG1 which must be the first argument to the compiler
1032 const char* compilerArg1
= 0;
1033 if(actualReplace
== "CMAKE_${LANG}_COMPILER")
1035 std::string arg1
= actualReplace
+ "_ARG1";
1036 cmSystemTools::ReplaceString(arg1
, "${LANG}", lang
);
1037 compilerArg1
= this->Makefile
->GetDefinition(arg1
.c_str());
1039 if(actualReplace
.find("${LANG}") != actualReplace
.npos
)
1041 cmSystemTools::ReplaceString(actualReplace
, "${LANG}", lang
);
1043 if(actualReplace
== variable
)
1045 std::string replace
=
1046 this->Makefile
->GetSafeDefinition(variable
.c_str());
1047 // if the variable is not a FLAG then treat it like a path
1048 if(variable
.find("_FLAG") == variable
.npos
)
1050 std::string ret
= this->ConvertToOutputForExisting(replace
.c_str());
1051 // if there is a required first argument to the compiler add it
1052 // to the compiler string
1056 ret
+= compilerArg1
;
1070 cmLocalGenerator::ExpandRuleVariables(std::string
& s
,
1071 const RuleVariables
& replaceValues
)
1073 std::vector
<std::string
> enabledLanguages
;
1074 this->GlobalGenerator
->GetEnabledLanguages(enabledLanguages
);
1075 this->InsertRuleLauncher(s
, replaceValues
.CMTarget
,
1076 replaceValues
.RuleLauncher
);
1077 std::string::size_type start
= s
.find('<');
1078 // no variables to expand
1083 std::string::size_type pos
= 0;
1084 std::string expandedInput
;
1085 while(start
!= s
.npos
&& start
< s
.size()-2)
1087 std::string::size_type end
= s
.find('>', start
);
1088 // if we find a < with no > we are done
1093 char c
= s
[start
+1];
1094 // if the next char after the < is not A-Za-z then
1095 // skip it and try to find the next < in the string
1098 start
= s
.find('<', start
+1);
1103 std::string var
= s
.substr(start
+1, end
- start
-1);
1104 std::string replace
= this->ExpandRuleVariable(var
,
1106 expandedInput
+= s
.substr(pos
, start
-pos
);
1107 expandedInput
+= replace
;
1109 start
= s
.find('<', start
+var
.size()+2);
1113 // add the rest of the input
1114 expandedInput
+= s
.substr(pos
, s
.size()-pos
);
1118 //----------------------------------------------------------------------------
1119 const char* cmLocalGenerator::GetRuleLauncher(cmTarget
* target
,
1124 return target
->GetProperty(prop
);
1128 return this->Makefile
->GetProperty(prop
);
1132 //----------------------------------------------------------------------------
1133 void cmLocalGenerator::InsertRuleLauncher(std::string
& s
, cmTarget
* target
,
1136 if(const char* val
= this->GetRuleLauncher(target
, prop
))
1138 cmOStringStream wrapped
;
1139 wrapped
<< val
<< " " << s
;
1144 //----------------------------------------------------------------------------
1146 cmLocalGenerator::ConvertToOutputForExistingCommon(const char* remote
,
1147 std::string
const& result
)
1149 // If this is a windows shell, the result has a space, and the path
1150 // already exists, we can use a short-path to reference it without a
1152 if(this->WindowsShell
&& result
.find(' ') != result
.npos
&&
1153 cmSystemTools::FileExists(remote
))
1156 if(cmSystemTools::GetShortPath(remote
, tmp
))
1158 return this->Convert(tmp
.c_str(), NONE
, SHELL
, true);
1162 // Otherwise, leave it unchanged.
1166 //----------------------------------------------------------------------------
1168 cmLocalGenerator::ConvertToOutputForExisting(const char* remote
,
1171 // Perform standard conversion.
1172 std::string result
= this->Convert(remote
, local
, SHELL
, true);
1174 // Consider short-path.
1175 return this->ConvertToOutputForExistingCommon(remote
, result
);
1178 //----------------------------------------------------------------------------
1180 cmLocalGenerator::ConvertToOutputForExisting(RelativeRoot remote
,
1183 // Perform standard conversion.
1184 std::string result
= this->Convert(remote
, local
, SHELL
, true);
1186 // Consider short-path.
1187 const char* remotePath
= this->GetRelativeRootPath(remote
);
1188 return this->ConvertToOutputForExistingCommon(remotePath
, result
);
1191 //----------------------------------------------------------------------------
1192 const char* cmLocalGenerator::GetIncludeFlags(const char* lang
)
1198 if(this->LanguageToIncludeFlags
.count(lang
))
1200 return this->LanguageToIncludeFlags
[lang
].c_str();
1203 cmOStringStream includeFlags
;
1204 std::vector
<std::string
> includes
;
1205 this->GetIncludeDirectories(includes
, lang
);
1206 std::vector
<std::string
>::iterator i
;
1208 std::string flagVar
= "CMAKE_INCLUDE_FLAG_";
1210 const char* includeFlag
=
1211 this->Makefile
->GetSafeDefinition(flagVar
.c_str());
1212 flagVar
= "CMAKE_INCLUDE_FLAG_SEP_";
1214 const char* sep
= this->Makefile
->GetDefinition(flagVar
.c_str());
1215 bool quotePaths
= false;
1216 if(this->Makefile
->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
1220 bool repeatFlag
= true;
1221 // should the include flag be repeated like ie. -IA -IB
1228 // if there is a separator then the flag is not repeated but is only
1229 // given once i.e. -classpath a:b:c
1233 // Support special system include flag if it is available and the
1234 // normal flag is repeated for each directory.
1235 std::string sysFlagVar
= "CMAKE_INCLUDE_SYSTEM_FLAG_";
1237 const char* sysIncludeFlag
= 0;
1240 sysIncludeFlag
= this->Makefile
->GetDefinition(sysFlagVar
.c_str());
1243 bool flagUsed
= false;
1244 std::set
<cmStdString
> emitted
;
1246 emitted
.insert("/System/Library/Frameworks");
1248 for(i
= includes
.begin(); i
!= includes
.end(); ++i
)
1251 if(cmSystemTools::IsPathToFramework(i
->c_str()))
1253 std::string frameworkDir
= *i
;
1254 frameworkDir
+= "/../";
1255 frameworkDir
= cmSystemTools::CollapseFullPath(frameworkDir
.c_str());
1256 if(emitted
.insert(frameworkDir
).second
)
1259 << "-F" << this->Convert(frameworkDir
.c_str(),
1260 cmLocalGenerator::START_OUTPUT
,
1261 cmLocalGenerator::SHELL
, true)
1267 std::string include
= *i
;
1268 if(!flagUsed
|| repeatFlag
)
1270 if(sysIncludeFlag
&&
1271 this->Makefile
->IsSystemIncludeDirectory(i
->c_str()))
1273 includeFlags
<< sysIncludeFlag
;
1277 includeFlags
<< includeFlag
;
1281 std::string includePath
= this->ConvertToOutputForExisting(i
->c_str());
1282 if(quotePaths
&& includePath
.size() && includePath
[0] != '\"')
1284 includeFlags
<< "\"";
1286 includeFlags
<< includePath
;
1287 if(quotePaths
&& includePath
.size() && includePath
[0] != '\"')
1289 includeFlags
<< "\"";
1291 includeFlags
<< sep
;
1293 std::string flags
= includeFlags
.str();
1294 // remove trailing separators
1295 if((sep
[0] != ' ') && flags
.size()>0 && flags
[flags
.size()-1] == sep
[0])
1297 flags
[flags
.size()-1] = ' ';
1299 std::string defineFlags
= this->Makefile
->GetDefineFlags();
1300 flags
+= defineFlags
;
1301 this->LanguageToIncludeFlags
[lang
] = flags
;
1303 // Use this temorary variable for the return value to work-around a
1304 // bogus GCC 2.95 warning.
1305 const char* ret
= this->LanguageToIncludeFlags
[lang
].c_str();
1309 //----------------------------------------------------------------------------
1310 void cmLocalGenerator::GetIncludeDirectories(std::vector
<std::string
>& dirs
,
1313 // Need to decide whether to automatically include the source and
1314 // binary directories at the beginning of the include path.
1315 bool includeSourceDir
= false;
1316 bool includeBinaryDir
= false;
1318 // When automatic include directories are requested for a build then
1319 // include the source and binary directories at the beginning of the
1320 // include path to approximate include file behavior for an
1321 // in-source build. This does not account for the case of a source
1322 // file in a subdirectory of the current source directory but we
1323 // cannot fix this because not all native build tools support
1324 // per-source-file include paths.
1325 if(this->Makefile
->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
1327 includeSourceDir
= true;
1328 includeBinaryDir
= true;
1331 // CMake versions below 2.0 would add the source tree to the -I path
1332 // automatically. Preserve compatibility.
1333 if(this->NeedBackwardsCompatibility(1,9))
1335 includeSourceDir
= true;
1338 // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
1339 // not set the backwards compatibility level automatically.
1340 const char* vtkSourceDir
=
1341 this->Makefile
->GetDefinition("VTK_SOURCE_DIR");
1344 const char* vtk_major
=
1345 this->Makefile
->GetDefinition("VTK_MAJOR_VERSION");
1346 const char* vtk_minor
=
1347 this->Makefile
->GetDefinition("VTK_MINOR_VERSION");
1348 vtk_major
= vtk_major
? vtk_major
: "4";
1349 vtk_minor
= vtk_minor
? vtk_minor
: "4";
1352 if(sscanf(vtk_major
, "%d", &vmajor
) &&
1353 sscanf(vtk_minor
, "%d", &vminor
) && vmajor
== 4 && vminor
<= 4)
1355 includeSourceDir
= true;
1359 // Do not repeat an include path.
1360 std::set
<cmStdString
> emitted
;
1362 // Store the automatic include paths.
1363 if(includeBinaryDir
)
1365 dirs
.push_back(this->Makefile
->GetStartOutputDirectory());
1366 emitted
.insert(this->Makefile
->GetStartOutputDirectory());
1368 if(includeSourceDir
)
1370 if(emitted
.find(this->Makefile
->GetStartDirectory()) == emitted
.end())
1372 dirs
.push_back(this->Makefile
->GetStartDirectory());
1373 emitted
.insert(this->Makefile
->GetStartDirectory());
1377 // Load implicit include directories for this language.
1378 std::string impDirVar
= "CMAKE_";
1380 impDirVar
+= "_IMPLICIT_INCLUDE_DIRECTORIES";
1381 if(const char* value
= this->Makefile
->GetDefinition(impDirVar
.c_str()))
1383 std::vector
<std::string
> impDirVec
;
1384 cmSystemTools::ExpandListArgument(value
, impDirVec
);
1385 for(std::vector
<std::string
>::const_iterator i
= impDirVec
.begin();
1386 i
!= impDirVec
.end(); ++i
)
1392 // Get the project-specified include directories.
1393 std::vector
<std::string
>& includes
=
1394 this->Makefile
->GetIncludeDirectories();
1396 // Support putting all the in-project include directories first if
1397 // it is requested by the project.
1398 if(this->Makefile
->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
1400 const char* topSourceDir
= this->Makefile
->GetHomeDirectory();
1401 const char* topBinaryDir
= this->Makefile
->GetHomeOutputDirectory();
1402 for(std::vector
<std::string
>::iterator i
= includes
.begin();
1403 i
!= includes
.end(); ++i
)
1405 // Emit this directory only if it is a subdirectory of the
1406 // top-level source or binary tree.
1407 if(cmSystemTools::ComparePath(i
->c_str(), topSourceDir
) ||
1408 cmSystemTools::ComparePath(i
->c_str(), topBinaryDir
) ||
1409 cmSystemTools::IsSubDirectory(i
->c_str(), topSourceDir
) ||
1410 cmSystemTools::IsSubDirectory(i
->c_str(), topBinaryDir
))
1412 if(emitted
.insert(*i
).second
)
1420 // Construct the final ordered include directory list.
1421 for(std::vector
<std::string
>::iterator i
= includes
.begin();
1422 i
!= includes
.end(); ++i
)
1424 if(emitted
.insert(*i
).second
)
1431 void cmLocalGenerator::GetTargetFlags(std::string
& linkLibs
,
1433 std::string
& linkFlags
,
1436 std::string buildType
=
1437 this->Makefile
->GetSafeDefinition("CMAKE_BUILD_TYPE");
1438 buildType
= cmSystemTools::UpperCase(buildType
);
1439 const char* libraryLinkVariable
=
1440 "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
1442 switch(target
.GetType())
1444 case cmTarget::STATIC_LIBRARY
:
1446 const char* targetLinkFlags
=
1447 target
.GetProperty("STATIC_LIBRARY_FLAGS");
1450 linkFlags
+= targetLinkFlags
;
1455 case cmTarget::MODULE_LIBRARY
:
1456 libraryLinkVariable
= "CMAKE_MODULE_LINKER_FLAGS";
1457 case cmTarget::SHARED_LIBRARY
:
1459 linkFlags
= this->Makefile
->GetSafeDefinition(libraryLinkVariable
);
1461 if(buildType
.size())
1463 std::string build
= libraryLinkVariable
;
1466 linkFlags
+= this->Makefile
->GetSafeDefinition(build
.c_str());
1469 if(this->Makefile
->IsOn("WIN32") &&
1470 !(this->Makefile
->IsOn("CYGWIN") || this->Makefile
->IsOn("MINGW")))
1472 const std::vector
<cmSourceFile
*>& sources
= target
.GetSourceFiles();
1473 for(std::vector
<cmSourceFile
*>::const_iterator i
= sources
.begin();
1474 i
!= sources
.end(); ++i
)
1476 cmSourceFile
* sf
= *i
;
1477 if(sf
->GetExtension() == "def")
1480 this->Makefile
->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
1481 linkFlags
+= this->Convert(sf
->GetFullPath().c_str(),
1482 START_OUTPUT
, SHELL
);
1487 const char* targetLinkFlags
= target
.GetProperty("LINK_FLAGS");
1490 linkFlags
+= targetLinkFlags
;
1492 std::string configLinkFlags
= targetLinkFlags
;
1493 configLinkFlags
+= buildType
;
1494 targetLinkFlags
= target
.GetProperty(configLinkFlags
.c_str());
1497 linkFlags
+= targetLinkFlags
;
1501 cmOStringStream linklibsStr
;
1502 this->OutputLinkLibraries(linklibsStr
, target
, false);
1503 linkLibs
= linklibsStr
.str();
1506 case cmTarget::EXECUTABLE
:
1509 this->Makefile
->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
1511 if(buildType
.size())
1513 std::string build
= "CMAKE_EXE_LINKER_FLAGS_";
1515 linkFlags
+= this->Makefile
->GetSafeDefinition(build
.c_str());
1518 const char* linkLanguage
= target
.GetLinkerLanguage();
1521 cmSystemTools::Error
1522 ("CMake can not determine linker language for target:",
1526 std::string langVar
= "CMAKE_";
1527 langVar
+= linkLanguage
;
1528 std::string flagsVar
= langVar
+ "_FLAGS";
1529 std::string sharedFlagsVar
= "CMAKE_SHARED_LIBRARY_";
1530 sharedFlagsVar
+= linkLanguage
;
1531 sharedFlagsVar
+= "_FLAGS";
1532 flags
+= this->Makefile
->GetSafeDefinition(flagsVar
.c_str());
1534 flags
+= this->Makefile
->GetSafeDefinition(sharedFlagsVar
.c_str());
1536 cmOStringStream linklibs
;
1537 this->OutputLinkLibraries(linklibs
, target
, false);
1538 linkLibs
= linklibs
.str();
1539 if(cmSystemTools::IsOn
1540 (this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
1542 std::string sFlagVar
= std::string("CMAKE_SHARED_BUILD_")
1543 + linkLanguage
+ std::string("_FLAGS");
1544 linkFlags
+= this->Makefile
->GetSafeDefinition(sFlagVar
.c_str());
1547 if ( target
.GetPropertyAsBool("WIN32_EXECUTABLE") )
1550 this->Makefile
->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
1556 this->Makefile
->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
1559 const char* targetLinkFlags
= target
.GetProperty("LINK_FLAGS");
1562 linkFlags
+= targetLinkFlags
;
1564 std::string configLinkFlags
= targetLinkFlags
;
1565 configLinkFlags
+= buildType
;
1566 targetLinkFlags
= target
.GetProperty(configLinkFlags
.c_str());
1569 linkFlags
+= targetLinkFlags
;
1580 std::string
cmLocalGenerator::ConvertToLinkReference(std::string
const& lib
)
1582 #if defined(_WIN32) && !defined(__CYGWIN__)
1583 // Work-ardound command line parsing limitations in MSVC 6.0 and
1585 if(this->Makefile
->IsOn("MSVC60") || this->Makefile
->IsOn("WATCOM"))
1587 // Search for the last space.
1588 std::string::size_type pos
= lib
.rfind(' ');
1591 // Find the slash after the last space, if any.
1592 pos
= lib
.find('/', pos
);
1594 // Convert the portion of the path with a space to a short path.
1596 if(cmSystemTools::GetShortPath(lib
.substr(0, pos
).c_str(), sp
))
1598 // Append the rest of the path with no space.
1599 sp
+= lib
.substr(pos
);
1601 // Convert to an output path.
1602 return this->Convert(sp
.c_str(), NONE
, SHELL
);
1609 return this->Convert(lib
.c_str(), START_OUTPUT
, SHELL
);
1613 * Output the linking rules on a command line. For executables,
1614 * targetLibrary should be a NULL pointer. For libraries, it should point
1615 * to the name of the library. This will not link a library against itself.
1617 void cmLocalGenerator::OutputLinkLibraries(std::ostream
& fout
,
1621 const char* config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
1622 cmComputeLinkInformation
* pcli
= tgt
.GetLinkInformation(config
);
1627 cmComputeLinkInformation
& cli
= *pcli
;
1629 // Collect library linking flags command line options.
1630 std::string linkLibs
;
1632 const char* linkLanguage
= cli
.GetLinkLanguage();
1634 std::string libPathFlag
=
1635 this->Makefile
->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1636 std::string libPathTerminator
=
1637 this->Makefile
->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1639 // Flags to link an executable to shared libraries.
1640 std::string linkFlagsVar
= "CMAKE_SHARED_LIBRARY_LINK_";
1641 linkFlagsVar
+= linkLanguage
;
1642 linkFlagsVar
+= "_FLAGS";
1643 if( tgt
.GetType() == cmTarget::EXECUTABLE
)
1645 linkLibs
= this->Makefile
->GetSafeDefinition(linkFlagsVar
.c_str());
1649 // Append the framework search path flags.
1650 std::vector
<std::string
> const& fwDirs
= cli
.GetFrameworkPaths();
1651 for(std::vector
<std::string
>::const_iterator fdi
= fwDirs
.begin();
1652 fdi
!= fwDirs
.end(); ++fdi
)
1655 linkLibs
+= this->Convert(fdi
->c_str(), NONE
, SHELL
, false);
1659 // Append the library search path flags.
1660 std::vector
<std::string
> const& libDirs
= cli
.GetDirectories();
1661 for(std::vector
<std::string
>::const_iterator libDir
= libDirs
.begin();
1662 libDir
!= libDirs
.end(); ++libDir
)
1664 std::string libpath
= this->ConvertToOutputForExisting(libDir
->c_str());
1665 linkLibs
+= libPathFlag
;
1666 linkLibs
+= libpath
;
1667 linkLibs
+= libPathTerminator
;
1671 // Append the link items.
1672 typedef cmComputeLinkInformation::ItemVector ItemVector
;
1673 ItemVector
const& items
= cli
.GetItems();
1674 for(ItemVector::const_iterator li
= items
.begin(); li
!= items
.end(); ++li
)
1678 linkLibs
+= this->ConvertToLinkReference(li
->Value
);
1682 linkLibs
+= li
->Value
;
1687 // Write the library flags to the build rule.
1690 // Get the RPATH entries.
1691 std::vector
<std::string
> runtimeDirs
;
1692 cli
.GetRPath(runtimeDirs
, relink
);
1694 // Check what kind of rpath flags to use.
1695 if(cli
.GetRuntimeSep().empty())
1697 // Each rpath entry gets its own option ("-R a -R b -R c")
1699 for(std::vector
<std::string
>::iterator ri
= runtimeDirs
.begin();
1700 ri
!= runtimeDirs
.end(); ++ri
)
1702 rpath
+= cli
.GetRuntimeFlag();
1703 rpath
+= this->Convert(ri
->c_str(), NONE
, SHELL
, false);
1710 // All rpath entries are combined ("-Wl,-rpath,a:b:c").
1711 std::string rpath
= cli
.GetRPathString(relink
);
1713 // Store the rpath option in the stream.
1716 fout
<< cli
.GetRuntimeFlag();
1717 fout
<< this->EscapeForShell(rpath
.c_str(), true);
1722 // Add the linker runtime search path if any.
1723 std::string rpath_link
= cli
.GetRPathLinkString();
1724 if(!cli
.GetRPathLinkFlag().empty() && !rpath_link
.empty())
1726 fout
<< cli
.GetRPathLinkFlag();
1727 fout
<< this->EscapeForShell(rpath_link
.c_str(), true);
1731 // Add standard libraries for this language.
1732 std::string standardLibsVar
= "CMAKE_";
1733 standardLibsVar
+= cli
.GetLinkLanguage();
1734 standardLibsVar
+= "_STANDARD_LIBRARIES";
1735 if(const char* stdLibs
=
1736 this->Makefile
->GetDefinition(standardLibsVar
.c_str()))
1738 fout
<< stdLibs
<< " ";
1742 //----------------------------------------------------------------------------
1743 void cmLocalGenerator::AddLanguageFlags(std::string
& flags
,
1747 // Add language-specific flags.
1748 std::string flagsVar
= "CMAKE_";
1750 flagsVar
+= "_FLAGS";
1751 // Add special OSX flags
1753 if(this->EmitUniversalBinaryFlags
)
1755 const char* osxArch
=
1756 this->Makefile
->GetDefinition("CMAKE_OSX_ARCHITECTURES");
1757 const char* sysroot
=
1758 this->Makefile
->GetDefinition("CMAKE_OSX_SYSROOT");
1759 const char* sysrootDefault
=
1760 this->Makefile
->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
1761 const char* deploymentTarget
=
1762 this->Makefile
->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
1763 std::string isysrootVar
= std::string("CMAKE_") + lang
+ "_HAS_ISYSROOT";
1764 bool hasIsysroot
= this->Makefile
->IsOn(isysrootVar
.c_str());
1765 bool flagsUsed
= false;
1766 if(osxArch
&& sysroot
&& lang
&& (lang
[0] =='C' || lang
[0] == 'F'))
1768 std::vector
<std::string
> archs
;
1769 cmSystemTools::ExpandListArgument(std::string(osxArch
),
1771 bool addArchFlag
= false;
1772 if(archs
.size() >= 1)
1779 // if there is more than one arch add the -arch and
1780 // -isysroot flags, or if there is one arch flag, but
1781 // it is not the default -arch flag for the system, then
1782 // add it. Otherwize do not add -arch and -isysroot
1785 for( std::vector
<std::string
>::iterator i
= archs
.begin();
1786 i
!= archs
.end(); ++i
)
1793 flags
+= " -isysroot ";
1800 if(!flagsUsed
&& sysroot
&& sysrootDefault
&&
1801 strcmp(sysroot
, sysrootDefault
) != 0 && hasIsysroot
)
1803 flags
+= " -isysroot ";
1807 if (deploymentTarget
&& *deploymentTarget
&&
1808 lang
&& (lang
[0] =='C' || lang
[0] == 'F'))
1810 flags
+= " -mmacosx-version-min=";
1811 flags
+= deploymentTarget
;
1815 this->AddConfigVariableFlags(flags
, flagsVar
.c_str(), config
);
1818 //----------------------------------------------------------------------------
1819 std::string
cmLocalGenerator::GetRealDependency(const char* inName
,
1822 // Older CMake code may specify the dependency using the target
1823 // output file rather than the target name. Such code would have
1824 // been written before there was support for target properties that
1825 // modify the name so stripping down to just the file name should
1826 // produce the target name in this case.
1827 std::string name
= cmSystemTools::GetFilenameName(inName
);
1828 if(cmSystemTools::GetFilenameLastExtension(name
) == ".exe")
1830 name
= cmSystemTools::GetFilenameWithoutLastExtension(name
);
1833 // Look for a CMake target with the given name.
1834 if(cmTarget
* target
= this->Makefile
->FindTargetToUse(name
.c_str()))
1836 // make sure it is not just a coincidence that the target name
1837 // found is part of the inName
1838 if(cmSystemTools::FileIsFullPath(inName
))
1840 std::string tLocation
= target
->GetLocation(config
);
1841 tLocation
= cmSystemTools::GetFilenamePath(tLocation
);
1842 std::string depLocation
= cmSystemTools::GetFilenamePath(
1843 std::string(inName
));
1844 depLocation
= cmSystemTools::CollapseFullPath(depLocation
.c_str());
1845 tLocation
= cmSystemTools::CollapseFullPath(tLocation
.c_str());
1846 if(depLocation
!= tLocation
)
1848 // it is a full path to a depend that has the same name
1849 // as a target but is in a different location so do not use
1850 // the target as the depend
1854 switch (target
->GetType())
1856 case cmTarget::EXECUTABLE
:
1857 case cmTarget::STATIC_LIBRARY
:
1858 case cmTarget::SHARED_LIBRARY
:
1859 case cmTarget::MODULE_LIBRARY
:
1860 case cmTarget::UNKNOWN_LIBRARY
:
1862 // Get the location of the target's output file and depend on it.
1863 if(const char* location
= target
->GetLocation(config
))
1869 case cmTarget::UTILITY
:
1870 case cmTarget::GLOBAL_TARGET
:
1871 // Depending on a utility target may not work but just trust
1872 // the user to have given a valid name.
1874 case cmTarget::INSTALL_FILES
:
1875 case cmTarget::INSTALL_PROGRAMS
:
1876 case cmTarget::INSTALL_DIRECTORY
:
1881 // The name was not that of a CMake target. It must name a file.
1882 if(cmSystemTools::FileIsFullPath(inName
))
1884 // This is a full path. Return it as given.
1888 // Check for a source file in this directory that matches the
1890 if(cmSourceFile
* sf
= this->Makefile
->GetSource(inName
))
1892 name
= sf
->GetFullPath();
1896 // Treat the name as relative to the source directory in which it
1898 name
= this->Makefile
->GetCurrentDirectory();
1904 //----------------------------------------------------------------------------
1905 std::string
cmLocalGenerator::GetRealLocation(const char* inName
,
1908 std::string outName
=inName
;
1909 // Look for a CMake target with the given name, which is an executable
1910 // and which can be run
1911 cmTarget
* target
= this->Makefile
->FindTargetToUse(inName
);
1913 && (target
->GetType() == cmTarget::EXECUTABLE
)
1914 && ((this->Makefile
->IsOn("CMAKE_CROSSCOMPILING") == false)
1915 || (target
->IsImported() == true)))
1917 outName
= target
->GetLocation( config
);
1922 //----------------------------------------------------------------------------
1923 void cmLocalGenerator::AddSharedFlags(std::string
& flags
,
1927 std::string flagsVar
;
1929 // Add flags for dealing with shared libraries for this language.
1932 flagsVar
= "CMAKE_SHARED_LIBRARY_";
1934 flagsVar
+= "_FLAGS";
1935 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1938 // Add flags specific to shared builds.
1939 if(cmSystemTools::IsOn(this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
1941 flagsVar
= "CMAKE_SHARED_BUILD_";
1943 flagsVar
+= "_FLAGS";
1944 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1948 //----------------------------------------------------------------------------
1949 void cmLocalGenerator::AddConfigVariableFlags(std::string
& flags
,
1953 // Add the flags from the variable itself.
1954 std::string flagsVar
= var
;
1955 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1956 // Add the flags from the build-type specific variable.
1957 if(config
&& *config
)
1960 flagsVar
+= cmSystemTools::UpperCase(config
);
1961 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1965 //----------------------------------------------------------------------------
1966 void cmLocalGenerator::AppendFlags(std::string
& flags
,
1967 const char* newFlags
)
1969 if(newFlags
&& *newFlags
)
1971 std::string newf
= newFlags
;
1980 //----------------------------------------------------------------------------
1981 void cmLocalGenerator::AppendDefines(std::string
& defines
,
1982 const char* defines_list
,
1985 // Short-circuit if there are no definitions.
1991 // Expand the list of definitions.
1992 std::vector
<std::string
> defines_vec
;
1993 cmSystemTools::ExpandListArgument(defines_list
, defines_vec
);
1995 // Short-circuit if there are no definitions.
1996 if(defines_vec
.empty())
2001 // Lookup the define flag for the current language.
2002 std::string dflag
= "-D";
2005 std::string defineFlagVar
= "CMAKE_";
2006 defineFlagVar
+= lang
;
2007 defineFlagVar
+= "_DEFINE_FLAG";
2008 const char* df
= this->Makefile
->GetDefinition(defineFlagVar
.c_str());
2015 // Add each definition to the command line with appropriate escapes.
2016 const char* dsep
= defines
.empty()? "" : " ";
2017 for(std::vector
<std::string
>::const_iterator di
= defines_vec
.begin();
2018 di
!= defines_vec
.end(); ++di
)
2020 // Skip unsupported definitions.
2021 if(!this->CheckDefinition(*di
))
2026 // Separate from previous definitions.
2030 // Append the definition with proper escaping.
2032 if(this->WatcomWMake
)
2034 // The Watcom compiler does its own command line parsing instead
2035 // of using the windows shell rules. Definitions are one of
2037 // -DNAME=<cpp-token>
2038 // -DNAME="c-string with spaces and other characters(?@#$)"
2040 // Watcom will properly parse each of these cases from the
2041 // command line without any escapes. However we still have to
2042 // get the '$' and '#' characters through WMake as '$$' and
2044 for(const char* c
= di
->c_str(); *c
; ++c
)
2046 if(*c
== '$' || *c
== '#')
2055 // Make the definition appear properly on the command line. Use
2056 // -DNAME="value" instead of -D"NAME=value" to help VS6 parser.
2057 std::string::size_type eq
= di
->find("=");
2058 defines
+= di
->substr(0, eq
);
2062 defines
+= this->EscapeForShell(di
->c_str() + eq
+ 1, true);
2068 //----------------------------------------------------------------------------
2070 cmLocalGenerator::ConstructComment(const cmCustomCommand
& cc
,
2071 const char* default_comment
)
2073 // Check for a comment provided with the command.
2076 return cc
.GetComment();
2079 // Construct a reasonable default comment if possible.
2080 if(!cc
.GetOutputs().empty())
2082 std::string comment
;
2083 comment
= "Generating ";
2084 const char* sep
= "";
2085 for(std::vector
<std::string
>::const_iterator o
= cc
.GetOutputs().begin();
2086 o
!= cc
.GetOutputs().end(); ++o
)
2089 comment
+= this->Convert(o
->c_str(), cmLocalGenerator::START_OUTPUT
);
2095 // Otherwise use the provided default.
2096 return default_comment
;
2099 //----------------------------------------------------------------------------
2101 cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote
)
2103 return this->Convert(remote
, START_OUTPUT
, SHELL
, true);
2106 //----------------------------------------------------------------------------
2107 const char* cmLocalGenerator::GetRelativeRootPath(RelativeRoot relroot
)
2111 case HOME
: return this->Makefile
->GetHomeDirectory();
2112 case START
: return this->Makefile
->GetStartDirectory();
2113 case HOME_OUTPUT
: return this->Makefile
->GetHomeOutputDirectory();
2114 case START_OUTPUT
: return this->Makefile
->GetStartOutputDirectory();
2120 //----------------------------------------------------------------------------
2121 std::string
cmLocalGenerator::Convert(const char* source
,
2122 RelativeRoot relative
,
2123 OutputFormat output
,
2126 // Make sure the relative path conversion components are set.
2127 if(!this->PathConversionsSetup
)
2129 this->SetupPathConversions();
2130 this->PathConversionsSetup
= true;
2133 // Convert the path to a relative path.
2134 std::string result
= source
;
2136 if (!optional
|| this->UseRelativePaths
)
2141 //result = cmSystemTools::CollapseFullPath(result.c_str());
2142 result
= this->ConvertToRelativePath(this->HomeDirectoryComponents
,
2146 //result = cmSystemTools::CollapseFullPath(result.c_str());
2147 result
= this->ConvertToRelativePath(this->StartDirectoryComponents
,
2151 //result = cmSystemTools::CollapseFullPath(result.c_str());
2153 this->ConvertToRelativePath(this->HomeOutputDirectoryComponents
,
2157 //result = cmSystemTools::CollapseFullPath(result.c_str());
2159 this->ConvertToRelativePath(this->StartOutputDirectoryComponents
,
2163 result
= cmSystemTools::CollapseFullPath(result
.c_str());
2169 return this->ConvertToOutputFormat(result
.c_str(), output
);
2172 //----------------------------------------------------------------------------
2173 std::string
cmLocalGenerator::ConvertToOutputFormat(const char* source
,
2174 OutputFormat output
)
2176 std::string result
= source
;
2177 // Convert it to an output path.
2178 if (output
== MAKEFILE
)
2180 result
= cmSystemTools::ConvertToOutputPath(result
.c_str());
2182 else if( output
== SHELL
)
2184 // For the MSYS shell convert drive letters to posix paths, so
2185 // that c:/some/path becomes /c/some/path. This is needed to
2186 // avoid problems with the shell path translation.
2187 if(this->MSYSShell
&& !this->LinkScriptShell
)
2189 if(result
.size() > 2 && result
[1] == ':')
2191 result
[1] = result
[0];
2195 if(this->WindowsShell
)
2197 std::string::size_type pos
= 0;
2198 while((pos
= result
.find('/', pos
)) != std::string::npos
)
2204 result
= this->EscapeForShell(result
.c_str(), true, false);
2209 //----------------------------------------------------------------------------
2210 std::string
cmLocalGenerator::Convert(RelativeRoot remote
,
2212 OutputFormat output
,
2215 const char* remotePath
= this->GetRelativeRootPath(remote
);
2217 // The relative root must have a path (i.e. not FULL or NONE)
2218 assert(remotePath
!= 0);
2220 if(local
&& (!optional
|| this->UseRelativePaths
))
2222 std::vector
<std::string
> components
;
2223 cmSystemTools::SplitPath(local
, components
);
2224 std::string result
= this->ConvertToRelativePath(components
, remotePath
);
2225 return this->ConvertToOutputFormat(result
.c_str(), output
);
2229 return this->ConvertToOutputFormat(remotePath
, output
);
2233 //----------------------------------------------------------------------------
2234 std::string
cmLocalGenerator::FindRelativePathTopSource()
2236 // Relative path conversion within a single tree managed by CMake is
2237 // safe. We can use our parent relative path top if and only if
2238 // this is a subdirectory of that top.
2239 if(cmLocalGenerator
* parent
= this->GetParent())
2241 std::string parentTop
= parent
->FindRelativePathTopSource();
2242 if(cmSystemTools::IsSubDirectory(
2243 this->Makefile
->GetStartDirectory(), parentTop
.c_str()))
2249 // Otherwise this directory itself is the new top.
2250 return this->Makefile
->GetStartDirectory();
2253 //----------------------------------------------------------------------------
2254 std::string
cmLocalGenerator::FindRelativePathTopBinary()
2256 // Relative path conversion within a single tree managed by CMake is
2257 // safe. We can use our parent relative path top if and only if
2258 // this is a subdirectory of that top.
2259 if(cmLocalGenerator
* parent
= this->GetParent())
2261 std::string parentTop
= parent
->FindRelativePathTopBinary();
2262 if(cmSystemTools::IsSubDirectory(
2263 this->Makefile
->GetStartOutputDirectory(), parentTop
.c_str()))
2269 // Otherwise this directory itself is the new top.
2270 return this->Makefile
->GetStartOutputDirectory();
2273 //----------------------------------------------------------------------------
2274 void cmLocalGenerator::ConfigureRelativePaths()
2276 // Relative path conversion inside the source tree is not used to
2277 // construct relative paths passed to build tools so it is safe to
2278 // even when the source is a network path.
2279 std::string source
= this->FindRelativePathTopSource();
2280 this->RelativePathTopSource
= source
;
2282 // The current working directory on Windows cannot be a network
2283 // path. Therefore relative paths cannot work when the binary tree
2284 // is a network path.
2285 std::string binary
= this->FindRelativePathTopBinary();
2286 if(binary
.size() < 2 || binary
.substr(0, 2) != "//")
2288 this->RelativePathTopBinary
= binary
;
2292 this->RelativePathTopBinary
= "";
2296 //----------------------------------------------------------------------------
2297 static bool cmLocalGeneratorNotAbove(const char* a
, const char* b
)
2299 return (cmSystemTools::ComparePath(a
, b
) ||
2300 cmSystemTools::IsSubDirectory(a
, b
));
2303 //----------------------------------------------------------------------------
2305 cmLocalGenerator::ConvertToRelativePath(const std::vector
<std::string
>& local
,
2306 const char* in_remote
, bool force
)
2308 // The path should never be quoted.
2309 assert(in_remote
[0] != '\"');
2311 // The local path should never have a trailing slash.
2312 assert(local
.size() > 0 && !(local
[local
.size()-1] == ""));
2314 // If the path is already relative then just return the path.
2315 if(!cmSystemTools::FileIsFullPath(in_remote
))
2320 // Make sure relative path conversion is configured.
2321 if(!this->RelativePathsConfigured
)
2323 this->ConfigureRelativePaths();
2324 this->RelativePathsConfigured
= true;
2329 // Skip conversion if the path and local are not both in the source
2330 // or both in the binary tree.
2331 std::string local_path
= cmSystemTools::JoinPath(local
);
2332 if(!((cmLocalGeneratorNotAbove(local_path
.c_str(),
2333 this->RelativePathTopBinary
.c_str()) &&
2334 cmLocalGeneratorNotAbove(in_remote
,
2335 this->RelativePathTopBinary
.c_str())) ||
2336 (cmLocalGeneratorNotAbove(local_path
.c_str(),
2337 this->RelativePathTopSource
.c_str()) &&
2338 cmLocalGeneratorNotAbove(in_remote
,
2339 this->RelativePathTopSource
.c_str()))))
2345 // Identify the longest shared path component between the remote
2346 // path and the local path.
2347 std::vector
<std::string
> remote
;
2348 cmSystemTools::SplitPath(in_remote
, remote
);
2349 unsigned int common
=0;
2350 while(common
< remote
.size() &&
2351 common
< local
.size() &&
2352 cmSystemTools::ComparePath(remote
[common
].c_str(),
2353 local
[common
].c_str()))
2358 // If no part of the path is in common then return the full path.
2364 // If the entire path is in common then just return a ".".
2365 if(common
== remote
.size() &&
2366 common
== local
.size())
2371 // If the entire path is in common except for a trailing slash then
2372 // just return a "./".
2373 if(common
+1 == remote
.size() &&
2374 remote
[common
].size() == 0 &&
2375 common
== local
.size())
2380 // Construct the relative path.
2381 std::string relative
;
2383 // First add enough ../ to get up to the level of the shared portion
2384 // of the path. Leave off the trailing slash. Note that the last
2385 // component of local will never be empty because local should never
2386 // have a trailing slash.
2387 for(unsigned int i
=common
; i
< local
.size(); ++i
)
2390 if(i
< local
.size()-1)
2396 // Now add the portion of the destination path that is not included
2397 // in the shared portion of the path. Add a slash the first time
2398 // only if there was already something in the path. If there was a
2399 // trailing slash in the input then the last iteration of the loop
2400 // will add a slash followed by an empty string which will preserve
2401 // the trailing slash in the output.
2402 for(unsigned int i
=common
; i
< remote
.size(); ++i
)
2404 if(relative
.size() > 0)
2408 relative
+= remote
[i
];
2411 // Finally return the path.
2415 //----------------------------------------------------------------------------
2418 ::GenerateTargetInstallRules(
2419 std::ostream
& os
, const char* config
,
2420 std::vector
<std::string
> const& configurationTypes
)
2422 // Convert the old-style install specification from each target to
2423 // an install generator and run it.
2424 cmTargets
& tgts
= this->Makefile
->GetTargets();
2425 for(cmTargets::iterator l
= tgts
.begin(); l
!= tgts
.end(); ++l
)
2427 // Include the user-specified pre-install script for this target.
2428 if(const char* preinstall
= l
->second
.GetProperty("PRE_INSTALL_SCRIPT"))
2430 cmInstallScriptGenerator
g(preinstall
, false, 0);
2431 g
.Generate(os
, config
, configurationTypes
);
2434 // Install this target if a destination is given.
2435 if(l
->second
.GetInstallPath() != "")
2437 // Compute the full install destination. Note that converting
2438 // to unix slashes also removes any trailing slash.
2439 // We also skip over the leading slash given by the user.
2440 std::string destination
= l
->second
.GetInstallPath().substr(1);
2441 cmSystemTools::ConvertToUnixSlashes(destination
);
2442 if(destination
.empty())
2447 // Generate the proper install generator for this target type.
2448 switch(l
->second
.GetType())
2450 case cmTarget::EXECUTABLE
:
2451 case cmTarget::STATIC_LIBRARY
:
2452 case cmTarget::MODULE_LIBRARY
:
2454 // Use a target install generator.
2455 cmInstallTargetGenerator
g(l
->second
, destination
.c_str(), false);
2456 g
.Generate(os
, config
, configurationTypes
);
2459 case cmTarget::SHARED_LIBRARY
:
2461 #if defined(_WIN32) || defined(__CYGWIN__)
2462 // Special code to handle DLL. Install the import library
2463 // to the normal destination and the DLL to the runtime
2465 cmInstallTargetGenerator
g1(l
->second
, destination
.c_str(), true);
2466 g1
.Generate(os
, config
, configurationTypes
);
2467 // We also skip over the leading slash given by the user.
2468 destination
= l
->second
.GetRuntimeInstallPath().substr(1);
2469 cmSystemTools::ConvertToUnixSlashes(destination
);
2470 cmInstallTargetGenerator
g2(l
->second
, destination
.c_str(), false);
2471 g2
.Generate(os
, config
, configurationTypes
);
2473 // Use a target install generator.
2474 cmInstallTargetGenerator
g(l
->second
, destination
.c_str(), false);
2475 g
.Generate(os
, config
, configurationTypes
);
2484 // Include the user-specified post-install script for this target.
2485 if(const char* postinstall
= l
->second
.GetProperty("POST_INSTALL_SCRIPT"))
2487 cmInstallScriptGenerator
g(postinstall
, false, 0);
2488 g
.Generate(os
, config
, configurationTypes
);
2493 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2494 static std::string
cmLocalGeneratorMD5(const char* input
)
2497 cmsysMD5
* md5
= cmsysMD5_New();
2498 cmsysMD5_Initialize(md5
);
2499 cmsysMD5_Append(md5
, reinterpret_cast<unsigned char const*>(input
), -1);
2500 cmsysMD5_FinalizeHex(md5
, md5out
);
2501 cmsysMD5_Delete(md5
);
2502 return std::string(md5out
, 32);
2506 cmLocalGeneratorShortenObjectName(std::string
& objName
,
2507 std::string::size_type max_len
)
2509 // Replace the beginning of the path portion of the object name with
2511 std::string::size_type pos
= objName
.find('/', objName
.size()-max_len
+32);
2512 if(pos
!= objName
.npos
)
2514 std::string md5name
= cmLocalGeneratorMD5(objName
.substr(0, pos
).c_str());
2515 md5name
+= objName
.substr(pos
);
2518 // The object name is now short enough.
2523 // The object name could not be shortened enough.
2529 bool cmLocalGeneratorCheckObjectName(std::string
& objName
,
2530 std::string::size_type dir_len
,
2531 std::string::size_type max_total_len
)
2533 // Enforce the maximum file name length if possible.
2534 std::string::size_type max_obj_len
= max_total_len
;
2535 if(dir_len
< max_total_len
)
2537 max_obj_len
= max_total_len
- dir_len
;
2538 if(objName
.size() > max_obj_len
)
2540 // The current object file name is too long. Try to shorten it.
2541 return cmLocalGeneratorShortenObjectName(objName
, max_obj_len
);
2545 // The object file name is short enough.
2551 // The build directory in which the object will be stored is
2552 // already too deep.
2558 //----------------------------------------------------------------------------
2561 ::CreateSafeUniqueObjectFileName(const char* sin
,
2562 std::string
const& dir_max
)
2564 // Look for an existing mapped name for this object file.
2565 std::map
<cmStdString
,cmStdString
>::iterator it
=
2566 this->UniqueObjectNamesMap
.find(sin
);
2568 // If no entry exists create one.
2569 if(it
== this->UniqueObjectNamesMap
.end())
2571 // Start with the original name.
2572 std::string ssin
= sin
;
2574 // Avoid full paths by removing leading slashes.
2575 std::string::size_type pos
= 0;
2576 for(;pos
< ssin
.size() && ssin
[pos
] == '/'; ++pos
)
2579 ssin
= ssin
.substr(pos
);
2581 // Avoid full paths by removing colons.
2582 cmSystemTools::ReplaceString(ssin
, ":", "_");
2584 // Avoid relative paths that go up the tree.
2585 cmSystemTools::ReplaceString(ssin
, "../", "__/");
2588 cmSystemTools::ReplaceString(ssin
, " ", "_");
2590 // Mangle the name if necessary.
2591 if(this->Makefile
->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
2596 sprintf(rpstr
, "_p_");
2597 cmSystemTools::ReplaceString(ssin
, "+", rpstr
);
2598 std::string sssin
= sin
;
2602 for ( it
= this->UniqueObjectNamesMap
.begin();
2603 it
!= this->UniqueObjectNamesMap
.end();
2606 if ( it
->second
== ssin
)
2616 cmSystemTools::ReplaceString(ssin
, "_p_", rpstr
);
2617 sprintf(rpstr
, "_p%d_", cc
++);
2622 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2623 if(!cmLocalGeneratorCheckObjectName(ssin
, dir_max
.size(),
2624 this->ObjectPathMax
))
2626 // Warn if this is the first time the path has been seen.
2627 if(this->ObjectMaxPathViolations
.insert(dir_max
).second
)
2630 m
<< "The object file directory\n"
2631 << " " << dir_max
<< "\n"
2632 << "has " << dir_max
.size() << " characters. "
2633 << "The maximum full path to an object file is "
2634 << this->ObjectPathMax
<< " characters "
2635 << "(see CMAKE_OBJECT_PATH_MAX). "
2637 << " " << ssin
<< "\n"
2638 << "cannot be safely placed under this directory. "
2639 << "The build may not work correctly.";
2640 this->Makefile
->IssueMessage(cmake::WARNING
, m
.str());
2647 // Insert the newly mapped object file name.
2648 std::map
<cmStdString
, cmStdString
>::value_type
e(sin
, ssin
);
2649 it
= this->UniqueObjectNamesMap
.insert(e
).first
;
2652 // Return the map entry.
2656 //----------------------------------------------------------------------------
2659 ::GetObjectFileNameWithoutTarget(const cmSourceFile
& source
,
2660 std::string
const& dir_max
,
2661 bool* hasSourceExtension
)
2663 // Construct the object file name using the full path to the source
2664 // file which is its only unique identification.
2665 const char* fullPath
= source
.GetFullPath().c_str();
2667 // Try referencing the source relative to the source tree.
2668 std::string relFromSource
= this->Convert(fullPath
, START
);
2669 assert(!relFromSource
.empty());
2670 bool relSource
= !cmSystemTools::FileIsFullPath(relFromSource
.c_str());
2671 bool subSource
= relSource
&& relFromSource
[0] != '.';
2673 // Try referencing the source relative to the binary tree.
2674 std::string relFromBinary
= this->Convert(fullPath
, START_OUTPUT
);
2675 assert(!relFromBinary
.empty());
2676 bool relBinary
= !cmSystemTools::FileIsFullPath(relFromBinary
.c_str());
2677 bool subBinary
= relBinary
&& relFromBinary
[0] != '.';
2679 // Select a nice-looking reference to the source file to construct
2680 // the object file name.
2681 std::string objectName
;
2682 if((relSource
&& !relBinary
) || (subSource
&& !subBinary
))
2684 objectName
= relFromSource
;
2686 else if((relBinary
&& !relSource
) || (subBinary
&& !subSource
))
2688 objectName
= relFromBinary
;
2690 else if(relFromBinary
.length() < relFromSource
.length())
2692 objectName
= relFromBinary
;
2696 objectName
= relFromSource
;
2699 // if it is still a full path check for the try compile case
2700 // try compile never have in source sources, and should not
2701 // have conflicting source file names in the same target
2702 if(cmSystemTools::FileIsFullPath(objectName
.c_str()))
2704 if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
2706 objectName
= cmSystemTools::GetFilenameName(source
.GetFullPath());
2710 // Replace the original source file extension with the object file
2712 bool keptSourceExtension
= true;
2713 if(!source
.GetPropertyAsBool("KEEP_EXTENSION"))
2715 // Decide whether this language wants to replace the source
2716 // extension with the object extension. For CMake 2.4
2717 // compatibility do this by default.
2718 bool replaceExt
= this->NeedBackwardsCompatibility(2, 4);
2721 std::string repVar
= "CMAKE_";
2722 repVar
+= source
.GetLanguage();
2723 repVar
+= "_OUTPUT_EXTENSION_REPLACE";
2724 replaceExt
= this->Makefile
->IsOn(repVar
.c_str());
2727 // Remove the source extension if it is to be replaced.
2730 keptSourceExtension
= false;
2731 std::string::size_type dot_pos
= objectName
.rfind(".");
2732 if(dot_pos
!= std::string::npos
)
2734 objectName
= objectName
.substr(0, dot_pos
);
2738 // Store the new extension.
2740 this->GlobalGenerator
->GetLanguageOutputExtension(source
);
2742 if(hasSourceExtension
)
2744 *hasSourceExtension
= keptSourceExtension
;
2747 // Convert to a safe name.
2748 return this->CreateSafeUniqueObjectFileName(objectName
.c_str(), dir_max
);
2751 //----------------------------------------------------------------------------
2754 ::GetSourceFileLanguage(const cmSourceFile
& source
)
2756 return source
.GetLanguage();
2759 //----------------------------------------------------------------------------
2760 std::string
cmLocalGenerator::EscapeForShellOldStyle(const char* str
)
2763 bool forceOn
= cmSystemTools::GetForceUnixPaths();
2764 if(forceOn
&& this->WindowsShell
)
2766 cmSystemTools::SetForceUnixPaths(false);
2768 result
= cmSystemTools::EscapeSpaces(str
);
2769 if(forceOn
&& this->WindowsShell
)
2771 cmSystemTools::SetForceUnixPaths(true);
2776 //----------------------------------------------------------------------------
2777 static bool cmLocalGeneratorIsShellOperator(const char* str
)
2779 if(strcmp(str
, "<") == 0 ||
2780 strcmp(str
, ">") == 0 ||
2781 strcmp(str
, "<<") == 0 ||
2782 strcmp(str
, ">>") == 0 ||
2783 strcmp(str
, "|") == 0 ||
2784 strcmp(str
, "||") == 0 ||
2785 strcmp(str
, "&&") == 0 ||
2786 strcmp(str
, "&>") == 0 ||
2787 strcmp(str
, "1>") == 0 ||
2788 strcmp(str
, "2>") == 0 ||
2789 strcmp(str
, "2>&1") == 0 ||
2790 strcmp(str
, "1>&2") == 0)
2797 //----------------------------------------------------------------------------
2798 std::string
cmLocalGenerator::EscapeForShell(const char* str
, bool makeVars
,
2801 // Do not escape shell operators.
2802 if(cmLocalGeneratorIsShellOperator(str
))
2807 // Compute the flags for the target shell environment.
2809 if(this->WindowsVSIDE
)
2811 flags
|= cmsysSystem_Shell_Flag_VSIDE
;
2813 else if(!this->LinkScriptShell
)
2815 flags
|= cmsysSystem_Shell_Flag_Make
;
2819 flags
|= cmsysSystem_Shell_Flag_AllowMakeVariables
;
2823 flags
|= cmsysSystem_Shell_Flag_EchoWindows
;
2825 if(this->WatcomWMake
)
2827 flags
|= cmsysSystem_Shell_Flag_WatcomWMake
;
2831 flags
|= cmsysSystem_Shell_Flag_MinGWMake
;
2835 flags
|= cmsysSystem_Shell_Flag_NMake
;
2838 // Compute the buffer size needed.
2839 int size
= (this->WindowsShell
?
2840 cmsysSystem_Shell_GetArgumentSizeForWindows(str
, flags
) :
2841 cmsysSystem_Shell_GetArgumentSizeForUnix(str
, flags
));
2843 // Compute the shell argument itself.
2844 std::vector
<char> arg(size
);
2845 if(this->WindowsShell
)
2847 cmsysSystem_Shell_GetArgumentForWindows(str
, &arg
[0], flags
);
2851 cmsysSystem_Shell_GetArgumentForUnix(str
, &arg
[0], flags
);
2853 return std::string(&arg
[0]);
2856 //----------------------------------------------------------------------------
2857 std::string
cmLocalGenerator::EscapeForCMake(const char* str
)
2859 // Always double-quote the argument to take care of most escapes.
2860 std::string result
= "\"";
2861 for(const char* c
= str
; *c
; ++c
)
2865 // Escape the double quote to avoid ending the argument.
2870 // Escape the dollar to avoid expanding variables.
2875 // Escape the backslash to avoid other escapes.
2880 // Other characters will be parsed correctly.
2888 //----------------------------------------------------------------------------
2890 cmLocalGenerator::GetTargetDirectory(cmTarget
const&) const
2892 cmSystemTools::Error("GetTargetDirectory"
2893 " called on cmLocalGenerator");
2898 //----------------------------------------------------------------------------
2900 cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget
* ,
2901 std::vector
<std::string
>&
2904 cmSystemTools::Error("GetTargetObjectFileDirectories"
2905 " called on cmLocalGenerator");
2908 //----------------------------------------------------------------------------
2909 unsigned int cmLocalGenerator::GetBackwardsCompatibility()
2911 // The computed version may change until the project is fully
2913 if(!this->BackwardsCompatibilityFinal
)
2915 unsigned int major
= 0;
2916 unsigned int minor
= 0;
2917 unsigned int patch
= 0;
2918 if(const char* value
2919 = this->Makefile
->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
2921 switch(sscanf(value
, "%u.%u.%u", &major
, &minor
, &patch
))
2923 case 2: patch
= 0; break;
2924 case 1: minor
= 0; patch
= 0; break;
2928 this->BackwardsCompatibility
= CMake_VERSION_ENCODE(major
, minor
, patch
);
2929 this->BackwardsCompatibilityFinal
= this->Configured
;
2932 return this->BackwardsCompatibility
;
2935 //----------------------------------------------------------------------------
2936 bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major
,
2940 // Check the policy to decide whether to pay attention to this
2942 switch(this->Makefile
->GetPolicyStatus(cmPolicies::CMP0001
))
2944 case cmPolicies::WARN
:
2945 // WARN is just OLD without warning because user code does not
2946 // always affect whether this check is done.
2947 case cmPolicies::OLD
:
2948 // Old behavior is to check the variable.
2950 case cmPolicies::NEW
:
2951 // New behavior is to ignore the variable.
2953 case cmPolicies::REQUIRED_IF_USED
:
2954 case cmPolicies::REQUIRED_ALWAYS
:
2955 // This will never be the case because the only way to require
2956 // the setting is to require the user to specify version policy
2957 // 2.6 or higher. Once we add that requirement then this whole
2958 // method can be removed anyway.
2962 // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
2963 // equal to or lower than the given version.
2964 unsigned int actual_compat
= this->GetBackwardsCompatibility();
2965 return (actual_compat
&&
2966 actual_compat
<= CMake_VERSION_ENCODE(major
, minor
, patch
));
2969 //----------------------------------------------------------------------------
2970 bool cmLocalGenerator::CheckDefinition(std::string
const& define
) const
2972 // Many compilers do not support -DNAME(arg)=sdf so we disable it.
2973 bool function_style
= false;
2974 for(const char* c
= define
.c_str(); *c
&& *c
!= '='; ++c
)
2978 function_style
= true;
2985 e
<< "WARNING: Function-style preprocessor definitions may not be "
2986 << "passed on the compiler command line because many compilers "
2987 << "do not support it.\n"
2988 << "CMake is dropping a preprocessor definition: " << define
<< "\n"
2989 << "Consider defining the macro in a (configured) header file.\n";
2990 cmSystemTools::Message(e
.str().c_str());
2994 // Many compilers do not support # in the value so we disable it.
2995 if(define
.find_first_of("#") != define
.npos
)
2998 e
<< "WARNING: Peprocessor definitions containing '#' may not be "
2999 << "passed on the compiler command line because many compilers "
3000 << "do not support it.\n"
3001 << "CMake is dropping a preprocessor definition: " << define
<< "\n"
3002 << "Consider defining the macro in a (configured) header file.\n";
3003 cmSystemTools::Message(e
.str().c_str());
3007 // Assume it is supported.
3011 //----------------------------------------------------------------------------
3012 static void cmLGInfoProp(cmMakefile
* mf
, cmTarget
* target
, const char* prop
)
3014 if(const char* val
= target
->GetProperty(prop
))
3016 mf
->AddDefinition(prop
, val
);
3020 //----------------------------------------------------------------------------
3021 void cmLocalGenerator::GenerateAppleInfoPList(cmTarget
* target
,
3022 const char* targetName
,
3025 // Find the Info.plist template.
3026 const char* in
= target
->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
3027 std::string inFile
= (in
&& *in
)? in
: "MacOSXBundleInfo.plist.in";
3028 if(!cmSystemTools::FileIsFullPath(inFile
.c_str()))
3030 std::string inMod
= this->Makefile
->GetModulesFile(inFile
.c_str());
3036 if(!cmSystemTools::FileExists(inFile
.c_str(), true))
3039 e
<< "Target " << target
->GetName() << " Info.plist template \""
3040 << inFile
<< "\" could not be found.";
3041 cmSystemTools::Error(e
.str().c_str());
3045 // Convert target properties to variables in an isolated makefile
3046 // scope to configure the file. If properties are set they will
3047 // override user make variables. If not the configuration will fall
3048 // back to the directory-level values set by the user.
3049 cmMakefile
* mf
= this->Makefile
;
3051 mf
->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName
);
3052 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_INFO_STRING");
3053 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_ICON_FILE");
3054 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_GUI_IDENTIFIER");
3055 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_LONG_VERSION_STRING");
3056 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_BUNDLE_NAME");
3057 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
3058 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_BUNDLE_VERSION");
3059 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_COPYRIGHT");
3060 mf
->ConfigureFile(inFile
.c_str(), fname
, false, false, false);
3064 //----------------------------------------------------------------------------
3065 void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget
* target
,
3066 const char* targetName
,
3069 // Find the Info.plist template.
3070 const char* in
= target
->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
3071 std::string inFile
= (in
&& *in
)? in
: "MacOSXFrameworkInfo.plist.in";
3072 if(!cmSystemTools::FileIsFullPath(inFile
.c_str()))
3074 std::string inMod
= this->Makefile
->GetModulesFile(inFile
.c_str());
3080 if(!cmSystemTools::FileExists(inFile
.c_str(), true))
3083 e
<< "Target " << target
->GetName() << " Info.plist template \""
3084 << inFile
<< "\" could not be found.";
3085 cmSystemTools::Error(e
.str().c_str());
3089 // Convert target properties to variables in an isolated makefile
3090 // scope to configure the file. If properties are set they will
3091 // override user make variables. If not the configuration will fall
3092 // back to the directory-level values set by the user.
3093 cmMakefile
* mf
= this->Makefile
;
3095 mf
->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName
);
3096 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_ICON_FILE");
3097 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_IDENTIFIER");
3098 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
3099 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
3100 mf
->ConfigureFile(inFile
.c_str(), fname
, false, false, false);