Skip to content

Commit

Permalink
🧹 gucc: move some functions into library
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jun 25, 2024
1 parent 70773e8 commit 6a20590
Show file tree
Hide file tree
Showing 29 changed files with 431 additions and 275 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ add_executable(${PROJECT_NAME}
src/definitions.hpp
src/config.cpp src/config.hpp
src/utils.cpp src/utils.hpp
src/cpu.cpp src/cpu.hpp
src/pacmanconf_repo.cpp src/pacmanconf_repo.hpp
src/initcpio.cpp src/initcpio.hpp
src/chwd_profiles.cpp src/chwd_profiles.hpp
src/disk.cpp src/disk.hpp
src/drivers.cpp src/drivers.hpp
Expand Down Expand Up @@ -135,13 +132,15 @@ target_link_libraries(screen PRIVATE range-v3::range-v3)
target_link_libraries(dom PRIVATE range-v3::range-v3)
target_link_libraries(component PRIVATE range-v3::range-v3)

include_directories(${CMAKE_SOURCE_DIR}/src ${RAPIDJSON_INCLUDE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/gucc/src ${RAPIDJSON_INCLUDE_DIR})

add_subdirectory(gucc)

if(COS_INSTALLER_BUILD_TESTS)
add_subdirectory(tests)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::screen ftxui::dom ftxui::component range-v3::range-v3 ctre::ctre tomlplusplus::tomlplusplus)
target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options spdlog::spdlog fmt::fmt ftxui::screen ftxui::dom ftxui::component range-v3::range-v3 ctre::ctre tomlplusplus::tomlplusplus gucc::gucc)
if(NOT ENABLE_DEVENV)
target_link_libraries(${PROJECT_NAME} PRIVATE cpr::cpr)
endif()
Expand Down
12 changes: 10 additions & 2 deletions cmake/StandardProjectSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE)
set(FTXUI_BUILD_DOCS OFF CACHE INTERNAL "" FORCE)
set(FTXUI_BUILD_EXAMPLES OFF CACHE INTERNAL "" FORCE)
set(FTXUI_ENABLE_INSTALL OFF CACHE INTERNAL "" FORCE)
Expand All @@ -30,6 +29,9 @@ set(CPR_USE_SYSTEM_CURL ON CACHE INTERNAL "" FORCE)
# Generate compile_commands.json to make it easier to work with clang based tools
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Build with PIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(CMAKE_C_COMPILER_ID MATCHES ".*Clang")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto=thin -fwhole-program-vtables")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto=thin -fwhole-program-vtables")
Expand All @@ -42,7 +44,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto -fwhole-program -fuse-linker-plugin")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")# -static")

if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
#add_compile_options(-nostdlib++ -stdlib=libc++ -nodefaultlibs -fexperimental-library)
Expand Down Expand Up @@ -80,6 +81,13 @@ else()
message(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
endif()

# Builds as statically linked
option(COS_BUILD_STATIC "Build all static" OFF)
if(COS_BUILD_STATIC)
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")# -static")
endif()

# Enables STL container checker if not building a release.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-D_GLIBCXX_ASSERTIONS)
Expand Down
28 changes: 28 additions & 0 deletions gucc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.6)

##
## PROJECT
## name and version
##
project(gucc
VERSION 0.0.1
LANGUAGES CXX)

add_library(${PROJECT_NAME} SHARED
#src/utils.cpp src/utils.hpp
src/string_utils.cpp src/string_utils.hpp
src/file_utils.cpp src/file_utils.hpp
src/cpu.cpp src/cpu.hpp
src/pacmanconf_repo.cpp src/pacmanconf_repo.hpp
src/initcpio.cpp src/initcpio.hpp
#src/chwd_profiles.cpp src/chwd_profiles.hpp
#src/disk.cpp src/disk.hpp
)

add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_DIR}/src)
target_link_libraries(${PROJECT_NAME} PUBLIC project_warnings project_options spdlog::spdlog fmt::fmt range-v3::range-v3)

