@@ -61,10 +61,15 @@ class Target : public BuilderInterface {
6161
6262 std::string target_ext{" " };
6363 std::string obj_ext{" .o" };
64+ std::string pch_header_ext{" .hpp" };
65+ std::string pch_compile_ext{" .gch" };
6466
6567 std::string prefix_include_dir{" -I" };
6668 std::string prefix_lib_dir{" -L" };
6769
70+ std::string pch_command{" {compiler} {preprocessor_flags} {include_dirs} "
71+ " {common_compile_flags} {pch_flags} "
72+ " {compile_flags} -o {output} -c {input}" };
6873 std::string compile_command{
6974 " {compiler} {preprocessor_flags} {include_dirs} {common_compile_flags} "
7075 " {compile_flags} -o {output} -c {input}" };
@@ -101,7 +106,6 @@ class Target : public BuilderInterface {
101106 }
102107 virtual ~Target () {}
103108
104- Target (Target &&target) = default ;
105109 Target (const Target &target) = delete ;
106110
107111 // Builders
@@ -129,6 +133,11 @@ class Target : public BuilderInterface {
129133 void GlobHeaders (const fs::path &relative_to_target_path);
130134 void GlobHeadersAbsolute (const fs::path &absolute_path);
131135
136+ // PCH
137+ void AddPch (const fs::path &relative_filename,
138+ const fs::path &relative_to_target_path = " " );
139+ void AddPchAbsolute (const fs::path &absolute_filepath);
140+
132141 // * Include and Lib directory
133142 void AddIncludeDir (const fs::path &relative_include_dir,
134143 bool glob_headers = false );
@@ -145,6 +154,7 @@ class Target : public BuilderInterface {
145154 // * Flags
146155 void AddPreprocessorFlag (const std::string &flag);
147156 void AddCommonCompileFlag (const std::string &flag);
157+ void AddPchFlag (const std::string &flag);
148158 void AddAsmCompileFlag (const std::string &flag);
149159 void AddCCompileFlag (const std::string &flag);
150160 void AddCppCompileFlag (const std::string &flag);
@@ -177,6 +187,17 @@ class Target : public BuilderInterface {
177187 // we can cache these variables during Target construction
178188 fs::path GetTargetPath () const { return ConstructTargetPath (); }
179189
190+ // TODO, Make these construct APIs
191+ fs::path GetPchHeaderPath () const {
192+ return target_intermediate_dir_ /
193+ fmt::format (" buildcc_pch{}" , config_.pch_header_ext );
194+ }
195+ // Each target only has only 1 PCH file
196+ fs::path GetPchCompilePath () const {
197+ return GetPchHeaderPath ().replace_extension (
198+ fmt::format (" {}{}" , config_.pch_header_ext , config_.pch_compile_ext ));
199+ }
200+
180201 // Const references
181202
182203 // TODO, Shift getters to source file as well
@@ -207,6 +228,9 @@ class Target : public BuilderInterface {
207228 const std::unordered_set<std::string> &GetCurrentCommonCompileFlags () const {
208229 return current_common_compile_flags_;
209230 }
231+ const std::unordered_set<std::string> &GetCurrentPchFlags () const {
232+ return current_pch_flags_;
233+ }
210234 const std::unordered_set<std::string> &GetCurrentAsmCompileFlags () const {
211235 return current_asm_compile_flags_;
212236 }
@@ -273,11 +297,15 @@ class Target : public BuilderInterface {
273297 void UnlockedAfterBuild () const ;
274298
275299 // Build
300+ void BuildPch ();
301+ // TODO, Rename to BuildObject
276302 void BuildCompile (std::vector<fs::path> &source_files,
277303 std::vector<fs::path> &dummy_source_files);
278304 void BuildLink ();
279305
280306 //
307+ void PrePchCompile ();
308+ // TODO, Rename to PreObjectCompile
281309 void PreCompile ();
282310 void PreLink ();
283311
@@ -298,6 +326,8 @@ class Target : public BuilderInterface {
298326 const std::unordered_set<std::string> ¤t_external_libs);
299327
300328 // Tasks
329+ void PchTask ();
330+ // TODO, Rename to ObjectTask and TargetTask
301331 void CompileTask ();
302332 void LinkTask ();
303333
@@ -319,6 +349,7 @@ class Target : public BuilderInterface {
319349 // Construct
320350 fs::path ConstructObjectPath (const fs::path &absolute_source_file) const ;
321351 fs::path ConstructTargetPath () const ;
352+ std::string ConstructPchCompileCommand () const ;
322353 std::string
323354 ConstructCompileCommand (const fs::path &absolute_current_source) const ;
324355 std::string ConstructLinkCommand () const ;
@@ -344,12 +375,14 @@ class Target : public BuilderInterface {
344375 // TODO, Use an internal::Storer class / struct for this to reduce clutter
345376 internal::default_files current_source_files_;
346377 internal::default_files current_header_files_;
378+ internal::default_files current_pch_files_;
347379 internal::default_files current_lib_deps_;
348380 internal::fs_unordered_set current_include_dirs_;
349381 internal::fs_unordered_set current_lib_dirs_;
350382 std::unordered_set<std::string> current_external_lib_deps_;
351383 std::unordered_set<std::string> current_preprocessor_flags_;
352384 std::unordered_set<std::string> current_common_compile_flags_;
385+ std::unordered_set<std::string> current_pch_flags_;
353386 std::unordered_set<std::string> current_asm_compile_flags_;
354387 std::unordered_set<std::string> current_c_compile_flags_;
355388 std::unordered_set<std::string> current_cpp_compile_flags_;
@@ -360,6 +393,8 @@ class Target : public BuilderInterface {
360393 // Not used for serialization
361394 // NOTE, Always store the absolute source path -> absolute compiled source
362395 // path here
396+ OutputInfo pch_file_;
397+ // TODO, Remove current from these
363398 std::unordered_map<fs::path, OutputInfo, internal::PathHash>
364399 current_object_files_;
365400 OutputInfo current_target_file_;
@@ -369,6 +404,7 @@ class Target : public BuilderInterface {
369404 FileExt ext_{*this };
370405
371406 tf::Taskflow tf_;
407+ tf::Task pch_task_;
372408 tf::Task compile_task_;
373409 tf::Task link_task_;
374410};
0 commit comments