-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompile_object.cpp
269 lines (233 loc) · 9.71 KB
/
compile_object.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright 2021-2022 Niket Naidu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "target/friend/compile_object.h"
#include "target/target.h"
namespace {
constexpr const char *const kCompiler = "compiler";
constexpr const char *const kCompileFlags = "compile_flags";
constexpr const char *const kOutput = "output";
constexpr const char *const kInput = "input";
} // namespace
namespace buildcc::internal {
void CompileObject::AddObjectData(const fs::path &absolute_source_path) {
const fs::path absolute_object_path =
ConstructObjectPath(absolute_source_path);
fs::create_directories(absolute_object_path.parent_path());
object_files_.try_emplace(
internal::PathInfo::ToPathString(absolute_source_path),
absolute_object_path, "");
}
void CompileObject::CacheCompileCommands() {
for (auto &[absolute_current_source, object_data] : object_files_) {
const std::string output =
fmt::format("{}", GetObjectData(absolute_current_source).output);
const std::string input = fmt::format("{}", absolute_current_source);
const auto type =
target_.toolchain_.GetConfig().GetFileExt(absolute_current_source);
const std::string selected_aggregated_compile_flags =
target_.SelectCompileFlags(type).value_or("");
const std::string selected_compiler =
fmt::format("{}", fs::path(target_.SelectCompiler(type).value_or("")));
object_data.command = target_.command_.Construct(
target_.GetConfig().compile_command,
{
{kCompiler, selected_compiler},
{kCompileFlags, selected_aggregated_compile_flags},
{kOutput, output},
{kInput, input},
});
}
}
std::vector<fs::path> CompileObject::GetCompiledSources() const {
std::vector<fs::path> compiled_sources;
for (const auto &[_, object_data] : object_files_) {
compiled_sources.push_back(object_data.output);
}
return compiled_sources;
}
const CompileObject::ObjectData &
CompileObject::GetObjectData(const fs::path &absolute_source) const {
const auto sanitized_source =
internal::PathInfo::ToPathString(absolute_source);
const auto fiter = object_files_.find(sanitized_source);
env::assert_fatal(fiter != object_files_.end(),
fmt::format("{} not found", absolute_source));
return object_files_.at(sanitized_source);
}
// PRIVATE
// NOTE: If RELATIVE TargetEnv supplied
// {target_root_dir} => `Project::GetRootDir()` /
// `target_relative_to_root`
// {target_build_dir} => `Project::GetBuildDir()` / `toolchain.GetName()`
// / `name`
// Scenarios
// - {target_root_dir} / source -> {target_build_dir} / source.compiled
// - {target_root_dir} / folder / source -> {target_build_dir} / folder /
// source.compiled
// - {target_root_dir} / .. / source -> {target_build_dir} / __ /
// source.compiled -> Warning
// Prompt using TargetEnv(abs_root, abs_build)
// - {target_absolute_root_dir} / FOOLIB / source -> {target_absolute_build_dir}
// / FOOLIB / source
// TODO, Discuss DifferentOutputFolder API
// {target_root_dir} / random / folder / file.cpp -> {target_build_dir} / random
// / folder / file.cpp.o (SAME)
// {OUT_OF_ROOT_FOLDER} / file.cpp -> {target_build_dir} / {USER_OUTPUT_FOLDER}
// / file.cpp.o
// {OUT_OF_ROOT_FOLDER} / random / folder / file.cpp -> {target_build_dir} /
// {USER_OUTPUT_FOLDER} / random / folder / file.cpp.o
fs::path
CompileObject::ConstructObjectPath(const fs::path &absolute_source_file) const {
// Compute the relative compiled source path
// Expects to convert
// 1. {project_root_dir} / file.cpp -> file.cpp
// 2. {project_root_dir} / folder / file.cpp -> folder / file.cpp
fs::path relative =
absolute_source_file.lexically_relative(target_.GetTargetRootDir());
// Expects to convert
// 1. {project_root_dir} / .. / file.cpp -> .. / file.cpp
// 2. {project_root_dir} / .. / folder / file.cpp -> .. / folder / file.cpp
// - Check if out of root
// - Convert .. to __
// NOTE, Similar to how CMake handles out of root files
std::string relstr = relative.string();
if (relstr.find("..") != std::string::npos) {
env::log_warning(
__FUNCTION__,
fmt::format("Out of Root Source detected '{}' -> '{}'. Use "
"TargetEnv to supply absolute target root "
"path -> absolute target build path. By "
"default converts '..' to '__'",
absolute_source_file.string(), relstr));
std::replace(relstr.begin(), relstr.end(), '.', '_');
// Converts above
// .. / file.cpp -> __ / file.cpp
// .. / folder / file.cpp -> __ / folder / file.cpp
relative = relstr;
// TODO, path replacement found
// * API
// AddSourceAbsolute("BUILDCC_HOME / libs / fmt / build.fmt.cpp",
// {"BUILDCC_HOME / libs / fmt", "fmt"});
// Converts above
// .. / file.cpp -> {REPLACEMENT_DIR} / file.cpp
// .. / folder / file.cpp -> {REPLACEMENT_DIR} / folder / file.cpp
// relative = relative_replacement_dir / absolute_source_file.filename();
// std::string absolute_source_file_str =
// path_as_string(absolute_source_file);
// auto iter = absolute_source_file_str.find(replacement_strategy.first);
// relative = absolute_source_file_str.replace(
// iter, replacement_strategy.first.length(),
// replacement_strategy.second);
}
// Compute relative object path
fs::path absolute_compiled_source = target_.GetTargetBuildDir() / relative;
absolute_compiled_source.replace_filename(
fmt::format("{}{}", absolute_source_file.filename().string(),
target_.toolchain_.GetConfig().obj_ext));
return absolute_compiled_source;
}
void CompileObject::BuildObjectCompile(
std::vector<internal::PathInfo> &source_files,
std::vector<internal::PathInfo> &dummy_source_files) {
PreObjectCompile();
const auto &serialization = target_.serialization_;
const auto &load_target_schema = serialization.GetLoad();
const auto &user_target_schema = target_.user_;
if (!serialization.IsLoaded()) {
target_.dirty_ = true;
} else {
if (target_.dirty_) {
} else if (!(load_target_schema.preprocessor_flags ==
user_target_schema.preprocessor_flags) ||
!(load_target_schema.common_compile_flags ==
user_target_schema.common_compile_flags) ||
!(load_target_schema.pch_object_flags ==
user_target_schema.pch_object_flags) ||
!(load_target_schema.asm_compile_flags ==
user_target_schema.asm_compile_flags) ||
!(load_target_schema.c_compile_flags ==
user_target_schema.c_compile_flags) ||
!(load_target_schema.cpp_compile_flags ==
user_target_schema.cpp_compile_flags)) {
target_.dirty_ = true;
target_.FlagChanged();
} else if (!(load_target_schema.include_dirs ==
user_target_schema.include_dirs)) {
target_.dirty_ = true;
target_.DirChanged();
} else if (!(load_target_schema.headers == user_target_schema.headers)) {
target_.dirty_ = true;
target_.PathChanged();
} else if (!(load_target_schema.compile_dependencies ==
user_target_schema.compile_dependencies)) {
target_.dirty_ = true;
target_.PathChanged();
}
}
if (target_.dirty_) {
CompileSources(source_files);
} else {
RecompileSources(source_files, dummy_source_files);
}
}
void CompileObject::PreObjectCompile() {
auto &target_user_schema = target_.user_;
// Convert user_source_files to current_source_files
target_user_schema.sources.ComputeHashForAll();
// Convert user_header_files to current_header_files
target_user_schema.headers.ComputeHashForAll();
// Convert user_compile_dependencies to current_compile_dependencies
target_user_schema.compile_dependencies.ComputeHashForAll();
}
void CompileObject::CompileSources(
std::vector<internal::PathInfo> &source_files) {
const auto &target_user_schema = target_.user_;
target_user_schema.sources.GetPathInfos();
source_files = target_user_schema.sources.GetPathInfos();
}
void CompileObject::RecompileSources(
std::vector<internal::PathInfo> &source_files,
std::vector<internal::PathInfo> &dummy_source_files) {
const auto &serialization = target_.serialization_;
const auto &user_target_schema = target_.user_;
auto previous_source_files =
serialization.GetLoad().sources.GetUnorderedPathInfos();
for (const auto ¤t_path_info :
user_target_schema.sources.GetPathInfos()) {
const auto ¤t_path = current_path_info.path;
if (previous_source_files.count(current_path) == 0) {
// Added
source_files.push_back(current_path_info);
target_.dirty_ = true;
target_.SourceAdded();
} else {
if (!(previous_source_files.at(current_path) == current_path_info.hash)) {
// Updated
source_files.push_back(current_path_info);
target_.dirty_ = true;
target_.SourceUpdated();
} else {
dummy_source_files.push_back(current_path_info);
}
previous_source_files.erase(current_path);
}
}
if (!previous_source_files.empty()) {
target_.dirty_ = true;
target_.SourceRemoved();
}
}
} // namespace buildcc::internal