Skip to content

Commit

Permalink
Add an option to avoid wchar APIs on Windows
Browse files Browse the repository at this point in the history
With this, fmt can be used on Windows 98 and the Original Xbox with:

    set(FMT_OS OFF)

`FMT_WINDOWS_NO_WCHAR` is detected automatically in 2 cases:

1. Missing `<io.h>` on the original Xbox
2. A defined `WINVER` on properly configured Windows projects

In other cases, one can `add_definitions(FMT_WINDOWS_NO_WCHAR)`
in the fmt subproject.

Fixes #3631
  • Loading branch information
glebm committed Sep 17, 2023
1 parent d498754 commit 3045f5f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
16 changes: 15 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@
# endif
#endif


#if !defined(FMT_WINDOWS_NO_WCHAR) && defined(_WIN32)
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message "WINVER: " XSTR(WINVER)
#pragma message "_WIN32_WINNT: " XSTR(WINVER)
# if ((defined(__has_include) && !FMT_HAS_INCLUDE(<io.h>)) || \
(defined(WINVER) && WINVER <= 0x0500 &&
!(defined(_WIN32_WINNT) && _WIN32_WINNT > 0)))
// A legacy Windows platform without wide char APIs, e.g. Windows 98, original Xbox.
# define FMT_WINDOWS_NO_WCHAR
# endif
#endif

// Enable minimal optimizations for more compact code in debug mode.
FMT_GCC_PRAGMA("GCC push_options")
#if !defined(__OPTIMIZE__) && !defined(__NVCOMPILER) && !defined(__LCC__) && \
Expand Down Expand Up @@ -2675,7 +2689,7 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
typename vformat_args<Char>::type args, locale_ref loc = {});

FMT_API void vprint_mojibake(std::FILE*, string_view, format_args);
#ifndef _WIN32
#if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
inline void vprint_mojibake(std::FILE*, string_view, format_args) {}
#endif
} // namespace detail
Expand Down
8 changes: 4 additions & 4 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
# include <locale>
#endif

#ifdef _WIN32
#include "format.h"

#if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
# include <io.h> // _isatty
#endif

#include "format.h"

FMT_BEGIN_NAMESPACE
namespace detail {

Expand Down Expand Up @@ -1426,7 +1426,7 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) {
}

namespace detail {
#ifndef _WIN32
#if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC bool write_console(std::FILE*, string_view) { return false; }
#else
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
Expand Down
2 changes: 1 addition & 1 deletion test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ TEST(format_impl_test, write_dragon_even) {
if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
}

#ifdef _WIN32
#if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
# include <windows.h>

TEST(format_impl_test, write_console_signature) {
Expand Down

0 comments on commit 3045f5f

Please sign in to comment.