CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
metadata_file.cpp
Go to the documentation of this file.
1
21// C++ libs
22#include "cstdint"
23#include "fstream"
24#include "sstream"
25#include "string"
26#include "vector"
27// Project headers
32/* Internal funcs */
34 if (!CBuild::fs::exists(path)) {
35 metadata = NULL;
36 return -1;
37 }
38 std::fstream f;
39 std::string FILE_DATA;
40 f.open(path);
41 std::getline(f, FILE_DATA, '\0');
42 f.close();
43 std::string line;
44 std::stringstream FILE_DATA_STREAM(FILE_DATA);
45 std::getline(FILE_DATA_STREAM, line, '\n');
46 metadata->source = line;
47 std::getline(FILE_DATA_STREAM, line, '\n');
48 metadata->object = line;
49 std::getline(FILE_DATA_STREAM, line, '\n');
50 if (line != std::string("DEPS")) {
51 return -3;
52 }
53 std::getline(FILE_DATA_STREAM, line, '\n');
54 while (line != std::string("ENDDEPS")) {
55 metadata->deps.push_back(line);
56 std::getline(FILE_DATA_STREAM, line, '\n');
57 }
58 std::getline(FILE_DATA_STREAM, line, '\n');
59 if (!std::isdigit(line.front()) || !std::isdigit(line.back())) {
60 return -3;
61 }
62 metadata->hash = std::strtoull(line.c_str(), NULL, 10);
63 return 0;
64}
65/* build_data.hpp */
66int CBuild::read_file_metadata(std::string target_id, std::string src_file,
68 std::string path = CBuild::get_file_metadata_path(target_id, src_file);
69 return read_file_metadata_internal(path, metadata);
70}
71int CBuild::read_file_metadata_direct(std::string target_id, std::string file,
73 std::string path = file;
74 return read_file_metadata_internal(path, metadata);
75}
76int CBuild::read_target_metadata(std::string target_id, CBuild::target_metadata_file* metadata) {
77 std::string path = CBuild::get_target_metadata_path(target_id);
78 return CBuild::read_target_metadata_direct(path, metadata);
79}
81 if (!CBuild::fs::exists(path)) {
82 metadata = NULL;
83 return -1;
84 }
85 std::fstream f;
86 std::string FILE_DATA;
87 f.open(path);
88 std::getline(f, FILE_DATA, '\0');
89 f.close();
90 std::string line;
91 std::stringstream FILE_DATA_STREAM(FILE_DATA);
92 std::getline(FILE_DATA_STREAM, line, '\n');
93 metadata->out = line;
94 std::getline(FILE_DATA_STREAM, line, '\n');
95 metadata->compiler = line;
96 std::getline(FILE_DATA_STREAM, line, '\n');
97 metadata->linker = line;
98 std::getline(FILE_DATA_STREAM, line, '\n');
99 metadata->packer = line;
100 std::getline(FILE_DATA_STREAM, line, '\n');
101 if (line != std::string("CARGS")) {
102 return -3;
103 }
104 std::getline(FILE_DATA_STREAM, line, '\n');
105 while (line != std::string("ENDCARGS")) {
106 metadata->cargs.push_back(line);
107 std::getline(FILE_DATA_STREAM, line, '\n');
108 }
109 std::getline(FILE_DATA_STREAM, line, '\n');
110 if (line != std::string("LARGS")) {
111 return -3;
112 }
113 std::getline(FILE_DATA_STREAM, line, '\n');
114 while (line != std::string("ENDLARGS")) {
115 metadata->largs.push_back(line);
116 std::getline(FILE_DATA_STREAM, line, '\n');
117 }
118 return 0;
119}
120int CBuild::read_file_hash(std::string target_id, std::string file, uint64_t* hash) {
122 int ret = CBuild::read_file_metadata(target_id, file, &m);
123 *hash = m.hash;
124 return ret;
125}
126int CBuild::write_file_metadata(std::string target_id, std::string src_file,
128 std::string path = CBuild::get_file_metadata_path(target_id, src_file);
129 std::ofstream f(path);
130 f << metadata->source << "\n";
131 f << metadata->object << "\n";
132 f << "DEPS\n";
133 for (auto dep : metadata->deps) {
134 f << dep << "\n";
135 }
136 f << "ENDDEPS\n";
137 f << metadata->hash << "\n";
138 f.close();
139 return 0;
140}
141int CBuild::write_target_metadata(std::string target_id, CBuild::target_metadata_file* metadata) {
142 std::string path = CBuild::get_target_metadata_path(target_id);
143 std::ofstream f(path);
144 f << metadata->out << "\n";
145 f << metadata->compiler << "\n";
146 f << metadata->linker << "\n";
147 f << metadata->packer << "\n";
148 f << "CARGS\n";
149 for (auto dep : metadata->cargs) {
150 f << dep << "\n";
151 }
152 f << "ENDCARGS\n";
153 f << "LARGS\n";
154 for (auto dep : metadata->largs) {
155 f << dep << "\n";
156 }
157 f << "ENDLARGS\n";
158 f.close();
159 return 0;
160}
161int CBuild::write_file_hash(std::string target_id, std::string file, uint64_t* hash) {
163 int ret = CBuild::read_file_metadata(target_id, file, &m);
164 if (ret != 0) {
165 return ret;
166 }
167 m.hash = *hash;
168 return CBuild::write_file_metadata(target_id, file, &m);
169}
170std::string CBuild::get_file_metadata_path(std::string target_id, std::string file) {
171 // Replace '/' with '.'
172 while (file.find("/") != std::string::npos) {
173 file.replace(file.find("/"), std::string("/").size(), ".");
174 }
175
176 return std::string(CBUILD_BUILD_DIR + "/" + target_id + "/" + CBUILD_METADATA_FOLDER + "/" +
178}
179std::string CBuild::get_target_metadata_path(std::string target_id) {
180 return CBUILD_BUILD_DIR + "/" + target_id + "/" + target_id + CBUILD_METADATA_FILE_EXTENSION;
181}
#define CBUILD_METADATA_FOLDER
Metadata folder for targets.
#define CBUILD_METADATA_FILE_EXTENSION
Extension for CBuild metadata files.
#define CBUILD_BUILD_DIR
Build directory of CBuild.
Build metadata config file.
filesystem++ api
int read_file_metadata_internal(std::string path, CBuild::source_metadata_file *metadata)
bool exists(std::string path)
Check if file exists.
uint64_t hash(std::string str)
FNV-1a hashing function for std::string.
Definition hash.cpp:218
int write_file_hash(std::string target_id, std::string file, uint64_t *hash)
int read_file_metadata_direct(std::string target_id, std::string file, CBuild::source_metadata_file *metadata)
Load metadata for source file.
int write_file_metadata(std::string target_id, std::string src_file, CBuild::source_metadata_file *metadata)
Write a metadata for a file to a file.
int read_file_metadata(std::string target_id, std::string src_file, CBuild::source_metadata_file *metadata)
Load metadata for source file.
int read_target_metadata(std::string target_id, CBuild::target_metadata_file *metadata)
Load metadata for a full target.
std::string get_target_metadata_path(std::string target_id)
Get the path to a target metadata file.
int read_target_metadata_direct(std::string path, CBuild::target_metadata_file *metadata)
Load metadata for a full target using path to metdata file.
std::string get_file_metadata_path(std::string target_id, std::string file)
Get the path to a file metadata file.
int write_target_metadata(std::string target_id, CBuild::target_metadata_file *metadata)
Write a metadata for a full target.
int read_file_hash(std::string target_id, std::string file, uint64_t *hash)
Get a file hash (internally uses read_file_metadata, so no performance benefits, but easiier to use i...
Custom print that support color codes.
Metadata for source files (.cpp/.c/etc) Structure of file:
std::string object
Object file name.
std::string source
Source file path (relative to CBuild.run)
std::vector< std::string > deps
On what files this file depends.
uint64_t hash
Hash of this file.
Metadata for a full toolchain Structure of file:
std::string out
Path to output binary (relative to CBuild.run)
std::string packer
Command used to pack this target to a static library (what shell command was used)
std::vector< std::string > largs
Link args.
std::vector< std::string > cargs
Compile args.
std::string linker
Command used to link this target (what shell command was used)
std::string compiler
Command used to compile this target (what shell command was used)