diff --git a/buildcc/lib/target/fbs/target.fbs b/buildcc/lib/target/fbs/target.fbs index d4826cea..717e9e48 100644 --- a/buildcc/lib/target/fbs/target.fbs +++ b/buildcc/lib/target/fbs/target.fbs @@ -44,6 +44,7 @@ table Target { // Flags preprocessor_flags:[string]; + common_compile_flags:[string]; c_compile_flags:[string]; cpp_compile_flags:[string]; link_flags:[string]; diff --git a/buildcc/lib/target/include/target/fbs_loader.h b/buildcc/lib/target/include/target/fbs_loader.h index 18cf9c3b..4559b7f3 100644 --- a/buildcc/lib/target/include/target/fbs_loader.h +++ b/buildcc/lib/target/include/target/fbs_loader.h @@ -59,6 +59,9 @@ class FbsLoader { const std::unordered_set &GetLoadedPreprocessorFlags() const { return loaded_preprocessor_flags_; } + const std::unordered_set &GetLoadedCommonCompileFlags() const { + return loaded_common_compile_flags_; + } const std::unordered_set &GetLoadedCCompileFlags() const { return loaded_c_compile_flags_; } @@ -87,6 +90,7 @@ class FbsLoader { fs_unordered_set loaded_lib_dirs_; std::unordered_set loaded_preprocessor_flags_; + std::unordered_set loaded_common_compile_flags_; std::unordered_set loaded_c_compile_flags_; std::unordered_set loaded_cpp_compile_flags_; std::unordered_set loaded_link_flags_; diff --git a/buildcc/lib/target/include/target/target.h b/buildcc/lib/target/include/target/target.h index 2affbe93..ad264c81 100644 --- a/buildcc/lib/target/include/target/target.h +++ b/buildcc/lib/target/include/target/target.h @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -113,6 +114,7 @@ class Target { // * Flags void AddPreprocessorFlag(const std::string &flag); + void AddCommonCompileFlag(const std::string &flag); void AddCCompileFlag(const std::string &flag); void AddCppCompileFlag(const std::string &flag); void AddLinkFlag(const std::string &flag); @@ -163,6 +165,9 @@ class Target { const std::unordered_set &GetCurrentPreprocessorFlags() const { return current_preprocessor_flags_; } + const std::unordered_set &GetCurrentCommonCompileFlags() const { + return current_common_compile_flags_; + } const std::unordered_set &GetCurrentCCompileFlags() const { return current_c_compile_flags_; } @@ -190,8 +195,8 @@ class Target { std::unordered_set valid_header_ext_{".h", ".hpp"}; std::string_view compile_command_{ - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} -o " - "{output} -c {input}"}; + "{compiler} {preprocessor_flags} {include_dirs} {common_compile_flags} " + "{compile_flags} -o {output} -c {input}"}; std::string_view link_command_{ "{cpp_compiler} {link_flags} {compiled_sources} -o {output} " "{lib_dirs} {lib_deps}"}; @@ -206,6 +211,7 @@ class Target { const internal::Path &GetCompiledSourcePath(const fs::path &source) const; internal::path_unordered_set GetCompiledSources() const; + std::optional GetCompiledFlags(FileExtType type) const; private: void Initialize(); @@ -286,6 +292,7 @@ class Target { // TODO, Common flags for asm, c and cpp files std::unordered_set current_preprocessor_flags_; + std::unordered_set current_common_compile_flags_; std::unordered_set current_c_compile_flags_; std::unordered_set current_cpp_compile_flags_; std::unordered_set current_link_flags_; diff --git a/buildcc/lib/target/src/fbs/fbs_loader.cpp b/buildcc/lib/target/src/fbs/fbs_loader.cpp index aed68c48..2d9bd8e9 100644 --- a/buildcc/lib/target/src/fbs/fbs_loader.cpp +++ b/buildcc/lib/target/src/fbs/fbs_loader.cpp @@ -87,6 +87,7 @@ bool FbsLoader::Load() { Extract(target->lib_dirs(), loaded_lib_dirs_); Extract(target->preprocessor_flags(), loaded_preprocessor_flags_); + Extract(target->common_compile_flags(), loaded_common_compile_flags_); Extract(target->c_compile_flags(), loaded_c_compile_flags_); Extract(target->cpp_compile_flags(), loaded_cpp_compile_flags_); Extract(target->link_flags(), loaded_link_flags_); diff --git a/buildcc/lib/target/src/fbs/fbs_storer.cpp b/buildcc/lib/target/src/fbs/fbs_storer.cpp index c0a6ac34..9fe80173 100644 --- a/buildcc/lib/target/src/fbs/fbs_storer.cpp +++ b/buildcc/lib/target/src/fbs/fbs_storer.cpp @@ -91,9 +91,11 @@ bool Target::Store() { auto fbs_preprocessor_flags = get_fbs_vector_string(builder, current_preprocessor_flags_); - auto fbs_c_compiler_flags = + auto fbs_common_compile_flags = + get_fbs_vector_string(builder, current_common_compile_flags_); + auto fbs_c_compile_flags = get_fbs_vector_string(builder, current_c_compile_flags_); - auto fbs_cpp_compiler_flags = + auto fbs_cpp_compile_flags = get_fbs_vector_string(builder, current_cpp_compile_flags_); auto fbs_link_flags = get_fbs_vector_string(builder, current_link_flags_); @@ -101,7 +103,8 @@ bool Target::Store() { builder, name_.c_str(), fbs_target_type, &fbs_source_files, &fbs_header_files, &fbs_lib_deps, &fbs_external_lib_deps, &fbs_include_dirs, &fbs_lib_dirs, &fbs_preprocessor_flags, - &fbs_c_compiler_flags, &fbs_cpp_compiler_flags, &fbs_link_flags); + &fbs_common_compile_flags, &fbs_c_compile_flags, &fbs_cpp_compile_flags, + &fbs_link_flags); fbs::FinishTargetBuffer(builder, fbs_target); auto file_path = GetBinaryPath(); diff --git a/buildcc/lib/target/src/target/build.cpp b/buildcc/lib/target/src/target/build.cpp index e7412162..c5ea6868 100644 --- a/buildcc/lib/target/src/target/build.cpp +++ b/buildcc/lib/target/src/target/build.cpp @@ -45,6 +45,8 @@ void Target::Build() { internal::aggregate_with_prefix(prefix_lib_dir_, current_lib_dirs_)}, {"preprocessor_flags", internal::aggregate(current_preprocessor_flags_)}, + {"common_compile_flags", + internal::aggregate(current_common_compile_flags_)}, {"link_flags", internal::aggregate(current_link_flags_)}, // Toolchain executables here @@ -63,11 +65,12 @@ void Target::Build() { } else { BuildRecompile(); } + + LinkTargetTask(dirty_); } void Target::BuildCompile() { CompileSources(); - LinkTargetTask(true); dirty_ = true; first_build_ = true; } @@ -87,6 +90,8 @@ void Target::BuildRecompile() { // TODO, Toolchain, ASM, C, C++ compiler related to a particular name RecheckFlags(loader_.GetLoadedPreprocessorFlags(), current_preprocessor_flags_); + RecheckFlags(loader_.GetLoadedCommonCompileFlags(), + current_common_compile_flags_); RecheckFlags(loader_.GetLoadedCCompileFlags(), current_c_compile_flags_); RecheckFlags(loader_.GetLoadedCppCompileFlags(), current_cpp_compile_flags_); RecheckDirs(loader_.GetLoadedIncludeDirs(), current_include_dirs_); @@ -108,7 +113,6 @@ void Target::BuildRecompile() { current_external_lib_deps_); // TODO, Verify the `physical` presence of the target if dirty_ == false - LinkTargetTask(dirty_); rebuild_ = dirty_; } diff --git a/buildcc/lib/target/src/target/flags.cpp b/buildcc/lib/target/src/target/flags.cpp index 6d2bf126..5fac51be 100644 --- a/buildcc/lib/target/src/target/flags.cpp +++ b/buildcc/lib/target/src/target/flags.cpp @@ -21,6 +21,9 @@ namespace buildcc::base { void Target::AddPreprocessorFlag(const std::string &flag) { current_preprocessor_flags_.insert(flag); } +void Target::AddCommonCompileFlag(const std::string &flag) { + current_common_compile_flags_.insert(flag); +} void Target::AddCCompileFlag(const std::string &flag) { current_c_compile_flags_.insert(flag); } diff --git a/buildcc/lib/target/src/target/source.cpp b/buildcc/lib/target/src/target/source.cpp index 5186e330..e9bd16a4 100644 --- a/buildcc/lib/target/src/target/source.cpp +++ b/buildcc/lib/target/src/target/source.cpp @@ -186,9 +186,7 @@ std::string Target::CompileCommand(const fs::path ¤t_source) const { // TODO, This doesn't look clean const auto type = GetFileExtType(current_source); const std::string &aggregated_compile_flags = - type == FileExtType::C ? aggregated_c_compile_flags_ - : type == FileExtType::Cpp ? aggregated_cpp_compile_flags_ - : ""; + GetCompiledFlags(type).value_or(""); return command_.Construct(compile_command_, { diff --git a/buildcc/lib/target/src/target/target.cpp b/buildcc/lib/target/src/target/target.cpp index 205b2fe1..33d89500 100644 --- a/buildcc/lib/target/src/target/target.cpp +++ b/buildcc/lib/target/src/target/target.cpp @@ -71,6 +71,20 @@ FileExtType Target::GetFileExtType(const fs::path &filepath) const { return type; } +std::optional Target::GetCompiledFlags(FileExtType type) const { + switch (type) { + case FileExtType::C: + return aggregated_c_compile_flags_; + break; + case FileExtType::Cpp: + return aggregated_cpp_compile_flags_; + break; + default: + break; + } + return {}; +} + bool Target::IsValidSource(const fs::path &sourcepath) const { bool valid = false; switch (GetFileExtType(sourcepath)) { diff --git a/buildcc/lib/target/src/target/tasks.cpp b/buildcc/lib/target/src/target/tasks.cpp index dfe7cfca..0e530891 100644 --- a/buildcc/lib/target/src/target/tasks.cpp +++ b/buildcc/lib/target/src/target/tasks.cpp @@ -29,6 +29,8 @@ namespace buildcc::base { void Target::CompileTargetTask(std::vector &&compile_sources, std::vector &&dummy_compile_sources) { + env::log_trace(name_, __FUNCTION__); + compile_task_ = tf_.emplace([this, compile_sources, dummy_compile_sources](tf::Subflow &subflow) { diff --git a/buildcc/targets/include/targets/target_gcc.h b/buildcc/targets/include/targets/target_gcc.h index 7cd2ca1e..b397ea66 100644 --- a/buildcc/targets/include/targets/target_gcc.h +++ b/buildcc/targets/include/targets/target_gcc.h @@ -30,16 +30,13 @@ constexpr std::string_view kGccDynamicLibExt = ".so"; constexpr std::string_view kGccPrefixIncludeDir = "-I"; constexpr std::string_view kGccPrefixLibDir = "-L"; constexpr std::string_view kGccGenericCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} -o " - "{output} -c {input}"; + "{compiler} {preprocessor_flags} {include_dirs} {common_compile_flags} " + "{compile_flags} -o {output} -c {input}"; constexpr std::string_view kGccExecutableLinkCommand = "{cpp_compiler} {link_flags} {compiled_sources} -o {output} " "{lib_dirs} {lib_deps}"; constexpr std::string_view kGccStaticLibLinkCommand = "{archiver} rcs {output} {compiled_sources}"; -constexpr std::string_view kGccDynamicLibCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " - "-fpic -o {output} -c {input}"; constexpr std::string_view kGccDynamicLibLinkCommand = "{cpp_compiler} -shared {link_flags} {compiled_sources} -o {output}"; @@ -70,8 +67,8 @@ class DynamicTarget_gcc : public base::Target { : Target(name, base::TargetType::DynamicLibrary, toolchain, target_path_relative_to_root) { target_ext_ = kGccDynamicLibExt; - compile_command_ = kGccDynamicLibCompileCommand; link_command_ = kGccDynamicLibLinkCommand; + AddCommonCompileFlag("-fpic"); } }; diff --git a/buildcc/targets/include/targets/target_generic.h b/buildcc/targets/include/targets/target_generic.h index 1cf709b6..b8e956a6 100644 --- a/buildcc/targets/include/targets/target_generic.h +++ b/buildcc/targets/include/targets/target_generic.h @@ -26,6 +26,7 @@ namespace buildcc { struct SyncTargetOptions { bool preprocessor_flags_{false}; + bool common_compile_flags_{false}; bool c_compile_flags_{false}; bool cpp_compile_flags_{false}; bool link_flags_{false}; @@ -46,6 +47,12 @@ inline void SyncTargets(base::Target &dest, const base::Target &source, } } + if (options.common_compile_flags_) { + for (const auto &flag : source.GetCurrentCommonCompileFlags()) { + dest.AddCommonCompileFlag(flag); + } + } + if (options.c_compile_flags_) { for (const auto &flag : source.GetCurrentCCompileFlags()) { dest.AddCCompileFlag(flag); @@ -90,6 +97,7 @@ class ExecutableTarget_generic : public base::Target { } SyncTargets(*this, *target, { + .common_compile_flags_ = true, .c_compile_flags_ = true, .cpp_compile_flags_ = true, .link_flags_ = true, @@ -123,6 +131,7 @@ class StaticTarget_generic : public base::Target { } SyncTargets(*this, *target, { + .common_compile_flags_ = true, .c_compile_flags_ = true, .cpp_compile_flags_ = true, .link_flags_ = true, @@ -155,6 +164,7 @@ class DynamicTarget_generic : public base::Target { } SyncTargets(*this, *target, { + .common_compile_flags_ = true, .c_compile_flags_ = true, .cpp_compile_flags_ = true, .link_flags_ = true, @@ -188,6 +198,7 @@ class Target_generic : public base::Target { } SyncTargets(*this, *target, { + .common_compile_flags_ = true, .c_compile_flags_ = true, .cpp_compile_flags_ = true, .link_flags_ = true, diff --git a/buildcc/targets/include/targets/target_msvc.h b/buildcc/targets/include/targets/target_msvc.h index 6312a6ba..d0e1d53f 100644 --- a/buildcc/targets/include/targets/target_msvc.h +++ b/buildcc/targets/include/targets/target_msvc.h @@ -36,8 +36,8 @@ constexpr std::string_view kMsvcPrefixIncludeDir = "/I"; constexpr std::string_view kMsvcPrefixLibDir = "/LIBPATH:"; // TODO, Split this into individual CompileCommands if any differences occur constexpr std::string_view kMsvcCompileCommand = - "{compiler} {preprocessor_flags} {include_dirs} {compile_flags} " - "/Fo{output} /c {input}"; + "{compiler} {preprocessor_flags} {include_dirs} {common_compile_flags} " + "{compile_flags} /Fo{output} /c {input}"; constexpr std::string_view kMsvcExecutableLinkCommand = "{linker} {link_flags} {lib_dirs} /OUT:{output} {lib_deps} " "{compiled_sources}";