From patchwork Thu Sep 12 20:19:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1984853 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AFSjtQQk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X4TwG4tMRz1y1y for ; Fri, 13 Sep 2024 06:45:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C53B33857C4F for ; Thu, 12 Sep 2024 20:45:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 7CE873858D28 for ; Thu, 12 Sep 2024 20:44:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7CE873858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7CE873858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726173893; cv=none; b=TpE3/n4VlQjda2uO659AFMYk58Av+1trl5Qyz5/qjxb3M9ySZxvPh0ekNLiTKJSlZ8r0lOnVPdyHyLNImjO1ysXoQjaoTQzoz3yQOMRpNWUyECTvbH1EgnxQDLJE1XiyBiLFVOgjpebYsOXhuZ2MtAoL7LiOGS/zNZsNnetRX9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726173893; c=relaxed/simple; bh=OR/BG89IR4y7JuXjXIK2cItg0nmLjo5dbDpWNrXSZRQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xSprnLf1/xz2hjsH98vjopHWTD/pYQBDn67TqOBLjJ0zsSpoNIXMmqkQWoQdzPLV2GtY+WBHqWY+lSQpyo8iEYjGs/+LnnYPleZphd1xhx39EH1jIESo0lnavHTzkoh26Pgeax4UoYjxDt+lpzhVBmSoAW8VQRM2GkgzWK05pDY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726173889; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UeVzFyL1MLAnstuTT9XwPiyjuOuQWITt3mG3VOaNQEQ=; b=AFSjtQQk8brp5hC/mX+BUK2uE5D0rNLH/KDjGezOpNVwBPQPflm1YnH9cNAuDmMlFYamSJ YhUMBfsr6aqDlkI2vpInfg2igwn5QATdRTf6L6wAUP7Usj4aWvsC/tlB+Gl8ACG3MewJ+J a1BkgziEUfsiEtjyiGpUMr12e1jdpZQ= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-ijLogTirPe6zEgAcixm2-w-1; Thu, 12 Sep 2024 16:44:46 -0400 X-MC-Unique: ijLogTirPe6zEgAcixm2-w-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E9A1A19560B4; Thu, 12 Sep 2024 20:44:43 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 057C619560B7; Thu, 12 Sep 2024 20:44:37 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: =?utf-8?q?Arsen_Arsenovi=C4=87?= Subject: [PATCH] libstdc++: Enable most of for freestanding Date: Thu, 12 Sep 2024 21:19:07 +0100 Message-ID: <20240912204436.478587-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PP_MIME_FAKE_ASCII_TEXT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This restores support for most of with -ffreestanding. In case there are users who want a minimal freestanding implementation that only provides what the standard guarantees, there's a new macro that disables again. This can be used to write more portable freestanding code that doesn't rely on being usable. As we add other things to the freestanding subset (e.g. for PR 113398 and for PR 109814) we can add other _GLIBCXX_NO_FREESTANDING_XXX macros, and a _GLIBCXX_NO_FREESTANDING_EXTRAS to define all of them at once. I haven't done that in this patch, because there's on the CHRONO one for now. Tested x86_64-linux. -- >8 -- This makes durations, time points and calendrical types available for freestanding. The clocks and time zone utilities are disabled for freestanding, as they require functions in the hosted lib. Add support for a new macro _GLIBCXX_NO_FREESTANDING_CHRONO which can be used to explicitly disable for freestanding. libstdc++-v3/ChangeLog: * doc/xml/manual/using.xml (_GLIBCXX_NO_FREESTANDING_CHRONO): Document macro. * doc/html/*: Regenerate. * include/bits/chrono.h [_GLIBCXX_NO_FREESTANDING_CHRONO]: Only include when this macro is defined. [_GLIBCXX_HOSTED]: Only define clocks for hosted. * include/bits/version.def (chrono_udls): Remove hosted=yes. * include/bits/version.h: Regenerate. * include/std/chrono [_GLIBCXX_HOSTED]: Only define clocks and time zone utilities for hosted. * testsuite/std/time/freestanding.cc: New test. --- .../doc/html/manual/using_macros.html | 7 +++ libstdc++-v3/doc/xml/manual/using.xml | 12 +++++ libstdc++-v3/include/bits/chrono.h | 24 ++++++--- libstdc++-v3/include/bits/version.def | 1 - libstdc++-v3/include/bits/version.h | 2 +- libstdc++-v3/include/std/chrono | 24 +++++++-- .../testsuite/std/time/freestanding.cc | 52 +++++++++++++++++++ 7 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/time/freestanding.cc diff --git a/libstdc++-v3/doc/html/manual/using_macros.html b/libstdc++-v3/doc/html/manual/using_macros.html index ae564692630..67623b5e2af 100644 --- a/libstdc++-v3/doc/html/manual/using_macros.html +++ b/libstdc++-v3/doc/html/manual/using_macros.html @@ -124,4 +124,11 @@ must be present on all vector operations or none, so this macro must be defined to the same value for all translation units that create, destroy, or modify vectors. +

_GLIBCXX_NO_FREESTANDING_CHRONO

+ Undefined by default. When defined, the + <chrono> header cannot + be used with -ffreestanding. + When not defined, durations, time points, and calendar types are + available for freestanding, but the standard clocks and the time zone + database are not (because they require OS support).

\ No newline at end of file diff --git a/libstdc++-v3/doc/xml/manual/using.xml b/libstdc++-v3/doc/xml/manual/using.xml index 6675359f3b3..4e1c70040b5 100644 --- a/libstdc++-v3/doc/xml/manual/using.xml +++ b/libstdc++-v3/doc/xml/manual/using.xml @@ -1321,6 +1321,18 @@ g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe destroy, or modify vectors. + + _GLIBCXX_NO_FREESTANDING_CHRONO + + + Undefined by default. When defined, the + <chrono> header cannot + be used with . + When not defined, durations, time points, and calendar types are + available for freestanding, but the standard clocks and the time zone + database are not (because they require OS support). + + diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index 0773867da71..fd9c4642f4f 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -37,7 +37,9 @@ #include #include #include -#include +#if _GLIBCXX_HOSTED +# include +#endif #include // for literals support. #if __cplusplus >= 202002L # include @@ -50,7 +52,7 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION -#if __cplusplus >= 201703L +#if __cplusplus >= 201703L && _GLIBCXX_HOSTED namespace filesystem { struct __file_clock; }; #endif @@ -372,7 +374,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { }; #endif // C++20 -#ifdef __glibcxx_chrono // C++ >= 17 && HOSTED +#if __cplusplus >= 201703L // C++ >= 17 /** Convert a `duration` to type `ToDur` and round down. * * If the duration cannot be represented exactly in the result type, @@ -1196,6 +1198,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @} /// @} group chrono +#if _GLIBCXX_HOSTED // Clocks. // Why nanosecond resolution as the default? @@ -1310,9 +1313,18 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) template<> inline constexpr bool is_clock_v = true; /// @} #endif // C++20 +#elif __cplusplus >= 202002L + // Define a fake clock like chrono::local_t so that sys_time etc. + // can be used for freestanding. + struct __sys_t; + template + using sys_time = time_point<__sys_t, _Duration>; + using sys_seconds = sys_time; + using sys_days = sys_time; +#endif // _GLIBCXX_HOSTED } // namespace chrono -#ifdef __glibcxx_chrono_udls // C++ >= 14 && HOSTED +#ifdef __glibcxx_chrono_udls // C++ >= 14 inline namespace literals { /** ISO C++ 2014 namespace for suffixes for duration literals. @@ -1435,7 +1447,7 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) } // namespace chrono #endif // __glibcxx_chrono_udls -#if __cplusplus >= 201703L +#if __cplusplus >= 201703L && _GLIBCXX_HOSTED namespace filesystem { struct __file_clock @@ -1497,7 +1509,7 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) } }; } // namespace filesystem -#endif // C++17 +#endif // C++17 && HOSTED _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index bd3af9cba99..36f9ea429c4 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -275,7 +275,6 @@ ftms = { values = { v = 201304; cxxmin = 14; - hosted = yes; }; }; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 364e3a05f0e..fb97e67fb99 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -289,7 +289,7 @@ #undef __glibcxx_want_to_chars #if !defined(__cpp_lib_chrono_udls) -# if (__cplusplus >= 201402L) && _GLIBCXX_HOSTED +# if (__cplusplus >= 201402L) # define __glibcxx_chrono_udls 201304L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_chrono_udls) # define __cpp_lib_chrono_udls 201304L diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 7ffa5360728..aa78254dac9 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -32,7 +32,9 @@ #pragma GCC system_header -#include // for and clocks +#ifdef _GLIBCXX_NO_FREESTANDING_CHRONO +# include // for and clocks +#endif #if __cplusplus < 201103L # include @@ -41,7 +43,9 @@ #include #if __cplusplus >= 202002L -# include +# include // __countr_zero +#endif +#if __cplusplus >= 202002L && _GLIBCXX_HOSTED # include # include # include @@ -82,6 +86,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using local_seconds = local_time; using local_days = local_time; +#if _GLIBCXX_HOSTED class utc_clock; class tai_clock; class gps_clock; @@ -234,7 +239,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s; } }; - +#endif // _GLIBCXX_HOSTED template struct clock_time_conversion @@ -251,6 +256,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return __t; } }; +#if _GLIBCXX_HOSTED template<> struct clock_time_conversion { @@ -355,6 +361,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return _DestClock::from_utc(__t); } }; +#endif // _GLIBCXX_HOSTED /// @cond undocumented namespace __detail @@ -365,6 +372,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION clock_time_conversion<_DestClock, _SourceClock>{}(__t); }; +#if _GLIBCXX_HOSTED template concept __clock_convs_sys = requires (const time_point<_SourceClock, _Duration>& __t) { @@ -394,7 +402,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION clock_time_conversion{}( clock_time_conversion{}(__t))); }; - +#endif // _GLIBCXX_HOSTED } // namespace __detail /// @endcond @@ -404,10 +412,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline auto clock_cast(const time_point<_SourceClock, _Duration>& __t) requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration> +#if _GLIBCXX_HOSTED || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration> || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration> || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration> || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration> +#endif // _GLIBCXX_HOSTED { constexpr bool __direct = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>; @@ -415,6 +425,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return clock_time_conversion<_DestClock, _SourceClock>{}(__t); } +#if _GLIBCXX_HOSTED else { constexpr bool __convert_via_sys_clock @@ -465,6 +476,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } } +#endif // _GLIBCXX_HOSTED } // CALENDRICAL TYPES @@ -2530,6 +2542,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } +#if _GLIBCXX_HOSTED #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI // C++20 [time.zones] Time zones @@ -3324,6 +3337,7 @@ namespace __detail const auto __li = __detail::__get_leap_second_info(__s, false); return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed; } +#endif // _GLIBCXX_HOSTED /// @} group chrono #endif // C++20 @@ -3358,7 +3372,7 @@ namespace __detail _GLIBCXX_END_NAMESPACE_VERSION } // namespace std -#if __cplusplus >= 202002L +#if __cplusplus >= 202002L && _GLIBCXX_HOSTED # include #endif diff --git a/libstdc++-v3/testsuite/std/time/freestanding.cc b/libstdc++-v3/testsuite/std/time/freestanding.cc new file mode 100644 index 00000000000..afda0d5e561 --- /dev/null +++ b/libstdc++-v3/testsuite/std/time/freestanding.cc @@ -0,0 +1,52 @@ +// { dg-options "-ffreestanding" } +// { dg-do compile { target c++11 } } + +#include + +using namespace std::chrono; + +milliseconds +test_duration() +{ + seconds sec{1}; + sec = sec + -sec; + return duration_cast(sec + microseconds{100}); +} + +struct Clock +{ + using rep = long; + using period = std::ratio<1,10>; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + + static const bool is_steady = true; + + static time_point now() noexcept + { + static time_point tick{duration{0}}; + return tick + tick.time_since_epoch(); + } +}; + +Clock::time_point +test_time_point() +{ + auto t = Clock::now() + milliseconds{1}; + return time_point_cast(t); +} + +#if __cplusplus > 202002L +static_assert( is_clock_v ); + +bool +test_calendar() +{ + auto t = test_time_point(); + t = clock_cast(t); + local_days d{floor(t + 1h + 1min + 1s).time_since_epoch()}; + year_month_day ymd{d}; + weekday w{d}; + return w.ok(); +} +#endif