Skip to content

Commit 86e5b00

Browse files
committed
Add builtin functions.
1 parent dd03969 commit 86e5b00

File tree

14 files changed

+379
-97
lines changed

14 files changed

+379
-97
lines changed

cppan.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ projects:
164164
public:
165165
- builder
166166
- pvt.cppan.demo.boost.assign: 1
167+
- pvt.cppan.demo.microsoft.gsl: "*"
167168
private:
168169
- name: tools.self_builder
169170
ref: self_builder

include/sw/builder/command.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ template struct SW_BUILDER_API CommandData<builder::Command>;
174174
void execute() override {}
175175
};*/
176176

177-
struct SW_BUILDER_API ExecuteCommand : builder::Command
177+
struct SW_BUILDER_API _ExecuteCommand : builder::Command
178178
{
179179
using F = std::function<void(void)>;
180180

@@ -183,17 +183,17 @@ struct SW_BUILDER_API ExecuteCommand : builder::Command
183183
F f;
184184
bool always = false;
185185

186-
ExecuteCommand(const char *file, int line) : file(file), line(line) {}
187-
ExecuteCommand(FileStorage &fs, const char *file, int line) : Command(fs), file(file), line(line) {}
186+
_ExecuteCommand(const char *file, int line) : file(file), line(line) {}
187+
_ExecuteCommand(FileStorage &fs, const char *file, int line) : Command(fs), file(file), line(line) {}
188188

189189
//template <class F2>
190190
//ExecuteCommand(const char *file, int line, F2 &&f) : ExecuteCommand(file, line), f(f) {}
191191

192192
template <class F2>
193-
ExecuteCommand(F2 &&f) : f(f) {}
193+
_ExecuteCommand(F2 &&f) : f(f) {}
194194

195195
template <class F2>
196-
ExecuteCommand(FileStorage &fs, F2 &&f) : Command(fs), f(f) {}
196+
_ExecuteCommand(FileStorage &fs, F2 &&f) : Command(fs), f(f) {}
197197

198198
/*template <class F2>
199199
ExecuteCommand(bool always, F2 &&f) : f(f), always(true) {}
@@ -211,7 +211,7 @@ struct SW_BUILDER_API ExecuteCommand : builder::Command
211211
addOutput(p);
212212
}*/
213213

214-
virtual ~ExecuteCommand();
214+
virtual ~_ExecuteCommand();
215215

216216
bool isOutdated() const override;
217217
void execute() override;
@@ -221,7 +221,7 @@ struct SW_BUILDER_API ExecuteCommand : builder::Command
221221
};
222222

223223
#define SW_INTERNAL_INIT_COMMAND(name, target) \
224-
name->fs = (target).getSolution()->fs; \
224+
name->fs = (target).getSolution()->fs; \
225225
name->addPathDirectory((target).getOutputDir() / (target).getConfig())
226226

227227
#define SW_MAKE_CUSTOM_COMMAND(type, name, target, ...) \
@@ -237,9 +237,9 @@ struct SW_BUILDER_API ExecuteCommand : builder::Command
237237
#define SW_MAKE_COMMAND_AND_ADD(name, target) \
238238
SW_MAKE_CUSTOM_COMMAND_AND_ADD(Command, name, target)
239239

240-
#define SW_MAKE_EXECUTE_COMMAND(name, target) \
240+
#define _SW_MAKE_EXECUTE_COMMAND(name, target) \
241241
SW_MAKE_CUSTOM_COMMAND(ExecuteCommand, name, target, __FILE__, __LINE__)
242-
#define SW_MAKE_EXECUTE_COMMAND_AND_ADD(name, target) \
242+
#define _SW_MAKE_EXECUTE_COMMAND_AND_ADD(name, target) \
243243
SW_MAKE_CUSTOM_COMMAND_AND_ADD(ExecuteCommand, name, target, __FILE__, __LINE__)
244244
}
245245

include/sw/driver/cpp/cmake.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2018-2018 Egor Pugin <[email protected]>
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
#pragma once
8+
9+
#include "sw.h"
10+
11+
#define SOLUTION_VAR _solution
12+
13+
#define PRIVATE .Private +=
14+
#define PUBLIC .Public +=
15+
#define INTERFACE .Interface +=
16+
17+
#define add_library(t) auto &t = SOLUTION_VAR.addLibrary(#t)
18+
#define add_executable(t) auto &t = SOLUTION_VAR.addExecutable(#t)
19+
20+
#define target_sources(t, ...) t __VA_ARGS__
21+
#define target_include_directories(t, ...) t __VA_ARGS__
22+
#define target_link_libraries(t, ...) t __VA_ARGS__

