From c13ae26235ec7e18e19a3461a07f95d45d7213de Mon Sep 17 00:00:00 2001 From: Jose Santiago Date: Mon, 22 Aug 2022 16:34:54 -0500 Subject: [PATCH 1/3] Fixes Builds where the CXX runtime library does not have sdt::put_time(). The original checks are not sufficient. For instance when building on Ubuntu14 with the Clang-3.4 compiler. This likely fixes other builds as well. --- CMakeLists.txt | 14 ++++++++ apps/statswriter.cpp | 18 ++++------ scripts/CheckCXXStdPutTime.cmake | 58 ++++++++++++++++++++++++++++++++ scripts/ShowProjectConfig.cmake | 1 + 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 scripts/CheckCXXStdPutTime.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e211f2e2..f0ed2df07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -484,6 +484,15 @@ CheckGCCAtomicIntrinsics() include(CheckCXXAtomic) CheckCXXAtomic() +# Check for sd::put_time(): +# Sets: +# HAVE_CXX_STD_PUT_TIME +include(CheckCXXStdPutTime) +CheckCXXStdPutTime() +if (HAVE_CXX_STD_PUT_TIME) + add_definitions(-DHAVE_CXX_STD_PUT_TIME=1) +endif() + if (DISABLE_CXX11) set (ENABLE_CXX11 0) elseif( DEFINED ENABLE_CXX11 ) @@ -1070,6 +1079,11 @@ elseif (HAVE_LIBATOMIC AND HAVE_LIBATOMIC_COMPILES_STATIC) if (srt_libspec_static) target_link_libraries(${TARGET_srt}_static PUBLIC atomic) endif() +elseif (LINUX AND HAVE_LIBATOMIC AND HAVE_LIBATOMIC_COMPILES) + # This is a workaround for some older Linux Toolchains. + if (srt_libspec_static) + target_link_libraries(${TARGET_srt}_static PUBLIC atomic) + endif() endif() # Cygwin installs the *.dll libraries in bin directory and uses PATH. diff --git a/apps/statswriter.cpp b/apps/statswriter.cpp index c176248ab..f1aa343b5 100644 --- a/apps/statswriter.cpp +++ b/apps/statswriter.cpp @@ -20,10 +20,6 @@ #include "netinet_any.h" #include "srt_compat.h" -// Note: std::put_time is supported only in GCC 5 and higher -#if !defined(__GNUC__) || defined(__clang__) || (__GNUC__ >= 5) -#define HAS_PUT_TIME -#endif using namespace std; @@ -100,7 +96,7 @@ string srt_json_cat_names [] = { "recv" }; -#ifdef HAS_PUT_TIME +#ifdef HAVE_CXX_STD_PUT_TIME // Follows ISO 8601 std::string SrtStatsWriter::print_timestamp() { @@ -125,10 +121,10 @@ std::string SrtStatsWriter::print_timestamp() // This is a stub. The error when not defining it would be too // misleading, so this stub will work if someone mistakenly adds -// the item to the output format without checking that HAS_PUT_TIME. +// the item to the output format without checking that HAVE_CXX_STD_PUT_TIME string SrtStatsWriter::print_timestamp() { return ""; } -#endif // HAS_PUT_TIME +#endif // HAVE_CXX_STD_PUT_TIME class SrtStatsJson : public SrtStatsWriter @@ -170,7 +166,7 @@ class SrtStatsJson : public SrtStatsWriter output << pretty_tab << quotekey("sid") << sid; // Extra Timepoint is also displayed manually -#ifdef HAS_PUT_TIME +#ifdef HAVE_CXX_STD_PUT_TIME // NOTE: still assumed SSC_GEN category output << "," << pretty_cr << pretty_tab << quotekey("timepoint") << quote(print_timestamp()); @@ -246,7 +242,7 @@ class SrtStatsCsv : public SrtStatsWriter // Header if (!first_line_printed) { -#ifdef HAS_PUT_TIME +#ifdef HAVE_CXX_STD_PUT_TIME output << "Timepoint,"; #endif output << "Time,SocketID"; @@ -260,10 +256,10 @@ class SrtStatsCsv : public SrtStatsWriter } // Values -#ifdef HAS_PUT_TIME +#ifdef HAVE_CXX_STD_PUT_TIME // HDR: Timepoint output << print_timestamp() << ","; -#endif // HAS_PUT_TIME +#endif // HAVE_CXX_STD_PUT_TIME // HDR: Time,SocketID output << mon.msTimeStamp << "," << sid; diff --git a/scripts/CheckCXXStdPutTime.cmake b/scripts/CheckCXXStdPutTime.cmake new file mode 100644 index 000000000..6a54ffadf --- /dev/null +++ b/scripts/CheckCXXStdPutTime.cmake @@ -0,0 +1,58 @@ +# +# SRT - Secure, Reliable, Transport Copyright (c) 2021 Haivision Systems Inc. +# +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at http://mozilla.org/MPL/2.0/. +# + +# Check for GCC Atomic Intrinsics and whether libatomic is required. +# +# Sets: +# HAVE_CXX_STD_PUT_TIME + +include(CheckCSourceCompiles) + +function(CheckCXXStdPutTime) + + unset(HAVE_CXX_STD_PUT_TIME CACHE) + + set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) # CMake 3.6 + + unset(CMAKE_REQUIRED_FLAGS) + unset(CMAKE_REQUIRED_LIBRARIES) + unset(CMAKE_REQUIRED_LINK_OPTIONS) + + # Example from: https://en.cppreference.com/w/cpp/io/manip/put_time + set(CheckCXXStdPutTime_CODE + " + #include + #include + #include + int main(void) + { + const int result = 0; + std::time_t t = std::time(nullptr); + std::tm tm = *std::localtime(&t); + std::cout + << std::put_time(&tm, \"%FT%T\") + << std::setfill('0') + << std::setw(6) + << std::endl; + return result; + } + " + ) + + # NOTE: Should we set -std or use the current compiler configuration. + # It seems that the top level build does not track the compiler + # in a consistent manner. So Maybe we need this? + set(CMAKE_REQUIRED_FLAGS "-std=c++11") + + # Check that the compiler can build the std::put_time() example: + message(STATUS "Checking for C++ 'std::put_time()':") + check_cxx_source_compiles( + "${CheckCXXStdPutTime_CODE}" + HAVE_CXX_STD_PUT_TIME) + +endfunction(CheckCXXStdPutTime) diff --git a/scripts/ShowProjectConfig.cmake b/scripts/ShowProjectConfig.cmake index 9cb959294..108dde5db 100644 --- a/scripts/ShowProjectConfig.cmake +++ b/scripts/ShowProjectConfig.cmake @@ -141,6 +141,7 @@ function(ShowProjectConfig) " HAVE_GCCATOMIC_INTRINSICS_REQUIRES_LIBATOMIC: ${HAVE_GCCATOMIC_INTRINSICS_REQUIRES_LIBATOMIC}\n" " HAVE_CXX_ATOMIC: ${HAVE_CXX_ATOMIC}\n" " HAVE_CXX_ATOMIC_STATIC: ${HAVE_CXX_ATOMIC_STATIC}\n" + " HAVE_CXX_STD_PUT_TIME: ${HAVE_CXX_STD_PUT_TIME}\n" " Project Configuration:\n" " ENABLE_DEBUG: ${ENABLE_DEBUG}\n" " ENABLE_CXX11: ${ENABLE_CXX11}\n" From 33c92e6bd3b7b28bbfcd6d9818143f744ba2c06b Mon Sep 17 00:00:00 2001 From: Jose Santiago Date: Mon, 22 Aug 2022 16:49:28 -0500 Subject: [PATCH 2/3] Fix opy paste mishap. --- scripts/CheckCXXStdPutTime.cmake | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/CheckCXXStdPutTime.cmake b/scripts/CheckCXXStdPutTime.cmake index 6a54ffadf..7f15ae09f 100644 --- a/scripts/CheckCXXStdPutTime.cmake +++ b/scripts/CheckCXXStdPutTime.cmake @@ -1,12 +1,12 @@ # -# SRT - Secure, Reliable, Transport Copyright (c) 2021 Haivision Systems Inc. +# SRT - Secure, Reliable, Transport Copyright (c) 2022 Haivision Systems Inc. # # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # -# Check for GCC Atomic Intrinsics and whether libatomic is required. +# Check for C++11 std::put_time(). # # Sets: # HAVE_CXX_STD_PUT_TIME @@ -23,7 +23,6 @@ function(CheckCXXStdPutTime) unset(CMAKE_REQUIRED_LIBRARIES) unset(CMAKE_REQUIRED_LINK_OPTIONS) - # Example from: https://en.cppreference.com/w/cpp/io/manip/put_time set(CheckCXXStdPutTime_CODE " #include From 8f1deba4410ca760dee47c84a86a86b18476ff7c Mon Sep 17 00:00:00 2001 From: Jose Santiago Date: Tue, 23 Aug 2022 09:17:12 -0500 Subject: [PATCH 3/3] Fix typo. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f0ed2df07..94d5b0e61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -484,7 +484,7 @@ CheckGCCAtomicIntrinsics() include(CheckCXXAtomic) CheckCXXAtomic() -# Check for sd::put_time(): +# Check for std::put_time(): # Sets: # HAVE_CXX_STD_PUT_TIME include(CheckCXXStdPutTime)