CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
simple_toolchain.hpp
Go to the documentation of this file.
1
22// C++ libraries
23#include "string"
24#include "vector"
25// Project includes
27#include "Build.hpp"
28// Code
29#ifndef __SIMPLE_TOOLCHAIN_HPP__
30#define __SIMPLE_TOOLCHAIN_HPP__
31namespace CBuild {
37template <CBuild::HashImpl hash = CBuild::CBuildHashV2>
39 public:
45 SimpleToolchain(std::string id) {
46 this->id = id;
47 this->name = "";
48 this->hasher = new hash(this->id);
49 }
56 SimpleToolchain(std::string id, std::string name) {
57 // Set id and name of toolchain and assign executables constants
58 this->id = id;
59 this->name = name;
60 }
67 void set_compiler(std::string compiler) {
68 this->compiler = compiler;
69 }
76 void set_linker(std::string linker) {
77 this->linker = linker;
78 }
85 void set_packer(std::string packer) {
86 this->packer = packer;
87 }
88
89 protected:
90 void build() override {
91 // Get all args
92 std::string args;
93 for (auto elem : this->compiler_args) {
94 args += elem;
95 args += " ";
96 }
97 // Get all files
98 auto files = this->gen_file_list(this->force);
99 if (files.size() > 0) {
100 // Compile file by file
101 for (unsigned int i = 0; i < files.size(); i++) {
102 // Construct command
103 std::string cmd = this->compiler + " -c ";
104 cmd += files.at(i).key;
105 cmd += " ";
106 cmd += args;
107 cmd += " -o ";
108 cmd += files.at(i).data;
109 // Execute command
110 this->compile(cmd);
111 }
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 // __SIMPLE_TOOLCHAIN_HPP__
Build toolchain headers.
Improved CBuild hasher.
Very simple generic toolchain, rely on compiler to support '-c' flat to specify files to compile and ...
void link_pack() override
Linking for static libraries.
void link() override
Linking.
void set_compiler(std::string compiler)
Set used compiler.
void build() override
Build.
void set_packer(std::string packer)
Set used packer.
SimpleToolchain(std::string id, std::string name)
Construct a new GXX object.
SimpleToolchain(std::string id)
Construct a new GXX object.
void set_linker(std::string linker)
Set used linker.
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
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.