src/builder/command.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,24 @@ path Command::getProgram() const
224224

225225
void Command::addInput(const path &p)
226226
{
227+
if (p.empty())
228+
return;
227229
inputs.insert(p);
228230
}
229231

230232
void Command::addIntermediate(const path &p)
231233
{
234+
if (p.empty())
235+
return;
232236
intermediate.insert(p);
233237
auto &r = File(p, *fs).getFileRecord();
234238
r.setGenerator(shared_from_this());
235239
}
236240

237241
void Command::addOutput(const path &p)
238242
{
243+
if (p.empty())
244+
return;
239245
outputs.insert(p);
240246
auto &r = File(p, *fs).getFileRecord();
241247
r.setGenerator(shared_from_this());
@@ -702,11 +708,11 @@ void Command::save(BinaryContext &bctx)
702708

703709
}
704710

705-
ExecuteCommand::~ExecuteCommand()
711+
_ExecuteCommand::~_ExecuteCommand()
706712
{
707713
}
708714

709-
size_t ExecuteCommand::getHash() const
715+
size_t _ExecuteCommand::getHash() const
710716
{
711717
if (hash != 0)
712718
return hash;
@@ -721,15 +727,15 @@ size_t ExecuteCommand::getHash() const
721727
return hash = h;
722728
}
723729

724-
void ExecuteCommand::prepare()
730+
void _ExecuteCommand::prepare()
725731
{
726732
if (prepared)
727733
return;
728734
addInputOutputDeps();
729735
prepared = true;
730736
}
731737

