Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmLocalGenerator.cxx
blob74de693b49b67437af3bd62a2c00cf4e6002123c
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2008/02/01 13:56:00 $
7 Version: $Revision: 1.264 $
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->LinkScriptShell = false;
54 this->IgnoreLibPrefix = false;
55 this->UseRelativePaths = false;
56 this->Configured = false;
57 this->EmitUniversalBinaryFlags = true;
58 this->IsMakefileGenerator = false;
59 this->RelativePathsConfigured = false;
60 this->PathConversionsSetup = false;
61 this->BackwardsCompatibility = 0;
62 this->BackwardsCompatibilityFinal = false;
65 cmLocalGenerator::~cmLocalGenerator()
67 delete this->Makefile;
70 void cmLocalGenerator::Configure()
72 cmLocalGenerator* previousLg =
73 this->GetGlobalGenerator()->GetCurrentLocalGenerator();
74 this->GetGlobalGenerator()->SetCurrentLocalGenerator(this);
76 // make sure the CMakeFiles dir is there
77 std::string filesDir = this->Makefile->GetStartOutputDirectory();
78 filesDir += cmake::GetCMakeFilesDirectory();
79 cmSystemTools::MakeDirectory(filesDir.c_str());
81 // find & read the list file
82 std::string currentStart = this->Makefile->GetStartDirectory();
83 currentStart += "/CMakeLists.txt";
84 this->Makefile->ReadListFile(currentStart.c_str());
86 // at the end of the ReadListFile handle any old style subdirs
87 // first get all the subdirectories
88 std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
90 // for each subdir recurse
91 std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
92 for (; sdi != subdirs.end(); ++sdi)
94 if (!(*sdi)->Configured)
96 this->Makefile->ConfigureSubDirectory(*sdi);
100 // Check whether relative paths should be used for optionally
101 // relative paths.
102 this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
104 this->Configured = true;
106 this->GetGlobalGenerator()->SetCurrentLocalGenerator(previousLg);
109 void cmLocalGenerator::SetupPathConversions()
111 // Setup the current output directory components for use by
112 // Convert
113 std::string outdir;
114 outdir =
115 cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
116 cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
117 outdir =
118 cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
119 cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
121 outdir = cmSystemTools::CollapseFullPath
122 (this->Makefile->GetHomeOutputDirectory());
123 cmSystemTools::SplitPath(outdir.c_str(),
124 this->HomeOutputDirectoryComponents);
126 outdir = cmSystemTools::CollapseFullPath
127 (this->Makefile->GetStartOutputDirectory());
128 cmSystemTools::SplitPath(outdir.c_str(),
129 this->StartOutputDirectoryComponents);
133 void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
135 this->GlobalGenerator = gg;
137 // setup the home directories
138 this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
139 this->Makefile->SetHomeDirectory(
140 gg->GetCMakeInstance()->GetHomeDirectory());
141 this->Makefile->SetHomeOutputDirectory(
142 gg->GetCMakeInstance()->GetHomeOutputDirectory());
145 void cmLocalGenerator::ConfigureFinalPass()
147 this->Makefile->ConfigureFinalPass();
150 void cmLocalGenerator::TraceDependencies()
152 // Generate the rule files for each target.
153 cmTargets& targets = this->Makefile->GetTargets();
154 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
156 // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
157 // so don't build a projectfile for it
158 if (strncmp(t->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
160 const char* projectFilename = 0;
161 if (this->IsMakefileGenerator == false) // only use of this variable
163 projectFilename = t->second.GetName();
165 t->second.TraceDependencies(projectFilename);
170 void cmLocalGenerator::GenerateTestFiles()
172 if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
174 return;
176 std::string file = this->Makefile->GetStartOutputDirectory();
177 file += "/";
178 file += "CTestTestfile.cmake";
180 cmGeneratedFileStream fout(file.c_str());
181 fout.SetCopyIfDifferent(true);
183 fout << "# CMake generated Testfile for " << std::endl
184 << "# Source directory: "
185 << this->Makefile->GetStartDirectory() << std::endl
186 << "# Build directory: "
187 << this->Makefile->GetStartOutputDirectory() << std::endl
188 << "# " << std::endl
189 << "# This file replicates the SUBDIRS() and ADD_TEST() commands "
190 << "from the source" << std::endl
191 << "# tree CMakeLists.txt file, skipping any SUBDIRS() or "
192 << "ADD_TEST() commands" << std::endl
193 << "# that are excluded by CMake control structures, i.e. IF() "
194 << "commands." << std::endl;
196 const char* testIncludeFile =
197 this->Makefile->GetProperty("TEST_INCLUDE_FILE");
198 if ( testIncludeFile )
200 fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
203 const std::vector<cmTest*> *tests = this->Makefile->GetTests();
204 std::vector<cmTest*>::const_iterator it;
205 for ( it = tests->begin(); it != tests->end(); ++ it )
207 cmTest* test = *it;
208 fout << "ADD_TEST(";
209 fout << test->GetName() << " \"" << test->GetCommand() << "\"";
211 std::vector<cmStdString>::const_iterator argit;
212 for (argit = test->GetArguments().begin();
213 argit != test->GetArguments().end(); ++argit)
215 // Just double-quote all arguments so they are re-parsed
216 // correctly by the test system.
217 fout << " \"";
218 for(std::string::const_iterator c = argit->begin();
219 c != argit->end(); ++c)
221 // Escape quotes within arguments. We should escape
222 // backslashes too but we cannot because it makes the result
223 // inconsistent with previous behavior of this command.
224 if((*c == '"'))
226 fout << '\\';
228 fout << *c;
230 fout << "\"";
232 fout << ")" << std::endl;
233 cmPropertyMap::const_iterator pit;
234 cmPropertyMap* mpit = &test->GetProperties();
235 if ( mpit->size() )
237 fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
238 for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
240 fout << " " << pit->first.c_str() << " \"";
241 const char* value = pit->second.GetValue();
242 for ( ; *value; ++ value )
244 switch ( *value )
246 case '\\':
247 case '"':
248 case ' ':
249 case '#':
250 case '(':
251 case ')':
252 case '$':
253 case '^':
254 fout << "\\" << *value;
255 break;
256 case '\t':
257 fout << "\\t";
258 break;
259 case '\n':
260 fout << "\\n";
261 break;
262 case '\r':
263 fout << "\\r";
264 break;
265 default:
266 fout << *value;
269 fout << "\"";
271 fout << ")" << std::endl;
274 if ( this->Children.size())
276 size_t i;
277 for(i = 0; i < this->Children.size(); ++i)
279 fout << "SUBDIRS(";
280 std::string outP =
281 this->Children[i]->GetMakefile()->GetStartOutputDirectory();
282 fout << this->Convert(outP.c_str(),START_OUTPUT);
283 fout << ")" << std::endl;
288 //----------------------------------------------------------------------------
289 void cmLocalGenerator::GenerateInstallRules()
291 // Compute the install prefix.
292 const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
293 #if defined(_WIN32) && !defined(__CYGWIN__)
294 std::string prefix_win32;
295 if(!prefix)
297 if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
299 prefix_win32 = "C:";
301 const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
302 if(project_name && project_name[0])
304 prefix_win32 += "/Program Files/";
305 prefix_win32 += project_name;
307 else
309 prefix_win32 += "/InstalledCMakeProject";
311 prefix = prefix_win32.c_str();
313 #else
314 if (!prefix)
316 prefix = "/usr/local";
318 #endif
320 // Compute the set of configurations.
321 std::vector<std::string> configurationTypes;
322 if(const char* types =
323 this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
325 cmSystemTools::ExpandListArgument(types, configurationTypes);
327 const char* config = 0;
328 if(configurationTypes.empty())
330 config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
333 // Choose a default install configuration.
334 const char* default_config = config;
335 const char* default_order[] = {"RELEASE", "MINSIZEREL",
336 "RELWITHDEBINFO", "DEBUG", 0};
337 for(const char** c = default_order; *c && !default_config; ++c)
339 for(std::vector<std::string>::iterator i = configurationTypes.begin();
340 i != configurationTypes.end(); ++i)
342 if(cmSystemTools::UpperCase(*i) == *c)
344 default_config = i->c_str();
348 if(!default_config && !configurationTypes.empty())
350 default_config = configurationTypes[0].c_str();
352 if(!default_config)
354 default_config = "Release";
357 // Create the install script file.
358 std::string file = this->Makefile->GetStartOutputDirectory();
359 std::string homedir = this->Makefile->GetHomeOutputDirectory();
360 std::string currdir = this->Makefile->GetCurrentOutputDirectory();
361 cmSystemTools::ConvertToUnixSlashes(file);
362 cmSystemTools::ConvertToUnixSlashes(homedir);
363 cmSystemTools::ConvertToUnixSlashes(currdir);
364 int toplevel_install = 0;
365 if ( currdir == homedir )
367 toplevel_install = 1;
369 file += "/cmake_install.cmake";
370 cmGeneratedFileStream fout(file.c_str());
371 fout.SetCopyIfDifferent(true);
373 // Write the header.
374 fout << "# Install script for directory: "
375 << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
376 fout << "# Set the install prefix" << std::endl
377 << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
378 << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
379 << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
380 << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
381 << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
382 << std::endl;
384 // Write support code for generating per-configuration install rules.
385 fout <<
386 "# Set the install configuration name.\n"
387 "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
388 " IF(BUILD_TYPE)\n"
389 " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
390 " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
391 " ELSE(BUILD_TYPE)\n"
392 " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
393 " ENDIF(BUILD_TYPE)\n"
394 " MESSAGE(STATUS \"Install configuration: "
395 "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
396 "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
397 "\n";
399 // Write support code for dealing with component-specific installs.
400 fout <<
401 "# Set the component getting installed.\n"
402 "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
403 " IF(COMPONENT)\n"
404 " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
405 " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
406 " ELSE(COMPONENT)\n"
407 " SET(CMAKE_INSTALL_COMPONENT)\n"
408 " ENDIF(COMPONENT)\n"
409 "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
410 "\n";
412 // Copy user-specified install options to the install code.
413 if(const char* so_no_exe =
414 this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
416 fout <<
417 "# Install shared libraries without execute permission?\n"
418 "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
419 " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
420 "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
421 "\n";
424 // Ask each install generator to write its code.
425 std::vector<cmInstallGenerator*> const& installers =
426 this->Makefile->GetInstallGenerators();
427 for(std::vector<cmInstallGenerator*>::const_iterator
428 gi = installers.begin();
429 gi != installers.end(); ++gi)
431 (*gi)->Generate(fout, config, configurationTypes);
434 // Write rules from old-style specification stored in targets.
435 this->GenerateTargetInstallRules(fout, config, configurationTypes);
437 // Include install scripts from subdirectories.
438 if(!this->Children.empty())
440 fout << "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
441 fout << " # Include the install script for each subdirectory.\n";
442 for(std::vector<cmLocalGenerator*>::const_iterator
443 ci = this->Children.begin(); ci != this->Children.end(); ++ci)
445 if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
447 std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
448 cmSystemTools::ConvertToUnixSlashes(odir);
449 fout << " INCLUDE(\"" << odir.c_str()
450 << "/cmake_install.cmake\")" << std::endl;
453 fout << "\n";
454 fout << "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n\n";
457 // Record the install manifest.
458 if ( toplevel_install )
460 fout <<
461 "IF(CMAKE_INSTALL_COMPONENT)\n"
462 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
463 "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
464 "ELSE(CMAKE_INSTALL_COMPONENT)\n"
465 " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
466 "ENDIF(CMAKE_INSTALL_COMPONENT)\n\n";
467 fout
468 << "FILE(WRITE \""
469 << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
470 << "\"\")" << std::endl;
471 fout
472 << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
473 << " FILE(APPEND \""
474 << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
475 << "\"${file}\\n\")" << std::endl
476 << "ENDFOREACH(file)" << std::endl;
480 //----------------------------------------------------------------------------
481 void cmLocalGenerator::GenerateTargetManifest()
483 // Collect the set of configuration types.
484 std::vector<std::string> configNames;
485 if(const char* configurationTypes =
486 this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
488 cmSystemTools::ExpandListArgument(configurationTypes, configNames);
490 else if(const char* buildType =
491 this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
493 if(*buildType)
495 configNames.push_back(buildType);
499 // Add our targets to the manifest for each configuration.
500 cmTargets& targets = this->Makefile->GetTargets();
501 for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
503 cmTarget& target = t->second;
504 if(configNames.empty())
506 target.GenerateTargetManifest(0);
508 else
510 for(std::vector<std::string>::iterator ci = configNames.begin();
511 ci != configNames.end(); ++ci)
513 const char* config = ci->c_str();
514 target.GenerateTargetManifest(config);
520 void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
521 const char* lang,
522 cmSourceFile& source,
523 cmTarget& )
525 std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
526 objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
527 std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
528 std::string sourceFile =
529 this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
530 std::string varString = "CMAKE_";
531 varString += lang;
532 varString += "_COMPILE_OBJECT";
533 std::vector<std::string> rules;
534 rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
535 varString = "CMAKE_";
536 varString += lang;
537 varString += "_FLAGS";
538 std::string flags;
539 flags += this->Makefile->GetSafeDefinition(varString.c_str());
540 flags += " ";
541 flags += this->GetIncludeFlags(lang);
543 // Construct the command lines.
544 cmCustomCommandLines commandLines;
545 std::vector<std::string> commands;
546 cmSystemTools::ExpandList(rules, commands);
547 cmLocalGenerator::RuleVariables vars;
548 vars.Language = lang;
549 vars.Source = sourceFile.c_str();
550 vars.Object = objectFile.c_str();
551 vars.ObjectDir = objectDir.c_str();
552 vars.Flags = flags.c_str();
553 for(std::vector<std::string>::iterator i = commands.begin();
554 i != commands.end(); ++i)
556 // Expand the full command line string.
557 this->ExpandRuleVariables(*i, vars);
559 // Parse the string to get the custom command line.
560 cmCustomCommandLine commandLine;
561 std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
562 for(std::vector<cmStdString>::iterator a = cmd.begin();
563 a != cmd.end(); ++a)
565 commandLine.push_back(*a);
568 // Store this command line.
569 commandLines.push_back(commandLine);
572 // Check for extra object-file dependencies.
573 std::vector<std::string> depends;
574 const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
575 if(additionalDeps)
577 cmSystemTools::ExpandListArgument(additionalDeps, depends);
580 // Generate a meaningful comment for the command.
581 std::string comment = "Building ";
582 comment += lang;
583 comment += " object ";
584 comment += this->Convert(ofname, START_OUTPUT);
586 // Add the custom command to build the object file.
587 this->Makefile->AddCustomCommandToOutput(
588 ofname,
589 depends,
590 source.GetFullPath().c_str(),
591 commandLines,
592 comment.c_str(),
593 this->Makefile->GetStartOutputDirectory()
597 void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
599 cmStdString objs;
600 std::vector<std::string> objVector;
601 // Add all the sources outputs to the depends of the target
602 std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
603 for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
604 i != classes.end(); ++i)
606 cmSourceFile* sf = *i;
607 if(!sf->GetCustomCommand() &&
608 !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
609 !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
611 std::string::size_type dir_len = 0;
612 dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
613 dir_len += 1;
614 std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_len);
615 if(!obj.empty())
617 std::string ofname = this->Makefile->GetCurrentOutputDirectory();
618 ofname += "/";
619 ofname += obj;
620 objVector.push_back(ofname);
621 this->AddCustomCommandToCreateObject(ofname.c_str(),
622 llang, *(*i), target);
623 objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
624 objs += " ";
628 std::string createRule = "CMAKE_";
629 createRule += llang;
630 createRule += target.GetCreateRuleVariable();
631 std::string targetName = target.GetFullName();
632 // Executable :
633 // Shared Library:
634 // Static Library:
635 // Shared Module:
636 std::string linkLibs; // should be set
637 std::string flags; // should be set
638 std::string linkFlags; // should be set
639 this->GetTargetFlags(linkLibs, flags, linkFlags, target);
640 cmLocalGenerator::RuleVariables vars;
641 vars.Language = llang;
642 vars.Objects = objs.c_str();
643 vars.ObjectDir = ".";
644 vars.Target = targetName.c_str();
645 vars.LinkLibraries = linkLibs.c_str();
646 vars.Flags = flags.c_str();
647 vars.LinkFlags = linkFlags.c_str();
649 std::string langFlags;
650 this->AddLanguageFlags(langFlags, llang, 0);
651 vars.LanguageCompileFlags = langFlags.c_str();
653 cmCustomCommandLines commandLines;
654 std::vector<std::string> rules;
655 rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
656 std::vector<std::string> commands;
657 cmSystemTools::ExpandList(rules, commands);
658 for(std::vector<std::string>::iterator i = commands.begin();
659 i != commands.end(); ++i)
661 // Expand the full command line string.
662 this->ExpandRuleVariables(*i, vars);
663 // Parse the string to get the custom command line.
664 cmCustomCommandLine commandLine;
665 std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
666 for(std::vector<cmStdString>::iterator a = cmd.begin();
667 a != cmd.end(); ++a)
669 commandLine.push_back(*a);
672 // Store this command line.
673 commandLines.push_back(commandLine);
675 std::string targetFullPath = target.GetFullPath();
676 // Generate a meaningful comment for the command.
677 std::string comment = "Linking ";
678 comment += llang;
679 comment += " target ";
680 comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
681 this->Makefile->AddCustomCommandToOutput(
682 targetFullPath.c_str(),
683 objVector,
685 commandLines,
686 comment.c_str(),
687 this->Makefile->GetStartOutputDirectory()
689 target.AddSourceFile
690 (this->Makefile->GetSource(targetFullPath.c_str()));
694 void cmLocalGenerator
695 ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
697 cmTargets &tgts = this->Makefile->GetTargets();
698 for(cmTargets::iterator l = tgts.begin();
699 l != tgts.end(); l++)
701 cmTarget& target = l->second;
702 switch(target.GetType())
704 case cmTarget::STATIC_LIBRARY:
705 case cmTarget::SHARED_LIBRARY:
706 case cmTarget::MODULE_LIBRARY:
707 case cmTarget::EXECUTABLE:
709 const char* llang =
710 target.GetLinkerLanguage(this->GetGlobalGenerator());
711 if(!llang)
713 cmSystemTools::Error
714 ("CMake can not determine linker language for target:",
715 target.GetName());
716 return;
718 // if the language is not in the set lang then create custom
719 // commands to build the target
720 if(lang.count(llang) == 0)
722 this->AddBuildTargetRule(llang, target);
725 break;
726 case cmTarget::UTILITY:
727 case cmTarget::GLOBAL_TARGET:
728 case cmTarget::INSTALL_FILES:
729 case cmTarget::INSTALL_PROGRAMS:
730 case cmTarget::INSTALL_DIRECTORY:
731 break;
736 // List of variables that are replaced when
737 // rules are expanced. These variables are
738 // replaced in the form <var> with GetSafeDefinition(var).
739 // ${LANG} is replaced in the variable first with all enabled
740 // languages.
741 static const char* ruleReplaceVars[] =
743 "CMAKE_${LANG}_COMPILER",
744 "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
745 "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
746 "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
747 "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
748 "CMAKE_${LANG}_LINK_FLAGS",
749 "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
750 "CMAKE_${LANG}_ARCHIVE",
751 "CMAKE_AR",
752 "CMAKE_CURRENT_SOURCE_DIR",
753 "CMAKE_CURRENT_BINARY_DIR",
754 "CMAKE_RANLIB",
755 "CMAKE_LINKER",
759 std::string
760 cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
761 const RuleVariables& replaceValues)
763 if(replaceValues.LinkFlags)
765 if(variable == "LINK_FLAGS")
767 return replaceValues.LinkFlags;
770 if(replaceValues.Flags)
772 if(variable == "FLAGS")
774 return replaceValues.Flags;
778 if(replaceValues.Source)
780 if(variable == "SOURCE")
782 return replaceValues.Source;
785 if(replaceValues.PreprocessedSource)
787 if(variable == "PREPROCESSED_SOURCE")
789 return replaceValues.PreprocessedSource;
792 if(replaceValues.AssemblySource)
794 if(variable == "ASSEMBLY_SOURCE")
796 return replaceValues.AssemblySource;
799 if(replaceValues.Object)
801 if(variable == "OBJECT")
803 return replaceValues.Object;
806 if(replaceValues.ObjectDir)
808 if(variable == "OBJECT_DIR")
810 return replaceValues.ObjectDir;
813 if(replaceValues.Objects)
815 if(variable == "OBJECTS")
817 return replaceValues.Objects;
820 if(replaceValues.ObjectsQuoted)
822 if(variable == "OBJECTS_QUOTED")
824 return replaceValues.ObjectsQuoted;
827 if(replaceValues.Defines && variable == "DEFINES")
829 return replaceValues.Defines;
831 if(replaceValues.TargetPDB )
833 if(variable == "TARGET_PDB")
835 return replaceValues.TargetPDB;
839 if(replaceValues.Target)
841 if(variable == "TARGET_QUOTED")
843 std::string targetQuoted = replaceValues.Target;
844 if(targetQuoted.size() && targetQuoted[0] != '\"')
846 targetQuoted = '\"';
847 targetQuoted += replaceValues.Target;
848 targetQuoted += '\"';
850 return targetQuoted;
852 if(replaceValues.LanguageCompileFlags)
854 if(variable == "LANGUAGE_COMPILE_FLAGS")
856 return replaceValues.LanguageCompileFlags;
859 if(replaceValues.Target)
861 if(variable == "TARGET")
863 return replaceValues.Target;
866 if(variable == "TARGET_IMPLIB")
868 return this->TargetImplib;
870 if(variable == "TARGET_VERSION_MAJOR")
872 if(replaceValues.TargetVersionMajor)
874 return replaceValues.TargetVersionMajor;
876 else
878 return "0";
881 if(variable == "TARGET_VERSION_MINOR")
883 if(replaceValues.TargetVersionMinor)
885 return replaceValues.TargetVersionMinor;
887 else
889 return "0";
892 if(replaceValues.Target)
894 if(variable == "TARGET_BASE")
896 // Strip the last extension off the target name.
897 std::string targetBase = replaceValues.Target;
898 std::string::size_type pos = targetBase.rfind(".");
899 if(pos != targetBase.npos)
901 return targetBase.substr(0, pos);
903 else
905 return targetBase;
910 if(replaceValues.TargetSOName)
912 if(variable == "TARGET_SONAME")
914 if(replaceValues.Language)
916 std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
917 name += replaceValues.Language;
918 name += "_FLAG";
919 if(this->Makefile->GetDefinition(name.c_str()))
921 return replaceValues.TargetSOName;
924 return "";
927 if(replaceValues.TargetInstallNameDir)
929 if(variable == "TARGET_INSTALLNAME_DIR")
931 return replaceValues.TargetInstallNameDir;
934 if(replaceValues.LinkLibraries)
936 if(variable == "LINK_LIBRARIES")
938 return replaceValues.LinkLibraries;
941 std::vector<std::string> enabledLanguages;
942 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
943 // loop over language specific replace variables
944 int pos = 0;
945 while(ruleReplaceVars[pos])
947 for(std::vector<std::string>::iterator i = enabledLanguages.begin();
948 i != enabledLanguages.end(); ++i)
950 const char* lang = i->c_str();
951 std::string actualReplace = ruleReplaceVars[pos];
952 // If this is the compiler then look for the extra variable
953 // _COMPILER_ARG1 which must be the first argument to the compiler
954 const char* compilerArg1 = 0;
955 if(actualReplace == "CMAKE_${LANG}_COMPILER")
957 std::string arg1 = actualReplace + "_ARG1";
958 cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
959 compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
961 if(actualReplace.find("${LANG}") != actualReplace.npos)
963 cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
965 if(actualReplace == variable)
967 std::string replace =
968 this->Makefile->GetSafeDefinition(variable.c_str());
969 // if the variable is not a FLAG then treat it like a path
970 if(variable.find("_FLAG") == variable.npos)
972 std::string ret = this->ConvertToOutputForExisting(replace.c_str());
973 // if there is a required first argument to the compiler add it
974 // to the compiler string
975 if(compilerArg1)
977 ret += " ";
978 ret += compilerArg1;
980 return ret;
982 return replace;
985 pos++;
987 return variable;
991 void
992 cmLocalGenerator::ExpandRuleVariables(std::string& s,
993 const RuleVariables& replaceValues)
995 std::vector<std::string> enabledLanguages;
996 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
997 std::string::size_type start = s.find('<');
998 // no variables to expand
999 if(start == s.npos)
1001 return;
1003 std::string::size_type pos = 0;
1004 std::string expandedInput;
1005 while(start != s.npos && start < s.size()-2)
1007 std::string::size_type end = s.find('>', start);
1008 // if we find a < with no > we are done
1009 if(end == s.npos)
1011 return;
1013 char c = s[start+1];
1014 // if the next char after the < is not A-Za-z then
1015 // skip it and try to find the next < in the string
1016 if(!isalpha(c))
1018 start = s.find('<', start+1);
1020 else
1022 // extract the var
1023 std::string var = s.substr(start+1, end - start-1);
1024 std::string replace = this->ExpandRuleVariable(var,
1025 replaceValues);
1026 expandedInput += s.substr(pos, start-pos);
1027 expandedInput += replace;
1028 // move to next one
1029 start = s.find('<', start+var.size()+2);
1030 pos = end+1;
1033 // add the rest of the input
1034 expandedInput += s.substr(pos, s.size()-pos);
1035 s = expandedInput;
1039 std::string
1040 cmLocalGenerator::ConvertToOutputForExisting(const char* p)
1042 std::string ret = p;
1043 if(this->WindowsShell && ret.find(' ') != ret.npos
1044 && cmSystemTools::FileExists(p))
1046 if(cmSystemTools::GetShortPath(p, ret))
1048 return this->Convert(ret.c_str(), NONE, SHELL, true);
1051 return this->Convert(p, START_OUTPUT, SHELL, true);
1054 const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
1056 if(!lang)
1058 return "";
1060 if(this->LanguageToIncludeFlags.count(lang))
1062 return this->LanguageToIncludeFlags[lang].c_str();
1064 cmOStringStream includeFlags;
1065 std::vector<std::string> includes;
1066 this->GetIncludeDirectories(includes);
1067 std::vector<std::string>::iterator i;
1069 std::string flagVar = "CMAKE_INCLUDE_FLAG_";
1070 flagVar += lang;
1071 const char* includeFlag =
1072 this->Makefile->GetSafeDefinition(flagVar.c_str());
1073 flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
1074 flagVar += lang;
1075 const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
1076 bool quotePaths = false;
1077 if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
1079 quotePaths = true;
1081 bool repeatFlag = true;
1082 // should the include flag be repeated like ie. -IA -IB
1083 if(!sep)
1085 sep = " ";
1087 else
1089 // if there is a separator then the flag is not repeated but is only
1090 // given once i.e. -classpath a:b:c
1091 repeatFlag = false;
1094 // Support special system include flag if it is available and the
1095 // normal flag is repeated for each directory.
1096 std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
1097 sysFlagVar += lang;
1098 const char* sysIncludeFlag = 0;
1099 if(repeatFlag)
1101 sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
1104 bool flagUsed = false;
1105 std::set<cmStdString> emitted;
1106 #ifdef __APPLE__
1107 emitted.insert("/System/Library/Frameworks");
1108 #endif
1109 for(i = includes.begin(); i != includes.end(); ++i)
1111 #ifdef __APPLE__
1112 if(cmSystemTools::IsPathToFramework(i->c_str()))
1114 std::string frameworkDir = *i;
1115 frameworkDir += "/../";
1116 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1117 if(emitted.insert(frameworkDir).second)
1119 includeFlags
1120 << "-F"
1121 << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
1123 continue;
1125 #endif
1126 std::string include = *i;
1127 if(!flagUsed || repeatFlag)
1129 if(sysIncludeFlag &&
1130 this->Makefile->IsSystemIncludeDirectory(i->c_str()))
1132 includeFlags << sysIncludeFlag;
1134 else
1136 includeFlags << includeFlag;
1138 flagUsed = true;
1140 std::string includePath = this->ConvertToOutputForExisting(i->c_str());
1141 if(quotePaths && includePath.size() && includePath[0] != '\"')
1143 includeFlags << "\"";
1145 includeFlags << includePath;
1146 if(quotePaths && includePath.size() && includePath[0] != '\"')
1148 includeFlags << "\"";
1150 includeFlags << sep;
1152 std::string flags = includeFlags.str();
1153 // remove trailing separators
1154 if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
1156 flags[flags.size()-1] = ' ';
1158 std::string defineFlags = this->Makefile->GetDefineFlags();
1159 flags += defineFlags;
1160 this->LanguageToIncludeFlags[lang] = flags;
1162 // Use this temorary variable for the return value to work-around a
1163 // bogus GCC 2.95 warning.
1164 const char* ret = this->LanguageToIncludeFlags[lang].c_str();
1165 return ret;
1168 //----------------------------------------------------------------------------
1169 void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
1170 bool filter_system_dirs)
1172 // Need to decide whether to automatically include the source and
1173 // binary directories at the beginning of the include path.
1174 bool includeSourceDir = false;
1175 bool includeBinaryDir = false;
1177 // When automatic include directories are requested for a build then
1178 // include the source and binary directories at the beginning of the
1179 // include path to approximate include file behavior for an
1180 // in-source build. This does not account for the case of a source
1181 // file in a subdirectory of the current source directory but we
1182 // cannot fix this because not all native build tools support
1183 // per-source-file include paths.
1184 if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
1186 includeSourceDir = true;
1187 includeBinaryDir = true;
1190 // CMake versions below 2.0 would add the source tree to the -I path
1191 // automatically. Preserve compatibility.
1192 const char* versionValue =
1193 this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
1194 int major = 0;
1195 int minor = 0;
1196 if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
1198 versionValue = 0;
1200 if(versionValue && major < 2)
1202 includeSourceDir = true;
1205 // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
1206 // not set the backwards compatibility level automatically.
1207 const char* vtkSourceDir =
1208 this->Makefile->GetDefinition("VTK_SOURCE_DIR");
1209 if(vtkSourceDir)
1211 const char* vtk_major =
1212 this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
1213 const char* vtk_minor =
1214 this->Makefile->GetDefinition("VTK_MINOR_VERSION");
1215 vtk_major = vtk_major? vtk_major : "4";
1216 vtk_minor = vtk_minor? vtk_minor : "4";
1217 int vmajor = 0;
1218 int vminor = 0;
1219 if(sscanf(vtk_major, "%d", &vmajor) &&
1220 sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
1222 includeSourceDir = true;
1226 // Do not repeat an include path.
1227 std::set<cmStdString> emitted;
1229 // Store the automatic include paths.
1230 if(includeBinaryDir)
1232 dirs.push_back(this->Makefile->GetStartOutputDirectory());
1233 emitted.insert(this->Makefile->GetStartOutputDirectory());
1235 if(includeSourceDir)
1237 if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
1239 dirs.push_back(this->Makefile->GetStartDirectory());
1240 emitted.insert(this->Makefile->GetStartDirectory());
1244 if(filter_system_dirs)
1246 // Do not explicitly add the standard include path "/usr/include".
1247 // This can cause problems with certain standard library
1248 // implementations because the wrong headers may be found first.
1249 emitted.insert("/usr/include");
1250 if(const char* implicitIncludes = this->Makefile->GetDefinition
1251 ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
1253 std::vector<std::string> implicitIncludeVec;
1254 cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
1255 for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
1257 emitted.insert(implicitIncludeVec[k]);
1262 // Get the project-specified include directories.
1263 std::vector<std::string>& includes =
1264 this->Makefile->GetIncludeDirectories();
1266 // Support putting all the in-project include directories first if
1267 // it is requested by the project.
1268 if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
1270 const char* topSourceDir = this->Makefile->GetHomeDirectory();
1271 const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
1272 for(std::vector<std::string>::iterator i = includes.begin();
1273 i != includes.end(); ++i)
1275 // Emit this directory only if it is a subdirectory of the
1276 // top-level source or binary tree.
1277 if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
1278 cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
1279 cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
1280 cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
1282 if(emitted.insert(*i).second)
1284 dirs.push_back(*i);
1290 // Construct the final ordered include directory list.
1291 for(std::vector<std::string>::iterator i = includes.begin();
1292 i != includes.end(); ++i)
1294 if(emitted.insert(*i).second)
1296 dirs.push_back(*i);
1301 void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
1302 std::string& flags,
1303 std::string& linkFlags,
1304 cmTarget& target)
1306 std::string buildType =
1307 this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
1308 buildType = cmSystemTools::UpperCase(buildType);
1309 const char* libraryLinkVariable =
1310 "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
1312 switch(target.GetType())
1314 case cmTarget::STATIC_LIBRARY:
1316 const char* targetLinkFlags =
1317 target.GetProperty("STATIC_LIBRARY_FLAGS");
1318 if(targetLinkFlags)
1320 linkFlags += targetLinkFlags;
1321 linkFlags += " ";
1324 break;
1325 case cmTarget::MODULE_LIBRARY:
1326 libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
1327 case cmTarget::SHARED_LIBRARY:
1329 linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
1330 linkFlags += " ";
1331 if(buildType.size())
1333 std::string build = libraryLinkVariable;
1334 build += "_";
1335 build += buildType;
1336 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1337 linkFlags += " ";
1339 if(this->Makefile->IsOn("WIN32") &&
1340 !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
1342 const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
1343 for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
1344 i != sources.end(); ++i)
1346 cmSourceFile* sf = *i;
1347 if(sf->GetExtension() == "def")
1349 linkFlags +=
1350 this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
1351 linkFlags += this->Convert(sf->GetFullPath().c_str(),
1352 START_OUTPUT, SHELL);
1353 linkFlags += " ";
1357 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1358 if(targetLinkFlags)
1360 linkFlags += targetLinkFlags;
1361 linkFlags += " ";
1362 std::string configLinkFlags = targetLinkFlags;
1363 configLinkFlags += buildType;
1364 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1365 if(targetLinkFlags)
1367 linkFlags += targetLinkFlags;
1368 linkFlags += " ";
1371 cmOStringStream linklibsStr;
1372 this->OutputLinkLibraries(linklibsStr, target, false);
1373 linkLibs = linklibsStr.str();
1375 break;
1376 case cmTarget::EXECUTABLE:
1378 linkFlags +=
1379 this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
1380 linkFlags += " ";
1381 if(buildType.size())
1383 std::string build = "CMAKE_EXE_LINKER_FLAGS_";
1384 build += buildType;
1385 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1386 linkFlags += " ";
1388 const char* linkLanguage =
1389 target.GetLinkerLanguage(this->GetGlobalGenerator());
1390 if(!linkLanguage)
1392 cmSystemTools::Error
1393 ("CMake can not determine linker language for target:",
1394 target.GetName());
1395 return;
1397 std::string langVar = "CMAKE_";
1398 langVar += linkLanguage;
1399 std::string flagsVar = langVar + "_FLAGS";
1400 std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
1401 sharedFlagsVar += linkLanguage;
1402 sharedFlagsVar += "_FLAGS";
1403 flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
1404 flags += " ";
1405 flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
1406 flags += " ";
1407 cmOStringStream linklibs;
1408 this->OutputLinkLibraries(linklibs, target, false);
1409 linkLibs = linklibs.str();
1410 if(cmSystemTools::IsOn
1411 (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1413 std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
1414 + linkLanguage + std::string("_FLAGS");
1415 linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
1416 linkFlags += " ";
1418 if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
1420 linkFlags +=
1421 this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
1422 linkFlags += " ";
1424 else
1426 linkFlags +=
1427 this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
1428 linkFlags += " ";
1430 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1431 if(targetLinkFlags)
1433 linkFlags += targetLinkFlags;
1434 linkFlags += " ";
1435 std::string configLinkFlags = targetLinkFlags;
1436 configLinkFlags += buildType;
1437 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1438 if(targetLinkFlags)
1440 linkFlags += targetLinkFlags;
1441 linkFlags += " ";
1445 break;
1446 case cmTarget::UTILITY:
1447 case cmTarget::GLOBAL_TARGET:
1448 case cmTarget::INSTALL_FILES:
1449 case cmTarget::INSTALL_PROGRAMS:
1450 case cmTarget::INSTALL_DIRECTORY:
1451 break;
1455 std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib)
1457 #if defined(_WIN32) && !defined(__CYGWIN__)
1458 // Work-ardound command line parsing limitations in MSVC 6.0 and
1459 // Watcom.
1460 if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
1462 // Search for the last space.
1463 std::string::size_type pos = lib.rfind(' ');
1464 if(pos != lib.npos)
1466 // Find the slash after the last space, if any.
1467 pos = lib.find('/', pos);
1469 // Convert the portion of the path with a space to a short path.
1470 std::string sp;
1471 if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
1473 // Append the rest of the path with no space.
1474 sp += lib.substr(pos);
1476 // Convert to an output path.
1477 return this->Convert(sp.c_str(), NONE, SHELL);
1481 #endif
1483 // Normal behavior.
1484 return this->Convert(lib.c_str(), START_OUTPUT, SHELL);
1488 * Output the linking rules on a command line. For executables,
1489 * targetLibrary should be a NULL pointer. For libraries, it should point
1490 * to the name of the library. This will not link a library against itself.
1492 void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
1493 cmTarget& tgt,
1494 bool relink)
1496 const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
1497 cmComputeLinkInformation* pcli = tgt.GetLinkInformation(config);
1498 if(!pcli)
1500 return;
1502 cmComputeLinkInformation& cli = *pcli;
1504 // Collect library linking flags command line options.
1505 std::string linkLibs;
1507 const char* linkLanguage = cli.GetLinkLanguage();
1509 std::string libPathFlag =
1510 this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1511 std::string libPathTerminator =
1512 this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1514 // Flags to link an executable to shared libraries.
1515 std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
1516 linkFlagsVar += linkLanguage;
1517 linkFlagsVar += "_FLAGS";
1518 if( tgt.GetType() == cmTarget::EXECUTABLE )
1520 linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
1521 linkLibs += " ";
1524 // Append the framework search path flags.
1525 std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
1526 for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
1527 fdi != fwDirs.end(); ++fdi)
1529 linkLibs += "-F";
1530 linkLibs += this->Convert(fdi->c_str(), NONE, SHELL, false);
1531 linkLibs += " ";
1534 // Append the library search path flags.
1535 std::vector<std::string> const& libDirs = cli.GetDirectories();
1536 for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
1537 libDir != libDirs.end(); ++libDir)
1539 std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
1540 linkLibs += libPathFlag;
1541 linkLibs += libpath;
1542 linkLibs += libPathTerminator;
1543 linkLibs += " ";
1546 // Append the link items.
1547 typedef cmComputeLinkInformation::ItemVector ItemVector;
1548 ItemVector const& items = cli.GetItems();
1549 for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
1551 if(li->IsPath)
1553 linkLibs += this->ConvertToLinkReference(li->Value);
1555 else
1557 linkLibs += li->Value;
1559 linkLibs += " ";
1562 // Write the library flags to the build rule.
1563 fout << linkLibs;
1565 // Get the RPATH entries.
1566 std::vector<std::string> runtimeDirs;
1567 cli.GetRPath(runtimeDirs, relink);
1569 // Check what kind of rpath flags to use.
1570 if(cli.GetRuntimeSep().empty())
1572 // Each rpath entry gets its own option ("-R a -R b -R c")
1573 std::string rpath;
1574 for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
1575 ri != runtimeDirs.end(); ++ri)
1577 rpath += cli.GetRuntimeFlag();
1578 rpath += this->Convert(ri->c_str(), FULL, SHELL, false);
1579 rpath += " ";
1581 fout << rpath;
1583 else
1585 // All rpath entries are combined ("-Wl,-rpath,a:b:c").
1586 std::string rpath = cli.GetRPathString(relink);
1588 // If not relinking, make sure the rpath string is long enough to
1589 // support a subsequent chrpath on installation.
1590 if(!relink)
1592 std::string::size_type minLength = cli.GetChrpathString().size();
1593 while(rpath.size() < minLength)
1595 rpath += cli.GetRuntimeSep();
1599 // Store the rpath option in the stream.
1600 if(!rpath.empty())
1602 fout << cli.GetRuntimeFlag();
1603 fout << this->EscapeForShell(rpath.c_str(), true);
1604 fout << " ";
1608 // Add the linker runtime search path if any.
1609 std::string rpath_link = cli.GetRPathLinkString();
1610 if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
1612 fout << cli.GetRPathLinkFlag();
1613 fout << this->EscapeForShell(rpath_link.c_str(), true);
1614 fout << " ";
1617 // Add standard libraries for this language.
1618 std::string standardLibsVar = "CMAKE_";
1619 standardLibsVar += cli.GetLinkLanguage();
1620 standardLibsVar += "_STANDARD_LIBRARIES";
1621 if(const char* stdLibs =
1622 this->Makefile->GetDefinition(standardLibsVar.c_str()))
1624 fout << stdLibs << " ";
1628 //----------------------------------------------------------------------------
1629 void cmLocalGenerator::AddLanguageFlags(std::string& flags,
1630 const char* lang,
1631 const char* config)
1633 // Add language-specific flags.
1634 std::string flagsVar = "CMAKE_";
1635 flagsVar += lang;
1636 flagsVar += "_FLAGS";
1637 if(this->EmitUniversalBinaryFlags)
1639 const char* osxArch =
1640 this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
1641 const char* sysroot =
1642 this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
1643 if(osxArch && sysroot && lang && lang[0] =='C')
1645 std::vector<std::string> archs;
1646 cmSystemTools::ExpandListArgument(std::string(osxArch),
1647 archs);
1648 bool addArchFlag = true;
1649 if(archs.size() == 1)
1651 const char* archOrig =
1652 this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
1653 if(archs[0] == archOrig)
1655 addArchFlag = false;
1658 // if there is more than one arch add the -arch and
1659 // -isysroot flags, or if there is one arch flag, but
1660 // it is not the default -arch flag for the system, then
1661 // add it. Otherwize do not add -arch and -isysroot
1662 if(addArchFlag)
1664 for( std::vector<std::string>::iterator i = archs.begin();
1665 i != archs.end(); ++i)
1667 flags += " -arch ";
1668 flags += *i;
1670 flags += " -isysroot ";
1671 flags += sysroot;
1675 this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
1678 //----------------------------------------------------------------------------
1679 std::string cmLocalGenerator::GetRealDependency(const char* inName,
1680 const char* config)
1682 // Older CMake code may specify the dependency using the target
1683 // output file rather than the target name. Such code would have
1684 // been written before there was support for target properties that
1685 // modify the name so stripping down to just the file name should
1686 // produce the target name in this case.
1687 std::string name = cmSystemTools::GetFilenameName(inName);
1688 if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
1690 name = cmSystemTools::GetFilenameWithoutLastExtension(name);
1693 // Look for a CMake target with the given name.
1694 if(cmTarget* target = this->Makefile->FindTargetToUse(name.c_str()))
1696 // make sure it is not just a coincidence that the target name
1697 // found is part of the inName
1698 if(cmSystemTools::FileIsFullPath(inName))
1700 std::string tLocation = target->GetLocation(config);
1701 tLocation = cmSystemTools::GetFilenamePath(tLocation);
1702 std::string depLocation = cmSystemTools::GetFilenamePath(
1703 std::string(inName));
1704 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
1705 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
1706 if(depLocation != tLocation)
1708 // it is a full path to a depend that has the same name
1709 // as a target but is in a different location so do not use
1710 // the target as the depend
1711 return inName;
1714 switch (target->GetType())
1716 case cmTarget::EXECUTABLE:
1717 case cmTarget::STATIC_LIBRARY:
1718 case cmTarget::SHARED_LIBRARY:
1719 case cmTarget::MODULE_LIBRARY:
1721 // Get the location of the target's output file and depend on it.
1722 if(const char* location = target->GetLocation(config))
1724 return location;
1727 break;
1728 case cmTarget::UTILITY:
1729 case cmTarget::GLOBAL_TARGET:
1730 // Depending on a utility target may not work but just trust
1731 // the user to have given a valid name.
1732 return inName;
1733 case cmTarget::INSTALL_FILES:
1734 case cmTarget::INSTALL_PROGRAMS:
1735 case cmTarget::INSTALL_DIRECTORY:
1736 break;
1740 // The name was not that of a CMake target. It must name a file.
1741 if(cmSystemTools::FileIsFullPath(inName))
1743 // This is a full path. Return it as given.
1744 return inName;
1746 // Treat the name as relative to the source directory in which it
1747 // was given.
1748 name = this->Makefile->GetCurrentDirectory();
1749 name += "/";
1750 name += inName;
1751 return name;
1754 //----------------------------------------------------------------------------
1755 std::string cmLocalGenerator::GetRealLocation(const char* inName,
1756 const char* config)
1758 std::string outName=inName;
1759 // Look for a CMake target with the given name, which is an executable
1760 // and which can be run
1761 cmTarget* target = this->Makefile->FindTargetToUse(inName);
1762 if ((target != 0)
1763 && (target->GetType() == cmTarget::EXECUTABLE)
1764 && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
1765 || (target->IsImported() == true)))
1767 outName = target->GetLocation( config );
1769 return outName;
1772 //----------------------------------------------------------------------------
1773 void cmLocalGenerator::AddSharedFlags(std::string& flags,
1774 const char* lang,
1775 bool shared)
1777 std::string flagsVar;
1779 // Add flags for dealing with shared libraries for this language.
1780 if(shared)
1782 flagsVar = "CMAKE_SHARED_LIBRARY_";
1783 flagsVar += lang;
1784 flagsVar += "_FLAGS";
1785 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1788 // Add flags specific to shared builds.
1789 if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1791 flagsVar = "CMAKE_SHARED_BUILD_";
1792 flagsVar += lang;
1793 flagsVar += "_FLAGS";
1794 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1798 //----------------------------------------------------------------------------
1799 void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
1800 const char* var,
1801 const char* config)
1803 // Add the flags from the variable itself.
1804 std::string flagsVar = var;
1805 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1806 // Add the flags from the build-type specific variable.
1807 if(config && *config)
1809 flagsVar += "_";
1810 flagsVar += cmSystemTools::UpperCase(config);
1811 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1815 //----------------------------------------------------------------------------
1816 void cmLocalGenerator::AppendFlags(std::string& flags,
1817 const char* newFlags)
1819 if(newFlags && *newFlags)
1821 std::string newf = newFlags;
1822 if(flags.size())
1824 flags += " ";
1826 flags += newFlags;
1830 //----------------------------------------------------------------------------
1831 void cmLocalGenerator::AppendDefines(std::string& defines,
1832 const char* defines_list,
1833 const char* lang)
1835 // Short-circuit if there are no definitions.
1836 if(!defines_list)
1838 return;
1841 // Expand the list of definitions.
1842 std::vector<std::string> defines_vec;
1843 cmSystemTools::ExpandListArgument(defines_list, defines_vec);
1845 // Short-circuit if there are no definitions.
1846 if(defines_vec.empty())
1848 return;
1851 // Lookup the define flag for the current language.
1852 std::string dflag = "-D";
1853 if(lang)
1855 std::string defineFlagVar = "CMAKE_";
1856 defineFlagVar += lang;
1857 defineFlagVar += "_DEFINE_FLAG";
1858 const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
1859 if(df && *df)
1861 dflag = df;
1865 // Add each definition to the command line with appropriate escapes.
1866 const char* dsep = defines.empty()? "" : " ";
1867 for(std::vector<std::string>::const_iterator di = defines_vec.begin();
1868 di != defines_vec.end(); ++di)
1870 // Skip unsupported definitions.
1871 if(!this->CheckDefinition(*di))
1873 continue;
1876 // Separate from previous definitions.
1877 defines += dsep;
1878 dsep = " ";
1880 // Append the definition with proper escaping.
1881 defines += dflag;
1882 if(this->WatcomWMake)
1884 // The Watcom compiler does its own command line parsing instead
1885 // of using the windows shell rules. Definitions are one of
1886 // -DNAME
1887 // -DNAME=<cpp-token>
1888 // -DNAME="c-string with spaces and other characters(?@#$)"
1890 // Watcom will properly parse each of these cases from the
1891 // command line without any escapes. However we still have to
1892 // get the '$' and '#' characters through WMake as '$$' and
1893 // '$#'.
1894 for(const char* c = di->c_str(); *c; ++c)
1896 if(*c == '$' || *c == '#')
1898 defines += '$';
1900 defines += *c;
1903 else
1905 // Make the definition appear properly on the command line.
1906 defines += this->EscapeForShell(di->c_str(), true);
1911 //----------------------------------------------------------------------------
1912 std::string
1913 cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
1914 const char* default_comment)
1916 // Check for a comment provided with the command.
1917 if(cc.GetComment())
1919 return cc.GetComment();
1922 // Construct a reasonable default comment if possible.
1923 if(!cc.GetOutputs().empty())
1925 std::string comment;
1926 comment = "Generating ";
1927 const char* sep = "";
1928 for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
1929 o != cc.GetOutputs().end(); ++o)
1931 comment += sep;
1932 comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
1933 sep = ", ";
1935 return comment;
1938 // Otherwise use the provided default.
1939 return default_comment;
1942 //----------------------------------------------------------------------------
1943 std::string
1944 cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
1946 return this->Convert(remote, START_OUTPUT, SHELL, true);
1949 //----------------------------------------------------------------------------
1950 std::string cmLocalGenerator::Convert(const char* source,
1951 RelativeRoot relative,
1952 OutputFormat output,
1953 bool optional)
1955 // Make sure the relative path conversion components are set.
1956 if(!this->PathConversionsSetup)
1958 this->SetupPathConversions();
1959 this->PathConversionsSetup = true;
1962 // Convert the path to a relative path.
1963 std::string result = source;
1965 if (!optional || this->UseRelativePaths)
1967 switch (relative)
1969 case HOME:
1970 //result = cmSystemTools::CollapseFullPath(result.c_str());
1971 result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
1972 result.c_str());
1973 break;
1974 case START:
1975 //result = cmSystemTools::CollapseFullPath(result.c_str());
1976 result = this->ConvertToRelativePath(this->StartDirectoryComponents,
1977 result.c_str());
1978 break;
1979 case HOME_OUTPUT:
1980 //result = cmSystemTools::CollapseFullPath(result.c_str());
1981 result =
1982 this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
1983 result.c_str());
1984 break;
1985 case START_OUTPUT:
1986 //result = cmSystemTools::CollapseFullPath(result.c_str());
1987 result =
1988 this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
1989 result.c_str());
1990 break;
1991 case FULL:
1992 result = cmSystemTools::CollapseFullPath(result.c_str());
1993 break;
1994 case NONE:
1995 break;
1998 // Now convert it to an output path.
1999 if (output == MAKEFILE)
2001 result = cmSystemTools::ConvertToOutputPath(result.c_str());
2003 else if( output == SHELL)
2005 // For the MSYS shell convert drive letters to posix paths, so
2006 // that c:/some/path becomes /c/some/path. This is needed to
2007 // avoid problems with the shell path translation.
2008 if(this->MSYSShell && !this->LinkScriptShell)
2010 if(result.size() > 2 && result[1] == ':')
2012 result[1] = result[0];
2013 result[0] = '/';
2016 if(this->WindowsShell)
2018 std::string::size_type pos = 0;
2019 while((pos = result.find('/', pos)) != std::string::npos)
2021 result[pos] = '\\';
2022 pos++;
2025 result = this->EscapeForShell(result.c_str(), true, false);
2027 return result;
2030 //----------------------------------------------------------------------------
2031 std::string cmLocalGenerator::FindRelativePathTopSource()
2033 // Relative path conversion within a single tree managed by CMake is
2034 // safe. We can use our parent relative path top if and only if
2035 // this is a subdirectory of that top.
2036 if(cmLocalGenerator* parent = this->GetParent())
2038 std::string parentTop = parent->FindRelativePathTopSource();
2039 if(cmSystemTools::IsSubDirectory(
2040 this->Makefile->GetStartDirectory(), parentTop.c_str()))
2042 return parentTop;
2046 // Otherwise this directory itself is the new top.
2047 return this->Makefile->GetStartDirectory();
2050 //----------------------------------------------------------------------------
2051 std::string cmLocalGenerator::FindRelativePathTopBinary()
2053 // Relative path conversion within a single tree managed by CMake is
2054 // safe. We can use our parent relative path top if and only if
2055 // this is a subdirectory of that top.
2056 if(cmLocalGenerator* parent = this->GetParent())
2058 std::string parentTop = parent->FindRelativePathTopBinary();
2059 if(cmSystemTools::IsSubDirectory(
2060 this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
2062 return parentTop;
2066 // Otherwise this directory itself is the new top.
2067 return this->Makefile->GetStartOutputDirectory();
2070 //----------------------------------------------------------------------------
2071 void cmLocalGenerator::ConfigureRelativePaths()
2073 // Relative path conversion inside the source tree is not used to
2074 // construct relative paths passed to build tools so it is safe to
2075 // even when the source is a network path.
2076 std::string source = this->FindRelativePathTopSource();
2077 this->RelativePathTopSource = source;
2079 // The current working directory on Windows cannot be a network
2080 // path. Therefore relative paths cannot work when the binary tree
2081 // is a network path.
2082 std::string binary = this->FindRelativePathTopBinary();
2083 if(binary.size() < 2 || binary.substr(0, 2) != "//")
2085 this->RelativePathTopBinary = binary;
2087 else
2089 this->RelativePathTopBinary = "";
2093 //----------------------------------------------------------------------------
2094 static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
2096 return (cmSystemTools::ComparePath(a, b) ||
2097 cmSystemTools::IsSubDirectory(a, b));
2100 //----------------------------------------------------------------------------
2101 std::string
2102 cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
2103 const char* in_remote)
2105 // The path should never be quoted.
2106 assert(in_remote[0] != '\"');
2108 // The local path should never have a trailing slash.
2109 assert(local.size() > 0 && !(local[local.size()-1] == ""));
2111 // If the path is already relative then just return the path.
2112 if(!cmSystemTools::FileIsFullPath(in_remote))
2114 return in_remote;
2117 // Make sure relative path conversion is configured.
2118 if(!this->RelativePathsConfigured)
2120 this->ConfigureRelativePaths();
2121 this->RelativePathsConfigured = true;
2124 // Skip conversion if the path and local are not both in the source
2125 // or both in the binary tree.
2126 std::string local_path = cmSystemTools::JoinPath(local);
2127 if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
2128 this->RelativePathTopBinary.c_str()) &&
2129 cmLocalGeneratorNotAbove(in_remote,
2130 this->RelativePathTopBinary.c_str())) ||
2131 (cmLocalGeneratorNotAbove(local_path.c_str(),
2132 this->RelativePathTopSource.c_str()) &&
2133 cmLocalGeneratorNotAbove(in_remote,
2134 this->RelativePathTopSource.c_str()))))
2136 return in_remote;
2139 // Identify the longest shared path component between the remote
2140 // path and the local path.
2141 std::vector<std::string> remote;
2142 cmSystemTools::SplitPath(in_remote, remote);
2143 unsigned int common=0;
2144 while(common < remote.size() &&
2145 common < local.size() &&
2146 cmSystemTools::ComparePath(remote[common].c_str(),
2147 local[common].c_str()))
2149 ++common;
2152 // If no part of the path is in common then return the full path.
2153 if(common == 0)
2155 return in_remote;
2158 // If the entire path is in common then just return a ".".
2159 if(common == remote.size() &&
2160 common == local.size())
2162 return ".";
2165 // If the entire path is in common except for a trailing slash then
2166 // just return a "./".
2167 if(common+1 == remote.size() &&
2168 remote[common].size() == 0 &&
2169 common == local.size())
2171 return "./";
2174 // Construct the relative path.
2175 std::string relative;
2177 // First add enough ../ to get up to the level of the shared portion
2178 // of the path. Leave off the trailing slash. Note that the last
2179 // component of local will never be empty because local should never
2180 // have a trailing slash.
2181 for(unsigned int i=common; i < local.size(); ++i)
2183 relative += "..";
2184 if(i < local.size()-1)
2186 relative += "/";
2190 // Now add the portion of the destination path that is not included
2191 // in the shared portion of the path. Add a slash the first time
2192 // only if there was already something in the path. If there was a
2193 // trailing slash in the input then the last iteration of the loop
2194 // will add a slash followed by an empty string which will preserve
2195 // the trailing slash in the output.
2196 for(unsigned int i=common; i < remote.size(); ++i)
2198 if(relative.size() > 0)
2200 relative += "/";
2202 relative += remote[i];
2205 // Finally return the path.
2206 return relative;
2209 //----------------------------------------------------------------------------
2210 void
2211 cmLocalGenerator
2212 ::GenerateTargetInstallRules(
2213 std::ostream& os, const char* config,
2214 std::vector<std::string> const& configurationTypes)
2216 // Convert the old-style install specification from each target to
2217 // an install generator and run it.
2218 cmTargets& tgts = this->Makefile->GetTargets();
2219 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
2221 // Include the user-specified pre-install script for this target.
2222 if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
2224 cmInstallScriptGenerator g(preinstall, false, 0);
2225 g.Generate(os, config, configurationTypes);
2228 // Install this target if a destination is given.
2229 if(l->second.GetInstallPath() != "")
2231 // Compute the full install destination. Note that converting
2232 // to unix slashes also removes any trailing slash.
2233 // We also skip over the leading slash given by the user.
2234 std::string destination = l->second.GetInstallPath().substr(1);
2235 cmSystemTools::ConvertToUnixSlashes(destination);
2237 // Generate the proper install generator for this target type.
2238 switch(l->second.GetType())
2240 case cmTarget::EXECUTABLE:
2241 case cmTarget::STATIC_LIBRARY:
2242 case cmTarget::MODULE_LIBRARY:
2244 // Use a target install generator.
2245 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2246 g.Generate(os, config, configurationTypes);
2248 break;
2249 case cmTarget::SHARED_LIBRARY:
2251 #if defined(_WIN32) || defined(__CYGWIN__)
2252 // Special code to handle DLL. Install the import library
2253 // to the normal destination and the DLL to the runtime
2254 // destination.
2255 cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
2256 g1.Generate(os, config, configurationTypes);
2257 // We also skip over the leading slash given by the user.
2258 destination = l->second.GetRuntimeInstallPath().substr(1);
2259 cmSystemTools::ConvertToUnixSlashes(destination);
2260 cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
2261 g2.Generate(os, config, configurationTypes);
2262 #else
2263 // Use a target install generator.
2264 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2265 g.Generate(os, config, configurationTypes);
2266 #endif
2268 break;
2269 default:
2270 break;
2274 // Include the user-specified post-install script for this target.
2275 if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
2277 cmInstallScriptGenerator g(postinstall, false, 0);
2278 g.Generate(os, config, configurationTypes);
2283 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2284 static std::string cmLocalGeneratorMD5(const char* input)
2286 char md5out[32];
2287 cmsysMD5* md5 = cmsysMD5_New();
2288 cmsysMD5_Initialize(md5);
2289 cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
2290 cmsysMD5_FinalizeHex(md5, md5out);
2291 cmsysMD5_Delete(md5);
2292 return std::string(md5out, 32);
2295 static bool
2296 cmLocalGeneratorShortenObjectName(std::string& objName,
2297 std::string::size_type max_len)
2299 // Replace the beginning of the path portion of the object name with
2300 // its own md5 sum.
2301 std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
2302 if(pos != objName.npos)
2304 std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
2305 md5name += objName.substr(pos);
2306 objName = md5name;
2308 // The object name is now short enough.
2309 return true;
2311 else
2313 // The object name could not be shortened enough.
2314 return false;
2318 static bool cmLocalGeneratorCheckObjectName(std::string& objName,
2319 std::string::size_type dir_len)
2321 // Choose a maximum file name length.
2322 #if defined(_WIN32) || defined(__CYGWIN__)
2323 std::string::size_type const max_total_len = 250;
2324 #else
2325 std::string::size_type const max_total_len = 1000;
2326 #endif
2328 // Enforce the maximum file name length if possible.
2329 std::string::size_type max_obj_len = max_total_len;
2330 if(dir_len < max_total_len)
2332 max_obj_len = max_total_len - dir_len;
2333 if(objName.size() > max_obj_len)
2335 // The current object file name is too long. Try to shorten it.
2336 return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
2338 else
2340 // The object file name is short enough.
2341 return true;
2344 else
2346 // The build directory in which the object will be stored is
2347 // already too deep.
2348 return false;
2351 #endif
2353 //----------------------------------------------------------------------------
2354 std::string&
2355 cmLocalGenerator
2356 ::CreateSafeUniqueObjectFileName(const char* sin,
2357 std::string::size_type dir_len)
2359 // Look for an existing mapped name for this object file.
2360 std::map<cmStdString,cmStdString>::iterator it =
2361 this->UniqueObjectNamesMap.find(sin);
2363 // If no entry exists create one.
2364 if(it == this->UniqueObjectNamesMap.end())
2366 // Start with the original name.
2367 std::string ssin = sin;
2369 // Avoid full paths by removing leading slashes.
2370 std::string::size_type pos = 0;
2371 for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
2372 ssin = ssin.substr(pos);
2374 // Avoid full paths by removing colons.
2375 cmSystemTools::ReplaceString(ssin, ":", "_");
2377 // Avoid relative paths that go up the tree.
2378 cmSystemTools::ReplaceString(ssin, "../", "__/");
2380 // Avoid spaces.
2381 cmSystemTools::ReplaceString(ssin, " ", "_");
2383 // Mangle the name if necessary.
2384 if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
2386 bool done;
2387 int cc = 0;
2388 char rpstr[100];
2389 sprintf(rpstr, "_p_");
2390 cmSystemTools::ReplaceString(ssin, "+", rpstr);
2391 std::string sssin = sin;
2394 done = true;
2395 for ( it = this->UniqueObjectNamesMap.begin();
2396 it != this->UniqueObjectNamesMap.end();
2397 ++ it )
2399 if ( it->second == ssin )
2401 done = false;
2404 if ( done )
2406 break;
2408 sssin = ssin;
2409 cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
2410 sprintf(rpstr, "_p%d_", cc++);
2412 while ( !done );
2415 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2416 cmLocalGeneratorCheckObjectName(ssin, dir_len);
2417 #else
2418 (void)dir_len;
2419 #endif
2421 // Insert the newly mapped object file name.
2422 std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
2423 it = this->UniqueObjectNamesMap.insert(e).first;
2426 // Return the map entry.
2427 return it->second;
2430 //----------------------------------------------------------------------------
2431 std::string
2432 cmLocalGenerator
2433 ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
2434 std::string::size_type dir_len,
2435 bool* hasSourceExtension)
2437 // Construct the object file name using the full path to the source
2438 // file which is its only unique identification.
2439 const char* fullPath = source.GetFullPath().c_str();
2441 // Try referencing the source relative to the source tree.
2442 std::string relFromSource = this->Convert(fullPath, START);
2443 assert(!relFromSource.empty());
2444 bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
2445 bool subSource = relSource && relFromSource[0] != '.';
2447 // Try referencing the source relative to the binary tree.
2448 std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
2449 assert(!relFromBinary.empty());
2450 bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
2451 bool subBinary = relBinary && relFromBinary[0] != '.';
2453 // Select a nice-looking reference to the source file to construct
2454 // the object file name.
2455 std::string objectName;
2456 if((relSource && !relBinary) || (subSource && !subBinary))
2458 objectName = relFromSource;
2460 else if((relBinary && !relSource) || (subBinary && !subSource))
2462 objectName = relFromBinary;
2464 else if(relFromBinary.length() < relFromSource.length())
2466 objectName = relFromBinary;
2468 else
2470 objectName = relFromSource;
2473 // if it is still a full path check for the try compile case
2474 // try compile never have in source sources, and should not
2475 // have conflicting source file names in the same target
2476 if(cmSystemTools::FileIsFullPath(objectName.c_str()))
2478 if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
2480 objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
2484 // Replace the original source file extension with the object file
2485 // extension.
2486 bool keptSourceExtension = true;
2487 if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
2489 // Decide whether this language wants to replace the source
2490 // extension with the object extension. For CMake 2.4
2491 // compatibility do this by default.
2492 bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
2493 if(!replaceExt)
2495 std::string repVar = "CMAKE_";
2496 repVar += source.GetLanguage();
2497 repVar += "_OUTPUT_EXTENSION_REPLACE";
2498 replaceExt = this->Makefile->IsOn(repVar.c_str());
2501 // Remove the source extension if it is to be replaced.
2502 if(replaceExt)
2504 keptSourceExtension = false;
2505 std::string::size_type dot_pos = objectName.rfind(".");
2506 if(dot_pos != std::string::npos)
2508 objectName = objectName.substr(0, dot_pos);
2512 // Store the new extension.
2513 objectName +=
2514 this->GlobalGenerator->GetLanguageOutputExtension(source);
2516 if(hasSourceExtension)
2518 *hasSourceExtension = keptSourceExtension;
2521 // Convert to a safe name.
2522 return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
2525 //----------------------------------------------------------------------------
2526 const char*
2527 cmLocalGenerator
2528 ::GetSourceFileLanguage(const cmSourceFile& source)
2530 return source.GetLanguage();
2533 //----------------------------------------------------------------------------
2534 std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
2536 std::string result;
2537 bool forceOn = cmSystemTools::GetForceUnixPaths();
2538 if(forceOn && this->WindowsShell)
2540 cmSystemTools::SetForceUnixPaths(false);
2542 result = cmSystemTools::EscapeSpaces(str);
2543 if(forceOn && this->WindowsShell)
2545 cmSystemTools::SetForceUnixPaths(true);
2547 return result;
2550 //----------------------------------------------------------------------------
2551 std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
2552 bool forEcho)
2554 // Compute the flags for the target shell environment.
2555 int flags = 0;
2556 if(this->WindowsVSIDE)
2558 flags |= cmsysSystem_Shell_Flag_VSIDE;
2560 else
2562 flags |= cmsysSystem_Shell_Flag_Make;
2564 if(makeVars)
2566 flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
2568 if(forEcho)
2570 flags |= cmsysSystem_Shell_Flag_EchoWindows;
2572 if(this->WatcomWMake)
2574 flags |= cmsysSystem_Shell_Flag_WatcomWMake;
2576 if(this->MinGWMake)
2578 flags |= cmsysSystem_Shell_Flag_MinGWMake;
2580 if(this->NMake)
2582 flags |= cmsysSystem_Shell_Flag_NMake;
2585 // Compute the buffer size needed.
2586 int size = (this->WindowsShell ?
2587 cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
2588 cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
2590 // Compute the shell argument itself.
2591 std::vector<char> arg(size);
2592 if(this->WindowsShell)
2594 cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
2596 else
2598 cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
2600 return std::string(&arg[0]);
2603 //----------------------------------------------------------------------------
2604 std::string cmLocalGenerator::EscapeForCMake(const char* str)
2606 // Always double-quote the argument to take care of most escapes.
2607 std::string result = "\"";
2608 for(const char* c = str; *c; ++c)
2610 if(*c == '"')
2612 // Escape the double quote to avoid ending the argument.
2613 result += "\\\"";
2615 else if(*c == '$')
2617 // Escape the dollar to avoid expanding variables.
2618 result += "\\$";
2620 else if(*c == '\\')
2622 // Escape the backslash to avoid other escapes.
2623 result += "\\\\";
2625 else
2627 // Other characters will be parsed correctly.
2628 result += *c;
2631 result += "\"";
2632 return result;
2635 //----------------------------------------------------------------------------
2636 std::string
2637 cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
2639 cmSystemTools::Error("GetTargetDirectory"
2640 " called on cmLocalGenerator");
2641 return "";
2645 //----------------------------------------------------------------------------
2646 void
2647 cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
2648 std::vector<std::string>&
2651 cmSystemTools::Error("GetTargetObjectFileDirectories"
2652 " called on cmLocalGenerator");
2655 //----------------------------------------------------------------------------
2656 unsigned int cmLocalGenerator::GetBackwardsCompatibility()
2658 // The computed version may change until the project is fully
2659 // configured.
2660 if(!this->BackwardsCompatibilityFinal)
2662 unsigned int major = 0;
2663 unsigned int minor = 0;
2664 unsigned int patch = 0;
2665 if(const char* value
2666 = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
2668 switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
2670 case 2: patch = 0; break;
2671 case 1: minor = 0; patch = 0; break;
2672 default: break;
2675 this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
2676 this->BackwardsCompatibilityFinal = this->Configured;
2679 return this->BackwardsCompatibility;
2682 //----------------------------------------------------------------------------
2683 bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
2684 unsigned int minor,
2685 unsigned int patch)
2687 // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
2688 // equal to or lower than the given version.
2689 unsigned int actual_compat = this->GetBackwardsCompatibility();
2690 return (actual_compat &&
2691 actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
2694 //----------------------------------------------------------------------------
2695 bool cmLocalGenerator::CheckDefinition(std::string const& define) const
2697 // Many compilers do not support -DNAME(arg)=sdf so we disable it.
2698 bool function_style = false;
2699 for(const char* c = define.c_str(); *c && *c != '='; ++c)
2701 if(*c == '(')
2703 function_style = true;
2704 break;
2707 if(function_style)
2709 cmOStringStream e;
2710 e << "WARNING: Function-style preprocessor definitions may not be "
2711 << "passed on the compiler command line because many compilers "
2712 << "do not support it.\n"
2713 << "CMake is dropping a preprocessor definition: " << define << "\n"
2714 << "Consider defining the macro in a (configured) header file.\n";
2715 cmSystemTools::Message(e.str().c_str());
2716 return false;
2719 // Many compilers do not support # in the value so we disable it.
2720 if(define.find_first_of("#") != define.npos)
2722 cmOStringStream e;
2723 e << "WARNING: Peprocessor definitions containing '#' may not be "
2724 << "passed on the compiler command line because many compilers "
2725 << "do not support it.\n"
2726 << "CMake is dropping a preprocessor definition: " << define << "\n"
2727 << "Consider defining the macro in a (configured) header file.\n";
2728 cmSystemTools::Message(e.str().c_str());
2729 return false;
2732 // Assume it is supported.
2733 return true;