From ef0ca794bc24f5439a3466db2ec54f03956e4ff0 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Sat, 24 Apr 2021 13:29:22 +0100 Subject: [PATCH] Use system libfmt by default if version >= 7.0.0 Allow non-strict ansi functions Cleanup --- 3rdParty/libfmt/CMakeLists.txt | 33 +++++++++++--------- CMakeLists.txt | 9 +++--- Source/DiabloUI/selgame.cpp | 11 +++---- Source/debug.cpp | 3 -- Source/stores.cpp | 6 ++-- Source/utils/format.hpp | 57 ---------------------------------- 6 files changed, 30 insertions(+), 89 deletions(-) delete mode 100644 Source/utils/format.hpp diff --git a/3rdParty/libfmt/CMakeLists.txt b/3rdParty/libfmt/CMakeLists.txt index aba617295..8caeaed0a 100644 --- a/3rdParty/libfmt/CMakeLists.txt +++ b/3rdParty/libfmt/CMakeLists.txt @@ -1,18 +1,23 @@ -if(NOT DEVILUTIONX_SYSTEM_LIBFMT) - include(FetchContent_MakeAvailableExcludeFromAll) +include(FetchContent_MakeAvailableExcludeFromAll) +if(NOT WIN32 AND NOT APPLE AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD) + # Enable POSIX extensions such as `readlink` and `ftruncate`. add_definitions(-D_POSIX_C_SOURCE=200809L) + # Defining _POSIX_C_SOURCE should be enough for the compiler not to define __STRICT_ANSI__ + # but some still do, so we also have to explicitly undefine it. + add_compile_options(-U__STRICT_ANSI__) +endif() - if(DEVILUTIONX_STATIC_LIBFMT) - set(BUILD_SHARED_LIBS OFF) - else() - set(BUILD_SHARED_LIBS ON) - endif() - include(FetchContent) - FetchContent_Declare(fmt - URL https://github.com/fmtlib/fmt/archive/7bdf0628b1276379886c7f6dda2cef2b3b374f0b.zip - URL_HASH MD5=70e0a0beda4bdac7debe60d380e1d802 - ) - FetchContent_MakeAvailableExcludeFromAll(fmt) - set(fmt_SOURCE_DIR ${fmt_SOURCE_DIR} PARENT_SCOPE) +if(DEVILUTIONX_STATIC_LIBFMT) + set(BUILD_SHARED_LIBS OFF) +else() + set(BUILD_SHARED_LIBS ON) endif() +include(FetchContent) +FetchContent_Declare(fmt + URL https://github.com/fmtlib/fmt/archive/7bdf0628b1276379886c7f6dda2cef2b3b374f0b.zip + URL_HASH MD5=70e0a0beda4bdac7debe60d380e1d802 +) +FetchContent_MakeAvailableExcludeFromAll(fmt) + +set(fmt_INCLUDE_DIR ${fmt_SOURCE_DIR} PARENT_SCOPE) diff --git a/CMakeLists.txt b/CMakeLists.txt index 272286d27..69ac11f53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ option(DEVILUTIONX_SYSTEM_LIBSODIUM "Use system-provided libsodium" ON) cmake_dependent_option(DEVILUTIONX_STATIC_LIBSODIUM "Link static libsodium" OFF "DEVILUTIONX_SYSTEM_LIBSODIUM AND NOT DIST" ON) -option(DEVILUTIONX_SYSTEM_LIBFMT "Use system-provided libfmt" OFF) +option(DEVILUTIONX_SYSTEM_LIBFMT "Use system-provided libfmt" ON) cmake_dependent_option(DEVILUTIONX_STATIC_LIBFMT "Link static libfmt" OFF "DEVILUTIONX_SYSTEM_LIBFMT AND NOT DIST" ON) @@ -176,8 +176,9 @@ endif() if(DEVILUTIONX_SYSTEM_LIBFMT) set(fmt_USE_STATIC_LIBS ${DEVILUTIONX_STATIC_LIBFMT}) - find_package(fmt REQUIRED) -else() + find_package(fmt 7.0.0 QUIET) +endif() +if(NOT fmt_FOUND) add_subdirectory(3rdParty/libfmt) endif() @@ -468,7 +469,7 @@ target_include_directories(${BIN_TARGET} PRIVATE 3rdParty/asio/include 3rdParty/Radon/Radon/include 3rdParty/libsmacker - ${fmt_SOURCE_DIR}/include + ${fmt_INCLUDE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}) if(NOT N3DS) diff --git a/Source/DiabloUI/selgame.cpp b/Source/DiabloUI/selgame.cpp index 352799e19..388fc2c10 100644 --- a/Source/DiabloUI/selgame.cpp +++ b/Source/DiabloUI/selgame.cpp @@ -11,8 +11,6 @@ #include "options.h" #include "storm/storm.h" -#include - namespace devilution { char selgame_Label[32]; @@ -412,11 +410,10 @@ static bool IsGameCompatible(GameData *data) if (data->programid != GAME_ID) { UiSelOkDialog(title, "The host is running a different game than you.", false); } else { - // An example of using a format with references. Note the references that allow a localized string to change the order of the inserted parameters. - // Note that named insertions would probably be better here, but this is just to provide an example. - auto msg = fmt::format("Your version {0} does not match the host {1}.{2}.{3}.", PROJECT_VERSION, PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH); - - UiSelOkDialog(title, msg.c_str(), false); + char msg[64]; + sprintf(msg, "Your version %s does not match the host %d.%d.%d.", PROJECT_VERSION, PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH); + + UiSelOkDialog(title, msg, false); } LoadBackgroundArt("ui_art\\selgame.pcx"); diff --git a/Source/debug.cpp b/Source/debug.cpp index b8087e54d..67bfae771 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -7,9 +7,6 @@ #include "cursor.h" #include "inv.h" #include "spells.h" -#include "utils/format.hpp" - -#include namespace devilution { diff --git a/Source/stores.cpp b/Source/stores.cpp index bbf7cf340..0bbd46b5e 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -6,7 +6,6 @@ #include "stores.h" #include -#include #include "cursor.h" #include "init.h" @@ -271,9 +270,8 @@ void S_StartSBuy() stextsize = true; stextscrl = true; stextsval = 0; - // An example of a named format insertion. - auto str = fmt::format("I have these items for sale: Your gold: {gold}", fmt::arg("gold", plr[myplr]._pGold)); - AddSText(0, 1, true, str.c_str(), COL_GOLD, false); + sprintf(tempstr, "I have these items for sale: Your gold: %i", plr[myplr]._pGold); + AddSText(0, 1, true, tempstr, COL_GOLD, false); AddSLine(3); AddSLine(21); S_ScrollSBuy(stextsval); diff --git a/Source/utils/format.hpp b/Source/utils/format.hpp deleted file mode 100644 index bd09087c0..000000000 --- a/Source/utils/format.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include "../player.h" - -#include - -namespace fmt { - -template <> -struct formatter : formatter { - template - auto format(devilution::PLR_MODE mode, FormatContext &ctx) - { - string_view name = "unknown"; - switch (mode) { - case devilution::PM_STAND: - name = "stand"; - break; - case devilution::PM_WALK: - name = "walk (N, NW, or NE)"; - break; - case devilution::PM_WALK2: - name = "walk (S, SW, or SE)"; - break; - case devilution::PM_WALK3: - name = "walk (W or E)"; - break; - case devilution::PM_ATTACK: - name = "attack"; - break; - case devilution::PM_RATTACK: - name = "ranged attack"; - break; - case devilution::PM_BLOCK: - name = "block"; - break; - case devilution::PM_GOTHIT: - name = "got hit"; - break; - case devilution::PM_DEATH: - name = "death"; - break; - case devilution::PM_SPELL: - name = "spell"; - break; - case devilution::PM_NEWLVL: - name = "new level"; - break; - case devilution::PM_QUIT: - name = "quit"; - break; - } - return formatter::format(name, ctx); - } -}; - -}