732-
bool ExecuteCommand::isOutdated() const
738+
bool _ExecuteCommand::isOutdated() const
733739
{
734740
if (std::none_of(inputs.begin(), inputs.end(),
735741
[this](auto &d) { return File(d, *fs).isChanged(); }) &&
@@ -739,7 +745,7 @@ bool ExecuteCommand::isOutdated() const
739745
return true;
740746
}
741747

742-
void ExecuteCommand::execute()
748+
void _ExecuteCommand::execute()
743749
{
744750
if (always)
745751
{

src/client/client.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <sw/builder/driver.h>
1818
#include <sw/driver/cpp/driver.h>
1919
#include <sw/driver/cppan/driver.h>
20+
#include <jumppad.h>
2021

2122
//#include <args.hxx>
2223
#include <boost/algorithm/string.hpp>
@@ -64,7 +65,7 @@ namespace sw::driver::cppan { SW_REGISTER_PACKAGE_DRIVER(CppanDriver); }
6465
int main(int argc, char **argv);
6566
#pragma pop_macro("main")
6667

67-
int sw_main();
68+
int sw_main(const Strings &args);
6869
void stop();
6970
void setup_log(const std::string &log_level);
7071

@@ -94,9 +95,9 @@ static cl::opt<bool> verbose("verbose", cl::desc("Verbose output"));
9495
static cl::opt<bool> trace("trace", cl::desc("Trace output"));
9596
static cl::opt<int> jobs("j", cl::desc("Number of jobs"), cl::init(-1));
9697

97-
int setup_main()
98+
int setup_main(const Strings &args)
9899
{
99-
// some init stuff
100+
// some initial stuff
100101

101102
if (!working_directory.empty())
102103
fs::current_path(working_directory);
@@ -118,7 +119,7 @@ int setup_main()
118119
getServiceDatabase();
119120

120121
// actual execution
121-
return sw_main();
122+
return sw_main(args);
122123
}
123124

124125
int parse_main(int argc, char **argv)
@@ -135,7 +136,7 @@ int parse_main(int argc, char **argv)
135136
}
136137

137138
const std::vector<std::string> args0(argv + 1, argv + argc);
138-
std::vector<std::string> args;
139+
Strings args;
139140
args.push_back(argv[0]);
140141
for (auto &a : args0)
141142
{
@@ -144,10 +145,15 @@ int parse_main(int argc, char **argv)
144145
args.insert(args.end(), t.begin(), t.end());
145146
}
146147

148+
if (args.size() > 1 && args[1] == "internal-call-builtin-function")
149+
{
150+
return jumppad_call(args);
151+
}
152+
147153
//
148154
cl::ParseCommandLineOptions(args, overview);
149155

150-
return setup_main();
156+
return setup_main(args);
151157
}
152158

153159
int main(int argc, char **argv)
@@ -224,7 +230,9 @@ static cl::list<String> override_package("override-remote-package", cl::value_de
224230
static cl::opt<String> delete_overridden_package("delete-overridden-remote-package", cl::value_desc("package"), cl::desc("Delete overridden package from index"));
225231
static cl::opt<path> delete_overridden_package_dir("delete-overridden-remote-package-dir", cl::value_desc("sdir"), cl::desc("Delete overridden dir packages"));
226232

227-
int sw_main()
233+
//static cl::list<String> builtin_function("internal-call-builtin-function", cl::desc("Call built-in function"), cl::Hidden);
234+
235+
int sw_main(const Strings &args)
228236
{
229237
if (!override_package.empty())
230238
{

src/driver/cpp/command.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
#include "target.h"
88
#include "command.h"
99

10+
#include "jumppad.h"
11+
#include "solution.h"
12+
1013
#include <boost/algorithm/string.hpp>
14+
#include <boost/dll.hpp>
1115

1216
#include <primitives/log.h>
1317
DECLARE_STATIC_LOGGER(logger, "command");
@@ -173,6 +177,8 @@ CommandBuilder &operator<<(CommandBuilder &cb, const NativeExecutedTarget &t)
173177
auto nt = (NativeExecutedTarget*)&t;
174178
cb.targets.push_back(nt);
175179
nt->Storage.push_back(cb.c);
180+
if (!cb.c->fs)
181+
cb.c->fs = nt->getSolution()->fs;
176182
return cb;
177183
}
178184

@@ -339,4 +345,42 @@ CommandBuilder &operator<<(CommandBuilder &cb, const Command::LazyCallback &t)
339345
return cb;
340346
}
341347

348+
ExecuteBuiltinCommand::ExecuteBuiltinCommand()
349+
{
350+
program = boost::dll::program_location().string();
351+
}
352+
353+
ExecuteBuiltinCommand::ExecuteBuiltinCommand(const String &cmd_name)
354+
: ExecuteBuiltinCommand()
355+
{
356+
args.push_back(cmd_name);
357+
}
358+
359+
void ExecuteBuiltinCommand::push_back(const Files &files)
360+
{
361+
args.push_back(std::to_string(files.size()));
362+
for (auto &o : files)
363+
args.push_back(o.u8string());
364+
}
365+
366+
void ExecuteBuiltinCommand::execute()
367+
{
368+
if (always)
369+
{
370+
jumppad_call(args);
371+
return;
372+
}
373+
374+
if (!isOutdated())
375+
return;
376+
377+
printLog();
378+
379+
jumppad_call(args);
380+
381+
// force outputs update
382+
for (auto &o : outputs)
383+
File(o, *fs).getFileRecord().load();
384+
}
385+
342386
}

src/driver/cpp/command.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ struct SW_DRIVER_CPP_API Command : ::sw::builder::Command
290290
std::vector<LazyAction> actions;
291291
};
292292

293+
struct SW_DRIVER_CPP_API ExecuteBuiltinCommand : builder::Command
294+
{
295+
using F = std::function<void(void)>;
296+
297+
ExecuteBuiltinCommand();
298+
ExecuteBuiltinCommand(const String &cmd_name);
299+
virtual ~ExecuteBuiltinCommand() = default;
300+
301+
void execute() override;
302+
path getProgram() const override { return "ExecuteBuiltinCommand"; };
303+
304+
//template <class T>
305+
//auto push_back(T &&v) { args.push_back(v); }
306+
307+
void push_back(const Files &files);
308+
};
309+
310+
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND(var_name, target, func_name) \
311+
SW_MAKE_CUSTOM_COMMAND(::sw::driver::cpp::ExecuteBuiltinCommand, var_name, target, func_name)
312+
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND_AND_ADD(var_name, target, func_name) \
313+
SW_MAKE_CUSTOM_COMMAND_AND_ADD(::sw::driver::cpp::ExecuteBuiltinCommand, var_name, target, func_name)
314+
293315
struct VSCommand : Command
294316
{
295317
//File file;

0 commit comments

Comments
 (0)