Skip to content

Commit

Permalink
Add fmtlib for older compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
Febbe committed Apr 19, 2024
1 parent c2fdaeb commit b7948f4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 12 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ include(GitRepo)

## Libraries ##
##############################
# c++17 support
# c++20 support
##############################
#find_package(CXX17 REQUIRED COMPONENTS optional)
#find_package(Filesystem REQUIRED COMPONENTS ghc Final Experimental)

find_package(format)

find_package(Backtrace)
if (Backtrace_FOUND)
Expand Down Expand Up @@ -197,6 +197,7 @@ target_link_libraries(external_modules INTERFACE
$<$<TARGET_EXISTS:X11::Xi>:X11::Xi>
$<$<TARGET_EXISTS:X11::X11>:X11::X11>
$<$<TARGET_EXISTS:Backtrace::backtrace>:Backtrace::backtrace>
$<$<TARGET_EXISTS:fmt::fmt>:fmt::fmt>
ZLIB::ZLIB
Threads::Threads
)
Expand Down Expand Up @@ -372,7 +373,8 @@ Configuration:
X11 support enabled: ${X11_FOUND}
GTEST enabled: ${ENABLE_GTEST}
GCOV enabled: ${DEV_ENABLE_GCOV}
Filesystem library: ${CXX_FILESYSTEM_NAMESPACE}
Filesystem library: std::filesystem
Format library: ${FORMAT_LIB}
Profiling enabled: ${ENABLE_PROFILING}
")

Expand Down
18 changes: 18 additions & 0 deletions cmake/find/Find-format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.14)

enable_language(CXX)

option(XOJ_USE_FMTLIB "Use fmtlib" OFF)
include(xoj_check_cxx_definition)
xoj_check_cxx_definition("__cpp_lib_format >= 202110L" "format" _XOJ_HAS_INCLUDE_FORMAT)

if(_XOJ_HAS_INCLUDE_FORMAT AND NOT XOJ_USE_FMTLIB)
message(STATUS "Found <format>")
set(FORMAT_LIB "std::format")
else()
message(STATUS "Could not find <format>, fetch {fmt}")
include(get_cpm)
CPMAddPackage("gh:fmtlib/fmt#7.1.3")
set(FORMAT_LIB "{fmt}")
endif()
set(format_FOUND TRUE)
24 changes: 24 additions & 0 deletions cmake/include/get_cpm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.39.0)
set(CPM_HASH_SUM "66639bcac9dd2907b2918de466783554c1334446b9874e90d38e3778d404c2ef")

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})
20 changes: 20 additions & 0 deletions cmake/include/xoj_check_cxx_definition.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Define the function
function(xoj_check_cxx_definition definition header variable)
set(BINARY_DIR ${CMAKE_BINARY_DIR}/xoj_check_cxx_definition)
set(SOURCE_FILE ${CMAKE_BINARY_DIR}/xoj_check_cxx_definition/${variable}.cpp)
file(WRITE ${SOURCE_FILE} "#include <${header}> \n#if !(${definition}) \n#error \n#endif\nint main(){}")
try_compile(${variable}
${BINARY_DIR}
${SOURCE_FILE}
CXX_STANDARD 20
CXX_STANDARD_REQUIRED TRUE
OUTPUT_VARIABLE _OUT_ERROR
)
if(${variable})
message(STATUS "${header} defines ${definition}")
else()
message(STATUS "${header} does not define ${definition}")
message(STATUS "${_OUT_ERROR}")
endif()
file(REMOVE ${SOURCE_FILE})
endfunction()
16 changes: 8 additions & 8 deletions src/core/control/Control.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "Control.h"

#include <algorithm> // for max
#include <cstdlib> // for size_t
#include <exception> // for exce...
#include <format>
#include <algorithm> // for max
#include <cstdlib> // for size_t
#include <exception> // for exce...
#include <functional> // for bind
#include <iterator> // for end
#include <memory> // for make...
Expand Down Expand Up @@ -112,6 +111,7 @@
#include "UndoRedoController.h" // for Undo...
#include "config-dev.h" // for SETT...
#include "config.h" // for PROJ...
#include "format.h" // for fmt::format

using std::string;

Expand Down Expand Up @@ -1812,29 +1812,29 @@ void Control::updateWindowTitle() {
title = _("Unsaved Document");
} else {
if (settings->isPageNumberInTitlebarShown()) {
title += std::format("[{}/{}] ", getCurrentPageNo() + 1, doc->getPageCount());
title += fmt::format("[{}/{}] ", getCurrentPageNo() + 1, doc->getPageCount());
}
if (undoRedo->isChanged()) {
title += "*";
}

if (settings->isFilepathInTitlebarShown()) {
title += std::format("[{}] - {}", char_cast(doc->getPdfFilepath().parent_path().u8string()),
title += fmt::format("[{}] - {}", char_cast(doc->getPdfFilepath().parent_path().u8string()),
char_cast(doc->getPdfFilepath().filename().u8string()));
} else {
title += char_cast(doc->getPdfFilepath().filename().u8string());
}
}
} else {
if (settings->isPageNumberInTitlebarShown()) {
title += ("[" + std::to_string(getCurrentPageNo() + 1) + "/" + std::to_string(doc->getPageCount()) + "] ");
title += fmt::format("[{}/{}] ", getCurrentPageNo() + 1 , doc->getPageCount());
}
if (undoRedo->isChanged()) {
title += "*";
}

if (settings->isFilepathInTitlebarShown()) {
title += (std::format("[{}] - {}", char_cast(doc->getFilepath().parent_path().u8string()),
title += (fmt::format("[{}] - {}", char_cast(doc->getFilepath().parent_path().u8string()),
char_cast(doc->getFilepath().filename().u8string())));
} else {
title += char_cast(doc->getFilepath().filename().u8string());
Expand Down
41 changes: 41 additions & 0 deletions src/util/include/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

// define XOJ_USE_STD_FORMAT if std::format should be forced

#if !defined(XOJ_USE_STD_FORMAT) && __has_include(<format>)
#include <format> // IWYU pragma: export
#if defined(__cpp_lib_format) && __cpp_lib_format >= 202110L
#define XOJ_USE_STD_FORMAT = 1
#endif
#endif

#if !defined(XOJ_USE_STD_FORMAT)
#include <fmt/format.h> // IWYU pragma: export
#else // Include all the symbols from std::format into the fmt namespace
namespace fmt {
using std::basic_format_arg;
using std::basic_format_args;
using std::basic_format_context;
using std::basic_format_parse_context;
using std::basic_format_string;
using std::format;
using std::format_args;
using std::format_context;
using std::format_error;
using std::format_parse_context;
using std::format_string;
using std::format_to;
using std::format_to_n;
using std::formatted_size;
using std::formatter;
using std::make_format_args;
using std::make_wformat_args;
using std::vformat;
using std::vformat_to;
using std::visit_format_arg;
using std::wformat_args;
using std::wformat_context;
using std::wformat_parse_context;
using std::wformat_string;
} // namespace fmt
#endif

0 comments on commit b7948f4

Please sign in to comment.