From 9c6ca2ddf94756fcc91e34bac84c735dcbdbfbe4 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sat, 12 Feb 2022 04:38:15 +0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20=20update=20dependencies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 56 +- cmake/CPM.cmake | 1027 +++++++++++++++++++++++++++ cmake/CompilerWarnings.cmake | 4 +- cmake/StandardProjectSettings.cmake | 7 + meson.build | 2 + src/crypto.cpp | 14 +- src/disk.cpp | 52 ++ src/disk.hpp | 16 + src/tui.cpp | 332 ++++----- src/utils.cpp | 56 +- src/utils.hpp | 2 + subprojects/fmt.wrap | 2 +- 12 files changed, 1346 insertions(+), 224 deletions(-) create mode 100644 cmake/CPM.cmake create mode 100644 src/disk.cpp create mode 100644 src/disk.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 02d10ed..104f0bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ include(EnableCcache) include(Linker) include(StaticAnalyzers) include(Sanitizers) -include(FetchContent) +include(CPM) find_package(PkgConfig REQUIRED) pkg_check_modules( @@ -35,34 +35,43 @@ pkg_check_modules( IMPORTED_TARGET glibmm-2.4>=2.56.0) -FetchContent_Declare(ftxui - GIT_REPOSITORY "https://github.com/vnepogodin/ftxui.git" +CPMAddPackage( + NAME ftxui + GITHUB_REPOSITORY vnepogodin/ftxui GIT_TAG "c++20" + EXCLUDE_FROM_ALL YES ) -FetchContent_Declare(fmt - GIT_REPOSITORY "https://github.com/fmtlib/fmt.git" - GIT_TAG "ae1aaaee5f5f36ce5303bb140d51b6db21e1af4e" +CPMAddPackage( + NAME fmt + GITHUB_REPOSITORY fmtlib/fmt + GIT_TAG a44716f58e943905d1357160b98cae2618d053cf + EXCLUDE_FROM_ALL YES ) -FetchContent_MakeAvailable(fmt) -FetchContent_Declare(spdlog - GIT_REPOSITORY "https://github.com/gabime/spdlog.git" - GIT_TAG "8fb112158a64afe1bd58c82c06bf6a142c9b3823" +CPMAddPackage( + NAME spdlog + GITHUB_REPOSITORY gabime/spdlog + GIT_TAG 8fb112158a64afe1bd58c82c06bf6a142c9b3823 + EXCLUDE_FROM_ALL YES ) -FetchContent_Declare(simdjson - GIT_REPOSITORY "https://github.com/simdjson/simdjson.git" - GIT_TAG "6698eb96b99576b8d53f8d90c9023717379e1c0f" +CPMAddPackage( + NAME simdjson + GITHUB_REPOSITORY simdjson/simdjson + GIT_TAG 6698eb96b99576b8d53f8d90c9023717379e1c0f + EXCLUDE_FROM_ALL YES ) -FetchContent_Declare(cpr - GIT_REPOSITORY "https://github.com/libcpr/cpr.git" - GIT_TAG "da381bb2afe4e36b18b1fe206bc06951d8fdc06e" +CPMAddPackage( + NAME cpr + GITHUB_REPOSITORY libcpr/cpr + GIT_TAG da381bb2afe4e36b18b1fe206bc06951d8fdc06e + EXCLUDE_FROM_ALL YES ) -FetchContent_MakeAvailable(ftxui spdlog simdjson cpr) if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") - FetchContent_Declare(range-v3 - GIT_REPOSITORY "https://github.com/ericniebler/range-v3.git" - GIT_TAG "9aa032ccd0eb4bd77f58e5b181168f1038c222c6" + CPMAddPackage( + NAME range-v3 + GITHUB_REPOSITORY ericniebler/range-v3 + GIT_TAG 9aa032ccd0eb4bd77f58e5b181168f1038c222c6 + EXCLUDE_FROM_ALL YES ) - FetchContent_MakeAvailable(range-v3) endif() ## @@ -87,6 +96,7 @@ add_executable(${PROJECT_NAME} src/screen_service.hpp src/screen_service.cpp src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp + src/disk.cpp src/disk.hpp src/widgets.cpp src/widgets.hpp src/follow_process_log.hpp src/follow_process_log.cpp src/crypto.cpp src/crypto.hpp @@ -97,8 +107,10 @@ add_executable(${PROJECT_NAME} add_executable(test-exec-interactive src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp + src/disk.cpp src/disk.hpp src/widgets.cpp src/widgets.hpp src/follow_process_log.hpp src/follow_process_log.cpp + src/crypto.cpp src/crypto.hpp src/tui.cpp src/tui.hpp src/main_test.cpp ) @@ -106,8 +118,10 @@ add_executable(test-exec-interactive add_executable(test-process-tailing src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp + src/disk.cpp src/disk.hpp src/widgets.cpp src/widgets.hpp src/follow_process_log.hpp src/follow_process_log.cpp + src/crypto.cpp src/crypto.hpp src/tui.cpp src/tui.hpp src/test_proccess_tailing.cpp ) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..0b8254c --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,1027 @@ +# CPM.cmake - CMake's missing package manager +# =========================================== +# See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. +# +# MIT License +# ----------- +#[[ + Copyright (c) 2021 Lars Melchior and additional contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +set(CURRENT_CPM_VERSION 1.0.0-development-version) + +if(CPM_DIRECTORY) + if(NOT CPM_DIRECTORY STREQUAL CMAKE_CURRENT_LIST_DIR) + if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) + message( + AUTHOR_WARNING + "${CPM_INDENT} \ +A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ +It is recommended to upgrade CPM to the most recent version. \ +See https://github.com/cpm-cmake/CPM.cmake for more information." + ) + endif() + if(${CMAKE_VERSION} VERSION_LESS "3.17.0") + include(FetchContent) + endif() + return() + endif() + + get_property( + CPM_INITIALIZED GLOBAL "" + PROPERTY CPM_INITIALIZED + SET + ) + if(CPM_INITIALIZED) + return() + endif() +endif() + +if(CURRENT_CPM_VERSION MATCHES "development-version") + message(WARNING "Your project is using an unstable development version of CPM.cmake. \ +Please update to a recent release if possible. \ +See https://github.com/cpm-cmake/CPM.cmake for details." + ) +endif() + +set_property(GLOBAL PROPERTY CPM_INITIALIZED true) + +option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" + $ENV{CPM_USE_LOCAL_PACKAGES} +) +option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" + $ENV{CPM_LOCAL_PACKAGES_ONLY} +) +option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) +option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" + $ENV{CPM_DONT_UPDATE_MODULE_PATH} +) +option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" + $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} +) +option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK + "Add all packages added through CPM.cmake to the package lock" + $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} +) +option(CPM_USE_NAMED_CACHE_DIRECTORIES + "Use additional directory of package name in cache on the most nested level." + $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} +) + +set(CPM_VERSION + ${CURRENT_CPM_VERSION} + CACHE INTERNAL "" +) +set(CPM_DIRECTORY + ${CMAKE_CURRENT_LIST_DIR} + CACHE INTERNAL "" +) +set(CPM_FILE + ${CMAKE_CURRENT_LIST_FILE} + CACHE INTERNAL "" +) +set(CPM_PACKAGES + "" + CACHE INTERNAL "" +) +set(CPM_DRY_RUN + OFF + CACHE INTERNAL "Don't download or configure dependencies (for testing)" +) + +if(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) +else() + set(CPM_SOURCE_CACHE_DEFAULT OFF) +endif() + +set(CPM_SOURCE_CACHE + ${CPM_SOURCE_CACHE_DEFAULT} + CACHE PATH "Directory to download CPM dependencies" +) + +if(NOT CPM_DONT_UPDATE_MODULE_PATH) + set(CPM_MODULE_PATH + "${CMAKE_BINARY_DIR}/CPM_modules" + CACHE INTERNAL "" + ) + # remove old modules + file(REMOVE_RECURSE ${CPM_MODULE_PATH}) + file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) + # locally added CPM modules should override global packages + set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") +endif() + +if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + set(CPM_PACKAGE_LOCK_FILE + "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" + CACHE INTERNAL "" + ) + file(WRITE ${CPM_PACKAGE_LOCK_FILE} + "# CPM Package Lock\n# This file should be committed to version control\n\n" + ) +endif() + +include(FetchContent) + +# Try to infer package name from git repository uri (path or url) +function(cpm_package_name_from_git_uri URI RESULT) + if("${URI}" MATCHES "([^/:]+)/?.git/?$") + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + else() + unset(${RESULT} PARENT_SCOPE) + endif() +endfunction() + +# Try to infer package name and version from a url +function(cpm_package_name_and_ver_from_url url outName outVer) + if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") + # We matched an archive + set(filename "${CMAKE_MATCH_1}") + + if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") + # We matched - (ie foo-1.2.3) + set(${outName} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + set(${outVer} + "${CMAKE_MATCH_2}" + PARENT_SCOPE + ) + elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") + # We couldn't find a name, but we found a version + # + # In many cases (which we don't handle here) the url would look something like + # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly + # distinguish the package name from the irrelevant bits. Moreover if we try to match the + # package name from the filename, we'd get bogus at best. + unset(${outName} PARENT_SCOPE) + set(${outVer} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + else() + # Boldly assume that the file name is the package name. + # + # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but + # such cases should be quite rare. No popular service does this... we think. + set(${outName} + "${filename}" + PARENT_SCOPE + ) + unset(${outVer} PARENT_SCOPE) + endif() + else() + # No ideas yet what to do with non-archives + unset(${outName} PARENT_SCOPE) + unset(${outVer} PARENT_SCOPE) + endif() +endfunction() + +# Initialize logging prefix +if(NOT CPM_INDENT) + set(CPM_INDENT + "CPM:" + CACHE INTERNAL "" + ) +endif() + +function(cpm_find_package NAME VERSION) + string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") + find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) + if(${CPM_ARGS_NAME}_FOUND) + message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${VERSION}") + CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") + set(CPM_PACKAGE_FOUND + YES + PARENT_SCOPE + ) + else() + set(CPM_PACKAGE_FOUND + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from +# finding the system library +function(cpm_create_module_file Name) + if(NOT CPM_DONT_UPDATE_MODULE_PATH) + # erase any previous modules + file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake + "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" + ) + endif() +endfunction() + +# Find a package locally or fallback to CPMAddPackage +function(CPMFindPackage) + set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_DOWNLOAD_ALL) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + if(CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(NOT CPM_PACKAGE_FOUND) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + endif() + +endfunction() + +# checks if a package has been added before +function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) + if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) + CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) + if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") + message( + WARNING + "${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." + ) + endif() + cpm_get_fetch_properties(${CPM_ARGS_NAME}) + set(${CPM_ARGS_NAME}_ADDED NO) + set(CPM_PACKAGE_ALREADY_ADDED + YES + PARENT_SCOPE + ) + cpm_export_variables(${CPM_ARGS_NAME}) + else() + set(CPM_PACKAGE_ALREADY_ADDED + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of +# arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted +# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 +function(cpm_parse_add_package_single_arg arg outArgs) + # Look for a scheme + if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") + string(TOLOWER "${CMAKE_MATCH_1}" scheme) + set(uri "${CMAKE_MATCH_2}") + + # Check for CPM-specific schemes + if(scheme STREQUAL "gh") + set(out "GITHUB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "gl") + set(out "GITLAB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "bb") + set(out "BITBUCKET_REPOSITORY;${uri}") + set(packageType "git") + # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine + # type + elseif(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Fall back to a URL + set(out "URL;${arg}") + set(packageType "archive") + + # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. + # We just won't bother with the additional complexity it will induce in this function. SVN is + # done by multi-arg + endif() + else() + if(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Give up + message(FATAL_ERROR "CPM: Can't determine package type of '${arg}'") + endif() + endif() + + # For all packages we interpret @... as version. Only replace the last occurence. Thus URIs + # containing '@' can be used + string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") + + # Parse the rest according to package type + if(packageType STREQUAL "git") + # For git repos we interpret #... as a tag or branch or commit hash + string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") + elseif(packageType STREQUAL "archive") + # For archives we interpret #... as a URL hash. + string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") + # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url + # should do this at a later point + else() + # We should never get here. This is an assertion and hitting it means there's a bug in the code + # above. A packageType was set, but not handled by this if-else. + message(FATAL_ERROR "CPM: Unsupported package type '${packageType}' of '${arg}'") + endif() + + set(${outArgs} + ${out} + PARENT_SCOPE + ) +endfunction() + +# Check that the working directory for a git repo is clean +function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) + + find_package(Git REQUIRED) + + if(NOT GIT_EXECUTABLE) + # No git executable, assume directory is clean + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + # check for uncommited changes + execute_process( + COMMAND ${GIT_EXECUTABLE} status --porcelain + RESULT_VARIABLE resultGitStatus + OUTPUT_VARIABLE repoStatus + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + WORKING_DIRECTORY ${repoPath} + ) + if(resultGitStatus) + # not supposed to happen, assume clean anyway + message(WARNING "Calling git status on folder ${repoPath} failed") + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + if(NOT "${repoStatus}" STREQUAL "") + set(${isClean} + FALSE + PARENT_SCOPE + ) + return() + endif() + + # check for commited changes + execute_process( + COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} + RESULT_VARIABLE resultGitDiff + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET + WORKING_DIRECTORY ${repoPath} + ) + + if(${resultGitDiff} EQUAL 0) + set(${isClean} + TRUE + PARENT_SCOPE + ) + else() + set(${isClean} + FALSE + PARENT_SCOPE + ) + endif() + +endfunction() + +# Download and add a package from source +function(CPMAddPackage) + list(LENGTH ARGN argnLength) + if(argnLength EQUAL 1) + cpm_parse_add_package_single_arg("${ARGN}" ARGN) + + # The shorthand syntax implies EXCLUDE_FROM_ALL + set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES") + endif() + + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + DOWNLOAD_COMMAND + FIND_PACKAGE_ARGUMENTS + NO_CACHE + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + ) + + set(multiValueArgs URL OPTIONS) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + + # Set default values for arguments + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_ARGS_DOWNLOAD_ONLY) + set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) + else() + set(DOWNLOAD_ONLY NO) + endif() + + if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") + endif() + + if(DEFINED CPM_ARGS_GIT_REPOSITORY) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) + if(NOT DEFINED CPM_ARGS_GIT_TAG) + set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) + endif() + + # If a name wasn't provided, try to infer it from the git repo + if(NOT DEFINED CPM_ARGS_NAME) + cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) + endif() + endif() + + set(CPM_SKIP_FETCH FALSE) + + if(DEFINED CPM_ARGS_GIT_TAG) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) + # If GIT_SHALLOW is explicitly specified, honor the value. + if(DEFINED CPM_ARGS_GIT_SHALLOW) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) + endif() + endif() + + if(DEFINED CPM_ARGS_URL) + # If a name or version aren't provided, try to infer them from the URL + list(GET CPM_ARGS_URL 0 firstUrl) + cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) + # If we fail to obtain name and version from the first URL, we could try other URLs if any. + # However multiple URLs are expected to be quite rare, so for now we won't bother. + + # If the caller provided their own name and version, they trump the inferred ones. + if(NOT DEFINED CPM_ARGS_NAME) + set(CPM_ARGS_NAME ${nameFromUrl}) + endif() + if(NOT DEFINED CPM_ARGS_VERSION) + set(CPM_ARGS_VERSION ${verFromUrl}) + endif() + + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") + endif() + + # Check for required arguments + + if(NOT DEFINED CPM_ARGS_NAME) + message( + FATAL_ERROR + "CPM: 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" + ) + endif() + + # Check if package has been added before + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + if(CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for manual overrides + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") + set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) + set(CPM_${CPM_ARGS_NAME}_SOURCE "") + CPMAddPackage( + NAME "${CPM_ARGS_NAME}" + SOURCE_DIR "${PACKAGE_SOURCE}" + EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" + OPTIONS "${CPM_ARGS_OPTIONS}" + SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" + DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" + FORCE True + ) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for available declaration + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") + set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) + set(CPM_DECLARATION_${CPM_ARGS_NAME} "") + CPMAddPackage(${declaration}) + cpm_export_variables(${CPM_ARGS_NAME}) + # checking again to ensure version and option compatibility + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + return() + endif() + + if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(CPM_PACKAGE_FOUND) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + if(CPM_LOCAL_PACKAGES_ONLY) + message( + SEND_ERROR + "CPM: ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" + ) + endif() + endif() + + CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") + + if(DEFINED CPM_ARGS_GIT_TAG) + set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") + else() + set(PACKAGE_INFO "${CPM_ARGS_VERSION}") + endif() + + if(DEFINED FETCHCONTENT_BASE_DIR) + # respect user's FETCHCONTENT_BASE_DIR if set + set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) + else() + set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) + endif() + + if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) + list(SORT origin_parameters) + if(CPM_USE_NAMED_CACHE_DIRECTORIES) + string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) + else() + string(SHA1 origin_hash "${origin_parameters}") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) + endif() + # Expand `download_directory` relative path. This is important because EXISTS doesn't work for + # relative paths. + get_filename_component(download_directory ${download_directory} ABSOLUTE) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) + if(EXISTS ${download_directory}) + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} "${download_directory}" + "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + ) + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + + if(DEFINED CPM_ARGS_GIT_TAG) + # warn if cache has been changed since checkout + cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) + if(NOT ${IS_CLEAN}) + message(WARNING "Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty") + endif() + endif() + + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" + ) + set(CPM_SKIP_FETCH TRUE) + set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") + else() + # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but + # it should guarantee no commit hash get mis-detected. + if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) + cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) + if(NOT ${IS_HASH}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) + endif() + endif() + + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) + set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") + endif() + endif() + + cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") + + if(CPM_PACKAGE_LOCK_ENABLED) + if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) + cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + elseif(CPM_ARGS_SOURCE_DIR) + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") + else() + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + endif() + endif() + + message( + STATUS "${CPM_INDENT} adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" + ) + + if(NOT CPM_SKIP_FETCH) + cpm_declare_fetch( + "${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}" + ) + cpm_fetch_package("${CPM_ARGS_NAME}" populated) + if(${populated}) + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" + ) + endif() + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + endif() + + set(${CPM_ARGS_NAME}_ADDED YES) + cpm_export_variables("${CPM_ARGS_NAME}") +endfunction() + +# Fetch a previously declared package +macro(CPMGetPackage Name) + if(DEFINED "CPM_DECLARATION_${Name}") + CPMAddPackage(NAME ${Name}) + else() + message(SEND_ERROR "Cannot retrieve package ${Name}: no declaration available") + endif() +endmacro() + +# export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set +macro(cpm_export_variables name) + set(${name}_SOURCE_DIR + "${${name}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${name}_BINARY_DIR + "${${name}_BINARY_DIR}" + PARENT_SCOPE + ) + set(${name}_ADDED + "${${name}_ADDED}" + PARENT_SCOPE + ) +endmacro() + +# declares a package, so that any call to CPMAddPackage for the package name will use these +# arguments instead. Previous declarations will not be overriden. +macro(CPMDeclarePackage Name) + if(NOT DEFINED "CPM_DECLARATION_${Name}") + set("CPM_DECLARATION_${Name}" "${ARGN}") + endif() +endmacro() + +function(cpm_add_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") + endif() +endfunction() + +function(cpm_add_comment_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} + "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" + ) + endif() +endfunction() + +# includes the package lock file if it exists and creates a target `cpm-update-package-lock` to +# update it +macro(CPMUsePackageLock file) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) + if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + if(NOT TARGET cpm-update-package-lock) + add_custom_target( + cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} + ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} + ) + endif() + set(CPM_PACKAGE_LOCK_ENABLED true) + endif() +endmacro() + +# registers a package that has been added to CPM +function(CPMRegisterPackage PACKAGE VERSION) + list(APPEND CPM_PACKAGES ${PACKAGE}) + set(CPM_PACKAGES + ${CPM_PACKAGES} + CACHE INTERNAL "" + ) + set("CPM_PACKAGE_${PACKAGE}_VERSION" + ${VERSION} + CACHE INTERNAL "" + ) +endfunction() + +# retrieve the current version of the package to ${OUTPUT} +function(CPMGetPackageVersion PACKAGE OUTPUT) + set(${OUTPUT} + "${CPM_PACKAGE_${PACKAGE}_VERSION}" + PARENT_SCOPE + ) +endfunction() + +# declares a package in FetchContent_Declare +function(cpm_declare_fetch PACKAGE VERSION INFO) + if(${CPM_DRY_RUN}) + message(STATUS "${CPM_INDENT} package not declared (dry run)") + return() + endif() + + FetchContent_Declare(${PACKAGE} ${ARGN}) +endfunction() + +# returns properties for a package previously defined by cpm_declare_fetch +function(cpm_get_fetch_properties PACKAGE) + if(${CPM_DRY_RUN}) + return() + endif() + + set(${PACKAGE}_SOURCE_DIR + "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" + PARENT_SCOPE + ) +endfunction() + +function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) + if(${CPM_DRY_RUN}) + return() + endif() + + set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR + "${source_dir}" + CACHE INTERNAL "" + ) + set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR + "${binary_dir}" + CACHE INTERNAL "" + ) +endfunction() + +# adds a package as a subdirectory if viable, according to provided options +function( + cpm_add_subdirectory + PACKAGE + DOWNLOAD_ONLY + SOURCE_DIR + BINARY_DIR + EXCLUDE + OPTIONS +) + if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) + if(EXCLUDE) + set(addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) + else() + set(addSubdirectoryExtraArgs "") + endif() + if(OPTIONS) + # the policy allows us to change options without caching + cmake_policy(SET CMP0077 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + + # the policy allows us to change set(CACHE) without caching + if(POLICY CMP0126) + cmake_policy(SET CMP0126 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) + endif() + + foreach(OPTION ${OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + set(CPM_OLD_INDENT "${CPM_INDENT}") + set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") + add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) + set(CPM_INDENT "${CPM_OLD_INDENT}") + endif() +endfunction() + +# downloads a previously declared package via FetchContent and exports the variables +# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope +function(cpm_fetch_package PACKAGE populated) + set(${populated} + FALSE + PARENT_SCOPE + ) + if(${CPM_DRY_RUN}) + message(STATUS "${CPM_INDENT} package ${PACKAGE} not fetched (dry run)") + return() + endif() + + FetchContent_GetProperties(${PACKAGE}) + + string(TOLOWER "${PACKAGE}" lower_case_name) + + if(NOT ${lower_case_name}_POPULATED) + FetchContent_Populate(${PACKAGE}) + set(${populated} + TRUE + PARENT_SCOPE + ) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} + ) + + set(${PACKAGE}_SOURCE_DIR + ${${lower_case_name}_SOURCE_DIR} + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + ${${lower_case_name}_BINARY_DIR} + PARENT_SCOPE + ) +endfunction() + +# splits a package option +function(cpm_parse_option OPTION) + string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") + string(LENGTH "${OPTION}" OPTION_LENGTH) + string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) + if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) + # no value for key provided, assume user wants to set option to "ON" + set(OPTION_VALUE "ON") + else() + math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") + string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) + endif() + set(OPTION_KEY + "${OPTION_KEY}" + PARENT_SCOPE + ) + set(OPTION_VALUE + "${OPTION_VALUE}" + PARENT_SCOPE + ) +endfunction() + +# guesses the package version from a git tag +function(cpm_get_version_from_git_tag GIT_TAG RESULT) + string(LENGTH ${GIT_TAG} length) + if(length EQUAL 40) + # GIT_TAG is probably a git hash + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + endif() +endfunction() + +# guesses if the git tag is a commit hash or an actual tag or a branch nane. +function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) + string(LENGTH "${GIT_TAG}" length) + # full hash has 40 characters, and short hash has at least 7 characters. + if(length LESS 7 OR length GREATER 40) + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") + set(${RESULT} + 1 + PARENT_SCOPE + ) + else() + set(${RESULT} + 0 + PARENT_SCOPE + ) + endif() + endif() +endfunction() + +function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + DOWNLOAD_COMMAND + FIND_PACKAGE_ARGUMENTS + NO_CACHE + GIT_SHALLOW + ) + set(multiValueArgs OPTIONS) + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + foreach(oneArgName ${oneValueArgs}) + if(DEFINED CPM_ARGS_${oneArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + if(${oneArgName} STREQUAL "SOURCE_DIR") + string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} + ${CPM_ARGS_${oneArgName}} + ) + endif() + string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") + endif() + endforeach() + foreach(multiArgName ${multiValueArgs}) + if(DEFINED CPM_ARGS_${multiArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") + foreach(singleOption ${CPM_ARGS_${multiArgName}}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") + endforeach() + endif() + endforeach() + + if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ") + foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) + string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") + endforeach() + string(APPEND PRETTY_OUT_VAR "\n") + endif() + + set(${OUT_VAR} + ${PRETTY_OUT_VAR} + PARENT_SCOPE + ) + +endfunction() diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index b16a561..38982e9 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -64,8 +64,8 @@ function(set_project_warnings project_name) -Wsuggest-attribute=malloc -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure - -Wsuggest-final-methods - -Wsuggest-final-types + #-Wsuggest-final-methods + #-Wsuggest-final-types -Wdiv-by-zero -Wanalyzer-double-fclose -Wanalyzer-double-free diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake index d00e93c..0307e2c 100644 --- a/cmake/StandardProjectSettings.cmake +++ b/cmake/StandardProjectSettings.cmake @@ -27,6 +27,13 @@ set(SIMDJSON_DISABLE_DEPRECATED_API ON CACHE INTERNAL "" FORCE) # Generate compile_commands.json to make it easier to work with clang based tools set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto") + +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fwhole-program -fuse-linker-plugin") +endif() +#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") #-static") + option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) if(ENABLE_IPO) diff --git a/meson.build b/meson.build index d920ad9..5e4eb36 100644 --- a/meson.build +++ b/meson.build @@ -47,6 +47,7 @@ src_files = files( 'src/screen_service.hpp', 'src/screen_service.cpp', 'src/config.cpp', 'src/config.hpp', 'src/utils.cpp', 'src/utils.hpp', + 'src/disk.cpp', 'src/disk.hpp', 'src/widgets.cpp', 'src/widgets.hpp', 'src/follow_process_log.cpp', 'src/follow_process_log.hpp', 'src/crypto.cpp', 'src/crypto.hpp', @@ -103,6 +104,7 @@ if not is_debug_build possible_cc_flags += [ '-flto', '-fwhole-program', + '-fuse-linker-plugin', ] else possible_cc_flags += [ diff --git a/src/crypto.cpp b/src/crypto.cpp index 85cbebf..91a39d7 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -37,7 +37,7 @@ bool select_crypt_partition(const std::string_view& text) noexcept { success = true; screen.ExitLoopClosure()(); }; - const auto& content = fmt::format("\n{}\n", text); + const auto& content = fmt::format(FMT_COMPILE("\n{}\n"), text); detail::menu_widget(partitions, ok_callback, &selected, &screen, content, {.text_size = size(HEIGHT, GREATER_THAN, 1)}); return success; @@ -107,10 +107,10 @@ bool luks_open() noexcept { // show the error detail::infobox_widget("\nPlease wait...\n"); #ifdef NDEVENV - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("echo \"{}\" | cryptsetup open --type luks {} {}", luks_password, partition, luks_root_name)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup open --type luks {} {}"), luks_password, partition, luks_root_name)}); #endif - const auto& devlist = utils::exec(fmt::format("lsblk -o NAME,TYPE,FSTYPE,SIZE,MOUNTPOINT {} | grep \"crypt\\|NAME\\|MODEL\\|TYPE\\|FSTYPE\\|SIZE\"", partition)); + const auto& devlist = utils::exec(fmt::format(FMT_COMPILE("lsblk -o NAME,TYPE,FSTYPE,SIZE,MOUNTPOINT {} | grep \"crypt\\|NAME\\|MODEL\\|TYPE\\|FSTYPE\\|SIZE\""), partition)); detail::msgbox_widget(devlist); return true; @@ -155,10 +155,10 @@ void luks_encrypt([[maybe_unused]] const std::string_view& command) noexcept { const auto& luks_root_name = std::get(config_data["LUKS_ROOT_NAME"]); const auto& luks_password = std::get(config_data["PASSWD"]); - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("echo \"{}\" | cryptsetup -q {} {}", luks_password, command, partition)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup -q {} {}"), luks_password, command, partition)}); // Now open the encrypted partition or LV - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("echo \"{}\" | cryptsetup open {} {}", luks_password, partition, luks_root_name)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("echo \"{}\" | cryptsetup open {} {}"), luks_password, partition, luks_root_name)}); #endif } @@ -184,7 +184,7 @@ void luks_express() noexcept { void luks_show() noexcept { static constexpr auto luks_success = "Done!"; const auto& lsblk = utils::exec("lsblk -o NAME,TYPE,FSTYPE,SIZE | grep \"part\\|crypt\\|NAME\\|TYPE\\|FSTYPE\\|SIZE\""); - const auto& content = fmt::format("\n{}\n \n{}", luks_success, lsblk); + const auto& content = fmt::format(FMT_COMPILE("\n{}\n \n{}"), luks_success, lsblk); detail::msgbox_widget(content, size(HEIGHT, GREATER_THAN, 5)); } @@ -205,7 +205,7 @@ void luks_menu_advanced() noexcept { screen.ExitLoopClosure()(); }; - const auto& content = fmt::format("\n{}\n \n{}\n \n{}\n", luks_menu_body, luks_menu_body2, luks_menu_body3); + const auto& content = fmt::format(FMT_COMPILE("\n{}\n \n{}\n \n{}\n"), luks_menu_body, luks_menu_body2, luks_menu_body3); const auto& content_size = size(HEIGHT, LESS_THAN, 10) | size(WIDTH, GREATER_THAN, 40); detail::menu_widget(menu_entries, ok_callback, &selected, &screen, content, {.content_size = content_size}); /* clang-format off */ diff --git a/src/disk.cpp b/src/disk.cpp new file mode 100644 index 0000000..c31015f --- /dev/null +++ b/src/disk.cpp @@ -0,0 +1,52 @@ +#include "disk.hpp" +#include "utils.hpp" +#include "widgets.hpp" + +#include // for exists, is_directory +#include // for Renderer, Button +#include // for ButtonOption +#include // for Component, ScreenI... +#include // for operator|, size +#include // for umount + +#include +#include + +namespace fs = std::filesystem; + +namespace utils { + +void mount_existing_subvols(const disk_part& disk) noexcept { + // Set mount options + const auto& format_name = utils::exec(fmt::format(FMT_COMPILE("echo {} | rev | cut -d/ -f1 | rev"), disk.part)); + const auto& format_device = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {}"), format_name, "awk '/disk/ {print $1}'")); + + std::string fs_opts{}; + if (utils::exec(fmt::format(FMT_COMPILE("cat /sys/block/{}/queue/rotational)"), format_device), true) == "1") { + fs_opts = "autodefrag,compress=zlib,noatime,nossd,commit=120"; + } else { + fs_opts = "compress=lzo,noatime,space_cache,ssd,commit=120"; + } +#ifdef NDEVENV + utils::exec("btrfs subvolume list /mnt | cut -d\" \" -f9 > /tmp/.subvols", true); + umount("/mnt"); + + // Mount subvolumes one by one + for (const auto& subvol : utils::make_multiline(utils::exec("cat /tmp/.subvols"))) { + // Ask for mountpoint + const auto& content = fmt::format(FMT_COMPILE("\nInput mountpoint of the subvolume {}\nas it would appear in installed system\n(without prepending /mnt).\n"), subvol); + std::string mountpoint{"/"}; + if (!tui::detail::inputbox_widget(mountpoint, content, size(ftxui::HEIGHT, ftxui::LESS_THAN, 9) | size(ftxui::WIDTH, ftxui::LESS_THAN, 30))) { + return; + } + const auto& mount_dir{fmt::format(FMT_COMPILE("/mnt/{}"), mountpoint)}; + if (!fs::exists(mount_dir)) { + fs::create_directories(mount_dir); + } + // Mount the subvolume + utils::exec(fmt::format(FMT_COMPILE("mount -o \"{},subvol={}\" \"{}\" \"{}\""), fs_opts, subvol, disk.root, mount_dir)); + } +#endif +} + +} // namespace utils diff --git a/src/disk.hpp b/src/disk.hpp new file mode 100644 index 0000000..687737f --- /dev/null +++ b/src/disk.hpp @@ -0,0 +1,16 @@ +#ifndef DISK_HPP +#define DISK_HPP + +#include + +namespace utils { + +struct disk_part { + const std::string_view root; + const std::string_view part; +}; + +void mount_existing_subvols(const disk_part& partition) noexcept; +} // namespace utils + +#endif // DISK_HPP diff --git a/src/tui.cpp b/src/tui.cpp index 4552ba4..abb4196 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -2,6 +2,7 @@ #include "config.hpp" #include "crypto.hpp" #include "definitions.hpp" +#include "disk.hpp" #include "utils.hpp" #include "widgets.hpp" @@ -41,7 +42,7 @@ namespace tui { // Revised to deal with partion sizes now being displayed to the user bool confirm_mount([[maybe_unused]] const std::string_view& part_user) { #ifdef NDEVENV - const auto& ret_status = utils::exec(fmt::format("mount | grep {}", part_user), true); + const auto& ret_status = utils::exec(fmt::format(FMT_COMPILE("mount | grep {}"), part_user), true); if (ret_status != "0") { detail::infobox_widget("\nMount Failed!\n"); std::this_thread::sleep_for(std::chrono::seconds(2)); @@ -59,7 +60,7 @@ bool confirm_mount([[maybe_unused]] const std::string_view& part_user) { // TODO: reimplement natively const auto& str = utils::make_multiline(partitions); - const auto& cmd = fmt::format("echo \"{0}\" | sed \"s~{1} [0-9]*[G-M]~~\" | sed \"s~{1} [0-9]*\\.[0-9]*[G-M]~~\" | sed \"s~{1}$\' -\'~~\"", str, partition); + const auto& cmd = fmt::format(FMT_COMPILE("echo \"{0}\" | sed \"s~{1} [0-9]*[G-M]~~\" | sed \"s~{1} [0-9]*\\.[0-9]*[G-M]~~\" | sed \"s~{1}$\' -\'~~\""), str, partition); const auto& res_text = utils::exec(cmd); partitions = utils::make_multiline(res_text); number_partitions -= 1; @@ -97,11 +98,11 @@ bool exit_done() noexcept { static constexpr auto CloseInstBody = "Close installer?"; const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - const auto& target_mnt = fmt::format("findmnt --list -o TARGET | grep {} 2>/dev/null", mountpoint); + const auto& target_mnt = fmt::format(FMT_COMPILE("findmnt --list -o TARGET | grep {} 2>/dev/null"), mountpoint); if (!target_mnt.empty()) { utils::final_check(); const auto& checklist = std::get(config_data["CHECKLIST"]); - const auto& do_close = detail::yesno_widget(fmt::format("\n{}\n{}\n", CloseInstBody, checklist), size(HEIGHT, LESS_THAN, 20) | size(WIDTH, LESS_THAN, 40)); + const auto& do_close = detail::yesno_widget(fmt::format(FMT_COMPILE("\n{}\n{}\n"), CloseInstBody, checklist), size(HEIGHT, LESS_THAN, 20) | size(WIDTH, LESS_THAN, 40)); /* clang-format off */ if (!do_close) { return false; } /* clang-format on */ @@ -109,15 +110,15 @@ bool exit_done() noexcept { spdlog::info("exit installer."); static constexpr auto LogInfo = "Would you like to save\nthe installation-log to the installed system?\nIt will be copied to\n"; - const auto& do_save_log = detail::yesno_widget(fmt::format("\n{} {}/cachyos-install.log\n", LogInfo, mountpoint), size(HEIGHT, LESS_THAN, 20) | size(WIDTH, LESS_THAN, 40)); + const auto& do_save_log = detail::yesno_widget(fmt::format(FMT_COMPILE("\n{} {}/cachyos-install.log\n"), LogInfo, mountpoint), size(HEIGHT, LESS_THAN, 20) | size(WIDTH, LESS_THAN, 40)); if (do_save_log) { - std::filesystem::copy_file("/tmp/cachyos-install.log", fmt::format("{}/cachyos-install.log", mountpoint), fs::copy_options::overwrite_existing); + std::filesystem::copy_file("/tmp/cachyos-install.log", fmt::format(FMT_COMPILE("{}/cachyos-install.log"), mountpoint), fs::copy_options::overwrite_existing); } utils::umount_partitions(); utils::clear_screen(); return true; } else { - const auto& do_close = detail::yesno_widget(fmt::format("\n{}\n", CloseInstBody), size(HEIGHT, LESS_THAN, 10) | size(WIDTH, LESS_THAN, 40)); + const auto& do_close = detail::yesno_widget(fmt::format(FMT_COMPILE("\n{}\n"), CloseInstBody), size(HEIGHT, LESS_THAN, 10) | size(WIDTH, LESS_THAN, 40)); /* clang-format off */ if (!do_close) { return false; } /* clang-format on */ @@ -156,15 +157,15 @@ void generate_fstab() noexcept { } #ifdef NDEVENV const auto& src = menu_entries[static_cast(selected)]; - utils::exec(fmt::format("{0} {1} > {1}/etc/fstab", src, mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("{0} {1} > {1}/etc/fstab"), src, mountpoint)); spdlog::info("Created fstab file:\n"); - utils::exec(fmt::format("cat {}/etc/fstab >> /tmp/cachyos-install.log", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("cat {}/etc/fstab >> /tmp/cachyos-install.log"), mountpoint)); #endif - const auto& swap_file = fmt::format("{}/swapfile", mountpoint); + const auto& swap_file = fmt::format(FMT_COMPILE("{}/swapfile"), mountpoint); if (fs::exists(swap_file) && fs::is_regular_file(swap_file)) { spdlog::info("appending swapfile to the fstab.."); #ifdef NDEVENV - utils::exec(fmt::format("sed -i \"s/\\\\{0}//\" {0}/etc/fstab", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/\\\\{0}//\" {0}/etc/fstab"), mountpoint)); #endif } screen.ExitLoopClosure()(); @@ -175,7 +176,7 @@ void generate_fstab() noexcept { #ifdef NDEVENV // Edit fstab in case of btrfs subvolumes - utils::exec(fmt::format("sed -i \"s/subvolid=.*,subvol=\\/.*,//g\" {}/etc/fstab", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/subvolid=.*,subvol=\\/.*,//g\" {}/etc/fstab"), mountpoint)); #endif } @@ -195,8 +196,8 @@ void set_hostname() noexcept { auto* config_instance = Config::instance(); auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - utils::exec(fmt::format("echo \"{}\" > {}/etc/hostname", hostname, mountpoint)); - const auto& cmd = fmt::format("echo -e \"#\\t\\t\\n127.0.0.1\\tlocalhost.localdomain\\tlocalhost\\t{0}\\n::1\\tlocalhost.localdomain\\tlocalhost\\t{0}\">{1}/etc/hosts", hostname, mountpoint); + utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" > {}/etc/hostname"), hostname, mountpoint)); + const auto& cmd = fmt::format(FMT_COMPILE("echo -e \"#\\t\\t\\n127.0.0.1\\tlocalhost.localdomain\\tlocalhost\\t{0}\\n::1\\tlocalhost.localdomain\\tlocalhost\\t{0}\">{1}/etc/hosts"), hostname, mountpoint); utils::exec(cmd); #endif } @@ -229,12 +230,12 @@ void set_locale() noexcept { auto* config_instance = Config::instance(); auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - const auto& locale_config_path = fmt::format("{}/etc/locale.conf", mountpoint); - const auto& locale_gen_path = fmt::format("{}/etc/locale.gen", mountpoint); + const auto& locale_config_path = fmt::format(FMT_COMPILE("{}/etc/locale.conf"), mountpoint); + const auto& locale_gen_path = fmt::format(FMT_COMPILE("{}/etc/locale.gen"), mountpoint); - utils::exec(fmt::format("echo \"LANG=\\\"{}\\\"\" > {}", locale, locale_config_path)); - utils::exec(fmt::format("echo \"LC_MESSAGES=\\\"{}\\\"\" >> {}", locale, locale_config_path)); - utils::exec(fmt::format("sed -i \"s/#{0}/{0}/\" {1}", locale, locale_gen_path)); + utils::exec(fmt::format(FMT_COMPILE("echo \"LANG=\\\"{}\\\"\" > {}"), locale, locale_config_path)); + utils::exec(fmt::format(FMT_COMPILE("echo \"LC_MESSAGES=\\\"{}\\\"\" >> {}"), locale, locale_config_path)); + utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/#{0}/{0}/\" {1}"), locale, locale_gen_path)); // Generate locales utils::arch_chroot("locale-gen", false); @@ -252,7 +253,7 @@ void set_xkbmap() noexcept { std::string_view xkbmap_choice{}; auto ok_callback = [&] { const auto& keymap = xkbmap_list[static_cast(selected)]; - xkbmap_choice = utils::exec(fmt::format("echo \"{}\" | sed 's/_.*//'", keymap)); + xkbmap_choice = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | sed 's/_.*//'"), keymap)); success = true; screen.ExitLoopClosure()(); }; @@ -270,7 +271,7 @@ void set_xkbmap() noexcept { auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - utils::exec(fmt::format("echo -e \"Section \"\\\"InputClass\"\\\"\\nIdentifier \"\\\"system-keyboard\"\\\"\\nMatchIsKeyboard \"\\\"on\"\\\"\\nOption \"\\\"XkbLayout\"\\\" \"\\\"{0}\"\\\"\\nEndSection\" > {1}/etc/X11/xorg.conf.d/00-keyboard.conf", xkbmap_choice, mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("echo -e \"Section \"\\\"InputClass\"\\\"\\nIdentifier \"\\\"system-keyboard\"\\\"\\nMatchIsKeyboard \"\\\"on\"\\\"\\nOption \"\\\"XkbLayout\"\\\" \"\\\"{0}\"\\\"\\nEndSection\" > {1}/etc/X11/xorg.conf.d/00-keyboard.conf"), xkbmap_choice, mountpoint)); #endif } @@ -281,7 +282,7 @@ void select_keymap() noexcept { // does user want to change the default settings? static constexpr auto default_keymap = "Currently configured keymap setting is:"; - const auto& content = fmt::format("\n {}\n \n[ {} ]\n", default_keymap, keymap); + const auto& content = fmt::format(FMT_COMPILE("\n {}\n \n[ {} ]\n"), default_keymap, keymap); const auto& keep_default = detail::yesno_widget(content, size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); /* clang-format off */ if (!keep_default) { return; } @@ -325,7 +326,7 @@ bool set_timezone() noexcept { std::string subzone{}; { auto screen = ScreenInteractive::Fullscreen(); - const auto& cmd = utils::exec(fmt::format("cat /usr/share/zoneinfo/zone.tab | {1} | grep \"{0}/\" | sed \"s/{0}\\///g\" | sort -ud", zone, "awk '{print $3}'")); + const auto& cmd = utils::exec(fmt::format(FMT_COMPILE("cat /usr/share/zoneinfo/zone.tab | {1} | grep \"{0}/\" | sed \"s/{0}\\///g\" | sort -ud"), zone, "awk '{print $3}'")); const auto& city_list = utils::make_multiline(cmd); std::int32_t selected{}; @@ -343,14 +344,14 @@ bool set_timezone() noexcept { if (subzone.empty()) { return false; } /* clang-format on */ - const auto& content = fmt::format("\nSet Time Zone: {}/{}?\n", zone, subzone); + const auto& content = fmt::format(FMT_COMPILE("\nSet Time Zone: {}/{}?\n"), zone, subzone); const auto& do_set_timezone = detail::yesno_widget(content, size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); /* clang-format off */ if (!do_set_timezone) { return false; } /* clang-format on */ #ifdef NDEVENV - utils::arch_chroot(fmt::format("ln -sf /usr/share/zoneinfo/{}/{} /etc/localtime", zone, subzone), false); + utils::arch_chroot(fmt::format(FMT_COMPILE("ln -sf /usr/share/zoneinfo/{}/{} /etc/localtime"), zone, subzone), false); #endif spdlog::info("Timezone is set to {}/{}", zone, subzone); return true; @@ -364,7 +365,7 @@ void set_hw_clock() noexcept { auto ok_callback = [&] { #ifdef NDEVENV const auto& clock_type = menu_entries[static_cast(selected)]; - utils::arch_chroot(fmt::format("hwclock --systohc --{}", clock_type)); + utils::arch_chroot(fmt::format(FMT_COMPILE("hwclock --systohc --{}"), clock_type)); #endif screen.ExitLoopClosure()(); }; @@ -398,8 +399,8 @@ void set_root_password() noexcept { auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - utils::exec(fmt::format("echo -e \"{}\n{}\" > /tmp/.passwd", pass, confirm)); - utils::exec(fmt::format("arch-chroot {} \"passwd root\" < /tmp/.passwd >/dev/null", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("echo -e \"{}\n{}\" > /tmp/.passwd"), pass, confirm)); + utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} \"passwd root\" < /tmp/.passwd >/dev/null"), mountpoint)); utils::exec("rm /tmp/.passwd"); #endif } @@ -431,7 +432,7 @@ void create_new_user() noexcept { { static constexpr auto DefShell = "\nChoose the default shell.\n"; static constexpr auto UseSpaceBar = "Use [Spacebar] to de/select options listed."; - const auto& shells_options_body = fmt::format("\n{}{}\n", DefShell, UseSpaceBar); + const auto& shells_options_body = fmt::format(FMT_COMPILE("\n{}{}\n"), DefShell, UseSpaceBar); const std::vector radiobox_list = { "zsh", "bash", @@ -455,10 +456,10 @@ void create_new_user() noexcept { std::string_view packages{"cachyos-fish-config"}; const auto& hostcache = std::get(config_data["hostcache"]); if (hostcache) { - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap {} {} |& tee /tmp/pacstrap.log"), mountpoint, packages)}); break; } - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap -c {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap -c {} {} |& tee /tmp/pacstrap.log"), mountpoint, packages)}); #endif break; } @@ -473,7 +474,7 @@ void create_new_user() noexcept { // Enter password. This step will only be reached where the loop has been skipped or broken. std::string pass{}; static constexpr auto user_pass_body = "Enter password for"; - if (!detail::inputbox_widget(pass, fmt::format("{} {}", user_pass_body, user), size(HEIGHT, GREATER_THAN, 1), true)) { + if (!detail::inputbox_widget(pass, fmt::format(FMT_COMPILE("{} {}"), user_pass_body, user), size(HEIGHT, GREATER_THAN, 1), true)) { return; } std::string confirm{}; @@ -487,7 +488,7 @@ void create_new_user() noexcept { detail::msgbox_widget(PassErrBody); pass.clear(); confirm.clear(); - if (!detail::inputbox_widget(pass, fmt::format("{} {}", user_pass_body, user), size(HEIGHT, GREATER_THAN, 1), true)) { + if (!detail::inputbox_widget(pass, fmt::format(FMT_COMPILE("{} {}"), user_pass_body, user), size(HEIGHT, GREATER_THAN, 1), true)) { return; } if (!detail::inputbox_widget(confirm, user_confirm_body, size(HEIGHT, GREATER_THAN, 1), true)) { @@ -501,27 +502,27 @@ void create_new_user() noexcept { #ifdef NDEVENV // Create the user, set password, then remove temporary password file - utils::arch_chroot(fmt::format("groupadd {}", user), false); - utils::arch_chroot(fmt::format("useradd {0} -m -g {0} -G wheel,storage,power,network,video,audio,lp,sys,input -s {1}", user, shell), false); + utils::arch_chroot(fmt::format(FMT_COMPILE("groupadd {}"), user), false); + utils::arch_chroot(fmt::format(FMT_COMPILE("useradd {0} -m -g {0} -G wheel,storage,power,network,video,audio,lp,sys,input -s {1}"), user, shell), false); spdlog::info("add user to groups"); // check if user has been created - const auto& user_check = utils::exec(fmt::format("arch-chroot {} getent passwd {}", mountpoint, user)); + const auto& user_check = utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} getent passwd {}"), mountpoint, user)); if (user_check.empty()) { spdlog::error("User has not been created!"); } - utils::exec(fmt::format("echo -e \"{}\\n{}\" > /tmp/.passwd", pass, confirm)); - const auto& ret_status = utils::exec(fmt::format("arch-chroot {} \"passwd {}\" < /tmp/.passwd >/dev/null", mountpoint, user), true); + utils::exec(fmt::format(FMT_COMPILE("echo -e \"{}\\n{}\" > /tmp/.passwd"), pass, confirm)); + const auto& ret_status = utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} \"passwd {}\" < /tmp/.passwd >/dev/null"), mountpoint, user), true); spdlog::info("create user pwd: {}", ret_status); fs::remove("/tmp/.passwd"); // Set up basic configuration files and permissions for user // arch_chroot "cp /etc/skel/.bashrc /home/${USER}" - utils::arch_chroot(fmt::format("chown -R {0}:{0} /home/{0}", user), false); - const auto& sudoers_file = fmt::format("{}/etc/sudoers", mountpoint); + utils::arch_chroot(fmt::format(FMT_COMPILE("chown -R {0}:{0} /home/{0}"), user), false); + const auto& sudoers_file = fmt::format(FMT_COMPILE("{}/etc/sudoers"), mountpoint); if (fs::exists(sudoers_file)) { - utils::exec(fmt::format("sed -i '/%wheel ALL=(ALL) ALL/s/^#//' {}", sudoers_file)); + utils::exec(fmt::format(FMT_COMPILE("sed -i '/%wheel ALL=(ALL) ALL/s/^#//' {}"), sudoers_file)); } #endif } @@ -545,12 +546,12 @@ void install_cust_pkgs() noexcept { const auto& hostcache = std::get(config_data["hostcache"]); if (hostcache) { - // utils::exec(fmt::format("pacstrap {} {}", mountpoint, packages)); - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} {}", mountpoint, packages)}); + // utils::exec(fmt::format(FMT_COMPILE("pacstrap {} {}"), mountpoint, packages)); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap {} {}"), mountpoint, packages)}); return; } - // utils::exec(fmt::format("pacstrap -c {} {}", mountpoint, packages)); - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap -c {} {}", mountpoint, packages)}); + // utils::exec(fmt::format(FMT_COMPILE("pacstrap -c {} {}"), mountpoint, packages)); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap -c {} {}"), mountpoint, packages)}); #endif } @@ -565,7 +566,7 @@ void rm_pgs() noexcept { /* clang-format on */ #ifdef NDEVENV - utils::arch_chroot(fmt::format("pacman -Rsn {}", packages)); + utils::arch_chroot(fmt::format(FMT_COMPILE("pacman -Rsn {}"), packages)); #endif } @@ -579,7 +580,7 @@ void chroot_interactive() noexcept { auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - const auto& cmd_formatted = fmt::format("arch-chroot {} bash", mountpoint); + const auto& cmd_formatted = fmt::format(FMT_COMPILE("arch-chroot {} bash"), mountpoint); utils::exec(cmd_formatted, true); #else utils::exec("bash", true); @@ -610,8 +611,8 @@ void install_grub_uefi() noexcept { // if root is encrypted, amend /etc/default/grub const auto& root_name = utils::exec("mount | awk '/\\/mnt / {print $1}' | sed s~/dev/mapper/~~g | sed s~/dev/~~g"); - const auto& root_device = utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {}", root_name, "awk '/disk/ {print $1}'")); - const auto& root_part = utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/part/p\" | {} | tr -cd '[:alnum:]'", root_name, "awk '/part/ {print $1}'")); + const auto& root_device = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {}"), root_name, "awk '/disk/ {print $1}'")); + const auto& root_part = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/part/p\" | {} | tr -cd '[:alnum:]'"), root_name, "awk '/part/ {print $1}'")); #ifdef NDEVENV utils::boot_encrypted_setting(); #endif @@ -624,10 +625,10 @@ void install_grub_uefi() noexcept { #ifdef NDEVENV const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - const auto& grub_installer_path = fmt::format("{}/usr/bin/grub_installer.sh", mountpoint); + const auto& grub_installer_path = fmt::format(FMT_COMPILE("{}/usr/bin/grub_installer.sh"), mountpoint); // grub config changes for non zfs root - if (utils::exec(fmt::format("findmnt -ln -o FSTYPE \"{}\"", mountpoint)) == "zfs") { + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint)) == "zfs") { return; } { @@ -637,7 +638,7 @@ pacman -S --noconfirm --needed grub efibootmgr dosfstools grub-btrfs findmnt | awk '/^\/ / {print $3}' | grep -q btrfs && sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i /etc/default/grub lsblk -ino TYPE,MOUNTPOINT | grep " /$" | grep -q lvm && sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i /etc/default/grub)"; - const auto& bash_code = fmt::format("{}\ngrub-install --target=x86_64-efi --efi-directory={} --bootloader-id={} --recheck\n", bash_codepart, uefi_mount, bootid); + const auto& bash_code = fmt::format(FMT_COMPILE("{}\ngrub-install --target=x86_64-efi --efi-directory={} --bootloader-id={} --recheck\n"), bash_codepart, uefi_mount, bootid); std::ofstream grub_installer{grub_installer_path}; grub_installer << bash_code; } @@ -647,22 +648,22 @@ lsblk -ino TYPE,MOUNTPOINT | grep " /$" | grep -q lvm && sed -e '/GRUB_SAVEDEFAU fs::perm_options::add); // if the device is removable append removable to the grub-install - const auto& removable = utils::exec(fmt::format("cat /sys/block/{}/removable", root_device)); + const auto& removable = utils::exec(fmt::format(FMT_COMPILE("cat /sys/block/{}/removable"), root_device)); if (utils::to_int(removable.data()) == 1) { - utils::exec(fmt::format("sed -e '/^grub-install /s/$/ --removable/g' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/^grub-install /s/$/ --removable/g' -i {}"), grub_installer_path)); } // If the root is on btrfs-subvolume, amend grub installation ret_status = utils::exec("mount | awk '$3 == \"/mnt\" {print $0}' | grep btrfs | grep -qv subvolid=5", true); if (ret_status != "0") { - utils::exec(fmt::format("sed -e 's/ grub-btrfs//g' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed -e 's/ grub-btrfs//g' -i {}"), grub_installer_path)); } // If Full disk encryption is used, use a keyfile const auto& fde = std::get(config_data["fde"]); if (fde == 1) { spdlog::info("Full disk encryption enabled"); - utils::exec(fmt::format("sed '3a\\grep -q \"^GRUB_ENABLE_CRYPTODISK=y\" /etc/default/grub || sed -i \"s/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/\" /etc/default/grub' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed '3a\\grep -q \"^GRUB_ENABLE_CRYPTODISK=y\" /etc/default/grub || sed -i \"s/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/\" /etc/default/grub' -i {}"), grub_installer_path)); } // install grub @@ -677,14 +678,14 @@ lsblk -ino TYPE,MOUNTPOINT | grep " /$" | grep -q lvm && sed -e '/GRUB_SAVEDEFAU static constexpr auto set_boot_default_body = "Some UEFI firmware may not detect the bootloader unless it is set\nas default by copying its efi stub to"; static constexpr auto set_boot_default_body2 = "and renaming it to bootx64.efi.\n\nIt is recommended to do so unless already using a default bootloader,\nor where intending to use multiple bootloaders.\n\nSet bootloader as default?"; - const auto& do_set_default_bootloader = detail::yesno_widget(fmt::format("\n{} {}/EFI/boot {}\n", set_boot_default_body, uefi_mount, set_boot_default_body2), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); + const auto& do_set_default_bootloader = detail::yesno_widget(fmt::format(FMT_COMPILE("\n{} {}/EFI/boot {}\n"), set_boot_default_body, uefi_mount, set_boot_default_body2), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); /* clang-format off */ if (!do_set_default_bootloader) { return; } /* clang-format on */ #ifdef NDEVENV - utils::arch_chroot(fmt::format("mkdir {}/EFI/boot", uefi_mount)); - utils::arch_chroot(fmt::format("cp -r {0}/EFI/cachyos/grubx64.efi {0}/EFI/boot/bootx64.efi", uefi_mount)); + utils::arch_chroot(fmt::format(FMT_COMPILE("mkdir {}/EFI/boot"), uefi_mount)); + utils::arch_chroot(fmt::format(FMT_COMPILE("cp -r {0}/EFI/cachyos/grubx64.efi {0}/EFI/boot/bootx64.efi"), uefi_mount)); #endif detail::infobox_widget("\nGrub has been set as the default bootloader.\n"); @@ -704,16 +705,16 @@ void install_systemd_boot() noexcept { const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); const auto& uefi_mount = std::get(config_data["UEFI_MOUNT"]); - utils::arch_chroot(fmt::format("bootctl --path={} install", uefi_mount), false); - // utils::exec(fmt::format("pacstrap {} systemd-boot-manager", mountpoint)); - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} systemd-boot-manager", mountpoint)}); + utils::arch_chroot(fmt::format(FMT_COMPILE("bootctl --path={} install"), uefi_mount), false); + // utils::exec(fmt::format(FMT_COMPILE("pacstrap {} systemd-boot-manager"), mountpoint)); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap {} systemd-boot-manager"), mountpoint)}); utils::arch_chroot("sdboot-manage gen", false); // Check if the volume is removable. If so, dont use autodetect const auto& root_name = utils::exec("mount | awk \'/\\/mnt / {print $1}\' | sed s~/dev/mapper/~~g | sed s~/dev/~~g"); - const auto& root_device = utils::exec(fmt::format("lsblk -i | tac | sed -r \'s/^[^[:alnum:]]+//\' | sed -n -e \"/{}/,/disk/p\" | {}", root_name, "awk \'/disk/ {print $1}\'")); + const auto& root_device = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r \'s/^[^[:alnum:]]+//\' | sed -n -e \"/{}/,/disk/p\" | {}"), root_name, "awk \'/disk/ {print $1}\'")); spdlog::info("root_name: {}. root_device: {}", root_name, root_device); - const auto& removable = utils::exec(fmt::format("cat /sys/block/{}/removable", root_device)); + const auto& removable = utils::exec(fmt::format(FMT_COMPILE("cat /sys/block/{}/removable"), root_device)); if (utils::to_int(removable.data()) == 1) { // Remove autodetect hook utils::exec("sed -i -e \'/^HOOKS=/s/\\ autodetect//g\' /mnt/etc/mkinitcpio.conf"); @@ -791,17 +792,17 @@ void install_base() noexcept { std::string packages{}; auto ok_callback = [&] { packages = detail::from_checklist_string(available_kernels, kernels_state.get()); - auto ret_status = utils::exec(fmt::format("echo \"{}\" | grep \"linux\"", packages), true); + auto ret_status = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | grep \"linux\""), packages), true); if (ret_status != "0") { // Check if a kernel is already installed - ret_status = utils::exec(fmt::format("ls {}/boot/*.img >/dev/null 2>&1", mountpoint), true); + ret_status = utils::exec(fmt::format(FMT_COMPILE("ls {}/boot/*.img >/dev/null 2>&1"), mountpoint), true); if (ret_status != "0") { static constexpr auto ErrNoKernel = "\nAt least one kernel must be selected.\n"; detail::msgbox_widget(ErrNoKernel); return; } - const auto& cmd = utils::exec(fmt::format("ls {}/boot/*.img | cut -d'-' -f2 | grep -v ucode.img | sort -u", mountpoint)); - detail::msgbox_widget(fmt::format("\nlinux-{} detected\n", cmd)); + const auto& cmd = utils::exec(fmt::format(FMT_COMPILE("ls {}/boot/*.img | cut -d'-' -f2 | grep -v ucode.img | sort -u"), mountpoint)); + detail::msgbox_widget(fmt::format(FMT_COMPILE("\nlinux-{} detected\n"), cmd)); screen.ExitLoopClosure()(); } spdlog::info("selected: {}", packages); @@ -810,7 +811,7 @@ void install_base() noexcept { static constexpr auto InstStandBseBody = "\nThe base package group will be installed automatically.\nThe base-devel package group is required to use the Arch User Repository (AUR).\n"; static constexpr auto UseSpaceBar = "Use [Spacebar] to de/select options listed."; - const auto& kernels_options_body = fmt::format("\n{}{}\n", InstStandBseBody, UseSpaceBar); + const auto& kernels_options_body = fmt::format(FMT_COMPILE("\n{}{}\n"), InstStandBseBody, UseSpaceBar); constexpr auto base_title = "New CLI Installer | Install Base"; detail::checklist_widget(available_kernels, ok_callback, kernels_state.get(), &screen, kernels_options_body, base_title, {.text_size = nothing}); @@ -824,7 +825,7 @@ void install_base() noexcept { const auto pkg_count = pkg_list.size(); for (std::size_t i = 0; i < pkg_count; ++i) { const auto& pkg = pkg_list[i]; - pkg_list.emplace_back(fmt::format("{}-headers", pkg)); + pkg_list.emplace_back(fmt::format(FMT_COMPILE("{}-headers"), pkg)); } pkg_list.insert(pkg_list.cend(), {"amd-ucode", "intel-ucode"}); pkg_list.insert(pkg_list.cend(), {"base", "base-devel", "zsh", "cachyos-keyring", "cachyos-mirrorlist", "cachyos-v3-mirrorlist", "cachyos-hello", "cachyos-hooks", "cachyos-settings", "cachyos-rate-mirrors", "cachy-browser"}); @@ -836,25 +837,25 @@ void install_base() noexcept { // filter_packages const auto& hostcache = std::get(config_data["hostcache"]); if (hostcache) { - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap {} {} |& tee /tmp/pacstrap.log"), mountpoint, packages)}); } else { - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap -c {} {} |& tee /tmp/pacstrap.log", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap -c {} {} |& tee /tmp/pacstrap.log"), mountpoint, packages)}); } - std::filesystem::copy_file("/etc/pacman.conf", fmt::format("{}/etc/pacman.conf", mountpoint), fs::copy_options::overwrite_existing); + std::filesystem::copy_file("/etc/pacman.conf", fmt::format(FMT_COMPILE("{}/etc/pacman.conf"), mountpoint), fs::copy_options::overwrite_existing); std::ofstream{base_installed}; // mkinitcpio handling for specific filesystems std::int32_t btrfs_root = 0; std::int32_t zfs_root = 0; - const auto& filesystem_type = fmt::format("findmnt -ln -o FSTYPE {}", mountpoint); + const auto& filesystem_type = fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE {}"), mountpoint); if (filesystem_type == "btrfs") { btrfs_root = 1; - utils::exec(fmt::format("sed -e '/^HOOKS=/s/\\ fsck//g' -e '/^MODULES=/s/\"$/ btrfs\"/g' -i {}/etc/mkinitcpio.conf", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/^HOOKS=/s/\\ fsck//g' -e '/^MODULES=/s/\"$/ btrfs\"/g' -i {}/etc/mkinitcpio.conf"), mountpoint)); } else if (filesystem_type == "zfs") { zfs_root = 1; - utils::exec(fmt::format("sed -e '/^HOOKS=/s/\\ filesystems//g' -e '/^HOOKS=/s/\\ keyboard/\\ keyboard\\ zfs\\ filesystems/g' -e '/^HOOKS=/s/\\ fsck//g' -e '/^FILES=/c\\FILES=(\"/usr/lib/libgcc_s.so.1\")' -i {}/etc/mkinitcpio.conf", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/^HOOKS=/s/\\ filesystems//g' -e '/^HOOKS=/s/\\ keyboard/\\ keyboard\\ zfs\\ filesystems/g' -e '/^HOOKS=/s/\\ fsck//g' -e '/^FILES=/c\\FILES=(\"/usr/lib/libgcc_s.so.1\")' -i {}/etc/mkinitcpio.conf"), mountpoint)); } // add luks and lvm hooks as needed @@ -862,11 +863,11 @@ void install_base() noexcept { const auto& luks = std::get(config_data["LUKS"]); if (lvm == 1 && luks == 0) { - utils::exec(fmt::format("sed -i 's/block filesystems/block lvm2 filesystems/g' {}/etc/mkinitcpio.conf", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems/block lvm2 filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); } else if (lvm == 0 && luks == 1) { - utils::exec(fmt::format("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt filesystems/g' {}/etc/mkinitcpio.conf", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); } else if (lvm == 1 && luks == 1) { - utils::exec(fmt::format("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt lvm2 filesystems/g' {}/etc/mkinitcpio.conf", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/block filesystems keyboard/block consolefont keymap keyboard encrypt lvm2 filesystems/g' {}/etc/mkinitcpio.conf"), mountpoint)); } if (lvm + luks + btrfs_root + zfs_root > 0) { @@ -874,9 +875,9 @@ void install_base() noexcept { } // Generate fstab with UUID - utils::exec(fmt::format("genfstab -U -p {0} > {0}/etc/fstab", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("genfstab -U -p {0} > {0}/etc/fstab"), mountpoint)); // Edit fstab in case of btrfs subvolumes - utils::exec(fmt::format("sed -i \"s/subvolid=.*,subvol=\\/.*,//g\" {}/etc/fstab", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/subvolid=.*,subvol=\\/.*,//g\" {}/etc/fstab"), mountpoint)); #endif } @@ -908,7 +909,7 @@ void install_desktop() noexcept { static constexpr auto InstManDEBody = "\nPlease choose a desktop environment.\n"; static constexpr auto UseSpaceBar = "Use [Spacebar] to de/select options listed."; - const auto& des_options_body = fmt::format("\n{}{}\n", InstManDEBody, UseSpaceBar); + const auto& des_options_body = fmt::format(FMT_COMPILE("\n{}{}\n"), InstManDEBody, UseSpaceBar); constexpr auto desktop_title = "New CLI Installer | Install Desktop"; detail::checklist_widget(available_des, ok_callback, des_state.get(), &screen, des_options_body, desktop_title, {.text_size = nothing}); @@ -980,10 +981,10 @@ void install_desktop() noexcept { const auto& hostcache = std::get(config_data["hostcache"]); if (hostcache) { - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap {} {}", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap {} {}"), mountpoint, packages)}); return; } - detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacstrap -c {} {}", mountpoint, packages)}); + detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacstrap -c {} {}"), mountpoint, packages)}); #endif } @@ -1062,7 +1063,7 @@ void bios_bootloader() { if (selected_bootloader.empty()) { return; } /* clang-format on */ - selected_bootloader = utils::exec(fmt::format("echo \"{}\" | sed 's/+ \\|\"//g'", selected_bootloader)); + selected_bootloader = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | sed 's/+ \\|\"//g'"), selected_bootloader)); if (!tui::select_device()) { return; @@ -1081,26 +1082,26 @@ void bios_bootloader() { // if /boot is LVM (whether using a seperate /boot mount or not), amend grub if ((lvm == 1 && lvm_sep_boot == 0) || lvm_sep_boot == 2) { - utils::exec(fmt::format("sed -i \"s/GRUB_PRELOAD_MODULES=\\\"/GRUB_PRELOAD_MODULES=\\\"lvm /g\" {}/etc/default/grub", mountpoint)); - utils::exec(fmt::format("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i \"s/GRUB_PRELOAD_MODULES=\\\"/GRUB_PRELOAD_MODULES=\\\"lvm /g\" {}/etc/default/grub"), mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub"), mountpoint)); } // If root is on btrfs volume, amend grub - if (utils::exec(fmt::format("findmnt -no FSTYPE {}", mountpoint)) == "btrfs") { - utils::exec(fmt::format("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub", mountpoint)); + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -no FSTYPE {}", mountpoint)) == "btrfs") { + utils::exec(fmt::format(FMT_COMPILE("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub"), mountpoint)); } // Same setting is needed for LVM if (lvm == 1) { - utils::exec(fmt::format("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub"), mountpoint)); } // grub config changes for non zfs root - if (utils::exec(fmt::format("findmnt -ln -o FSTYPE \"{}\"", mountpoint)) == "zfs") { + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint)) == "zfs") { return; } - const auto& grub_installer_path = fmt::format("{}/usr/bin/grub_installer.sh", mountpoint); + const auto& grub_installer_path = fmt::format(FMT_COMPILE("{}/usr/bin/grub_installer.sh"), mountpoint); { constexpr auto bash_codepart = R"(#!/bin/bash ln -s /hostlvm /run/lvm @@ -1108,7 +1109,7 @@ pacman -S --noconfirm --needed grub os-prober grub-btrfs findmnt | awk '/^\/ / {print $3}' | grep -q btrfs && sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i /etc/default/grub grub-install --target=i386-pc --recheck)"; - const auto& bash_code = fmt::format("{} {}\n", bash_codepart, device_info); + const auto& bash_code = fmt::format(FMT_COMPILE("{} {}\n"), bash_codepart, device_info); std::ofstream grub_installer{grub_installer_path}; grub_installer << bash_code; } @@ -1116,15 +1117,15 @@ grub-install --target=i386-pc --recheck)"; // If the root is on btrfs-subvolume, amend grub installation auto ret_status = utils::exec("mount | awk '$3 == \"/mnt\" {print $0}' | grep btrfs | grep -qv subvolid=5", true); if (ret_status != "0") { - utils::exec(fmt::format("sed -e 's/ grub-btrfs//g' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed -e 's/ grub-btrfs//g' -i {}"), grub_installer_path)); } // If encryption used amend grub if (luks_dev != "") { - utils::exec(fmt::format("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub", mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -e '/GRUB_SAVEDEFAULT/ s/^#*/#/' -i {}/etc/default/grub"), mountpoint)); - const auto& luks_dev_formatted = utils::exec(fmt::format("echo \"{}\" | {}", luks_dev, "awk '{print $1}'")); - ret_status = utils::exec(fmt::format("echo \"sed -i \"s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\\\"\"{}\\\"~g\"\" /etc/default/grub\" >> {}", luks_dev_formatted, grub_installer_path), true); + const auto& luks_dev_formatted = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | {}"), luks_dev, "awk '{print $1}'")); + ret_status = utils::exec(fmt::format(FMT_COMPILE("echo \"sed -i \"s~GRUB_CMDLINE_LINUX=.*~GRUB_CMDLINE_LINUX=\\\"\"{}\\\"~g\"\" /etc/default/grub\" >> {}"), luks_dev_formatted, grub_installer_path), true); if (ret_status == "0") { spdlog::info("adding kernel parameter {}", luks_dev); } @@ -1134,14 +1135,14 @@ grub-install --target=i386-pc --recheck)"; const auto& fde = std::get(config_data["fde"]); if (fde == 1) { spdlog::info("Full disk encryption enabled"); - utils::exec(fmt::format("sed '3a\\grep -q \"^GRUB_ENABLE_CRYPTODISK=y\" /etc/default/grub || sed -i \"s/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/\" /etc/default/grub' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed '3a\\grep -q \"^GRUB_ENABLE_CRYPTODISK=y\" /etc/default/grub || sed -i \"s/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/\" /etc/default/grub' -i {}"), grub_installer_path)); } // Remove os-prober if not selected constexpr std::string_view needle{"os-prober"}; const auto& found = ranges::search(selected_bootloader, needle); if (found.empty()) { - utils::exec(fmt::format("sed -e 's/ os-prober//g' -i {}", grub_installer_path)); + utils::exec(fmt::format(FMT_COMPILE("sed -e 's/ os-prober//g' -i {}"), grub_installer_path)); } fs::permissions(grub_installer_path, @@ -1149,7 +1150,7 @@ grub-install --target=i386-pc --recheck)"; fs::perm_options::add); detail::infobox_widget("\nPlease wait...\n"); - utils::exec(fmt::format("dd if=/dev/zero of={} seek=1 count=2047", device_info)); + utils::exec(fmt::format(FMT_COMPILE("dd if=/dev/zero of={} seek=1 count=2047"), device_info)); fs::create_directory("/mnt/hostlvm"); utils::exec("mount --bind /run/lvm /mnt/hostlvm"); @@ -1187,11 +1188,11 @@ void auto_partition() noexcept { [[maybe_unused]] const auto& system_info = std::get(config_data["SYSTEM"]); // Find existing partitions (if any) to remove - const auto& parts = utils::exec(fmt::format("parted -s {} print | {}", device_info, "awk \'/^ / {print $1}\'")); + const auto& parts = utils::exec(fmt::format(FMT_COMPILE("parted -s {} print | {}"), device_info, "awk \'/^ / {print $1}\'")); const auto& del_parts = utils::make_multiline(parts); for (const auto& del_part : del_parts) { #ifdef NDEVENV - utils::exec(fmt::format("parted -s {} rm {} >/dev/null", device_info, del_part)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} rm {} >/dev/null"), device_info, del_part)); #else spdlog::debug("{}", del_part); #endif @@ -1199,26 +1200,26 @@ void auto_partition() noexcept { #ifdef NDEVENV // Identify the partition table - const auto& part_table = utils::exec(fmt::format("parted -s {} print | grep -i \'partition table\' | {}", device_info, "awk \'{print $3}\'")); + const auto& part_table = utils::exec(fmt::format(FMT_COMPILE("parted -s {} print | grep -i \'partition table\' | {}"), device_info, "awk \'{print $3}\'")); // Create partition table if one does not already exist if ((system_info == "BIOS") && (part_table != "msdos")) - utils::exec(fmt::format("parted -s {} mklabel msdos >/dev/null", device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} mklabel msdos >/dev/null"), device_info)); if ((system_info == "UEFI") && (part_table != "gpt")) - utils::exec(fmt::format("parted -s {} mklabel gpt >/dev/null", device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} mklabel gpt >/dev/null"), device_info)); // Create partitions (same basic partitioning scheme for BIOS and UEFI) if (system_info == "BIOS") - utils::exec(fmt::format("parted -s {} mkpart primary ext3 1MiB 513MiB >/dev/null", device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} mkpart primary ext3 1MiB 513MiB >/dev/null"), device_info)); else - utils::exec(fmt::format("parted -s {} mkpart ESP fat32 1MiB 513MiB >/dev/null", device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} mkpart ESP fat32 1MiB 513MiB >/dev/null"), device_info)); - utils::exec(fmt::format("parted -s {} set 1 boot on >/dev/null", device_info)); - utils::exec(fmt::format("parted -s {} mkpart primary ext3 513MiB 100% >/dev/null", device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} set 1 boot on >/dev/null"), device_info)); + utils::exec(fmt::format(FMT_COMPILE("parted -s {} mkpart primary ext3 513MiB 100% >/dev/null"), device_info)); #endif // Show created partitions - const auto& disk_list = utils::exec(fmt::format("lsblk {} -o NAME,TYPE,FSTYPE,SIZE", device_info)); + const auto& disk_list = utils::exec(fmt::format(FMT_COMPILE("lsblk {} -o NAME,TYPE,FSTYPE,SIZE"), device_info)); detail::msgbox_widget(disk_list, size(HEIGHT, GREATER_THAN, 5)); } @@ -1330,11 +1331,11 @@ bool select_filesystem() noexcept { // Warn about formatting! const auto& file_sys = std::get(config_data["FILESYSTEM"]); const auto& partition = std::get(config_data["PARTITION"]); - const auto& content = fmt::format("\nMount {}\n \n! Data on {} will be lost !\n", file_sys, partition); + const auto& content = fmt::format(FMT_COMPILE("\nMount {}\n \n! Data on {} will be lost !\n"), file_sys, partition); const auto& do_mount = detail::yesno_widget(content, size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); if (do_mount) { #ifdef NDEVENV - utils::exec(fmt::format("{} {}", file_sys, partition)); + utils::exec(fmt::format(FMT_COMPILE("{} {}"), file_sys, partition)); #endif spdlog::info("mount.{} {}", partition, file_sys); } @@ -1351,10 +1352,10 @@ void mount_opts() noexcept { const auto& file_sys = std::get(config_data["FILESYSTEM"]); const auto& fs_opts = std::get>(config_data["fs_opts"]); const auto& partition = std::get(config_data["PARTITION"]); - const auto& format_name = utils::exec(fmt::format("echo {} | rev | cut -d/ -f1 | rev", partition)); - const auto& format_device = utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {}", format_name, "awk \'/disk/ {print $1}\'")); + const auto& format_name = utils::exec(fmt::format(FMT_COMPILE("echo {} | rev | cut -d/ -f1 | rev"), partition)); + const auto& format_device = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {}"), format_name, "awk \'/disk/ {print $1}\'")); - const auto& rotational_queue = (utils::exec(fmt::format("cat /sys/block/{}/queue/rotational", format_device)) == "1"); + const auto& rotational_queue = (utils::exec(fmt::format(FMT_COMPILE("cat /sys/block/{}/queue/rotational"), format_device)) == "1"); std::unique_ptr fs_opts_state{new bool[fs_opts.size()]{false}}; for (size_t i = 0; i < fs_opts.size(); ++i) { @@ -1383,16 +1384,16 @@ void mount_opts() noexcept { screen.ExitLoopClosure()(); }; - const auto& file_sys_formatted = utils::exec(fmt::format("echo {} | sed \"s/.*\\.//g;s/-.*//g\"", file_sys)); - const auto& fs_title = fmt::format("New CLI Installer | {}", file_sys_formatted); + const auto& file_sys_formatted = utils::exec(fmt::format(FMT_COMPILE("echo {} | sed \"s/.*\\.//g;s/-.*//g\""), file_sys)); + const auto& fs_title = fmt::format(FMT_COMPILE("New CLI Installer | {}"), file_sys_formatted); const auto& content_size = size(HEIGHT, GREATER_THAN, 10) | size(WIDTH, GREATER_THAN, 40) | vscroll_indicator | yframe | flex; static constexpr auto mount_options_body = "\nUse [Space] to de/select the desired mount\noptions and review carefully. Please do not\nselect multiple versions of the same option.\n"; detail::checklist_widget(fs_opts, ok_callback, fs_opts_state.get(), &screen, mount_options_body, fs_title, {content_size, nothing}); // Now clean up the file - mount_opts_info = utils::exec(fmt::format("echo \"{}\" | sed \'s/ /,/g\'", mount_opts_info)); - mount_opts_info = utils::exec(fmt::format("echo \"{}\" | sed \'$s/,$//\'", mount_opts_info)); + mount_opts_info = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | sed \'s/ /,/g\'"), mount_opts_info)); + mount_opts_info = utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | sed \'$s/,$//\'"), mount_opts_info)); // If mount options selected, confirm choice if (!mount_opts_info.empty()) { @@ -1416,7 +1417,7 @@ bool mount_current_partition() noexcept { #ifdef NDEVENV // Make the mount directory - fs::path mount_dir(fmt::format("{}{}", mountpoint, mount_dev)); + fs::path mount_dir(fmt::format(FMT_COMPILE("{}{}"), mountpoint, mount_dev)); fs::create_directories(mount_dir); #endif @@ -1433,26 +1434,26 @@ bool mount_current_partition() noexcept { const auto& mount_opts_info = std::get(config_data["MOUNT_OPTS"]); if (!mount_opts_info.empty()) { // check_for_error "mount ${PARTITION} $(cat ${MOUNT_OPTS})" - const auto& mount_status = utils::exec(fmt::format("mount -o {} {} {}{}", mount_opts_info, partition, mountpoint, mount_dev)); + const auto& mount_status = utils::exec(fmt::format(FMT_COMPILE("mount -o {} {} {}{}"), mount_opts_info, partition, mountpoint, mount_dev)); spdlog::info("{}", mount_status); } else { // check_for_error "mount ${PARTITION}" - const auto& mount_status = utils::exec(fmt::format("mount {} {}{}", partition, mountpoint, mount_dev)); + const auto& mount_status = utils::exec(fmt::format(FMT_COMPILE("mount {} {}{}"), partition, mountpoint, mount_dev)); spdlog::info("{}", mount_status); } #endif - confirm_mount(fmt::format("{}{}", mountpoint, mount_dev)); + confirm_mount(fmt::format(FMT_COMPILE("{}{}"), mountpoint, mount_dev)); // Identify if mounted partition is type "crypt" (LUKS on LVM, or LUKS alone) - if (!utils::exec(fmt::format("lsblk -lno TYPE {} | grep \"crypt\"", partition)).empty()) { + if (!utils::exec(fmt::format(FMT_COMPILE("lsblk -lno TYPE {} | grep \"crypt\""), partition)).empty()) { // cryptname for bootloader configuration either way config_data["LUKS"] = 1; auto& luks_name = std::get(config_data["LUKS_NAME"]); const auto& luks_dev = std::get(config_data["LUKS_DEV"]); - luks_name = utils::exec(fmt::format("echo {} | sed \"s~^/dev/mapper/~~g\"", partition)); + luks_name = utils::exec(fmt::format(FMT_COMPILE("echo {} | sed \"s~^/dev/mapper/~~g\""), partition)); const auto& check_cryptparts = [&](const auto cryptparts, auto functor) { for (const auto& cryptpart : cryptparts) { - if (!utils::exec(fmt::format("lsblk -lno NAME {} | grep \"{}\"", cryptpart, luks_name)).empty()) { + if (!utils::exec(fmt::format(FMT_COMPILE("lsblk -lno NAME {} | grep \"{}\""), cryptpart, luks_name)).empty()) { functor(cryptpart); return true; } @@ -1463,7 +1464,7 @@ bool mount_current_partition() noexcept { // Check if LUKS on LVM (parent = lvm /dev/mapper/...) auto cryptparts = utils::make_multiline(utils::exec("lsblk -lno NAME,FSTYPE,TYPE | grep \"lvm\" | grep -i \"crypto_luks\" | uniq | awk '{print \"/dev/mapper/\"$1}'")); auto check_functor = [&](const auto cryptpart) { - config_data["LUKS_DEV"] = fmt::format("{} cryptdevice={}:{}", luks_dev, cryptpart, luks_name); + config_data["LUKS_DEV"] = fmt::format(FMT_COMPILE("{} cryptdevice={}:{}"), luks_dev, cryptpart, luks_name); config_data["LVM"] = 1; }; if (check_cryptparts(cryptparts, check_functor)) { @@ -1480,8 +1481,8 @@ bool mount_current_partition() noexcept { cryptparts = utils::make_multiline(utils::exec("lsblk -lno NAME,FSTYPE,TYPE | grep \"part\" | grep -i \"crypto_luks\" | uniq | awk '{print \"/dev/\"$1}'")); const auto& check_func_dev = [&](const auto cryptpart) { auto& luks_uuid = std::get(config_data["LUKS_UUID"]); - luks_uuid = utils::exec(fmt::format("lsblk -lno UUID,TYPE,FSTYPE {} | grep \"part\" | grep -i \"crypto_luks\" | {}", cryptpart, "awk '{print $1}'")); - config_data["LUKS_DEV"] = fmt::format("{} cryptdevice=UUID={}:{}", luks_dev, luks_uuid, luks_name); + luks_uuid = utils::exec(fmt::format(FMT_COMPILE("lsblk -lno UUID,TYPE,FSTYPE {} | grep \"part\" | grep -i \"crypto_luks\" | {}"), cryptpart, "awk '{print $1}'")); + config_data["LUKS_DEV"] = fmt::format(FMT_COMPILE("{} cryptdevice=UUID={}:{}"), luks_dev, luks_uuid, luks_name); }; if (check_cryptparts(cryptparts, check_func_dev)) { return true; @@ -1532,7 +1533,7 @@ void make_swap() noexcept { std::string answer{}; { std::vector temp{"None -"}; - const auto& root_filesystem = utils::exec(fmt::format("findmnt -ln -o FSTYPE \"{}\"", mountpoint_info)); + const auto& root_filesystem = utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint_info)); if (!(root_filesystem == "zfs" || root_filesystem == "btrfs")) { temp.push_back("Swapfile -"); } @@ -1564,25 +1565,25 @@ void make_swap() noexcept { if (partition == SelSwpFile) { const auto& total_memory = utils::exec("grep MemTotal /proc/meminfo | awk \'{print $2/1024}\' | sed \'s/\\..*//\'"); - std::string value{fmt::format("{}M", total_memory)}; + std::string value{fmt::format(FMT_COMPILE("{}M"), total_memory)}; if (!detail::inputbox_widget(value, "\nM = MB, G = GB\n", size(ftxui::HEIGHT, ftxui::LESS_THAN, 9) | size(ftxui::WIDTH, ftxui::LESS_THAN, 30))) { return; } - while (utils::exec(fmt::format("echo \"{}\" | grep \"M\\|G\"", value)) == "") { - detail::msgbox_widget(fmt::format("\n{} Error: M = MB, G = GB\n", SelSwpFile)); - value = fmt::format("{}M", total_memory); + while (utils::exec(fmt::format(FMT_COMPILE("echo \"{}\" | grep \"M\\|G\""), value)) == "") { + detail::msgbox_widget(fmt::format(FMT_COMPILE("\n{} Error: M = MB, G = GB\n"), SelSwpFile)); + value = fmt::format(FMT_COMPILE("{}M"), total_memory); if (!detail::inputbox_widget(value, "\nM = MB, G = GB\n", size(ftxui::HEIGHT, ftxui::LESS_THAN, 9) | size(ftxui::WIDTH, ftxui::LESS_THAN, 30))) { return; } } #ifdef NDEVENV - const auto& swapfile_path = fmt::format("{}/swapfile", mountpoint_info); - utils::exec(fmt::format("fallocate -l {} {} >/dev/null", value, swapfile_path)); - utils::exec(fmt::format("chmod 600 {}", swapfile_path)); - utils::exec(fmt::format("mkswap {} >/dev/null", swapfile_path)); - utils::exec(fmt::format("swapon {} >/dev/null", swapfile_path)); + const auto& swapfile_path = fmt::format(FMT_COMPILE("{}/swapfile"), mountpoint_info); + utils::exec(fmt::format(FMT_COMPILE("fallocate -l {} {} >/dev/null"), value, swapfile_path)); + utils::exec(fmt::format(FMT_COMPILE("chmod 600 {}"), swapfile_path)); + utils::exec(fmt::format(FMT_COMPILE("mkswap {} >/dev/null"), swapfile_path)); + utils::exec(fmt::format(FMT_COMPILE("swapon {} >/dev/null"), swapfile_path)); #endif return; } @@ -1591,28 +1592,28 @@ void make_swap() noexcept { auto& number_partitions = std::get(config_data["NUMBER_PARTITIONS"]); // Warn user if creating a new swap - const auto& swap_part = utils::exec(fmt::format("lsblk -o FSTYPE \"{}\" | grep -i \"swap\"", partition)); + const auto& swap_part = utils::exec(fmt::format(FMT_COMPILE("lsblk -o FSTYPE \"{}\" | grep -i \"swap\""), partition)); if (swap_part != "swap") { - const auto& do_swap = detail::yesno_widget(fmt::format("\nmkswap {}\n", partition), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); + const auto& do_swap = detail::yesno_widget(fmt::format(FMT_COMPILE("\nmkswap {}\n"), partition), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); /* clang-format off */ if (!do_swap) { return; } /* clang-format on */ #ifdef NDEVENV - utils::exec(fmt::format("mkswap {} >/dev/null", partition)); + utils::exec(fmt::format(FMT_COMPILE("mkswap {} >/dev/null"), partition)); #endif spdlog::info("mkswap.{}", partition); } #ifdef NDEVENV // Whether existing to newly created, activate swap - utils::exec(fmt::format("swapon {} >/dev/null", partition)); + utils::exec(fmt::format(FMT_COMPILE("swapon {} >/dev/null"), partition)); #endif // TODO: reimplement natively // Since a partition was used, remove that partition from the list const auto& str = utils::make_multiline(partitions); - const auto& cmd = fmt::format("echo \"{0}\" | sed \"s~{1} [0-9]*[G-M]~~\" | sed \"s~{1} [0-9]*\\.[0-9]*[G-M]~~\" | sed \"s~{1}$\' -\'~~\"", str, partition); + const auto& cmd = fmt::format(FMT_COMPILE("echo \"{0}\" | sed \"s~{1} [0-9]*[G-M]~~\" | sed \"s~{1} [0-9]*\\.[0-9]*[G-M]~~\" | sed \"s~{1}$\' -\'~~\""), str, partition); const auto& res_text = utils::exec(cmd); partitions = utils::make_multiline(res_text); number_partitions -= 1; @@ -1670,15 +1671,15 @@ void make_esp() noexcept { config_data["UEFI_PART"] = partition; // If it is already a fat/vfat partition... - const auto& ret_status = utils::exec(fmt::format("fsck -N {} | grep fat", partition), true); + const auto& ret_status = utils::exec(fmt::format(FMT_COMPILE("fsck -N {} | grep fat"), partition), true); bool do_boot_partition{}; if (ret_status != "0") { - const auto& content = fmt::format("\nThe UEFI partition {} has already been formatted.\n \nReformat? Doing so will erase ALL data already on that partition.\n", partition); + const auto& content = fmt::format(FMT_COMPILE("\nThe UEFI partition {} has already been formatted.\n \nReformat? Doing so will erase ALL data already on that partition.\n"), partition); do_boot_partition = detail::yesno_widget(content, size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); } if (do_boot_partition) { #ifdef NDEVENV - utils::exec(fmt::format("mkfs.vfat -F32 {} >/dev/null", partition)); + utils::exec(fmt::format(FMT_COMPILE("mkfs.vfat -F32 {} >/dev/null"), partition)); #endif spdlog::debug("Formating boot partition with fat/vfat!"); } @@ -1709,10 +1710,10 @@ void make_esp() noexcept { const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); auto& uefi_mount = std::get(config_data["UEFI_MOUNT"]); uefi_mount = answer; - const auto& path_formatted = fmt::format("{}{}", mountpoint_info, uefi_mount); + const auto& path_formatted = fmt::format(FMT_COMPILE("{}{}"), mountpoint_info, uefi_mount); #ifdef NDEVENV - utils::exec(fmt::format("mkdir -p {}", path_formatted)); - utils::exec(fmt::format("mount {} {}", partition, path_formatted)); + utils::exec(fmt::format(FMT_COMPILE("mkdir -p {}"), path_formatted)); + utils::exec(fmt::format(FMT_COMPILE("mount {} {}"), partition, path_formatted)); #endif confirm_mount(path_formatted); } @@ -1756,7 +1757,7 @@ void mount_partitions() noexcept { // check to see if we already have a zfs root mounted const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); - if (utils::exec(fmt::format("findmnt -ln -o FSTYPE \"{}\"", mountpoint_info)) == "zfs") { + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -ln -o FSTYPE \"{}\""), mountpoint_info)) == "zfs") { detail::infobox_widget("\nUsing ZFS root on \'/\'\n"); std::this_thread::sleep_for(std::chrono::seconds(3)); } else { @@ -1780,7 +1781,9 @@ void mount_partitions() noexcept { if (!success) { return; } /* clang-format on */ } - spdlog::info("partition: {}", std::get(config_data["PARTITION"])); + const auto& root_part = std::get(config_data["ROOT_PART"]); + const auto& part = std::get(config_data["PARTITION"]); + spdlog::info("partition: {}", part); // Reset the mountpoint variable, in case this is the second time through this menu and old state is still around config_data["MOUNT"] = ""; @@ -1797,26 +1800,25 @@ void mount_partitions() noexcept { // get_cryptroot // echo "$LUKS_DEV" > /tmp/.luks_dev // If the root partition is btrfs, offer to create subvolumus - if (utils::exec(fmt::format("findmnt -no FSTYPE \"{}\"", mountpoint_info)) == "btrfs") { + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -no FSTYPE \"{}\""), mountpoint_info)) == "btrfs") { // Check if there are subvolumes already on the btrfs partition - const auto& subvolumes = fmt::format("btrfs subvolume list \"{}\"", mountpoint_info); - const auto& subvolumes_count = utils::exec(fmt::format("{} | wc -l", subvolumes)); + const auto& subvolumes = fmt::format(FMT_COMPILE("btrfs subvolume list \"{}\""), mountpoint_info); + const auto& subvolumes_count = utils::exec(fmt::format(FMT_COMPILE("{} | wc -l"), subvolumes)); const auto& lines_count = utils::to_int(subvolumes_count.data()); if (lines_count > 1) { - const auto& subvolumes_formated = utils::exec(fmt::format("{} | cut -d\" \" -f9", subvolumes)); - const auto& existing_subvolumes = detail::yesno_widget(fmt::format("\nFound subvolumes {}\n \nWould you like to mount them?\n ", subvolumes_formated), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); + const auto& subvolumes_formated = utils::exec(fmt::format(FMT_COMPILE("{} | cut -d\" \" -f9"), subvolumes)); + const auto& existing_subvolumes = detail::yesno_widget(fmt::format(FMT_COMPILE("\nFound subvolumes {}\n \nWould you like to mount them?\n "), subvolumes_formated), size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); // Pre-existing subvolumes and user wants to mount them if (existing_subvolumes) { - spdlog::debug("Implement me!"); + utils::mount_existing_subvols({root_part, part}); } - // mount_existing_subvols } else { // No subvolumes present. Make some new ones const auto& create_subvolumes = detail::yesno_widget("\nWould you like to create subvolumes in it? \n", size(HEIGHT, LESS_THAN, 15) | size(WIDTH, LESS_THAN, 75)); if (create_subvolumes) { spdlog::debug("Implement me!"); + // utils::btrfs_subvolumes({root_part, part}); } - // DIALOG " Your root volume is formatted in btrfs " --yesno "" 0 0 && btrfs_subvolumes } } } @@ -1885,7 +1887,7 @@ void mount_partitions() noexcept { std::string value{"/"}; static constexpr auto ExtPartBody1 = "Specify partition mountpoint. Ensure\nthe name begins with a forward slash (/).\nExamples include:"; - if (!detail::inputbox_widget(value, fmt::format("\n{}\n{}\n", ExtPartBody1, mnt_examples))) { return; } + if (!detail::inputbox_widget(value, fmt::format(FMT_COMPILE("\n{}\n{}\n"), ExtPartBody1, mnt_examples))) { return; } /* clang-format on */ auto& mount_dev = std::get(config_data["MOUNT"]); mount_dev = std::move(value); @@ -1896,7 +1898,7 @@ void mount_partitions() noexcept { detail::msgbox_widget("\nPartition cannot be mounted due to a problem with the mountpoint name.\nA name must be given after a forward slash.\n"); // Ask user for mountpoint again value = "/"; - if (!detail::inputbox_widget(value, fmt::format("\n{}\n{}\n", ExtPartBody1, mnt_examples))) { + if (!detail::inputbox_widget(value, fmt::format(FMT_COMPILE("\n{}\n{}\n"), ExtPartBody1, mnt_examples))) { return; } mount_dev = std::move(value); @@ -1910,7 +1912,7 @@ void mount_partitions() noexcept { // 1 = separate non-lvm boot, // 2 = separate lvm boot. For Grub configuration if (mount_dev == "/boot") { - const auto& cmd = fmt::format("lsblk -lno TYPE {} | grep \"lvm\"", partition); + const auto& cmd = fmt::format(FMT_COMPILE("lsblk -lno TYPE {} | grep \"lvm\""), partition); const auto& cmd_out = utils::exec(cmd); config_data["LVM_SEP_BOOT"] = 1; if (!cmd_out.empty()) { @@ -1944,9 +1946,9 @@ void create_partitions() noexcept { if (selected_entry != optwipe && selected_entry != optauto) { screen.Suspend(); #ifdef NDEVENV - utils::exec(fmt::format("{} {}", selected_entry, std::get(config_data["DEVICE"])), true); + utils::exec(fmt::format(FMT_COMPILE("{} {}"), selected_entry, std::get(config_data["DEVICE"])), true); #else - spdlog::debug("to be executed: {}", fmt::format("{} {}", selected_entry, std::get(config_data["DEVICE"]))); + spdlog::debug("to be executed: {}", fmt::format(FMT_COMPILE("{} {}"), selected_entry, std::get(config_data["DEVICE"]))); #endif screen.Resume(); screen.ExitLoopClosure()(); diff --git a/src/utils.cpp b/src/utils.cpp index 41b9eef..89cba0d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -101,7 +101,7 @@ void arch_chroot(const std::string_view& command, bool follow) noexcept { auto& config_data = config_instance->data(); const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - const auto& cmd_formatted = fmt::format("arch-chroot {} {}", mountpoint, command); + const auto& cmd_formatted = fmt::format(FMT_COMPILE("arch-chroot {} {}"), mountpoint, command); if (follow) { tui::detail::follow_process_log_widget({"/bin/sh", "-c", cmd_formatted}); return; @@ -261,11 +261,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) { - if (utils::exec(fmt::format("pacman -Q {}", pkg), true) != "0") { + if (utils::exec(fmt::format(FMT_COMPILE("pacman -Q {}"), pkg), true) != "0") { std::this_thread::sleep_for(std::chrono::seconds(1)); utils::clear_screen(); - tui::detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("pacman -Sy --noconfirm {}", pkg)}); - // utils::exec(fmt::format("pacman -Sy --noconfirm {}", pkg)); + tui::detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("pacman -Sy --noconfirm {}"), pkg)}); + // utils::exec(fmt::format(FMT_COMPILE("pacman -Sy --noconfirm {}"), pkg)); } } @@ -275,7 +275,7 @@ void umount_partitions() noexcept { auto& config_data = config_instance->data(); const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); - auto mount_info = utils::exec(fmt::format("mount | grep \"{}\" | {}", mountpoint_info, "awk \'{print $3}\' | sort -r")); + auto mount_info = utils::exec(fmt::format(FMT_COMPILE("mount | grep \"{}\" | {}"), mountpoint_info, "awk \'{print $3}\' | sort -r")); #ifdef NDEVENV utils::exec("swapoff -a"); #endif @@ -298,8 +298,8 @@ void secure_wipe() noexcept { #ifdef NDEVENV utils::inst_needed("wipe"); - tui::detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format("wipe -Ifre {}", device_info)}); - // utils::exec(fmt::format("wipe -Ifre {}", device_info)); + tui::detail::follow_process_log_widget({"/bin/sh", "-c", fmt::format(FMT_COMPILE("wipe -Ifre {}"), device_info)}); + // utils::exec(fmt::format(FMT_COMPILE("wipe -Ifre {}"), device_info)); #else spdlog::debug("{}\n", device_info); #endif @@ -318,7 +318,7 @@ void find_partitions() noexcept { // get the list of partitions and also include the zvols since it is common to mount filesystems directly on them. It should be safe to include them here since they present as block devices. static constexpr auto other_piece = "sed 's/part$/\\/dev\\//g' | sed 's/lvm$\\|crypt$/\\/dev\\/mapper\\//g' | awk '{print $3$1 \" \" $2}' | awk '!/mapper/{a[++i]=$0;next}1;END{while(x/dev/null | awk '{printf \"/dev/zvol/%s %s\\n\", $1, $2}'"; - const auto& partitions_tmp = utils::exec(fmt::format("lsblk -lno NAME,SIZE,TYPE | grep '{}' | {}", include_part, other_piece)); + const auto& partitions_tmp = utils::exec(fmt::format(FMT_COMPILE("lsblk -lno NAME,SIZE,TYPE | grep '{}' | {}"), include_part, other_piece)); // create a raid partition list // old_ifs="$IFS" @@ -397,7 +397,7 @@ void get_cryptroot() noexcept { const auto& check_cryptparts = [&luks_name](const auto& cryptparts, auto functor) { for (const auto& cryptpart : cryptparts) { - if (!utils::exec(fmt::format("lsblk -lno NAME {} | grep \"{}\"", cryptpart, luks_name)).empty()) { + if (!utils::exec(fmt::format(FMT_COMPILE("lsblk -lno NAME {} | grep \"{}\""), cryptpart, luks_name)).empty()) { functor(cryptpart); } } @@ -408,7 +408,7 @@ void get_cryptroot() noexcept { if (!temp_out.empty()) { const auto& cryptparts = utils::make_multiline(temp_out); const auto& check_functor = [&](const auto cryptpart) { - config_data["LUKS_DEV"] = fmt::format("cryptdevice={}:{}", cryptpart, luks_name); + config_data["LUKS_DEV"] = fmt::format(FMT_COMPILE("cryptdevice={}:{}"), cryptpart, luks_name); config_data["LVM"] = 1; }; check_cryptparts(cryptparts, check_functor); @@ -422,7 +422,7 @@ void get_cryptroot() noexcept { const auto& check_functor = [&]([[maybe_unused]] const auto cryptpart) { auto& luks_uuid = std::get(config_data["LUKS_UUID"]); luks_uuid = utils::exec("lsblk -ino NAME,FSTYPE,TYPE,MOUNTPOINT,UUID | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/\\/mnt /,/part/p\" | awk '/crypto_LUKS/ {print $4}'"); - config_data["LUKS_DEV"] = fmt::format("cryptdevice=UUID={}:{}", luks_uuid, luks_name); + config_data["LUKS_DEV"] = fmt::format(FMT_COMPILE("cryptdevice=UUID={}:{}"), luks_uuid, luks_name); config_data["LVM"] = 1; }; check_cryptparts(cryptparts, check_functor); @@ -435,8 +435,8 @@ void get_cryptroot() noexcept { const auto& cryptparts = utils::make_multiline(temp_out); const auto& check_functor = [&](const auto cryptpart) { auto& luks_uuid = std::get(config_data["LUKS_UUID"]); - luks_uuid = utils::exec(fmt::format("lsblk -lno UUID,TYPE,FSTYPE {} | grep \"part\" | grep -i \"crypto_luks\" | {}", cryptpart, "awk '{print $1}'")); - config_data["LUKS_DEV"] = fmt::format("cryptdevice=UUID={}:{}", luks_uuid, luks_name); + luks_uuid = utils::exec(fmt::format(FMT_COMPILE("lsblk -lno UUID,TYPE,FSTYPE {} | grep \"part\" | grep -i \"crypto_luks\" | {}"), cryptpart, "awk '{print $1}'")); + config_data["LUKS_DEV"] = fmt::format(FMT_COMPILE("cryptdevice=UUID={}:{}"), luks_uuid, luks_name); }; check_cryptparts(cryptparts, check_functor); return; @@ -475,7 +475,7 @@ void get_cryptboot() noexcept { auto& luks_dev = std::get(config_data["LUKS_DEV"]); const auto& found = ranges::search(luks_dev, boot_uuid); if (found.empty()) { - luks_dev = fmt::format("{} cryptdevice=UUID={}:{}", luks_dev, boot_uuid, boot_name); + luks_dev = fmt::format(FMT_COMPILE("{} cryptdevice=UUID={}:{}"), luks_dev, boot_uuid, boot_name); } } @@ -493,10 +493,10 @@ void boot_encrypted_setting() noexcept { const auto& luks = std::get(config_data["LUKS"]); // Check if root is encrypted if ((luks == 1) - || (utils::exec(fmt::format("lsblk \"/dev/mapper/{}\" | grep -q 'crypt'", root_name), true) == "0") + || (utils::exec(fmt::format(FMT_COMPILE("lsblk \"/dev/mapper/{}\" | grep -q 'crypt'"), root_name), true) == "0") || (utils::exec("lsblk | grep \"/mnt$\" | grep -q 'crypt'", true) == "0") // Check if root is on encrypted lvm volume - || (utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {} | grep -q crypt", root_name, "awk '{print $6}'"), true) == "0")) { + || (utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {} | grep -q crypt"), root_name, "awk '{print $6}'"), true) == "0")) { fde = 1; utils::setup_luks_keyfile(); } @@ -506,8 +506,8 @@ void boot_encrypted_setting() noexcept { const auto& boot_name = utils::exec("mount | awk '/\\/mnt\\/boot / {print $1}' | sed s~/dev/mapper/~~g | sed s~/dev/~~g"); if ((utils::exec("lsblk | grep '/mnt/boot' | grep -q 'crypt'", true) == "0") // Check if the /boot is inside encrypted lvm volume - || (utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {} | grep -q crypt", boot_name, "awk '{print $6}'"), true) == "0") - || (utils::exec(fmt::format("lsblk \"/dev/mapper/{}\" | grep -q 'crypt'", boot_name), true) == "0")) { + || (utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/disk/p\" | {} | grep -q crypt"), boot_name, "awk '{print $6}'"), true) == "0") + || (utils::exec(fmt::format(FMT_COMPILE("lsblk \"/dev/mapper/{}\" | grep -q 'crypt'"), boot_name), true) == "0")) { fde = 1; utils::setup_luks_keyfile(); } @@ -519,7 +519,7 @@ bool check_mount() noexcept { auto* config_instance = Config::instance(); auto& config_data = config_instance->data(); const auto& mountpoint_info = std::get(config_data["MOUNTPOINT"]); - if (utils::exec(fmt::format("findmnt -nl {}", mountpoint_info)) == "") { + if (utils::exec(fmt::format(FMT_COMPILE("findmnt -nl {}"), mountpoint_info)) == "") { return false; } #endif @@ -590,8 +590,8 @@ void try_v3() noexcept { static constexpr auto pacman_conf_path_backup = "/etc/pacman.conf.bak"; std::error_code err{}; - utils::exec(fmt::format("sed -i 's/Architecture = auto/#Architecture = auto/' {}", pacman_conf_cachyos)); - utils::exec(fmt::format("sed -i 's/#//g' {}", pacman_conf_cachyos)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/Architecture = auto/#Architecture = auto/' {}"), pacman_conf_cachyos)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/#//g' {}"), pacman_conf_cachyos)); spdlog::info("backup old config"); fs::rename(pacman_conf, pacman_conf_path_backup, err); @@ -663,7 +663,7 @@ void set_keymap() noexcept { auto& config_data = config_instance->data(); const auto& keymap = std::get(config_data["KEYMAP"]); - utils::exec(fmt::format("loadkeys {}", keymap)); + utils::exec(fmt::format(FMT_COMPILE("loadkeys {}"), keymap)); } void parse_config() noexcept { @@ -683,8 +683,8 @@ void parse_config() noexcept { void setup_luks_keyfile() noexcept { // Add keyfile to luks const auto& root_name = utils::exec("mount | awk '/\\/mnt / {print $1}' | sed s~/dev/mapper/~~g | sed s~/dev/~~g"); - const auto& root_part = utils::exec(fmt::format("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/part/p\" | {} | tr -cd '[:alnum:]'", root_name, "awk '/part/ {print $1}'")); - const auto& number_of_lukskeys = to_int(utils::exec(fmt::format("cryptsetup luksDump /dev/\"{}\" | grep \"ENABLED\" | wc -l", root_part))); + const auto& root_part = utils::exec(fmt::format(FMT_COMPILE("lsblk -i | tac | sed -r 's/^[^[:alnum:]]+//' | sed -n -e \"/{}/,/part/p\" | {} | tr -cd '[:alnum:]'"), root_name, "awk '/part/ {print $1}'")); + const auto& number_of_lukskeys = to_int(utils::exec(fmt::format(FMT_COMPILE("cryptsetup luksDump /dev/\"{}\" | grep \"ENABLED\" | wc -l"), root_part))); if (number_of_lukskeys < 4) { // Create a keyfile #ifdef NDEVENV @@ -696,7 +696,7 @@ void setup_luks_keyfile() noexcept { } utils::exec("chmod 000 /mnt/crypto_keyfile.bin"); spdlog::info("Adding the keyfile to the LUKS configuration"); - auto ret_status = utils::exec(fmt::format("cryptsetup --pbkdf-force-iterations 200000 luksAddKey /dev/\"{}\" /mnt/crypto_keyfile.bin", root_part), true); + auto ret_status = utils::exec(fmt::format(FMT_COMPILE("cryptsetup --pbkdf-force-iterations 200000 luksAddKey /dev/\"{}\" /mnt/crypto_keyfile.bin"), root_part), true); /* clang-format off */ if (ret_status != "0") { spdlog::info("Something went wrong with adding the LUKS key. Is /dev/{} the right partition?", root_part); } /* clang-format on */ @@ -728,13 +728,13 @@ void final_check() noexcept { const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); // Check if bootloader is installed if (system_info == "BIOS") { - if (utils::exec(fmt::format("arch-chroot {} \"pacman -Qq grub\" &> /dev/null", mountpoint), true) != "0") { + if (utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} \"pacman -Qq grub\" &> /dev/null"), mountpoint), true) != "0") { checklist += "- Bootloader is not installed\n"; } } // Check if fstab is generated - if (utils::exec(fmt::format("grep -qv '^#' {}/etc/fstab 2>/dev/null", mountpoint), true) != "0") { + if (utils::exec(fmt::format(FMT_COMPILE("grep -qv '^#' {}/etc/fstab 2>/dev/null"), mountpoint), true) != "0") { checklist += "- Fstab has not been generated\n"; } @@ -742,7 +742,7 @@ void final_check() noexcept { //[[ ! -e /mnt/.video_installed ]] && echo "- $_GCCheck" >> ${CHECKLIST} // Check if locales have been generated - if (to_int(utils::exec(fmt::format("arch-chroot {} 'locale -a' | wc -l", mountpoint), true)) < 3) { + if (to_int(utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} 'locale -a' | wc -l"), mountpoint), true)) < 3) { checklist += "- Locales have not been generated\n"; } diff --git a/src/utils.hpp b/src/utils.hpp index 6a336b6..3c33cdc 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -9,6 +9,8 @@ #include // for string_view #include // for vector +#include + namespace utils { [[nodiscard]] bool is_connected() noexcept; diff --git a/subprojects/fmt.wrap b/subprojects/fmt.wrap index 1dae121..05b12f9 100644 --- a/subprojects/fmt.wrap +++ b/subprojects/fmt.wrap @@ -1,6 +1,6 @@ [wrap-git] url = https://github.com/fmtlib/fmt.git -revision = ae1aaaee5f5f36ce5303bb140d51b6db21e1af4e +revision = a44716f58e943905d1357160b98cae2618d053cf patch_directory = fmt