CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
g++mt.hpp
Go to the documentation of this file.
1
21#ifndef CBUILD_GXXMT_TOOLCHAIN
22#define CBUILD_GXXMT_TOOLCHAIN
23// Project files
24#include "../CBuild_defs.hpp"
26#include "../map.hpp"
27#include "Build.hpp"
28#include "atomic"
29#include "thread"
30#include <thread>
31// Code
32namespace CBuild {
33template <CBuild::HashImpl hash = CBuild::CBuildHashV2> class GXXMT : public CBuild::Toolchain {
34 public:
40 GXXMT(std::string id) {
41 // Set id of toolchain and assign executables constants
42 this->id = id;
43 this->name = "";
44 this->linker = "g++";
45 this->compiler = "g++";
46 this->packer = "ar cr";
47 this->add_link_arg("-Wl,-z,origin");
48 this->add_link_arg(" -Wl,-rpath,\"\\$ORIGIN\"");
49 this->add_link_arg("-lstdc++");
50 this->hasher = new hash(this->id);
51 }
58 GXXMT(std::string id, std::string name) {
59 // Set id and name of toolchain and assign executables constants
60 this->id = id;
61 this->name = name;
62 this->linker = "g++";
63 this->compiler = "g++";
64 this->packer = "ar cr";
65 this->add_link_arg("-Wl,-z,origin");
66 this->add_link_arg(" -Wl,-rpath,\"\\$ORIGIN\"");
67 this->add_link_arg("-lstdc++");
68 this->hasher = new hash(this->id);
69 }
70
71 protected:
72 void build() override {
73 // Get all args
74 std::string args;
75 for (auto elem : this->compiler_args) {
76 args += elem;
77 args += " ";
78 }
79 // Get all files
80 auto files = this->gen_file_list(this->force);
81 // Read pointer
82 std::atomic_int64_t read_ptr;
83 read_ptr.store(files.size() - 1);
84 // Get hw thread count
85 unsigned int num_threads = std::thread::hardware_concurrency();
86 // Setup threads
87 std::thread threads[num_threads];
88 for (unsigned int i = 0; i < num_threads; i++) {
89 threads[i] = std::thread([&files, this, &args, &read_ptr](void) -> void {
90 while (true) {
91 // Fetch read pointer
92 int64_t i = read_ptr.fetch_sub(1);
93 if (i < 0) {
94 return;
95 }
96 // Construct command
97 std::string cmd = this->compiler + " -c ";
98 cmd += files.at(i).key;
99 cmd += " ";
100 cmd += args;
101 cmd += " -o ";
102 cmd += files.at(i).data;
103 // Execute command
104 std::this_thread::get_id();
105
106 this->compile(cmd);
107 }
108 });
109 }
110 for (unsigned int i = 0; i < num_threads; i++) {
111 threads[i].join();
112 }
113 }
114 void link() override {
115 // Get args
116 std::string args;
117 for (auto elem : this->link_args) {
118 args += elem;
119 args += " ";
120 }
121 // Get files
122 this->gen_file_list_for_linking = true;
123 auto files = this->gen_file_list(true);
124 this->gen_file_list_for_linking = false;
125 std::string flist;
126 for (unsigned int i = 0; i < files.size(); i++) {
127 flist += files.at(i).data;
128 flist += " ";
129 }
130 if (files.size() > 0) {
131 // Construct command
132 std::string cmd = this->linker + " ";
133 cmd += flist;
134 cmd += " ";
135 cmd += args;
136 cmd += " ";
137 cmd += " -o ";
138 cmd += this->gen_out_name();
139 // Call command
140 // CBuild::print(cmd.c_str(), CBuild::color::BLUE);
141 this->compile(cmd);
142 }
143 }
144 void link_pack() override {
145 // Get args
146 std::string args;
147 for (auto elem : this->link_args) {
148 args += elem;
149 args += " ";
150 }
151 // Get all files
152 this->gen_file_list_for_linking = true;
153 auto files = this->gen_file_list(true);
154 this->gen_file_list_for_linking = false;
155 std::string flist;
156 for (unsigned int i = 0; i < files.size(); i++) {
157 flist += files.at(i).data;
158 flist += " ";
159 }
160 if (files.size() > 0) {
161 // Construct command
162 std::string cmd = this->packer + " ";
163 cmd += this->gen_out_name();
164 cmd += " ";
165 cmd += flist;
166 cmd += " ";
167 // Call command
168 // CBuild::print(cmd.c_str(), CBuild::color::BLUE);
169 this->compile(cmd);
170 }
171 }
172};
173} // namespace CBuild
174#endif // CBUILD_GXXMT_TOOLCHAIN
Build toolchain headers.
Improved CBuild hasher.
void build() override
Build.
Definition g++mt.hpp:72
void link() override
Linking.
Definition g++mt.hpp:114
GXXMT(std::string id)
Construct a new GXXMT object.
Definition g++mt.hpp:40
GXXMT(std::string id, std::string name)
Construct a new GXXMT object.
Definition g++mt.hpp:58
void link_pack() override
Linking for static libraries.
Definition g++mt.hpp:144
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
virtual std::string gen_out_name(std::string executable=".run", std::string dyn_lib=".so", std::string stat_lib=".a")
Generate output file name (after linking)
Definition Build.cpp:394
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::string compiler
Compiler command.
Definition Build.hpp:98
std::vector< std::string > * args
Argument pointer (scratch variable)
Definition Build.hpp:149
Custom implementation of map datatype.
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
Command for compile_commands.json.