if(COS_INSTALLER_BUILD_TESTS)
add_subdirectory(tests)
endif()
1 change: 1 addition & 0 deletions gucc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# what
4 changes: 2 additions & 2 deletions src/cpu.cpp → gucc/src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <spdlog/spdlog.h>

namespace utils {
namespace gucc::cpu {

/* clang-format off */
namespace {
Expand Down Expand Up @@ -145,4 +145,4 @@ auto get_isa_levels() noexcept -> std::vector<std::string> {
return supported_isa_levels;
}

} // namespace utils
} // namespace gucc::cpu
4 changes: 2 additions & 2 deletions src/cpu.hpp → gucc/src/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <string> // for string
#include <vector> // for vector

namespace utils {
namespace gucc::cpu {

auto get_isa_levels() noexcept -> std::vector<std::string>;

} // namespace utils
} // namespace gucc::cpu

#endif // CPU_HPP
48 changes: 48 additions & 0 deletions gucc/src/file_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "file_utils.hpp"

#include <cerrno> // for errno, strerror
#include <cstdio> // for feof, fgets, pclose, perror, popen
#include <cstdlib> // for exit, WIFEXITED, WIFSIGNALED

#include <fstream> // for ofstream

#include <spdlog/spdlog.h>

namespace gucc::file_utils {

auto read_whole_file(const std::string_view& filepath) noexcept -> std::string {
// Use std::fopen because it's faster than std::ifstream
auto* file = std::fopen(filepath.data(), "rb");
if (file == nullptr) {
spdlog::error("[READWHOLEFILE] '{}' read failed: {}", filepath, std::strerror(errno));
return {};
}

std::fseek(file, 0u, SEEK_END);
const auto size = static_cast<std::size_t>(std::ftell(file));
std::fseek(file, 0u, SEEK_SET);

std::string buf;
buf.resize(size);

const std::size_t read = std::fread(buf.data(), sizeof(char), size, file);
if (read != size) {
spdlog::error("[READWHOLEFILE] '{}' read failed: {}", filepath, std::strerror(errno));
return {};
}
std::fclose(file);

return buf;
}

bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept {
std::ofstream file{filepath.data()};
if (!file.is_open()) {
spdlog::error("[WRITE_TO_FILE] '{}' open failed: {}", filepath, std::strerror(errno));
return false;
}
file << data;
return true;
}

} // namespace gucc::file_utils
14 changes: 14 additions & 0 deletions gucc/src/file_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef FILE_UTILS_HPP
#define FILE_UTILS_HPP

#include <string> // for string
#include <string_view> // for string_view

namespace gucc::file_utils {

auto read_whole_file(const std::string_view& filepath) noexcept -> std::string;
bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept;

} // namespace gucc::file_utils

#endif // FILE_UTILS_HPP
12 changes: 7 additions & 5 deletions src/initcpio.cpp → gucc/src/initcpio.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "initcpio.hpp"
#include "utils.hpp"
#include "file_utils.hpp"
#include "string_utils.hpp"

#include <fstream> // for ofstream

#include <fmt/compile.h>
#include <fmt/format.h>
#include <spdlog/spdlog.h>

Expand All @@ -27,10 +29,10 @@
#pragma GCC diagnostic pop
#endif

namespace detail {
namespace gucc::detail {

bool Initcpio::write() const noexcept {
auto&& file_content = utils::read_whole_file(m_file_path);
auto&& file_content = file_utils::read_whole_file(m_file_path);
if (file_content.empty()) {
spdlog::error("[INITCPIO] '{}' error occurred!", m_file_path);
return false;
Expand Down Expand Up @@ -69,7 +71,7 @@ bool Initcpio::write() const noexcept {
}

bool Initcpio::parse_file() noexcept {
auto&& file_content = utils::read_whole_file(m_file_path);
auto&& file_content = file_utils::read_whole_file(m_file_path);
if (file_content.empty()) {
spdlog::error("[INITCPIO] '{}' error occurred!", m_file_path);
return false;
Expand Down Expand Up @@ -101,4 +103,4 @@ bool Initcpio::parse_file() noexcept {
return true;
}

} // namespace detail
} // namespace gucc::detail
6 changes: 3 additions & 3 deletions src/initcpio.hpp → gucc/src/initcpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#pragma GCC diagnostic pop
#endif

namespace detail {
namespace gucc::detail {

class Initcpio {
class Initcpio final {
public:
explicit Initcpio(const std::string_view& file_path) noexcept : m_file_path(file_path) { }

Expand Down Expand Up @@ -127,6 +127,6 @@ class Initcpio {
std::string_view m_file_path{};
};

} // namespace detail
} // namespace gucc::detail

#endif // INITCPIO_HPP
10 changes: 5 additions & 5 deletions src/pacmanconf_repo.cpp → gucc/src/pacmanconf_repo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "pacmanconf_repo.hpp"
#include "utils.hpp"
#include "file_utils.hpp"

#include <fstream> // for ofstream
#include <string> // for string
Expand Down Expand Up @@ -28,11 +28,11 @@
#pragma GCC diagnostic pop
#endif

namespace detail::pacmanconf {
namespace gucc::detail::pacmanconf {

bool push_repos_front(std::string_view file_path, std::string_view value) noexcept {
using StringVec = std::vector<std::string>;
auto&& file_content = utils::read_whole_file(file_path);
auto&& file_content = file_utils::read_whole_file(file_path);
if (file_content.empty()) {
spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path);
return false;
Expand Down Expand Up @@ -62,7 +62,7 @@ bool push_repos_front(std::string_view file_path, std::string_view value) noexce
}

auto get_repo_list(std::string_view file_path) noexcept -> std::vector<std::string> {
auto&& file_content = utils::read_whole_file(file_path);
auto&& file_content = file_utils::read_whole_file(file_path);
if (file_content.empty()) {
spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path);
return {};
Expand All @@ -75,4 +75,4 @@ auto get_repo_list(std::string_view file_path) noexcept -> std::vector<std::stri
| ranges::to<std::vector<std::string>>();
}

} // namespace detail::pacmanconf
} // namespace gucc::detail::pacmanconf
4 changes: 2 additions & 2 deletions src/pacmanconf_repo.hpp → gucc/src/pacmanconf_repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <string_view> // for string_view
#include <vector> // for vector

namespace detail::pacmanconf {
namespace gucc::detail::pacmanconf {
bool push_repos_front(std::string_view file_path, std::string_view value) noexcept;
auto get_repo_list(std::string_view file_path) noexcept -> std::vector<std::string>;
} // namespace detail::pacmanconf
} // namespace gucc::detail::pacmanconf

#endif // PACMANCONF_REPO_HPP
61 changes: 61 additions & 0 deletions gucc/src/string_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "string_utils.hpp"

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuseless-cast"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif

#include <range/v3/algorithm/for_each.hpp>
#include <range/v3/algorithm/reverse.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>

#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

namespace gucc::utils {

auto make_multiline(std::string_view str, bool reverse, char delim) noexcept -> std::vector<std::string> {
std::vector<std::string> lines{};
ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); });
if (reverse) {
ranges::reverse(lines);
}
return lines;
}

auto make_multiline_view(std::string_view str, bool reverse, char delim) noexcept -> std::vector<std::string_view> {
std::vector<std::string_view> lines{};
ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); });
if (reverse) {
ranges::reverse(lines);
}
return lines;
}

auto make_multiline(const std::vector<std::string>& multiline, bool reverse, std::string_view delim) noexcept -> std::string {
std::string res{};
for (const auto& line : multiline) {
res += line;
res += delim.data();
}

if (reverse) {
ranges::reverse(res);
}

return res;
}

auto join(const std::vector<std::string>& lines, std::string_view delim) noexcept -> std::string {
return lines | ranges::views::join(delim) | ranges::to<std::string>();
}

} // namespace gucc::utils
Loading

0 comments on commit 6a20590

Please sign in to comment.