Subject: [PATCH] cobol: Eliminate unguarded clock_gettime dependencies.
[PR119975]
These changes are help make it possible to compile on MacOS. In
addition to guarding clock_settime() calls, it removes the use
of structures and constants needed for clock_settime().
---
libgcobol/intrinsic.cc | 12 ++++++------
libgcobol/libgcobol.cc | 44 ++++++++++++++++++++++++++++--------------
libgcobol/libgcobol.h | 2 +-
3 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/libgcobol/intrinsic.cc b/libgcobol/intrinsic.cc
index 2d8d79c1c7c..81ae638630f 100644
--- a/libgcobol/intrinsic.cc
+++ b/libgcobol/intrinsic.cc
@@ -1219,7 +1219,7 @@ __gg__current_date(cblc_field_t *dest)
{
// FUNCTION CURRENT-DATE
struct cbl_timespec tp = {};
- __gg__clock_gettime(CLOCK_REALTIME, &tp); // time_t tv_sec; long
tv_nsec
+ __gg__clock_gettime(&tp); // time_t tv_sec; long tv_nsec
char retval[DATE_STRING_BUFFER_SIZE];
timespec_to_string(retval, tp);
@@ -1236,7 +1236,7 @@ __gg__seconds_past_midnight(cblc_field_t *dest)
struct tm tm;
__int128 retval=0;
- __gg__clock_gettime(CLOCK_REALTIME, &tp); // time_t tv_sec; long
tv_nsec
+ __gg__clock_gettime(&tp); // time_t tv_sec; long tv_nsec
localtime_r(&tp.tv_sec, &tm);
retval += tm.tm_hour;
@@ -1460,7 +1460,7 @@ __gg__formatted_current_date( cblc_field_t *dest, //
Destination string
size_t input_offset,
size_t input_size)
{
- // FUNCTION CURRENT-DATE
+ // FUNCTION FORMATTED-CURRENT-DATE
// Establish the destination, and set it to spaces
char *d = PTRCAST(char, dest->data);
@@ -1485,7 +1485,7 @@ __gg__formatted_current_date( cblc_field_t *dest, //
Destination string
}
struct cbl_timespec ts = {};
- __gg__clock_gettime(CLOCK_REALTIME, &ts);
+ __gg__clock_gettime(&ts);
struct tm tm = {};
#ifdef HAVE_STRUCT_TM_TM_ZONE
@@ -3433,7 +3433,7 @@ __gg__random( cblc_field_t *dest,
state = (char *)malloc(state_len);
struct cbl_timespec ts;
- __gg__clock_gettime(CLOCK_REALTIME, &ts);
+ __gg__clock_gettime(&ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
int seed = (int)__gg__binary_value_from_qualified_field(&rdigits,
@@ -3473,7 +3473,7 @@ __gg__random_next(cblc_field_t *dest)
buf->state = NULL;
state = (char *)malloc(state_len);
struct cbl_timespec ts;
- __gg__clock_gettime(CLOCK_REALTIME, &ts);
+ __gg__clock_gettime(&ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
random_r(buf, &retval_31);
diff --git a/libgcobol/libgcobol.cc b/libgcobol/libgcobol.cc
index f8697afd59c..81b5b7af812 100644
--- a/libgcobol/libgcobol.cc
+++ b/libgcobol/libgcobol.cc
@@ -69,6 +69,7 @@
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <sys/time.h>
#include <execinfo.h>
#include "exceptl.h"
@@ -264,7 +265,7 @@ class ec_status_t {
, operation(file_op_none)
, mode(file_mode_none_e)
, user_status(nullptr)
- , filename(nullptr)
+ , filename(nullptr)
{}
explicit file_status_t( const cblc_file_t *file )
: ifile(file->symbol_table_index)
@@ -558,7 +559,7 @@ __gg__abort(const char *msg)
abort();
}
-void
+void
__gg__mabort()
{
__gg__abort("Memory allocation error\n");
@@ -2290,7 +2291,7 @@ static time_t
cobol_time()
{
struct cbl_timespec tp;
- __gg__clock_gettime(CLOCK_REALTIME, &tp);
+ __gg__clock_gettime(&tp);
return tp.tv_sec;
}
@@ -2402,12 +2403,28 @@ int_from_digits(const char * &p, int ndigits)
return retval;
}
-uint64_t
-get_time_nanoseconds()
+// For testing purposes, this undef causes the use of gettimeofday().
+// #undef HAVE_CLOCK_GETTIME
+
+static uint64_t
+get_time_nanoseconds_local()
{
// This code was unabashedly stolen from gcc/timevar.cc.
// It returns the Unix epoch with nine decimal places.
+ /* Note: I am perplexed. I have been examining the gcc Makefiles and
+ configure.ac files, and I am unable to locate where
HAVE_GETTIMEOFDAY
+ is established. There have been issues compiling on MacOS, where
+ apparently clock_gettime() is not available. But I don't see
exactly
+ how gettimeofday() gets used, instead. But without the ability to
+ compile on a MacOS system, I am fumbling along as best I can.
+
+ I decided to simply replace clock_gettime() with getttimeofday()
when
+ clock_gettime() isn't available, even though gcc/timevar.cc handles
+ the situation differently.
+
+ -- Bob Dubner, 2025-06-11*/
+
uint64_t retval = 0;
#ifdef HAVE_CLOCK_GETTIME
@@ -2415,8 +2432,9 @@ get_time_nanoseconds()
clock_gettime (CLOCK_REALTIME, &ts);
retval = ts.tv_sec * 1000000000 + ts.tv_nsec;
return retval;
-#endif
-#ifdef HAVE_GETTIMEOFDAY
+//#endif
+//#ifdef HAVE_GETTIMEOFDAY
+#else
struct timeval tv;
gettimeofday (&tv, NULL);
retval = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
@@ -2427,7 +2445,7 @@ get_time_nanoseconds()
extern "C"
void
-__gg__clock_gettime(clockid_t clk_id, struct cbl_timespec *tp)
+__gg__clock_gettime(struct cbl_timespec *tp)
{
const char *p = getenv("GCOBOL_CURRENT_DATE");
@@ -2457,9 +2475,7 @@ __gg__clock_gettime(clockid_t clk_id, struct
cbl_timespec *tp)
}
else
{
- timespec tm;
- clock_gettime(clk_id, &tm);
- uint64_t ns = get_time_nanoseconds();
+ uint64_t ns = get_time_nanoseconds_local();
tp->tv_sec = ns/1000000000;
tp->tv_nsec = ns%1000000000;
}
@@ -2472,7 +2488,7 @@ __gg__get_date_hhmmssff()
char ach[32];
struct cbl_timespec tv;
- __gg__clock_gettime(CLOCK_REALTIME, &tv);
+ __gg__clock_gettime(&tv);
struct tm tm;
localtime_r(&tv.tv_sec, &tm);
@@ -3691,7 +3707,7 @@ compare_88( const char *list,
}
else
{
- cmpval = cstrncmp (test,
+ cmpval = cstrncmp (test,
PTRCAST(char, conditional_location),
conditional_length);
if( cmpval == 0 && (int)strlen(test) != conditional_length )
@@ -4573,7 +4589,7 @@ __gg__compare_2(cblc_field_t *left_side,
}
static size_t right_string_size = MINIMUM_ALLOCATION_SIZE;
- static char *right_string
+ static char *right_string
= static_cast<char
*>(malloc(right_string_size));
right_string = format_for_display_internal(
diff --git a/libgcobol/libgcobol.h b/libgcobol/libgcobol.h
index 4aa2cffb803..2e338c2596a 100644
--- a/libgcobol/libgcobol.h
+++ b/libgcobol/libgcobol.h
@@ -112,7 +112,7 @@ struct cbl_timespec
long tv_nsec; // Nanoseconds.
} ;
-extern "C" void __gg__clock_gettime(clockid_t clk_id, struct cbl_timespec
*tp);
+extern "C" void __gg__clock_gettime(struct cbl_timespec *tp);
extern "C" GCOB_FP128 __gg__float128_from_location(
const cblc_field_t *var,
--
2.34.1