CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
mingw-gcc.hpp
Go to the documentation of this file.
1
21#ifndef CBUILD_MINGW_GCC_TOOLCHAIN
22#define CBUILD_MINGW_GCC_TOOLCHAIN
23// Project files
24#include "../CBuild_defs.hpp"
25#include "../filesystem++.hpp"
27#include "../print.hpp"
28#include "../register.hpp"
29#include "./Build.hpp"
30// Code
31namespace CBuild {
32template <CBuild::HashImpl hash = CBuild::CBuildHashV2> class MINGW_GCC : public CBuild::Toolchain {
33 protected:
34 std::string wine;
35
36 public:
42 MINGW_GCC(std::string id) {
43 this->id = id;
44 this->name = "";
45 this->linker = "x86_64-w64-mingw32-gcc";
46 this->compiler = "x86_64-w64-mingw32-gcc";
47 this->packer = "x86_64-w64-mingw32-ar cr";
48 this->wine = "wine";
49 this->add_link_arg("-static-libgcc");
50 this->hasher = new hash(this->id);
51 }
58 MINGW_GCC(std::string id, std::string name) {
59 this->id = id;
60 this->name = name;
61 this->linker = "x86_64-w64-mingw32-gcc";
62 this->compiler = "x86_64-w64-mingw32-gcc";
63 this->packer = "x86_64-w64-mingw32-ar cr";
64 this->wine = "wine";
65 this->add_link_arg("-static-libgcc");
66 this->hasher = new hash(this->id);
67 }
68
69 protected:
70 // For docs see mingw-g++.hpp
71 void build() override {
72 std::string args;
73 for (auto elem : this->compiler_args) {
74 args += elem;
75 args += " ";
76 }
77 auto files = this->gen_file_list(this->force);
78 std::vector<std::string> hash_files;
79 if (files.size() > 0) {
80 for (unsigned int i = 0; i < files.size(); i++) {
81 std::string cmd = this->compiler + " -c ";
82 cmd += files.at(i).key;
83 cmd += " ";
84 cmd += args;
85 cmd += " -o ";
86 cmd += files.at(i).data;
87 // CBuild::print(cmd, CBuild::BLUE);
88 this->compile(cmd);
89 }
90 }
91 }
92 void link() override {
93 std::string args;
94 for (auto elem : this->link_args) {
95 args += elem;
96 args += " ";
97 }
98 // Get files
99 this->gen_file_list_for_linking = true;
100 auto files = this->gen_file_list(true);
101 this->gen_file_list_for_linking = false;
102 std::string flist;
103 for (unsigned int i = 0; i < files.size(); i++) {
104 flist += files.at(i).data;
105 flist += " ";
106 }
107 if (files.size() > 0) {
108 std::string cmd = this->linker + " ";
109 cmd += flist;
110 cmd += " ";
111 cmd += args;
112 cmd += " ";
113 cmd += " -o ";
114 cmd += this->gen_out_name();
115 // CBuild::print(cmd, CBuild::BLUE);
116 this->compile(cmd);
117 }
118 }
119 void link_pack() override {
120 std::string args;
121 for (auto elem : this->link_args) {
122 args += elem;
123 args += " ";
124 }
125 this->gen_file_list_for_linking = true;
126 auto files = this->gen_file_list(true);
127 this->gen_file_list_for_linking = false;
128 std::string flist;
129 for (unsigned int i = 0; i < files.size(); i++) {
130 flist += files.at(i).data;
131 flist += " ";
132 }
133 if (files.size() > 0) {
134 std::string cmd = this->packer + " ";
135 cmd += this->gen_out_name();
136 cmd += " ";
137 cmd += flist;
138 cmd += " ";
139 // CBuild::print(cmd, CBuild::BLUE);
140 this->compile(cmd);
141 }
142 }
143 void post_link() override {
144 for (std::string id : this->depends) {
145 auto target = CBuild::Registry::GetToolchain(id, true);
146 if (target != NULL) {
147 auto out_path = target->gen_out_name(".exe", ".dll");
148 unsigned int end_slash = out_path.find_last_of('/');
149 std::string out = CBUILD_BUILD_DIR + "/" + this->id + "/" + CBUILD_BUILD_OUT_DIR +
150 "/" + out_path.substr(end_slash + 1);
151 CBuild::fs::copy(out_path, out);
152 }
153 }
154 }
155
156 public:
157 std::string gen_out_name(std::string executable = ".exe", std::string dyn_lib = ".dll",
158 std::string stat_lib = ".a") override {
159 std::string base = CBUILD_BUILD_DIR + "/" + this->id + "/" + CBUILD_BUILD_OUT_DIR + "/";
160 if (this->name != std::string("")) {
161 if (this->build_type == CBuild::EXECUTABLE) {
162 base += this->name;
163 base += executable;
164 } else {
165 base += "lib";
166 base += this->name;
167 if (this->build_type == CBuild::DYNAMIC_LIBRARY) {
168 base += dyn_lib;
169 } else {
170 base += stat_lib;
171 }
172 }
173 } else {
174 if (this->build_type == CBuild::EXECUTABLE) {
175 base += this->name;
176 base += executable;
177 } else {
178 base += "lib";
179 base += this->id;
180 if (this->build_type == CBuild::DYNAMIC_LIBRARY) {
181 base += dyn_lib;
182 } else {
183 base += stat_lib;
184 }
185 }
186 }
187 return this->cmd_str(base);
188 }
189 // void call(std::vector<std::string> *args, bool force = false,
190 // bool debug = false) override {
191 // CBuild::print("Starting " + this->id + " toolchain in build mode ",
192 // CBuild::color::RED);
193 // this->args = args;
194 // this->force = force;
195 // if (this->build_type == CBuild::DYNAMIC_LIBRARY)
196 // this->link_args.push_back("-shared");
197 // if (debug)
198 // this->compiler_args.push_back("-g");
199 // this->compiler_args.push_back("-fPIC");
200 // this->init();
201 // CBuild::print("Calling tasks marked as PRE ", CBuild::color::GREEN);
202 // for (unsigned int i = 0; i < this->required.size(); i++) {
203 // auto elem = this->required.at(i);
204 // if (elem.data == CBuild::PRE) {
205 // CBuild::Registry::CallTask(elem.key, {});
206 // }
207 // }
208 // CBuild::print("Calling all required toolchains ",
209 // CBuild::color::GREEN); for (std::string id : this->depends) {
210 // auto target = CBuild::Registry::GetToolchain(id);
211 // if (target != NULL) {
212 // target->call(args, force, debug);
213 // auto out_path = target->gen_out_name();
214 // unsigned int end_slash = out_path.find_last_of('/');
215 // unsigned int end_dot = out_path.find_last_of('.');
216 // std::string out =
217 // out_path.substr(end_slash + 4, end_dot - end_slash - 4);
218 // this->add_library_include(out);
219 // this->add_library_dir(".", CBUILD_BUILD_DIR + "/" + target->get_id() +
220 // "/" + CBUILD_BUILD_OUT_DIR + "/");
221 // }
222 // }
223 // if (!force)
224 // CBuild::print("Using precompiled object were possible ",
225 // CBuild::color::MAGENTA);
226 // CBuild::print("Running pre build tasks ", CBuild::GREEN);
227 // this->pre_build();
228 // CBuild::print("Running build tasks ", CBuild::GREEN);
229 // CBuild::print("Now you can see compiler output", CBuild::MAGENTA);
230 // this->build();
231 // CBuild::print("Running post build tasks ", CBuild::GREEN);
232 // this->post_build();
233 // CBuild::print("Running pre link tasks ", CBuild::GREEN);
234 // this->pre_link();
235 // CBuild::print("Running link tasks ", CBuild::GREEN);
236 // CBuild::print("Now you can see linker output", CBuild::MAGENTA);
237 // if (this->build_type == CBuild::STATIC_LIBRARY) {
238 // this->link_pack();
239 // } else {
240 // this->link();
241 // }
242 // CBuild::print("Running post link tasks ", CBuild::GREEN);
243 // this->post_link();
244 // CBuild::print("Calling tasks marked as POST ", CBuild::GREEN);
245 // for (unsigned int i = 0; i < this->required.size(); i++) {
246 // auto elem = this->required.at(i);
247 // if (elem.data == CBuild::POST) {
248 // CBuild::Registry::CallTask(elem.key, {});
249 // }
250 // }
251 // CBuild::print("End of execution of toolchain " + this->id + " ",
252 // CBuild::RED);
253 // }
254 // void run(std::vector<std::string> *args) override {
255 // CBuild::print("Starting \"" + this->name + "\" ", CBuild::RED);
256 // std::string pargs = "";
257 // if (args != NULL) {
258 // for (auto elem : *args) {
259 // pargs += elem;
260 // pargs += " ";
261 // }
262 // }
263 // std::string cmd;
264 // cmd = this->wine + " ";
265 // cmd += this->gen_out_name();
266 // cmd += " ";
267 // cmd += pargs;
268 // CBuild::print("App output (if any):", CBuild::MAGENTA);
269 // CBuild::system(cmd);
270 // CBuild::print("End of app execution", CBuild::RED);
271 // }
272 void debug(std::vector<std::string>* args, std::vector<std::string>* pargs) override {
273 CBuild::print("It is unimplemented for now!", CBuild::RED);
274 // CBuild::print("Starting \"" + this->id + "\" toolchain in
275 // debug mode ", CBuild::RED); this->call(args, true, true);
276 // CBuild::print("Running builded app with gdb ",
277 // CBuild::GREEN); std::string ppargs = ""; if (pargs != NULL)
278 // {
279 // for (auto elem : *pargs)
280 // {
281 // ppargs += elem;
282 // ppargs += " ";
283 // }
284 // }
285 // std::string cmd;
286 // cmd = "x86_64-w64-mingw32-gdb ";
287 // cmd += this->gen_out_name();
288 // cmd += " ";
289 // cmd += ppargs;
290 // // CBuild::print("Now you can see gdb shell ",
291 // CBuild::MAGENTA); CBuild::system(cmd); CBuild::print("End of
292 // app execution", CBuild::RED);
293 }
294};
295} // namespace CBuild
296#endif // CBUILD_MINGW_GCC_TOOLCHAIN
Build toolchain headers.
#define CBUILD_BUILD_OUT_DIR
Build out in build/toolchain.
#define CBUILD_BUILD_DIR
Build directory of CBuild.
Improved CBuild hasher.
MINGW_GCC(std::string id)
Construct a new MINGW_GCC object.
Definition mingw-gcc.hpp:42
std::string wine
Definition mingw-gcc.hpp:34
void build() override
Build.
Definition mingw-gcc.hpp:71
void link() override
Linking.
Definition mingw-gcc.hpp:92
MINGW_GCC(std::string id, std::string name)
Construct a new MINGW_GCC object.
Definition mingw-gcc.hpp:58
void link_pack() override
Linking for static libraries.
void post_link() override
After linking.
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::string gen_out_name(std::string executable=".exe", std::string dyn_lib=".dll", std::string stat_lib=".a") override
Generate output file name (after linking)
Toolchain class.
Definition Build.hpp:65
bool force
Force argument (scratch variable)
Definition Build.hpp:153
std::string packer
ar command for packing static libraries
Definition Build.hpp:106
Hash * hasher
Hasher used here.
Definition Build.hpp:138
virtual void compile(std::string cmd)
Call compiler.
Definition Build.cpp:504
std::string name
Name of output, if undefined (""), for id is used for binary name.
Definition Build.hpp:70
std::vector< std::string > link_args
Linker args (specified and generated)
Definition Build.hpp:94
bool gen_file_list_for_linking
Generate file list for linking - force and no changes to hashes.
Definition Build.hpp:130
virtual lib::map< std::string, std::string > gen_file_list(bool force)
Generate list of files that need to be compiles.
Definition Build.cpp:216
std::string id
Must be initialized in derived classes.
Definition Build.hpp:74
std::vector< std::string > compiler_args
Compiler args (specified and generated)
Definition Build.hpp:90
virtual void add_link_arg(std::string arg)
Add linker arguments.
Definition Build.cpp:44
std::string linker
Linker command.
Definition Build.hpp:102
std::vector< std::string > depends
All other targets, that is needed for this target.
Definition Build.hpp:82
virtual std::string cmd_str(std::string in)
Replace spaces by "\ ".
Definition Build.cpp:443
std::string compiler
Compiler command.
Definition Build.hpp:98
std::vector< std::string > * args
Argument pointer (scratch variable)
Definition Build.hpp:149
filesystem++ api
CBuild::Toolchain * GetToolchain(std::string name, bool force=false)
Get the registered toolchain.
Definition register.cpp:239
bool copy(std::string start, std::string end)
Copy files or directories.
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
uint64_t hash(std::string str)
FNV-1a hashing function for std::string.
Definition hash.cpp:218
void print(std::string msg, color fg=CBuild::WHITE)
Print colored text to STDOUT.
Definition print.cpp:70
build_type
Definition Build.hpp:55
@ EXECUTABLE
Definition Build.hpp:55
@ DYNAMIC_LIBRARY
Definition Build.hpp:55
@ RED
Definition print.hpp:34
Custom print that support color codes.
Register any things.
Command for compile_commands.json.