-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cc
310 lines (215 loc) · 11.5 KB
/
build.cc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <algorithm>
#include <filesystem>
#include <string_view>
#include <system_error>
#include <vector>
#include <config.h>
#include <journal.h>
#include <yaml-cpp/yaml.h>
#include <fs_utils.h>
#include <string_utils.h>
#include <sys_utils.h>
#include "common.h"
#include "context.h"
#include "shell.h"
//#define BOOST_THREAD_PROVIDES_FUTURE
//#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
//#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
//#include <boost/thread/future.hpp>
namespace fs = std::filesystem;
namespace fs_utils = filesystem_utils;
namespace app {
namespace bs {
auto parse_conf( bs::context &ctx, std::string_view conf_path ) -> bool;
} // namespace bs
namespace commands {
static auto compile_target( bs::context &ctx, const std::string_view target_path, const bs::target &target ) {
using namespace std;
namespace su = string_utils;
namespace au = algorithm_utils;
fs::current_path( ctx.build_path );
vector<string> compiled_files;
size_t file_num = 0;
for ( const auto &f : target.sources ) {
const auto compiled_file = fs::relative( fs::path{f}, fs::path{target_path} ).replace_extension( ".o" );
const auto work_path = ctx.build_path / fs::path( compiled_file ).remove_filename( );
auto work_dir = work_path.string( );
work_dir.pop_back( );
fs::create_directories( work_dir );
fs::current_path( work_dir );
vector all_command_options{ctx.cxx_compiller};
if ( target.type == bs::target_build_type::BINARY_APPLICATION ) {
}
if ( target.type == bs::target_build_type::STATIC_LIBRARY ) {
all_command_options.push_back( "-fPIC" );
}
if ( target.type == bs::target_build_type::SHARED_LIBRARY ) {
all_command_options.push_back( "-fPIC" );
}
au::join_copy( all_command_options, ctx.cxx_compile_options );
all_command_options.push_back( f );
const auto command = su::join( all_command_options, strings::WHITESPACE );
file_num++;
LOG_MESSAGE( APP_TAG, "[%1/%2] Compile: '%3'", file_num, target.sources.size( ), f );
const auto res = sys::shell::execute( command );
if ( res.empty( ) ) {
}
compiled_files.push_back( compiled_file );
}
return compiled_files;
}
static auto get_library_link_name( std::string_view name ) {
std::string link_name{name};
link_name.replace( 0, 3, "" );
const auto found = link_name.find_last_of( '.' );
if ( found != std::string::npos )
link_name.replace( found, 3, "" );
return "-l" + link_name;
}
static auto link_target( bs::context &ctx, bs::target &target ) -> bool;
static auto link_target( bs::context &ctx, bs::target &target ) -> bool {
using namespace std;
namespace su = string_utils;
namespace au = algorithm_utils;
fs::current_path( ctx.build_path );
target.output_path = ctx.build_path;
if ( !target.sub_targets.empty( ) ) {
for ( auto &t : target.sub_targets ) {
link_target( ctx, t );
}
}
const auto all_compiled = su::join( target.compiled_files, strings::WHITESPACE );
vector<string> all_command_options;
if ( target.type == bs::target_build_type::BINARY_APPLICATION ) {
all_command_options.push_back( ctx.cxx_compiller );
all_command_options.push_back( all_compiled );
vector<string> linker_options;
if ( !target.run_time_path.empty( ) ) {
linker_options.push_back( "-Wl" );
auto rpath = fs::absolute( target.run_time_path );
linker_options.push_back( "-rpath=" + rpath.string( ) );
}
const auto all_linker_options = su::join( linker_options, "," );
all_command_options.push_back( all_linker_options );
vector<string> library_paths;
for ( const auto &st : target.sub_targets ) {
if ( st.type == bs::target_build_type::STATIC_LIBRARY || st.type == bs::target_build_type::SHARED_LIBRARY ) {
const auto link_path = "-L" + st.output_path;
auto it = std::find( library_paths.begin( ), library_paths.end( ), link_path );
if ( it == library_paths.end( ) )
library_paths.push_back( "-L" + st.output_path );
}
}
au::join_move( all_command_options, library_paths );
for ( const auto &st : target.sub_targets ) {
if ( st.type == bs::target_build_type::STATIC_LIBRARY || st.type == bs::target_build_type::SHARED_LIBRARY ) {
all_command_options.push_back( get_library_link_name( st.output ) );
}
}
all_command_options.push_back( "-o" );
all_command_options.push_back( target.output );
}
if ( target.type == bs::target_build_type::STATIC_LIBRARY ) {
all_command_options.push_back( "ar rcs" );
all_command_options.push_back( target.output );
all_command_options.push_back( all_compiled );
}
if ( target.type == bs::target_build_type::SHARED_LIBRARY ) {
all_command_options.push_back( ctx.cxx_compiller );
all_command_options.push_back( "-shared" );
all_command_options.push_back( all_compiled );
all_command_options.push_back( "-o" );
all_command_options.push_back( target.output );
}
au::join_copy( all_command_options, target.link_libraries );
const auto command = su::join( all_command_options, strings::WHITESPACE );
const auto res = sys::shell::execute( command );
if ( res.empty( ) ) {
}
LOG_MESSAGE( APP_TAG, "Build output: '%1'", target.output );
return true;
}
static auto build_target( bs::context &ctx, bs::target &target ) -> bool;
static auto build_target( bs::context &ctx, bs::target &target ) -> bool {
using namespace std;
const auto initial_dir = fs::current_path( );
bool build_result = false;
if ( !target.sub_targets.empty( ) ) {
for ( auto &t : target.sub_targets ) {
build_target( ctx, t );
}
}
if ( !target.sources.empty( ) ) {
const auto compiled_files = compile_target( ctx, initial_dir.string( ), target );
target.compiled_files = compiled_files;
build_result = !compiled_files.empty( );
} else {
build_result = false;
}
fs::current_path( initial_dir );
return build_result;
}
auto build_all( bs::context &ctx, std::string_view arguments ) -> bool {
(void)arguments;
ctx.base_path = fs::current_path( ).string( );
const auto conf_path = ctx.base_path + fs::path::preferred_separator + DEFAULT_BUMP_FILE;
LOG_INFO( APP_TAG, "%1", "Build start..." );
ctx.build_start = bs::clock_type::now( );
if ( bs::parse_conf( ctx, conf_path ) ) {
if ( !ctx.build_targets.empty( ) ) {
std::error_code err;
const auto default_include_path = ctx.base_path + fs::path::preferred_separator + common::DEFAULT_INCLUDE_DIR;
if ( fs::exists( default_include_path, err ) ) {
ctx.cxx_compile_options.push_back( "-I" + default_include_path );
}
const auto default_external_path = ctx.base_path + fs::path::preferred_separator + common::DEFAULT_EXTERNAL_DIR;
if ( fs::exists( default_external_path, err ) ) {
ctx.cxx_compile_options.push_back( "-I" + default_external_path );
}
fs::create_directory( ctx.build_path, err );
if ( !err ) {
if ( ctx.jobs == 0 || ctx.jobs == 1 ) {
for ( auto &t : ctx.build_targets ) {
build_target( ctx, t );
}
for ( auto &t : ctx.build_targets ) {
link_target( ctx, t );
}
} else {
// sys::thread_pool pool{ctx.jobs};
// for ( auto &t : ctx.build_targets ) {
// pool.enqueue( build_target, ctx, t );
// }
// pool.wait_until_complete( );
// for ( auto &t : ctx.build_targets ) {
// pool.enqueue( link_target, ctx, t );
// }
// using namespace stlab;
// std::vector<stlab::future<bool>> build_futures;
// for ( auto &t : ctx.build_targets ) {
// auto f = stlab::async( default_executor, [&]( ) { return build_target( ctx, t
// ); } );
// build_futures.push_back( f );
// }
// auto res = when_all( default_executor, build_futures );
// auto f = when_all( build_futures );
// auto done = f.then( []( decltype( f ) ) { LOG_INFO( APP_TAG, "%s", "Build done" );
// } );
// boost::wait_for_all()
}
ctx.build_end = bs::clock_type::now( );
const std::chrono::duration<double> build_time = ctx.build_end - ctx.build_start;
LOG_INFO( APP_TAG, "Build end(%1)... [%2s]", ctx.profile_name, build_time.count( ) );
return true;
}
LOG_ERROR( APP_TAG, "Could't create build directory %1", err.message( ) );
return false;
}
LOG_ERROR( APP_TAG, "%1", "No targets to build" );
return false;
}
LOG_ERROR( APP_TAG, "Couldn't parse build config '%1'", conf_path );
return false;
}
} // namespace commands
} // namespace app