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