FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmUnixMakefileGenerator.cxx
blobc685f1b5f36c0a336b1536fcc2e2a2a46624c1b9
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmUnixMakefileGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2002-07-31 17:45:07 $
7 Version: $Revision: 1.224 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmUnixMakefileGenerator.h"
18 #include "cmMakefile.h"
19 #include "cmStandardIncludes.h"
20 #include "cmSystemTools.h"
21 #include "cmSourceFile.h"
22 #include "cmMakeDepend.h"
23 #include "cmCacheManager.h"
24 #include "cmGeneratedFileStream.h"
26 cmUnixMakefileGenerator::cmUnixMakefileGenerator()
27 :m_SharedLibraryExtension("$(SHLIB_SUFFIX)"),
28 m_ObjectFileExtension(".o"),
29 m_ExecutableExtension(""),
30 m_StaticLibraryExtension(".a"),
31 m_LibraryPrefix("lib")
33 m_CacheOnly = false;
34 m_Recurse = false;
37 cmUnixMakefileGenerator::~cmUnixMakefileGenerator()
42 void cmUnixMakefileGenerator::GenerateMakefile()
44 // for backwards compatibility if niether c or cxx is
45 // enabled, the enable cxx
46 if(! (this->GetLanguageEnabled("C") ||
47 this->GetLanguageEnabled("CXX")))
49 this->EnableLanguage("CXX");
53 // suppoirt override in output directories
54 if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
56 m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
57 if(m_LibraryOutputPath.size())
59 if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
61 m_LibraryOutputPath += "/";
63 if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
65 cmSystemTools::Error("Error failed create "
66 "LIBRARY_OUTPUT_PATH directory:",
67 m_LibraryOutputPath.c_str());
69 m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
72 if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
74 m_ExecutableOutputPath =
75 m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
76 if(m_ExecutableOutputPath.size())
78 if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
80 m_ExecutableOutputPath += "/";
82 if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
84 cmSystemTools::Error("Error failed to create "
85 "EXECUTABLE_OUTPUT_PATH directory:",
86 m_ExecutableOutputPath.c_str());
88 m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
93 if(m_CacheOnly)
95 // Generate the cache only stuff
96 this->GenerateCacheOnly();
97 // if recurse then generate for all sub- makefiles
98 if(m_Recurse)
100 this->RecursiveGenerateCacheOnly();
103 // normal makefile output
104 else
106 // Generate depends
107 cmMakeDepend md;
108 md.SetMakefile(m_Makefile);
109 md.GenerateMakefileDependencies();
110 this->ProcessDepends(md);
111 // output the makefile fragment
112 this->OutputMakefile("Makefile");
116 void cmUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
118 // Now create cmDependInformation objects for files in the directory
119 cmTargets &tgts = m_Makefile->GetTargets();
120 for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
122 std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
123 for(std::vector<cmSourceFile*>::iterator i = classes.begin();
124 i != classes.end(); ++i)
126 if(!(*i)->GetIsAHeaderFileOnly())
128 // get the depends
129 const cmDependInformation *info =
130 md.GetDependInformationForSourceFile(*(*i));
132 // Delete any hints from the source file's dependencies.
133 (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
135 // Now add the real dependencies for the file.
136 if (info)
138 for(cmDependInformation::DependencySet::const_iterator d =
139 info->m_DependencySet.begin();
140 d != info->m_DependencySet.end(); ++d)
142 // Make sure the full path is given. If not, the dependency was
143 // not found.
144 if((*d)->m_FullPath != "")
146 (*i)->GetDepends().push_back((*d)->m_FullPath);
156 // This is where CMakeTargets.make is generated
157 void cmUnixMakefileGenerator::OutputMakefile(const char* file)
159 // Create sub directories fro aux source directories
160 std::vector<std::string>& auxSourceDirs =
161 m_Makefile->GetAuxSourceDirectories();
162 if( auxSourceDirs.size() )
164 // For the case when this is running as a remote build
165 // on unix, make the directory
166 for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
167 i != auxSourceDirs.end(); ++i)
169 if(i->c_str()[0] != '/')
171 std::string dir = m_Makefile->GetCurrentOutputDirectory();
172 if(dir.size() && dir[dir.size()-1] != '/')
174 dir += "/";
176 dir += *i;
177 cmSystemTools::MakeDirectory(dir.c_str());
179 else
181 cmSystemTools::MakeDirectory(i->c_str());
185 // Create a stream that writes to a temporary file
186 // then does a copy at the end. This is to allow users
187 // to hit control-c during the make of the makefile
188 cmGeneratedFileStream tempFile(file);
189 tempFile.SetAlwaysCopy(true);
190 std::ostream& fout = tempFile.GetStream();
191 if(!fout)
193 cmSystemTools::Error("Error can not open for write: ", file);
194 return;
196 fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
197 << "# Generated by \"" << this->GetName() << "\""
198 << " Generator, CMake Version "
199 << cmMakefile::GetMajorVersion() << "."
200 << cmMakefile::GetMinorVersion() << "\n"
201 << "# Generated from the following files:\n# "
202 << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
203 std::vector<std::string> lfiles = m_Makefile->GetListFiles();
204 // sort the array
205 std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
206 // remove duplicates
207 std::vector<std::string>::iterator new_end =
208 std::unique(lfiles.begin(), lfiles.end());
209 lfiles.erase(new_end, lfiles.end());
211 for(std::vector<std::string>::const_iterator i = lfiles.begin();
212 i != lfiles.end(); ++i)
214 fout << "# " << i->c_str() << "\n";
216 fout << "\n\n";
217 fout << "# Suppresses display of executed commands\n";
218 fout << ".SILENT:\n";
219 fout << "# disable some common implicit rules to speed things up\n";
220 fout << ".SUFFIXES:\n";
221 fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
222 // create a make variable with all of the sources for this Makefile
223 // for depend purposes.
224 fout << "CMAKE_MAKEFILE_SOURCES = ";
225 for(std::vector<std::string>::const_iterator i = lfiles.begin();
226 i != lfiles.end(); ++i)
228 fout << " " << this->ConvertToOutputPath(i->c_str());
230 // Add the cache to the list
231 std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
232 cacheFile += "/CMakeCache.txt";
233 fout << " " << this->ConvertToOutputPath(cacheFile.c_str());
234 fout << "\n\n\n";
235 this->OutputMakeVariables(fout);
236 // Set up the default target as the VERY first target, so that make with no arguments will run it
237 this->
238 OutputMakeRule(fout,
239 "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
240 "default_target",
242 "$(MAKE) $(MAKESILENT) cmake.depends",
243 "$(MAKE) $(MAKESILENT) cmake.check_depends",
244 "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
245 "$(MAKE) $(MAKESILENT) all");
247 this->OutputTargetRules(fout);
248 this->OutputDependLibs(fout);
249 this->OutputTargets(fout);
250 this->OutputSubDirectoryRules(fout);
251 std::string dependName = m_Makefile->GetStartOutputDirectory();
252 dependName += "/cmake.depends";
253 if(!this->m_CacheOnly)
255 std::ofstream dependout(dependName.c_str());
256 if(!dependout)
258 cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
259 return;
261 dependout << "# .o dependencies in this directory." << std::endl;
263 std::string checkDepend = m_Makefile->GetStartOutputDirectory();
264 checkDepend += "/cmake.check_depends";
265 std::ofstream checkdependout(checkDepend.c_str());
266 if(!checkdependout)
268 cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
269 return;
271 checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
272 // if there were any depends output, then output the check depends
273 // information inot checkdependout
274 if(this->OutputObjectDepends(dependout))
276 this->OutputCheckDepends(checkdependout);
278 else
280 checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
283 this->OutputCustomRules(fout);
284 this->OutputMakeRules(fout);
285 this->OutputInstallRules(fout);
286 // only add the depend include if the depend file exists
287 if(cmSystemTools::FileExists(dependName.c_str()))
289 this->OutputIncludeMakefile(fout, "cmake.depends");
293 void cmUnixMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
294 const char* file)
296 fout << "include " << file << "\n";
300 std::string
301 cmUnixMakefileGenerator::GetOutputExtension(const char*)
303 return m_ObjectFileExtension;
308 // Output the rules for any targets
309 void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
311 // for each target add to the list of targets
312 fout << "TARGETS = ";
313 const cmTargets &tgts = m_Makefile->GetTargets();
314 // list libraries first
315 for(cmTargets::const_iterator l = tgts.begin();
316 l != tgts.end(); l++)
318 if (l->second.IsInAll())
320 std::string path = m_LibraryOutputPath + m_LibraryPrefix;
321 if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
323 path = path + l->first + m_StaticLibraryExtension;
324 fout << " \\\n"
325 << this->ConvertToOutputPath(path.c_str());
327 else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
329 path = path + l->first +
330 m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
331 fout << " \\\n"
332 << this->ConvertToOutputPath(path.c_str());
334 else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
336 path = path + l->first +
337 m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
338 fout << " \\\n"
339 << this->ConvertToOutputPath(path.c_str());
343 // executables
344 for(cmTargets::const_iterator l = tgts.begin();
345 l != tgts.end(); l++)
347 if ((l->second.GetType() == cmTarget::EXECUTABLE ||
348 l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
349 l->second.IsInAll())
351 std::string path = m_ExecutableOutputPath + l->first +
352 m_ExecutableExtension;
353 fout << " \\\n" << this->ConvertToOutputPath(path.c_str());
356 // list utilities last
357 for(cmTargets::const_iterator l = tgts.begin();
358 l != tgts.end(); l++)
360 if (l->second.GetType() == cmTarget::UTILITY &&
361 l->second.IsInAll())
363 fout << " \\\n" << l->first.c_str();
366 fout << "\n\n";
367 // get the classes from the source lists then add them to the groups
368 for(cmTargets::const_iterator l = tgts.begin();
369 l != tgts.end(); l++)
371 std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
372 if (classes.begin() != classes.end())
374 fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
375 for(std::vector<cmSourceFile*>::iterator i = classes.begin();
376 i != classes.end(); i++)
378 if(!(*i)->IsAHeaderFileOnly())
380 std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
381 if(outExt.size())
383 fout << "\\\n" << this->ConvertToOutputPath((*i)->GetSourceName().c_str())
384 << outExt.c_str() << " ";
388 fout << "\n\n";
389 fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
390 for(std::vector<cmSourceFile*>::iterator i = classes.begin();
391 i != classes.end(); i++)
393 if(!(*i)->IsAHeaderFileOnly())
395 std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
396 if(outExt.size())
398 fout << "\\\n\"" << this->ConvertToOutputPath((*i)->GetSourceName().c_str())
399 << outExt.c_str() << "\" ";
403 fout << "\n\n";
406 fout << "CLEAN_OBJECT_FILES = ";
407 for(cmTargets::const_iterator l = tgts.begin();
408 l != tgts.end(); l++)
410 std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
411 if (classes.begin() != classes.end())
413 fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
414 << ") ";
417 fout << "\n\n";
422 * Output the linking rules on a command line. For executables,
423 * targetLibrary should be a NULL pointer. For libraries, it should point
424 * to the name of the library. This will not link a library against itself.
426 void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
427 const char* targetLibrary,
428 const cmTarget &tgt)
430 // Try to emit each search path once
431 std::set<std::string> emitted;
433 // Embed runtime search paths if possible and if required.
434 bool outputRuntime = true;
435 std::string runtimeFlag;
436 std::string runtimeSep;
437 std::vector<std::string> runtimeDirs;
439 bool cxx = tgt.HasCxx();
440 if(!cxx )
442 // if linking a c executable use the C runtime flag as cc
443 // may not be the same program that creates shared libaries
444 // and may have different flags
445 if( tgt.GetType() == cmTarget::EXECUTABLE)
447 if(m_Makefile->GetDefinition("CMAKE_C_SHLIB_RUNTIME_FLAG"))
449 runtimeFlag = m_Makefile->GetDefinition("CMAKE_C_SHLIB_RUNTIME_FLAG");
452 else
454 if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
456 runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
459 if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
461 runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
464 else
466 if(m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_FLAG"))
468 runtimeFlag = m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_FLAG");
471 if(m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_SEP"))
473 runtimeSep = m_Makefile->GetDefinition("CMAKE_CXX_SHLIB_RUNTIME_SEP");
479 // concatenate all paths or no?
480 bool runtimeConcatenate = ( runtimeSep!="" );
481 if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
483 outputRuntime = false;
486 // Some search paths should never be emitted
487 emitted.insert("");
488 emitted.insert("/usr/lib");
490 // collect all the flags needed for linking libraries
491 std::string linkLibs;
492 const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
493 for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
494 libDir != libdirs.end(); ++libDir)
496 std::string libpath = this->ConvertToOutputPath(libDir->c_str());
497 if(emitted.insert(libpath).second)
499 std::string::size_type pos = libDir->find("-L");
500 if((pos == std::string::npos || pos > 0)
501 && libDir->find("${") == std::string::npos)
503 linkLibs += "-L";
504 if(outputRuntime)
506 runtimeDirs.push_back( libpath );
509 linkLibs += libpath;
510 linkLibs += " ";
514 std::string librariesLinked;
515 const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
516 for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
517 lib != libs.end(); ++lib)
519 // Don't link the library against itself!
520 if(targetLibrary && (lib->first == targetLibrary)) continue;
521 // don't look at debug libraries
522 if (lib->second == cmTarget::DEBUG) continue;
523 // skip zero size library entries, this may happen
524 // if a variable expands to nothing.
525 if (lib->first.size() == 0) continue;
526 // if it is a full path break it into -L and -l
527 cmRegularExpression reg("([ \t]*\\-l)|([ \t]*\\-framework)|(\\${)");
528 if(lib->first.find('/') != std::string::npos
529 && !reg.find(lib->first))
531 std::string dir, file;
532 cmSystemTools::SplitProgramPath(lib->first.c_str(),
533 dir, file);
534 std::string libpath = this->ConvertToOutputPath(dir.c_str());
535 if(emitted.insert(libpath).second)
537 linkLibs += "-L";
538 linkLibs += libpath;
539 linkLibs += " ";
540 if(outputRuntime)
542 runtimeDirs.push_back( libpath );
545 cmRegularExpression libname("lib(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
546 cmRegularExpression libname_noprefix("(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
547 if(libname.find(file))
549 librariesLinked += "-l";
550 file = libname.match(1);
551 librariesLinked += file;
552 librariesLinked += " ";
554 else if(libname_noprefix.find(file))
556 librariesLinked += "-l";
557 file = libname_noprefix.match(1);
558 librariesLinked += file;
559 librariesLinked += " ";
562 // not a full path, so add -l name
563 else
565 if(!reg.find(lib->first))
567 librariesLinked += "-l";
569 librariesLinked += lib->first;
570 librariesLinked += " ";
574 linkLibs += librariesLinked;
576 fout << linkLibs;
578 if(outputRuntime && runtimeDirs.size()>0)
580 // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
581 // a "-R a -R b -R c" type link line
582 fout << runtimeFlag;
583 std::vector<std::string>::iterator itr = runtimeDirs.begin();
584 fout << *itr;
585 ++itr;
586 for( ; itr != runtimeDirs.end(); ++itr )
588 if(runtimeConcatenate)
590 fout << runtimeSep << *itr;
592 else
594 fout << " " << runtimeFlag << *itr;
597 fout << " ";
602 std::string cmUnixMakefileGenerator::CreateTargetRules(const cmTarget &target,
603 const char* targetName)
605 std::string customRuleCode = "";
606 bool initNext = false;
607 for (std::vector<cmCustomCommand>::const_iterator cr =
608 target.GetCustomCommands().begin();
609 cr != target.GetCustomCommands().end(); ++cr)
611 cmCustomCommand cc(*cr);
612 cc.ExpandVariables(*m_Makefile);
613 if (cc.GetSourceName() == targetName)
615 if(initNext)
617 customRuleCode += "\n\t";
619 else
621 initNext = true;
623 std::string command = this->ConvertToOutputPath(cc.GetCommand().c_str());
624 customRuleCode += command + " " + cc.GetArguments();
627 return customRuleCode;
631 void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
632 const char* name,
633 const cmTarget &t)
635 std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
636 std::string depend = "$(";
637 depend += this->CreateMakeVariable(name, "_SRC_OBJS");
638 depend += ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
639 std::string command = "$(RM) lib";
640 command += name;
641 command += "$(SHLIB_SUFFIX)";
642 std::string command2;
643 if(t.HasCxx())
645 command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_SHLIB_LINK_FLAGS) "
646 "$(CMAKE_CXX_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
648 else
650 command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
651 "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
653 command2 += "\t ";
654 std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX)";
655 libName = this->ConvertToOutputPath(libName.c_str());
656 command2 += libName + " \\\n";
657 command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
658 cmStringStream linklibs;
659 this->OutputLinkLibraries(linklibs, name, t);
660 command2 += linklibs.str();
661 std::string customCommands = this->CreateTargetRules(t, name);
662 const char* cc = 0;
663 if(customCommands.size() > 0)
665 cc = customCommands.c_str();
667 this->OutputMakeRule(fout, "rules for a shared library",
668 target.c_str(),
669 depend.c_str(),
670 command.c_str(),
671 command2.c_str(),
672 cc);
675 void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
676 const char* name,
677 const cmTarget &t)
679 std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
680 std::string depend = "$(";
681 depend += this->CreateMakeVariable(name, "_SRC_OBJS")
682 + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
683 std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
684 std::string command2;
685 if(t.HasCxx())
687 command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_MODULE_LINK_FLAGS) "
688 "$(CMAKE_CXX_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
690 else
692 command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
693 "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
695 command2 += "\t ";
696 std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
697 libName = this->ConvertToOutputPath(libName.c_str());
698 command2 += libName + " \\\n";
699 command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
700 cmStringStream linklibs;
701 this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
702 command2 += linklibs.str();
703 std::string customCommands = this->CreateTargetRules(t, name);
704 const char* cc = 0;
705 if(customCommands.size() > 0)
707 cc = customCommands.c_str();
709 this->OutputMakeRule(fout, "rules for a shared module library",
710 target.c_str(),
711 depend.c_str(),
712 command.c_str(),
713 command2.c_str(),
714 cc);
718 void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
719 const char* name,
720 const cmTarget &t)
722 std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
723 target = this->ConvertToOutputPath(target.c_str());
724 std::string depend = "$(";
725 depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
726 std::string command;
727 if(t.HasCxx())
729 command = "$(CMAKE_CXX_AR) $(CMAKE_CXX_AR_ARGS) ";
731 else
733 command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
735 command += target;
736 command += " $(";
737 command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
738 std::string command2 = "$(CMAKE_RANLIB) ";
739 command2 += target;
740 std::string comment = "rule to build static library: ";
741 comment += name;
742 std::string customCommands = this->CreateTargetRules(t, name);
743 const char* cc = 0;
744 if(customCommands.size() > 0)
746 cc = customCommands.c_str();
748 this->OutputMakeRule(fout,
749 comment.c_str(),
750 target.c_str(),
751 depend.c_str(),
752 command.c_str(),
753 command2.c_str(),
754 cc);
757 void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
758 const char* name,
759 const cmTarget &t)
761 std::string target = m_ExecutableOutputPath + name;
762 std::string depend = "$(";
763 depend += this->CreateMakeVariable(name, "_SRC_OBJS")
764 + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
765 std::string command;
766 if(t.HasCxx())
768 command =
769 "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
771 else
773 command =
774 "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) ";
776 command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
777 cmStringStream linklibs;
778 this->OutputLinkLibraries(linklibs, 0, t);
779 command += linklibs.str();
780 std::string outputFile = m_ExecutableOutputPath + name;
781 command += " -o " + this->ConvertToOutputPath(outputFile.c_str());
782 std::string comment = "rule to build executable: ";
783 comment += name;
785 std::string customCommands = this->CreateTargetRules(t, name);
786 const char* cc = 0;
787 if(customCommands.size() > 0)
789 cc = customCommands.c_str();
791 this->OutputMakeRule(fout,
792 comment.c_str(),
793 target.c_str(),
794 depend.c_str(),
795 command.c_str(),
796 cc);
801 void cmUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
802 const char* name,
803 const cmTarget &t)
805 std::string customCommands = this->CreateTargetRules(t, name);
806 const char* cc = 0;
807 if(customCommands.size() > 0)
809 cc = customCommands.c_str();
811 std::string comment = "Rule to build Utility ";
812 comment += name;
813 std::string depends;
814 const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
815 for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
816 i != ccs.end(); ++i)
818 const std::vector<std::string> & dep = i->GetDepends();
819 for(std::vector<std::string>::const_iterator d = dep.begin();
820 d != dep.end(); ++d)
822 depends += " \\\n";
823 depends += *d;
826 this->OutputMakeRule(fout,
827 comment.c_str(),
828 name,
829 depends.c_str(),
830 cc);
835 void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
837 // for each target
838 const cmTargets &tgts = m_Makefile->GetTargets();
839 for(cmTargets::const_iterator l = tgts.begin();
840 l != tgts.end(); l++)
842 switch(l->second.GetType())
844 case cmTarget::STATIC_LIBRARY:
845 this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
846 break;
847 case cmTarget::SHARED_LIBRARY:
848 this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
849 break;
850 case cmTarget::MODULE_LIBRARY:
851 this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
852 break;
853 case cmTarget::EXECUTABLE:
854 case cmTarget::WIN32_EXECUTABLE:
855 this->OutputExecutableRule(fout, l->first.c_str(), l->second);
856 break;
857 case cmTarget::UTILITY:
858 this->OutputUtilityRule(fout, l->first.c_str(), l->second);
859 break;
860 // This is handled by the OutputCustomRules method
861 case cmTarget::INSTALL_FILES:
862 // This is handled by the OutputInstallRules method
863 case cmTarget::INSTALL_PROGRAMS:
864 // This is handled by the OutputInstallRules method
865 break;
872 // For each target that is an executable or shared library, generate
873 // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
874 void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
876 // Build a set of libraries that will be linked into any target in
877 // this directory.
878 std::set<std::string> used;
880 // for each target
881 const cmTargets &tgts = m_Makefile->GetTargets();
882 for(cmTargets::const_iterator l = tgts.begin();
883 l != tgts.end(); l++)
885 // Each dependency should only be emitted once per target.
886 std::set<std::string> emitted;
887 if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
888 || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
889 || (l->second.GetType() == cmTarget::EXECUTABLE)
890 || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
892 fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
894 // A library should not depend on itself!
895 emitted.insert(l->first);
897 // Now, look at all link libraries specific to this target.
898 const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
899 for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
900 lib != tlibs.end(); ++lib)
902 // Record that this library was used.
903 used.insert(lib->first);
905 // Don't emit the same library twice for this target.
906 if(emitted.insert(lib->first).second)
908 // Output this dependency.
909 this->OutputLibDepend(fout, lib->first.c_str());
913 // Now, look at all utilities specific to this target.
914 const std::set<cmStdString>& tutils = l->second.GetUtilities();
915 for(std::set<cmStdString>::const_iterator util = tutils.begin();
916 util != tutils.end(); ++util)
918 // Record that this utility was used.
919 used.insert(*util);
921 // Don't emit the same utility twice for this target.
922 if(emitted.insert(*util).second)
924 // Output this dependency.
925 this->OutputExeDepend(fout, util->c_str());
929 fout << "\n";
933 fout << "\n";
935 // Loop over the libraries used and make sure there is a rule to
936 // build them in this makefile. If the library is in another
937 // directory, add a rule to jump to that directory and make sure it
938 // exists.
939 for(std::set<std::string>::const_iterator lib = used.begin();
940 lib != used.end(); ++lib)
942 // loop over the list of directories that the libraries might
943 // be in, looking for an ADD_LIBRARY(lib...) line. This would
944 // be stored in the cache
945 std::string libPath = *lib + "_CMAKE_PATH";
946 const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
947 // if cache and not the current directory add a rule, to
948 // jump into the directory and build for the first time
949 if(cacheValue &&
950 (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
952 // add the correct extension
953 std::string ltname = *lib+"_LIBRARY_TYPE";
954 const char* libType
955 = m_Makefile->GetDefinition(ltname.c_str());
956 // if it was a library..
957 if (libType)
959 std::string library = m_LibraryPrefix;
960 library += *lib;
961 std::string libpath = cacheValue;
962 if(libType && std::string(libType) == "SHARED")
964 library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
966 else if(libType && std::string(libType) == "MODULE")
968 library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
970 else if(libType && std::string(libType) == "STATIC")
972 library += m_StaticLibraryExtension;
974 else
976 cmSystemTools::Error("Unknown library type!");
977 return;
979 if(m_LibraryOutputPath.size())
981 libpath = m_LibraryOutputPath;
983 else
985 libpath += "/";
987 libpath += library;
988 // put out a rule to build the library if it does not exist
989 this->OutputBuildTargetInDir(fout,
990 cacheValue,
991 library.c_str(),
992 libpath.c_str(),
993 m_Makefile->
994 GetDefinition("LIBRARY_OUTPUT_PATH")
997 // something other than a library...
998 else
1000 std::string exepath = cacheValue;
1001 if(m_ExecutableOutputPath.size())
1003 exepath = m_ExecutableOutputPath;
1005 else
1007 exepath += "/";
1009 exepath += *lib;
1010 this->OutputBuildTargetInDir(fout,
1011 cacheValue,
1012 lib->c_str(),
1013 exepath.c_str(),
1014 m_Makefile->
1015 GetDefinition("EXECUTABLE_OUTPUT_PATH")
1022 void cmUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
1023 const char* path,
1024 const char* library,
1025 const char* fullpath,
1026 const char* outputPath)
1028 const char* makeTarget = library;
1029 if(outputPath && strcmp( outputPath, "" ) != 0)
1031 makeTarget = fullpath;
1033 fout << this->ConvertToOutputPath(fullpath)
1034 << ":\n\tcd " << this->ConvertToOutputPath(path)
1035 << "; $(MAKE) $(MAKESILENT) cmake.depends"
1036 << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
1037 << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
1038 << "; $(MAKE) $(MAKESILENT) "
1039 << this->ConvertToOutputPath(makeTarget) << "\n\n";
1043 bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
1045 return strcmp(path1, path2) == 0;
1048 void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
1049 const char* name)
1051 std::string libPath = name;
1052 libPath += "_CMAKE_PATH";
1053 const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
1054 if(cacheValue )
1056 // if there is a cache value, then this is a library that cmake
1057 // knows how to build, so we can depend on it
1058 std::string libpath;
1059 if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
1061 // if the library is not in the current directory, then get the full
1062 // path to it
1063 if(m_LibraryOutputPath.size())
1065 libpath = m_LibraryOutputPath;
1066 libpath += m_LibraryPrefix;
1068 else
1070 libpath = cacheValue;
1071 libpath += "/";
1072 libpath += m_LibraryPrefix;
1075 else
1077 // library is in current Makefile so use lib as a prefix
1078 libpath = m_LibraryOutputPath;
1079 libpath += m_LibraryPrefix;
1081 // add the library name
1082 libpath += name;
1083 // add the correct extension
1084 std::string ltname = name;
1085 ltname += "_LIBRARY_TYPE";
1086 const char* libType = m_Makefile->GetDefinition(ltname.c_str());
1087 if(libType && std::string(libType) == "SHARED")
1089 libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
1091 else if (libType && std::string(libType) == "MODULE")
1093 libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
1095 else if (libType && std::string(libType) == "STATIC")
1097 libpath += m_StaticLibraryExtension;
1099 fout << this->ConvertToOutputPath(libpath.c_str()) << " ";
1104 void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
1105 const char* name)
1107 std::string exePath = name;
1108 exePath += "_CMAKE_PATH";
1109 const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
1110 if(cacheValue )
1112 // if there is a cache value, then this is a executable/utility that cmake
1113 // knows how to build, so we can depend on it
1114 std::string exepath;
1115 if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
1117 // if the exe/utility is not in the current directory, then get the full
1118 // path to it
1119 if(m_ExecutableOutputPath.size())
1121 exepath = m_ExecutableOutputPath;
1123 else
1125 exepath = cacheValue;
1126 exepath += "/";
1129 else
1131 // library is in current Makefile
1132 exepath = m_ExecutableOutputPath;
1134 // add the library name
1135 exepath += name;
1136 // add the correct extension
1137 if (m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX"))
1139 std::string replaceVars =
1140 m_Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
1141 if (!strcmp(replaceVars.c_str(),"@CMAKE_EXECUTABLE_SUFFIX@"))
1143 replaceVars = "";
1145 exepath += replaceVars;
1147 fout << this->ConvertToOutputPath(exepath.c_str()) << " ";
1153 // fix up names of directories so they can be used
1154 // as targets in makefiles.
1155 inline std::string FixDirectoryName(const char* dir)
1157 std::string s = dir;
1158 // replace ../ with 3 under bars
1159 size_t pos = s.find("../");
1160 if(pos != std::string::npos)
1162 s.replace(pos, 3, "___");
1164 // replace / directory separators with a single under bar
1165 pos = s.find("/");
1166 while(pos != std::string::npos)
1168 s.replace(pos, 1, "_");
1169 pos = s.find("/");
1171 return s;
1175 void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
1176 const char* dir,
1177 const char* target1,
1178 const char* target2,
1179 bool silent)
1181 std::string directory = this->ConvertToOutputPath(dir);
1182 if(target1)
1184 fout << "\t@if test ! -d " << directory
1185 << "; then $(MAKE) rebuild_cache; fi\n";
1186 if (!silent)
1188 fout << "\techo " << directory << ": building " << target1 << "\n";
1190 fout << "\t@cd " << directory
1191 << "; $(MAKE) " << target1 << "\n";
1193 if(target2)
1195 if (!silent)
1197 fout << "\techo " << directory << ": building " << target2 << "\n";
1199 fout << "\t@cd " << directory
1200 << "; $(MAKE) " << target2 << "\n";
1202 fout << "\n";
1206 void
1207 cmUnixMakefileGenerator::
1208 OutputSubDirectoryVars(std::ostream& fout,
1209 const char* var,
1210 const char* target,
1211 const char* target1,
1212 const char* target2,
1213 const char* depend,
1214 const std::vector<std::string>& SubDirectories,
1215 bool silent)
1217 if(!depend)
1219 depend = "";
1221 if( SubDirectories.size() == 0)
1223 return;
1225 fout << "# Variable for making " << target << " in subdirectories.\n";
1226 fout << var << " = \\\n";
1227 unsigned int i;
1228 for(i =0; i < SubDirectories.size(); i++)
1230 std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
1231 fout << target << "_" << subdir.c_str();
1232 if(i == SubDirectories.size()-1)
1234 fout << " \n\n";
1236 else
1238 fout << " \\\n";
1241 fout << "# Targets for making " << target << " in subdirectories.\n";
1242 std::string last = "";
1243 for(unsigned int i =0; i < SubDirectories.size(); i++)
1245 std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
1246 fout << target << "_" << subdir.c_str() << ": " << depend;
1248 // Make each subdirectory depend on previous one. This forces
1249 // parallel builds (make -j 2) to build in same order as a single
1250 // threaded build to avoid dependency problems.
1251 if(i > 0)
1253 fout << " " << target << "_" << last.c_str();
1256 fout << "\n";
1257 last = subdir;
1258 std::string dir = m_Makefile->GetCurrentOutputDirectory();
1259 dir += "/";
1260 dir += SubDirectories[i];
1261 this->BuildInSubDirectory(fout, dir.c_str(),
1262 target1, target2, silent);
1264 fout << "\n\n";
1268 // output rules for decending into sub directories
1269 void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
1271 // Output Sub directory build rules
1272 const std::vector<std::string>& SubDirectories
1273 = m_Makefile->GetSubDirectories();
1275 if( SubDirectories.size() == 0)
1277 return;
1279 this->OutputSubDirectoryVars(fout,
1280 "SUBDIR_BUILD",
1281 "default_target",
1282 "default_target",
1283 0, "$(TARGETS)",
1284 SubDirectories,
1285 false);
1286 this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
1287 "clean",
1288 0, 0,
1289 SubDirectories);
1290 this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
1291 "depend",
1292 0, 0,
1293 SubDirectories);
1294 this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
1295 "install",
1296 0, 0,
1297 SubDirectories);
1303 // Output the depend information for all the classes
1304 // in the makefile. These would have been generated
1305 // by the class cmMakeDepend GenerateMakefile
1306 bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
1308 bool ret = false;
1309 // Iterate over every target.
1310 std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
1311 for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
1312 target != targets.end(); ++target)
1314 // Iterate over every source for this target.
1315 const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
1316 for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
1317 source != sources.end(); ++source)
1319 if(!(*source)->IsAHeaderFileOnly())
1321 if(!(*source)->GetDepends().empty())
1323 fout << (*source)->GetSourceName() << m_ObjectFileExtension << " :";
1324 // Iterate through all the dependencies for this source.
1325 for(std::vector<std::string>::const_iterator dep =
1326 (*source)->GetDepends().begin();
1327 dep != (*source)->GetDepends().end(); ++dep)
1329 fout << " \\\n"
1330 << this->ConvertToOutputPath(dep->c_str());
1331 ret = true;
1333 fout << "\n\n";
1338 return ret;
1343 // Output the depend information for all the classes
1344 // in the makefile. These would have been generated
1345 // by the class cmMakeDepend GenerateMakefile
1346 void cmUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
1348 std::set<std::string> emittedLowerPath;
1349 std::set<std::string> emitted;
1350 // Iterate over every target.
1351 std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
1352 fout << "# Suppresses display of executed commands\n";
1353 fout << ".SILENT:\n";
1354 fout << "# disable some common implicit rules to speed things up\n";
1355 fout << ".SUFFIXES:\n";
1356 fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
1357 this->OutputMakeVariables(fout);
1358 fout << "default:\n";
1359 fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
1360 << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
1361 fout << "all: ";
1362 for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
1363 target != targets.end(); ++target)
1365 // Iterate over every source for this target.
1366 const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
1367 for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
1368 source != sources.end(); ++source)
1370 if(!(*source)->IsAHeaderFileOnly())
1372 if(!(*source)->GetDepends().empty())
1374 for(std::vector<std::string>::const_iterator dep =
1375 (*source)->GetDepends().begin();
1376 dep != (*source)->GetDepends().end(); ++dep)
1378 std::string dependfile =
1379 this->ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
1380 // use the lower path function to create uniqe names
1381 std::string lowerpath = this->LowerCasePath(dependfile.c_str());
1382 if(emittedLowerPath.insert(lowerpath).second)
1384 emitted.insert(dependfile);
1385 fout << " \\\n" << dependfile ;
1392 fout << "\n\n# if any of these files changes run make dependlocal\n";
1393 fout << "cmake.depends: ";
1394 std::set<std::string>::iterator i;
1395 for(i = emitted.begin(); i != emitted.end(); ++i)
1397 fout << " \\\n" << *i;
1399 fout << "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
1400 fout << "\n\n";
1401 fout << "# if a .h file is removed then run make dependlocal\n\n";
1402 for(std::set<std::string>::iterator i = emitted.begin();
1403 i != emitted.end(); ++i)
1405 fout << *i << ":\n"
1406 << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
1410 // Output each custom rule in the following format:
1411 // output: source depends...
1412 // (tab) command...
1413 void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
1415 // We may be modifying the source groups temporarily, so make a copy.
1416 std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
1418 const cmTargets &tgts = m_Makefile->GetTargets();
1419 for(cmTargets::const_iterator tgt = tgts.begin();
1420 tgt != tgts.end(); ++tgt)
1422 // add any custom rules to the source groups
1423 for (std::vector<cmCustomCommand>::const_iterator cr =
1424 tgt->second.GetCustomCommands().begin();
1425 cr != tgt->second.GetCustomCommands().end(); ++cr)
1427 // if the source for the custom command is the same name
1428 // as the target, then to not create a rule in the makefile for
1429 // the custom command, as the command will be fired when the other target
1430 // is built.
1431 if ( cr->GetSourceName().compare(tgt->first) !=0)
1433 cmSourceGroup& sourceGroup =
1434 m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
1435 sourceGroups);
1436 cmCustomCommand cc(*cr);
1437 cc.ExpandVariables(*m_Makefile);
1438 sourceGroup.AddCustomCommand(cc);
1443 // Loop through every source group.
1444 for(std::vector<cmSourceGroup>::const_iterator sg =
1445 sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
1447 const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
1448 if(buildRules.empty())
1449 { continue; }
1451 std::string name = sg->GetName();
1452 if(name != "")
1454 fout << "# Start of source group \"" << name.c_str() << "\"\n";
1457 // Loop through each source in the source group.
1458 for(cmSourceGroup::BuildRules::const_iterator cc =
1459 buildRules.begin(); cc != buildRules.end(); ++ cc)
1461 std::string source = cc->first;
1462 const cmSourceGroup::Commands& commands = cc->second.m_Commands;
1463 // Loop through every command generating code from the current source.
1464 for(cmSourceGroup::Commands::const_iterator c = commands.begin();
1465 c != commands.end(); ++c)
1467 // escape spaces and convert to native slashes path for
1468 // the command
1469 std::string command =
1470 this->ConvertToOutputPath(c->second.m_Command.c_str());
1471 command += " ";
1472 // now add the arguments
1473 command += c->second.m_Arguments;
1474 const cmSourceGroup::CommandFiles& commandFiles = c->second;
1475 // if the command has no outputs, then it is a utility command
1476 // with no outputs
1477 if(commandFiles.m_Outputs.size() == 0)
1479 std::string depends;
1480 // collect out all the dependencies for this rule.
1481 for(std::set<std::string>::const_iterator d =
1482 commandFiles.m_Depends.begin();
1483 d != commandFiles.m_Depends.end(); ++d)
1485 std::string dep = this->ConvertToOutputPath(d->c_str());
1486 depends += " ";
1487 depends += dep;
1489 // output rule
1490 this->OutputMakeRule(fout,
1491 "Custom command",
1492 source.c_str(),
1493 depends.c_str(),
1494 command.c_str());
1496 // Write a rule for every output generated by this command.
1497 for(std::set<std::string>::const_iterator output =
1498 commandFiles.m_Outputs.begin();
1499 output != commandFiles.m_Outputs.end(); ++output)
1501 std::string src = this->ConvertToOutputPath(source.c_str());
1502 std::string depends;
1503 depends += src;
1504 // Collect out all the dependencies for this rule.
1505 for(std::set<std::string>::const_iterator d =
1506 commandFiles.m_Depends.begin();
1507 d != commandFiles.m_Depends.end(); ++d)
1509 std::string dep = this->ConvertToOutputPath(d->c_str());
1510 depends += " ";
1511 depends += dep;
1513 // output rule
1514 this->OutputMakeRule(fout,
1515 "Custom command",
1516 output->c_str(),
1517 depends.c_str(),
1518 command.c_str());
1522 if(name != "")
1524 fout << "# End of source group \"" << name.c_str() << "\"\n\n";
1530 void cmUnixMakefileGenerator::GenerateCacheOnly()
1532 cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
1533 std::string dest = m_Makefile->GetStartOutputDirectory();
1534 dest += "/Makefile";
1535 std::cout << "cmake: creating : " << dest.c_str() << std::endl;
1536 this->OutputMakefile(dest.c_str());
1537 return;
1540 void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
1542 std::vector<cmMakefile*> makefiles;
1543 m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
1544 for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
1545 i != makefiles.end(); ++i)
1547 cmMakefile* mf = *i;
1548 if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
1550 mf->AddDefinition("RUN_CONFIGURE", true);
1552 cmUnixMakefileGenerator* gen =
1553 static_cast<cmUnixMakefileGenerator*>(mf->GetMakefileGenerator());
1554 gen->SetCacheOnlyOn();
1555 gen->SetRecurseOff();
1556 mf->GenerateMakefile();
1558 // CLEAN up the makefiles created
1559 for(unsigned int i =0; i < makefiles.size(); ++i)
1561 delete makefiles[i];
1565 void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
1567 const char* variables =
1568 "# the standard shell for make\n"
1569 "SHELL = /bin/sh\n"
1570 "\n"
1571 "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
1572 "CMAKE_AR = @CMAKE_AR@\n"
1573 "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
1574 "CMAKE_CXX_AR = @CMAKE_CXX_AR@\n"
1575 "CMAKE_CXX_AR_ARGS = @CMAKE_CXX_AR_ARGS@\n"
1576 "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
1577 "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
1578 "CMAKE_C_LINK_SHARED = @CMAKE_C_LINK_SHARED@\n"
1579 "CMAKE_CXX_LINK_SHARED = @CMAKE_CXX_LINK_SHARED@\n"
1580 "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
1582 "CMAKE_CXX_SHLIB_CFLAGS = @CMAKE_CXX_SHLIB_CFLAGS@\n"
1583 "CMAKE_CXX_SHLIB_BUILD_FLAGS = @CMAKE_CXX_SHLIB_BUILD_FLAGS@\n"
1584 "CMAKE_CXX_SHLIB_LINK_FLAGS = @CMAKE_CXX_SHLIB_LINK_FLAGS@\n"
1585 "CMAKE_CXX_MODULE_BUILD_FLAGS = @CMAKE_CXX_MODULE_BUILD_FLAGS@\n"
1586 "CMAKE_CXX_MODULE_LINK_FLAGS = @CMAKE_CXX_MODULE_LINK_FLAGS@\n"
1587 "CMAKE_CXX_SHLIB_RUNTIME_FLAG = @CMAKE_CXX_SHLIB_RUNTIME_FLAG@\n"
1588 "CMAKE_CXX_SHLIB_RUNTIME_SEP = @CMAKE_CXX_SHLIB_RUNTIME_SEP@\n"
1590 "\n"
1591 "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
1592 "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
1593 "\n"
1594 "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
1595 "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
1596 "CMAKE_C_SHLIB_LINK_FLAGS = @CMAKE_C_SHLIB_LINK_FLAGS@\n"
1597 "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
1598 "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
1599 "CMAKE_C_SHLIB_RUNTIME_FLAG = @CMAKE_C_SHLIB_RUNTIME_FLAG@\n"
1600 "CMAKE_SHLIB_RUNTIME_FLAG = @CMAKE_SHLIB_RUNTIME_FLAG@\n"
1601 "CMAKE_SHLIB_RUNTIME_SEP = @CMAKE_SHLIB_RUNTIME_SEP@\n"
1602 "DL_LIBS = @CMAKE_DL_LIBS@\n"
1603 "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
1604 "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
1605 "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
1606 "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
1607 "RM = rm -f\n"
1608 "\n";
1609 std::string replaceVars = variables;
1610 m_Makefile->ExpandVariablesInString(replaceVars);
1611 fout << replaceVars.c_str();
1612 fout << "CMAKE_COMMAND = "
1613 << this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_COMMAND"))
1614 << "\n";
1615 if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
1617 fout << "CMAKE_EDIT_COMMAND = "
1618 << this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
1619 << "\n";
1622 fout << "CMAKE_CURRENT_SOURCE = " <<
1623 this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << "\n";
1624 fout << "CMAKE_CURRENT_BINARY = " <<
1625 this->ConvertToOutputPath(m_Makefile->GetStartOutputDirectory()) << "\n";
1626 fout << "CMAKE_SOURCE_DIR = " <<
1627 this->ConvertToOutputPath(m_Makefile->GetHomeDirectory()) << "\n";
1628 fout << "CMAKE_BINARY_DIR = " <<
1629 this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()) << "\n";
1630 // Output Include paths
1631 fout << "INCLUDE_FLAGS = ";
1632 std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
1633 std::vector<std::string>::iterator i;
1634 fout << "-I" <<
1635 this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
1636 for(i = includes.begin(); i != includes.end(); ++i)
1638 std::string include = *i;
1639 // Don't output a -I for the standard include path "/usr/include".
1640 // This can cause problems with certain standard library
1641 // implementations because the wrong headers may be found first.
1642 if(include != "/usr/include")
1644 fout << "-I" << this->ConvertToOutputPath(i->c_str()) << " ";
1647 fout << m_Makefile->GetDefineFlags();
1648 fout << "\n\n";
1652 void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
1654 const char* root
1655 = m_Makefile->GetDefinition("CMAKE_ROOT");
1656 fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
1657 fout << "INSTALL_PROGRAM = $(INSTALL)\n";
1658 fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
1660 const cmTargets &tgts = m_Makefile->GetTargets();
1661 fout << "install: $(SUBDIR_INSTALL)\n";
1662 fout << "\t@echo \"Installing ...\"\n";
1664 const char* prefix
1665 = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
1666 if (!prefix)
1668 prefix = "/usr/local";
1671 for(cmTargets::const_iterator l = tgts.begin();
1672 l != tgts.end(); l++)
1674 if (l->second.GetInstallPath() != "")
1676 // first make the directories for each target
1677 fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
1678 " ] ; then \\\n";
1679 fout << "\t echo \"Making directory $(DESTDIR)" << prefix
1680 << l->second.GetInstallPath() << " \"; \\\n";
1681 fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
1682 << "; \\\n";
1683 fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
1684 << "; \\\n";
1685 fout << "\t else true; \\\n";
1686 fout << "\t fi\n";
1687 // now install the target
1688 switch (l->second.GetType())
1690 case cmTarget::STATIC_LIBRARY:
1691 fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
1692 << l->first;
1693 fout << ".a";
1694 fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
1695 break;
1696 case cmTarget::SHARED_LIBRARY:
1697 fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
1698 << l->first;
1699 fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
1700 fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
1701 break;
1702 case cmTarget::MODULE_LIBRARY:
1703 fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
1704 << l->first;
1705 fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
1706 fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
1707 break;
1708 case cmTarget::WIN32_EXECUTABLE:
1709 case cmTarget::EXECUTABLE:
1710 fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
1711 << l->first
1712 << cmSystemTools::GetExecutableExtension()
1713 << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
1714 break;
1715 case cmTarget::INSTALL_FILES:
1717 const std::vector<std::string> &sf = l->second.GetSourceLists();
1718 std::vector<std::string>::const_iterator i;
1719 for (i = sf.begin(); i != sf.end(); ++i)
1721 fout << "\t@ echo \"Installing " << *i << " \"\n";
1722 fout << "\t@if [ -f " << *i << " ] ; then \\\n";
1723 // avoid using install-sh to install install-sh
1724 // does not work on windows....
1725 if(*i == "install-sh")
1727 fout << "\t cp ";
1729 else
1731 fout << "\t $(INSTALL_DATA) ";
1733 fout << *i
1734 << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
1735 fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
1736 // avoid using install-sh to install install-sh
1737 // does not work on windows....
1738 if(*i == "install-sh")
1740 fout << "\t cp ";
1742 else
1744 fout << "\t $(INSTALL_DATA) ";
1746 fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
1747 << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
1748 fout << "\telse \\\n";
1749 fout << "\t echo \" ERROR!!! Unable to find: " << *i
1750 << " \"; \\\n";
1751 fout << "\t fi\n";
1754 break;
1755 case cmTarget::INSTALL_PROGRAMS:
1757 const std::vector<std::string> &sf = l->second.GetSourceLists();
1758 std::vector<std::string>::const_iterator i;
1759 for (i = sf.begin(); i != sf.end(); ++i)
1761 fout << "\t@ echo \"Installing " << *i << " \"\n";
1762 fout << "\t@if [ -f " << *i << " ] ; then \\\n";
1763 // avoid using install-sh to install install-sh
1764 // does not work on windows....
1765 if(*i == "install-sh")
1767 fout << "\t cp ";
1769 else
1771 fout << "\t $(INSTALL_PROGRAM) ";
1773 fout << *i
1774 << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
1775 fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
1776 // avoid using install-sh to install install-sh
1777 // does not work on windows....
1778 if(*i == "install-sh")
1780 fout << "\t cp ";
1782 else
1784 fout << "\t $(INSTALL_PROGRAM) ";
1786 fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
1787 << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
1788 fout << "\telse \\\n";
1789 fout << "\t echo \" ERROR!!! Unable to find: " << *i
1790 << " \"; \\\n";
1791 fout << "\t fi\n";
1794 break;
1795 case cmTarget::UTILITY:
1796 default:
1797 break;
1803 void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
1805 this->OutputMakeRule(fout,
1806 "Default build rule",
1807 "all",
1808 "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
1810 this->OutputMakeRule(fout,
1811 "remove generated files",
1812 "clean",
1813 "$(SUBDIR_CLEAN)",
1814 "-@ $(RM) $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
1815 " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
1817 // collect up all the sources
1818 std::string allsources;
1819 std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
1820 for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
1821 target != targets.end(); ++target)
1823 // Iterate over every source for this target.
1824 const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
1825 for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
1826 source != sources.end(); ++source)
1828 if(!(*source)->IsAHeaderFileOnly())
1830 allsources += " \\\n";
1831 allsources += this->ConvertToOutputPath((*source)->GetFullPath().c_str());
1836 this->OutputMakeRule(fout,
1837 "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
1838 "cmake.depends",
1839 "$(CMAKE_MAKEFILE_SOURCES)",
1840 "$(CMAKE_COMMAND) "
1841 "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
1842 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
1844 this->OutputMakeRule(fout,
1845 "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
1846 "cmake.check_depends",
1847 allsources.c_str(),
1848 "$(CMAKE_COMMAND) "
1849 "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
1850 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
1853 this->OutputMakeRule(fout,
1854 "Rule to force the build of cmake.depends",
1855 "depend",
1856 "$(SUBDIR_DEPEND)",
1857 "$(CMAKE_COMMAND) "
1858 "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
1859 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
1860 this->OutputMakeRule(fout,
1861 "Rule to force the build of cmake.depends "
1862 "in the current directory only.",
1863 "dependlocal",
1865 "$(CMAKE_COMMAND) "
1866 "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
1867 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
1869 this->OutputMakeRule(fout,
1870 "Rebuild CMakeCache.txt file",
1871 "rebuild_cache",
1872 "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
1873 "$(CMAKE_COMMAND) "
1874 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
1875 // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
1876 // called edit_cache
1877 if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
1879 this->OutputMakeRule(fout,
1880 "Edit the CMakeCache.txt file with ccmake or CMakeSetup",
1881 "edit_cache",
1883 "$(CMAKE_EDIT_COMMAND) "
1884 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
1887 this->OutputMakeRule(fout,
1888 "Create CMakeCache.txt file",
1889 "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
1891 "$(CMAKE_COMMAND) "
1892 "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
1894 this->OutputMakeRule(fout,
1895 "Rule to keep make from removing Makefiles "
1896 "if control-C is hit during a run of cmake.",
1897 ".PRECIOUS",
1898 "Makefile cmake.depends",
1901 this->OutputSourceObjectBuildRules(fout);
1902 // find ctest
1903 std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
1904 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
1905 ctest += "/";
1906 ctest += "ctest";
1907 ctest += cmSystemTools::GetExecutableExtension();
1908 if(!cmSystemTools::FileExists(ctest.c_str()))
1910 ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
1911 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
1912 ctest += "/Debug/";
1913 ctest += "ctest";
1914 ctest += cmSystemTools::GetExecutableExtension();
1916 if(!cmSystemTools::FileExists(ctest.c_str()))
1918 ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
1919 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
1920 ctest += "/Release/";
1921 ctest += "ctest";
1922 ctest += cmSystemTools::GetExecutableExtension();
1924 if (cmSystemTools::FileExists(ctest.c_str()))
1926 this->OutputMakeRule(fout,
1927 "run any tests",
1928 "test",
1930 this->ConvertToOutputPath(ctest.c_str()).c_str());
1934 void
1935 cmUnixMakefileGenerator::
1936 OutputBuildObjectFromSource(std::ostream& fout,
1937 const char* shortName,
1938 const cmSourceFile& source,
1939 const char* extraCompileFlags,
1940 bool shared)
1942 // Header files shouldn't have build rules.
1943 if(source.IsAHeaderFileOnly())
1945 return;
1948 std::string comment = "Build ";
1949 std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
1950 objectFile = this->ConvertToOutputPath(objectFile.c_str());
1951 comment += objectFile + " From ";
1952 comment += source.GetFullPath();
1953 std::string compileCommand;
1954 std::string ext = source.GetSourceExtension();
1955 if(ext == "c" )
1957 compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
1958 compileCommand += extraCompileFlags;
1959 if(shared)
1961 compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
1963 compileCommand += "$(INCLUDE_FLAGS) -c ";
1964 compileCommand +=
1965 this->ConvertToOutputPath(source.GetFullPath().c_str());
1966 compileCommand += " -o ";
1967 compileCommand += objectFile;
1969 else
1971 compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
1972 compileCommand += extraCompileFlags;
1973 if(shared)
1975 compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
1977 compileCommand += "$(INCLUDE_FLAGS) -c ";
1978 compileCommand +=
1979 this->ConvertToOutputPath(source.GetFullPath().c_str());
1980 compileCommand += " -o ";
1981 compileCommand += objectFile;
1983 this->OutputMakeRule(fout,
1984 comment.c_str(),
1985 objectFile.c_str(),
1986 this->ConvertToOutputPath(source.GetFullPath().
1987 c_str()).c_str(),
1988 compileCommand.c_str());
1993 void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
1995 fout << "# Rules to build " << m_ObjectFileExtension
1996 << " files from their sources:\n";
1998 std::set<std::string> rules;
2000 // Iterate over every target.
2001 std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
2002 for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
2003 target != targets.end(); ++target)
2005 bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
2006 (target->second.GetType() == cmTarget::MODULE_LIBRARY));
2007 std::string exportsDef = "";
2008 if(shared)
2010 exportsDef = "-D"+target->first+"_EXPORTS ";
2012 // Iterate over every source for this target.
2013 const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
2014 for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
2015 source != sources.end(); ++source)
2017 if(!(*source)->IsAHeaderFileOnly())
2019 std::string shortName;
2020 std::string sourceName;
2021 // If the full path to the source file includes this
2022 // directory, we want to use the relative path for the
2023 // filename of the object file. Otherwise, we will use just
2024 // the filename portion.
2025 if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
2026 || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
2027 GetCurrentOutputDirectory()) == 0))
2029 sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
2030 shortName = (*source)->GetSourceName();
2032 // The path may be relative. See if a directory needs to be
2033 // created for the output file. This is a ugly, and perhaps
2034 // should be moved elsewhere.
2035 std::string relPath =
2036 cmSystemTools::GetFilenamePath((*source)->GetSourceName());
2037 if(relPath != "")
2039 std::string outPath = m_Makefile->GetCurrentOutputDirectory();
2040 outPath += "/"+relPath;
2041 cmSystemTools::MakeDirectory(outPath.c_str());
2044 else
2046 sourceName = (*source)->GetFullPath();
2047 shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
2049 std::string shortNameWithExt = shortName +
2050 (*source)->GetSourceExtension();
2051 // Only output a rule for each .o once.
2052 if(rules.find(shortNameWithExt) == rules.end())
2054 if((*source)->GetCompileFlags())
2056 exportsDef += (*source)->GetCompileFlags();
2057 exportsDef += " ";
2059 this->OutputBuildObjectFromSource(fout,
2060 shortName.c_str(),
2061 *(*source),
2062 exportsDef.c_str(),
2063 shared);
2064 rules.insert(shortNameWithExt);
2071 void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
2072 const char* comment,
2073 const char* target,
2074 const char* depends,
2075 const char* command,
2076 const char* command2,
2077 const char* command3,
2078 const char* command4)
2080 if(!target)
2082 cmSystemTools::Error("no target for OutputMakeRule");
2083 return;
2086 std::string replace;
2087 if(comment)
2089 replace = comment;
2090 m_Makefile->ExpandVariablesInString(replace);
2091 fout << "#---------------------------------------------------------\n";
2092 fout << "# " << replace;
2093 fout << "\n#\n";
2095 fout << "\n";
2097 replace = target;
2098 m_Makefile->ExpandVariablesInString(replace);
2099 fout << this->ConvertToOutputPath(replace.c_str()) << ": ";
2101 if(depends)
2103 replace = depends;
2104 m_Makefile->ExpandVariablesInString(replace);
2105 fout << replace.c_str();
2107 fout << "\n";
2109 const char* commands[] = { command, command2, command3, command4 };
2111 for (unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i)
2113 if(commands[i])
2115 replace = commands[i];
2116 m_Makefile->ExpandVariablesInString(replace);
2117 if(replace[0] != '-' && replace.find("echo") != 0
2118 && replace.find("$(MAKE)") != 0)
2120 std::string echostring = replace;
2121 // for unix we want to quote the output of echo
2122 // for nmake and borland, the echo should not be quoted
2123 if(strcmp(this->GetName(), "Unix Makefiles") == 0)
2125 cmSystemTools::ReplaceString(echostring, "\\\n", " ");
2126 cmSystemTools::ReplaceString(echostring, " \t", " ");
2127 cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
2128 fout << "\techo \"" << echostring.c_str() << "\"\n";
2130 else
2132 cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
2133 fout << "\techo " << echostring.c_str() << "\n";
2136 fout << "\t" << replace.c_str() << "\n";
2139 fout << "\n";
2142 void cmUnixMakefileGenerator::SetLocal (bool local)
2144 if (local)
2146 m_CacheOnly = false;
2147 m_Recurse = false;
2149 else
2151 m_CacheOnly = true;
2152 m_Recurse = true;
2156 void cmUnixMakefileGenerator::EnableLanguage(const char* lang)
2158 if (m_CacheOnly)
2160 // see man putenv for explaination of this stupid code....
2161 static char envCXX[5000];
2162 static char envCC[5000];
2163 if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
2165 std::string env = "CXX=${CMAKE_CXX_COMPILER}";
2166 m_Makefile->ExpandVariablesInString(env);
2167 strncpy(envCXX, env.c_str(), 4999);
2168 envCXX[4999] = 0;
2169 putenv(envCXX);
2171 if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
2173 std::string env = "CC=${CMAKE_C_COMPILER}";
2174 m_Makefile->ExpandVariablesInString(env);
2175 strncpy(envCC, env.c_str(), 4999);
2176 envCC[4999] = 0;
2177 putenv(envCC);
2179 std::string output;
2180 std::string root
2181 = this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_ROOT"));
2182 // if no lang specified use CXX
2183 if(!lang )
2185 lang = "CXX";
2187 // if CXX or C, then enable C
2188 if((!this->GetLanguageEnabled(lang) && lang[0] == 'C'))
2190 std::string cmd = root;
2191 cmd += "/Templates/cconfigure";
2192 cmSystemTools::RunCommand(cmd.c_str(), output,
2193 this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
2194 std::string fpath = m_Makefile->GetHomeOutputDirectory();
2195 fpath += "/CCMakeSystemConfig.cmake";
2196 m_Makefile->ReadListFile(NULL,fpath.c_str());
2197 this->SetLanguageEnabled("C");
2199 // if CXX
2200 if(!this->GetLanguageEnabled(lang) || strcmp(lang, "CXX") == 0)
2202 std::string cmd = root;
2203 cmd += "/Templates/cxxconfigure";
2204 cmSystemTools::RunCommand(cmd.c_str(), output,
2205 this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
2206 std::string fpath = m_Makefile->GetHomeOutputDirectory();
2207 fpath += "/CXXCMakeSystemConfig.cmake";
2208 m_Makefile->ReadListFile(NULL,fpath.c_str());
2209 this->SetLanguageEnabled("CXX");
2211 m_Makefile->AddDefinition("RUN_CONFIGURE", true);