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