diff --git a/CMakeLists.txt b/CMakeLists.txt index e808fa80..90f48ffa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -338,6 +338,13 @@ target_include_directories(fmt-header-only ${FMT_SYSTEM_HEADERS_ATTRIBUTE} INTER $ $) +option(FMT_FILE_DESCRIPTORS "Disable this if your system does not provide numeric file descriptors" ON) +mark_as_advanced(FMT_FILE_DESCRIPTORS) +if (NOT FMT_FILE_DESCRIPTORS) + target_compile_definitions(fmt PUBLIC FMT_HAS_FILE_DESCRIPTORS=0) + target_compile_definitions(fmt-header-only INTERFACE FMT_HAS_FILE_DESCRIPTORS=0) +endif () + # Install targets. if (FMT_INSTALL) include(CMakePackageConfigHelpers) diff --git a/include/fmt/os.h b/include/fmt/os.h index 2126424d..676ab112 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -19,6 +19,17 @@ #include "format.h" +// Some operating systems do not implement numeric file descriptors. +// If set to 0, functions that rely on file descriptors will not be defined. +#ifndef FMT_HAS_FILE_DESCRIPTORS +# define FMT_HAS_FILE_DESCRIPTORS 1 +#endif + +// FCNTL requires file descriptors. +#if FMT_HAS_FILE_DESCRIPTORS == 0 +# define FMT_USE_FCNTL 0 +#endif + #ifndef FMT_USE_FCNTL // UWP doesn't provide _pipe. # if FMT_HAS_INCLUDE("winapifamily.h") @@ -222,7 +233,9 @@ class buffered_file { // Returns the pointer to a FILE object representing this file. FILE* get() const noexcept { return file_; } +#if FMT_HAS_FILE_DESCRIPTORS FMT_API int descriptor() const; +#endif void vprint(string_view format_str, format_args args) { fmt::vprint(file_, format_str, args); diff --git a/src/os.cc b/src/os.cc index bca410e9..33202f5b 100644 --- a/src/os.cc +++ b/src/os.cc @@ -181,6 +181,7 @@ void buffered_file::close() { FMT_THROW(system_error(errno, FMT_STRING("cannot close file"))); } +#if FMT_HAS_FILE_DESCRIPTORS int buffered_file::descriptor() const { #ifdef fileno // fileno is a macro on OpenBSD so we cannot use FMT_POSIX_CALL. int fd = fileno(file_); @@ -191,6 +192,7 @@ int buffered_file::descriptor() const { FMT_THROW(system_error(errno, FMT_STRING("cannot get file descriptor"))); return fd; } +#endif // FMT_HAS_FILE_DESCRIPTORS #if FMT_USE_FCNTL # ifdef _WIN32