1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmExportLibraryDependencies.h"
13 #include "cmGlobalGenerator.h"
14 #include "cmLocalGenerator.h"
15 #include "cmGeneratedFileStream.h"
17 #include "cmVersion.h"
19 #include <cmsys/auto_ptr.hxx>
21 bool cmExportLibraryDependenciesCommand
22 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
26 this->SetError("called with incorrect number of arguments");
30 // store the arguments for the final pass
31 this->Filename = args[0];
35 if(args[1] == "APPEND")
44 void cmExportLibraryDependenciesCommand::FinalPass()
46 // export_library_dependencies() shouldn't modify anything
47 // ensure this by calling a const method
48 this->ConstFinalPass();
51 void cmExportLibraryDependenciesCommand::ConstFinalPass() const
53 // Use copy-if-different if not appending.
54 cmsys::auto_ptr<std::ofstream> foutPtr;
57 cmsys::auto_ptr<std::ofstream> ap(
58 new std::ofstream(this->Filename.c_str(), std::ios::app));
63 cmsys::auto_ptr<cmGeneratedFileStream> ap(
64 new cmGeneratedFileStream(this->Filename.c_str(), true));
65 ap->SetCopyIfDifferent(true);
68 std::ostream& fout = *foutPtr.get();
72 cmSystemTools::Error("Error Writing ", this->Filename.c_str());
73 cmSystemTools::ReportLastSystemError("");
77 // Collect dependency information about all library targets built in
79 cmake* cm = this->Makefile->GetCMakeInstance();
80 cmGlobalGenerator* global = cm->GetGlobalGenerator();
81 const std::vector<cmLocalGenerator *>& locals = global->GetLocalGenerators();
82 std::map<cmStdString, cmStdString> libDepsOld;
83 std::map<cmStdString, cmStdString> libDepsNew;
84 std::map<cmStdString, cmStdString> libTypes;
85 for(std::vector<cmLocalGenerator *>::const_iterator i = locals.begin();
86 i != locals.end(); ++i)
88 const cmLocalGenerator* gen = *i;
89 const cmTargets &tgts = gen->GetMakefile()->GetTargets();
90 for(cmTargets::const_iterator l = tgts.begin();
93 // Get the current target.
94 cmTarget const& target = l->second;
96 // Skip non-library targets.
97 if(target.GetType() < cmTarget::STATIC_LIBRARY
98 || target.GetType() > cmTarget::MODULE_LIBRARY)
103 // Construct the dependency variable name.
104 std::string targetEntry = target.GetName();
105 targetEntry += "_LIB_DEPENDS";
107 // Construct the dependency variable value. It is safe to use
108 // the target GetLinkLibraries method here because this code is
109 // called at the end of configure but before generate so library
110 // dependencies have yet to be analyzed. Therefore the value
111 // will be the direct link dependencies.
112 std::string valueOld;
113 std::string valueNew;
114 cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
115 for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
116 li != libs.end(); ++li)
118 std::string ltVar = li->first;
119 ltVar += "_LINK_TYPE";
123 case cmTarget::GENERAL:
124 valueNew += "general;";
127 case cmTarget::DEBUG:
128 valueNew += "debug;";
131 case cmTarget::OPTIMIZED:
132 valueNew += "optimized;";
133 ltValue = "optimized";
136 std::string lib = li->first;
137 if(cmTarget* libtgt = global->FindTarget(0, lib.c_str()))
139 // Handle simple output name changes. This command is
140 // deprecated so we do not support full target name
141 // translation (which requires per-configuration info).
142 if(const char* outname = libtgt->GetProperty("OUTPUT_NAME"))
152 std::string& ltEntry = libTypes[ltVar];
157 else if(ltEntry != ltValue)
162 libDepsNew[targetEntry] = valueNew;
163 libDepsOld[targetEntry] = valueOld;
167 // Generate dependency information for both old and new style CMake
169 const char* vertest =
170 "\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
171 fout << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
172 fout << "IF(" << vertest << ")\n";
173 fout << " # Information for CMake 2.6 and above.\n";
174 for(std::map<cmStdString, cmStdString>::const_iterator
175 i = libDepsNew.begin();
176 i != libDepsNew.end(); ++i)
178 if(!i->second.empty())
180 fout << " SET(\"" << i->first << "\" \"" << i->second << "\")\n";
183 fout << "ELSE(" << vertest << ")\n";
184 fout << " # Information for CMake 2.4 and lower.\n";
185 for(std::map<cmStdString, cmStdString>::const_iterator
186 i = libDepsOld.begin();
187 i != libDepsOld.end(); ++i)
189 if(!i->second.empty())
191 fout << " SET(\"" << i->first << "\" \"" << i->second << "\")\n";
194 for(std::map<cmStdString, cmStdString>::const_iterator i = libTypes.begin();
195 i != libTypes.end(); ++i)
197 if(i->second != "general")
199 fout << " SET(\"" << i->first << "\" \"" << i->second << "\")\n";
202 fout << "ENDIF(" << vertest << ")\n";