From f7d464842abea8d8d7fc4fc8fbdf4ba160ac47ee Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Mon, 3 Jan 2022 01:45:54 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20fix=20compilation=20with=20clang?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 +++--- cmake/CompilerWarnings.cmake | 13 +++++++++ cmake/Linker.cmake | 2 +- meson.build | 28 ++++++++++++++++--- src/config.hpp | 2 +- src/follow_process_log.cpp | 12 ++++++-- src/screen_service.hpp | 2 +- src/test_proccess_tailing.cpp | 2 +- src/tui.cpp | 2 +- src/utils.cpp | 11 ++++---- subprojects/packagefiles/simdjson/meson.build | 6 ++-- 11 files changed, 64 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51b3f98..53254ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,8 +103,8 @@ add_executable(test-exec-interactive ) add_executable(test-process-tailing - src/config.cpp - src/utils.cpp + src/config.cpp src/config.hpp + src/utils.cpp src/utils.hpp src/widgets.cpp src/widgets.hpp src/follow_process_log.hpp src/follow_process_log.cpp src/tui.cpp src/tui.hpp @@ -124,8 +124,8 @@ enable_sanitizers(project_options) include_directories(${CMAKE_SOURCE_DIR}/src) target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::screen ftxui::dom ftxui::component simdjson::simdjson cpr::cpr PkgConfig::GLIBMM) -target_link_libraries(test-exec-interactive PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component cpr::cpr PkgConfig::GLIBMM) -target_link_libraries(test-process-tailing PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component cpr::cpr PkgConfig::GLIBMM) +target_link_libraries(test-exec-interactive PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component simdjson::simdjson cpr::cpr PkgConfig::GLIBMM) +target_link_libraries(test-process-tailing PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::component simdjson::simdjson cpr::cpr PkgConfig::GLIBMM) if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") target_link_libraries(${PROJECT_NAME} PRIVATE range-v3::range-v3) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index efbaa3a..b16a561 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -58,6 +58,19 @@ function(set_project_warnings project_name) -Wduplicated-branches # warn if if / else branches have duplicated code -Wlogical-op # warn about logical operations being used where bitwise were probably wanted -Wuseless-cast # warn if you perform a cast to the same type + + -Wsuggest-attribute=cold + -Wsuggest-attribute=format + -Wsuggest-attribute=malloc + -Wsuggest-attribute=noreturn + -Wsuggest-attribute=pure + -Wsuggest-final-methods + -Wsuggest-final-types + -Wdiv-by-zero + -Wanalyzer-double-fclose + -Wanalyzer-double-free + -Wanalyzer-malloc-leak + -Wanalyzer-use-after-free ) if(MSVC) diff --git a/cmake/Linker.cmake b/cmake/Linker.cmake index e3de921..8fc1757 100644 --- a/cmake/Linker.cmake +++ b/cmake/Linker.cmake @@ -5,7 +5,7 @@ include(CheckCXXCompilerFlag) set(USER_LINKER_OPTION "lld" CACHE STRING "Linker to be used") -set(USER_LINKER_OPTION_VALUES "lld" "gold" "bfd") +set(USER_LINKER_OPTION_VALUES "lld" "gold" "bfd" "mold") set_property(CACHE USER_LINKER_OPTION PROPERTY STRINGS ${USER_LINKER_OPTION_VALUES}) list( FIND diff --git a/meson.build b/meson.build index d14fcdd..bef2047 100644 --- a/meson.build +++ b/meson.build @@ -14,6 +14,7 @@ cc = meson.get_compiler('cpp') if cc.get_id() == 'clang' specific_cc_flags = [ '-nostdlib++', + #'-stdlib=libc++', '-nodefaultlibs', ] specific_link_flags = [ @@ -32,7 +33,7 @@ if not is_dev_environment endif # Common dependencies -spdlog = dependency('spdlog', version : ['>=1.9.2']) +spdlog = dependency('spdlog', version : ['>=1.9.2'], fallback : ['spdlog', 'spdlog_dep']) fmt = dependency('fmt', version : ['>=8.0.0'], fallback : ['fmt', 'fmt_dep']) ftxui = dependency('ftxui', modules : ['ftxui::screen', 'ftxui::dom', 'ftxui::component'], fallback : ['ftxui', 'ftxui_dep']) simdjson = dependency('simdjson', version : ['>=1.0.2'], fallback : ['simdjson', 'simdjson_dep']) @@ -80,6 +81,19 @@ if cc.get_id() == 'gcc' '-Wduplicated-branches', '-Wlogical-op', '-Wuseless-cast', + + '-Wsuggest-attribute=cold', + '-Wsuggest-attribute=format', + '-Wsuggest-attribute=malloc', + '-Wsuggest-attribute=noreturn', + '-Wsuggest-attribute=pure', + '-Wsuggest-final-methods', + '-Wsuggest-final-types', + '-Wdiv-by-zero', + '-Wanalyzer-double-fclose', + '-Wanalyzer-double-free', + '-Wanalyzer-malloc-leak', + '-Wanalyzer-use-after-free', ] endif @@ -102,24 +116,30 @@ endif add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : 'cpp') +deps = [fmt, spdlog, ftxui, simdjson, cpr, glibmm] +if cc.get_id() == 'clang' + ranges = dependency('range-v3', version : ['>=0.11.0']) + deps += [ranges] +endif + executable( 'cachyos-installer', src_files, - dependencies: [fmt, spdlog, ftxui, simdjson, cpr, glibmm], + dependencies: deps, include_directories: [include_directories('src')], install: true) executable( 'test-exec-interactive', files('src/config.cpp', 'src/tui.cpp', 'src/utils.cpp', 'src/main_test.cpp'), - dependencies: [fmt, spdlog, ftxui, simdjson, cpr, glibmm], + dependencies: deps, include_directories: [include_directories('src')], install: false) executable( 'test-process-tailing', files('src/config.cpp', 'src/widgets.cpp', 'src/tui.cpp', 'src/utils.cpp', 'src/follow_process_log.cpp','src/test_proccess_tailing.cpp'), - dependencies: [fmt, spdlog, ftxui, simdjson, cpr, glibmm], + dependencies: deps, include_directories: [include_directories('src')], install: false) diff --git a/src/config.hpp b/src/config.hpp index 2ea0a9a..ba2f2d9 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -18,7 +18,7 @@ class Config final { virtual ~Config() noexcept = default; static bool initialize() noexcept; - static Config* instance(); + [[gnu::pure]] static Config* instance(); /* clang-format off */ diff --git a/src/follow_process_log.cpp b/src/follow_process_log.cpp index 8b074ab..bdd7638 100644 --- a/src/follow_process_log.cpp +++ b/src/follow_process_log.cpp @@ -30,10 +30,10 @@ void follow_process_log_widget(const std::vector& vec, Decorator bo std::this_thread::sleep_for(0.05s); } }; - std::jthread t(execute_thread); + std::thread t(execute_thread); auto screen = ScreenInteractive::Fullscreen(); - std::jthread refresh_ui([&] { + std::thread refresh_ui([&] { while (running) { std::this_thread::sleep_for(0.05s); screen.PostEvent(Event::Custom); @@ -63,11 +63,17 @@ void follow_process_log_widget(const std::vector& vec, Decorator bo auto container = Container::Horizontal({button_back}); auto renderer = Renderer(container, [&] { - return tui::detail::centered_widget(container, "New CLI Installer", tui::detail::multiline_text(utils::make_multiline(process_log)) | boxsize | vscroll_indicator | yframe | flex); + return tui::detail::centered_widget(container, "New CLI Installer", tui::detail::multiline_text(utils::make_multiline(process_log, true)) | boxsize | vscroll_indicator | yframe | flex); }); screen.Loop(renderer); running = false; + if (refresh_ui.joinable()) { + refresh_ui.join(); + } + if (t.joinable()) { + t.join(); + } } } // namespace tui::detail diff --git a/src/screen_service.hpp b/src/screen_service.hpp index 34971fe..d668bce 100644 --- a/src/screen_service.hpp +++ b/src/screen_service.hpp @@ -20,7 +20,7 @@ class screen_service final { virtual ~screen_service() noexcept = default; static bool initialize() noexcept; - static screen_service* instance(); + [[gnu::pure]] static screen_service* instance(); /* clang-format off */ diff --git a/src/test_proccess_tailing.cpp b/src/test_proccess_tailing.cpp index c707986..4d05955 100644 --- a/src/test_proccess_tailing.cpp +++ b/src/test_proccess_tailing.cpp @@ -1,5 +1,5 @@ #include "follow_process_log.hpp" int main() { - tui::detail::follow_process_log_widget({"/bin/sh", "-c", "sleep 1 && echo \"I slept\""}); + tui::detail::follow_process_log_widget({"/bin/sh", "-c", "sleep 1 && echo \"I slept\" && sleep 1 && echo \"I slept twice\""}); } diff --git a/src/tui.cpp b/src/tui.cpp index 6527030..8bd2504 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -751,7 +751,7 @@ void install_base() noexcept { // utils::exec(fmt::format("pacstrap {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)); detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)}); - std::filesystem::copy("/etc/pacman.conf", fmt::format("{}/etc/pacman.conf", mountpoint)); + std::filesystem::copy_file("/etc/pacman.conf", fmt::format("{}/etc/pacman.conf", mountpoint), fs::copy_options::overwrite_existing); #endif std::ofstream{base_installed}; } diff --git a/src/utils.cpp b/src/utils.cpp index c1e2558..d81492f 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -8,6 +8,7 @@ #include // for transform #include // for array +#include // for bit_cast #include // for filesystem, seconds #include // for int32_t #include // for feof, fgets, pclose, perror, popen @@ -49,6 +50,7 @@ namespace ranges = std::ranges; #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wsign-conversion" +#pragma clang diagnostic ignored "-Wambiguous-reversed-operator" #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wsign-conversion" @@ -257,12 +259,11 @@ auto make_multiline(std::vector& multiline, bool reverse, const std // install a pkg in the live session if not installed void inst_needed(const std::string_view& pkg) { - const auto& pkg_info = utils::exec(fmt::format("pacman -Q {}", pkg)); - const std::regex pkg_regex("/error/"); - if (!std::regex_search(pkg_info, pkg_regex)) { - std::this_thread::sleep_for(std::chrono::seconds(2)); + if (utils::exec(fmt::format("pacman -Q {}", pkg), true) != "0") { + std::this_thread::sleep_for(std::chrono::seconds(1)); utils::clear_screen(); - utils::exec(fmt::format("pacman -Sy --noconfirm {}", pkg)); + tui::detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacman -Sy --noconfirm {}", pkg)}); + // utils::exec(fmt::format("pacman -Sy --noconfirm {}", pkg)); } } diff --git a/subprojects/packagefiles/simdjson/meson.build b/subprojects/packagefiles/simdjson/meson.build index f231694..2dad42d 100644 --- a/subprojects/packagefiles/simdjson/meson.build +++ b/subprojects/packagefiles/simdjson/meson.build @@ -9,17 +9,17 @@ project('simdjson', 'cpp', simdjson_lib = library('simdjson', sources : ['singleheader/simdjson.cpp'], - cpp_args : '-DSIMDJSON_THREADS_ENABLED=1', + cpp_args : ['-DSIMDJSON_THREADS_ENABLED=1', '-Wno-ambiguous-reversed-operator'], include_directories: include_directories('singleheader') ) simdjson_dep = declare_dependency( include_directories: include_directories('singleheader'), - compile_args : '-DSIMDJSON_THREADS_ENABLED=1', + compile_args : ['-DSIMDJSON_THREADS_ENABLED=1', '-Wno-ambiguous-reversed-operator'], link_with : simdjson_lib ) simdjson_multiple_headers = declare_dependency( include_directories: include_directories('include'), - compile_args : '-DSIMDJSON_THREADS_ENABLED=1' + compile_args : ['-DSIMDJSON_THREADS_ENABLED=1', '-Wno-ambiguous-reversed-operator'], )