From 9a36b9c6c526282b17b4b68216fd5fc27aa65c7d Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Thu, 25 Nov 2021 22:34:58 +0400 Subject: [PATCH] cleanup --- .clang-format | 76 +---- .gitignore | 362 ++------------------- CMakeLists.txt | 100 ++++-- cmake/ClangTidy.cmake | 15 + cmake/CompilerWarnings.cmake | 76 +++++ cmake/EnableCcache.cmake | 13 + cmake/StandardProjectSettings.cmake | 42 +++ meson.build | 90 +++++ src/definitions.hpp | 31 ++ src/main.cpp | 58 ++-- src/utils.cpp | 68 ++-- src/utils.hpp | 16 + subprojects/fmt.wrap | 8 + subprojects/ftxui.wrap | 8 + subprojects/nlohmann_json.wrap | 10 + subprojects/packagefiles/fmt/meson.build | 34 ++ subprojects/packagefiles/ftxui/meson.build | 106 ++++++ 17 files changed, 633 insertions(+), 480 deletions(-) create mode 100644 cmake/ClangTidy.cmake create mode 100644 cmake/CompilerWarnings.cmake create mode 100644 cmake/EnableCcache.cmake create mode 100644 cmake/StandardProjectSettings.cmake create mode 100644 meson.build create mode 100644 src/definitions.hpp create mode 100644 src/utils.hpp create mode 100644 subprojects/fmt.wrap create mode 100644 subprojects/ftxui.wrap create mode 100644 subprojects/nlohmann_json.wrap create mode 100644 subprojects/packagefiles/fmt/meson.build create mode 100644 subprojects/packagefiles/ftxui/meson.build diff --git a/.clang-format b/.clang-format index 1766a7c..3ab4962 100644 --- a/.clang-format +++ b/.clang-format @@ -1,66 +1,18 @@ -# Generated from CLion C/C++ Code Style settings -BasedOnStyle: LLVM -AccessModifierOffset: -4 -AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: None -AlignOperands: DontAlign -AllowAllArgumentsOnNextLine: false -AllowAllConstructorInitializersOnNextLine: false -AllowAllParametersOfDeclarationOnNextLine: false -AllowShortBlocksOnASingleLine: Never -AllowShortCaseLabelsOnASingleLine: true -AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: Never -AllowShortLambdasOnASingleLine: None -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterReturnType: None -AlwaysBreakTemplateDeclarations: MultiLine +BasedOnStyle: WebKit +AccessModifierOffset: -3 +AllowShortLoopsOnASingleLine: true +AlignConsecutiveAssignments: true +AlignConsecutiveMacros: true +AlignTrailingComments: true BreakBeforeBraces: Custom -BraceWrapping: - AfterCaseLabel: false - AfterClass: true - AfterControlStatement: Never - AfterEnum: true - AfterFunction: true - AfterNamespace: true - AfterUnion: true - BeforeCatch: true - BeforeElse: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true -BreakBeforeBinaryOperators: NonAssignment -BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon -BreakInheritanceList: BeforeColon -ColumnLimit: 210 -CompactNamespaces: false -ContinuationIndentWidth: 4 -IndentCaseLabels: true -IndentPPDirectives: None -IndentWidth: 4 -KeepEmptyLinesAtTheStartOfBlocks: true -MaxEmptyLinesToKeep: 2 -NamespaceIndentation: All -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PointerAlignment: Right -ReflowComments: false -SpaceAfterCStyleCast: false -SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: false -SpaceBeforeAssignmentOperators: true +BreakStringLiterals: false +ConstructorInitializerIndentWidth: 2 +Cpp11BracedListStyle: true +PointerAlignment: Left +FixNamespaceComments: true SpaceBeforeCpp11BracedList: false -SpaceBeforeCtorInitializerColon: true -SpaceBeforeInheritanceColon: false -SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: true -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 0 -SpacesInAngles: false -SpacesInCStyleCastParentheses: false -SpacesInContainerLiterals: false -SpacesInParentheses: false -SpacesInSquareBrackets: false +SpacesBeforeTrailingComments: 2 +Standard: c++17 TabWidth: 4 -UseTab: ForIndentation +UseTab: Never diff --git a/.gitignore b/.gitignore index 83e1f42..164d84a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,341 +1,37 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +*.DS_Store +*.dump +*.ctu-info +.cache +.idea +build -# User-specific files -*.rsuser -*.suo -*.user -*.userosscache -*.sln.docstates +# Prerequisites +*.d -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta +# Compiled Object files +*.slo +*.lo +*.o *.obj -*.iobj + +# Precompiled Headers +*.gch *.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*_wpftmp.csproj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc -# Chutzpah Test files -_Chutzpah* +# Compiled Dynamic libraries +*.so +*.dylib -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb +# Fortran module files +*.mod +*.smod -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap +# Compiled Static libraries +*.lai +*.la +*.a -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider -.idea/ -*.sln.iml - -# CodeRush personal settings -.cr/personal - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -# Local History for Visual Studio -.localhistory/ - -# CLion -/cmake-build-debug/ -/cmake-build-release/ - -# GDB -/gdb.txt +# Executables +*.exe +*.out +*.app diff --git a/CMakeLists.txt b/CMakeLists.txt index fd4f6ae..79ddcb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,33 +1,87 @@ -cmake_minimum_required(VERSION 3.21) -project(CachyOS-Installer) +cmake_minimum_required(VERSION 3.15) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") -set(CMAKE_C_COMPILER "/usr/bin/clang") -set(CMAKE_CXX_COMPILER "/usr/bin/clang++") +## +## PROJECT +## name and version +## +project(cachyos-installer + VERSION 0.0.1 + LANGUAGES CXX) -set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld") -set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld") -set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld") -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined") +## +## INCLUDE +## +include(GNUInstallDirs) +include(StandardProjectSettings) +include(CompilerWarnings) +include(EnableCcache) +include(ClangTidy) +include(FetchContent) -set(CMAKE_C_FLAGS "-fno-rtti -Wno-non-virtual-dtor -Wno-strict-aliasing -Wl,-Bdynamic -Wno-unused-command-line-argument") -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++20") +FetchContent_Declare(ftxui + GIT_REPOSITORY "https://github.com/arthursonzogni/ftxui.git" + GIT_TAG "cecd54df42dd66fdf8386ed461e16b725bffc827" +) +FetchContent_MakeAvailable(ftxui) -set(CMAKE_C_FLAGS_RELEASE "-flto=thin -gsplit-dwarf -fno-stack-protector -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-math-errno -fno-ident -Wl,--strip-all -Wl,--discard-all") -set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") +FetchContent_Declare(fmt + GIT_REPOSITORY "https://github.com/fmtlib/fmt.git" + GIT_TAG "491ba2dda5a04c2438abb6bd90219de773793ec0" +) +FetchContent_MakeAvailable(fmt) -set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -g") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") +FetchContent_Declare(nlohmann_json + GIT_REPOSITORY "https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent.git" + GIT_TAG "v3.10.4" +) +FetchContent_MakeAvailable(nlohmann_json) -file(GLOB_RECURSE SOURCES "${CMAKE_SOURCE_DIR}/include/*.cpp" "${CMAKE_SOURCE_DIR}/resources/*.cpp" "${CMAKE_SOURCE_DIR}/src/*.cpp") +## +## CONFIGURATION +## +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto") -find_package(nlohmann_json REQUIRED) -find_package(ftxui REQUIRED) +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fwhole-program") +endif() -add_executable(CachyOS-Installer ${SOURCES} src/utils.h src/utils.cpp src/definitions.h) +# Link this 'library' to set the c++ standard / compile-time options requested +add_library(project_options INTERFACE) +target_compile_features(project_options INTERFACE cxx_std_20) -target_include_directories(CachyOS-Installer PRIVATE "${CMAKE_SOURCE_DIR}/include/") -target_link_libraries(CachyOS-Installer PRIVATE ftxui::screen PRIVATE ftxui::dom PRIVATE ftxui::component nlohmann_json::nlohmann_json) -target_link_options(CachyOS-Installer PRIVATE LINKER:--no-undefined) +## +## Target +## +add_executable(${PROJECT_NAME} + src/definitions.hpp + src/utils.cpp src/utils.hpp + src/main.cpp + ) + +# Link this 'library' to use the warnings specified in CompilerWarnings.cmake +add_library(project_warnings INTERFACE) +set_project_warnings(project_warnings) + +include_directories(${CMAKE_SOURCE_DIR}/src) + +target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options fmt::fmt ftxui::screen ftxui::dom ftxui::component nlohmann_json::nlohmann_json) + +option(ENABLE_UNITY "Enable Unity builds of projects" OFF) +if(ENABLE_UNITY) + # Add for any project you want to apply unity builds for + set_target_properties(${PROJECT_NAME} PROPERTIES UNITY_BUILD ON) +endif() + +install( + TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) + + +# uninstall +add_custom_target(uninstall + COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/cmake/ClangTidy.cmake b/cmake/ClangTidy.cmake new file mode 100644 index 0000000..cbe36ae --- /dev/null +++ b/cmake/ClangTidy.cmake @@ -0,0 +1,15 @@ + +option(ENABLE_TIDY "Enable clang-tidy [default: OFF]" OFF) +if(ENABLE_TIDY) + find_program(CLANG_TIDY_EXE + NAMES clang-tidy-9 clang-tidy-8 clang-tidy-7 clang-tidy + DOC "Path to clang-tidy executable") + if(NOT CLANG_TIDY_EXE) + message(STATUS "[clang-tidy] Not found.") + else() + message(STATUS "[clang-tidy] found: ${CLANG_TIDY_EXE}") + set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}") + endif() +else() + message(STATUS "[clang-tidy] Disabled.") +endif() diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake new file mode 100644 index 0000000..65834c5 --- /dev/null +++ b/cmake/CompilerWarnings.cmake @@ -0,0 +1,76 @@ +function(set_project_warnings project_name) + option(WARNINGS_AS_ERRORS "Treat compiler warnings as error" ON) + + set(MSVC_WARNINGS + /W4 # Base + /w14242 # Conversion + /w14254 # Operator convers. + /w14263 # Func member doesn't override + /w14265 # class has vfuncs, but destructor is not + /w14287 # unsigned/negative constant mismatch + /we4289 # nonstandard extension used: loop control var + /w14296 # expression is always 'boolean_value' + /w14311 # pointer trunc from one tipe to another + /w14545 # expression before comma evaluates to a function which missign an argument list + + /w14546 # function call before comma missing argument list + /w14547 # operator before comma has no effect; expected operator with side-effect + /w14549 # operator before comma has no effect; did you intend operator? + + /w14555 # expresion has no effect; expected expression with side-effect + /w14619 # pragma warning + /w14640 # Enable warning on thread; static member + + /w14826 # Conversion from one tipe to another is sign-extended cause unexpected runtime behavior. + /w14928 # illegal copy-initialization; more than user-defined. + /X + /constexpr + ) + + set(CLANG_WARNINGS + -Wall + -Wextra # standard + -Wshadow + + -Wnon-virtual-dtor + + -Wold-style-cast # c-style cast + -Wcast-align + -Wunused + -Woverloaded-virtual + + -Wpedantic # non-standard C++ + -Wconversion # type conversion that may lose data + -Wsign-conversion + -Wnull-dereference + -Wdouble-promotion # float to double + + -Wformat=2 + ) + + if(WARNINGS_AS_ERRORS) + set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror) + set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX) + endif() + + set(GCC_WARNINGS + ${CLANG_WARNINGS} + -Wmisleading-indentation + + -Wduplicated-cond + -Wduplicated-branches + -Wlogical-op + + -Wuseless-cast + ) + + if(MSVC) + set(PROJECT_WARNINGS ${MSVC_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + set(PROJECT_WARNINGS ${CLANG_WARNINGS}) + else() + set(PROJECT_WARNINGS ${GCC_WARNINGS}) + endif() + + target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS}) +endfunction() diff --git a/cmake/EnableCcache.cmake b/cmake/EnableCcache.cmake new file mode 100644 index 0000000..bc51a8b --- /dev/null +++ b/cmake/EnableCcache.cmake @@ -0,0 +1,13 @@ +# Setup ccache. +# +# The ccache is auto-enabled if the tool is found. +# To disable set -DCCACHE=OFF option. +if(NOT DEFINED CMAKE_CXX_COMPILER_LAUNCHER) + find_program(CCACHE ccache DOC "ccache tool path; set to OFF to disable") + if(CCACHE) + set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE}) + message(STATUS "[ccache] Enabled: ${CCACHE}") + else() + message(STATUS "[ccache] Disabled.") + endif() +endif() diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake new file mode 100644 index 0000000..59c36aa --- /dev/null +++ b/cmake/StandardProjectSettings.cmake @@ -0,0 +1,42 @@ +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui, ccmake + set_property( + CACHE CMAKE_BUILD_TYPE + PROPERTY STRINGS + "Debug" + "Release" + "MinSizeRel" + "RelWithDebInfo") +endif() + +# Generate compile_commands.json to make it easier to work with clang based tools +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) + +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported( + RESULT + result + OUTPUT + output) + if(result) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON) + else() + message(SEND_ERROR "IPO is not supported: ${output}") + endif() +endif() +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + add_compile_options(-fcolor-diagnostics) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-fdiagnostics-color=always) +else() + message(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.") +endif() + diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..961066a --- /dev/null +++ b/meson.build @@ -0,0 +1,90 @@ +project('cachyos-installer', 'cpp', + version: '0.0.1', + license: 'GPLv3', + meson_version: '>=0.55.0', + default_options: ['cpp_std=c++20', + 'buildtype=debugoptimized', + 'warning_level=3', + 'werror=true', + 'b_ndebug=if-release']) + +cc = meson.get_compiler('cpp') + +# Common dependencies +fmt = dependency('fmt', version : ['>=8.0.0'], fallback : ['fmt', 'fmt_dep']) +ftxui = dependency('ftxui', version : ['>=0.11.1'], modules : ['ftxui::screen', 'ftxui::dom', 'ftxui::component'], fallback : ['ftxui', 'ftxui_dep']) +nlohmann_json = dependency('nlohmann_json', version : ['>=3.10.4'], fallback : ['nlohmann_json', 'nlohmann_json_dep']) + +src_files = files( + 'src/definitions.hpp', + 'src/utils.cpp', 'src/utils.hpp', + 'src/main.cpp', +) + +possible_cc_flags = [ + '-Wshadow', + + '-Wnon-virtual-dtor', + + '-Wcast-align', + '-Wunused', + '-Woverloaded-virtual', + + '-Wpedantic', # non-standard C++ + '-Wconversion', # type conversion that may lose data + '-Wnull-dereference', + '-Wdouble-promotion', # float to double + + '-Wformat=2', +] + +if cc.get_id() == 'gcc' + possible_cc_flags += [ + '-Wmisleading-indentation', + + '-Wduplicated-cond', + '-Wduplicated-branches', + '-Wlogical-op', + ] +endif + +if get_option('buildtype') != 'debug' + if cc.get_id() == 'gcc' + possible_cc_flags += [ + '-flto', + '-fwhole-program', + ] + else + possible_cc_flags += [ + '-flto=thin', + ] + endif +endif + +add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : 'cpp') + +executable( + 'cachyos-installer', + src_files, + dependencies: [fmt, ftxui, nlohmann_json], + include_directories: [include_directories('src')], + install: true) + +summary( + { + 'Build type': get_option('buildtype'), + }, + bool_yn: true +) + +clangtidy = find_program('clang-tidy', required: false) + +if clangtidy.found() + run_target( + 'tidy', + command: [ + clangtidy, + '-checks=*,-fuchsia-default-arguments', + '-p', meson.build_root() + ] + src_files) +endif diff --git a/src/definitions.hpp b/src/definitions.hpp new file mode 100644 index 0000000..195ff3d --- /dev/null +++ b/src/definitions.hpp @@ -0,0 +1,31 @@ +#ifndef DEFINITIONS_HPP +#define DEFINITIONS_HPP + +#include +#include + +static constexpr auto RESET = "\033[0m"; +static constexpr auto BLACK = "\033[30m"; /* Black */ +static constexpr auto RED = "\033[31m"; /* Red */ +static constexpr auto GREEN = "\033[32m"; /* Green */ +static constexpr auto YELLOW = "\033[33m"; /* Yellow */ +static constexpr auto BLUE = "\033[34m"; /* Blue */ +static constexpr auto MAGENTA = "\033[35m"; /* Magenta */ +static constexpr auto CYAN = "\033[36m"; /* Cyan */ +static constexpr auto WHITE = "\033[37m"; /* White */ +static constexpr auto BOLDBLACK = "\033[1m\033[30m"; /* Bold Black */ +static constexpr auto BOLDRED = "\033[1m\033[31m"; /* Bold Red */ +static constexpr auto BOLDGREEN = "\033[1m\033[32m"; /* Bold Green */ +static constexpr auto BOLDYELLOW = "\033[1m\033[33m"; /* Bold Yellow */ +static constexpr auto BOLDBLUE = "\033[1m\033[34m"; /* Bold Blue */ +static constexpr auto BOLDMAGENTA = "\033[1m\033[35m"; /* Bold Magenta */ +static constexpr auto BOLDCYAN = "\033[1m\033[36m"; /* Bold Cyan */ +static constexpr auto BOLDWHITE = "\033[1m\033[37m"; /* Bold White */ + +#define output(...) fmt::print(__VA_ARGS__) +#define error(...) fmt::print(stderr, fmt::fg(fmt::color::red), __VA_ARGS__) +#define warning(...) fmt::print(fmt::fg(fmt::color::yellow), __VA_ARGS__) +#define info(...) fmt::print(fmt::fg(fmt::color::cyan), __VA_ARGS__) +#define success(...) fmt::print(fmt::fg(fmt::color::green), __VA_ARGS__) + +#endif diff --git a/src/main.cpp b/src/main.cpp index 1762730..6de5c5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,36 +1,36 @@ -#include "definitions.h" -#include "utils.h" -#include -#include +#include "definitions.hpp" +#include "utils.hpp" -int main() -{ - if (!Utils::isConnected()) { - warning("It seems you are not connected, waiting for 15s before retrying..."); - sleep(15); +#include - if (!Utils::isConnected()) { - char type = '\0'; +static constexpr int32_t SLEEP_TIMEOUT = 15; - while (Utils::promptForChar("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", type, RED)) { - if (type != 'n') { +int main() { + if (!utils::is_connected()) { + warning("It seems you are not connected, waiting for 15s before retrying...\n"); + std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIMEOUT)); - info("\nInstructions to connect to wifi using iwclt:"); - info("1 - Find your wifi device name ex: wlan0"); - info("2 - type `station wlan0 scan`, and wait couple seconds"); - info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)"); - info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!"); - info("5 - type `station wlan0 show` (status should be connected)"); - info("6 - type `exit`\n"); + if (!utils::is_connected()) { + char type = '\0'; - while (Utils::promptForChar("Press a key to continue...", type, CYAN)) { - Utils::exec("iwctl"); - break; - } - } + while (utils::prompt_char("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", type, RED)) { + if (type != 'n') { - break; - } - } - } + info("\nInstructions to connect to wifi using iwclt:\n"); + info("1 - Find your wifi device name ex: wlan0\n"); + info("2 - type `station wlan0 scan`, and wait couple seconds\n"); + info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n"); + info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n"); + info("5 - type `station wlan0 show` (status should be connected)\n"); + info("6 - type `exit`\n"); + while (utils::prompt_char("Press a key to continue...", type, CYAN)) { + utils::exec("iwctl"); + break; + } + } + + break; + } + } + } } diff --git a/src/utils.cpp b/src/utils.cpp index b318fe4..cdce4d7 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,48 +1,50 @@ -#include "utils.h" -#include +#include "utils.hpp" + +#include #include -bool Utils::isConnected() -{ - return gethostbyname("google.com"); +#include + +namespace utils { +bool is_connected() noexcept { + return gethostbyname("google.com"); } -string Utils::exec(const string &command) -{ - char buffer[128]; - string result; +std::string exec(const std::string_view& command) noexcept { + char buffer[128]; + std::string result; - FILE *pipe = popen(command.c_str(), "r"); + FILE* pipe = popen(command.data(), "r"); - if (!pipe) { - return "popen failed!"; - } + if (!pipe) { + return "popen failed!"; + } - while (!feof(pipe)) { - if (fgets(buffer, 128, pipe) != nullptr) { - result += buffer; - } - } + while (!feof(pipe)) { + if (fgets(buffer, 128, pipe) != nullptr) { + result += buffer; + } + } - pclose(pipe); + pclose(pipe); - return result; + return result; } -bool Utils::promptForChar(const char *prompt, char &read, const char *colour) -{ - std::string tmp; - std::cout << colour << prompt << std::endl; +bool prompt_char(const char* prompt, char& read, const char* color) noexcept { + fmt::print("{}{}{}\n", color, prompt, RESET); - if (std::getline(std::cin, tmp)) { - if (tmp.length() == 1) { - read = tmp[0]; - } else { - read = '\0'; - } + std::string tmp; + if (std::getline(std::cin, tmp)) { + if (tmp.length() == 1) { + read = tmp[0]; + } else { + read = '\0'; + } - return true; - } + return true; + } - return false; + return false; } +} // namespace utils diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..7437801 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,16 @@ +#ifndef UTILS_HPP +#define UTILS_HPP + +#include "definitions.hpp" + +#include + +namespace utils { +void print_banner() noexcept; +bool is_connected() noexcept; +bool prompt_char(const char* prompt, char& read, const char* color = RESET) noexcept; + +std::string exec(const std::string_view& command) noexcept; +} // namespace utils + +#endif // UTILS_HPP diff --git a/subprojects/fmt.wrap b/subprojects/fmt.wrap new file mode 100644 index 0000000..9a1b8d4 --- /dev/null +++ b/subprojects/fmt.wrap @@ -0,0 +1,8 @@ +[wrap-git] +url = https://github.com/fmtlib/fmt.git +revision = 491ba2dda5a04c2438abb6bd90219de773793ec0 + +patch_directory = fmt + +[provide] +fmt = fmt_dep diff --git a/subprojects/ftxui.wrap b/subprojects/ftxui.wrap new file mode 100644 index 0000000..c712be0 --- /dev/null +++ b/subprojects/ftxui.wrap @@ -0,0 +1,8 @@ +[wrap-git] +url = https://github.com/arthursonzogni/ftxui.git +revision = cecd54df42dd66fdf8386ed461e16b725bffc827 + +patch_directory = ftxui + +[provide] +ftxui = ftxui_dep diff --git a/subprojects/nlohmann_json.wrap b/subprojects/nlohmann_json.wrap new file mode 100644 index 0000000..d5d4754 --- /dev/null +++ b/subprojects/nlohmann_json.wrap @@ -0,0 +1,10 @@ +[wrap-file] +directory = nlohmann_json-3.10.4 +lead_directory_missing = true + +source_url = https://github.com/nlohmann/json/releases/download/v3.10.4/include.zip +source_filename = nlohmann_json-3.10.4.zip +source_hash = 62c585468054e2d8e7c2759c0d990fd339d13be988577699366fe195162d16cb + +[provide] +nlohmann_json = nlohmann_json_dep diff --git a/subprojects/packagefiles/fmt/meson.build b/subprojects/packagefiles/fmt/meson.build new file mode 100644 index 0000000..fe20724 --- /dev/null +++ b/subprojects/packagefiles/fmt/meson.build @@ -0,0 +1,34 @@ +project('fmt', 'cpp', + version : '8.0.1', + license : 'BSD', + default_options : ['cpp_std=c++14'] +) + +fmt_private_cpp_args = [ ] +fmt_interface_cpp_args = [ ] +libtype = get_option('default_library') +if libtype == 'shared' + fmt_private_cpp_args += [ '-DFMT_EXPORT' ] + fmt_interface_cpp_args += [ '-DFMT_SHARED' ] +endif + +fmt_inc = include_directories('include') +fmt_lib = library('fmt', + sources : [ + 'src/format.cc', + 'src/os.cc' + ], + cpp_args : fmt_private_cpp_args, + include_directories : fmt_inc +) + +fmt_dep = declare_dependency( + include_directories : fmt_inc, + compile_args : fmt_interface_cpp_args, + link_with : fmt_lib +) + +fmt_header_only_dep = declare_dependency( + include_directories : fmt_inc, + compile_args : '-DFMT_HEADER_ONLY' +) diff --git a/subprojects/packagefiles/ftxui/meson.build b/subprojects/packagefiles/ftxui/meson.build new file mode 100644 index 0000000..838aaad --- /dev/null +++ b/subprojects/packagefiles/ftxui/meson.build @@ -0,0 +1,106 @@ +project('ftxui', 'cpp', + version: '0.11.1', + license : 'MIT', + default_options: ['cpp_std=c++17'] +) + +cpp = meson.get_compiler('cpp') +thread_dep = dependency('threads') + +ftxui_inc = include_directories('include') +ftxui_priv_inc = include_directories('src') +ftxui_screen_lib = static_library('ftxui_screen', + sources : [ + 'src/ftxui/screen/box.cpp', + 'src/ftxui/screen/color.cpp', + 'src/ftxui/screen/color_info.cpp', + 'src/ftxui/screen/screen.cpp', + 'src/ftxui/screen/string.cpp', + 'src/ftxui/screen/terminal.cpp', + 'include/ftxui/screen/box.hpp', + 'include/ftxui/screen/color.hpp', + 'include/ftxui/screen/color_info.hpp', + 'include/ftxui/screen/screen.hpp', + 'include/ftxui/screen/string.hpp' + ], + include_directories : ftxui_inc +) + +ftxui_dom_lib = static_library('ftxui_dom', + sources : [ + 'include/ftxui/dom/elements.hpp', + 'include/ftxui/dom/node.hpp', + 'include/ftxui/dom/requirement.hpp', + 'include/ftxui/dom/take_any_args.hpp', + 'src/ftxui/dom/blink.cpp', + 'src/ftxui/dom/bold.cpp', + 'src/ftxui/dom/border.cpp', + 'src/ftxui/dom/box_helper.hpp', + 'src/ftxui/dom/box_helper.cpp', + 'src/ftxui/dom/clear_under.cpp', + 'src/ftxui/dom/color.cpp', + 'src/ftxui/dom/composite_decorator.cpp', + 'src/ftxui/dom/dbox.cpp', + 'src/ftxui/dom/dim.cpp', + 'src/ftxui/dom/flex.cpp', + 'src/ftxui/dom/frame.cpp', + 'src/ftxui/dom/gauge.cpp', + 'src/ftxui/dom/graph.cpp', + 'src/ftxui/dom/gridbox.cpp', + 'src/ftxui/dom/hbox.cpp', + 'src/ftxui/dom/hflow.cpp', + 'src/ftxui/dom/inverted.cpp', + 'src/ftxui/dom/node.cpp', + 'src/ftxui/dom/node_decorator.hpp', + 'src/ftxui/dom/node_decorator.cpp', + 'src/ftxui/dom/paragraph.cpp', + 'src/ftxui/dom/reflect.cpp', + 'src/ftxui/dom/scroll_indicator.cpp', + 'src/ftxui/dom/separator.cpp', + 'src/ftxui/dom/size.cpp', + 'src/ftxui/dom/spinner.cpp', + 'src/ftxui/dom/table.cpp', + 'src/ftxui/dom/text.cpp', + 'src/ftxui/dom/underlined.cpp', + 'src/ftxui/dom/util.cpp', + 'src/ftxui/dom/vbox.cpp' + ], + include_directories : [ftxui_inc, ftxui_priv_inc] +) + +ftxui_component_lib = static_library('ftxui_component', + sources : [ + 'include/ftxui/component/captured_mouse.hpp', + 'include/ftxui/component/component.hpp', + 'include/ftxui/component/component_base.hpp', + 'include/ftxui/component/event.hpp', + 'include/ftxui/component/mouse.hpp', + 'include/ftxui/component/receiver.hpp', + 'include/ftxui/component/screen_interactive.hpp', + 'src/ftxui/component/button.cpp', + 'src/ftxui/component/catch_event.cpp', + 'src/ftxui/component/checkbox.cpp', + 'src/ftxui/component/component.cpp', + 'src/ftxui/component/container.cpp', + 'src/ftxui/component/dropdown.cpp', + 'src/ftxui/component/event.cpp', + 'src/ftxui/component/input.cpp', + 'src/ftxui/component/maybe.cpp', + 'src/ftxui/component/menu.cpp', + 'src/ftxui/component/radiobox.cpp', + 'src/ftxui/component/radiobox.cpp', + 'src/ftxui/component/renderer.cpp', + 'src/ftxui/component/resizable_split.cpp', + 'src/ftxui/component/screen_interactive.cpp', + 'src/ftxui/component/slider.cpp', + 'src/ftxui/component/terminal_input_parser.cpp', + 'src/ftxui/component/terminal_input_parser.hpp', + 'src/ftxui/component/toggle.cpp' + ], + include_directories : [ftxui_inc, ftxui_priv_inc] +) + +ftxui_dep = declare_dependency( + include_directories : ftxui_inc, + link_with : [ftxui_screen_lib, ftxui_dom_lib, ftxui_component_lib] +)