1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalGenerator.cxx,v $
6 Date: $Date: 2009-03-17 19:11:33 $
7 Version: $Revision: 1.299 $
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
= new cmMakefile
;
47 this->Makefile
->SetLocalGenerator(this);
49 this->WindowsShell
= false;
50 this->WindowsVSIDE
= false;
51 this->WatcomWMake
= false;
52 this->MinGWMake
= false;
54 this->MSYSShell
= false;
55 this->LinkScriptShell
= false;
56 this->IgnoreLibPrefix
= false;
57 this->UseRelativePaths
= false;
58 this->Configured
= false;
59 this->EmitUniversalBinaryFlags
= true;
60 this->IsMakefileGenerator
= false;
61 this->RelativePathsConfigured
= false;
62 this->PathConversionsSetup
= false;
63 this->BackwardsCompatibility
= 0;
64 this->BackwardsCompatibilityFinal
= false;
67 cmLocalGenerator::~cmLocalGenerator()
69 delete this->Makefile
;
72 void cmLocalGenerator::Configure()
74 cmLocalGenerator
* previousLg
=
75 this->GetGlobalGenerator()->GetCurrentLocalGenerator();
76 this->GetGlobalGenerator()->SetCurrentLocalGenerator(this);
78 // make sure the CMakeFiles dir is there
79 std::string filesDir
= this->Makefile
->GetStartOutputDirectory();
80 filesDir
+= cmake::GetCMakeFilesDirectory();
81 cmSystemTools::MakeDirectory(filesDir
.c_str());
83 // find & read the list file
84 std::string currentStart
= this->Makefile
->GetStartDirectory();
85 currentStart
+= "/CMakeLists.txt";
86 this->Makefile
->ReadListFile(currentStart
.c_str());
88 // at the end of the ReadListFile handle any old style subdirs
89 // first get all the subdirectories
90 std::vector
<cmLocalGenerator
*> subdirs
= this->GetChildren();
92 // for each subdir recurse
93 std::vector
<cmLocalGenerator
*>::iterator sdi
= subdirs
.begin();
94 for (; sdi
!= subdirs
.end(); ++sdi
)
96 if (!(*sdi
)->Configured
)
98 this->Makefile
->ConfigureSubDirectory(*sdi
);
102 // Check whether relative paths should be used for optionally
104 this->UseRelativePaths
= this->Makefile
->IsOn("CMAKE_USE_RELATIVE_PATHS");
106 // Choose a maximum object file name length.
108 #if defined(_WIN32) || defined(__CYGWIN__)
109 this->ObjectPathMax
= 250;
111 this->ObjectPathMax
= 1000;
113 const char* plen
= this->Makefile
->GetDefinition("CMAKE_OBJECT_PATH_MAX");
117 if(sscanf(plen
, "%u", &pmax
) == 1)
121 this->ObjectPathMax
= pmax
;
126 w
<< "CMAKE_OBJECT_PATH_MAX is set to " << pmax
127 << ", which is less than the minimum of 128. "
128 << "The value will be ignored.";
129 this->Makefile
->IssueMessage(cmake::AUTHOR_WARNING
, w
.str());
135 w
<< "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
136 << "\", which fails to parse as a positive integer. "
137 << "The value will be ignored.";
138 this->Makefile
->IssueMessage(cmake::AUTHOR_WARNING
, w
.str());
141 this->ObjectMaxPathViolations
.clear();
144 this->Configured
= true;
146 this->GetGlobalGenerator()->SetCurrentLocalGenerator(previousLg
);
149 void cmLocalGenerator::SetupPathConversions()
151 // Setup the current output directory components for use by
155 cmSystemTools::CollapseFullPath(this->Makefile
->GetHomeDirectory());
156 cmSystemTools::SplitPath(outdir
.c_str(), this->HomeDirectoryComponents
);
158 cmSystemTools::CollapseFullPath(this->Makefile
->GetStartDirectory());
159 cmSystemTools::SplitPath(outdir
.c_str(), this->StartDirectoryComponents
);
161 outdir
= cmSystemTools::CollapseFullPath
162 (this->Makefile
->GetHomeOutputDirectory());
163 cmSystemTools::SplitPath(outdir
.c_str(),
164 this->HomeOutputDirectoryComponents
);
166 outdir
= cmSystemTools::CollapseFullPath
167 (this->Makefile
->GetStartOutputDirectory());
168 cmSystemTools::SplitPath(outdir
.c_str(),
169 this->StartOutputDirectoryComponents
);
173 void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator
*gg
)
175 this->GlobalGenerator
= gg
;
177 // setup the home directories
178 this->Makefile
->GetProperties().SetCMakeInstance(gg
->GetCMakeInstance());
179 this->Makefile
->SetHomeDirectory(
180 gg
->GetCMakeInstance()->GetHomeDirectory());
181 this->Makefile
->SetHomeOutputDirectory(
182 gg
->GetCMakeInstance()->GetHomeOutputDirectory());
185 void cmLocalGenerator::ConfigureFinalPass()
187 this->Makefile
->ConfigureFinalPass();
190 void cmLocalGenerator::TraceDependencies()
192 // Generate the rule files for each target.
193 cmTargets
& targets
= this->Makefile
->GetTargets();
194 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
196 // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
197 // so don't build a projectfile for it
198 if (strncmp(t
->first
.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
200 const char* projectFilename
= 0;
201 if (this->IsMakefileGenerator
== false) // only use of this variable
203 projectFilename
= t
->second
.GetName();
205 t
->second
.TraceDependencies(projectFilename
);
210 void cmLocalGenerator::GenerateTestFiles()
212 if ( !this->Makefile
->IsOn("CMAKE_TESTING_ENABLED") )
217 // Compute the set of configurations.
218 std::vector
<std::string
> configurationTypes
;
219 if(const char* types
=
220 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
222 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
224 const char* config
= 0;
225 if(configurationTypes
.empty())
227 config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
230 std::string file
= this->Makefile
->GetStartOutputDirectory();
232 file
+= "CTestTestfile.cmake";
234 cmGeneratedFileStream
fout(file
.c_str());
235 fout
.SetCopyIfDifferent(true);
237 fout
<< "# CMake generated Testfile for " << std::endl
238 << "# Source directory: "
239 << this->Makefile
->GetStartDirectory() << std::endl
240 << "# Build directory: "
241 << this->Makefile
->GetStartOutputDirectory() << std::endl
243 << "# This file replicates the SUBDIRS() and ADD_TEST() commands "
244 << "from the source" << std::endl
245 << "# tree CMakeLists.txt file, skipping any SUBDIRS() or "
246 << "ADD_TEST() commands" << std::endl
247 << "# that are excluded by CMake control structures, i.e. IF() "
248 << "commands." << std::endl
;
250 const char* testIncludeFile
=
251 this->Makefile
->GetProperty("TEST_INCLUDE_FILE");
252 if ( testIncludeFile
)
254 fout
<< "INCLUDE(\"" << testIncludeFile
<< "\")" << std::endl
;
257 // Ask each test generator to write its code.
258 std::vector
<cmTestGenerator
*> const&
259 testers
= this->Makefile
->GetTestGenerators();
260 for(std::vector
<cmTestGenerator
*>::const_iterator gi
= testers
.begin();
261 gi
!= testers
.end(); ++gi
)
263 (*gi
)->Generate(fout
, config
, configurationTypes
);
265 if ( this->Children
.size())
268 for(i
= 0; i
< this->Children
.size(); ++i
)
272 this->Children
[i
]->GetMakefile()->GetStartOutputDirectory();
273 fout
<< this->Convert(outP
.c_str(),START_OUTPUT
);
274 fout
<< ")" << std::endl
;
279 //----------------------------------------------------------------------------
280 void cmLocalGenerator::GenerateInstallRules()
282 // Compute the install prefix.
283 const char* prefix
= this->Makefile
->GetDefinition("CMAKE_INSTALL_PREFIX");
284 #if defined(_WIN32) && !defined(__CYGWIN__)
285 std::string prefix_win32
;
288 if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32
))
292 const char* project_name
= this->Makefile
->GetDefinition("PROJECT_NAME");
293 if(project_name
&& project_name
[0])
295 prefix_win32
+= "/Program Files/";
296 prefix_win32
+= project_name
;
300 prefix_win32
+= "/InstalledCMakeProject";
302 prefix
= prefix_win32
.c_str();
307 prefix
= "/usr/local";
311 // Compute the set of configurations.
312 std::vector
<std::string
> configurationTypes
;
313 if(const char* types
=
314 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
316 cmSystemTools::ExpandListArgument(types
, configurationTypes
);
318 const char* config
= 0;
319 if(configurationTypes
.empty())
321 config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
324 // Choose a default install configuration.
325 const char* default_config
= config
;
326 const char* default_order
[] = {"RELEASE", "MINSIZEREL",
327 "RELWITHDEBINFO", "DEBUG", 0};
328 for(const char** c
= default_order
; *c
&& !default_config
; ++c
)
330 for(std::vector
<std::string
>::iterator i
= configurationTypes
.begin();
331 i
!= configurationTypes
.end(); ++i
)
333 if(cmSystemTools::UpperCase(*i
) == *c
)
335 default_config
= i
->c_str();
339 if(!default_config
&& !configurationTypes
.empty())
341 default_config
= configurationTypes
[0].c_str();
345 default_config
= "Release";
348 // Create the install script file.
349 std::string file
= this->Makefile
->GetStartOutputDirectory();
350 std::string homedir
= this->Makefile
->GetHomeOutputDirectory();
351 std::string currdir
= this->Makefile
->GetCurrentOutputDirectory();
352 cmSystemTools::ConvertToUnixSlashes(file
);
353 cmSystemTools::ConvertToUnixSlashes(homedir
);
354 cmSystemTools::ConvertToUnixSlashes(currdir
);
355 int toplevel_install
= 0;
356 if ( currdir
== homedir
)
358 toplevel_install
= 1;
360 file
+= "/cmake_install.cmake";
361 cmGeneratedFileStream
fout(file
.c_str());
362 fout
.SetCopyIfDifferent(true);
365 fout
<< "# Install script for directory: "
366 << this->Makefile
->GetCurrentDirectory() << std::endl
<< std::endl
;
367 fout
<< "# Set the install prefix" << std::endl
368 << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
369 << " SET(CMAKE_INSTALL_PREFIX \"" << prefix
<< "\")" << std::endl
370 << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
371 << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
372 << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
375 // Write support code for generating per-configuration install rules.
377 "# Set the install configuration name.\n"
378 "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
380 " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
381 " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
382 " ELSE(BUILD_TYPE)\n"
383 " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config
<< "\")\n"
384 " ENDIF(BUILD_TYPE)\n"
385 " MESSAGE(STATUS \"Install configuration: "
386 "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
387 "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
390 // Write support code for dealing with component-specific installs.
392 "# Set the component getting installed.\n"
393 "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
395 " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
396 " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
398 " SET(CMAKE_INSTALL_COMPONENT)\n"
399 " ENDIF(COMPONENT)\n"
400 "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
403 // Copy user-specified install options to the install code.
404 if(const char* so_no_exe
=
405 this->Makefile
->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
408 "# Install shared libraries without execute permission?\n"
409 "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
410 " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe
<< "\")\n"
411 "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
415 // Ask each install generator to write its code.
416 std::vector
<cmInstallGenerator
*> const& installers
=
417 this->Makefile
->GetInstallGenerators();
418 for(std::vector
<cmInstallGenerator
*>::const_iterator
419 gi
= installers
.begin();
420 gi
!= installers
.end(); ++gi
)
422 (*gi
)->Generate(fout
, config
, configurationTypes
);
425 // Write rules from old-style specification stored in targets.
426 this->GenerateTargetInstallRules(fout
, config
, configurationTypes
);
428 // Include install scripts from subdirectories.
429 if(!this->Children
.empty())
431 fout
<< "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
432 fout
<< " # Include the install script for each subdirectory.\n";
433 for(std::vector
<cmLocalGenerator
*>::const_iterator
434 ci
= this->Children
.begin(); ci
!= this->Children
.end(); ++ci
)
436 if(!(*ci
)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
438 std::string odir
= (*ci
)->GetMakefile()->GetStartOutputDirectory();
439 cmSystemTools::ConvertToUnixSlashes(odir
);
440 fout
<< " INCLUDE(\"" << odir
.c_str()
441 << "/cmake_install.cmake\")" << std::endl
;
445 fout
<< "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
448 // Record the install manifest.
449 if ( toplevel_install
)
452 "IF(CMAKE_INSTALL_COMPONENT)\n"
453 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
454 "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
455 "ELSE(CMAKE_INSTALL_COMPONENT)\n"
456 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
457 "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
460 << homedir
.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
461 << "\"\")" << std::endl
;
463 << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
465 << homedir
.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
466 << "\"${file}\\n\")" << std::endl
467 << "ENDFOREACH(file)" << std::endl
;
471 //----------------------------------------------------------------------------
472 void cmLocalGenerator::GenerateTargetManifest()
474 // Collect the set of configuration types.
475 std::vector
<std::string
> configNames
;
476 if(const char* configurationTypes
=
477 this->Makefile
->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
479 cmSystemTools::ExpandListArgument(configurationTypes
, configNames
);
481 else if(const char* buildType
=
482 this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE"))
486 configNames
.push_back(buildType
);
490 // Add our targets to the manifest for each configuration.
491 cmTargets
& targets
= this->Makefile
->GetTargets();
492 for(cmTargets::iterator t
= targets
.begin(); t
!= targets
.end(); ++t
)
494 cmTarget
& target
= t
->second
;
495 if(configNames
.empty())
497 target
.GenerateTargetManifest(0);
501 for(std::vector
<std::string
>::iterator ci
= configNames
.begin();
502 ci
!= configNames
.end(); ++ci
)
504 const char* config
= ci
->c_str();
505 target
.GenerateTargetManifest(config
);
511 void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname
,
513 cmSourceFile
& source
,
516 std::string objectDir
= cmSystemTools::GetFilenamePath(std::string(ofname
));
517 objectDir
= this->Convert(objectDir
.c_str(),START_OUTPUT
,SHELL
);
518 std::string objectFile
= this->Convert(ofname
,START_OUTPUT
,SHELL
);
519 std::string sourceFile
=
520 this->Convert(source
.GetFullPath().c_str(),START_OUTPUT
,SHELL
,true);
521 std::string varString
= "CMAKE_";
523 varString
+= "_COMPILE_OBJECT";
524 std::vector
<std::string
> rules
;
525 rules
.push_back(this->Makefile
->GetRequiredDefinition(varString
.c_str()));
526 varString
= "CMAKE_";
528 varString
+= "_FLAGS";
530 flags
+= this->Makefile
->GetSafeDefinition(varString
.c_str());
532 flags
+= this->GetIncludeFlags(lang
);
534 // Construct the command lines.
535 cmCustomCommandLines commandLines
;
536 std::vector
<std::string
> commands
;
537 cmSystemTools::ExpandList(rules
, commands
);
538 cmLocalGenerator::RuleVariables vars
;
539 vars
.Language
= lang
;
540 vars
.Source
= sourceFile
.c_str();
541 vars
.Object
= objectFile
.c_str();
542 vars
.ObjectDir
= objectDir
.c_str();
543 vars
.Flags
= flags
.c_str();
544 for(std::vector
<std::string
>::iterator i
= commands
.begin();
545 i
!= commands
.end(); ++i
)
547 // Expand the full command line string.
548 this->ExpandRuleVariables(*i
, vars
);
550 // Parse the string to get the custom command line.
551 cmCustomCommandLine commandLine
;
552 std::vector
<cmStdString
> cmd
= cmSystemTools::ParseArguments(i
->c_str());
553 for(std::vector
<cmStdString
>::iterator a
= cmd
.begin();
556 commandLine
.push_back(*a
);
559 // Store this command line.
560 commandLines
.push_back(commandLine
);
563 // Check for extra object-file dependencies.
564 std::vector
<std::string
> depends
;
565 const char* additionalDeps
= source
.GetProperty("OBJECT_DEPENDS");
568 cmSystemTools::ExpandListArgument(additionalDeps
, depends
);
571 // Generate a meaningful comment for the command.
572 std::string comment
= "Building ";
574 comment
+= " object ";
575 comment
+= this->Convert(ofname
, START_OUTPUT
);
577 // Add the custom command to build the object file.
578 this->Makefile
->AddCustomCommandToOutput(
581 source
.GetFullPath().c_str(),
584 this->Makefile
->GetStartOutputDirectory()
588 void cmLocalGenerator::AddBuildTargetRule(const char* llang
, cmTarget
& target
)
591 std::vector
<std::string
> objVector
;
592 // Add all the sources outputs to the depends of the target
593 std::vector
<cmSourceFile
*> const& classes
= target
.GetSourceFiles();
594 for(std::vector
<cmSourceFile
*>::const_iterator i
= classes
.begin();
595 i
!= classes
.end(); ++i
)
597 cmSourceFile
* sf
= *i
;
598 if(!sf
->GetCustomCommand() &&
599 !sf
->GetPropertyAsBool("HEADER_FILE_ONLY") &&
600 !sf
->GetPropertyAsBool("EXTERNAL_OBJECT"))
603 dir_max
+= this->Makefile
->GetCurrentOutputDirectory();
605 std::string obj
= this->GetObjectFileNameWithoutTarget(*sf
, dir_max
);
608 std::string ofname
= this->Makefile
->GetCurrentOutputDirectory();
611 objVector
.push_back(ofname
);
612 this->AddCustomCommandToCreateObject(ofname
.c_str(),
613 llang
, *(*i
), target
);
614 objs
+= this->Convert(ofname
.c_str(),START_OUTPUT
,MAKEFILE
);
619 std::string createRule
= "CMAKE_";
621 createRule
+= target
.GetCreateRuleVariable();
622 std::string targetName
= target
.GetFullName();
627 std::string linkLibs
; // should be set
628 std::string flags
; // should be set
629 std::string linkFlags
; // should be set
630 this->GetTargetFlags(linkLibs
, flags
, linkFlags
, target
);
631 cmLocalGenerator::RuleVariables vars
;
632 vars
.Language
= llang
;
633 vars
.Objects
= objs
.c_str();
634 vars
.ObjectDir
= ".";
635 vars
.Target
= targetName
.c_str();
636 vars
.LinkLibraries
= linkLibs
.c_str();
637 vars
.Flags
= flags
.c_str();
638 vars
.LinkFlags
= linkFlags
.c_str();
640 std::string langFlags
;
641 this->AddLanguageFlags(langFlags
, llang
, 0);
642 vars
.LanguageCompileFlags
= langFlags
.c_str();
644 cmCustomCommandLines commandLines
;
645 std::vector
<std::string
> rules
;
646 rules
.push_back(this->Makefile
->GetRequiredDefinition(createRule
.c_str()));
647 std::vector
<std::string
> commands
;
648 cmSystemTools::ExpandList(rules
, commands
);
649 for(std::vector
<std::string
>::iterator i
= commands
.begin();
650 i
!= commands
.end(); ++i
)
652 // Expand the full command line string.
653 this->ExpandRuleVariables(*i
, vars
);
654 // Parse the string to get the custom command line.
655 cmCustomCommandLine commandLine
;
656 std::vector
<cmStdString
> cmd
= cmSystemTools::ParseArguments(i
->c_str());
657 for(std::vector
<cmStdString
>::iterator a
= cmd
.begin();
660 commandLine
.push_back(*a
);
663 // Store this command line.
664 commandLines
.push_back(commandLine
);
666 std::string targetFullPath
= target
.GetFullPath();
667 // Generate a meaningful comment for the command.
668 std::string comment
= "Linking ";
670 comment
+= " target ";
671 comment
+= this->Convert(targetFullPath
.c_str(), START_OUTPUT
);
672 this->Makefile
->AddCustomCommandToOutput(
673 targetFullPath
.c_str(),
678 this->Makefile
->GetStartOutputDirectory()
681 (this->Makefile
->GetSource(targetFullPath
.c_str()));
685 void cmLocalGenerator
686 ::CreateCustomTargetsAndCommands(std::set
<cmStdString
> const& lang
)
688 cmTargets
&tgts
= this->Makefile
->GetTargets();
689 for(cmTargets::iterator l
= tgts
.begin();
690 l
!= tgts
.end(); l
++)
692 cmTarget
& target
= l
->second
;
693 switch(target
.GetType())
695 case cmTarget::STATIC_LIBRARY
:
696 case cmTarget::SHARED_LIBRARY
:
697 case cmTarget::MODULE_LIBRARY
:
698 case cmTarget::EXECUTABLE
:
701 target
.GetLinkerLanguage(this->GetGlobalGenerator());
705 ("CMake can not determine linker language for target:",
709 // if the language is not in the set lang then create custom
710 // commands to build the target
711 if(lang
.count(llang
) == 0)
713 this->AddBuildTargetRule(llang
, target
);
723 // List of variables that are replaced when
724 // rules are expanced. These variables are
725 // replaced in the form <var> with GetSafeDefinition(var).
726 // ${LANG} is replaced in the variable first with all enabled
728 static const char* ruleReplaceVars
[] =
730 "CMAKE_${LANG}_COMPILER",
731 "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
732 "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
733 "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
734 "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
735 "CMAKE_${LANG}_LINK_FLAGS",
736 "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
737 "CMAKE_${LANG}_ARCHIVE",
739 "CMAKE_CURRENT_SOURCE_DIR",
740 "CMAKE_CURRENT_BINARY_DIR",
747 cmLocalGenerator::ExpandRuleVariable(std::string
const& variable
,
748 const RuleVariables
& replaceValues
)
750 if(replaceValues
.LinkFlags
)
752 if(variable
== "LINK_FLAGS")
754 return replaceValues
.LinkFlags
;
757 if(replaceValues
.Flags
)
759 if(variable
== "FLAGS")
761 return replaceValues
.Flags
;
765 if(replaceValues
.Source
)
767 if(variable
== "SOURCE")
769 return replaceValues
.Source
;
772 if(replaceValues
.PreprocessedSource
)
774 if(variable
== "PREPROCESSED_SOURCE")
776 return replaceValues
.PreprocessedSource
;
779 if(replaceValues
.AssemblySource
)
781 if(variable
== "ASSEMBLY_SOURCE")
783 return replaceValues
.AssemblySource
;
786 if(replaceValues
.Object
)
788 if(variable
== "OBJECT")
790 return replaceValues
.Object
;
793 if(replaceValues
.ObjectDir
)
795 if(variable
== "OBJECT_DIR")
797 return replaceValues
.ObjectDir
;
800 if(replaceValues
.Objects
)
802 if(variable
== "OBJECTS")
804 return replaceValues
.Objects
;
807 if(replaceValues
.ObjectsQuoted
)
809 if(variable
== "OBJECTS_QUOTED")
811 return replaceValues
.ObjectsQuoted
;
814 if(replaceValues
.Defines
&& variable
== "DEFINES")
816 return replaceValues
.Defines
;
818 if(replaceValues
.TargetPDB
)
820 if(variable
== "TARGET_PDB")
822 return replaceValues
.TargetPDB
;
826 if(replaceValues
.Target
)
828 if(variable
== "TARGET_QUOTED")
830 std::string targetQuoted
= replaceValues
.Target
;
831 if(targetQuoted
.size() && targetQuoted
[0] != '\"')
834 targetQuoted
+= replaceValues
.Target
;
835 targetQuoted
+= '\"';
839 if(replaceValues
.LanguageCompileFlags
)
841 if(variable
== "LANGUAGE_COMPILE_FLAGS")
843 return replaceValues
.LanguageCompileFlags
;
846 if(replaceValues
.Target
)
848 if(variable
== "TARGET")
850 return replaceValues
.Target
;
853 if(variable
== "TARGET_IMPLIB")
855 return this->TargetImplib
;
857 if(variable
== "TARGET_VERSION_MAJOR")
859 if(replaceValues
.TargetVersionMajor
)
861 return replaceValues
.TargetVersionMajor
;
868 if(variable
== "TARGET_VERSION_MINOR")
870 if(replaceValues
.TargetVersionMinor
)
872 return replaceValues
.TargetVersionMinor
;
879 if(replaceValues
.Target
)
881 if(variable
== "TARGET_BASE")
883 // Strip the last extension off the target name.
884 std::string targetBase
= replaceValues
.Target
;
885 std::string::size_type pos
= targetBase
.rfind(".");
886 if(pos
!= targetBase
.npos
)
888 return targetBase
.substr(0, pos
);
897 if(replaceValues
.TargetSOName
)
899 if(variable
== "TARGET_SONAME")
901 if(replaceValues
.Language
)
903 std::string name
= "CMAKE_SHARED_LIBRARY_SONAME_";
904 name
+= replaceValues
.Language
;
906 if(this->Makefile
->GetDefinition(name
.c_str()))
908 return replaceValues
.TargetSOName
;
914 if(replaceValues
.TargetInstallNameDir
)
916 if(variable
== "TARGET_INSTALLNAME_DIR")
918 return replaceValues
.TargetInstallNameDir
;
921 if(replaceValues
.LinkLibraries
)
923 if(variable
== "LINK_LIBRARIES")
925 return replaceValues
.LinkLibraries
;
928 if(replaceValues
.Language
)
930 if(variable
== "LANGUAGE")
932 return replaceValues
.Language
;
935 if(replaceValues
.CMTarget
)
937 if(variable
== "TARGET_NAME")
939 return replaceValues
.CMTarget
->GetName();
941 if(variable
== "TARGET_TYPE")
943 return cmTarget::TargetTypeNames
[replaceValues
.CMTarget
->GetType()];
946 if(replaceValues
.Output
)
948 if(variable
== "OUTPUT")
950 return replaceValues
.Output
;
953 if(variable
== "CMAKE_COMMAND")
955 const char* cmcommand
=
956 this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
957 return this->Convert(cmcommand
, FULL
, SHELL
);
959 std::vector
<std::string
> enabledLanguages
;
960 this->GlobalGenerator
->GetEnabledLanguages(enabledLanguages
);
961 // loop over language specific replace variables
963 while(ruleReplaceVars
[pos
])
965 for(std::vector
<std::string
>::iterator i
= enabledLanguages
.begin();
966 i
!= enabledLanguages
.end(); ++i
)
968 const char* lang
= i
->c_str();
969 std::string actualReplace
= ruleReplaceVars
[pos
];
970 // If this is the compiler then look for the extra variable
971 // _COMPILER_ARG1 which must be the first argument to the compiler
972 const char* compilerArg1
= 0;
973 if(actualReplace
== "CMAKE_${LANG}_COMPILER")
975 std::string arg1
= actualReplace
+ "_ARG1";
976 cmSystemTools::ReplaceString(arg1
, "${LANG}", lang
);
977 compilerArg1
= this->Makefile
->GetDefinition(arg1
.c_str());
979 if(actualReplace
.find("${LANG}") != actualReplace
.npos
)
981 cmSystemTools::ReplaceString(actualReplace
, "${LANG}", lang
);
983 if(actualReplace
== variable
)
985 std::string replace
=
986 this->Makefile
->GetSafeDefinition(variable
.c_str());
987 // if the variable is not a FLAG then treat it like a path
988 if(variable
.find("_FLAG") == variable
.npos
)
990 std::string ret
= this->ConvertToOutputForExisting(replace
.c_str());
991 // if there is a required first argument to the compiler add it
992 // to the compiler string
1010 cmLocalGenerator::ExpandRuleVariables(std::string
& s
,
1011 const RuleVariables
& replaceValues
)
1013 std::vector
<std::string
> enabledLanguages
;
1014 this->GlobalGenerator
->GetEnabledLanguages(enabledLanguages
);
1015 this->InsertRuleLauncher(s
, replaceValues
.CMTarget
,
1016 replaceValues
.RuleLauncher
);
1017 std::string::size_type start
= s
.find('<');
1018 // no variables to expand
1023 std::string::size_type pos
= 0;
1024 std::string expandedInput
;
1025 while(start
!= s
.npos
&& start
< s
.size()-2)
1027 std::string::size_type end
= s
.find('>', start
);
1028 // if we find a < with no > we are done
1033 char c
= s
[start
+1];
1034 // if the next char after the < is not A-Za-z then
1035 // skip it and try to find the next < in the string
1038 start
= s
.find('<', start
+1);
1043 std::string var
= s
.substr(start
+1, end
- start
-1);
1044 std::string replace
= this->ExpandRuleVariable(var
,
1046 expandedInput
+= s
.substr(pos
, start
-pos
);
1047 expandedInput
+= replace
;
1049 start
= s
.find('<', start
+var
.size()+2);
1053 // add the rest of the input
1054 expandedInput
+= s
.substr(pos
, s
.size()-pos
);
1058 //----------------------------------------------------------------------------
1059 const char* cmLocalGenerator::GetRuleLauncher(cmTarget
* target
,
1064 return target
->GetProperty(prop
);
1068 return this->Makefile
->GetProperty(prop
);
1072 //----------------------------------------------------------------------------
1073 void cmLocalGenerator::InsertRuleLauncher(std::string
& s
, cmTarget
* target
,
1076 if(const char* val
= this->GetRuleLauncher(target
, prop
))
1078 cmOStringStream wrapped
;
1079 wrapped
<< val
<< " " << s
;
1084 //----------------------------------------------------------------------------
1086 cmLocalGenerator::ConvertToOutputForExistingCommon(const char* remote
,
1087 std::string
const& result
)
1089 // If this is a windows shell, the result has a space, and the path
1090 // already exists, we can use a short-path to reference it without a
1092 if(this->WindowsShell
&& result
.find(' ') != result
.npos
&&
1093 cmSystemTools::FileExists(remote
))
1096 if(cmSystemTools::GetShortPath(remote
, tmp
))
1098 return this->Convert(tmp
.c_str(), NONE
, SHELL
, true);
1102 // Otherwise, leave it unchanged.
1106 //----------------------------------------------------------------------------
1108 cmLocalGenerator::ConvertToOutputForExisting(const char* remote
,
1111 // Perform standard conversion.
1112 std::string result
= this->Convert(remote
, local
, SHELL
, true);
1114 // Consider short-path.
1115 return this->ConvertToOutputForExistingCommon(remote
, result
);
1118 //----------------------------------------------------------------------------
1120 cmLocalGenerator::ConvertToOutputForExisting(RelativeRoot remote
,
1123 // Perform standard conversion.
1124 std::string result
= this->Convert(remote
, local
, SHELL
, true);
1126 // Consider short-path.
1127 const char* remotePath
= this->GetRelativeRootPath(remote
);
1128 return this->ConvertToOutputForExistingCommon(remotePath
, result
);
1131 //----------------------------------------------------------------------------
1132 const char* cmLocalGenerator::GetIncludeFlags(const char* lang
)
1138 if(this->LanguageToIncludeFlags
.count(lang
))
1140 return this->LanguageToIncludeFlags
[lang
].c_str();
1143 cmOStringStream includeFlags
;
1144 std::vector
<std::string
> includes
;
1145 this->GetIncludeDirectories(includes
, lang
);
1146 std::vector
<std::string
>::iterator i
;
1148 std::string flagVar
= "CMAKE_INCLUDE_FLAG_";
1150 const char* includeFlag
=
1151 this->Makefile
->GetSafeDefinition(flagVar
.c_str());
1152 flagVar
= "CMAKE_INCLUDE_FLAG_SEP_";
1154 const char* sep
= this->Makefile
->GetDefinition(flagVar
.c_str());
1155 bool quotePaths
= false;
1156 if(this->Makefile
->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
1160 bool repeatFlag
= true;
1161 // should the include flag be repeated like ie. -IA -IB
1168 // if there is a separator then the flag is not repeated but is only
1169 // given once i.e. -classpath a:b:c
1173 // Support special system include flag if it is available and the
1174 // normal flag is repeated for each directory.
1175 std::string sysFlagVar
= "CMAKE_INCLUDE_SYSTEM_FLAG_";
1177 const char* sysIncludeFlag
= 0;
1180 sysIncludeFlag
= this->Makefile
->GetDefinition(sysFlagVar
.c_str());
1183 bool flagUsed
= false;
1184 std::set
<cmStdString
> emitted
;
1186 emitted
.insert("/System/Library/Frameworks");
1188 for(i
= includes
.begin(); i
!= includes
.end(); ++i
)
1191 if(cmSystemTools::IsPathToFramework(i
->c_str()))
1193 std::string frameworkDir
= *i
;
1194 frameworkDir
+= "/../";
1195 frameworkDir
= cmSystemTools::CollapseFullPath(frameworkDir
.c_str());
1196 if(emitted
.insert(frameworkDir
).second
)
1199 << "-F" << this->Convert(frameworkDir
.c_str(),
1200 cmLocalGenerator::START_OUTPUT
,
1201 cmLocalGenerator::SHELL
, true)
1207 std::string include
= *i
;
1208 if(!flagUsed
|| repeatFlag
)
1210 if(sysIncludeFlag
&&
1211 this->Makefile
->IsSystemIncludeDirectory(i
->c_str()))
1213 includeFlags
<< sysIncludeFlag
;
1217 includeFlags
<< includeFlag
;
1221 std::string includePath
= this->ConvertToOutputForExisting(i
->c_str());
1222 if(quotePaths
&& includePath
.size() && includePath
[0] != '\"')
1224 includeFlags
<< "\"";
1226 includeFlags
<< includePath
;
1227 if(quotePaths
&& includePath
.size() && includePath
[0] != '\"')
1229 includeFlags
<< "\"";
1231 includeFlags
<< sep
;
1233 std::string flags
= includeFlags
.str();
1234 // remove trailing separators
1235 if((sep
[0] != ' ') && flags
[flags
.size()-1] == sep
[0])
1237 flags
[flags
.size()-1] = ' ';
1239 std::string defineFlags
= this->Makefile
->GetDefineFlags();
1240 flags
+= defineFlags
;
1241 this->LanguageToIncludeFlags
[lang
] = flags
;
1243 // Use this temorary variable for the return value to work-around a
1244 // bogus GCC 2.95 warning.
1245 const char* ret
= this->LanguageToIncludeFlags
[lang
].c_str();
1249 //----------------------------------------------------------------------------
1250 void cmLocalGenerator::GetIncludeDirectories(std::vector
<std::string
>& dirs
,
1253 // Need to decide whether to automatically include the source and
1254 // binary directories at the beginning of the include path.
1255 bool includeSourceDir
= false;
1256 bool includeBinaryDir
= false;
1258 // When automatic include directories are requested for a build then
1259 // include the source and binary directories at the beginning of the
1260 // include path to approximate include file behavior for an
1261 // in-source build. This does not account for the case of a source
1262 // file in a subdirectory of the current source directory but we
1263 // cannot fix this because not all native build tools support
1264 // per-source-file include paths.
1265 if(this->Makefile
->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
1267 includeSourceDir
= true;
1268 includeBinaryDir
= true;
1271 // CMake versions below 2.0 would add the source tree to the -I path
1272 // automatically. Preserve compatibility.
1273 if(this->NeedBackwardsCompatibility(1,9))
1275 includeSourceDir
= true;
1278 // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
1279 // not set the backwards compatibility level automatically.
1280 const char* vtkSourceDir
=
1281 this->Makefile
->GetDefinition("VTK_SOURCE_DIR");
1284 const char* vtk_major
=
1285 this->Makefile
->GetDefinition("VTK_MAJOR_VERSION");
1286 const char* vtk_minor
=
1287 this->Makefile
->GetDefinition("VTK_MINOR_VERSION");
1288 vtk_major
= vtk_major
? vtk_major
: "4";
1289 vtk_minor
= vtk_minor
? vtk_minor
: "4";
1292 if(sscanf(vtk_major
, "%d", &vmajor
) &&
1293 sscanf(vtk_minor
, "%d", &vminor
) && vmajor
== 4 && vminor
<= 4)
1295 includeSourceDir
= true;
1299 // Do not repeat an include path.
1300 std::set
<cmStdString
> emitted
;
1302 // Store the automatic include paths.
1303 if(includeBinaryDir
)
1305 dirs
.push_back(this->Makefile
->GetStartOutputDirectory());
1306 emitted
.insert(this->Makefile
->GetStartOutputDirectory());
1308 if(includeSourceDir
)
1310 if(emitted
.find(this->Makefile
->GetStartDirectory()) == emitted
.end())
1312 dirs
.push_back(this->Makefile
->GetStartDirectory());
1313 emitted
.insert(this->Makefile
->GetStartDirectory());
1317 // Load implicit include directories for this language.
1318 std::string impDirVar
= "CMAKE_";
1320 impDirVar
+= "_IMPLICIT_INCLUDE_DIRECTORIES";
1321 if(const char* value
= this->Makefile
->GetDefinition(impDirVar
.c_str()))
1323 std::vector
<std::string
> impDirVec
;
1324 cmSystemTools::ExpandListArgument(value
, impDirVec
);
1325 for(std::vector
<std::string
>::const_iterator i
= impDirVec
.begin();
1326 i
!= impDirVec
.end(); ++i
)
1332 // Get the project-specified include directories.
1333 std::vector
<std::string
>& includes
=
1334 this->Makefile
->GetIncludeDirectories();
1336 // Support putting all the in-project include directories first if
1337 // it is requested by the project.
1338 if(this->Makefile
->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
1340 const char* topSourceDir
= this->Makefile
->GetHomeDirectory();
1341 const char* topBinaryDir
= this->Makefile
->GetHomeOutputDirectory();
1342 for(std::vector
<std::string
>::iterator i
= includes
.begin();
1343 i
!= includes
.end(); ++i
)
1345 // Emit this directory only if it is a subdirectory of the
1346 // top-level source or binary tree.
1347 if(cmSystemTools::ComparePath(i
->c_str(), topSourceDir
) ||
1348 cmSystemTools::ComparePath(i
->c_str(), topBinaryDir
) ||
1349 cmSystemTools::IsSubDirectory(i
->c_str(), topSourceDir
) ||
1350 cmSystemTools::IsSubDirectory(i
->c_str(), topBinaryDir
))
1352 if(emitted
.insert(*i
).second
)
1360 // Construct the final ordered include directory list.
1361 for(std::vector
<std::string
>::iterator i
= includes
.begin();
1362 i
!= includes
.end(); ++i
)
1364 if(emitted
.insert(*i
).second
)
1371 void cmLocalGenerator::GetTargetFlags(std::string
& linkLibs
,
1373 std::string
& linkFlags
,
1376 std::string buildType
=
1377 this->Makefile
->GetSafeDefinition("CMAKE_BUILD_TYPE");
1378 buildType
= cmSystemTools::UpperCase(buildType
);
1379 const char* libraryLinkVariable
=
1380 "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
1382 switch(target
.GetType())
1384 case cmTarget::STATIC_LIBRARY
:
1386 const char* targetLinkFlags
=
1387 target
.GetProperty("STATIC_LIBRARY_FLAGS");
1390 linkFlags
+= targetLinkFlags
;
1395 case cmTarget::MODULE_LIBRARY
:
1396 libraryLinkVariable
= "CMAKE_MODULE_LINKER_FLAGS";
1397 case cmTarget::SHARED_LIBRARY
:
1399 linkFlags
= this->Makefile
->GetSafeDefinition(libraryLinkVariable
);
1401 if(buildType
.size())
1403 std::string build
= libraryLinkVariable
;
1406 linkFlags
+= this->Makefile
->GetSafeDefinition(build
.c_str());
1409 if(this->Makefile
->IsOn("WIN32") &&
1410 !(this->Makefile
->IsOn("CYGWIN") || this->Makefile
->IsOn("MINGW")))
1412 const std::vector
<cmSourceFile
*>& sources
= target
.GetSourceFiles();
1413 for(std::vector
<cmSourceFile
*>::const_iterator i
= sources
.begin();
1414 i
!= sources
.end(); ++i
)
1416 cmSourceFile
* sf
= *i
;
1417 if(sf
->GetExtension() == "def")
1420 this->Makefile
->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
1421 linkFlags
+= this->Convert(sf
->GetFullPath().c_str(),
1422 START_OUTPUT
, SHELL
);
1427 const char* targetLinkFlags
= target
.GetProperty("LINK_FLAGS");
1430 linkFlags
+= targetLinkFlags
;
1432 std::string configLinkFlags
= targetLinkFlags
;
1433 configLinkFlags
+= buildType
;
1434 targetLinkFlags
= target
.GetProperty(configLinkFlags
.c_str());
1437 linkFlags
+= targetLinkFlags
;
1441 cmOStringStream linklibsStr
;
1442 this->OutputLinkLibraries(linklibsStr
, target
, false);
1443 linkLibs
= linklibsStr
.str();
1446 case cmTarget::EXECUTABLE
:
1449 this->Makefile
->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
1451 if(buildType
.size())
1453 std::string build
= "CMAKE_EXE_LINKER_FLAGS_";
1455 linkFlags
+= this->Makefile
->GetSafeDefinition(build
.c_str());
1458 const char* linkLanguage
=
1459 target
.GetLinkerLanguage(this->GetGlobalGenerator());
1462 cmSystemTools::Error
1463 ("CMake can not determine linker language for target:",
1467 std::string langVar
= "CMAKE_";
1468 langVar
+= linkLanguage
;
1469 std::string flagsVar
= langVar
+ "_FLAGS";
1470 std::string sharedFlagsVar
= "CMAKE_SHARED_LIBRARY_";
1471 sharedFlagsVar
+= linkLanguage
;
1472 sharedFlagsVar
+= "_FLAGS";
1473 flags
+= this->Makefile
->GetSafeDefinition(flagsVar
.c_str());
1475 flags
+= this->Makefile
->GetSafeDefinition(sharedFlagsVar
.c_str());
1477 cmOStringStream linklibs
;
1478 this->OutputLinkLibraries(linklibs
, target
, false);
1479 linkLibs
= linklibs
.str();
1480 if(cmSystemTools::IsOn
1481 (this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
1483 std::string sFlagVar
= std::string("CMAKE_SHARED_BUILD_")
1484 + linkLanguage
+ std::string("_FLAGS");
1485 linkFlags
+= this->Makefile
->GetSafeDefinition(sFlagVar
.c_str());
1488 if ( target
.GetPropertyAsBool("WIN32_EXECUTABLE") )
1491 this->Makefile
->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
1497 this->Makefile
->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
1500 const char* targetLinkFlags
= target
.GetProperty("LINK_FLAGS");
1503 linkFlags
+= targetLinkFlags
;
1505 std::string configLinkFlags
= targetLinkFlags
;
1506 configLinkFlags
+= buildType
;
1507 targetLinkFlags
= target
.GetProperty(configLinkFlags
.c_str());
1510 linkFlags
+= targetLinkFlags
;
1521 std::string
cmLocalGenerator::ConvertToLinkReference(std::string
const& lib
)
1523 #if defined(_WIN32) && !defined(__CYGWIN__)
1524 // Work-ardound command line parsing limitations in MSVC 6.0 and
1526 if(this->Makefile
->IsOn("MSVC60") || this->Makefile
->IsOn("WATCOM"))
1528 // Search for the last space.
1529 std::string::size_type pos
= lib
.rfind(' ');
1532 // Find the slash after the last space, if any.
1533 pos
= lib
.find('/', pos
);
1535 // Convert the portion of the path with a space to a short path.
1537 if(cmSystemTools::GetShortPath(lib
.substr(0, pos
).c_str(), sp
))
1539 // Append the rest of the path with no space.
1540 sp
+= lib
.substr(pos
);
1542 // Convert to an output path.
1543 return this->Convert(sp
.c_str(), NONE
, SHELL
);
1550 return this->Convert(lib
.c_str(), START_OUTPUT
, SHELL
);
1554 * Output the linking rules on a command line. For executables,
1555 * targetLibrary should be a NULL pointer. For libraries, it should point
1556 * to the name of the library. This will not link a library against itself.
1558 void cmLocalGenerator::OutputLinkLibraries(std::ostream
& fout
,
1562 const char* config
= this->Makefile
->GetDefinition("CMAKE_BUILD_TYPE");
1563 cmComputeLinkInformation
* pcli
= tgt
.GetLinkInformation(config
);
1568 cmComputeLinkInformation
& cli
= *pcli
;
1570 // Collect library linking flags command line options.
1571 std::string linkLibs
;
1573 const char* linkLanguage
= cli
.GetLinkLanguage();
1575 std::string libPathFlag
=
1576 this->Makefile
->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1577 std::string libPathTerminator
=
1578 this->Makefile
->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1580 // Flags to link an executable to shared libraries.
1581 std::string linkFlagsVar
= "CMAKE_SHARED_LIBRARY_LINK_";
1582 linkFlagsVar
+= linkLanguage
;
1583 linkFlagsVar
+= "_FLAGS";
1584 if( tgt
.GetType() == cmTarget::EXECUTABLE
)
1586 linkLibs
= this->Makefile
->GetSafeDefinition(linkFlagsVar
.c_str());
1590 // Append the framework search path flags.
1591 std::vector
<std::string
> const& fwDirs
= cli
.GetFrameworkPaths();
1592 for(std::vector
<std::string
>::const_iterator fdi
= fwDirs
.begin();
1593 fdi
!= fwDirs
.end(); ++fdi
)
1596 linkLibs
+= this->Convert(fdi
->c_str(), NONE
, SHELL
, false);
1600 // Append the library search path flags.
1601 std::vector
<std::string
> const& libDirs
= cli
.GetDirectories();
1602 for(std::vector
<std::string
>::const_iterator libDir
= libDirs
.begin();
1603 libDir
!= libDirs
.end(); ++libDir
)
1605 std::string libpath
= this->ConvertToOutputForExisting(libDir
->c_str());
1606 linkLibs
+= libPathFlag
;
1607 linkLibs
+= libpath
;
1608 linkLibs
+= libPathTerminator
;
1612 // Append the link items.
1613 typedef cmComputeLinkInformation::ItemVector ItemVector
;
1614 ItemVector
const& items
= cli
.GetItems();
1615 for(ItemVector::const_iterator li
= items
.begin(); li
!= items
.end(); ++li
)
1619 linkLibs
+= this->ConvertToLinkReference(li
->Value
);
1623 linkLibs
+= li
->Value
;
1628 // Write the library flags to the build rule.
1631 // Get the RPATH entries.
1632 std::vector
<std::string
> runtimeDirs
;
1633 cli
.GetRPath(runtimeDirs
, relink
);
1635 // Check what kind of rpath flags to use.
1636 if(cli
.GetRuntimeSep().empty())
1638 // Each rpath entry gets its own option ("-R a -R b -R c")
1640 for(std::vector
<std::string
>::iterator ri
= runtimeDirs
.begin();
1641 ri
!= runtimeDirs
.end(); ++ri
)
1643 rpath
+= cli
.GetRuntimeFlag();
1644 rpath
+= this->Convert(ri
->c_str(), NONE
, SHELL
, false);
1651 // All rpath entries are combined ("-Wl,-rpath,a:b:c").
1652 std::string rpath
= cli
.GetRPathString(relink
);
1654 // Store the rpath option in the stream.
1657 fout
<< cli
.GetRuntimeFlag();
1658 fout
<< this->EscapeForShell(rpath
.c_str(), true);
1663 // Add the linker runtime search path if any.
1664 std::string rpath_link
= cli
.GetRPathLinkString();
1665 if(!cli
.GetRPathLinkFlag().empty() && !rpath_link
.empty())
1667 fout
<< cli
.GetRPathLinkFlag();
1668 fout
<< this->EscapeForShell(rpath_link
.c_str(), true);
1672 // Add standard libraries for this language.
1673 std::string standardLibsVar
= "CMAKE_";
1674 standardLibsVar
+= cli
.GetLinkLanguage();
1675 standardLibsVar
+= "_STANDARD_LIBRARIES";
1676 if(const char* stdLibs
=
1677 this->Makefile
->GetDefinition(standardLibsVar
.c_str()))
1679 fout
<< stdLibs
<< " ";
1683 //----------------------------------------------------------------------------
1684 void cmLocalGenerator::AddLanguageFlags(std::string
& flags
,
1688 // Add language-specific flags.
1689 std::string flagsVar
= "CMAKE_";
1691 flagsVar
+= "_FLAGS";
1693 if(this->EmitUniversalBinaryFlags
)
1695 const char* osxArch
=
1696 this->Makefile
->GetDefinition("CMAKE_OSX_ARCHITECTURES");
1697 const char* sysroot
=
1698 this->Makefile
->GetDefinition("CMAKE_OSX_SYSROOT");
1699 const char* sysrootDefault
=
1700 this->Makefile
->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
1701 const char* deploymentTarget
=
1702 this->Makefile
->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
1704 bool flagsUsed
= false;
1706 if(osxArch
&& sysroot
&& lang
&& (lang
[0] =='C' || lang
[0] == 'F'))
1708 std::vector
<std::string
> archs
;
1709 cmSystemTools::ExpandListArgument(std::string(osxArch
),
1711 bool addArchFlag
= true;
1712 if(archs
.size() == 1)
1714 const char* archOrig
=
1715 this->Makefile
->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
1717 if(archs
[0] == archOrig
)
1719 addArchFlag
= false;
1722 // if there is more than one arch add the -arch and
1723 // -isysroot flags, or if there is one arch flag, but
1724 // it is not the default -arch flag for the system, then
1725 // add it. Otherwize do not add -arch and -isysroot
1728 for( std::vector
<std::string
>::iterator i
= archs
.begin();
1729 i
!= archs
.end(); ++i
)
1735 flags
+= " -isysroot ";
1742 if(!flagsUsed
&& sysroot
&& sysrootDefault
&&
1743 strcmp(sysroot
, sysrootDefault
) != 0)
1745 flags
+= " -isysroot ";
1749 if (deploymentTarget
&& *deploymentTarget
&&
1750 lang
&& (lang
[0] =='C' || lang
[0] == 'F'))
1752 flags
+= " -mmacosx-version-min=";
1753 flags
+= deploymentTarget
;
1757 this->AddConfigVariableFlags(flags
, flagsVar
.c_str(), config
);
1760 //----------------------------------------------------------------------------
1761 std::string
cmLocalGenerator::GetRealDependency(const char* inName
,
1764 // Older CMake code may specify the dependency using the target
1765 // output file rather than the target name. Such code would have
1766 // been written before there was support for target properties that
1767 // modify the name so stripping down to just the file name should
1768 // produce the target name in this case.
1769 std::string name
= cmSystemTools::GetFilenameName(inName
);
1770 if(cmSystemTools::GetFilenameLastExtension(name
) == ".exe")
1772 name
= cmSystemTools::GetFilenameWithoutLastExtension(name
);
1775 // Look for a CMake target with the given name.
1776 if(cmTarget
* target
= this->Makefile
->FindTargetToUse(name
.c_str()))
1778 // make sure it is not just a coincidence that the target name
1779 // found is part of the inName
1780 if(cmSystemTools::FileIsFullPath(inName
))
1782 std::string tLocation
= target
->GetLocation(config
);
1783 tLocation
= cmSystemTools::GetFilenamePath(tLocation
);
1784 std::string depLocation
= cmSystemTools::GetFilenamePath(
1785 std::string(inName
));
1786 depLocation
= cmSystemTools::CollapseFullPath(depLocation
.c_str());
1787 tLocation
= cmSystemTools::CollapseFullPath(tLocation
.c_str());
1788 if(depLocation
!= tLocation
)
1790 // it is a full path to a depend that has the same name
1791 // as a target but is in a different location so do not use
1792 // the target as the depend
1796 switch (target
->GetType())
1798 case cmTarget::EXECUTABLE
:
1799 case cmTarget::STATIC_LIBRARY
:
1800 case cmTarget::SHARED_LIBRARY
:
1801 case cmTarget::MODULE_LIBRARY
:
1802 case cmTarget::UNKNOWN_LIBRARY
:
1804 // Get the location of the target's output file and depend on it.
1805 if(const char* location
= target
->GetLocation(config
))
1811 case cmTarget::UTILITY
:
1812 case cmTarget::GLOBAL_TARGET
:
1813 // Depending on a utility target may not work but just trust
1814 // the user to have given a valid name.
1816 case cmTarget::INSTALL_FILES
:
1817 case cmTarget::INSTALL_PROGRAMS
:
1818 case cmTarget::INSTALL_DIRECTORY
:
1823 // The name was not that of a CMake target. It must name a file.
1824 if(cmSystemTools::FileIsFullPath(inName
))
1826 // This is a full path. Return it as given.
1830 // Check for a source file in this directory that matches the
1832 if(cmSourceFile
* sf
= this->Makefile
->GetSource(inName
))
1834 name
= sf
->GetFullPath();
1838 // Treat the name as relative to the source directory in which it
1840 name
= this->Makefile
->GetCurrentDirectory();
1846 //----------------------------------------------------------------------------
1847 std::string
cmLocalGenerator::GetRealLocation(const char* inName
,
1850 std::string outName
=inName
;
1851 // Look for a CMake target with the given name, which is an executable
1852 // and which can be run
1853 cmTarget
* target
= this->Makefile
->FindTargetToUse(inName
);
1855 && (target
->GetType() == cmTarget::EXECUTABLE
)
1856 && ((this->Makefile
->IsOn("CMAKE_CROSSCOMPILING") == false)
1857 || (target
->IsImported() == true)))
1859 outName
= target
->GetLocation( config
);
1864 //----------------------------------------------------------------------------
1865 void cmLocalGenerator::AddSharedFlags(std::string
& flags
,
1869 std::string flagsVar
;
1871 // Add flags for dealing with shared libraries for this language.
1874 flagsVar
= "CMAKE_SHARED_LIBRARY_";
1876 flagsVar
+= "_FLAGS";
1877 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1880 // Add flags specific to shared builds.
1881 if(cmSystemTools::IsOn(this->Makefile
->GetDefinition("BUILD_SHARED_LIBS")))
1883 flagsVar
= "CMAKE_SHARED_BUILD_";
1885 flagsVar
+= "_FLAGS";
1886 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1890 //----------------------------------------------------------------------------
1891 void cmLocalGenerator::AddConfigVariableFlags(std::string
& flags
,
1895 // Add the flags from the variable itself.
1896 std::string flagsVar
= var
;
1897 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1898 // Add the flags from the build-type specific variable.
1899 if(config
&& *config
)
1902 flagsVar
+= cmSystemTools::UpperCase(config
);
1903 this->AppendFlags(flags
, this->Makefile
->GetDefinition(flagsVar
.c_str()));
1907 //----------------------------------------------------------------------------
1908 void cmLocalGenerator::AppendFlags(std::string
& flags
,
1909 const char* newFlags
)
1911 if(newFlags
&& *newFlags
)
1913 std::string newf
= newFlags
;
1922 //----------------------------------------------------------------------------
1923 void cmLocalGenerator::AppendDefines(std::string
& defines
,
1924 const char* defines_list
,
1927 // Short-circuit if there are no definitions.
1933 // Expand the list of definitions.
1934 std::vector
<std::string
> defines_vec
;
1935 cmSystemTools::ExpandListArgument(defines_list
, defines_vec
);
1937 // Short-circuit if there are no definitions.
1938 if(defines_vec
.empty())
1943 // Lookup the define flag for the current language.
1944 std::string dflag
= "-D";
1947 std::string defineFlagVar
= "CMAKE_";
1948 defineFlagVar
+= lang
;
1949 defineFlagVar
+= "_DEFINE_FLAG";
1950 const char* df
= this->Makefile
->GetDefinition(defineFlagVar
.c_str());
1957 // Add each definition to the command line with appropriate escapes.
1958 const char* dsep
= defines
.empty()? "" : " ";
1959 for(std::vector
<std::string
>::const_iterator di
= defines_vec
.begin();
1960 di
!= defines_vec
.end(); ++di
)
1962 // Skip unsupported definitions.
1963 if(!this->CheckDefinition(*di
))
1968 // Separate from previous definitions.
1972 // Append the definition with proper escaping.
1974 if(this->WatcomWMake
)
1976 // The Watcom compiler does its own command line parsing instead
1977 // of using the windows shell rules. Definitions are one of
1979 // -DNAME=<cpp-token>
1980 // -DNAME="c-string with spaces and other characters(?@#$)"
1982 // Watcom will properly parse each of these cases from the
1983 // command line without any escapes. However we still have to
1984 // get the '$' and '#' characters through WMake as '$$' and
1986 for(const char* c
= di
->c_str(); *c
; ++c
)
1988 if(*c
== '$' || *c
== '#')
1997 // Make the definition appear properly on the command line.
1998 defines
+= this->EscapeForShell(di
->c_str(), true);
2003 //----------------------------------------------------------------------------
2005 cmLocalGenerator::ConstructComment(const cmCustomCommand
& cc
,
2006 const char* default_comment
)
2008 // Check for a comment provided with the command.
2011 return cc
.GetComment();
2014 // Construct a reasonable default comment if possible.
2015 if(!cc
.GetOutputs().empty())
2017 std::string comment
;
2018 comment
= "Generating ";
2019 const char* sep
= "";
2020 for(std::vector
<std::string
>::const_iterator o
= cc
.GetOutputs().begin();
2021 o
!= cc
.GetOutputs().end(); ++o
)
2024 comment
+= this->Convert(o
->c_str(), cmLocalGenerator::START_OUTPUT
);
2030 // Otherwise use the provided default.
2031 return default_comment
;
2034 //----------------------------------------------------------------------------
2036 cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote
)
2038 return this->Convert(remote
, START_OUTPUT
, SHELL
, true);
2041 //----------------------------------------------------------------------------
2042 const char* cmLocalGenerator::GetRelativeRootPath(RelativeRoot relroot
)
2046 case HOME
: return this->Makefile
->GetHomeDirectory();
2047 case START
: return this->Makefile
->GetStartDirectory();
2048 case HOME_OUTPUT
: return this->Makefile
->GetHomeOutputDirectory();
2049 case START_OUTPUT
: return this->Makefile
->GetStartOutputDirectory();
2055 //----------------------------------------------------------------------------
2056 std::string
cmLocalGenerator::Convert(const char* source
,
2057 RelativeRoot relative
,
2058 OutputFormat output
,
2061 // Make sure the relative path conversion components are set.
2062 if(!this->PathConversionsSetup
)
2064 this->SetupPathConversions();
2065 this->PathConversionsSetup
= true;
2068 // Convert the path to a relative path.
2069 std::string result
= source
;
2071 if (!optional
|| this->UseRelativePaths
)
2076 //result = cmSystemTools::CollapseFullPath(result.c_str());
2077 result
= this->ConvertToRelativePath(this->HomeDirectoryComponents
,
2081 //result = cmSystemTools::CollapseFullPath(result.c_str());
2082 result
= this->ConvertToRelativePath(this->StartDirectoryComponents
,
2086 //result = cmSystemTools::CollapseFullPath(result.c_str());
2088 this->ConvertToRelativePath(this->HomeOutputDirectoryComponents
,
2092 //result = cmSystemTools::CollapseFullPath(result.c_str());
2094 this->ConvertToRelativePath(this->StartOutputDirectoryComponents
,
2098 result
= cmSystemTools::CollapseFullPath(result
.c_str());
2104 return this->ConvertToOutputFormat(result
.c_str(), output
);
2107 //----------------------------------------------------------------------------
2108 std::string
cmLocalGenerator::ConvertToOutputFormat(const char* source
,
2109 OutputFormat output
)
2111 std::string result
= source
;
2112 // Convert it to an output path.
2113 if (output
== MAKEFILE
)
2115 result
= cmSystemTools::ConvertToOutputPath(result
.c_str());
2117 else if( output
== SHELL
)
2119 // For the MSYS shell convert drive letters to posix paths, so
2120 // that c:/some/path becomes /c/some/path. This is needed to
2121 // avoid problems with the shell path translation.
2122 if(this->MSYSShell
&& !this->LinkScriptShell
)
2124 if(result
.size() > 2 && result
[1] == ':')
2126 result
[1] = result
[0];
2130 if(this->WindowsShell
)
2132 std::string::size_type pos
= 0;
2133 while((pos
= result
.find('/', pos
)) != std::string::npos
)
2139 result
= this->EscapeForShell(result
.c_str(), true, false);
2144 //----------------------------------------------------------------------------
2145 std::string
cmLocalGenerator::Convert(RelativeRoot remote
,
2147 OutputFormat output
,
2150 const char* remotePath
= this->GetRelativeRootPath(remote
);
2152 // The relative root must have a path (i.e. not FULL or NONE)
2153 assert(remotePath
!= 0);
2155 if(local
&& (!optional
|| this->UseRelativePaths
))
2157 std::vector
<std::string
> components
;
2158 cmSystemTools::SplitPath(local
, components
);
2159 std::string result
= this->ConvertToRelativePath(components
, remotePath
);
2160 return this->ConvertToOutputFormat(result
.c_str(), output
);
2164 return this->ConvertToOutputFormat(remotePath
, output
);
2168 //----------------------------------------------------------------------------
2169 std::string
cmLocalGenerator::FindRelativePathTopSource()
2171 // Relative path conversion within a single tree managed by CMake is
2172 // safe. We can use our parent relative path top if and only if
2173 // this is a subdirectory of that top.
2174 if(cmLocalGenerator
* parent
= this->GetParent())
2176 std::string parentTop
= parent
->FindRelativePathTopSource();
2177 if(cmSystemTools::IsSubDirectory(
2178 this->Makefile
->GetStartDirectory(), parentTop
.c_str()))
2184 // Otherwise this directory itself is the new top.
2185 return this->Makefile
->GetStartDirectory();
2188 //----------------------------------------------------------------------------
2189 std::string
cmLocalGenerator::FindRelativePathTopBinary()
2191 // Relative path conversion within a single tree managed by CMake is
2192 // safe. We can use our parent relative path top if and only if
2193 // this is a subdirectory of that top.
2194 if(cmLocalGenerator
* parent
= this->GetParent())
2196 std::string parentTop
= parent
->FindRelativePathTopBinary();
2197 if(cmSystemTools::IsSubDirectory(
2198 this->Makefile
->GetStartOutputDirectory(), parentTop
.c_str()))
2204 // Otherwise this directory itself is the new top.
2205 return this->Makefile
->GetStartOutputDirectory();
2208 //----------------------------------------------------------------------------
2209 void cmLocalGenerator::ConfigureRelativePaths()
2211 // Relative path conversion inside the source tree is not used to
2212 // construct relative paths passed to build tools so it is safe to
2213 // even when the source is a network path.
2214 std::string source
= this->FindRelativePathTopSource();
2215 this->RelativePathTopSource
= source
;
2217 // The current working directory on Windows cannot be a network
2218 // path. Therefore relative paths cannot work when the binary tree
2219 // is a network path.
2220 std::string binary
= this->FindRelativePathTopBinary();
2221 if(binary
.size() < 2 || binary
.substr(0, 2) != "//")
2223 this->RelativePathTopBinary
= binary
;
2227 this->RelativePathTopBinary
= "";
2231 //----------------------------------------------------------------------------
2232 static bool cmLocalGeneratorNotAbove(const char* a
, const char* b
)
2234 return (cmSystemTools::ComparePath(a
, b
) ||
2235 cmSystemTools::IsSubDirectory(a
, b
));
2238 //----------------------------------------------------------------------------
2240 cmLocalGenerator::ConvertToRelativePath(const std::vector
<std::string
>& local
,
2241 const char* in_remote
)
2243 // The path should never be quoted.
2244 assert(in_remote
[0] != '\"');
2246 // The local path should never have a trailing slash.
2247 assert(local
.size() > 0 && !(local
[local
.size()-1] == ""));
2249 // If the path is already relative then just return the path.
2250 if(!cmSystemTools::FileIsFullPath(in_remote
))
2255 // Make sure relative path conversion is configured.
2256 if(!this->RelativePathsConfigured
)
2258 this->ConfigureRelativePaths();
2259 this->RelativePathsConfigured
= true;
2262 // Skip conversion if the path and local are not both in the source
2263 // or both in the binary tree.
2264 std::string local_path
= cmSystemTools::JoinPath(local
);
2265 if(!((cmLocalGeneratorNotAbove(local_path
.c_str(),
2266 this->RelativePathTopBinary
.c_str()) &&
2267 cmLocalGeneratorNotAbove(in_remote
,
2268 this->RelativePathTopBinary
.c_str())) ||
2269 (cmLocalGeneratorNotAbove(local_path
.c_str(),
2270 this->RelativePathTopSource
.c_str()) &&
2271 cmLocalGeneratorNotAbove(in_remote
,
2272 this->RelativePathTopSource
.c_str()))))
2277 // Identify the longest shared path component between the remote
2278 // path and the local path.
2279 std::vector
<std::string
> remote
;
2280 cmSystemTools::SplitPath(in_remote
, remote
);
2281 unsigned int common
=0;
2282 while(common
< remote
.size() &&
2283 common
< local
.size() &&
2284 cmSystemTools::ComparePath(remote
[common
].c_str(),
2285 local
[common
].c_str()))
2290 // If no part of the path is in common then return the full path.
2296 // If the entire path is in common then just return a ".".
2297 if(common
== remote
.size() &&
2298 common
== local
.size())
2303 // If the entire path is in common except for a trailing slash then
2304 // just return a "./".
2305 if(common
+1 == remote
.size() &&
2306 remote
[common
].size() == 0 &&
2307 common
== local
.size())
2312 // Construct the relative path.
2313 std::string relative
;
2315 // First add enough ../ to get up to the level of the shared portion
2316 // of the path. Leave off the trailing slash. Note that the last
2317 // component of local will never be empty because local should never
2318 // have a trailing slash.
2319 for(unsigned int i
=common
; i
< local
.size(); ++i
)
2322 if(i
< local
.size()-1)
2328 // Now add the portion of the destination path that is not included
2329 // in the shared portion of the path. Add a slash the first time
2330 // only if there was already something in the path. If there was a
2331 // trailing slash in the input then the last iteration of the loop
2332 // will add a slash followed by an empty string which will preserve
2333 // the trailing slash in the output.
2334 for(unsigned int i
=common
; i
< remote
.size(); ++i
)
2336 if(relative
.size() > 0)
2340 relative
+= remote
[i
];
2343 // Finally return the path.
2347 //----------------------------------------------------------------------------
2350 ::GenerateTargetInstallRules(
2351 std::ostream
& os
, const char* config
,
2352 std::vector
<std::string
> const& configurationTypes
)
2354 // Convert the old-style install specification from each target to
2355 // an install generator and run it.
2356 cmTargets
& tgts
= this->Makefile
->GetTargets();
2357 for(cmTargets::iterator l
= tgts
.begin(); l
!= tgts
.end(); ++l
)
2359 // Include the user-specified pre-install script for this target.
2360 if(const char* preinstall
= l
->second
.GetProperty("PRE_INSTALL_SCRIPT"))
2362 cmInstallScriptGenerator
g(preinstall
, false, 0);
2363 g
.Generate(os
, config
, configurationTypes
);
2366 // Install this target if a destination is given.
2367 if(l
->second
.GetInstallPath() != "")
2369 // Compute the full install destination. Note that converting
2370 // to unix slashes also removes any trailing slash.
2371 // We also skip over the leading slash given by the user.
2372 std::string destination
= l
->second
.GetInstallPath().substr(1);
2373 cmSystemTools::ConvertToUnixSlashes(destination
);
2374 if(destination
.empty())
2379 // Generate the proper install generator for this target type.
2380 switch(l
->second
.GetType())
2382 case cmTarget::EXECUTABLE
:
2383 case cmTarget::STATIC_LIBRARY
:
2384 case cmTarget::MODULE_LIBRARY
:
2386 // Use a target install generator.
2387 cmInstallTargetGenerator
g(l
->second
, destination
.c_str(), false);
2388 g
.Generate(os
, config
, configurationTypes
);
2391 case cmTarget::SHARED_LIBRARY
:
2393 #if defined(_WIN32) || defined(__CYGWIN__)
2394 // Special code to handle DLL. Install the import library
2395 // to the normal destination and the DLL to the runtime
2397 cmInstallTargetGenerator
g1(l
->second
, destination
.c_str(), true);
2398 g1
.Generate(os
, config
, configurationTypes
);
2399 // We also skip over the leading slash given by the user.
2400 destination
= l
->second
.GetRuntimeInstallPath().substr(1);
2401 cmSystemTools::ConvertToUnixSlashes(destination
);
2402 cmInstallTargetGenerator
g2(l
->second
, destination
.c_str(), false);
2403 g2
.Generate(os
, config
, configurationTypes
);
2405 // Use a target install generator.
2406 cmInstallTargetGenerator
g(l
->second
, destination
.c_str(), false);
2407 g
.Generate(os
, config
, configurationTypes
);
2416 // Include the user-specified post-install script for this target.
2417 if(const char* postinstall
= l
->second
.GetProperty("POST_INSTALL_SCRIPT"))
2419 cmInstallScriptGenerator
g(postinstall
, false, 0);
2420 g
.Generate(os
, config
, configurationTypes
);
2425 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2426 static std::string
cmLocalGeneratorMD5(const char* input
)
2429 cmsysMD5
* md5
= cmsysMD5_New();
2430 cmsysMD5_Initialize(md5
);
2431 cmsysMD5_Append(md5
, reinterpret_cast<unsigned char const*>(input
), -1);
2432 cmsysMD5_FinalizeHex(md5
, md5out
);
2433 cmsysMD5_Delete(md5
);
2434 return std::string(md5out
, 32);
2438 cmLocalGeneratorShortenObjectName(std::string
& objName
,
2439 std::string::size_type max_len
)
2441 // Replace the beginning of the path portion of the object name with
2443 std::string::size_type pos
= objName
.find('/', objName
.size()-max_len
+32);
2444 if(pos
!= objName
.npos
)
2446 std::string md5name
= cmLocalGeneratorMD5(objName
.substr(0, pos
).c_str());
2447 md5name
+= objName
.substr(pos
);
2450 // The object name is now short enough.
2455 // The object name could not be shortened enough.
2461 bool cmLocalGeneratorCheckObjectName(std::string
& objName
,
2462 std::string::size_type dir_len
,
2463 std::string::size_type max_total_len
)
2465 // Enforce the maximum file name length if possible.
2466 std::string::size_type max_obj_len
= max_total_len
;
2467 if(dir_len
< max_total_len
)
2469 max_obj_len
= max_total_len
- dir_len
;
2470 if(objName
.size() > max_obj_len
)
2472 // The current object file name is too long. Try to shorten it.
2473 return cmLocalGeneratorShortenObjectName(objName
, max_obj_len
);
2477 // The object file name is short enough.
2483 // The build directory in which the object will be stored is
2484 // already too deep.
2490 //----------------------------------------------------------------------------
2493 ::CreateSafeUniqueObjectFileName(const char* sin
,
2494 std::string
const& dir_max
)
2496 // Look for an existing mapped name for this object file.
2497 std::map
<cmStdString
,cmStdString
>::iterator it
=
2498 this->UniqueObjectNamesMap
.find(sin
);
2500 // If no entry exists create one.
2501 if(it
== this->UniqueObjectNamesMap
.end())
2503 // Start with the original name.
2504 std::string ssin
= sin
;
2506 // Avoid full paths by removing leading slashes.
2507 std::string::size_type pos
= 0;
2508 for(;pos
< ssin
.size() && ssin
[pos
] == '/'; ++pos
)
2511 ssin
= ssin
.substr(pos
);
2513 // Avoid full paths by removing colons.
2514 cmSystemTools::ReplaceString(ssin
, ":", "_");
2516 // Avoid relative paths that go up the tree.
2517 cmSystemTools::ReplaceString(ssin
, "../", "__/");
2520 cmSystemTools::ReplaceString(ssin
, " ", "_");
2522 // Mangle the name if necessary.
2523 if(this->Makefile
->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
2528 sprintf(rpstr
, "_p_");
2529 cmSystemTools::ReplaceString(ssin
, "+", rpstr
);
2530 std::string sssin
= sin
;
2534 for ( it
= this->UniqueObjectNamesMap
.begin();
2535 it
!= this->UniqueObjectNamesMap
.end();
2538 if ( it
->second
== ssin
)
2548 cmSystemTools::ReplaceString(ssin
, "_p_", rpstr
);
2549 sprintf(rpstr
, "_p%d_", cc
++);
2554 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2555 if(!cmLocalGeneratorCheckObjectName(ssin
, dir_max
.size(),
2556 this->ObjectPathMax
))
2558 // Warn if this is the first time the path has been seen.
2559 if(this->ObjectMaxPathViolations
.insert(dir_max
).second
)
2562 m
<< "The object file directory\n"
2563 << " " << dir_max
<< "\n"
2564 << "has " << dir_max
.size() << " characters. "
2565 << "The maximum full path to an object file is "
2566 << this->ObjectPathMax
<< " characters "
2567 << "(see CMAKE_OBJECT_PATH_MAX). "
2569 << " " << ssin
<< "\n"
2570 << "cannot be safely placed under this directory. "
2571 << "The build may not work correctly.";
2572 this->Makefile
->IssueMessage(cmake::WARNING
, m
.str());
2579 // Insert the newly mapped object file name.
2580 std::map
<cmStdString
, cmStdString
>::value_type
e(sin
, ssin
);
2581 it
= this->UniqueObjectNamesMap
.insert(e
).first
;
2584 // Return the map entry.
2588 //----------------------------------------------------------------------------
2591 ::GetObjectFileNameWithoutTarget(const cmSourceFile
& source
,
2592 std::string
const& dir_max
,
2593 bool* hasSourceExtension
)
2595 // Construct the object file name using the full path to the source
2596 // file which is its only unique identification.
2597 const char* fullPath
= source
.GetFullPath().c_str();
2599 // Try referencing the source relative to the source tree.
2600 std::string relFromSource
= this->Convert(fullPath
, START
);
2601 assert(!relFromSource
.empty());
2602 bool relSource
= !cmSystemTools::FileIsFullPath(relFromSource
.c_str());
2603 bool subSource
= relSource
&& relFromSource
[0] != '.';
2605 // Try referencing the source relative to the binary tree.
2606 std::string relFromBinary
= this->Convert(fullPath
, START_OUTPUT
);
2607 assert(!relFromBinary
.empty());
2608 bool relBinary
= !cmSystemTools::FileIsFullPath(relFromBinary
.c_str());
2609 bool subBinary
= relBinary
&& relFromBinary
[0] != '.';
2611 // Select a nice-looking reference to the source file to construct
2612 // the object file name.
2613 std::string objectName
;
2614 if((relSource
&& !relBinary
) || (subSource
&& !subBinary
))
2616 objectName
= relFromSource
;
2618 else if((relBinary
&& !relSource
) || (subBinary
&& !subSource
))
2620 objectName
= relFromBinary
;
2622 else if(relFromBinary
.length() < relFromSource
.length())
2624 objectName
= relFromBinary
;
2628 objectName
= relFromSource
;
2631 // if it is still a full path check for the try compile case
2632 // try compile never have in source sources, and should not
2633 // have conflicting source file names in the same target
2634 if(cmSystemTools::FileIsFullPath(objectName
.c_str()))
2636 if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
2638 objectName
= cmSystemTools::GetFilenameName(source
.GetFullPath());
2642 // Replace the original source file extension with the object file
2644 bool keptSourceExtension
= true;
2645 if(!source
.GetPropertyAsBool("KEEP_EXTENSION"))
2647 // Decide whether this language wants to replace the source
2648 // extension with the object extension. For CMake 2.4
2649 // compatibility do this by default.
2650 bool replaceExt
= this->NeedBackwardsCompatibility(2, 4);
2653 std::string repVar
= "CMAKE_";
2654 repVar
+= source
.GetLanguage();
2655 repVar
+= "_OUTPUT_EXTENSION_REPLACE";
2656 replaceExt
= this->Makefile
->IsOn(repVar
.c_str());
2659 // Remove the source extension if it is to be replaced.
2662 keptSourceExtension
= false;
2663 std::string::size_type dot_pos
= objectName
.rfind(".");
2664 if(dot_pos
!= std::string::npos
)
2666 objectName
= objectName
.substr(0, dot_pos
);
2670 // Store the new extension.
2672 this->GlobalGenerator
->GetLanguageOutputExtension(source
);
2674 if(hasSourceExtension
)
2676 *hasSourceExtension
= keptSourceExtension
;
2679 // Convert to a safe name.
2680 return this->CreateSafeUniqueObjectFileName(objectName
.c_str(), dir_max
);
2683 //----------------------------------------------------------------------------
2686 ::GetSourceFileLanguage(const cmSourceFile
& source
)
2688 return source
.GetLanguage();
2691 //----------------------------------------------------------------------------
2692 std::string
cmLocalGenerator::EscapeForShellOldStyle(const char* str
)
2695 bool forceOn
= cmSystemTools::GetForceUnixPaths();
2696 if(forceOn
&& this->WindowsShell
)
2698 cmSystemTools::SetForceUnixPaths(false);
2700 result
= cmSystemTools::EscapeSpaces(str
);
2701 if(forceOn
&& this->WindowsShell
)
2703 cmSystemTools::SetForceUnixPaths(true);
2708 //----------------------------------------------------------------------------
2709 static bool cmLocalGeneratorIsShellOperator(const char* str
)
2711 if(strcmp(str
, "<") == 0 ||
2712 strcmp(str
, ">") == 0 ||
2713 strcmp(str
, "<<") == 0 ||
2714 strcmp(str
, ">>") == 0 ||
2715 strcmp(str
, "|") == 0 ||
2716 strcmp(str
, "||") == 0 ||
2717 strcmp(str
, "&&") == 0 ||
2718 strcmp(str
, "&>") == 0 ||
2719 strcmp(str
, "1>") == 0 ||
2720 strcmp(str
, "2>") == 0 ||
2721 strcmp(str
, "2>&1") == 0 ||
2722 strcmp(str
, "1>&2") == 0)
2729 //----------------------------------------------------------------------------
2730 std::string
cmLocalGenerator::EscapeForShell(const char* str
, bool makeVars
,
2733 // Do not escape shell operators.
2734 if(cmLocalGeneratorIsShellOperator(str
))
2739 // Compute the flags for the target shell environment.
2741 if(this->WindowsVSIDE
)
2743 flags
|= cmsysSystem_Shell_Flag_VSIDE
;
2745 else if(!this->LinkScriptShell
)
2747 flags
|= cmsysSystem_Shell_Flag_Make
;
2751 flags
|= cmsysSystem_Shell_Flag_AllowMakeVariables
;
2755 flags
|= cmsysSystem_Shell_Flag_EchoWindows
;
2757 if(this->WatcomWMake
)
2759 flags
|= cmsysSystem_Shell_Flag_WatcomWMake
;
2763 flags
|= cmsysSystem_Shell_Flag_MinGWMake
;
2767 flags
|= cmsysSystem_Shell_Flag_NMake
;
2770 // Compute the buffer size needed.
2771 int size
= (this->WindowsShell
?
2772 cmsysSystem_Shell_GetArgumentSizeForWindows(str
, flags
) :
2773 cmsysSystem_Shell_GetArgumentSizeForUnix(str
, flags
));
2775 // Compute the shell argument itself.
2776 std::vector
<char> arg(size
);
2777 if(this->WindowsShell
)
2779 cmsysSystem_Shell_GetArgumentForWindows(str
, &arg
[0], flags
);
2783 cmsysSystem_Shell_GetArgumentForUnix(str
, &arg
[0], flags
);
2785 return std::string(&arg
[0]);
2788 //----------------------------------------------------------------------------
2789 std::string
cmLocalGenerator::EscapeForCMake(const char* str
)
2791 // Always double-quote the argument to take care of most escapes.
2792 std::string result
= "\"";
2793 for(const char* c
= str
; *c
; ++c
)
2797 // Escape the double quote to avoid ending the argument.
2802 // Escape the dollar to avoid expanding variables.
2807 // Escape the backslash to avoid other escapes.
2812 // Other characters will be parsed correctly.
2820 //----------------------------------------------------------------------------
2822 cmLocalGenerator::GetTargetDirectory(cmTarget
const&) const
2824 cmSystemTools::Error("GetTargetDirectory"
2825 " called on cmLocalGenerator");
2830 //----------------------------------------------------------------------------
2832 cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget
* ,
2833 std::vector
<std::string
>&
2836 cmSystemTools::Error("GetTargetObjectFileDirectories"
2837 " called on cmLocalGenerator");
2840 //----------------------------------------------------------------------------
2841 unsigned int cmLocalGenerator::GetBackwardsCompatibility()
2843 // The computed version may change until the project is fully
2845 if(!this->BackwardsCompatibilityFinal
)
2847 unsigned int major
= 0;
2848 unsigned int minor
= 0;
2849 unsigned int patch
= 0;
2850 if(const char* value
2851 = this->Makefile
->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
2853 switch(sscanf(value
, "%u.%u.%u", &major
, &minor
, &patch
))
2855 case 2: patch
= 0; break;
2856 case 1: minor
= 0; patch
= 0; break;
2860 this->BackwardsCompatibility
= CMake_VERSION_ENCODE(major
, minor
, patch
);
2861 this->BackwardsCompatibilityFinal
= this->Configured
;
2864 return this->BackwardsCompatibility
;
2867 //----------------------------------------------------------------------------
2868 bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major
,
2872 // Check the policy to decide whether to pay attention to this
2874 switch(this->Makefile
->GetPolicyStatus(cmPolicies::CMP0001
))
2876 case cmPolicies::WARN
:
2877 // WARN is just OLD without warning because user code does not
2878 // always affect whether this check is done.
2879 case cmPolicies::OLD
:
2880 // Old behavior is to check the variable.
2882 case cmPolicies::NEW
:
2883 // New behavior is to ignore the variable.
2885 case cmPolicies::REQUIRED_IF_USED
:
2886 case cmPolicies::REQUIRED_ALWAYS
:
2887 // This will never be the case because the only way to require
2888 // the setting is to require the user to specify version policy
2889 // 2.6 or higher. Once we add that requirement then this whole
2890 // method can be removed anyway.
2894 // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
2895 // equal to or lower than the given version.
2896 unsigned int actual_compat
= this->GetBackwardsCompatibility();
2897 return (actual_compat
&&
2898 actual_compat
<= CMake_VERSION_ENCODE(major
, minor
, patch
));
2901 //----------------------------------------------------------------------------
2902 bool cmLocalGenerator::CheckDefinition(std::string
const& define
) const
2904 // Many compilers do not support -DNAME(arg)=sdf so we disable it.
2905 bool function_style
= false;
2906 for(const char* c
= define
.c_str(); *c
&& *c
!= '='; ++c
)
2910 function_style
= true;
2917 e
<< "WARNING: Function-style preprocessor definitions may not be "
2918 << "passed on the compiler command line because many compilers "
2919 << "do not support it.\n"
2920 << "CMake is dropping a preprocessor definition: " << define
<< "\n"
2921 << "Consider defining the macro in a (configured) header file.\n";
2922 cmSystemTools::Message(e
.str().c_str());
2926 // Many compilers do not support # in the value so we disable it.
2927 if(define
.find_first_of("#") != define
.npos
)
2930 e
<< "WARNING: Peprocessor definitions containing '#' may not be "
2931 << "passed on the compiler command line because many compilers "
2932 << "do not support it.\n"
2933 << "CMake is dropping a preprocessor definition: " << define
<< "\n"
2934 << "Consider defining the macro in a (configured) header file.\n";
2935 cmSystemTools::Message(e
.str().c_str());
2939 // Assume it is supported.
2943 //----------------------------------------------------------------------------
2944 static void cmLGInfoProp(cmMakefile
* mf
, cmTarget
* target
, const char* prop
)
2946 if(const char* val
= target
->GetProperty(prop
))
2948 mf
->AddDefinition(prop
, val
);
2952 //----------------------------------------------------------------------------
2953 void cmLocalGenerator::GenerateAppleInfoPList(cmTarget
* target
,
2954 const char* targetName
,
2957 // Find the Info.plist template.
2958 const char* in
= target
->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
2959 std::string inFile
= (in
&& *in
)? in
: "MacOSXBundleInfo.plist.in";
2960 if(!cmSystemTools::FileIsFullPath(inFile
.c_str()))
2962 std::string inMod
= this->Makefile
->GetModulesFile(inFile
.c_str());
2968 if(!cmSystemTools::FileExists(inFile
.c_str(), true))
2971 e
<< "Target " << target
->GetName() << " Info.plist template \""
2972 << inFile
<< "\" could not be found.";
2973 cmSystemTools::Error(e
.str().c_str());
2977 // Convert target properties to variables in an isolated makefile
2978 // scope to configure the file. If properties are set they will
2979 // override user make variables. If not the configuration will fall
2980 // back to the directory-level values set by the user.
2981 cmMakefile
* mf
= this->Makefile
;
2983 mf
->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName
);
2984 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_INFO_STRING");
2985 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_ICON_FILE");
2986 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_GUI_IDENTIFIER");
2987 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_LONG_VERSION_STRING");
2988 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_BUNDLE_NAME");
2989 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
2990 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_BUNDLE_VERSION");
2991 cmLGInfoProp(mf
, target
, "MACOSX_BUNDLE_COPYRIGHT");
2992 mf
->ConfigureFile(inFile
.c_str(), fname
, false, false, false);
2996 //----------------------------------------------------------------------------
2997 void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget
* target
,
2998 const char* targetName
,
3001 // Find the Info.plist template.
3002 const char* in
= target
->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
3003 std::string inFile
= (in
&& *in
)? in
: "MacOSXFrameworkInfo.plist.in";
3004 if(!cmSystemTools::FileIsFullPath(inFile
.c_str()))
3006 std::string inMod
= this->Makefile
->GetModulesFile(inFile
.c_str());
3012 if(!cmSystemTools::FileExists(inFile
.c_str(), true))
3015 e
<< "Target " << target
->GetName() << " Info.plist template \""
3016 << inFile
<< "\" could not be found.";
3017 cmSystemTools::Error(e
.str().c_str());
3021 // Convert target properties to variables in an isolated makefile
3022 // scope to configure the file. If properties are set they will
3023 // override user make variables. If not the configuration will fall
3024 // back to the directory-level values set by the user.
3025 cmMakefile
* mf
= this->Makefile
;
3027 mf
->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName
);
3028 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_ICON_FILE");
3029 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_IDENTIFIER");
3030 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
3031 cmLGInfoProp(mf
, target
, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
3032 mf
->ConfigureFile(inFile
.c_str(), fname
, false, false, false);