Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmExportLibraryDependencies.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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"
16 #include "cmake.h"
17 #include "cmVersion.h"
18
19 #include <cmsys/auto_ptr.hxx>
20
21 bool cmExportLibraryDependenciesCommand
22 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
23 {
24   if(args.size() < 1 )
25     {
26     this->SetError("called with incorrect number of arguments");
27     return false;
28     }
29
30   // store the arguments for the final pass
31   this->Filename = args[0];
32   this->Append = false;
33   if(args.size() > 1)
34     {
35     if(args[1] == "APPEND")
36       {
37       this->Append = true;
38       }
39     }
40   return true;
41 }
42
43
44 void cmExportLibraryDependenciesCommand::FinalPass()
45 {
46   // export_library_dependencies() shouldn't modify anything
47   // ensure this by calling a const method
48   this->ConstFinalPass();
49 }
50
51 void cmExportLibraryDependenciesCommand::ConstFinalPass() const
52 {
53   // Use copy-if-different if not appending.
54   cmsys::auto_ptr<std::ofstream> foutPtr;
55   if(this->Append)
56     {
57     cmsys::auto_ptr<std::ofstream> ap(
58       new std::ofstream(this->Filename.c_str(), std::ios::app));
59     foutPtr = ap;
60     }
61   else
62     {
63     cmsys::auto_ptr<cmGeneratedFileStream> ap(
64       new cmGeneratedFileStream(this->Filename.c_str(), true));
65     ap->SetCopyIfDifferent(true);
66     foutPtr = ap;
67     }
68   std::ostream& fout = *foutPtr.get();
69
70   if (!fout)
71     {
72     cmSystemTools::Error("Error Writing ", this->Filename.c_str());
73     cmSystemTools::ReportLastSystemError("");
74     return;
75     }
76
77   // Collect dependency information about all library targets built in
78   // the project.
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)
87     {
88     const cmLocalGenerator* gen = *i;
89     const cmTargets &tgts = gen->GetMakefile()->GetTargets();
90     for(cmTargets::const_iterator l = tgts.begin();
91         l != tgts.end(); ++l)
92       {
93       // Get the current target.
94       cmTarget const& target = l->second;
95
96       // Skip non-library targets.
97       if(target.GetType() < cmTarget::STATIC_LIBRARY
98          || target.GetType() > cmTarget::MODULE_LIBRARY)
99         {
100         continue;
101         }
102
103       // Construct the dependency variable name.
104       std::string targetEntry = target.GetName();
105       targetEntry += "_LIB_DEPENDS";
106
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)
117         {
118         std::string ltVar = li->first;
119         ltVar += "_LINK_TYPE";
120         std::string ltValue;
121         switch(li->second)
122           {
123           case cmTarget::GENERAL:
124             valueNew += "general;";
125             ltValue = "general";
126             break;
127           case cmTarget::DEBUG:
128             valueNew += "debug;";
129             ltValue = "debug";
130             break;
131           case cmTarget::OPTIMIZED:
132             valueNew += "optimized;";
133             ltValue = "optimized";
134             break;
135           }
136         std::string lib = li->first;
137         if(cmTarget* libtgt = global->FindTarget(0, lib.c_str()))
138           {
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"))
143             {
144             lib = outname;
145             }
146           }
147         valueOld += lib;
148         valueOld += ";";
149         valueNew += lib;
150         valueNew += ";";
151
152         std::string& ltEntry = libTypes[ltVar];
153         if(ltEntry.empty())
154           {
155           ltEntry = ltValue;
156           }
157         else if(ltEntry != ltValue)
158           {
159           ltEntry = "general";
160           }
161         }
162       libDepsNew[targetEntry] = valueNew;
163       libDepsOld[targetEntry] = valueOld;
164       }
165     }
166
167   // Generate dependency information for both old and new style CMake
168   // versions.
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)
177     {
178     if(!i->second.empty())
179       {
180       fout << "  SET(\"" << i->first << "\" \"" << i->second << "\")\n";
181       }
182     }
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)
188     {
189     if(!i->second.empty())
190       {
191       fout << "  SET(\"" << i->first << "\" \"" << i->second << "\")\n";
192       }
193     }
194   for(std::map<cmStdString, cmStdString>::const_iterator i = libTypes.begin();
195       i != libTypes.end(); ++i)
196     {
197     if(i->second != "general")
198       {
199       fout << "  SET(\"" << i->first << "\" \"" << i->second << "\")\n";
200       }
201     }
202   fout << "ENDIF(" << vertest << ")\n";
203   return;
204 }