-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cpp
161 lines (129 loc) · 5.24 KB
/
build.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
void build() {
const std::filesystem::path include_dir = "include";
const std::filesystem::path src_dir = "src";
const std::filesystem::path build_dir = "build/cppx";
const std::filesystem::path build_lib_dir = build_dir / "lib";
const std::filesystem::path build_bin_dir = build_dir / "bin";
const std::string lib_name = "libcppx.a";
try {
std::filesystem::create_directories(build_lib_dir / src_dir);
std::filesystem::create_directories(build_bin_dir / src_dir);
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error: Failed to create build directories: " << e.what() << std::endl;
return;
}
std::vector<std::filesystem::path> lib_cpp_files = {
"src/cppx/json.cpp",
"src/cppx/preprocessor.cpp"
};
std::vector<std::filesystem::path> exe_sources = {
"src/cppx/main.cpp"
};
std::vector<std::filesystem::path> lib_object_files;
std::cout << "Building library..." << std::endl;
for (const auto &cpp_file : lib_cpp_files) {
std::filesystem::path relative_path = std::filesystem::relative(cpp_file, src_dir);
std::filesystem::path object_path = build_lib_dir / src_dir / relative_path;
object_path.replace_extension(".o");
try {
std::filesystem::create_directories(object_path.parent_path());
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error: Failed to create directory for " << object_path << ": " << e.what() << std::endl;
return;
}
std::string compile_cmd = "g++ -c \"" + cpp_file.string() + "\" -I\"" + include_dir.string() + "\" -std=c++20 -O3 -o \"" + object_path.string() + "\"";
if (std::system(compile_cmd.c_str()) != 0) {
std::cerr << "Error: Compilation failed for " << cpp_file << std::endl;
return;
}
lib_object_files.push_back(object_path);
}
std::cout << "Archiving library..." << std::endl;
if (!lib_object_files.empty()) {
std::filesystem::path archive_path = build_lib_dir / lib_name;
std::string archive_cmd = "ar rcs \"" + archive_path.string() + "\"";
for (const auto &obj : lib_object_files) {
archive_cmd += " \"" + obj.string() + "\"";
}
if (std::system(archive_cmd.c_str()) != 0) {
std::cerr << "Error: Archiving failed for " << lib_name << std::endl;
return;
}
} else {
std::cerr << "Error: No library source files found to archive." << std::endl;
return;
}
std::cout << "Building executables..." << std::endl;
for (const auto &exe_src : exe_sources) {
std::filesystem::path relative_path = std::filesystem::relative(exe_src, src_dir);
std::filesystem::path exe_obj_path = build_bin_dir / src_dir / relative_path;
exe_obj_path.replace_extension(".o");
try {
std::filesystem::create_directories(exe_obj_path.parent_path());
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error: Failed to create directory for " << exe_obj_path << ": " << e.what() << std::endl;
return;
}
std::string compile_exe_cmd = "g++ -c \"" + exe_src.string() + "\" -I\"" + include_dir.string() + "\" -std=c++20 -O3 -o \"" + exe_obj_path.string() + "\"";
if (std::system(compile_exe_cmd.c_str()) != 0) {
std::cerr << "Error: Compilation failed for executable source " << exe_src << std::endl;
return;
}
std::filesystem::path exe_output_path = build_bin_dir / src_dir / relative_path;
exe_output_path.replace_extension(""); // Remove the .cpp extension
std::string link_cmd = "g++ \"" + exe_obj_path.string() + "\" -L\"" + build_lib_dir.string() + "\" -lcppx -std=c++20 -O3 -o \"" + exe_output_path.string() + "\"";
if (std::system(link_cmd.c_str()) != 0) {
std::cerr << "Error: Linking failed for executable " << exe_output_path << std::endl;
return;
}
}
std::cout << "Build completed successfully!" << std::endl;
}
void watch() {
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> files_last_write_time;
std::vector<std::string> dirs_to_watch = {
"include",
"src"
};
for (const auto &dir : dirs_to_watch) {
for (const auto &file : std::filesystem::recursive_directory_iterator(dir)) {
if (file.is_regular_file()) {
files_last_write_time[file.path()] = std::filesystem::last_write_time(file);
}
}
}
build();
std::cout << "Watching for changes..." << std::endl;
while (true) {
bool files_changed = false;
for (const auto &dir : dirs_to_watch) {
for (const auto &file : std::filesystem::recursive_directory_iterator(dir)) {
if (file.is_regular_file()) {
auto current_file_time = std::filesystem::last_write_time(file);
if (files_last_write_time[file.path()] != current_file_time) {
files_last_write_time[file.path()] = current_file_time;
files_changed = true;
}
}
}
}
if (files_changed) build();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main(int argc, char *argv[]) {
if (argc > 1 && (std::string(argv[1]) == "-w" || std::string(argv[1]) == "--watch")) {
watch();
} else {
build();
}
return 0;
}