Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmLocalGenerator.cxx
blob7183fba929645b998e81f5c79d7c37c1b824528a
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmLocalGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-28 19:54:20 $
7 Version: $Revision: 1.270 $
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 if(variable == "CMAKE_COMMAND")
943 const char* cmcommand =
944 this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
945 return this->Convert(cmcommand, FULL, SHELL);
947 std::vector<std::string> enabledLanguages;
948 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
949 // loop over language specific replace variables
950 int pos = 0;
951 while(ruleReplaceVars[pos])
953 for(std::vector<std::string>::iterator i = enabledLanguages.begin();
954 i != enabledLanguages.end(); ++i)
956 const char* lang = i->c_str();
957 std::string actualReplace = ruleReplaceVars[pos];
958 // If this is the compiler then look for the extra variable
959 // _COMPILER_ARG1 which must be the first argument to the compiler
960 const char* compilerArg1 = 0;
961 if(actualReplace == "CMAKE_${LANG}_COMPILER")
963 std::string arg1 = actualReplace + "_ARG1";
964 cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
965 compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
967 if(actualReplace.find("${LANG}") != actualReplace.npos)
969 cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
971 if(actualReplace == variable)
973 std::string replace =
974 this->Makefile->GetSafeDefinition(variable.c_str());
975 // if the variable is not a FLAG then treat it like a path
976 if(variable.find("_FLAG") == variable.npos)
978 std::string ret = this->ConvertToOutputForExisting(replace.c_str());
979 // if there is a required first argument to the compiler add it
980 // to the compiler string
981 if(compilerArg1)
983 ret += " ";
984 ret += compilerArg1;
986 return ret;
988 return replace;
991 pos++;
993 return variable;
997 void
998 cmLocalGenerator::ExpandRuleVariables(std::string& s,
999 const RuleVariables& replaceValues)
1001 std::vector<std::string> enabledLanguages;
1002 this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
1003 std::string::size_type start = s.find('<');
1004 // no variables to expand
1005 if(start == s.npos)
1007 return;
1009 std::string::size_type pos = 0;
1010 std::string expandedInput;
1011 while(start != s.npos && start < s.size()-2)
1013 std::string::size_type end = s.find('>', start);
1014 // if we find a < with no > we are done
1015 if(end == s.npos)
1017 return;
1019 char c = s[start+1];
1020 // if the next char after the < is not A-Za-z then
1021 // skip it and try to find the next < in the string
1022 if(!isalpha(c))
1024 start = s.find('<', start+1);
1026 else
1028 // extract the var
1029 std::string var = s.substr(start+1, end - start-1);
1030 std::string replace = this->ExpandRuleVariable(var,
1031 replaceValues);
1032 expandedInput += s.substr(pos, start-pos);
1033 expandedInput += replace;
1034 // move to next one
1035 start = s.find('<', start+var.size()+2);
1036 pos = end+1;
1039 // add the rest of the input
1040 expandedInput += s.substr(pos, s.size()-pos);
1041 s = expandedInput;
1045 std::string
1046 cmLocalGenerator::ConvertToOutputForExisting(const char* p)
1048 std::string ret = p;
1049 if(this->WindowsShell && ret.find(' ') != ret.npos
1050 && cmSystemTools::FileExists(p))
1052 if(cmSystemTools::GetShortPath(p, ret))
1054 return this->Convert(ret.c_str(), NONE, SHELL, true);
1057 return this->Convert(p, START_OUTPUT, SHELL, true);
1060 const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
1062 if(!lang)
1064 return "";
1066 if(this->LanguageToIncludeFlags.count(lang))
1068 return this->LanguageToIncludeFlags[lang].c_str();
1070 cmOStringStream includeFlags;
1071 std::vector<std::string> includes;
1072 this->GetIncludeDirectories(includes);
1073 std::vector<std::string>::iterator i;
1075 std::string flagVar = "CMAKE_INCLUDE_FLAG_";
1076 flagVar += lang;
1077 const char* includeFlag =
1078 this->Makefile->GetSafeDefinition(flagVar.c_str());
1079 flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
1080 flagVar += lang;
1081 const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
1082 bool quotePaths = false;
1083 if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
1085 quotePaths = true;
1087 bool repeatFlag = true;
1088 // should the include flag be repeated like ie. -IA -IB
1089 if(!sep)
1091 sep = " ";
1093 else
1095 // if there is a separator then the flag is not repeated but is only
1096 // given once i.e. -classpath a:b:c
1097 repeatFlag = false;
1100 // Support special system include flag if it is available and the
1101 // normal flag is repeated for each directory.
1102 std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
1103 sysFlagVar += lang;
1104 const char* sysIncludeFlag = 0;
1105 if(repeatFlag)
1107 sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
1110 bool flagUsed = false;
1111 std::set<cmStdString> emitted;
1112 #ifdef __APPLE__
1113 emitted.insert("/System/Library/Frameworks");
1114 #endif
1115 for(i = includes.begin(); i != includes.end(); ++i)
1117 #ifdef __APPLE__
1118 if(cmSystemTools::IsPathToFramework(i->c_str()))
1120 std::string frameworkDir = *i;
1121 frameworkDir += "/../";
1122 frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
1123 if(emitted.insert(frameworkDir).second)
1125 includeFlags
1126 << "-F"
1127 << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
1129 continue;
1131 #endif
1132 std::string include = *i;
1133 if(!flagUsed || repeatFlag)
1135 if(sysIncludeFlag &&
1136 this->Makefile->IsSystemIncludeDirectory(i->c_str()))
1138 includeFlags << sysIncludeFlag;
1140 else
1142 includeFlags << includeFlag;
1144 flagUsed = true;
1146 std::string includePath = this->ConvertToOutputForExisting(i->c_str());
1147 if(quotePaths && includePath.size() && includePath[0] != '\"')
1149 includeFlags << "\"";
1151 includeFlags << includePath;
1152 if(quotePaths && includePath.size() && includePath[0] != '\"')
1154 includeFlags << "\"";
1156 includeFlags << sep;
1158 std::string flags = includeFlags.str();
1159 // remove trailing separators
1160 if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
1162 flags[flags.size()-1] = ' ';
1164 std::string defineFlags = this->Makefile->GetDefineFlags();
1165 flags += defineFlags;
1166 this->LanguageToIncludeFlags[lang] = flags;
1168 // Use this temorary variable for the return value to work-around a
1169 // bogus GCC 2.95 warning.
1170 const char* ret = this->LanguageToIncludeFlags[lang].c_str();
1171 return ret;
1174 //----------------------------------------------------------------------------
1175 void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
1176 bool filter_system_dirs)
1178 // Need to decide whether to automatically include the source and
1179 // binary directories at the beginning of the include path.
1180 bool includeSourceDir = false;
1181 bool includeBinaryDir = false;
1183 // When automatic include directories are requested for a build then
1184 // include the source and binary directories at the beginning of the
1185 // include path to approximate include file behavior for an
1186 // in-source build. This does not account for the case of a source
1187 // file in a subdirectory of the current source directory but we
1188 // cannot fix this because not all native build tools support
1189 // per-source-file include paths.
1190 if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
1192 includeSourceDir = true;
1193 includeBinaryDir = true;
1196 // CMake versions below 2.0 would add the source tree to the -I path
1197 // automatically. Preserve compatibility.
1198 if(this->NeedBackwardsCompatibility(1,9))
1200 includeSourceDir = true;
1203 // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
1204 // not set the backwards compatibility level automatically.
1205 const char* vtkSourceDir =
1206 this->Makefile->GetDefinition("VTK_SOURCE_DIR");
1207 if(vtkSourceDir)
1209 const char* vtk_major =
1210 this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
1211 const char* vtk_minor =
1212 this->Makefile->GetDefinition("VTK_MINOR_VERSION");
1213 vtk_major = vtk_major? vtk_major : "4";
1214 vtk_minor = vtk_minor? vtk_minor : "4";
1215 int vmajor = 0;
1216 int vminor = 0;
1217 if(sscanf(vtk_major, "%d", &vmajor) &&
1218 sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
1220 includeSourceDir = true;
1224 // Do not repeat an include path.
1225 std::set<cmStdString> emitted;
1227 // Store the automatic include paths.
1228 if(includeBinaryDir)
1230 dirs.push_back(this->Makefile->GetStartOutputDirectory());
1231 emitted.insert(this->Makefile->GetStartOutputDirectory());
1233 if(includeSourceDir)
1235 if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
1237 dirs.push_back(this->Makefile->GetStartDirectory());
1238 emitted.insert(this->Makefile->GetStartDirectory());
1242 if(filter_system_dirs)
1244 // Do not explicitly add the standard include path "/usr/include".
1245 // This can cause problems with certain standard library
1246 // implementations because the wrong headers may be found first.
1247 emitted.insert("/usr/include");
1248 if(const char* implicitIncludes = this->Makefile->GetDefinition
1249 ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
1251 std::vector<std::string> implicitIncludeVec;
1252 cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
1253 for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
1255 emitted.insert(implicitIncludeVec[k]);
1260 // Get the project-specified include directories.
1261 std::vector<std::string>& includes =
1262 this->Makefile->GetIncludeDirectories();
1264 // Support putting all the in-project include directories first if
1265 // it is requested by the project.
1266 if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
1268 const char* topSourceDir = this->Makefile->GetHomeDirectory();
1269 const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
1270 for(std::vector<std::string>::iterator i = includes.begin();
1271 i != includes.end(); ++i)
1273 // Emit this directory only if it is a subdirectory of the
1274 // top-level source or binary tree.
1275 if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
1276 cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
1277 cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
1278 cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
1280 if(emitted.insert(*i).second)
1282 dirs.push_back(*i);
1288 // Construct the final ordered include directory list.
1289 for(std::vector<std::string>::iterator i = includes.begin();
1290 i != includes.end(); ++i)
1292 if(emitted.insert(*i).second)
1294 dirs.push_back(*i);
1299 void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
1300 std::string& flags,
1301 std::string& linkFlags,
1302 cmTarget& target)
1304 std::string buildType =
1305 this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
1306 buildType = cmSystemTools::UpperCase(buildType);
1307 const char* libraryLinkVariable =
1308 "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
1310 switch(target.GetType())
1312 case cmTarget::STATIC_LIBRARY:
1314 const char* targetLinkFlags =
1315 target.GetProperty("STATIC_LIBRARY_FLAGS");
1316 if(targetLinkFlags)
1318 linkFlags += targetLinkFlags;
1319 linkFlags += " ";
1322 break;
1323 case cmTarget::MODULE_LIBRARY:
1324 libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
1325 case cmTarget::SHARED_LIBRARY:
1327 linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
1328 linkFlags += " ";
1329 if(buildType.size())
1331 std::string build = libraryLinkVariable;
1332 build += "_";
1333 build += buildType;
1334 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1335 linkFlags += " ";
1337 if(this->Makefile->IsOn("WIN32") &&
1338 !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
1340 const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
1341 for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
1342 i != sources.end(); ++i)
1344 cmSourceFile* sf = *i;
1345 if(sf->GetExtension() == "def")
1347 linkFlags +=
1348 this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
1349 linkFlags += this->Convert(sf->GetFullPath().c_str(),
1350 START_OUTPUT, SHELL);
1351 linkFlags += " ";
1355 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1356 if(targetLinkFlags)
1358 linkFlags += targetLinkFlags;
1359 linkFlags += " ";
1360 std::string configLinkFlags = targetLinkFlags;
1361 configLinkFlags += buildType;
1362 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1363 if(targetLinkFlags)
1365 linkFlags += targetLinkFlags;
1366 linkFlags += " ";
1369 cmOStringStream linklibsStr;
1370 this->OutputLinkLibraries(linklibsStr, target, false);
1371 linkLibs = linklibsStr.str();
1373 break;
1374 case cmTarget::EXECUTABLE:
1376 linkFlags +=
1377 this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
1378 linkFlags += " ";
1379 if(buildType.size())
1381 std::string build = "CMAKE_EXE_LINKER_FLAGS_";
1382 build += buildType;
1383 linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
1384 linkFlags += " ";
1386 const char* linkLanguage =
1387 target.GetLinkerLanguage(this->GetGlobalGenerator());
1388 if(!linkLanguage)
1390 cmSystemTools::Error
1391 ("CMake can not determine linker language for target:",
1392 target.GetName());
1393 return;
1395 std::string langVar = "CMAKE_";
1396 langVar += linkLanguage;
1397 std::string flagsVar = langVar + "_FLAGS";
1398 std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
1399 sharedFlagsVar += linkLanguage;
1400 sharedFlagsVar += "_FLAGS";
1401 flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
1402 flags += " ";
1403 flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
1404 flags += " ";
1405 cmOStringStream linklibs;
1406 this->OutputLinkLibraries(linklibs, target, false);
1407 linkLibs = linklibs.str();
1408 if(cmSystemTools::IsOn
1409 (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1411 std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
1412 + linkLanguage + std::string("_FLAGS");
1413 linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
1414 linkFlags += " ";
1416 if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
1418 linkFlags +=
1419 this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
1420 linkFlags += " ";
1422 else
1424 linkFlags +=
1425 this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
1426 linkFlags += " ";
1428 const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
1429 if(targetLinkFlags)
1431 linkFlags += targetLinkFlags;
1432 linkFlags += " ";
1433 std::string configLinkFlags = targetLinkFlags;
1434 configLinkFlags += buildType;
1435 targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
1436 if(targetLinkFlags)
1438 linkFlags += targetLinkFlags;
1439 linkFlags += " ";
1443 break;
1444 case cmTarget::UTILITY:
1445 case cmTarget::GLOBAL_TARGET:
1446 case cmTarget::INSTALL_FILES:
1447 case cmTarget::INSTALL_PROGRAMS:
1448 case cmTarget::INSTALL_DIRECTORY:
1449 break;
1453 std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib)
1455 #if defined(_WIN32) && !defined(__CYGWIN__)
1456 // Work-ardound command line parsing limitations in MSVC 6.0 and
1457 // Watcom.
1458 if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
1460 // Search for the last space.
1461 std::string::size_type pos = lib.rfind(' ');
1462 if(pos != lib.npos)
1464 // Find the slash after the last space, if any.
1465 pos = lib.find('/', pos);
1467 // Convert the portion of the path with a space to a short path.
1468 std::string sp;
1469 if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
1471 // Append the rest of the path with no space.
1472 sp += lib.substr(pos);
1474 // Convert to an output path.
1475 return this->Convert(sp.c_str(), NONE, SHELL);
1479 #endif
1481 // Normal behavior.
1482 return this->Convert(lib.c_str(), START_OUTPUT, SHELL);
1486 * Output the linking rules on a command line. For executables,
1487 * targetLibrary should be a NULL pointer. For libraries, it should point
1488 * to the name of the library. This will not link a library against itself.
1490 void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
1491 cmTarget& tgt,
1492 bool relink)
1494 const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
1495 cmComputeLinkInformation* pcli = tgt.GetLinkInformation(config);
1496 if(!pcli)
1498 return;
1500 cmComputeLinkInformation& cli = *pcli;
1502 // Collect library linking flags command line options.
1503 std::string linkLibs;
1505 const char* linkLanguage = cli.GetLinkLanguage();
1507 std::string libPathFlag =
1508 this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1509 std::string libPathTerminator =
1510 this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1512 // Flags to link an executable to shared libraries.
1513 std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
1514 linkFlagsVar += linkLanguage;
1515 linkFlagsVar += "_FLAGS";
1516 if( tgt.GetType() == cmTarget::EXECUTABLE )
1518 linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
1519 linkLibs += " ";
1522 // Append the framework search path flags.
1523 std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
1524 for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
1525 fdi != fwDirs.end(); ++fdi)
1527 linkLibs += "-F";
1528 linkLibs += this->Convert(fdi->c_str(), NONE, SHELL, false);
1529 linkLibs += " ";
1532 // Append the library search path flags.
1533 std::vector<std::string> const& libDirs = cli.GetDirectories();
1534 for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
1535 libDir != libDirs.end(); ++libDir)
1537 std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
1538 linkLibs += libPathFlag;
1539 linkLibs += libpath;
1540 linkLibs += libPathTerminator;
1541 linkLibs += " ";
1544 // Append the link items.
1545 typedef cmComputeLinkInformation::ItemVector ItemVector;
1546 ItemVector const& items = cli.GetItems();
1547 for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
1549 if(li->IsPath)
1551 linkLibs += this->ConvertToLinkReference(li->Value);
1553 else
1555 linkLibs += li->Value;
1557 linkLibs += " ";
1560 // Write the library flags to the build rule.
1561 fout << linkLibs;
1563 // Get the RPATH entries.
1564 std::vector<std::string> runtimeDirs;
1565 cli.GetRPath(runtimeDirs, relink);
1567 // Check what kind of rpath flags to use.
1568 if(cli.GetRuntimeSep().empty())
1570 // Each rpath entry gets its own option ("-R a -R b -R c")
1571 std::string rpath;
1572 for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
1573 ri != runtimeDirs.end(); ++ri)
1575 rpath += cli.GetRuntimeFlag();
1576 rpath += this->Convert(ri->c_str(), FULL, SHELL, false);
1577 rpath += " ";
1579 fout << rpath;
1581 else
1583 // All rpath entries are combined ("-Wl,-rpath,a:b:c").
1584 std::string rpath = cli.GetRPathString(relink);
1586 // Store the rpath option in the stream.
1587 if(!rpath.empty())
1589 fout << cli.GetRuntimeFlag();
1590 fout << this->EscapeForShell(rpath.c_str(), true);
1591 fout << " ";
1595 // Add the linker runtime search path if any.
1596 std::string rpath_link = cli.GetRPathLinkString();
1597 if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
1599 fout << cli.GetRPathLinkFlag();
1600 fout << this->EscapeForShell(rpath_link.c_str(), true);
1601 fout << " ";
1604 // Add standard libraries for this language.
1605 std::string standardLibsVar = "CMAKE_";
1606 standardLibsVar += cli.GetLinkLanguage();
1607 standardLibsVar += "_STANDARD_LIBRARIES";
1608 if(const char* stdLibs =
1609 this->Makefile->GetDefinition(standardLibsVar.c_str()))
1611 fout << stdLibs << " ";
1615 //----------------------------------------------------------------------------
1616 void cmLocalGenerator::AddLanguageFlags(std::string& flags,
1617 const char* lang,
1618 const char* config)
1620 // Add language-specific flags.
1621 std::string flagsVar = "CMAKE_";
1622 flagsVar += lang;
1623 flagsVar += "_FLAGS";
1624 if(this->EmitUniversalBinaryFlags)
1626 const char* osxArch =
1627 this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
1628 const char* sysroot =
1629 this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
1630 if(osxArch && sysroot && lang && lang[0] =='C')
1632 std::vector<std::string> archs;
1633 cmSystemTools::ExpandListArgument(std::string(osxArch),
1634 archs);
1635 bool addArchFlag = true;
1636 if(archs.size() == 1)
1638 const char* archOrig =
1639 this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
1640 if(archs[0] == archOrig)
1642 addArchFlag = false;
1645 // if there is more than one arch add the -arch and
1646 // -isysroot flags, or if there is one arch flag, but
1647 // it is not the default -arch flag for the system, then
1648 // add it. Otherwize do not add -arch and -isysroot
1649 if(addArchFlag)
1651 for( std::vector<std::string>::iterator i = archs.begin();
1652 i != archs.end(); ++i)
1654 flags += " -arch ";
1655 flags += *i;
1657 flags += " -isysroot ";
1658 flags += sysroot;
1662 this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
1665 //----------------------------------------------------------------------------
1666 std::string cmLocalGenerator::GetRealDependency(const char* inName,
1667 const char* config)
1669 // Older CMake code may specify the dependency using the target
1670 // output file rather than the target name. Such code would have
1671 // been written before there was support for target properties that
1672 // modify the name so stripping down to just the file name should
1673 // produce the target name in this case.
1674 std::string name = cmSystemTools::GetFilenameName(inName);
1675 if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
1677 name = cmSystemTools::GetFilenameWithoutLastExtension(name);
1680 // Look for a CMake target with the given name.
1681 if(cmTarget* target = this->Makefile->FindTargetToUse(name.c_str()))
1683 // make sure it is not just a coincidence that the target name
1684 // found is part of the inName
1685 if(cmSystemTools::FileIsFullPath(inName))
1687 std::string tLocation = target->GetLocation(config);
1688 tLocation = cmSystemTools::GetFilenamePath(tLocation);
1689 std::string depLocation = cmSystemTools::GetFilenamePath(
1690 std::string(inName));
1691 depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
1692 tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
1693 if(depLocation != tLocation)
1695 // it is a full path to a depend that has the same name
1696 // as a target but is in a different location so do not use
1697 // the target as the depend
1698 return inName;
1701 switch (target->GetType())
1703 case cmTarget::EXECUTABLE:
1704 case cmTarget::STATIC_LIBRARY:
1705 case cmTarget::SHARED_LIBRARY:
1706 case cmTarget::MODULE_LIBRARY:
1708 // Get the location of the target's output file and depend on it.
1709 if(const char* location = target->GetLocation(config))
1711 return location;
1714 break;
1715 case cmTarget::UTILITY:
1716 case cmTarget::GLOBAL_TARGET:
1717 // Depending on a utility target may not work but just trust
1718 // the user to have given a valid name.
1719 return inName;
1720 case cmTarget::INSTALL_FILES:
1721 case cmTarget::INSTALL_PROGRAMS:
1722 case cmTarget::INSTALL_DIRECTORY:
1723 break;
1727 // The name was not that of a CMake target. It must name a file.
1728 if(cmSystemTools::FileIsFullPath(inName))
1730 // This is a full path. Return it as given.
1731 return inName;
1733 // Treat the name as relative to the source directory in which it
1734 // was given.
1735 name = this->Makefile->GetCurrentDirectory();
1736 name += "/";
1737 name += inName;
1738 return name;
1741 //----------------------------------------------------------------------------
1742 std::string cmLocalGenerator::GetRealLocation(const char* inName,
1743 const char* config)
1745 std::string outName=inName;
1746 // Look for a CMake target with the given name, which is an executable
1747 // and which can be run
1748 cmTarget* target = this->Makefile->FindTargetToUse(inName);
1749 if ((target != 0)
1750 && (target->GetType() == cmTarget::EXECUTABLE)
1751 && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
1752 || (target->IsImported() == true)))
1754 outName = target->GetLocation( config );
1756 return outName;
1759 //----------------------------------------------------------------------------
1760 void cmLocalGenerator::AddSharedFlags(std::string& flags,
1761 const char* lang,
1762 bool shared)
1764 std::string flagsVar;
1766 // Add flags for dealing with shared libraries for this language.
1767 if(shared)
1769 flagsVar = "CMAKE_SHARED_LIBRARY_";
1770 flagsVar += lang;
1771 flagsVar += "_FLAGS";
1772 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1775 // Add flags specific to shared builds.
1776 if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
1778 flagsVar = "CMAKE_SHARED_BUILD_";
1779 flagsVar += lang;
1780 flagsVar += "_FLAGS";
1781 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1785 //----------------------------------------------------------------------------
1786 void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
1787 const char* var,
1788 const char* config)
1790 // Add the flags from the variable itself.
1791 std::string flagsVar = var;
1792 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1793 // Add the flags from the build-type specific variable.
1794 if(config && *config)
1796 flagsVar += "_";
1797 flagsVar += cmSystemTools::UpperCase(config);
1798 this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
1802 //----------------------------------------------------------------------------
1803 void cmLocalGenerator::AppendFlags(std::string& flags,
1804 const char* newFlags)
1806 if(newFlags && *newFlags)
1808 std::string newf = newFlags;
1809 if(flags.size())
1811 flags += " ";
1813 flags += newFlags;
1817 //----------------------------------------------------------------------------
1818 void cmLocalGenerator::AppendDefines(std::string& defines,
1819 const char* defines_list,
1820 const char* lang)
1822 // Short-circuit if there are no definitions.
1823 if(!defines_list)
1825 return;
1828 // Expand the list of definitions.
1829 std::vector<std::string> defines_vec;
1830 cmSystemTools::ExpandListArgument(defines_list, defines_vec);
1832 // Short-circuit if there are no definitions.
1833 if(defines_vec.empty())
1835 return;
1838 // Lookup the define flag for the current language.
1839 std::string dflag = "-D";
1840 if(lang)
1842 std::string defineFlagVar = "CMAKE_";
1843 defineFlagVar += lang;
1844 defineFlagVar += "_DEFINE_FLAG";
1845 const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
1846 if(df && *df)
1848 dflag = df;
1852 // Add each definition to the command line with appropriate escapes.
1853 const char* dsep = defines.empty()? "" : " ";
1854 for(std::vector<std::string>::const_iterator di = defines_vec.begin();
1855 di != defines_vec.end(); ++di)
1857 // Skip unsupported definitions.
1858 if(!this->CheckDefinition(*di))
1860 continue;
1863 // Separate from previous definitions.
1864 defines += dsep;
1865 dsep = " ";
1867 // Append the definition with proper escaping.
1868 defines += dflag;
1869 if(this->WatcomWMake)
1871 // The Watcom compiler does its own command line parsing instead
1872 // of using the windows shell rules. Definitions are one of
1873 // -DNAME
1874 // -DNAME=<cpp-token>
1875 // -DNAME="c-string with spaces and other characters(?@#$)"
1877 // Watcom will properly parse each of these cases from the
1878 // command line without any escapes. However we still have to
1879 // get the '$' and '#' characters through WMake as '$$' and
1880 // '$#'.
1881 for(const char* c = di->c_str(); *c; ++c)
1883 if(*c == '$' || *c == '#')
1885 defines += '$';
1887 defines += *c;
1890 else
1892 // Make the definition appear properly on the command line.
1893 defines += this->EscapeForShell(di->c_str(), true);
1898 //----------------------------------------------------------------------------
1899 std::string
1900 cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
1901 const char* default_comment)
1903 // Check for a comment provided with the command.
1904 if(cc.GetComment())
1906 return cc.GetComment();
1909 // Construct a reasonable default comment if possible.
1910 if(!cc.GetOutputs().empty())
1912 std::string comment;
1913 comment = "Generating ";
1914 const char* sep = "";
1915 for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
1916 o != cc.GetOutputs().end(); ++o)
1918 comment += sep;
1919 comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
1920 sep = ", ";
1922 return comment;
1925 // Otherwise use the provided default.
1926 return default_comment;
1929 //----------------------------------------------------------------------------
1930 std::string
1931 cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
1933 return this->Convert(remote, START_OUTPUT, SHELL, true);
1936 //----------------------------------------------------------------------------
1937 std::string cmLocalGenerator::Convert(const char* source,
1938 RelativeRoot relative,
1939 OutputFormat output,
1940 bool optional)
1942 // Make sure the relative path conversion components are set.
1943 if(!this->PathConversionsSetup)
1945 this->SetupPathConversions();
1946 this->PathConversionsSetup = true;
1949 // Convert the path to a relative path.
1950 std::string result = source;
1952 if (!optional || this->UseRelativePaths)
1954 switch (relative)
1956 case HOME:
1957 //result = cmSystemTools::CollapseFullPath(result.c_str());
1958 result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
1959 result.c_str());
1960 break;
1961 case START:
1962 //result = cmSystemTools::CollapseFullPath(result.c_str());
1963 result = this->ConvertToRelativePath(this->StartDirectoryComponents,
1964 result.c_str());
1965 break;
1966 case HOME_OUTPUT:
1967 //result = cmSystemTools::CollapseFullPath(result.c_str());
1968 result =
1969 this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
1970 result.c_str());
1971 break;
1972 case START_OUTPUT:
1973 //result = cmSystemTools::CollapseFullPath(result.c_str());
1974 result =
1975 this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
1976 result.c_str());
1977 break;
1978 case FULL:
1979 result = cmSystemTools::CollapseFullPath(result.c_str());
1980 break;
1981 case NONE:
1982 break;
1985 // Now convert it to an output path.
1986 if (output == MAKEFILE)
1988 result = cmSystemTools::ConvertToOutputPath(result.c_str());
1990 else if( output == SHELL)
1992 // For the MSYS shell convert drive letters to posix paths, so
1993 // that c:/some/path becomes /c/some/path. This is needed to
1994 // avoid problems with the shell path translation.
1995 if(this->MSYSShell && !this->LinkScriptShell)
1997 if(result.size() > 2 && result[1] == ':')
1999 result[1] = result[0];
2000 result[0] = '/';
2003 if(this->WindowsShell)
2005 std::string::size_type pos = 0;
2006 while((pos = result.find('/', pos)) != std::string::npos)
2008 result[pos] = '\\';
2009 pos++;
2012 result = this->EscapeForShell(result.c_str(), true, false);
2014 return result;
2017 //----------------------------------------------------------------------------
2018 std::string cmLocalGenerator::FindRelativePathTopSource()
2020 // Relative path conversion within a single tree managed by CMake is
2021 // safe. We can use our parent relative path top if and only if
2022 // this is a subdirectory of that top.
2023 if(cmLocalGenerator* parent = this->GetParent())
2025 std::string parentTop = parent->FindRelativePathTopSource();
2026 if(cmSystemTools::IsSubDirectory(
2027 this->Makefile->GetStartDirectory(), parentTop.c_str()))
2029 return parentTop;
2033 // Otherwise this directory itself is the new top.
2034 return this->Makefile->GetStartDirectory();
2037 //----------------------------------------------------------------------------
2038 std::string cmLocalGenerator::FindRelativePathTopBinary()
2040 // Relative path conversion within a single tree managed by CMake is
2041 // safe. We can use our parent relative path top if and only if
2042 // this is a subdirectory of that top.
2043 if(cmLocalGenerator* parent = this->GetParent())
2045 std::string parentTop = parent->FindRelativePathTopBinary();
2046 if(cmSystemTools::IsSubDirectory(
2047 this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
2049 return parentTop;
2053 // Otherwise this directory itself is the new top.
2054 return this->Makefile->GetStartOutputDirectory();
2057 //----------------------------------------------------------------------------
2058 void cmLocalGenerator::ConfigureRelativePaths()
2060 // Relative path conversion inside the source tree is not used to
2061 // construct relative paths passed to build tools so it is safe to
2062 // even when the source is a network path.
2063 std::string source = this->FindRelativePathTopSource();
2064 this->RelativePathTopSource = source;
2066 // The current working directory on Windows cannot be a network
2067 // path. Therefore relative paths cannot work when the binary tree
2068 // is a network path.
2069 std::string binary = this->FindRelativePathTopBinary();
2070 if(binary.size() < 2 || binary.substr(0, 2) != "//")
2072 this->RelativePathTopBinary = binary;
2074 else
2076 this->RelativePathTopBinary = "";
2080 //----------------------------------------------------------------------------
2081 static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
2083 return (cmSystemTools::ComparePath(a, b) ||
2084 cmSystemTools::IsSubDirectory(a, b));
2087 //----------------------------------------------------------------------------
2088 std::string
2089 cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
2090 const char* in_remote)
2092 // The path should never be quoted.
2093 assert(in_remote[0] != '\"');
2095 // The local path should never have a trailing slash.
2096 assert(local.size() > 0 && !(local[local.size()-1] == ""));
2098 // If the path is already relative then just return the path.
2099 if(!cmSystemTools::FileIsFullPath(in_remote))
2101 return in_remote;
2104 // Make sure relative path conversion is configured.
2105 if(!this->RelativePathsConfigured)
2107 this->ConfigureRelativePaths();
2108 this->RelativePathsConfigured = true;
2111 // Skip conversion if the path and local are not both in the source
2112 // or both in the binary tree.
2113 std::string local_path = cmSystemTools::JoinPath(local);
2114 if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
2115 this->RelativePathTopBinary.c_str()) &&
2116 cmLocalGeneratorNotAbove(in_remote,
2117 this->RelativePathTopBinary.c_str())) ||
2118 (cmLocalGeneratorNotAbove(local_path.c_str(),
2119 this->RelativePathTopSource.c_str()) &&
2120 cmLocalGeneratorNotAbove(in_remote,
2121 this->RelativePathTopSource.c_str()))))
2123 return in_remote;
2126 // Identify the longest shared path component between the remote
2127 // path and the local path.
2128 std::vector<std::string> remote;
2129 cmSystemTools::SplitPath(in_remote, remote);
2130 unsigned int common=0;
2131 while(common < remote.size() &&
2132 common < local.size() &&
2133 cmSystemTools::ComparePath(remote[common].c_str(),
2134 local[common].c_str()))
2136 ++common;
2139 // If no part of the path is in common then return the full path.
2140 if(common == 0)
2142 return in_remote;
2145 // If the entire path is in common then just return a ".".
2146 if(common == remote.size() &&
2147 common == local.size())
2149 return ".";
2152 // If the entire path is in common except for a trailing slash then
2153 // just return a "./".
2154 if(common+1 == remote.size() &&
2155 remote[common].size() == 0 &&
2156 common == local.size())
2158 return "./";
2161 // Construct the relative path.
2162 std::string relative;
2164 // First add enough ../ to get up to the level of the shared portion
2165 // of the path. Leave off the trailing slash. Note that the last
2166 // component of local will never be empty because local should never
2167 // have a trailing slash.
2168 for(unsigned int i=common; i < local.size(); ++i)
2170 relative += "..";
2171 if(i < local.size()-1)
2173 relative += "/";
2177 // Now add the portion of the destination path that is not included
2178 // in the shared portion of the path. Add a slash the first time
2179 // only if there was already something in the path. If there was a
2180 // trailing slash in the input then the last iteration of the loop
2181 // will add a slash followed by an empty string which will preserve
2182 // the trailing slash in the output.
2183 for(unsigned int i=common; i < remote.size(); ++i)
2185 if(relative.size() > 0)
2187 relative += "/";
2189 relative += remote[i];
2192 // Finally return the path.
2193 return relative;
2196 //----------------------------------------------------------------------------
2197 void
2198 cmLocalGenerator
2199 ::GenerateTargetInstallRules(
2200 std::ostream& os, const char* config,
2201 std::vector<std::string> const& configurationTypes)
2203 // Convert the old-style install specification from each target to
2204 // an install generator and run it.
2205 cmTargets& tgts = this->Makefile->GetTargets();
2206 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
2208 // Include the user-specified pre-install script for this target.
2209 if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
2211 cmInstallScriptGenerator g(preinstall, false, 0);
2212 g.Generate(os, config, configurationTypes);
2215 // Install this target if a destination is given.
2216 if(l->second.GetInstallPath() != "")
2218 // Compute the full install destination. Note that converting
2219 // to unix slashes also removes any trailing slash.
2220 // We also skip over the leading slash given by the user.
2221 std::string destination = l->second.GetInstallPath().substr(1);
2222 cmSystemTools::ConvertToUnixSlashes(destination);
2224 // Generate the proper install generator for this target type.
2225 switch(l->second.GetType())
2227 case cmTarget::EXECUTABLE:
2228 case cmTarget::STATIC_LIBRARY:
2229 case cmTarget::MODULE_LIBRARY:
2231 // Use a target install generator.
2232 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2233 g.Generate(os, config, configurationTypes);
2235 break;
2236 case cmTarget::SHARED_LIBRARY:
2238 #if defined(_WIN32) || defined(__CYGWIN__)
2239 // Special code to handle DLL. Install the import library
2240 // to the normal destination and the DLL to the runtime
2241 // destination.
2242 cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
2243 g1.Generate(os, config, configurationTypes);
2244 // We also skip over the leading slash given by the user.
2245 destination = l->second.GetRuntimeInstallPath().substr(1);
2246 cmSystemTools::ConvertToUnixSlashes(destination);
2247 cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
2248 g2.Generate(os, config, configurationTypes);
2249 #else
2250 // Use a target install generator.
2251 cmInstallTargetGenerator g(l->second, destination.c_str(), false);
2252 g.Generate(os, config, configurationTypes);
2253 #endif
2255 break;
2256 default:
2257 break;
2261 // Include the user-specified post-install script for this target.
2262 if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
2264 cmInstallScriptGenerator g(postinstall, false, 0);
2265 g.Generate(os, config, configurationTypes);
2270 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2271 static std::string cmLocalGeneratorMD5(const char* input)
2273 char md5out[32];
2274 cmsysMD5* md5 = cmsysMD5_New();
2275 cmsysMD5_Initialize(md5);
2276 cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
2277 cmsysMD5_FinalizeHex(md5, md5out);
2278 cmsysMD5_Delete(md5);
2279 return std::string(md5out, 32);
2282 static bool
2283 cmLocalGeneratorShortenObjectName(std::string& objName,
2284 std::string::size_type max_len)
2286 // Replace the beginning of the path portion of the object name with
2287 // its own md5 sum.
2288 std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
2289 if(pos != objName.npos)
2291 std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
2292 md5name += objName.substr(pos);
2293 objName = md5name;
2295 // The object name is now short enough.
2296 return true;
2298 else
2300 // The object name could not be shortened enough.
2301 return false;
2305 static bool cmLocalGeneratorCheckObjectName(std::string& objName,
2306 std::string::size_type dir_len)
2308 // Choose a maximum file name length.
2309 #if defined(_WIN32) || defined(__CYGWIN__)
2310 std::string::size_type const max_total_len = 250;
2311 #else
2312 std::string::size_type const max_total_len = 1000;
2313 #endif
2315 // Enforce the maximum file name length if possible.
2316 std::string::size_type max_obj_len = max_total_len;
2317 if(dir_len < max_total_len)
2319 max_obj_len = max_total_len - dir_len;
2320 if(objName.size() > max_obj_len)
2322 // The current object file name is too long. Try to shorten it.
2323 return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
2325 else
2327 // The object file name is short enough.
2328 return true;
2331 else
2333 // The build directory in which the object will be stored is
2334 // already too deep.
2335 return false;
2338 #endif
2340 //----------------------------------------------------------------------------
2341 std::string&
2342 cmLocalGenerator
2343 ::CreateSafeUniqueObjectFileName(const char* sin,
2344 std::string::size_type dir_len)
2346 // Look for an existing mapped name for this object file.
2347 std::map<cmStdString,cmStdString>::iterator it =
2348 this->UniqueObjectNamesMap.find(sin);
2350 // If no entry exists create one.
2351 if(it == this->UniqueObjectNamesMap.end())
2353 // Start with the original name.
2354 std::string ssin = sin;
2356 // Avoid full paths by removing leading slashes.
2357 std::string::size_type pos = 0;
2358 for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
2359 ssin = ssin.substr(pos);
2361 // Avoid full paths by removing colons.
2362 cmSystemTools::ReplaceString(ssin, ":", "_");
2364 // Avoid relative paths that go up the tree.
2365 cmSystemTools::ReplaceString(ssin, "../", "__/");
2367 // Avoid spaces.
2368 cmSystemTools::ReplaceString(ssin, " ", "_");
2370 // Mangle the name if necessary.
2371 if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
2373 bool done;
2374 int cc = 0;
2375 char rpstr[100];
2376 sprintf(rpstr, "_p_");
2377 cmSystemTools::ReplaceString(ssin, "+", rpstr);
2378 std::string sssin = sin;
2381 done = true;
2382 for ( it = this->UniqueObjectNamesMap.begin();
2383 it != this->UniqueObjectNamesMap.end();
2384 ++ it )
2386 if ( it->second == ssin )
2388 done = false;
2391 if ( done )
2393 break;
2395 sssin = ssin;
2396 cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
2397 sprintf(rpstr, "_p%d_", cc++);
2399 while ( !done );
2402 #if defined(CM_LG_ENCODE_OBJECT_NAMES)
2403 cmLocalGeneratorCheckObjectName(ssin, dir_len);
2404 #else
2405 (void)dir_len;
2406 #endif
2408 // Insert the newly mapped object file name.
2409 std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
2410 it = this->UniqueObjectNamesMap.insert(e).first;
2413 // Return the map entry.
2414 return it->second;
2417 //----------------------------------------------------------------------------
2418 std::string
2419 cmLocalGenerator
2420 ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
2421 std::string::size_type dir_len,
2422 bool* hasSourceExtension)
2424 // Construct the object file name using the full path to the source
2425 // file which is its only unique identification.
2426 const char* fullPath = source.GetFullPath().c_str();
2428 // Try referencing the source relative to the source tree.
2429 std::string relFromSource = this->Convert(fullPath, START);
2430 assert(!relFromSource.empty());
2431 bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
2432 bool subSource = relSource && relFromSource[0] != '.';
2434 // Try referencing the source relative to the binary tree.
2435 std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
2436 assert(!relFromBinary.empty());
2437 bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
2438 bool subBinary = relBinary && relFromBinary[0] != '.';
2440 // Select a nice-looking reference to the source file to construct
2441 // the object file name.
2442 std::string objectName;
2443 if((relSource && !relBinary) || (subSource && !subBinary))
2445 objectName = relFromSource;
2447 else if((relBinary && !relSource) || (subBinary && !subSource))
2449 objectName = relFromBinary;
2451 else if(relFromBinary.length() < relFromSource.length())
2453 objectName = relFromBinary;
2455 else
2457 objectName = relFromSource;
2460 // if it is still a full path check for the try compile case
2461 // try compile never have in source sources, and should not
2462 // have conflicting source file names in the same target
2463 if(cmSystemTools::FileIsFullPath(objectName.c_str()))
2465 if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
2467 objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
2471 // Replace the original source file extension with the object file
2472 // extension.
2473 bool keptSourceExtension = true;
2474 if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
2476 // Decide whether this language wants to replace the source
2477 // extension with the object extension. For CMake 2.4
2478 // compatibility do this by default.
2479 bool replaceExt = this->NeedBackwardsCompatibility(2, 4);
2480 if(!replaceExt)
2482 std::string repVar = "CMAKE_";
2483 repVar += source.GetLanguage();
2484 repVar += "_OUTPUT_EXTENSION_REPLACE";
2485 replaceExt = this->Makefile->IsOn(repVar.c_str());
2488 // Remove the source extension if it is to be replaced.
2489 if(replaceExt)
2491 keptSourceExtension = false;
2492 std::string::size_type dot_pos = objectName.rfind(".");
2493 if(dot_pos != std::string::npos)
2495 objectName = objectName.substr(0, dot_pos);
2499 // Store the new extension.
2500 objectName +=
2501 this->GlobalGenerator->GetLanguageOutputExtension(source);
2503 if(hasSourceExtension)
2505 *hasSourceExtension = keptSourceExtension;
2508 // Convert to a safe name.
2509 return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
2512 //----------------------------------------------------------------------------
2513 const char*
2514 cmLocalGenerator
2515 ::GetSourceFileLanguage(const cmSourceFile& source)
2517 return source.GetLanguage();
2520 //----------------------------------------------------------------------------
2521 std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
2523 std::string result;
2524 bool forceOn = cmSystemTools::GetForceUnixPaths();
2525 if(forceOn && this->WindowsShell)
2527 cmSystemTools::SetForceUnixPaths(false);
2529 result = cmSystemTools::EscapeSpaces(str);
2530 if(forceOn && this->WindowsShell)
2532 cmSystemTools::SetForceUnixPaths(true);
2534 return result;
2537 //----------------------------------------------------------------------------
2538 std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
2539 bool forEcho)
2541 // Compute the flags for the target shell environment.
2542 int flags = 0;
2543 if(this->WindowsVSIDE)
2545 flags |= cmsysSystem_Shell_Flag_VSIDE;
2547 else
2549 flags |= cmsysSystem_Shell_Flag_Make;
2551 if(makeVars)
2553 flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
2555 if(forEcho)
2557 flags |= cmsysSystem_Shell_Flag_EchoWindows;
2559 if(this->WatcomWMake)
2561 flags |= cmsysSystem_Shell_Flag_WatcomWMake;
2563 if(this->MinGWMake)
2565 flags |= cmsysSystem_Shell_Flag_MinGWMake;
2567 if(this->NMake)
2569 flags |= cmsysSystem_Shell_Flag_NMake;
2572 // Compute the buffer size needed.
2573 int size = (this->WindowsShell ?
2574 cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
2575 cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
2577 // Compute the shell argument itself.
2578 std::vector<char> arg(size);
2579 if(this->WindowsShell)
2581 cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
2583 else
2585 cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
2587 return std::string(&arg[0]);
2590 //----------------------------------------------------------------------------
2591 std::string cmLocalGenerator::EscapeForCMake(const char* str)
2593 // Always double-quote the argument to take care of most escapes.
2594 std::string result = "\"";
2595 for(const char* c = str; *c; ++c)
2597 if(*c == '"')
2599 // Escape the double quote to avoid ending the argument.
2600 result += "\\\"";
2602 else if(*c == '$')
2604 // Escape the dollar to avoid expanding variables.
2605 result += "\\$";
2607 else if(*c == '\\')
2609 // Escape the backslash to avoid other escapes.
2610 result += "\\\\";
2612 else
2614 // Other characters will be parsed correctly.
2615 result += *c;
2618 result += "\"";
2619 return result;
2622 //----------------------------------------------------------------------------
2623 std::string
2624 cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
2626 cmSystemTools::Error("GetTargetDirectory"
2627 " called on cmLocalGenerator");
2628 return "";
2632 //----------------------------------------------------------------------------
2633 void
2634 cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
2635 std::vector<std::string>&
2638 cmSystemTools::Error("GetTargetObjectFileDirectories"
2639 " called on cmLocalGenerator");
2642 //----------------------------------------------------------------------------
2643 unsigned int cmLocalGenerator::GetBackwardsCompatibility()
2645 // The computed version may change until the project is fully
2646 // configured.
2647 if(!this->BackwardsCompatibilityFinal)
2649 unsigned int major = 0;
2650 unsigned int minor = 0;
2651 unsigned int patch = 0;
2652 if(const char* value
2653 = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
2655 switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
2657 case 2: patch = 0; break;
2658 case 1: minor = 0; patch = 0; break;
2659 default: break;
2662 this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
2663 this->BackwardsCompatibilityFinal = this->Configured;
2666 return this->BackwardsCompatibility;
2669 //----------------------------------------------------------------------------
2670 bool cmLocalGenerator::NeedBackwardsCompatibility(unsigned int major,
2671 unsigned int minor,
2672 unsigned int patch)
2674 // Check the policy to decide whether to pay attention to this
2675 // variable.
2676 switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0001))
2678 case cmPolicies::WARN:
2679 // WARN is just OLD without warning because user code does not
2680 // always affect whether this check is done.
2681 case cmPolicies::OLD:
2682 // Old behavior is to check the variable.
2683 break;
2684 case cmPolicies::NEW:
2685 // New behavior is to ignore the variable.
2686 return false;
2687 case cmPolicies::REQUIRED_IF_USED:
2688 case cmPolicies::REQUIRED_ALWAYS:
2689 // This will never be the case because the only way to require
2690 // the setting is to require the user to specify version policy
2691 // 2.6 or higher. Once we add that requirement then this whole
2692 // method can be removed anyway.
2693 return false;
2696 // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
2697 // equal to or lower than the given version.
2698 unsigned int actual_compat = this->GetBackwardsCompatibility();
2699 return (actual_compat &&
2700 actual_compat <= CMake_VERSION_ENCODE(major, minor, patch));
2703 //----------------------------------------------------------------------------
2704 bool cmLocalGenerator::CheckDefinition(std::string const& define) const
2706 // Many compilers do not support -DNAME(arg)=sdf so we disable it.
2707 bool function_style = false;
2708 for(const char* c = define.c_str(); *c && *c != '='; ++c)
2710 if(*c == '(')
2712 function_style = true;
2713 break;
2716 if(function_style)
2718 cmOStringStream e;
2719 e << "WARNING: Function-style preprocessor definitions may not be "
2720 << "passed on the compiler command line because many compilers "
2721 << "do not support it.\n"
2722 << "CMake is dropping a preprocessor definition: " << define << "\n"
2723 << "Consider defining the macro in a (configured) header file.\n";
2724 cmSystemTools::Message(e.str().c_str());
2725 return false;
2728 // Many compilers do not support # in the value so we disable it.
2729 if(define.find_first_of("#") != define.npos)
2731 cmOStringStream e;
2732 e << "WARNING: Peprocessor definitions containing '#' may not be "
2733 << "passed on the compiler command line because many compilers "
2734 << "do not support it.\n"
2735 << "CMake is dropping a preprocessor definition: " << define << "\n"
2736 << "Consider defining the macro in a (configured) header file.\n";
2737 cmSystemTools::Message(e.str().c_str());
2738 return false;
2741 // Assume it is supported.
2742 return true;
2745 //----------------------------------------------------------------------------
2746 static std::string cmLGInfoProp(cmTarget* target, const char* prop)
2748 if(const char* val = target->GetProperty(prop))
2750 return val;
2752 else
2754 // For compatibility check for a variable.
2755 return target->GetMakefile()->GetSafeDefinition(prop);
2759 //----------------------------------------------------------------------------
2760 void cmLocalGenerator::GenerateAppleInfoPList(cmTarget* target,
2761 const char* targetName,
2762 const char* fname)
2764 std::string info_EXECUTABLE_NAME = targetName;
2766 // Lookup the properties.
2767 std::string info_INFO_STRING =
2768 cmLGInfoProp(target, "MACOSX_BUNDLE_INFO_STRING");
2769 std::string info_ICON_FILE =
2770 cmLGInfoProp(target, "MACOSX_BUNDLE_ICON_FILE");
2771 std::string info_GUI_IDENTIFIER =
2772 cmLGInfoProp(target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
2773 std::string info_LONG_VERSION_STRING =
2774 cmLGInfoProp(target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
2775 std::string info_BUNDLE_NAME =
2776 cmLGInfoProp(target, "MACOSX_BUNDLE_BUNDLE_NAME");
2777 std::string info_SHORT_VERSION_STRING =
2778 cmLGInfoProp(target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
2779 std::string info_BUNDLE_VERSION =
2780 cmLGInfoProp(target, "MACOSX_BUNDLE_BUNDLE_VERSION");
2781 std::string info_COPYRIGHT =
2782 cmLGInfoProp(target, "MACOSX_BUNDLE_COPYRIGHT");
2784 // Generate the file.
2785 cmGeneratedFileStream fout(fname);
2786 fout.SetCopyIfDifferent(true);
2787 fout <<
2788 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
2789 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"\n"
2790 " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
2791 "<plist version=\"1.0\">\n"
2792 "<dict>\n"
2793 "\t<key>CFBundleDevelopmentRegion</key>\n"
2794 "\t<string>English</string>\n"
2795 "\t<key>CFBundleExecutable</key>\n"
2796 "\t<string>" << info_EXECUTABLE_NAME << "</string>\n"
2797 "\t<key>CFBundleGetInfoString</key>\n"
2798 "\t<string>" << info_INFO_STRING << "</string>\n"
2799 "\t<key>CFBundleIconFile</key>\n"
2800 "\t<string>" << info_ICON_FILE << "</string>\n"
2801 "\t<key>CFBundleIdentifier</key>\n"
2802 "\t<string>" << info_GUI_IDENTIFIER << "</string>\n"
2803 "\t<key>CFBundleInfoDictionaryVersion</key>\n"
2804 "\t<string>6.0</string>\n"
2805 "\t<key>CFBundleLongVersionString</key>\n"
2806 "\t<string>" << info_LONG_VERSION_STRING << "</string>\n"
2807 "\t<key>CFBundleName</key>\n"
2808 "\t<string>" << info_BUNDLE_NAME << "</string>\n"
2809 "\t<key>CFBundlePackageType</key>\n"
2810 "\t<string>APPL</string>\n"
2811 "\t<key>CFBundleShortVersionString</key>\n"
2812 "\t<string>" << info_SHORT_VERSION_STRING << "</string>\n"
2813 "\t<key>CFBundleSignature</key>\n"
2814 "\t<string>????" /* break string to avoid trigraph */ "</string>\n"
2815 "\t<key>CFBundleVersion</key>\n"
2816 "\t<string>" << info_BUNDLE_VERSION << "</string>\n"
2817 "\t<key>CSResourcesFileMapped</key>\n"
2818 "\t<true/>\n"
2819 "\t<key>LSRequiresCarbon</key>\n"
2820 "\t<true/>\n"
2821 "\t<key>NSHumanReadableCopyright</key>\n"
2822 "\t<string>" << info_COPYRIGHT << "</string>\n"
2823 "</dict>\n"
2824 "</plist>\n"