Skip to content

Commit 85f555d

Browse files
authored
[build] Fix builds where the C++ runtime library does not have std::put_time() (#2439).
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.
1 parent 64dedef commit 85f555d

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ CheckGCCAtomicIntrinsics()
495495
include(CheckCXXAtomic)
496496
CheckCXXAtomic()
497497

498+
# Check for std::put_time():
499+
# Sets:
500+
# HAVE_CXX_STD_PUT_TIME
501+
include(CheckCXXStdPutTime)
502+
CheckCXXStdPutTime()
503+
if (HAVE_CXX_STD_PUT_TIME)
504+
add_definitions(-DHAVE_CXX_STD_PUT_TIME=1)
505+
endif()
506+
498507
if (DISABLE_CXX11)
499508
set (ENABLE_CXX11 0)
500509
elseif( DEFINED ENABLE_CXX11 )
@@ -1082,6 +1091,11 @@ elseif (HAVE_LIBATOMIC AND HAVE_LIBATOMIC_COMPILES_STATIC)
10821091
if (srt_libspec_static)
10831092
target_link_libraries(${TARGET_srt}_static PUBLIC atomic)
10841093
endif()
1094+
elseif (LINUX AND HAVE_LIBATOMIC AND HAVE_LIBATOMIC_COMPILES)
1095+
# This is a workaround for some older Linux Toolchains.
1096+
if (srt_libspec_static)
1097+
target_link_libraries(${TARGET_srt}_static PUBLIC atomic)
1098+
endif()
10851099
endif()
10861100

10871101
# Cygwin installs the *.dll libraries in bin directory and uses PATH.

apps/statswriter.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
#include "netinet_any.h"
2121
#include "srt_compat.h"
2222

23-
// Note: std::put_time is supported only in GCC 5 and higher
24-
#if !defined(__GNUC__) || defined(__clang__) || (__GNUC__ >= 5)
25-
#define HAS_PUT_TIME
26-
#endif
2723

2824
using namespace std;
2925

@@ -100,7 +96,7 @@ string srt_json_cat_names [] = {
10096
"recv"
10197
};
10298

103-
#ifdef HAS_PUT_TIME
99+
#ifdef HAVE_CXX_STD_PUT_TIME
104100
// Follows ISO 8601
105101
std::string SrtStatsWriter::print_timestamp()
106102
{
@@ -125,10 +121,10 @@ std::string SrtStatsWriter::print_timestamp()
125121

126122
// This is a stub. The error when not defining it would be too
127123
// misleading, so this stub will work if someone mistakenly adds
128-
// the item to the output format without checking that HAS_PUT_TIME.
124+
// the item to the output format without checking that HAVE_CXX_STD_PUT_TIME
129125
string SrtStatsWriter::print_timestamp()
130126
{ return "<NOT IMPLEMENTED>"; }
131-
#endif // HAS_PUT_TIME
127+
#endif // HAVE_CXX_STD_PUT_TIME
132128

133129

134130
class SrtStatsJson : public SrtStatsWriter
@@ -170,7 +166,7 @@ class SrtStatsJson : public SrtStatsWriter
170166
output << pretty_tab << quotekey("sid") << sid;
171167

172168
// Extra Timepoint is also displayed manually
173-
#ifdef HAS_PUT_TIME
169+
#ifdef HAVE_CXX_STD_PUT_TIME
174170
// NOTE: still assumed SSC_GEN category
175171
output << "," << pretty_cr << pretty_tab
176172
<< quotekey("timepoint") << quote(print_timestamp());
@@ -246,7 +242,7 @@ class SrtStatsCsv : public SrtStatsWriter
246242
// Header
247243
if (!first_line_printed)
248244
{
249-
#ifdef HAS_PUT_TIME
245+
#ifdef HAVE_CXX_STD_PUT_TIME
250246
output << "Timepoint,";
251247
#endif
252248
output << "Time,SocketID";
@@ -260,10 +256,10 @@ class SrtStatsCsv : public SrtStatsWriter
260256
}
261257

262258
// Values
263-
#ifdef HAS_PUT_TIME
259+
#ifdef HAVE_CXX_STD_PUT_TIME
264260
// HDR: Timepoint
265261
output << print_timestamp() << ",";
266-
#endif // HAS_PUT_TIME
262+
#endif // HAVE_CXX_STD_PUT_TIME
267263

268264
// HDR: Time,SocketID
269265
output << mon.msTimeStamp << "," << sid;

scripts/CheckCXXStdPutTime.cmake

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# SRT - Secure, Reliable, Transport Copyright (c) 2022 Haivision Systems Inc.
3+
#
4+
# This Source Code Form is subject to the terms of the Mozilla Public License,
5+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
# obtain one at http://mozilla.org/MPL/2.0/.
7+
#
8+
9+
# Check for C++11 std::put_time().
10+
#
11+
# Sets:
12+
# HAVE_CXX_STD_PUT_TIME
13+
14+
include(CheckCSourceCompiles)
15+
16+
function(CheckCXXStdPutTime)
17+
18+
unset(HAVE_CXX_STD_PUT_TIME CACHE)
19+
20+
set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) # CMake 3.6
21+
22+
unset(CMAKE_REQUIRED_FLAGS)
23+
unset(CMAKE_REQUIRED_LIBRARIES)
24+
unset(CMAKE_REQUIRED_LINK_OPTIONS)
25+
26+
set(CheckCXXStdPutTime_CODE
27+
"
28+
#include <iostream>
29+
#include <iomanip>
30+
#include <ctime>
31+
int main(void)
32+
{
33+
const int result = 0;
34+
std::time_t t = std::time(nullptr);
35+
std::tm tm = *std::localtime(&t);
36+
std::cout
37+
<< std::put_time(&tm, \"%FT%T\")
38+
<< std::setfill('0')
39+
<< std::setw(6)
40+
<< std::endl;
41+
return result;
42+
}
43+
"
44+
)
45+
46+
# NOTE: Should we set -std or use the current compiler configuration.
47+
# It seems that the top level build does not track the compiler
48+
# in a consistent manner. So Maybe we need this?
49+
set(CMAKE_REQUIRED_FLAGS "-std=c++11")
50+
51+
# Check that the compiler can build the std::put_time() example:
52+
message(STATUS "Checking for C++ 'std::put_time()':")
53+
check_cxx_source_compiles(
54+
"${CheckCXXStdPutTime_CODE}"
55+
HAVE_CXX_STD_PUT_TIME)
56+
57+
endfunction(CheckCXXStdPutTime)

scripts/ShowProjectConfig.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ function(ShowProjectConfig)
141141
" HAVE_GCCATOMIC_INTRINSICS_REQUIRES_LIBATOMIC: ${HAVE_GCCATOMIC_INTRINSICS_REQUIRES_LIBATOMIC}\n"
142142
" HAVE_CXX_ATOMIC: ${HAVE_CXX_ATOMIC}\n"
143143
" HAVE_CXX_ATOMIC_STATIC: ${HAVE_CXX_ATOMIC_STATIC}\n"
144+
" HAVE_CXX_STD_PUT_TIME: ${HAVE_CXX_STD_PUT_TIME}\n"
144145
" Project Configuration:\n"
145146
" ENABLE_DEBUG: ${ENABLE_DEBUG}\n"
146147
" ENABLE_CXX11: ${ENABLE_CXX11}\n"

0 commit comments

Comments
 (0)