Skip to content

CLOCK_UPTIME_RAW isn't always available on __APPLE__ #15957

Closed
@adamnovak

Description

Since 76d05d4 and #15554, Protobuf tries to always use CLOCK_UPTIME_RAW if compiled for an __APPLE__ system:

#if !defined(GOOGLE_PROTOBUF_NO_RDTSC)
#if defined(__APPLE__)
// Use a commpage-based fast time function on Apple environments (MacOS,
// iOS, tvOS, watchOS, etc).
s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#elif defined(__x86_64__) && defined(__GNUC__)

But that symbol isn't always defined. On my machine, the MacOSX 14.2 SDK time.h only defines it if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE) evaluates to true:

#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
_CLOCK_MONOTONIC_RAW __CLOCK_AVAILABILITY = 4,
#define CLOCK_MONOTONIC_RAW _CLOCK_MONOTONIC_RAW
_CLOCK_MONOTONIC_RAW_APPROX __CLOCK_AVAILABILITY = 5,
#define CLOCK_MONOTONIC_RAW_APPROX _CLOCK_MONOTONIC_RAW_APPROX
_CLOCK_UPTIME_RAW __CLOCK_AVAILABILITY = 8,
#define CLOCK_UPTIME_RAW _CLOCK_UPTIME_RAW
_CLOCK_UPTIME_RAW_APPROX __CLOCK_AVAILABILITY = 9,
#define CLOCK_UPTIME_RAW_APPROX _CLOCK_UPTIME_RAW_APPROX
#endif

And Protobuf doesn't seem to define the appropriate macros to enable non-POSIX Darwin features before it includes time.h:

#if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__)
#include <time.h>
#endif

This leads to a build failure in my project, which defines _POSIX_C_SOURCE to get Posix 2008 features, but does not define _DARWIN_C_SOURCE because it does not directly use Darwin extensions.

    /usr/local/Cellar/protobuf/25.3/include/google/protobuf/map.h:694:31: error: use of undeclared identifier 'CLOCK_UPTIME_RAW'
        s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
                                  ^

Protobuf should either set the right macros for the features it needs itself, or else document the macros that user code must (or must not) define to build against Protobuf. Or, Protobuf should check the defined macros and use only features that will be available.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    inactiveDenotes the issue/PR has not seen activity in the last 90 days.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions