CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
cbuild_dep.hpp
Go to the documentation of this file.
1
22// C++ libraries
23#include "string"
24#include "vector"
25// Project includes
26#include "../CBuild_defs.hpp"
27#include "../build_data.hpp"
28#include "../filesystem++.hpp"
29#include "../print.hpp"
30#include "../system.hpp"
31#include "dependency.hpp"
32// Code
33#ifndef __CBUILD_DEP_HPP__
34#define __CBUILD_DEP_HPP__
35namespace CBuild {
37 protected:
38 std::string path;
39 std::string target_id;
40 std::string headers_dir;
41
42 public:
43 CBuildDependency(std::string path, std::string target_id, std::string headers_dir) {
44 this->path = path;
45 this->target_id = target_id;
46 this->headers_dir = headers_dir;
47 }
48 bool need_prepare() override {
51 this->target_id + "/" + this->target_id +
53 &meta);
55 meta.out.substr(meta.out.find_last_of("/") + 1));
56 }
57 std::string cargs() override {
58 return ("-I" + CBUILD_CACHE_DIR + "/" + CBUILD_PROJECT_DEPS_HEADERS + "/extproject_" +
59 this->target_id);
60 }
61 std::string largs() override {
64 this->target_id + "/" + this->target_id +
66 &meta);
67 return ("-L" + CBUILD_CACHE_DIR + "/" + CBUILD_PROJECT_DEPS_DIR + " -l" +
68 meta.out.substr(meta.out.find_last_of("/") + 4,
69 (meta.out.length() - (meta.out.find_last_of("/") + 4) -
70 (meta.out.length() - meta.out.find_last_of(".")))));
71 }
72 void prepare() override {
73 CBuild::print("Running prepare() for dependency \"" + this->target_id + "\"", CBuild::RED);
76 this->target_id + "/" + this->target_id +
78 &meta);
79 std::string ccmd = meta.compiler + " ";
80 for (auto carg : meta.cargs) {
81 ccmd += carg;
82 ccmd += " ";
83 }
84 std::string lcmd = meta.linker + " ";
85 for (auto larg : meta.largs) {
86 lcmd += larg;
87 lcmd += " ";
88 }
89 CBuild::print_full("Search metadata path: \"" + this->path + "/" + CBUILD_BUILD_DIR + "/" +
90 this->target_id + "/" + CBUILD_METADATA_FOLDER + "\"",
92 std::vector files = CBuild::fs::dir(this->path + "/" + CBUILD_BUILD_DIR + "/" +
93 this->target_id + "/" + CBUILD_METADATA_FOLDER,
94 ".*\\.meta");
95 std::string object_list = "";
96 CBuild::fs::create({CBUILD_BUILD_DIR + "/extproject_" + this->target_id}, CBuild::fs::DIR);
98 {CBUILD_BUILD_DIR + "/extproject_" + this->target_id + "/" + CBUILD_BUILD_OUT_DIR},
101 {CBUILD_BUILD_DIR + "/extproject_" + this->target_id + "/" + CBUILD_BUILD_CACHE_DIR},
103 for (auto file : files) {
105 CBuild::read_file_metadata_direct(this->target_id, file, &smeta);
106 if (smeta.object != std::string("header")) {
107 CBuild::print_full("Compiling file \"" + file + "\"", CBuild::GREEN);
108 object_list = object_list + " " + CBUILD_BUILD_DIR + "/extproject_" +
109 this->target_id + "/" + CBUILD_BUILD_CACHE_DIR + "/" +
110 smeta.object.substr(smeta.object.find_last_of("/") + 1);
111 this->compile(ccmd + " -c " + this->path + "/" + smeta.source + " -o " +
112 CBUILD_BUILD_DIR + "/extproject_" + this->target_id + "/" +
114 smeta.object.substr(smeta.object.find_last_of("/") + 1));
115 }
116 }
117 this->compile(lcmd + object_list + " -o " + CBUILD_BUILD_DIR + "/extproject_" +
118 this->target_id + "/" + CBUILD_BUILD_OUT_DIR + "/" +
119 meta.out.substr(meta.out.find_last_of("/")));
120 CBuild::fs::copy(CBUILD_BUILD_DIR + "/extproject_" + this->target_id + "/" +
122 meta.out.substr(meta.out.find_last_of("/") + 1),
124 meta.out.substr(meta.out.find_last_of("/") + 1));
126 this->headers_dir.substr(this->headers_dir.find_last_of("/") + 1)},
129 "/extproject_" + this->target_id);
130 }
131 void compile(std::string str) {
132 int ret = CBuild::system(str);
133 if (ret != 0) {
134 CBuild::print("Error in compilation! Stopping compilation...", CBuild::RED);
135 exit(0xFF);
136 }
137 }
138};
139} // namespace CBuild
140#endif // __CBUILD_DEP_HPP__
#define CBUILD_BUILD_OUT_DIR
Build out in build/toolchain.
#define CBUILD_PROJECT_DEPS_DIR
For other included project, in cache dir.
#define CBUILD_BUILD_CACHE_DIR
Object cache in build/toolchain.
#define CBUILD_METADATA_FOLDER
Metadata folder for targets.
#define CBUILD_METADATA_FILE_EXTENSION
Extension for CBuild metadata files.
#define CBUILD_CACHE_DIR
Cache directory of CBuild.
#define CBUILD_PROJECT_DEPS_HEADERS
For other included project, in cache dir.
#define CBUILD_BUILD_DIR
Build directory of CBuild.
Build metadata config file.
std::string cargs() override
CBuildDependency(std::string path, std::string target_id, std::string headers_dir)
void prepare() override
bool need_prepare() override
std::string largs() override
void compile(std::string str)
Some external dependency. Base class.
filesystem++ api
bool exists(std::string path)
Check if file exists.
bool create(std::vector< std::string > paths, CBuild::fs::type what)
Create element.
bool copy(std::string start, std::string end)
Copy files or directories.
@ DIR
Alias for DIRECTORY.
std::vector< std::string > dir(std::string path, std::string search)
Search files using provided regex.
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
void print(std::string msg, color fg=CBuild::WHITE)
Print colored text to STDOUT.
Definition print.cpp:70
@ RED
Definition print.hpp:34
@ MAGENTA
Definition print.hpp:38
@ GREEN
Definition print.hpp:35
int read_file_metadata_direct(std::string target_id, std::string file, CBuild::source_metadata_file *metadata)
Load metadata for source file.
void exit(int code)
int read_target_metadata_direct(std::string path, CBuild::target_metadata_file *metadata)
Load metadata for a full target using path to metdata file.
int system(std::string cmd)
Call stdlib system() and print cmd to shell.
Definition system.cpp:47
void print_full(std::string msg, color fg=CBuild::WHITE)
Print colored text to STDOUT if verbose flag is set.
Definition print.cpp:75
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)
Metadata for a full toolchain Structure of file:
std::string out
Path to output binary (relative to CBuild.run)
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)
Custom system() wraper.