Implemented most build settings as constexpr functions and ported most of macro-based conditional compilation to if constexpr(); this significantly improves code quality and the chance of build success on different platforms and build configurations.

More code modernization.
This commit is contained in:
Alexander Shaduri
2022-01-18 15:30:55 +04:00
parent 235fba8a04
commit 88e9e8c2f7
25 changed files with 542 additions and 416 deletions
+43 -40
View File
@@ -16,6 +16,7 @@ Copyright:
#include "rconfig/rconfig.h"
#include "app_pcrecpp.h"
#include "hz/fs.h"
#include "build_config.h"
@@ -23,54 +24,56 @@ hz::fs::path get_smartctl_binary()
{
auto smartctl_binary = hz::fs::u8path(rconfig::get_data<std::string>("system/smartctl_binary"));
#ifdef _WIN32
// Look in smartmontools installation directory.
hz::fs::path system_binary;
do {
bool use_smt = rconfig::get_data<bool>("system/win32_search_smartctl_in_smartmontools");
if (!use_smt)
break;
if constexpr(BuildEnv::is_kernel_family_windows()) {
// Look in smartmontools installation directory.
hz::fs::path system_binary;
do {
bool use_smt = rconfig::get_data<bool>("system/win32_search_smartctl_in_smartmontools");
if (!use_smt)
break;
auto smt_regpath = rconfig::get_data<std::string>("system/win32_smartmontools_regpath");
auto smt_regpath_wow = rconfig::get_data<std::string>("system/win32_smartmontools_regpath_wow"); // same as above, but with WOW6432Node
auto smt_regkey = rconfig::get_data<std::string>("system/win32_smartmontools_regkey");
auto smt_smartctl = rconfig::get_data<std::string>("system/win32_smartmontools_smartctl_binary");
auto smt_regpath = rconfig::get_data<std::string>("system/win32_smartmontools_regpath");
auto smt_regpath_wow = rconfig::get_data<std::string>("system/win32_smartmontools_regpath_wow"); // same as above, but with WOW6432Node
auto smt_regkey = rconfig::get_data<std::string>("system/win32_smartmontools_regkey");
auto smt_smartctl = rconfig::get_data<std::string>("system/win32_smartmontools_smartctl_binary");
if ((smt_regpath.empty() && smt_regpath_wow.empty()) || smt_regkey.empty() || smt_smartctl.empty())
break;
if ((smt_regpath.empty() && smt_regpath_wow.empty()) || smt_regkey.empty() || smt_smartctl.empty())
break;
std::string smt_inst_dir;
hz::win32_get_registry_value_string(HKEY_LOCAL_MACHINE, smt_regpath, smt_regkey, smt_inst_dir);
if (smt_inst_dir.empty()) {
hz::win32_get_registry_value_string(HKEY_LOCAL_MACHINE, smt_regpath_wow, smt_regkey, smt_inst_dir);
}
std::string smt_inst_dir;
#ifdef _WIN32
hz::win32_get_registry_value_string(HKEY_LOCAL_MACHINE, smt_regpath, smt_regkey, smt_inst_dir);
if (smt_inst_dir.empty()) {
hz::win32_get_registry_value_string(HKEY_LOCAL_MACHINE, smt_regpath_wow, smt_regkey, smt_inst_dir);
}
#endif
if (smt_inst_dir.empty()) {
debug_out_info("app", DBG_FUNC_MSG << "Smartmontools installation not found in \"HKLM\\"
<< smt_regpath << "\\" << smt_regkey << "\".\n");
break;
}
debug_out_info("app", DBG_FUNC_MSG << "Smartmontools installation found at \"" << smt_inst_dir
<< "\", using \"" << smt_smartctl << "\".\n");
if (smt_inst_dir.empty()) {
debug_out_info("app", DBG_FUNC_MSG << "Smartmontools installation not found in \"HKLM\\"
<< smt_regpath << "\\" << smt_regkey << "\".\n");
break;
}
debug_out_info("app", DBG_FUNC_MSG << "Smartmontools installation found at \"" << smt_inst_dir
<< "\", using \"" << smt_smartctl << "\".\n");
auto p = hz::fs::u8path(smt_inst_dir) / hz::fs::u8path(smt_smartctl);
auto p = hz::fs::u8path(smt_inst_dir) / hz::fs::u8path(smt_smartctl);
if (!hz::fs::exists(p) || !hz::fs::is_regular_file(p))
break;
if (!hz::fs::exists(p) || !hz::fs::is_regular_file(p))
break;
system_binary = p;
} while (false);
system_binary = p;
} while (false);
if (!system_binary.empty()) {
smartctl_binary = system_binary;
if (!system_binary.empty()) {
smartctl_binary = system_binary;
} else if (smartctl_binary.is_relative()) {
// If smartctl path is relative, and it's Windows, and the package seems to contain smartctl, use our own binary.
if (auto app_dir = hz::fs_get_application_dir(); !app_dir.empty() && hz::fs::exists(app_dir / smartctl_binary)) {
smartctl_binary = app_dir / smartctl_binary;
} else if (smartctl_binary.is_relative()) {
// If smartctl path is relative, and it's Windows, and the package seems to contain smartctl, use our own binary.
if (auto app_dir = hz::fs_get_application_dir(); !app_dir.empty() && hz::fs::exists(app_dir / smartctl_binary)) {
smartctl_binary = app_dir / smartctl_binary;
}
}
}
#endif
return smartctl_binary;
}
@@ -81,15 +84,15 @@ std::string execute_smartctl(const std::string& device, const std::string& devic
const std::string& command_options,
std::shared_ptr<CommandExecutor> smartctl_ex, std::string& smartctl_output)
{
#ifndef _WIN32 // win32 doesn't have slashes in devices names
{
// win32 doesn't have slashes in devices names. For others, check that slash is present.
if constexpr(!BuildEnv::is_kernel_family_windows()) {
std::string::size_type pos = device.rfind('/'); // find basename
if (pos == std::string::npos) {
debug_out_error("app", DBG_FUNC_MSG << "Invalid device name \"" << device << "\".\n");
return _("Invalid device name specified.");
}
}
#endif
if (!smartctl_ex) // if it doesn't exist, create a default one
smartctl_ex = std::make_shared<SmartctlExecutor>();