CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
cross_compile.cpp
Go to the documentation of this file.
1
23// C++ libraries
24#include "../../headers/map.hpp"
25#include "string"
26// Project includes
33/* Config toolchain */
34namespace CBuild {
36 protected:
39
40 public:
42 this->hasher = NULL;
43 type_set = false;
44 vmajor_set = false;
45 }
46 void call(std::vector<std::string>* args [[maybe_unused]], bool force = false,
47 bool debug = false, bool dummy = false) override {
48 CBuild::print("You cannot call \"call\" on config toolchain!", CBuild::RED);
49 exit(1);
50 }
51 void debug(std::vector<std::string>* args, std::vector<std::string>* pargs) override {
52 CBuild::print("You cannot call \"debug\" on config toolchain!", CBuild::RED);
53 exit(1);
54 }
55 void clear() override {
56 CBuild::print("You cannot call \"clear\" on config toolchain!", CBuild::RED);
57 exit(1);
58 }
59 void init() override {
60 CBuild::print("You cannot call \"init\" on config toolchain!", CBuild::RED);
61 exit(1);
62 }
63 void run(std::vector<std::string>* args) override {
64 CBuild::print("You cannot call \"run\" on config toolchain!", CBuild::RED);
65 exit(1);
66 }
67 void pre_build() override {
68 CBuild::print("You cannot call \"pre_build\" on config toolchain!", CBuild::RED);
69 exit(1);
70 }
71 void build() override {
72 CBuild::print("You cannot call \"build\" on config toolchain!", CBuild::RED);
73 exit(1);
74 }
75 void post_build() override {
76 CBuild::print("You cannot call \"post_build\" on config toolchain!", CBuild::RED);
77 exit(1);
78 }
79 void pre_link() override {
80 CBuild::print("You cannot call \"pre_link\" on config toolchain!", CBuild::RED);
81 exit(1);
82 }
83 void link_pack() override {
84 CBuild::print("You cannot call \"link_pack\" on config toolchain!", CBuild::RED);
85 exit(1);
86 }
87 void link() override {
88 CBuild::print("You cannot call \"link\" on config toolchain!", CBuild::RED);
89 exit(1);
90 }
91 void post_link() override {
92 CBuild::print("You cannot call \"post_link\" on config toolchain!", CBuild::RED);
93 exit(1);
94 }
95 void stdargs() override {
96 CBuild::print("You cannot call \"stdargs\" on config toolchain!", CBuild::RED);
97 exit(1);
98 }
99 void compile(std::string cmd) override {
100 CBuild::print("You cannot call \"compile\" on config toolchain!", CBuild::RED);
101 exit(1);
102 }
103 void load_project_deps(std::string curr_path) override {
104 CBuild::print("You cannot call \"load_project_deps\" on config toolchain!", CBuild::RED);
105 exit(1);
106 }
107 std::array<std::string, 3> get_cmds() override {
108 CBuild::print("You cannot call \"get_cmds\" on config toolchain!", CBuild::RED);
109 exit(1);
110 }
111 void set_id(std::string id) override {
112 CBuild::print("You cannot call \"set_id\" on config toolchain! Use constructor of "
113 "cross-compilation wrapper for this!",
115 exit(1);
116 }
117 void set_name(std::string name) override {
118 CBuild::print("You cannot call \"set_name\" on config toolchain! Use constructor of "
119 "cross-compilation wrapper for this!",
121 exit(1);
122 }
123 void set_version_major(int version) override {
124 this->vmajor_set = true;
125 this->version_major = version;
126 }
127 void set_type(CBuild::build_type type) override {
128 this->type_set = true;
129 this->build_type = type;
130 }
132 for (auto carg : this->compiler_args) {
133 tool->add_compile_arg(carg);
134 }
135 for (auto larg : this->link_args) {
136 tool->add_link_arg(larg);
137 }
138 for (auto pdep : this->project_deps) {
139 tool->depends_on_project(pdep.path, pdep.name, pdep.id, pdep.headers_path);
140 }
141 for (auto cdep : this->depends) {
142 tool->depends_on(cdep);
143 }
144 for (auto tdep : this->required) {
145 tool->add_requirment(tdep.key, tdep.data);
146 }
147 for (auto pkgcdep : this->pkg_config_include_entries) {
148 tool->add_pkgconfig_entry(pkgcdep);
149 }
150 for (auto edep : this->dependency_list) {
151 tool->add_external_dependency(edep);
152 }
153 for (auto comp : this->targets) {
154 if (comp.folder) {
155 tool->add_folder(comp.path);
156 } else {
157 tool->add_file(comp.path);
158 }
159 }
160 if (this->hasher != NULL) {
161 tool->set_hasher(this->hasher);
162 }
163 if (this->type_set) {
164 tool->set_type(this->build_type);
165 }
166 if (this->vmajor_set) {
167 tool->set_version_major(this->version_major);
168 }
169 }
170};
172 private:
174 std::string default_tool;
176
177 public:
179 this->parent = parent;
180 this->default_tool = "";
181 this->default_tool_set = false;
182 }
183 void build() override {}
184 void link() override {}
185 void link_pack() override {}
186 void load_project_deps(std::string curr_path) override {
187 CBuild::print(std::string("Showing toolchain selector:"), CBuild::MAGENTA);
188 auto toollist = this->parent->get_toolchain_list();
189 size_t i = 0;
190 for (; i < toollist.size(); i++) {
191 CBuild::printf(CBuild::WHITE, "\t%lu => %s\n", i, toollist.at(i).c_str());
192 }
193 CBuild::printf(CBuild::MAGENTA, "Enter desired toolchain id [0-%lu]: ", (i - 1));
194 size_t val;
195 scanf("%lu", &val);
196 if (val >= i) {
197 CBuild::print("Error, toolchain id overflow. Exiting...", CBuild::RED);
198 }
199 this->parent->get_arch_tool(toollist.at(val))->load_project_deps(curr_path);
200 }
201 void run(std::vector<std::string>* args) override {
202 CBuild::print(std::string("Showing toolchain selector:"), CBuild::MAGENTA);
203 auto toollist = this->parent->get_toolchain_list();
204 size_t i = 0;
205 for (; i < toollist.size(); i++) {
206 CBuild::printf(CBuild::WHITE, "\t%lu => %s\n", i, toollist.at(i).c_str());
207 }
208 CBuild::printf(CBuild::MAGENTA, "Enter desired toolchain id [0-%lu]: ", (i - 1));
209 size_t val;
210 scanf("%lu", &val);
211 if (val >= i) {
212 CBuild::print("Error, toolchain id overflow. Exiting...", CBuild::RED);
213 }
214 this->parent->get_arch_tool(toollist.at(val))->run(args);
215 }
216 void call(std::vector<std::string>* args, bool force = false, bool debug = false,
217 bool dummy = false) override {
218 std::vector<std::string> new_args;
219 bool select = false;
220 for (auto arg : *args) {
221 if (arg == std::string("select")) {
222 select = true;
223 } else {
224 new_args.push_back(arg);
225 }
226 }
227 if (dummy == false) {
228 if (this->default_tool_set && !select) {
229 CBuild::print(std::string("Redirecting to default toolchain \"") +
230 this->default_tool + std::string("\"."),
232 this->parent->get_arch_tool(this->default_tool)
233 ->call(&new_args, force, debug, false);
234 } else {
235 CBuild::print(std::string("Showing toolchain selector:"), CBuild::MAGENTA);
236 auto toollist = this->parent->get_toolchain_list();
237 size_t i = 0;
238 for (; i < toollist.size(); i++) {
239 CBuild::printf(CBuild::WHITE, "\t%lu => %s\n", i, toollist.at(i).c_str());
240 }
241 CBuild::printf(CBuild::MAGENTA, "Enter desired toolchain id [0-%lu]: ", (i - 1));
242 size_t val;
243 scanf("%lu", &val);
244 if (val >= i) {
245 CBuild::print("Error, toolchain id overflow. Exiting...", CBuild::RED);
246 }
247 this->parent->get_arch_tool(toollist.at(val))->call(&new_args, force, debug, false);
248 }
249 }
250 }
251 void debug(std::vector<std::string>* args, std::vector<std::string>* pargs) override {
252 std::vector<std::string> new_args;
253 bool select = false;
254 for (auto arg : *args) {
255 if (arg == std::string("select")) {
256 select = true;
257 } else {
258 new_args.push_back(arg);
259 }
260 }
261 if (this->default_tool_set && !select) {
262 CBuild::print(std::string("Redirecting to default toolchain \"") + this->default_tool +
263 std::string("\"."),
265 this->parent->get_arch_tool(this->default_tool)->debug(&new_args, pargs);
266 } else {
267 select:
268 CBuild::print(std::string("Showing toolchain selector:"), CBuild::MAGENTA);
269 auto toollist = this->parent->get_toolchain_list();
270 size_t i = 0;
271 for (; i < toollist.size(); i++) {
272 CBuild::printf(CBuild::WHITE, "\t%lu => %s\n", i, toollist.at(i).c_str());
273 }
274 CBuild::printf(CBuild::MAGENTA, "Enter desired toolchain id [0-%lu]: ", (i - 1));
275 size_t val;
276 scanf("%lu", &val);
277 if (val >= i) {
278 CBuild::print("Error, toolchain id overflow. Exiting...", CBuild::RED);
279 }
280 this->parent->get_arch_tool(toollist.at(val))->debug(&new_args, pargs);
281 }
282 }
283 void clear() override {
284 CBuild::print(std::string("Showing toolchain selector:"), CBuild::MAGENTA);
285 auto toollist = this->parent->get_toolchain_list();
286 size_t i = 0;
287 for (; i < toollist.size(); i++) {
288 CBuild::printf(CBuild::WHITE, "\t%lu => %s\n", i, toollist.at(i).c_str());
289 }
290 CBuild::printf(CBuild::MAGENTA, "Enter desired toolchain id [0-%lu]: ", (i - 1));
291 size_t val;
292 scanf("%lu", &val);
293 if (val >= i) {
294 CBuild::print("Error, toolchain id overflow. Exiting...", CBuild::RED);
295 }
296 this->parent->get_arch_tool(toollist.at(val))->clear();
297 }
298 std::array<std::string, 3> get_cmds() override {
299 return std::array<std::string, 3>{"META", "META", "META"};
300 }
301 void set_default_tool(std::string arch) {
302 this->default_tool = arch;
303 this->default_tool_set = true;
304 }
305};
306} // namespace CBuild
307/* cross_compile.hpp */
308CBuild::CrossCompiler::CrossCompiler(std::string id, std::string name) {
309 this->meta_id = id;
310 this->meta_name = name;
312 this->meta_toolchain = new CBuild::MetaToolchain(this);
313}
315 std::vector<std::string> ret;
316 for (auto arch : this->toolchain_list) {
317 ret.push_back(arch.int_id);
318 }
319 return ret;
320}
322 for (auto arch : this->toolchain_list) {
323 if (arch.int_id == id) {
324 return arch.tool;
325 }
326 }
327 return NULL;
328}
330 return this->meta_config;
331}
333 ((CBuild::MetaToolchain*)this->meta_toolchain)->set_default_tool(arch);
334}
336 this->toolchain_list.push_back(
337 (CBuild::CrossCompiler::ToolData){.int_id = id, .tool = tool, .internally_managed = false});
338 tool->set_id(this->meta_id + "-" + id);
339 tool->set_name(this->meta_name);
340}
342 this->toolchain_list.push_back((CBuild::CrossCompiler::ToolData){
343 .int_id = id,
344 .tool = new CBuild::SimpleToolchain(this->meta_id + "-" + id, this->meta_name),
345 .internally_managed = true});
346}
348 for (auto t : this->toolchain_list) {
350 }
351 CBuild::Registry::RegisterTarget(this->meta_toolchain);
352}
354 for (auto [id, tool, flag] : this->toolchain_list) {
355 ((CBuild::ConfigToolchain*)this->meta_config)->clone_config(tool);
356 }
357}
Build toolchain headers.
Improved CBuild hasher.
void post_link() override
After linking.
void set_name(std::string name) override
Change out file name.
void clone_config(CBuild::Toolchain *tool)
void set_type(CBuild::build_type type) override
Set the type of build.
void set_version_major(int version) override
Set major version component, used in -Wl,-soname
void link_pack() override
Linking for static libraries.
void stdargs() override
Add standard compile and link arguments into compilation Need to be publick for some hashers to work.
void compile(std::string cmd) override
Call compiler.
void clear() override
Clear all caches and builded app.
void run(std::vector< std::string > *args) override
Run builded app.
void call(std::vector< std::string > *args, bool force=false, bool debug=false, bool dummy=false) override
Call tollchain to execute.
void set_id(std::string id) override
Change id of this toolchain, if done after registering it can brake things.
void debug(std::vector< std::string > *args, std::vector< std::string > *pargs) override
Build program in debug mode and after run gdb on it.
std::array< std::string, 3 > get_cmds() override
Get all three commands from class - compile, linker and packer.
void pre_link() override
Before linking.
void post_build() override
After build.
void init() override
Initialize folder structure.
void link() override
Linking.
void load_project_deps(std::string curr_path) override
Load all project dependencies.
void build() override
Build.
void pre_build() override
Before build.
std::string meta_name
Output name for this group.
CrossCompiler(std::string id, std::string name)
Create new cross-compilation collection.
CBuild::Toolchain * get_global_config()
Get global configuration (for compatibility reasons returns CBuild::Toolchain* to config)
std::vector< std::string > get_toolchain_list()
Get list of sub-toolchains in this collection (skips config and meta-toolchain)
CBuild::Toolchain * get_arch_tool(std::string id)
Get toolchain for specific arch.
void perform_registry()
Register all internal toolchains.
CBuild::Toolchain * meta_config
Global configuration.
void set_default_tool(std::string arch)
Set default toolchain.
std::string meta_id
Id of this group.
void add_arch_tool(std::string id, CBuild::Toolchain *tool)
Create new architecture dependant tool.
void apply_global_config()
Load global configuration data into local, per-toolchain, configuration.
CBuild::Toolchain * meta_toolchain
Meta-toolchain fot this group.
CBuild::CrossCompiler * parent
void link_pack() override
Linking for static libraries.
void debug(std::vector< std::string > *args, std::vector< std::string > *pargs) override
Build program in debug mode and after run gdb on it.
void run(std::vector< std::string > *args) override
Run builded app.
std::array< std::string, 3 > get_cmds() override
Get all three commands from class - compile, linker and packer.
void build() override
Build.
MetaToolchain(CBuild::CrossCompiler *parent)
void set_default_tool(std::string arch)
void load_project_deps(std::string curr_path) override
Load all project dependencies.
void clear() override
Clear all caches and builded app.
void link() override
Linking.
void call(std::vector< std::string > *args, bool force=false, bool debug=false, bool dummy=false) override
Call tollchain to execute.
Very simple generic toolchain, rely on compiler to support '-c' flat to specify files to compile and ...
Toolchain class.
Definition Build.hpp:65
std::vector< CBuild::Project_dependency > project_deps
Project dependencies.
Definition Build.hpp:114
virtual void clear()
Clear all caches and builded app.
Definition Build.cpp:723
virtual void load_project_deps(std::string curr_path)
Load all project dependencies.
Definition Build.cpp:478
bool force
Force argument (scratch variable)
Definition Build.hpp:153
virtual void set_version_major(int version)
Set major version component, used in -Wl,-soname
Definition Build.cpp:153
virtual void debug(std::vector< std::string > *args, std::vector< std::string > *pargs)
Build program in debug mode and after run gdb on it.
Definition Build.cpp:698
Hash * hasher
Hasher used here.
Definition Build.hpp:138
std::vector< CBuild::Path > targets
Target files or folders.
Definition Build.hpp:86
virtual void run(std::vector< std::string > *args)
Run builded app.
Definition Build.cpp:677
virtual void add_compile_arg(std::string arg)
Directly add compiler argument.
Definition Build.cpp:38
void add_external_dependency(CBuild::Dependency *dep)
Add external dependency to this toolchain.
Definition Build.cpp:172
virtual void add_pkgconfig_entry(std::string id)
Add a lib, data for what need to be searched using pkg-config tool.
Definition Build.cpp:160
virtual void add_requirment(std::string task_, CBuild::stage run_stage=CBuild::PRE)
Add required task, that task will be executed before toolchain, it can have dependecies,...
Definition Build.cpp:104
std::string name
Name of output, if undefined (""), for id is used for binary name.
Definition Build.hpp:70
virtual void set_id(std::string id)
Change id of this toolchain, if done after registering it can brake things.
Definition Build.cpp:169
std::vector< std::string > link_args
Linker args (specified and generated)
Definition Build.hpp:94
virtual void add_folder(std::string path)
Add folder to compile path, scan is not recursive.
Definition Build.cpp:187
virtual void set_name(std::string name)
Change out file name.
Definition Build.cpp:166
virtual void call(std::vector< std::string > *args, bool force=false, bool debug=false, bool dummy=false)
Call tollchain to execute.
Definition Build.cpp:572
virtual void set_hasher(Hash *hasher)
Set used hasher for this target.
Definition Build.cpp:163
std::vector< std::string > compiler_args
Compiler args (specified and generated)
Definition Build.hpp:90
virtual void add_file(std::string path)
Add file for compilation.
Definition Build.cpp:176
int version_major
Major version, can be necesary for shared libraries.
Definition Build.hpp:122
bool dummy
We do dummy compilation.
Definition Build.hpp:118
lib::map< std::string, CBuild::stage > required
Save task id with run stage sprecified for this task.
Definition Build.hpp:78
virtual void add_link_arg(std::string arg)
Add linker arguments.
Definition Build.cpp:44
virtual void set_type(CBuild::build_type type)
Set the type of build.
Definition Build.cpp:133
std::vector< std::string > pkg_config_include_entries
Libs that need to be searched in pkg-config.
Definition Build.hpp:134
std::vector< std::string > depends
All other targets, that is needed for this target.
Definition Build.hpp:82
virtual void depends_on_project(std::string path, std::string name, std::string id, std::string headers_path)
Add another CBuild project as dependency.
Definition Build.cpp:140
std::vector< std::string > * args
Argument pointer (scratch variable)
Definition Build.hpp:149
std::vector< CBuild::Dependency * > dependency_list
Dependencies.
Definition Build.hpp:142
virtual void depends_on(std::string target)
This target depends on other target.
Definition Build.cpp:136
CrossCompilation front-end for CBuild.
Custom implementation of map datatype.
void RegisterTarget(CBuild::Toolchain *target)
Register new target for build.
Definition register.cpp:226
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
void print(std::string msg, color fg=CBuild::WHITE)
Print colored text to STDOUT.
Definition print.cpp:70
build_type
Definition Build.hpp:55
int version[]
Definition CBuild.cpp:556
@ RED
Definition print.hpp:34
@ WHITE
Definition print.hpp:40
@ MAGENTA
Definition print.hpp:38
void exit(int code)
void printf(color fg, const char *fmt,...) __attribute__((format(printf
Some poor printf implementation.
Definition print.cpp:80
Custom print that support color codes.
Register any things.
Simple retargetable toolchain.
Internal toolchain data.
Command for compile_commands.json.