blob: b4b2962b1869913cda382e43358e671321bd8115 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmExportLibraryDependenciesCommand.h"
#include "cmsys/FStream.hxx"
#include <map>
#include <memory> // IWYU pragma: keep
#include <utility>
#include "cmAlgorithms.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmTargetLinkLibraryType.h"
#include "cmake.h"
class cmExecutionStatus;
bool cmExportLibraryDependenciesCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
}
// store the arguments for the final pass
this->Filename = args[0];
this->Append = false;
if (args.size() > 1) {
if (args[1] == "APPEND") {
this->Append = true;
}
}
return true;
}
void cmExportLibraryDependenciesCommand::FinalPass()
{
// export_library_dependencies() shouldn't modify anything
// ensure this by calling a const method
this->ConstFinalPass();
}
void cmExportLibraryDependenciesCommand::ConstFinalPass() const
{
// Use copy-if-different if not appending.
std::unique_ptr<cmsys::ofstream> foutPtr;
if (this->Append) {
foutPtr =
cm::make_unique<cmsys::ofstream>(this->Filename.c_str(), std::ios::app);
} else {
std::unique_ptr<cmGeneratedFileStream> ap(
new cmGeneratedFileStream(this->Filename, true));
ap->SetCopyIfDifferent(true);
foutPtr = std::move(ap);
}
std::ostream& fout = *foutPtr;
if (!fout) {
cmSystemTools::Error("Error Writing ", this->Filename.c_str());
cmSystemTools::ReportLastSystemError("");
return;
}
// Collect dependency information about all library targets built in
// the project.
cmake* cm = this->Makefile->GetCMakeInstance();
cmGlobalGenerator* global = cm->GetGlobalGenerator();
const std::vector<cmMakefile*>& locals = global->GetMakefiles();
std::map<std::string, std::string> libDepsOld;
std::map<std::string, std::string> libDepsNew;
std::map<std::string, std::string> libTypes;
for (cmMakefile* local : locals) {
const cmTargets& tgts = local->GetTargets();
for (auto const& tgt : tgts) {
// Get the current target.
cmTarget const& target = tgt.second;
// Skip non-library targets.
if (target.GetType() < cmStateEnums::STATIC_LIBRARY ||
target.GetType() > cmStateEnums::MODULE_LIBRARY) {
continue;
}
// Construct the dependency variable name.
std::string targetEntry = target.GetName();
targetEntry += "_LIB_DEPENDS";
// Construct the dependency variable value with the direct link
// dependencies.
std::string valueOld;
std::string valueNew;
cmTarget::LinkLibraryVectorType const& libs =
target.GetOriginalLinkLibraries();
for (cmTarget::LibraryID const& li : libs) {
std::string ltVar = li.first;
ltVar += "_LINK_TYPE";
std::string ltValue;
switch (li.second) {
case GENERAL_LibraryType:
valueNew += "general;";
ltValue = "general";
break;
case DEBUG_LibraryType:
valueNew += "debug;";
ltValue = "debug";
break;
case OPTIMIZED_LibraryType:
valueNew += "optimized;";
ltValue = "optimized";
break;
}
std::string lib = li.first;
if (cmTarget* libtgt = global->FindTarget(lib)) {
// Handle simple output name changes. This command is
// deprecated so we do not support full target name
// translation (which requires per-configuration info).
if (const char* outname = libtgt->GetProperty("OUTPUT_NAME")) {
lib = outname;
}
}
valueOld += lib;
valueOld += ";";
valueNew += lib;
valueNew += ";";
std::string& ltEntry = libTypes[ltVar];
if (ltEntry.empty()) {
ltEntry = ltValue;
} else if (ltEntry != ltValue) {
ltEntry = "general";
}
}
libDepsNew[targetEntry] = valueNew;
libDepsOld[targetEntry] = valueOld;
}
}
// Generate dependency information for both old and new style CMake
// versions.
const char* vertest =
"\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
fout << "# Generated by CMake\n\n";
fout << "if(" << vertest << ")\n";
fout << " # Information for CMake 2.6 and above.\n";
for (auto const& i : libDepsNew) {
if (!i.second.empty()) {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
}
fout << "else()\n";
fout << " # Information for CMake 2.4 and lower.\n";
for (auto const& i : libDepsOld) {
if (!i.second.empty()) {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
}
for (auto const& i : libTypes) {
if (i.second != "general") {
fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
}
}
fout << "endif()\n";
}
|