From 12cf2b41a2f065406c6df8ecbc8165fd6ff69fd4 Mon Sep 17 00:00:00 2001 From: vector-of-bool Date: Sat, 12 Oct 2019 22:38:29 -0600 Subject: [PATCH] Compiler warning support --- src/dds/build.cpp | 5 +++++ src/dds/build.hpp | 1 + src/dds/ddslim.main.cpp | 6 ++++++ src/dds/toolchain.cpp | 15 +++++++++++---- src/dds/toolchain.hpp | 19 +++++++++++-------- src/dds/util.hpp | 5 +++++ 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/dds/build.cpp b/src/dds/build.cpp index 376e5dae..18ab76ab 100644 --- a/src/dds/build.cpp +++ b/src/dds/build.cpp @@ -111,6 +111,7 @@ fs::path compile_file(fs::path src_path, spdlog::info("Compile file: {}", fs::relative(src_path, params.root).string()); compile_file_spec spec{src_path, obj_path}; + spec.enable_warnings = params.enable_warnings; spec.include_dirs.push_back(params.root / "src"); spec.include_dirs.push_back(params.root / "include"); @@ -135,6 +136,10 @@ fs::path compile_file(fs::path src_path, throw compile_failure("Compilation failed."); } + if (!compile_res.output.empty()) { + spdlog::warn("While compiling file {}:\n{}", spec.source_path.string(), compile_res.output); + } + return obj_path; } diff --git a/src/dds/build.hpp b/src/dds/build.hpp index 280b803c..24ae0d5a 100644 --- a/src/dds/build.hpp +++ b/src/dds/build.hpp @@ -15,6 +15,7 @@ struct build_params { std::string export_name; bool do_export = false; bool build_tests = false; + bool enable_warnings = false; }; void build(const build_params&, const library_manifest& man); diff --git a/src/dds/ddslim.main.cpp b/src/dds/ddslim.main.cpp index 0bf0063d..2ed48b72 100644 --- a/src/dds/ddslim.main.cpp +++ b/src/dds/ddslim.main.cpp @@ -52,6 +52,11 @@ struct cli_build { args::Flag build_tests{cmd, "build_tests", "Build the tests", {"tests"}}; args::Flag export_{cmd, "export_dir", "Generate a library export", {"export", 'E'}}; + args::Flag enable_warnings{cmd, + "enable_warnings", + "Enable compiler warnings", + {"--warnings", 'W'}}; + int run() { dds::build_params params; params.root = lib_dir.Get(); @@ -60,6 +65,7 @@ struct cli_build { params.export_name = export_name.Get(); params.do_export = export_.Get(); params.build_tests = build_tests.Get(); + params.enable_warnings = enable_warnings.Get(); dds::library_manifest man; const auto man_filepath = params.root / "manifest.dds"; if (exists(man_filepath)) { diff --git a/src/dds/toolchain.cpp b/src/dds/toolchain.cpp index 587302bf..e7fe61de 100644 --- a/src/dds/toolchain.cpp +++ b/src/dds/toolchain.cpp @@ -29,6 +29,7 @@ toolchain toolchain::load_from_file(fs::path p) { opt_string c_compile_template; opt_string cxx_compile_template; opt_string create_archive_template; + opt_string warning_flags; opt_string archive_suffix; @@ -64,6 +65,7 @@ toolchain toolchain::load_from_file(fs::path p) { || try_single("Compile-C++-Template", cxx_compile_template) || try_single("Create-Archive-Template", create_archive_template) || try_single("Archive-Suffix", archive_suffix) + || try_single("Warning-Flags", warning_flags) || false; // clang-format on @@ -88,6 +90,7 @@ toolchain toolchain::load_from_file(fs::path p) { def_template.value(), create_archive_template.value(), archive_suffix.value(), + warning_flags.value_or(""), }; } @@ -183,18 +186,22 @@ vector toolchain::create_compile_command(const compile_file_spec& spec) for (auto&& inc_dir : spec.include_dirs) { auto inc_args = include_args(inc_dir); - flags.insert(flags.end(), inc_args.begin(), inc_args.end()); + extend(flags, inc_args); } for (auto&& def : spec.definitions) { auto def_args = definition_args(def); - flags.insert(flags.end(), def_args.begin(), def_args.end()); + extend(flags, def_args); + } + + if (spec.enable_warnings) { + extend(flags, _warning_flags); } vector command; for (auto arg : _cxx_compile) { if (arg == "") { - command.insert(command.end(), flags.begin(), flags.end()); + extend(command, flags); } else { arg = replace(arg, "", spec.source_path.string()); arg = replace(arg, "", spec.out_path.string()); @@ -208,7 +215,7 @@ vector toolchain::create_archive_command(const archive_spec& spec) const vector cmd; for (auto& arg : _archive_template) { if (arg == "") { - cmd.insert(cmd.end(), spec.input_files.begin(), spec.input_files.end()); + extend(cmd, spec.input_files); } else { cmd.push_back(replace(arg, "", spec.out_path.string())); } diff --git a/src/dds/toolchain.hpp b/src/dds/toolchain.hpp index 60802fa0..78e6caa3 100644 --- a/src/dds/toolchain.hpp +++ b/src/dds/toolchain.hpp @@ -22,6 +22,7 @@ struct compile_file_spec { std::vector definitions = {}; std::vector include_dirs = {}; language lang = language::automatic; + bool enable_warnings = false; }; struct archive_spec { @@ -32,13 +33,13 @@ struct archive_spec { class toolchain { using string_seq = std::vector; - string_seq _c_compile; - string_seq _cxx_compile; - string_seq _inc_template; - string_seq _def_template; - string_seq _archive_template; - + string_seq _c_compile; + string_seq _cxx_compile; + string_seq _inc_template; + string_seq _def_template; + string_seq _archive_template; std::string _archive_suffix; + string_seq _warning_flags; public: toolchain(const std::string& c_compile, @@ -46,13 +47,15 @@ class toolchain { const std::string& inc_template, const std::string& def_template, const std::string& archive_template, - const std::string& archive_suffix) + const std::string& archive_suffix, + const std::string& warning_flags) : _c_compile(split_shell_string(c_compile)) , _cxx_compile(split_shell_string(cxx_compile)) , _inc_template(split_shell_string(inc_template)) , _def_template(split_shell_string(def_template)) , _archive_template(split_shell_string(archive_template)) - , _archive_suffix(archive_suffix) {} + , _archive_suffix(archive_suffix) + , _warning_flags(split_shell_string(warning_flags)) {} static toolchain load_from_file(fs::path); diff --git a/src/dds/util.hpp b/src/dds/util.hpp index 64091f69..53f21b1b 100644 --- a/src/dds/util.hpp +++ b/src/dds/util.hpp @@ -41,6 +41,11 @@ void erase_if(Container& c, Predicate&& p) { c.erase(erase_point, c.end()); } +template +void extend(Container& c, const Other& o) { + c.insert(c.end(), o.begin(), o.end()); +} + } // namespace dds #endif // DDS_UTIL_HPP_INCLUDED \ No newline at end of file