forked from purpleKarrot/boost-depend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depend.cpp
211 lines (173 loc) · 4.49 KB
/
depend.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (C) 2013 Daniel Pfeifer <[email protected]>
*
* Distributed under the Boost Software License, Version 1.0.
* See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt
*/
#include <string>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/regex.hpp>
#include <boost/unordered_map.hpp>
namespace fs = boost::filesystem;
using header_map = boost::unordered_map<fs::path, fs::path>;
std::string alias(fs::path const& path)
{
std::string result = path.string();
boost::replace_first(result, "libs/", "boost::");
boost::replace_all(result, "/", "::");
return result;
}
std::string target(fs::path const& path)
{
std::string result = path.string();
boost::replace_first(result, "libs/", "boost_");
boost::replace_first(result, "tools/", "");
boost::replace_all(result, "/", "_");
return result;
}
fs::path remove_from_start(fs::path const& inPath, fs::path const& remPath)
{
fs::path result;
auto itIn = inPath.begin();
auto itRem = remPath.begin();
while ((itIn != inPath.end()) && (itRem != remPath.end()) && (*itIn == *itRem))
{
++itIn;
++itRem;
}
for (; itIn != inPath.end(); ++itIn)
{
result /= *itIn;
}
return result;
}
std::string load_file(fs::path const& filename)
{
fs::ifstream file(filename);
std::ostringstream content;
content << file.rdbuf();
return content.str();
}
std::vector<fs::path> load_projects()
{
std::vector<fs::path> result;
auto const text = load_file(".gitmodules");
boost::regex const submodule_path("path\\s*=\\s*([^\\s]+)");
boost::sregex_iterator m1(text.begin(), text.end(), submodule_path), m2;
std::for_each(m1, m2,
[&result](boost::match_results<std::string::const_iterator> const& what)
{
result.emplace_back(what[1]);
});
std::sort(std::begin(result), std::end(result));
return result;
}
std::vector<fs::path> parse_includes(fs::path const& filename)
{
std::vector<fs::path> result;
auto const text = load_file(filename);
boost::regex expr("\n#include\\s+[\"<](boost/[^\">]*)[\">]");
boost::sregex_iterator m1(text.begin(), text.end(), expr), m2;
std::for_each(m1, m2,
[&result](boost::match_results<std::string::const_iterator> const& what)
{
result.emplace_back(what[1]);
});
return result;
}
header_map load_map(std::vector<fs::path> const& projects)
{
header_map result;
for (auto& proj : projects)
{
auto const include_dir = proj / "include";
if (!fs::is_directory(include_dir))
{
continue;
}
fs::recursive_directory_iterator m1(include_dir), m2;
std::for_each(m1, m2,
[&](fs::directory_entry const& entry)
{
if (fs::is_directory(entry))
{
return;
}
auto const header = remove_from_start(entry.path(), include_dir);
result[header] = proj;
});
}
return result;
}
void analyze(fs::path const& proj, header_map const& map)
{
auto const include_dir = proj / "include";
// if (!fs::is_directory(include_dir))
// {
// return;
// }
std::set<fs::path> depends_public;
std::set<fs::path> depends_private;
fs::recursive_directory_iterator m1(proj), m2;
std::for_each(m1, m2,
[&](fs::directory_entry const& entry)
{
if (fs::is_directory(entry))
{
return;
}
std::set<fs::path>& depends
= boost::starts_with(entry.path(), include_dir)
? depends_public : depends_private;
for (auto& file : parse_includes(entry.path()))
{
try
{
depends.insert(map.at(file));
}
catch (std::out_of_range&)
{
}
}
});
depends_public.erase(proj);
depends_private.erase(proj);
if (!depends_public.empty())
{
std::cout << "target_link_libraries(" << target(proj) << " PUBLIC\n";
for (auto const& d : depends_public)
{
std::cout << " " << alias(d) << std::endl;
}
std::cout << " )\n" << std::endl;
}
std::set<fs::path> depends_priv;
std::set_difference(depends_private.begin(), depends_private.end(),
depends_public.begin(), depends_public.end(),
std::inserter(depends_priv, depends_priv.begin()));
if (!depends_priv.empty())
{
std::cout << "target_link_libraries(" << target(proj) << " PRIVATE\n";
for (auto const& d : depends_priv)
{
std::cout << " " << alias(d) << std::endl;
}
std::cout << " )\n" << std::endl;
}
}
int main(int argc, char* argv[])
{
auto const projects = load_projects();
auto const map = load_map(projects);
for (auto& proj : projects)
{
analyze(proj, map);
}
return 0;
}