egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
69 stars 17 forks source link

implicit declaration of function 'clock_gettime' #519

Closed ryandesign closed 1 year ago

ryandesign commented 1 year ago

stklos 1.70 and git master fail to build on older versions of macOS:

system.c:1485:3: warning: implicit declaration of function 'clock_gettime' is invalid in C99 [-Wimplicit-function-declaration]
  clock_gettime(CLOCK_REALTIME, &now);
  ^
system.c:1485:17: error: use of undeclared identifier 'CLOCK_REALTIME'
  clock_gettime(CLOCK_REALTIME, &now);
                ^
system.c:1498:17: error: use of undeclared identifier 'CLOCK_REALTIME'
  clock_gettime(CLOCK_REALTIME, &now);
                ^
1 warning and 2 errors generated.
make[2]: *** [system.o] Error 1

Here is a full build log from OS X 10.11: https://build.macports.org/builders/ports-10.11_x86_64-builder/builds/214665/steps/install-port/logs/stdio

Can system.c survive without clock_gettime? If so, you probably want a configure test to check if it exists before you use it in system.c and elsewhere.

jpellegrini commented 1 year ago

I wonder why it fails... glock_gettime is POSIX since 2001, and its declaration should be in time.h, which system.c includes. (?)

ryandesign commented 1 year ago

macOS 10.12 (2016) is the earliest version of macOS that includes clock_gettime.

lassik commented 1 year ago

@ryandesign Does pre-10.12 have some equivalent of CLOCK_MONOTONIC?

CLOCK_REALTIME is basically the layman's notion of "current time", right?

ryandesign commented 1 year ago

MacPorts has a "legacy support" library that backports functions available in newer operating systems like clock_gettime to older OS versions. I can include that library in the STklos ports to probably fix the problem, but that only helps users installing STklos via MacPorts, not those building from source or other methods.

You could include compatibility implementations of such functions in the STklos codebase and use them when the OS doesn't have them. MacPorts legacy support is open source but each function seems to have its own origin with its own license so if you wanted to copy it you'd have to evaluate whether its license is compatible. Here's the code for clock_gettime. See also a pull request for an improvement to it.

lassik commented 1 year ago

Looks like good code under te ISC license. Maybe it should be added to the SRFI 170 repo where others would find it as well.

egallesio commented 1 year ago

I have added a test in autoconf for clock_gettime, and simulated it for CLOCK_REALTIME only. This implies that the function monotonic-time will yield an error on such systems. I have not added the code given by @ryandesign, because it is too macOS specific, and the number of impacted persons should be low.

Thanks for signaling the problem.