ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Source / cmLocalGenerator.cxx
blob1a7eb84b62663ccb4e818bc5a2d8105a5e8e2841
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-23 18:30:55 $
7 Version: $Revision: 1.260 $
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"
28 #include "cmTest.h"
29 #include "cmake.h"
31 #if defined(CMAKE_BUILD_WITH_CMAKE)
32 # define CM_LG_ENCODE_OBJECT_NAMES
33 # include <cmsys/MD5.h>
34 #endif
36 #include <cmsys/System.h>
38 #include <ctype.h> // for isalpha
40 #include <assert.h>
42 cmLocalGenerator::cmLocalGenerator()
44 this->Makefile = new cmMakefile;
45 this->Makefile->SetLocalGenerator(this);
46 this->Parent = 0;
47 this->WindowsShell = false;
48 this->WindowsVSIDE = false;
49 this->WatcomWMake = false;
50 this->MinGWMake = false;
51 this->NMake = false;
52 this->MSYSShell = false;
53 this->IgnoreLibPrefix = false;
54 this->UseRelativePaths = false;
55 this->Configured = false;
56 this->EmitUniversalBinaryFlags = true;
57 this->IsMakefileGenerator = false;
58 this->RelativePathsConfigured = false;
59 this->PathConversionsSetup = false;
60 this->BackwardsCompatibility = 0;
61 this->BackwardsCompatibilityFinal = false;
64 cmLocalGenerator::~cmLocalGenerator()
66 delete this->Makefile;
69 void cmLocalGenerator::Configure()
71 cmLocalGenerator* previousLg =
72 this->GetGlobalGenerator()->GetCurrentLocalGenerator();
73 this->GetGlobalGenerator()->SetCurrentLocalGenerator(this);
75 // make sure the CMakeFiles dir is there
76 std::string filesDir = this->Makefile->GetStartOutputDirectory();
77 filesDir += cmake::GetCMakeFilesDirectory();
78 cmSystemTools::MakeDirectory(filesDir.c_str());
80 // find & read the list file
81 std::string currentStart = this->Makefile->GetStartDirectory();
82 currentStart += "/CMakeLists.txt";
83 this->Makefile->ReadListFile(currentStart.c_str());
85 // at the end of the ReadListFile handle any old style subdirs
86 // first get all the subdirectories
87 std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
89 // for each subdir recurse
90 std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
91 for (; sdi != subdirs.end(); ++sdi)
93 if (!(*sdi)->Configured)
95 this->Makefile->ConfigureSubDirectory(*sdi);
99 // Check whether relative paths should be used for optionally
100 // relative paths.
101 this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
103 this->Configured = true;
105 this->GetGlobalGenerator()->SetCurrentLocalGenerator(previousLg);
108 void cmLocalGenerator::SetupPathConversions()
110 // Setup the current output directory components for use by
111 // Convert
112 std::string outdir;
113 outdir =
114 cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
115 cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
116 outdir =
117 cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
118 cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
120 outdir = cmSystemTools::CollapseFullPath
121 (this->Makefile->GetHomeOutputDirectory());
122 cmSystemTools::SplitPath(outdir.c_str(),
123 this->HomeOutputDirectoryComponents);
125 outdir = cmSystemTools::CollapseFullPath
126 (this->Makefile->GetStartOutputDirectory());
127 cmSystemTools::SplitPath(outdir.c_str(),
128 this->StartOutputDirectoryComponents);
132 void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
134 this->GlobalGenerator = gg;
136 // setup the home directories
137 this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
138 this->Makefile->SetHomeDirectory(
139 gg->GetCMakeInstance()->GetHomeDirectory());
140 this->Makefile->SetHomeOutputDirectory(
141 gg->GetCMakeInstance()->GetHomeOutputDirectory());
144 void cmLocalGenerator::ConfigureFinalPass()
146 this->Makefile->ConfigureFinalPass();
149 void cmLocalGenerator::TraceDependencies()
151 // Generate the rule files for each target.
152 cmTargets& targets = this->Makefile->GetTargets();
153 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
155 // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
156 // so don't build a projectfile for it
157 if (strncmp(t->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
159 const char* projectFilename = 0;
160 if (this->IsMakefileGenerator == false) // only use of this variable
162 projectFilename = t->second.GetName();
164 t->second.TraceDependencies(projectFilename);
169 void cmLocalGenerator::GenerateTestFiles()
171 if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
173 return;
175 std::string file = this->Makefile->GetStartOutputDirectory();
176 file += "/";
177 file += "CTestTestfile.cmake";
179 cmGeneratedFileStream fout(file.c_str());
180 fout.SetCopyIfDifferent(true);
182 fout << "# CMake generated Testfile for " << std::endl
183 << "# Source directory: "
184 << this->Makefile->GetStartDirectory() << std::endl
185 << "# Build directory: "
186 << this->Makefile->GetStartOutputDirectory() << std::endl
187 << "# " << std::endl
188 << "# This file replicates the SUBDIRS() and ADD_TEST() commands "
189 << "from the source" << std::endl
190 << "# tree CMakeLists.txt file, skipping any SUBDIRS() or "
191 << "ADD_TEST() commands" << std::endl
192 << "# that are excluded by CMake control structures, i.e. IF() "
193 << "commands." << std::endl;
195 const char* testIncludeFile =
196 this->Makefile->GetProperty("TEST_INCLUDE_FILE");
197 if ( testIncludeFile )
199 fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
202 const std::vector<cmTest*> *tests = this->Makefile->GetTests();
203 std::vector<cmTest*>::const_iterator it;
204 for ( it = tests->begin(); it != tests->end(); ++ it )
206 cmTest* test = *it;
207 fout << "ADD_TEST(";
208 fout << test->GetName() << " \"" << test->GetCommand() << "\"";
210 std::vector<cmStdString>::const_iterator argit;
211 for (argit = test->GetArguments().begin();
212 argit != test->GetArguments().end(); ++argit)
214 // Just double-quote all arguments so they are re-parsed
215 // correctly by the test system.
216 fout << " \"";
217 for(std::string::const_iterator c = argit->begin();
218 c != argit->end(); ++c)
220 // Escape quotes within arguments. We should escape
221 // backslashes too but we cannot because it makes the result
222 // inconsistent with previous behavior of this command.
223 if((*c == '"'))
225 fout << '\\';
227 fout << *c;
229 fout << "\"";
231 fout << ")" << std::endl;
232 cmPropertyMap::const_iterator pit;
233 cmPropertyMap* mpit = &test->GetProperties();
234 if ( mpit->size() )
236 fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
237 for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
239 fout << " " << pit->first.c_str() << " \"";
240 const char* value = pit->second.GetValue();
241 for ( ; *value; ++ value )
243 switch ( *value )
245 case '\\':
246 case '"':
247 case ' ':
248 case '#':
249 case '(':
250 case ')':
251 case '$':
252 case '^':
253 fout << "\\" << *value;
254 break;
255 case '\t':
256 fout << "\\t";
257 break;
258 case '\n':
259 fout << "\\n";
260 break;
261 case '\r':
262 fout << "\\r";
263 break;
264 default:
265 fout << *value;
268 fout << "\"";
270 fout << ")" << std::endl;
273 if ( this->Children.size())
275 size_t i;
276 for(i = 0; i < this->Children.size(); ++i)
278 fout << "SUBDIRS(";
279 std::string outP =
280 this->Children[i]->GetMakefile()->GetStartOutputDirectory();
281 fout << this->Convert(outP.c_str(),START_OUTPUT);
282 fout << ")" << std::endl;
287 //----------------------------------------------------------------------------
288 void cmLocalGenerator::GenerateInstallRules()
290 // Compute the install prefix.
291 const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
292 #if defined(_WIN32) && !defined(__CYGWIN__)
293 std::string prefix_win32;
294 if(!prefix)
296 if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
298 prefix_win32 = "C:";
300 const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
301 if(project_name && project_name[0])
303 prefix_win32 += "/Program Files/";
304 prefix_win32 += project_name;
306 else
308 prefix_win32 += "/InstalledCMakeProject";
310 prefix = prefix_win32.c_str();
312 #else
313 if (!prefix)
315 prefix = "/usr/local";
317 #endif
319 // Compute the set of configurations.
320 std::vector<std::string> configurationTypes;
321 if(const char* types =
322 this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
324 cmSystemTools::ExpandListArgument(types, configurationTypes);
326 const char* config = 0;
327 if(configurationTypes.empty())
329 config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
332 // Choose a default install configuration.
333 const char* default_config = config;
334 const char* default_order[] = {"RELEASE", "MINSIZEREL",
335 "RELWITHDEBINFO", "DEBUG", 0};
336 for(const char** c = default_order; *c && !default_config; ++c)
338 for(std::vector<std::string>::iterator i = configurationTypes.begin();
339 i != configurationTypes.end(); ++i)
341 if(cmSystemTools::UpperCase(*i) == *c)
343 default_config = i->c_str();
347 if(!default_config && !configurationTypes.empty())
349 default_config = configurationTypes[0].c_str();
351 if(!default_config)
353 default_config = "Release";
356 // Create the install script file.
357 std::string file = this->Makefile->GetStartOutputDirectory();
358 std::string homedir = this->Makefile->GetHomeOutputDirectory();
359 std::string currdir = this->Makefile->GetCurrentOutputDirectory();
360 cmSystemTools::ConvertToUnixSlashes(file);
361 cmSystemTools::ConvertToUnixSlashes(homedir);
362 cmSystemTools::ConvertToUnixSlashes(currdir);
363 int toplevel_install = 0;
364 if ( currdir == homedir )
366 toplevel_install = 1;
368 file += "/cmake_install.cmake";
369 cmGeneratedFileStream fout(file.c_str());
370 fout.SetCopyIfDifferent(true);
372 // Write the header.
373 fout << "# Install script for directory: "
374 << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
375 fout << "# Set the install prefix" << std::endl
376 << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
377 << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
378 << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
379 << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
380 << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
381 << std::endl;
383 // Write support code for generating per-configuration install rules.
384 fout <<
385 "# Set the install configuration name.\n"
386 "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
387 " IF(BUILD_TYPE)\n"
388 " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
389 " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
390 " ELSE(BUILD_TYPE)\n"
391 " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
392 " ENDIF(BUILD_TYPE)\n"
393 " MESSAGE(STATUS \"Install configuration: "
394 "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
395 "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
396 "\n";
398 // Write support code for dealing with component-specific installs.
399 fout <<
400 "# Set the component getting installed.\n"
401 "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
402 " IF(COMPONENT)\n"
403 " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
404 " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
405 " ELSE(COMPONENT)\n"
406 " SET(CMAKE_INSTALL_COMPONENT)\n"
407 " ENDIF(COMPONENT)\n"
408 "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
409 "\n";
411 // Copy user-specified install options to the install code.
412 if(const char* so_no_exe =
413 this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
415 fout <<
416 "# Install shared libraries without execute permission?\n"
417 "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
418 " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
419 "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
420 "\n";
423 // Ask each install generator to write its code.
424 std::vector<cmInstallGenerator*> const& installers =
425 this->Makefile->GetInstallGenerators();
426 for(std::vector<cmInstallGenerator*>::const_iterator
427 gi = installers.begin();
428 gi != installers.end(); ++gi)
430 (*gi)->Generate(fout, config, configurationTypes);
433 // Write rules from old-style specification stored in targets.
434 this->GenerateTargetInstallRules(fout, config, configurationTypes);
436 // Include install scripts from subdirectories.
437 if(!this->Children.empty())
439 fout << "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
440 fout << " # Include the install script for each subdirectory.\n";
441 for(std::vector<cmLocalGenerator*>::const_iterator
442 ci = this->Children.begin(); ci != this->Children.end(); ++ci)
444 if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
446 std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
447 cmSystemTools::ConvertToUnixSlashes(odir);
448 fout << " INCLUDE(\"" << odir.c_str()
449 << "/cmake_install.cmake\")" << std::endl;
452 fout << "\n";
453 fout << "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
456 // Record the install manifest.
457 if ( toplevel_install )
459 fout <<
460 "IF(CMAKE_INSTALL_COMPONENT)\n"
461 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
462 "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
463 "ELSE(CMAKE_INSTALL_COMPONENT)\n"
464 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
465 "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
466 fout
467 << "FILE(WRITE \""
468 << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
469 << "\"\")" << std::endl;
470 fout
471 << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
472 << " FILE(APPEND \""
473 << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
474 << "\"${file}\\n\")" << std::endl
475 << "ENDFOREACH(file)" << std::endl;
479 //----------------------------------------------------------------------------
480 void cmLocalGenerator::GenerateTargetManifest()
482 // Collect the set of configuration types.
483 std::vector<std::string> configNames;
484 if(const char* configurationTypes =
485 this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
487 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
489 else if(const char* buildType =
490 this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
492 if(*buildType)
494 configNames.push_back(buildType);
498 // Add our targets to the manifest for each configuration.
499 cmTargets& targets = this->Makefile->GetTargets();
500 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
502 cmTarget& target = t->second;
503 if(configNames.empty())
505 target.GenerateTargetManifest(0);
507 else
509 for(std::vector<std::string>::iterator ci = configNames.begin();
510 ci != configNames.end(); ++ci)
512 const char* config = ci->c_str();
513 target.GenerateTargetManifest(config);
519 void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
520 const char* lang,
521 cmSourceFile& source,
522 cmTarget& )
524 std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
525 objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
526 std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
527 std::string sourceFile =
528 this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
529 std::string varString = "CMAKE_";
530 varString += lang;
531 varString += "_COMPILE_OBJECT";
532 std::vector<std::string> rules;
533 rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
534 varString = "CMAKE_";
535 varString += lang;
536 varString += "_FLAGS";
537 std::string flags;
538 flags += this->Makefile->GetSafeDefinition(varString.c_str());
539 flags += " ";
540 flags += this->GetIncludeFlags(lang);
542 // Construct the command lines.
543 cmCustomCommandLines commandLines;
544 std::vector<std::string> commands;
545 cmSystemTools::ExpandList(rules, commands);
546 cmLocalGenerator::RuleVariables vars;
547 vars.Language = lang;
548 vars.Source = sourceFile.c_str();
549 vars.Object = objectFile.c_str();
550 vars.ObjectDir = objectDir.c_str();
551 vars.Flags = flags.c_str();
552 for(std::vector<std::string>::iterator i = commands.begin();
553 i != commands.end(); ++i)
555 // Expand the full command line string.
556 this->ExpandRuleVariables(*i, vars);
558 // Parse the string to get the custom command line.
559 cmCustomCommandLine commandLine;
560 std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
561 for(std::vector<cmStdString>::iterator a = cmd.begin();
562 a != cmd.end(); ++a)
564 commandLine.push_back(*a);
567 // Store this command line.
568 commandLines.push_back(commandLine);
571 // Check for extra object-file dependencies.
572 std::vector<std::string> depends;
573 const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
574 if(additionalDeps)
576 cmSystemTools::ExpandListArgument(additionalDeps, depends);
579 // Generate a meaningful comment for the command.
580 std::string comment = "Building ";
581 comment += lang;
582 comment += " object ";
583 comment += this->Convert(ofname, START_OUTPUT);
585 // Add the custom command to build the object file.
586 this->Makefile->AddCustomCommandToOutput(
587 ofname,
588 depends,
589 source.GetFullPath().c_str(),
590 commandLines,
591 comment.c_str(),
592 this->Makefile->GetStartOutputDirectory()
596 void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
598 cmStdString objs;
599 std::vector<std::string> objVector;
600 // Add all the sources outputs to the depends of the target
601 std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
602 for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
603 i != classes.end(); ++i)
605 cmSourceFile* sf = *i;
606 if(!sf->GetCustomCommand() &&
607 !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
608 !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
610 std::string::size_type dir_len = 0;
611 dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
612 dir_len += 1;
613 std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_len);
614 if(!obj.empty())
616 std::string ofname = this->Makefile->GetCurrentOutputDirectory();
617 ofname += "/";
618 ofname += obj;
619 objVector.push_back(ofname);
620 this->AddCustomCommandToCreateObject(ofname.c_str(),
621 llang, *(*i), target);
622 objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
623 objs += " ";
627 std::string createRule = "CMAKE_";
628 createRule += llang;
629 createRule += target.GetCreateRuleVariable();
630 std::string targetName = target.GetFullName();
631 // Executable :
632 // Shared Library:
633 // Static Library:
634 // Shared Module:
635 std::string linkLibs; // should be set
636 std::string flags; // should be set
637 std::string linkFlags; // should be set
638 this->GetTargetFlags(linkLibs, flags, linkFlags, target);
639 cmLocalGenerator::RuleVariables vars;
640 vars.Language = llang;
641 vars.Objects = objs.c_str();
642 vars.ObjectDir = ".";
643 vars.Target = targetName.c_str();
644 vars.LinkLibraries = linkLibs.c_str();
645 vars.Flags = flags.c_str();
646 vars.LinkFlags = linkFlags.c_str();
648 std::string langFlags;
649 this->AddLanguageFlags(langFlags, llang, 0);
650 vars.LanguageCompileFlags = langFlags.c_str();
652 cmCustomCommandLines commandLines;
653 std::vector<std::string> rules;
654 rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
655 std::vector<std::string> commands;
656 cmSystemTools::ExpandList(rules, commands);
657 for(std::vector<std::string>::iterator i = commands.begin();
658 i != commands.end(); ++i)
660 // Expand the full command line string.
661 this->ExpandRuleVariables(*i, vars);
662 // Parse the string to get the custom command line.
663 cmCustomCommandLine commandLine;
664 std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
665 for(std::vector<cmStdString>::iterator a = cmd.begin();
666 a != cmd.end(); ++a)
668 commandLine.push_back(*a);
671 // Store this command line.
672 commandLines.push_back(commandLine);
674 std::string targetFullPath = target.GetFullPath();
675 // Generate a meaningful comment for the command.
676 std::string comment = "Linking ";
677 comment += llang;
678 comment += " target ";
679 comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
680 this->Makefile->AddCustomCommandToOutput(
681 targetFullPath.c_str(),
682 objVector,
684 commandLines,
685 comment.c_str(),
686 this->Makefile->GetStartOutputDirectory()
688 target.AddSourceFile
689 (this->Makefile->GetSource(targetFullPath.c_str()));
693 void cmLocalGenerator
694 ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
696 cmTargets &tgts = this->Makefile->GetTargets();
697 for(cmTargets::iterator l = tgts.begin();
698 l != tgts.end(); l++)
700 cmTarget& target = l->second;
701 switch(target.GetType())
703 case cmTarget::STATIC_LIBRARY:
704 case cmTarget::SHARED_LIBRARY:
705 case cmTarget::MODULE_LIBRARY:
706 case cmTarget::EXECUTABLE:
708 const char* llang =
709 target.GetLinkerLanguage(this->GetGlobalGenerator());
710 if(!llang)
712 cmSystemTools::Error
713 ("CMake can not determine linker language for target:",
714 target.GetName());
715 return;
717 // if the language is not in the set lang then create custom
718 // commands to build the target
719 if(lang.count(llang) == 0)
721 this->AddBuildTargetRule(llang, target);
724 break;
725 case cmTarget::UTILITY:
726 case cmTarget::GLOBAL_TARGET:
727 case cmTarget::INSTALL_FILES:
728 case cmTarget::INSTALL_PROGRAMS:
729 case cmTarget::INSTALL_DIRECTORY:
730 break;
735 // List of variables that are replaced when
736 // rules are expanced. These variables are
737 // replaced in the form <var> with GetSafeDefinition(var).
738 // ${LANG} is replaced in the variable first with all enabled
739 // languages.
740 static const char* ruleReplaceVars[] =
742 "CMAKE_${LANG}_COMPILER",
743 "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
744 "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
745 "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
746 "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
747 "CMAKE_${LANG}_LINK_FLAGS",
748 "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
749 "CMAKE_${LANG}_ARCHIVE",
750 "CMAKE_AR",
751 "CMAKE_CURRENT_SOURCE_DIR",
752 "CMAKE_CURRENT_BINARY_DIR",
753 "CMAKE_RANLIB",
754 "CMAKE_LINKER",
758 std::string
759 cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
760 const RuleVariables& replaceValues)
762 if(replaceValues.LinkFlags)
764 if(variable == "LINK_FLAGS")
766 return replaceValues.LinkFlags;
769 if(replaceValues.Flags)
771 if(variable == "FLAGS")
773 return replaceValues.Flags;
777 if(replaceValues.Source)
779 if(variable == "SOURCE")
781 return replaceValues.Source;
784 if(replaceValues.PreprocessedSource)
786 if(variable == "PREPROCESSED_SOURCE")
788 return replaceValues.PreprocessedSource;
791 if(replaceValues.AssemblySource)
793 if(variable == "ASSEMBLY_SOURCE")
795 return replaceValues.AssemblySource;
798 if(replaceValues.Object)
800 if(variable == "OBJECT")
802 return replaceValues.Object;
805 if(replaceValues.ObjectDir)
807 if(variable == "OBJECT_DIR")
809 return replaceValues.ObjectDir;
812 if(replaceValues.Objects)
814 if(variable == "OBJECTS")
816 return replaceValues.Objects;
819 if(replaceValues.ObjectsQuoted)
821 if(variable == "OBJECTS_QUOTED")
823 return replaceValues.ObjectsQuoted;
826 if(replaceValues.Defines && variable == "DEFINES")
828 return replaceValues.Defines;
830 if(replaceValues.TargetPDB )
832 if(variable == "TARGET_PDB")
834 return replaceValues.TargetPDB;
838 if(replaceValues.Target)
840 if(variable == "TARGET_QUOTED")
842 std::string targetQuoted = replaceValues.Target;
843 if(targetQuoted.size() && targetQuoted[0] != '\"')
845 targetQuoted = '\"';
846 targetQuoted += replaceValues.Target;
847 targetQuoted += '\"';
849 return targetQuoted;
851 if(replaceValues.LanguageCompileFlags)
853 if(variable == "LANGUAGE_COMPILE_FLAGS")
855 return replaceValues.LanguageCompileFlags;
858 if(replaceValues.Target)
860 if(variable == "TARGET")
862 return replaceValues.Target;
865 if(variable == "TARGET_IMPLIB")
867 return this->TargetImplib;
869 if(variable == "TARGET_VERSION_MAJOR")
871 if(replaceValues.TargetVersionMajor)
873 return replaceValues.TargetVersionMajor;
875 else
877 return "0";
880 if(variable == "TARGET_VERSION_MINOR")
882 if(replaceValues.TargetVersionMinor)
884 return replaceValues.TargetVersionMinor;
886 else
888 return "0";
891 if(replaceValues.Target)
893 if(variable == "TARGET_BASE")
895 // Strip the last extension off the target name.
896 std::string targetBase = replaceValues.Target;
897 std::string::size_type pos = targetBase.rfind(".");
898 if(pos != targetBase.npos)
900 return targetBase.substr(0, pos);
902 else
904 return targetBase;
909 if(replaceValues.TargetSOName)
911 if(variable == "TARGET_SONAME")
913 if(replaceValues.Language)
915 std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
916 name += replaceValues.Language;
917 name += "_FLAG";
918 if(this->Makefile->GetDefinition(name.c_str()))
920 return replaceValues.TargetSOName;
923 return "";
926 if(replaceValues.TargetInstallNameDir)
928 if(variable == "TARGET_INSTALLNAME_DIR")
930 return replaceValues.TargetInstallNameDir;
933 if(replaceValues.LinkLibraries)
935 if(variable == "LINK_LIBRARIES")
937 return replaceValues.LinkLibraries;
940 std::vector<std::string> enabledLanguages;
941 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
942 // loop over language specific replace variables
943 int pos = 0;
944 while(ruleReplaceVars[pos])
946 for(std::vector<std::string>::iterator i = enabledLanguages.begin();
947 i != enabledLanguages.end(); ++i)
949 const char* lang = i->c_str();
950 std::string actualReplace = ruleReplaceVars[pos];
951 // If this is the compiler then look for the extra variable
952 // _COMPILER_ARG1 which must be the first argument to the compiler
953 const char* compilerArg1 = 0;
954 if(actualReplace == "CMAKE_${LANG}_COMPILER")
956 std::string arg1 = actualReplace + "_ARG1";
957 cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
958 compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
960 if(actualReplace.find("${LANG}") != actualReplace.npos)
962 cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
964 if(actualReplace == variable)
966 std::string replace =
967 this->Makefile->GetSafeDefinition(variable.c_str());
968 // if the variable is not a FLAG then treat it like a path
969 if(variable.find("_FLAG") == variable.npos)
971 std::string ret = this->ConvertToOutputForExisting(replace.c_str());
972 // if there is a required first argument to the compiler add it
973 // to the compiler string
974 if(compilerArg1)
976 ret += " ";
977 ret += compilerArg1;
979 return ret;
981 return replace;
984 pos++;
986 return variable;
990 void
991 cmLocalGenerator::ExpandRuleVariables(std::string& s,
992 const RuleVariables& replaceValues)
994 std::vector<std::string> enabledLanguages;
995 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
996 std::string::size_type start = s.find('<');
997 // no variables to expand
998 if(start == s.npos)
1000 return;
1002 std::string::size_type pos = 0;
1003 std::string expandedInput;
1004 while(start != s.npos && start < s.size()-2)
1006 std::string::size_type end = s.find('>', start);
1007 // if we find a < with no > we are done
1008 if(end == s.npos)
1010 return;
1012 char c = s[start+1];
1013 // if the next char after the < is not A-Za-z then
1014 // skip it and try to find the next < in the string
1015 if(!isalpha(c))
1017 start = s.find('<', start+1);
1019 else
1021 // extract the var
1022 std::string var = s.substr(start+1, end - start-1);
1023 std::string replace = this->ExpandRuleVariable(var,
1024 replaceValues);
1025 expandedInput += s.substr(pos, start-pos);
1026 expandedInput += replace;
1027 // move to next one
1028 start = s.find('<', start+var.size()+2);
1029 pos = end+1;
1032 // add the rest of the input
1033 expandedInput += s.substr(pos, s.size()-pos);
1034 s = expandedInput;
1038 std::string
1039 cmLocalGenerator::ConvertToOutputForExisting(const char* p)
1041 std::string ret = p;
1042 if(this->WindowsShell && ret.find(' ') != ret.npos
1043 && cmSystemTools::FileExists(p))
1045 if(cmSystemTools::GetShortPath(p, ret))
1047 return this->Convert(ret.c_str(), NONE, SHELL, true);
1050 return this->Convert(p, START_OUTPUT, SHELL, true);
1053 const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
1055 if(!lang)
1057 return "";
1059 if(this->LanguageToIncludeFlags.count(lang))
1061 return this->LanguageToIncludeFlags[lang].c_str();
1063 cmOStringStream includeFlags;
1064 std::vector<std::string> includes;
1065 this->GetIncludeDirectories(includes);
1066 std::vector<std::string>::iterator i;
1068 std::string flagVar = "CMAKE_INCLUDE_FLAG_";
1069 flagVar += lang;
1070 const char* includeFlag =
1071 this->Makefile->GetSafeDefinition(flagVar.c_str());
1072 flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
1073 flagVar += lang;
1074 const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
1075 bool quotePaths = false;
1076 if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
1078 quotePaths = true;
1080 bool repeatFlag = true;
1081 // should the include flag be repeated like ie. -IA -IB
1082 if(!sep)
1084 sep = " ";
1086 else
1088 // if there is a separator then the flag is not repeated but is only
1089 // given once i.e. -classpath a:b:c
1090 repeatFlag = false;
1093 // Support special system include flag if it is available and the
1094 // normal flag is repeated for each directory.
1095 std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
1096 sysFlagVar += lang;
1097 const char* sysIncludeFlag = 0;
1098 if(repeatFlag)
1100 sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
1103 bool flagUsed = false;
1104 std::set<cmStdString> emitted;
1105 #ifdef __APPLE__
1106 emitted.insert("/System/Library/Frameworks");
1107 #endif
1108 for(i = includes.begin(); i != includes.end(); ++i)
1110 #ifdef __APPLE__
1111 if(cmSystemTools::IsPathToFramework(i->c_str()))
1113 std::string frameworkDir = *i;
1114 frameworkDir += "/../";
1115 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1116 if(emitted.insert(frameworkDir).second)
1118 includeFlags
1119 << "-F"
1120 << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
1122 continue;
1124 #endif
1125 std::string include = *i;
1126 if(!flagUsed || repeatFlag)
1128 if(sysIncludeFlag &&
1129 this->Makefile->IsSystemIncludeDirectory(i->c_str()))
1131 includeFlags << sysIncludeFlag;
1133 else
1135 includeFlags << includeFlag;
1137 flagUsed = true;
1139 std::string includePath = this->ConvertToOutputForExisting(i->c_str());
1140 if(quotePaths && includePath.size() && includePath[0] != '\"')
1142 includeFlags << "\"";
1144 includeFlags << includePath;
1145 if(quotePaths && includePath.size() && includePath[0] != '\"')
1147 includeFlags << "\"";
1149 includeFlags << sep;
1151 std::string flags = includeFlags.str();
1152 // remove trailing separators
1153 if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
1155 flags[flags.size()-1] = ' ';
1157 std::string defineFlags = this->Makefile->GetDefineFlags();
1158 flags += defineFlags;
1159 this->LanguageToIncludeFlags[lang] = flags;
1161 // Use this temorary variable for the return value to work-around a
1162 // bogus GCC 2.95 warning.
1163 const char* ret = this->LanguageToIncludeFlags[lang].c_str();
1164 return ret;
1167 //----------------------------------------------------------------------------
1168 void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
1169 bool filter_system_dirs)
1171 // Need to decide whether to automatically include the source and
1172 // binary directories at the beginning of the include path.
1173 bool includeSourceDir = false;
1174 bool includeBinaryDir = false;
1176 // When automatic include directories are requested for a build then
1177 // include the source and binary directories at the beginning of the
1178 // include path to approximate include file behavior for an
1179 // in-source build. This does not account for the case of a source
1180 // file in a subdirectory of the current source directory but we
1181 // cannot fix this because not all native build tools support
1182 // per-source-file include paths.
1183 if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
1185 includeSourceDir = true;
1186 includeBinaryDir = true;
1189 // CMake versions below 2.0 would add the source tree to the -I path
1190 // automatically. Preserve compatibility.
1191 const char* versionValue =
1192 this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
1193 int major = 0;
1194 int minor = 0;
1195 if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
1197 versionValue = 0;
1199 if(versionValue && major < 2)
1201 includeSourceDir = true;
1204 // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
1205 // not set the backwards compatibility level automatically.
1206 const char* vtkSourceDir =
1207 this->Makefile->GetDefinition("VTK_SOURCE_DIR");
1208 if(vtkSourceDir)
1210 const char* vtk_major =
1211 this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
1212 const char* vtk_minor =
1213 this->Makefile->GetDefinition("VTK_MINOR_VERSION");
1214 vtk_major = vtk_major? vtk_major : "4";
1215 vtk_minor = vtk_minor? vtk_minor : "4";
1216 int vmajor = 0;
1217 int vminor = 0;
1218 if(sscanf(vtk_major, "%d", &vmajor) &&
1219 sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
1221 includeSourceDir = true;
1225 // Do not repeat an include path.
1226 std::set<cmStdString> emitted;
1228 // Store the automatic include paths.
1229 if(includeBinaryDir)
1231 dirs.push_back(this->Makefile->GetStartOutputDirectory());
1232 emitted.insert(this->Makefile->GetStartOutputDirectory());
1234 if(includeSourceDir)
1236 if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
1238 dirs.push_back(this->Makefile->GetStartDirectory());
1239 emitted.insert(this->Makefile->GetStartDirectory());
1243 if(filter_system_dirs)
1245 // Do not explicitly add the standard include path "/usr/include".
1246 // This can cause problems with certain standard library
1247 // implementations because the wrong headers may be found first.
1248 emitted.insert("/usr/include");
1249 if(const char* implicitIncludes = this->Makefile->GetDefinition
1250 ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
1252 std::vector<std::string> implicitIncludeVec;
1253 cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
1254 for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
1256 emitted.insert(implicitIncludeVec[k]);
1261 // Get the project-specified include directories.
1262 std::vector<std::string>& includes =
1263 this->Makefile->GetIncludeDirectories();
1265 // Support putting all the in-project include directories first if
1266 // it is requested by the project.
1267 if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
1269 const char* topSourceDir = this->Makefile->GetHomeDirectory();
1270 const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
1271 for(std::vector<std::string>::iterator i = includes.begin();
1272 i != includes.end(); ++i)
1274 // Emit this directory only if it is a subdirectory of the
1275 // top-level source or binary tree.
1276 if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
1277 cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
1278 cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
1279 cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
1281 if(emitted.insert(*i).second)
1283 dirs.push_back(*i);
1289 // Construct the final ordered include directory list.
1290 for(std::vector<std::string>::iterator i = includes.begin();
1291 i != includes.end(); ++i)
1293 if(emitted.insert(*i).second)
1295 dirs.push_back(*i);
1300 void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
1301 std::string& flags,
1302 std::string& linkFlags,
1303 cmTarget& target)
1305 std::string buildType =
1306 this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
1307 buildType = cmSystemTools::UpperCase(buildType);
1308 const char* libraryLinkVariable =
1309 "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
1311 switch(target.GetType())
1313 case cmTarget::STATIC_LIBRARY:
1315 const char* targetLinkFlags =
1316 target.GetProperty("STATIC_LIBRARY_FLAGS");
1317 if(targetLinkFlags)
1319 linkFlags += targetLinkFlags;
1320 linkFlags += " ";
1323 break;
1324 case cmTarget::MODULE_LIBRARY:
1325 libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
1326 case cmTarget::SHARED_LIBRARY:
1328 linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
1329 linkFlags += " ";
1330 if(buildType.size())
1332 std::string build = libraryLinkVariable;
1333 build += "_";
1334 build += buildType;
1335 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1336 linkFlags += " ";
1338 if(this->Makefile->IsOn("WIN32") &&
1339 !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
1341 const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
1342 for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
1343 i != sources.end(); ++i)
1345 cmSourceFile* sf = *i;
1346 if(sf->GetExtension() == "def")
1348 linkFlags +=
1349 this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
1350 linkFlags += this->Convert(sf->GetFullPath().c_str(),
1351 START_OUTPUT, SHELL);
1352 linkFlags += " ";
1356 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1357 if(targetLinkFlags)
1359 linkFlags += targetLinkFlags;
1360 linkFlags += " ";
1361 std::string configLinkFlags = targetLinkFlags;
1362 configLinkFlags += buildType;
1363 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1364 if(targetLinkFlags)
1366 linkFlags += targetLinkFlags;
1367 linkFlags += " ";
1370 cmOStringStream linklibsStr;
1371 this->OutputLinkLibraries(linklibsStr, target, false);
1372 linkLibs = linklibsStr.str();
1374 break;
1375 case cmTarget::EXECUTABLE:
1377 linkFlags +=
1378 this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
1379 linkFlags += " ";
1380 if(buildType.size())
1382 std::string build = "CMAKE_EXE_LINKER_FLAGS_";
1383 build += buildType;
1384 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1385 linkFlags += " ";
1387 const char* linkLanguage =
1388 target.GetLinkerLanguage(this->GetGlobalGenerator());
1389 if(!linkLanguage)
1391 cmSystemTools::Error
1392 ("CMake can not determine linker language for target:",
1393 target.GetName());
1394 return;
1396 std::string langVar = "CMAKE_";
1397 langVar += linkLanguage;
1398 std::string flagsVar = langVar + "_FLAGS";
1399 std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
1400 sharedFlagsVar += linkLanguage;
1401 sharedFlagsVar += "_FLAGS";
1402 flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
1403 flags += " ";
1404 flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
1405 flags += " ";
1406 cmOStringStream linklibs;
1407 this->OutputLinkLibraries(linklibs, target, false);
1408 linkLibs = linklibs.str();
1409 if(cmSystemTools::IsOn
1410 (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1412 std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
1413 + linkLanguage + std::string("_FLAGS");
1414 linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
1415 linkFlags += " ";
1417 if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
1419 linkFlags +=
1420 this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
1421 linkFlags += " ";
1423 else
1425 linkFlags +=
1426 this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
1427 linkFlags += " ";
1429 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1430 if(targetLinkFlags)
1432 linkFlags += targetLinkFlags;
1433 linkFlags += " ";
1434 std::string configLinkFlags = targetLinkFlags;
1435 configLinkFlags += buildType;
1436 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1437 if(targetLinkFlags)
1439 linkFlags += targetLinkFlags;
1440 linkFlags += " ";
1444 break;
1445 case cmTarget::UTILITY:
1446 case cmTarget::GLOBAL_TARGET:
1447 case cmTarget::INSTALL_FILES:
1448 case cmTarget::INSTALL_PROGRAMS:
1449 case cmTarget::INSTALL_DIRECTORY:
1450 break;
1454 std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib)
1456 #if defined(_WIN32) && !defined(__CYGWIN__)
1457 // Work-ardound command line parsing limitations in MSVC 6.0 and
1458 // Watcom.
1459 if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
1461 // Search for the last space.
1462 std::string::size_type pos = lib.rfind(' ');
1463 if(pos != lib.npos)
1465 // Find the slash after the last space, if any.
1466 pos = lib.find('/', pos);
1468 // Convert the portion of the path with a space to a short path.
1469 std::string sp;
1470 if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
1472 // Append the rest of the path with no space.
1473 sp += lib.substr(pos);
1475 // Convert to an output path.
1476 return this->Convert(sp.c_str(), NONE, SHELL);
1480 #endif
1482 // Normal behavior.
1483 return this->Convert(lib.c_str(), START_OUTPUT, SHELL);
1486 bool cmLocalGenerator::GetLinkerArgs(std::string& rpath,
1487 std::string& linkLibs,
1488 cmTarget& tgt,
1489 bool relink,
1490 unsigned int minRpathSize)
1492 rpath = "";
1493 // collect all the flags needed for linking libraries
1494 linkLibs = "";
1496 const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
1498 cmComputeLinkInformation cli(&tgt, config);
1499 if(!cli.Compute())
1501 return false;
1504 const char* linkLanguage = cli.GetLinkLanguage();
1506 // Embed runtime search paths if possible and if required.
1507 bool outputRuntime = !this->Makefile->IsOn("CMAKE_SKIP_RPATH");
1509 // Lookup rpath specification flags.
1510 std::string runtimeFlag;
1511 std::string runtimeSep;
1512 if(tgt.GetType() != cmTarget::STATIC_LIBRARY)
1514 std::string runTimeFlagVar = "CMAKE_";
1515 if(tgt.GetType() == cmTarget::EXECUTABLE)
1517 runTimeFlagVar += "EXECUTABLE";
1519 else
1521 runTimeFlagVar += "SHARED_LIBRARY";
1523 runTimeFlagVar += "_RUNTIME_";
1524 runTimeFlagVar += linkLanguage;
1525 runTimeFlagVar += "_FLAG";
1526 std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
1527 runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
1528 runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
1530 // concatenate all paths or no?
1531 bool runtimeConcatenate = !runtimeSep.empty();
1533 const char* runtimeAlways =
1534 this->Makefile->GetDefinition("CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH");
1536 // Turn off rpath support if no flag is available to specify it.
1537 if(runtimeFlag.empty())
1539 outputRuntime = false;
1540 runtimeAlways = 0;
1543 std::string libPathFlag =
1544 this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1545 std::string libPathTerminator =
1546 this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1547 std::string libLinkFlag =
1548 this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
1550 // Flags to link an executable to shared libraries.
1551 std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
1552 linkFlagsVar += linkLanguage;
1553 linkFlagsVar += "_FLAGS";
1554 if( tgt.GetType() == cmTarget::EXECUTABLE )
1556 linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
1557 linkLibs += " ";
1560 // Append the framework search path flags.
1561 std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
1562 for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
1563 fdi != fwDirs.end(); ++fdi)
1565 linkLibs += "-F";
1566 linkLibs += this->Convert(fdi->c_str(), NONE, SHELL, false);
1567 linkLibs += " ";
1570 // Append the library search path flags.
1571 std::vector<std::string> const& libDirs = cli.GetDirectories();
1572 for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
1573 libDir != libDirs.end(); ++libDir)
1575 std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
1576 linkLibs += libPathFlag;
1577 linkLibs += libpath;
1578 linkLibs += libPathTerminator;
1579 linkLibs += " ";
1582 // Append the link items.
1583 typedef cmComputeLinkInformation::ItemVector ItemVector;
1584 ItemVector const& items = cli.GetItems();
1585 for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
1587 if(li->IsPath)
1589 linkLibs += this->ConvertToLinkReference(li->Value);
1591 else
1593 linkLibs += li->Value;
1595 linkLibs += " ";
1598 // Select whether to generate an rpath for the install tree or the
1599 // build tree.
1600 bool linking_for_install =
1601 relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
1602 bool use_install_rpath =
1603 outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
1604 bool use_build_rpath =
1605 outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
1606 bool use_link_rpath =
1607 outputRuntime && linking_for_install &&
1608 tgt.GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
1610 // Construct the RPATH.
1611 std::vector<std::string> runtimeDirs;
1612 if(use_install_rpath)
1614 const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
1615 cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
1617 if(use_build_rpath || use_link_rpath)
1619 std::vector<std::string> const& rdirs = cli.GetRuntimeSearchPath();
1620 for(std::vector<std::string>::const_iterator ri = rdirs.begin();
1621 ri != rdirs.end(); ++ri)
1623 // Put this directory in the rpath if using build-tree rpath
1624 // support or if using the link path as an rpath.
1625 if(use_build_rpath)
1627 runtimeDirs.push_back(*ri);
1629 else if(use_link_rpath)
1631 // Do not add any path inside the source or build tree.
1632 const char* topSourceDir = this->Makefile->GetHomeDirectory();
1633 const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
1634 if(!cmSystemTools::ComparePath(ri->c_str(), topSourceDir) &&
1635 !cmSystemTools::ComparePath(ri->c_str(), topBinaryDir) &&
1636 !cmSystemTools::IsSubDirectory(ri->c_str(), topSourceDir) &&
1637 !cmSystemTools::IsSubDirectory(ri->c_str(), topBinaryDir))
1639 runtimeDirs.push_back(*ri);
1644 if(runtimeAlways)
1646 // Add runtime paths required by the platform to always be
1647 // present. This is done even when skipping rpath support.
1648 cmSystemTools::ExpandListArgument(runtimeAlways, runtimeDirs);
1651 // Convert the runtime directory names for use in the build file.
1652 for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
1653 ri != runtimeDirs.end(); ++ri)
1655 *ri = this->Convert(ri->c_str(), FULL, SHELL, false);
1658 if(!runtimeDirs.empty())
1660 // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
1661 // a "-R a -R b -R c" type link line
1662 rpath += runtimeFlag;
1663 std::vector<std::string>::iterator itr = runtimeDirs.begin();
1664 rpath += *itr;
1665 ++itr;
1666 for( ; itr != runtimeDirs.end(); ++itr )
1668 if(runtimeConcatenate)
1670 rpath += runtimeSep;
1671 rpath += *itr;
1673 else
1675 rpath += " ";
1676 rpath += runtimeFlag;
1677 rpath += *itr;
1682 while (rpath.size() < minRpathSize)
1684 if (rpath.size()==0)
1686 rpath += runtimeFlag;
1689 rpath += runtimeSep;
1691 return true;
1695 * Output the linking rules on a command line. For executables,
1696 * targetLibrary should be a NULL pointer. For libraries, it should point
1697 * to the name of the library. This will not link a library against itself.
1699 void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
1700 cmTarget& tgt,
1701 bool relink)
1703 std::string rpath;
1704 std::string linkLibs;
1705 unsigned int minBuildRpathSize = 0;
1707 if ((relink==false)
1708 && this->Makefile->IsOn("CMAKE_USE_CHRPATH")
1709 && (tgt.IsChrpathAvailable()))
1711 std::string installRpath;
1712 std::string dummy;
1713 this->GetLinkerArgs(installRpath, dummy, tgt, true, 0);
1714 minBuildRpathSize = static_cast<unsigned int>(installRpath.size());
1717 if (!this->GetLinkerArgs(rpath, linkLibs, tgt, relink, minBuildRpathSize))
1719 return;
1722 const char* linkLanguage =
1723 tgt.GetLinkerLanguage(this->GetGlobalGenerator());
1724 if(!linkLanguage)
1726 cmSystemTools::
1727 Error("CMake can not determine linker language for target:",
1728 tgt.GetName());
1729 return;
1732 fout << linkLibs;
1733 fout << rpath << " ";
1735 // Add standard libraries for this language.
1736 std::string standardLibsVar = "CMAKE_";
1737 standardLibsVar += linkLanguage;
1738 standardLibsVar += "_STANDARD_LIBRARIES";
1739 if(const char* stdLibs =
1740 this->Makefile->GetDefinition(standardLibsVar.c_str()))
1742 fout << stdLibs << " ";
1746 //----------------------------------------------------------------------------
1747 void cmLocalGenerator::AddLanguageFlags(std::string& flags,
1748 const char* lang,
1749 const char* config)
1751 // Add language-specific flags.
1752 std::string flagsVar = "CMAKE_";
1753 flagsVar += lang;
1754 flagsVar += "_FLAGS";
1755 if(this->EmitUniversalBinaryFlags)
1757 const char* osxArch =
1758 this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
1759 const char* sysroot =
1760 this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
1761 if(osxArch && sysroot && lang && lang[0] =='C')
1763 std::vector<std::string> archs;
1764 cmSystemTools::ExpandListArgument(std::string(osxArch),
1765 archs);
1766 bool addArchFlag = true;
1767 if(archs.size() == 1)
1769 const char* archOrig =
1770 this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
1771 if(archs[0] == archOrig)
1773 addArchFlag = false;
1776 // if there is more than one arch add the -arch and
1777 // -isysroot flags, or if there is one arch flag, but
1778 // it is not the default -arch flag for the system, then
1779 // add it. Otherwize do not add -arch and -isysroot
1780 if(addArchFlag)
1782 for( std::vector<std::string>::iterator i = archs.begin();
1783 i != archs.end(); ++i)
1785 flags += " -arch ";
1786 flags += *i;
1788 flags += " -isysroot ";
1789 flags += sysroot;
1793 this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
1796 //----------------------------------------------------------------------------
1797 std::string cmLocalGenerator::GetRealDependency(const char* inName,
1798 const char* config)
1800 // Older CMake code may specify the dependency using the target
1801 // output file rather than the target name. Such code would have
1802 // been written before there was support for target properties that
1803 // modify the name so stripping down to just the file name should
1804 // produce the target name in this case.
1805 std::string name = cmSystemTools::GetFilenameName(inName);
1806 if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
1808 name = cmSystemTools::GetFilenameWithoutLastExtension(name);
1811 // Look for a CMake target with the given name.
1812 if(cmTarget* target = this->GlobalGenerator->FindTarget(0,name.c_str(),true))
1814 // make sure it is not just a coincidence that the target name
1815 // found is part of the inName
1816 if(cmSystemTools::FileIsFullPath(inName))
1818 std::string tLocation = target->GetLocation(config);
1819 tLocation = cmSystemTools::GetFilenamePath(tLocation);
1820 std::string depLocation = cmSystemTools::GetFilenamePath(
1821 std::string(inName));
1822 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
1823 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
1824 if(depLocation != tLocation)
1826 // it is a full path to a depend that has the same name
1827 // as a target but is in a different location so do not use
1828 // the target as the depend
1829 return inName;
1832 switch (target->GetType())
1834 case cmTarget::EXECUTABLE:
1835 case cmTarget::STATIC_LIBRARY:
1836 case cmTarget::SHARED_LIBRARY:
1837 case cmTarget::MODULE_LIBRARY:
1839 // Get the location of the target's output file and depend on it.
1840 if(const char* location = target->GetLocation(config))
1842 return location;
1845 break;
1846 case cmTarget::UTILITY:
1847 case cmTarget::GLOBAL_TARGET:
1848 // Depending on a utility target may not work but just trust
1849 // the user to have given a valid name.
1850 return inName;
1851 case cmTarget::INSTALL_FILES:
1852 case cmTarget::INSTALL_PROGRAMS:
1853 case cmTarget::INSTALL_DIRECTORY:
1854 break;
1858 // The name was not that of a CMake target. It must name a file.
1859 if(cmSystemTools::FileIsFullPath(inName))
1861 // This is a full path. Return it as given.
1862 return inName;
1864 // Treat the name as relative to the source directory in which it
1865 // was given.
1866 name = this->Makefile->GetCurrentDirectory();
1867 name += "/";
1868 name += inName;
1869 return name;
1872 //----------------------------------------------------------------------------
1873 std::string cmLocalGenerator::GetRealLocation(const char* inName,
1874 const char* config)
1876 std::string outName=inName;
1877 // Look for a CMake target with the given name, which is an executable
1878 // and which can be run
1879 cmTarget* target = this->GlobalGenerator->FindTarget(0, inName, true);
1880 if ((target != 0)
1881 && (target->GetType() == cmTarget::EXECUTABLE)
1882 && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
1883 || (target->IsImported() == true)))
1885 outName = target->GetLocation( config );
1887 return outName;
1890 //----------------------------------------------------------------------------
1891 void cmLocalGenerator::AddSharedFlags(std::string& flags,
1892 const char* lang,
1893 bool shared)
1895 std::string flagsVar;
1897 // Add flags for dealing with shared libraries for this language.
1898 if(shared)
1900 flagsVar = "CMAKE_SHARED_LIBRARY_";
1901 flagsVar += lang;
1902 flagsVar += "_FLAGS";
1903 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1906 // Add flags specific to shared builds.
1907 if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1909 flagsVar = "CMAKE_SHARED_BUILD_";
1910 flagsVar += lang;
1911 flagsVar += "_FLAGS";
1912 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1916 //----------------------------------------------------------------------------
1917 void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
1918 const char* var,
1919 const char* config)
1921 // Add the flags from the variable itself.
1922 std::string flagsVar = var;
1923 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1924 // Add the flags from the build-type specific variable.
1925 if(config && *config)
1927 flagsVar += "_";
1928 flagsVar += cmSystemTools::UpperCase(config);
1929 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1933 //----------------------------------------------------------------------------
1934 void cmLocalGenerator::AppendFlags(std::string& flags,
1935 const char* newFlags)
1937 if(newFlags && *newFlags)
1939 std::string newf = newFlags;
1940 if(flags.size())
1942 flags += " ";
1944 flags += newFlags;
1948 //----------------------------------------------------------------------------
1949 void cmLocalGenerator::AppendDefines(std::string& defines,
1950 const char* defines_list,
1951 const char* lang)
1953 // Short-circuit if there are no definitions.
1954 if(!defines_list)
1956 return;
1959 // Expand the list of definitions.
1960 std::vector<std::string> defines_vec;
1961 cmSystemTools::ExpandListArgument(defines_list, defines_vec);
1963 // Short-circuit if there are no definitions.
1964 if(defines_vec.empty())
1966 return;
1969 // Lookup the define flag for the current language.
1970 std::string dflag = "-D";
1971 if(lang)
1973 std::string defineFlagVar = "CMAKE_";
1974 defineFlagVar += lang;
1975 defineFlagVar += "_DEFINE_FLAG";
1976 const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
1977 if(df && *df)
1979 dflag = df;
1983 // Add each definition to the command line with appropriate escapes.
1984 const char* dsep = defines.empty()? "" : " ";
1985 for(std::vector<std::string>::const_iterator di = defines_vec.begin();
1986 di != defines_vec.end(); ++di)
1988 // Skip unsupported definitions.
1989 if(!this->CheckDefinition(*di))
1991 continue;
1994 // Separate from previous definitions.
1995 defines += dsep;
1996 dsep = " ";
1998 // Append the definition with proper escaping.
1999 defines += dflag;
2000 if(this->WatcomWMake)
2002 // The Watcom compiler does its own command line parsing instead
2003 // of using the windows shell rules. Definitions are one of
2004 // -DNAME
2005 // -DNAME=<cpp-token>
2006 // -DNAME="c-string with spaces and other characters(?@#$)"
2008 // Watcom will properly parse each of these cases from the
2009 // command line without any escapes. However we still have to
2010 // get the '$' and '#' characters through WMake as '$$' and
2011 // '$#'.
2012 for(const char* c = di->c_str(); *c; ++c)
2014 if(*c == '$' || *c == '#')
2016 defines += '$';
2018 defines += *c;
2021 else
2023 // Make the definition appear properly on the command line.
2024 defines += this->EscapeForShell(di->c_str(), true);
2029 //----------------------------------------------------------------------------
2030 std::string
2031 cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
2032 const char* default_comment)
2034 // Check for a comment provided with the command.
2035 if(cc.GetComment())
2037 return cc.GetComment();
2040 // Construct a reasonable default comment if possible.
2041 if(!cc.GetOutputs().empty())
2043 std::string comment;
2044 comment = "Generating ";
2045 const char* sep = "";
2046 for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
2047 o != cc.GetOutputs().end(); ++o)
2049 comment += sep;
2050 comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
2051 sep = ", ";
2053 return comment;
2056 // Otherwise use the provided default.
2057 return default_comment;
2060 //----------------------------------------------------------------------------
2061 std::string
2062 cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
2064 return this->Convert(remote, START_OUTPUT, SHELL, true);
2067 //----------------------------------------------------------------------------
2068 std::string cmLocalGenerator::Convert(const char* source,
2069 RelativeRoot relative,
2070 OutputFormat output,
2071 bool optional)
2073 // Make sure the relative path conversion components are set.
2074 if(!this->PathConversionsSetup)
2076 this->SetupPathConversions();
2077 this->PathConversionsSetup = true;
2080 // Convert the path to a relative path.
2081 std::string result = source;
2083 if (!optional || this->UseRelativePaths)
2085 switch (relative)
2087 case HOME:
2088 //result = cmSystemTools::CollapseFullPath(result.c_str());
2089 result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
2090 result.c_str());
2091 break;
2092 case START:
2093 //result = cmSystemTools::CollapseFullPath(result.c_str());
2094 result = this->ConvertToRelativePath(this->StartDirectoryComponents,
2095 result.c_str());
2096 break;
2097 case HOME_OUTPUT:
2098 //result = cmSystemTools::CollapseFullPath(result.c_str());
2099 result =
2100 this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
2101 result.c_str());
2102 break;
2103 case START_OUTPUT:
2104 //result = cmSystemTools::CollapseFullPath(result.c_str());
2105 result =
2106 this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
2107 result.c_str());
2108 break;
2109 case FULL:
2110 result = cmSystemTools::CollapseFullPath(result.c_str());
2111 break;
2112 case NONE:
2113 break;
2116 // Now convert it to an output path.
2117 if (output == MAKEFILE)
2119 result = cmSystemTools::ConvertToOutputPath(result.c_str());
2121 else if( output == SHELL)
2123 // For the MSYS shell convert drive letters to posix paths, so
2124 // that c:/some/path becomes /c/some/path. This is needed to
2125 // avoid problems with the shell path translation.
2126 if(this->MSYSShell)
2128 if(result.size() > 2 && result[1] == ':')
2130 result[1] = result[0];
2131 result[0] = '/';
2134 if(this->WindowsShell)
2136 std::string::size_type pos = 0;
2137 while((pos = result.find('/', pos)) != std::string::npos)
2139 result[pos] = '\\';
2140 pos++;
2143 result = this->EscapeForShell(result.c_str(), true, false);
2145 return result;
2148 //----------------------------------------------------------------------------
2149 std::string cmLocalGenerator::FindRelativePathTopSource()
2151 // Relative path conversion within a single tree managed by CMake is
2152 // safe. We can use our parent relative path top if and only if
2153 // this is a subdirectory of that top.
2154 if(cmLocalGenerator* parent = this->GetParent())
2156 std::string parentTop = parent->FindRelativePathTopSource();
2157 if(cmSystemTools::IsSubDirectory(
2158 this->Makefile->GetStartDirectory(), parentTop.c_str()))
2160 return parentTop;
2164 // Otherwise this directory itself is the new top.
2165 return this->Makefile->GetStartDirectory();
2168 //----------------------------------------------------------------------------
2169 std::string cmLocalGenerator::FindRelativePathTopBinary()
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->FindRelativePathTopBinary();
2177 if(cmSystemTools::IsSubDirectory(
2178 this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
2180 return parentTop;
2184 // Otherwise this directory itself is the new top.
2185 return this->Makefile->GetStartOutputDirectory();
2188 //----------------------------------------------------------------------------
2189 void cmLocalGenerator::ConfigureRelativePaths()
2191 // Relative path conversion inside the source tree is not used to
2192 // construct relative paths passed to build tools so it is safe to
2193 // even when the source is a network path.
2194 std::string source = this->FindRelativePathTopSource();
2195 this->RelativePathTopSource = source;
2197 // The current working directory on Windows cannot be a network
2198 // path. Therefore relative paths cannot work when the binary tree
2199 // is a network path.
2200 std::string binary = this->FindRelativePathTopBinary();
2201 if(binary.size() < 2 || binary.substr(0, 2) != "//")
2203 this->RelativePathTopBinary = binary;
2205 else
2207 this->RelativePathTopBinary = "";
2211 //----------------------------------------------------------------------------
2212 static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
2214 return (cmSystemTools::ComparePath(a, b) ||
2215 cmSystemTools::IsSubDirectory(a, b));
2218 //----------------------------------------------------------------------------
2219 std::string
2220 cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
2221 const char* in_remote)
2223 // The path should never be quoted.
2224 assert(in_remote[0] != '\"');
2226 // The local path should never have a trailing slash.
2227 assert(local.size() > 0 && !(local[local.size()-1] == ""));
2229 // If the path is already relative then just return the path.
2230 if(!cmSystemTools::FileIsFullPath(in_remote))
2232 return in_remote;
2235 // Make sure relative path conversion is configured.
2236 if(!this->RelativePathsConfigured)
2238 this->ConfigureRelativePaths();
2239 this->RelativePathsConfigured = true;
2242 // Skip conversion if the path and local are not both in the source
2243 // or both in the binary tree.
2244 std::string local_path = cmSystemTools::JoinPath(local);
2245 if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
2246 this->RelativePathTopBinary.c_str()) &&
2247 cmLocalGeneratorNotAbove(in_remote,
2248 this->RelativePathTopBinary.c_str())) ||
2249 (cmLocalGeneratorNotAbove(local_path.c_str(),
2250 this->RelativePathTopSource.c_str()) &&
2251 cmLocalGeneratorNotAbove(in_remote,
2252 this->RelativePathTopSource.c_str()))))
2254 return in_remote;
2257 // Identify the longest shared path component between the remote
2258 // path and the local path.
2259 std::vector<std::string> remote;
2260 cmSystemTools::SplitPath(in_remote, remote);
2261 unsigned int common=0;
2262 while(common < remote.size() &&
2263 common < local.size() &&
2264 cmSystemTools::ComparePath(remote[common].c_str(),
2265 local[common].c_str()))
2267 ++common;
2270 // If no part of the path is in common then return the full path.
2271 if(common == 0)
2273 return in_remote;
2276 // If the entire path is in common then just return a ".".
2277 if(common == remote.size() &&
2278 common == local.size())
2280 return ".";
2283 // If the entire path is in common except for a trailing slash then
2284 // just return a "./".
2285 if(common+1 == remote.size() &&
2286 remote[common].size() == 0 &&
2287 common == local.size())
2289 return "./";
2292 // Construct the relative path.
2293 std::string relative;
2295 // First add enough ../ to get up to the level of the shared portion
2296 // of the path. Leave off the trailing slash. Note that the last
2297 // component of local will never be empty because local should never
2298 // have a trailing slash.
2299 for(unsigned int i=common; i < local.size(); ++i)
2301 relative += "..";
2302 if(i < local.size()-1)
2304 relative += "/";
2308 // Now add the portion of the destination path that is not included
2309 // in the shared portion of the path. Add a slash the first time
2310 // only if there was already something in the path. If there was a
2311 // trailing slash in the input then the last iteration of the loop
2312 // will add a slash followed by an empty string which will preserve
2313 // the trailing slash in the output.
2314 for(unsigned int i=common; i < remote.size(); ++i)
2316 if(relative.size() > 0)
2318 relative += "/";
2320 relative += remote[i];
2323 // Finally return the path.
2324 return relative;
2327 //----------------------------------------------------------------------------
2328 void
2329 cmLocalGenerator
2330 ::GenerateTargetInstallRules(
2331 std::ostream& os, const char* config,
2332 std::vector<std::string> const& configurationTypes)
2334 // Convert the old-style install specification from each target to
2335 // an install generator and run it.
2336 cmTargets& tgts = this->Makefile->GetTargets();
2337 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
2339 // Include the user-specified pre-install script for this target.
2340 if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
2342 cmInstallScriptGenerator g(preinstall, false, 0);
2343 g.Generate(os, config, configurationTypes);
2346 // Install this target if a destination is given.
2347 if(l->second.GetInstallPath() != "")
2349 // Compute the full install destination. Note that converting
2350 // to unix slashes also removes any trailing slash.
2351 std::string destination = "${CMAKE_INSTALL_PREFIX}";
2352 destination += l->second.GetInstallPath();
2353 cmSystemTools::ConvertToUnixSlashes(destination);
2355 // Generate the proper install generator for this target type.
2356 switch(l->second.GetType())
2358 case cmTarget::EXECUTABLE:
2359 case cmTarget::STATIC_LIBRARY:
2360 case cmTarget::MODULE_LIBRARY:
2362 // Use a target install generator.
2363 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2364 g.Generate(os, config, configurationTypes);
2366 break;
2367 case cmTarget::SHARED_LIBRARY:
2369 #if defined(_WIN32) || defined(__CYGWIN__)
2370 // Special code to handle DLL. Install the import library
2371 // to the normal destination and the DLL to the runtime
2372 // destination.
2373 cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
2374 g1.Generate(os, config, configurationTypes);
2375 destination = "${CMAKE_INSTALL_PREFIX}";
2376 destination += l->second.GetRuntimeInstallPath();
2377 cmSystemTools::ConvertToUnixSlashes(destination);
2378 cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
2379 g2.Generate(os, config, configurationTypes);
2380 #else
2381 // Use a target install generator.
2382 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2383 g.Generate(os, config, configurationTypes);
2384 #endif
2386 break;
2387 default:
2388 break;
2392 // Include the user-specified post-install script for this target.
2393 if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
2395 cmInstallScriptGenerator g(postinstall, false, 0);
2396 g.Generate(os, config, configurationTypes);
2401 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2402 static std::string cmLocalGeneratorMD5(const char* input)
2404 char md5out[32];
2405 cmsysMD5* md5 = cmsysMD5_New();
2406 cmsysMD5_Initialize(md5);
2407 cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
2408 cmsysMD5_FinalizeHex(md5, md5out);
2409 cmsysMD5_Delete(md5);
2410 return std::string(md5out, 32);
2413 static bool
2414 cmLocalGeneratorShortenObjectName(std::string& objName,
2415 std::string::size_type max_len)
2417 // Replace the beginning of the path portion of the object name with
2418 // its own md5 sum.
2419 std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
2420 if(pos != objName.npos)
2422 std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
2423 md5name += objName.substr(pos);
2424 objName = md5name;
2426 // The object name is now short enough.
2427 return true;
2429 else
2431 // The object name could not be shortened enough.
2432 return false;
2436 static bool cmLocalGeneratorCheckObjectName(std::string& objName,
2437 std::string::size_type dir_len)
2439 // Choose a maximum file name length.
2440 #if defined(_WIN32) || defined(__CYGWIN__)
2441 std::string::size_type const max_total_len = 250;
2442 #else
2443 std::string::size_type const max_total_len = 1000;
2444 #endif
2446 // Enforce the maximum file name length if possible.
2447 std::string::size_type max_obj_len = max_total_len;
2448 if(dir_len < max_total_len)
2450 max_obj_len = max_total_len - dir_len;
2451 if(objName.size() > max_obj_len)
2453 // The current object file name is too long. Try to shorten it.
2454 return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
2456 else
2458 // The object file name is short enough.
2459 return true;
2462 else
2464 // The build directory in which the object will be stored is
2465 // already too deep.
2466 return false;
2469 #endif
2471 //----------------------------------------------------------------------------
2472 std::string&
2473 cmLocalGenerator
2474 ::CreateSafeUniqueObjectFileName(const char* sin,
2475 std::string::size_type dir_len)
2477 // Look for an existing mapped name for this object file.
2478 std::map<cmStdString,cmStdString>::iterator it =
2479 this->UniqueObjectNamesMap.find(sin);
2481 // If no entry exists create one.
2482 if(it == this->UniqueObjectNamesMap.end())
2484 // Start with the original name.
2485 std::string ssin = sin;
2487 // Avoid full paths by removing leading slashes.
2488 std::string::size_type pos = 0;
2489 for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
2490 ssin = ssin.substr(pos);
2492 // Avoid full paths by removing colons.
2493 cmSystemTools::ReplaceString(ssin, ":", "_");
2495 // Avoid relative paths that go up the tree.
2496 cmSystemTools::ReplaceString(ssin, "../", "__/");
2498 // Avoid spaces.
2499 cmSystemTools::ReplaceString(ssin, " ", "_");
2501 // Mangle the name if necessary.
2502 if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
2504 bool done;
2505 int cc = 0;
2506 char rpstr[100];
2507 sprintf(rpstr, "_p_");
2508 cmSystemTools::ReplaceString(ssin, "+", rpstr);
2509 std::string sssin = sin;
2512 done = true;
2513 for ( it = this->UniqueObjectNamesMap.begin();
2514 it != this->UniqueObjectNamesMap.end();
2515 ++ it )
2517 if ( it->second == ssin )
2519 done = false;
2522 if ( done )
2524 break;
2526 sssin = ssin;
2527 cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
2528 sprintf(rpstr, "_p%d_", cc++);
2530 while ( !done );
2533 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2534 cmLocalGeneratorCheckObjectName(ssin, dir_len);
2535 #else
2536 (void)dir_len;
2537 #endif
2539 // Insert the newly mapped object file name.
2540 std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
2541 it = this->UniqueObjectNamesMap.insert(e).first;
2544 // Return the map entry.
2545 return it->second;
2548 //----------------------------------------------------------------------------
2549 std::string
2550 cmLocalGenerator
2551 ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
2552 std::string::size_type dir_len,
2553 bool* hasSourceExtension)
2555 // Construct the object file name using the full path to the source
2556 // file which is its only unique identification.
2557 const char* fullPath = source.GetFullPath().c_str();
2559 // Try referencing the source relative to the source tree.
2560 std::string relFromSource = this->Convert(fullPath, START);
2561 assert(!relFromSource.empty());
2562 bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
2563 bool subSource = relSource && relFromSource[0] != '.';
2565 // Try referencing the source relative to the binary tree.
2566 std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
2567 assert(!relFromBinary.empty());
2568 bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
2569 bool subBinary = relBinary && relFromBinary[0] != '.';
2571 // Select a nice-looking reference to the source file to construct
2572 // the object file name.
2573 std::string objectName;
2574 if((relSource && !relBinary) || (subSource && !subBinary))
2576 objectName = relFromSource;
2578 else if((relBinary && !relSource) || (subBinary && !subSource))
2580 objectName = relFromBinary;
2582 else if(relFromBinary.length() < relFromSource.length())
2584 objectName = relFromBinary;
2586 else
2588 objectName = relFromSource;
2591 // if it is still a full path check for the try compile case
2592 // try compile never have in source sources, and should not
2593 // have conflicting source file names in the same target
2594 if(cmSystemTools::FileIsFullPath(objectName.c_str()))
2596 if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
2598 objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
2602 // Replace the original source file extension with the object file
2603 // extension.
2604 bool keptSourceExtension = true;
2605 if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
2607 // Decide whether this language wants to replace the source
2608 // extension with the object extension. For CMake 2.4
2609 // compatibility do this by default.
2610 bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
2611 if(!replaceExt)
2613 std::string repVar = "CMAKE_";
2614 repVar += source.GetLanguage();
2615 repVar += "_OUTPUT_EXTENSION_REPLACE";
2616 replaceExt = this->Makefile->IsOn(repVar.c_str());
2619 // Remove the source extension if it is to be replaced.
2620 if(replaceExt)
2622 keptSourceExtension = false;
2623 std::string::size_type dot_pos = objectName.rfind(".");
2624 if(dot_pos != std::string::npos)
2626 objectName = objectName.substr(0, dot_pos);
2630 // Store the new extension.
2631 objectName +=
2632 this->GlobalGenerator->GetLanguageOutputExtension(source);
2634 if(hasSourceExtension)
2636 *hasSourceExtension = keptSourceExtension;
2639 // Convert to a safe name.
2640 return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
2643 //----------------------------------------------------------------------------
2644 const char*
2645 cmLocalGenerator
2646 ::GetSourceFileLanguage(const cmSourceFile& source)
2648 return source.GetLanguage();
2651 //----------------------------------------------------------------------------
2652 std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
2654 std::string result;
2655 bool forceOn = cmSystemTools::GetForceUnixPaths();
2656 if(forceOn && this->WindowsShell)
2658 cmSystemTools::SetForceUnixPaths(false);
2660 result = cmSystemTools::EscapeSpaces(str);
2661 if(forceOn && this->WindowsShell)
2663 cmSystemTools::SetForceUnixPaths(true);
2665 return result;
2668 //----------------------------------------------------------------------------
2669 std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
2670 bool forEcho)
2672 // Compute the flags for the target shell environment.
2673 int flags = 0;
2674 if(this->WindowsVSIDE)
2676 flags |= cmsysSystem_Shell_Flag_VSIDE;
2678 else
2680 flags |= cmsysSystem_Shell_Flag_Make;
2682 if(makeVars)
2684 flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
2686 if(forEcho)
2688 flags |= cmsysSystem_Shell_Flag_EchoWindows;
2690 if(this->WatcomWMake)
2692 flags |= cmsysSystem_Shell_Flag_WatcomWMake;
2694 if(this->MinGWMake)
2696 flags |= cmsysSystem_Shell_Flag_MinGWMake;
2698 if(this->NMake)
2700 flags |= cmsysSystem_Shell_Flag_NMake;
2703 // Compute the buffer size needed.
2704 int size = (this->WindowsShell ?
2705 cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
2706 cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
2708 // Compute the shell argument itself.
2709 std::vector<char> arg(size);
2710 if(this->WindowsShell)
2712 cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
2714 else
2716 cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
2718 return std::string(&arg[0]);
2721 //----------------------------------------------------------------------------
2722 std::string cmLocalGenerator::EscapeForCMake(const char* str)
2724 // Always double-quote the argument to take care of most escapes.
2725 std::string result = "\"";
2726 for(const char* c = str; *c; ++c)
2728 if(*c == '"')
2730 // Escape the double quote to avoid ending the argument.
2731 result += "\\\"";
2733 else if(*c == '$')
2735 // Escape the dollar to avoid expanding variables.
2736 result += "\\$";
2738 else if(*c == '\\')
2740 // Escape the backslash to avoid other escapes.
2741 result += "\\\\";
2743 else
2745 // Other characters will be parsed correctly.
2746 result += *c;
2749 result += "\"";
2750 return result;
2753 //----------------------------------------------------------------------------
2754 std::string
2755 cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
2757 cmSystemTools::Error("GetTargetDirectory"
2758 " called on cmLocalGenerator");
2759 return "";
2763 //----------------------------------------------------------------------------
2764 void
2765 cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
2766 std::vector<std::string>&
2769 cmSystemTools::Error("GetTargetObjectFileDirectories"
2770 " called on cmLocalGenerator");
2773 //----------------------------------------------------------------------------
2774 unsigned int cmLocalGenerator::GetBackwardsCompatibility()
2776 // The computed version may change until the project is fully
2777 // configured.
2778 if(!this->BackwardsCompatibilityFinal)
2780 unsigned int major = 0;
2781 unsigned int minor = 0;
2782 unsigned int patch = 0;
2783 if(const char* value
2784 = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
2786 switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
2788 case 2: patch = 0; break;
2789 case 1: minor = 0; patch = 0; break;
2790 default: break;
2793 this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
2794 this->BackwardsCompatibilityFinal = this->Configured;
2797 return this->BackwardsCompatibility;
2800 //----------------------------------------------------------------------------
2801 bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
2802 unsigned int minor,
2803 unsigned int patch)
2805 // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
2806 // equal to or lower than the given version.
2807 unsigned int actual_compat = this->GetBackwardsCompatibility();
2808 return (actual_compat &&
2809 actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
2812 //----------------------------------------------------------------------------
2813 bool cmLocalGenerator::CheckDefinition(std::string const& define) const
2815 // Many compilers do not support -DNAME(arg)=sdf so we disable it.
2816 bool function_style = false;
2817 for(const char* c = define.c_str(); *c && *c != '='; ++c)
2819 if(*c == '(')
2821 function_style = true;
2822 break;
2825 if(function_style)
2827 cmOStringStream e;
2828 e << "WARNING: Function-style preprocessor definitions may not be "
2829 << "passed on the compiler command line because many compilers "
2830 << "do not support it.\n"
2831 << "CMake is dropping a preprocessor definition: " << define << "\n"
2832 << "Consider defining the macro in a (configured) header file.\n";
2833 cmSystemTools::Message(e.str().c_str());
2834 return false;
2837 // Many compilers do not support # in the value so we disable it.
2838 if(define.find_first_of("#") != define.npos)
2840 cmOStringStream e;
2841 e << "WARNING: Peprocessor definitions containing '#' may not be "
2842 << "passed on the compiler command line because many compilers "
2843 << "do not support it.\n"
2844 << "CMake is dropping a preprocessor definition: " << define << "\n"
2845 << "Consider defining the macro in a (configured) header file.\n";
2846 cmSystemTools::Message(e.str().c_str());
2847 return false;
2850 // Assume it is supported.
2851 return true;