From a79b788ba2f66f60d3acaa8cb381ddaa3ff44f7e Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 23 Aug 2023 03:52:53 +0800 Subject: [PATCH] [Build] tests: fix compilation on older macOS: add port_platform.h (to pull in __STDC_FORMAT_MACROS define) (#32159) Some versions of MacOS (as well as some `glibc`-based platforms) require `__STDC_FORMAT_MACROS` to be defined prior to including `` or ``, otherwise build fails with undeclared `PRIdMAX`, `PRIdPTR` etc. See note here: https://en.cppreference.com/w/cpp/types/integer label: (release notes: no) --- CMakeLists.txt | 18 ++++++++++++++++++ templates/CMakeLists.txt.template | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57c5765eacc..2ff9866d7d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,6 +304,24 @@ if(UNIX AND NOT HAIKU) endif() endif() +include(CheckCXXSourceCompiles) + +if(UNIX OR APPLE) + # Some systems require the __STDC_FORMAT_MACROS macro to be defined + # to get the fixed-width integer type formatter macros. + check_cxx_source_compiles("#include + #include + int main() + { + int64_t i64{}; + std::printf(\"%\" PRId64, i64); + }" + HAVE_STDC_FORMAT_MACROS) + if(NOT HAVE_STDC_FORMAT_MACROS) + add_definitions(-D__STDC_FORMAT_MACROS) + endif() +endif() + # configure ccache if requested include(cmake/ccache.cmake) diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 034497df7fb..f274da5442a 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -427,6 +427,24 @@ endif() endif() + include(CheckCXXSourceCompiles) + + if(UNIX OR APPLE) + # Some systems require the __STDC_FORMAT_MACROS macro to be defined + # to get the fixed-width integer type formatter macros. + check_cxx_source_compiles("#include + #include + int main() + { + int64_t i64{}; + std::printf(\"%\" PRId64, i64); + }" + HAVE_STDC_FORMAT_MACROS) + if(NOT HAVE_STDC_FORMAT_MACROS) + add_definitions(-D__STDC_FORMAT_MACROS) + endif() + endif() + # configure ccache if requested include(cmake/ccache.cmake)