From e09e7825369ec8217456065d31dbbbb992b7f1bb Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Wed, 31 May 2023 20:15:08 -0400 Subject: meta: Use system libraries to compile Also: try to make pqxx not use local socket for connections (unsuccessful) Change-Id: I351fe1a405a7c204e1276a90da75ab3d32f51bdc --- CMakeLists.txt | 45 ++++++++------------------------------------- cmake/Findcpr.cmake | 14 ++++++++++++++ cmake/Findcurl.cmake | 14 ++++++++++++++ cmake/Finddpp.cmake | 14 ++++++++++++++ cmake/Findspdlog.cmake | 14 ++++++++++++++ include/142bot/db.hpp | 2 +- src/bot.cpp | 5 ++++- src/db.cpp | 25 ++----------------------- 8 files changed, 71 insertions(+), 62 deletions(-) create mode 100644 cmake/Findcpr.cmake create mode 100644 cmake/Findcurl.cmake create mode 100644 cmake/Finddpp.cmake create mode 100644 cmake/Findspdlog.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e5159b6..ffbe96a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,44 +1,17 @@ cmake_minimum_required(VERSION 3.0) -project(onefortytwobot VERSION 0.4 LANGUAGES CXX C) +project(onefortytwobot VERSION 0.5 LANGUAGES CXX C) set(CMAKE_CXX_STANDARD 20 REQUIRED) set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE OPT_IN) -find_package(spdlog) -find_package(dpp) -#find_package(cpr REQUIRED) +list(APPEND CMAKE_MODULE_PATH + "${CMAKE_SOURCE_DIR}/cmake") -include(FetchContent) -FetchContent_Declare(clog - GIT_REPOSITORY https://git.devcara.com/clog - GIT_TAG 0.1.0 -) - -FetchContent_Declare(dpp - GIT_REPOSITORY https://github.com/brainboxdotcc/DPP - GIT_TAG v10.0.23 - FIND_PACKAGE_ARGS -) - -FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git - GIT_TAG 871ed52d350214a034f6ef8a3b8f51c5ce1bd400 - FIND_PACKAGE_ARGS) # The commit hash for 1.9.0. Replace with the latest from: https://github.com/libcpr/cpr/releases - -FetchContent_Declare(fmt GIT_REPOSITORY https://github.com/fmtlib/fmt - GIT_TAG 9.1.0) - -FetchContent_Declare(spdlog - GIT_REPOSITORY https://github.com/gabime/spdlog - GIT_TAG v1.1.0 - FIND_PACKAGE_ARGS NAMES spdlog - ) - -FetchContent_Declare(sentry - GIT_REPOSITORY https://github.com/getsentry/sentry-native - GIT_TAG 0.6.1 -) - -FetchContent_MakeAvailable(clog dpp cpr fmt spdlog sentry) +find_package(spdlog REQUIRED) +find_package(dpp REQUIRED) +find_package(cpr REQUIRED) +find_package(sentry REQUIRED) +find_package(curl) include(cmake/FindPCRE.cmake) @@ -59,7 +32,6 @@ add_executable(142bot ${coresrc}) include_directories("include" ${PROJECT_BINARY_DIR}) target_link_libraries(142bot PRIVATE clog dpp fmt::fmt spdlog::spdlog pcre cpr::cpr ${PQXX_LIB} ${PQ_LIB} sentry) -add_dependencies(142bot clog cpr fmt) install(TARGETS 142bot) @@ -71,7 +43,6 @@ foreach (fullmodname ${subdirlist}) set (modsrc "") aux_source_directory(${modules_dir}/${modname} modsrc) add_library(module_${modname} SHARED ${modsrc}) - add_dependencies(module_${modname} cpr fmt) target_link_libraries(module_${modname} dpp cpr sentry) set_target_properties(module_${modname} PROPERTIES PREFIX "") endforeach(fullmodname) diff --git a/cmake/Findcpr.cmake b/cmake/Findcpr.cmake new file mode 100644 index 0000000..a8aed9d --- /dev/null +++ b/cmake/Findcpr.cmake @@ -0,0 +1,14 @@ +include(FindPackageHandleStandardArgs) + +find_library(CPR_LIBRARY NAMES cpr) + +find_package_handle_standard_args(cpr REQUIRED_VARS CPR_LIBRARY) + +if (CPR_FOUND) + mark_as_advanced(CPR_LIBRARY) +endif() + +if(CPR_FOUND AND NOT TARGET cpr::cpr) + add_library(cpr::cpr IMPORTED UNKNOWN) + set_property(TARGET cpr::cpr PROPERTY IMPORTED_LOCATION ${CPR_LIBRARY}) +endif() \ No newline at end of file diff --git a/cmake/Findcurl.cmake b/cmake/Findcurl.cmake new file mode 100644 index 0000000..3ad12ea --- /dev/null +++ b/cmake/Findcurl.cmake @@ -0,0 +1,14 @@ +include(FindPackageHandleStandardArgs) + +find_library(CURL_LIBRARY NAMES curl) + +find_package_handle_standard_args(curl REQUIRED_VARS CURL_LIBRARY) + +if (CURL_FOUND) + mark_as_advanced(CURL_LIBRARY) +endif() + +if(CURL_FOUND AND NOT TARGET curl::curl) + add_library(CURL::libcurl IMPORTED UNKNOWN) + set_property(TARGET CURL::libcurl PROPERTY IMPORTED_LOCATION ${CURL_LIBRARY}) +endif() \ No newline at end of file diff --git a/cmake/Finddpp.cmake b/cmake/Finddpp.cmake new file mode 100644 index 0000000..92933b8 --- /dev/null +++ b/cmake/Finddpp.cmake @@ -0,0 +1,14 @@ +include(FindPackageHandleStandardArgs) + +find_library(DPP_LIBRARY NAMES dpp) + +find_package_handle_standard_args(dpp REQUIRED_VARS DPP_LIBRARY) + +if (DPP_FOUND) + mark_as_advanced(DPP_LIBRARY) +endif() + +if(DPP_FOUND AND NOT TARGET dpp::dpp) + add_library(dpp::dpp IMPORTED UNKNOWN) + set_property(TARGET dpp::dpp PROPERTY IMPORTED_LOCATION ${DPP_LIBRARY}) +endif() \ No newline at end of file diff --git a/cmake/Findspdlog.cmake b/cmake/Findspdlog.cmake new file mode 100644 index 0000000..e286724 --- /dev/null +++ b/cmake/Findspdlog.cmake @@ -0,0 +1,14 @@ +include(FindPackageHandleStandardArgs) + +find_library(SPDLOG_LIBRARY NAMES spdlog) + +find_package_handle_standard_args(spdlog REQUIRED_VARS SPDLOG_LIBRARY) + +if (SPDLOG_FOUND) + mark_as_advanced(SPDLOG_LIBRARY) +endif() + +if(SPDLOG_FOUND AND NOT TARGET spdlog::spdlog) + add_library(spdlog::spdlog IMPORTED UNKNOWN) + set_property(TARGET spdlog::spdlog PROPERTY IMPORTED_LOCATION ${SPDLOG_LIBRARY}) +endif() \ No newline at end of file diff --git a/include/142bot/db.hpp b/include/142bot/db.hpp index 505d6db..c7de707 100644 --- a/include/142bot/db.hpp +++ b/include/142bot/db.hpp @@ -31,7 +31,7 @@ namespace db { typedef std::vector> paramlist; - pqxx::connection connect(const std::string &host, const std::string &user, const std::string &pass, const std::string &db, int port); + pqxx::connection connect(const std::string cn_s); bool close(); resultset query(const std::string &format, const paramlist ¶meters); const std::string& error(); diff --git a/src/bot.cpp b/src/bot.cpp index b1d530c..8c3a84c 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -24,7 +24,10 @@ Bot::Bot(bool devel, dpp::cluster* cluster, char prefix, json &cfg) { std::string token = cfg.value("token", "bad-token"); - this->conn = db::connect(cfg["postgres"]["host"], cfg["postgres"]["user"], cfg["postgres"]["pass"], cfg["postgres"]["database"], cfg["postgres"]["port"]); + this->core->log(dpp::ll_debug, "Attempting DB connection"); + + + this->conn = db::connect(cfg.value("postgres", "postgres://localhost/142bot")); run_database_migrations(); diff --git a/src/db.cpp b/src/db.cpp index 120c9d4..038c06d 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -35,32 +35,11 @@ namespace db { /** * Connects to a postgres database, returns false if error **/ - pqxx::connection connect(const std::string &host, const std::string &user, const std::string &pass, const std::string &db, int port) { + pqxx::connection connect(const std::string cn_s) { std::lock_guard db_lock(db_mutex); - - std::string cn_s = "postgresql://"; - - if (!user.empty()) { - cn_s = cn_s + user; - } - if (!pass.empty() && !user.empty()) { - cn_s = cn_s + ":" + pass; - } - if ((!user.empty() || !pass.empty())) { - cn_s = cn_s + "@"; - } - - if (!host.empty()) { - cn_s = cn_s + "localhost"; - } - if (port != 0 && !host.empty()) { - cn_s = cn_s + ":" + std::to_string(port); - } - if (!db.empty()) { - cn_s = cn_s + "/" + db; - } + cout << cn_s << endl; sentry_value_t crumb = sentry_value_new_breadcrumb("default", "Started Database Connection"); sentry_value_set_by_key(crumb, "level", sentry_value_new_string("db")); -- cgit v1.2.3