Skip to content

Commit 722c066

Browse files
authored
[libc] Make use of 32-bit time_t a config option (#102012)
The 32-bit Arm builds of libc define time_t to be `__INTPTR_TYPE__`, i.e. a 32-bit integer. This is commented in the commit introducing it (75398f2) as being for compatibility with glibc. But in the near future not even every AArch32 build of glibc will have a 32-bit time_t: Debian is planning that their next release (trixie) will have switched to 64-bit. And non-Linux builds of this libc (e.g. baremetal) have no reason to need glibc compatibility in the first place – and every reason _not_ to want to start using a 32-bit time_t in 2024 or later. So I've replaced the `#ifdef` in `llvm-libc-types/time_t.h` with two versions of the header file, chosen in `CMakeLists.txt` via a new configuration option. This involved adding an extra parameter to the cmake `add_header` function to specify different names for the header file in the source and destination directories.
1 parent da492d4 commit 722c066

File tree

9 files changed

+70
-8
lines changed

9 files changed

+70
-8
lines changed

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function(_get_compile_options_from_config output_var)
7171
list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
7272
endif()
7373

74+
if(LIBC_TYPES_TIME_T_IS_32_BIT AND LLVM_LIBC_FULL_BUILD)
75+
list(APPEND config_options "-DLIBC_TYPES_TIME_T_IS_32_BIT")
76+
endif()
77+
7478
set(${output_var} ${config_options} PARENT_SCOPE)
7579
endfunction(_get_compile_options_from_config)
7680

libc/cmake/modules/LLVMLibCFlagRules.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,16 @@ if(NOT((LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE4_2")
286286
LIBC_TARGET_ARCHITECTURE_IS_AARCH64 OR LIBC_TARGET_OS_IS_GPU))
287287
set(SKIP_FLAG_EXPANSION_ROUND_OPT TRUE)
288288
endif()
289+
290+
# Choose whether time_t is 32- or 64-bit, based on target architecture
291+
# and config options. This will be used to set a #define during the
292+
# library build, and also to select the right version of time_t.h for
293+
# the output headers.
294+
if(LIBC_TARGET_ARCHITECTURE_IS_ARM AND NOT (LIBC_CONF_TIME_64BIT))
295+
# Set time_t to 32 bit for compatibility with glibc, unless
296+
# configuration says otherwise
297+
set(LIBC_TYPES_TIME_T_IS_32_BIT TRUE)
298+
else()
299+
# Other platforms default to 64-bit time_t
300+
set(LIBC_TYPES_TIME_T_IS_32_BIT FALSE)
301+
endif()

libc/cmake/modules/LLVMLibCHeaderRules.cmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
function(add_header target_name)
1010
cmake_parse_arguments(
1111
"ADD_HEADER"
12-
"" # No optional arguments
13-
"HDR" # Single value arguments
12+
"" # No optional arguments
13+
"HDR;DEST_HDR" # Single value arguments
1414
"DEPENDS"
1515
${ARGN}
1616
)
1717
if(NOT ADD_HEADER_HDR)
1818
message(FATAL_ERROR "'add_header' rules requires the HDR argument specifying a headef file.")
1919
endif()
2020

21-
set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR})
21+
if(ADD_HEADER_DEST_HDR)
22+
set(dest_leaf_filename ${ADD_HEADER_DEST_HDR})
23+
else()
24+
set(dest_leaf_filename ${ADD_HEADER_HDR})
25+
endif()
26+
set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${dest_leaf_filename})
2227
file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
2328
set(dest_file ${LIBC_INCLUDE_DIR}/${relative_path})
2429
set(src_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR})

libc/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,11 @@
8888
"value": true,
8989
"doc": "Make setjmp save the value of x18, and longjmp restore it. The AArch64 ABI delegates this register to platform ABIs, which can choose whether to make it caller-saved."
9090
}
91+
},
92+
"time": {
93+
"LIBC_CONF_TIME_64BIT": {
94+
"value": false,
95+
"doc": "Force the size of time_t to 64 bits, even on platforms where compatibility considerations would otherwise make it 32-bit."
96+
}
9197
}
9298
}

libc/docs/configure.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ to learn about the defaults for your platform and target.
5252
* **"string" options**
5353
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
5454
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.
55+
* **"time" options**
56+
- ``LIBC_CONF_TIME_64BIT``: Force the size of time_t to 64 bits, even on platforms where compatibility considerations would otherwise make it 32-bit.

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ add_header(pthread_rwlockattr_t HDR pthread_rwlockattr_t.h)
5959
add_header(pthread_spinlock_t HDR pthread_spinlock_t.h DEPENDS .pid_t)
6060
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
6161
add_header(rlim_t HDR rlim_t.h)
62-
add_header(time_t HDR time_t.h)
62+
if(LIBC_TYPES_TIME_T_IS_32_BIT)
63+
add_header(time_t HDR time_t_32.h DEST_HDR time_t.h)
64+
else()
65+
add_header(time_t HDR time_t_64.h DEST_HDR time_t.h)
66+
endif()
6367
add_header(stack_t HDR stack_t.h DEPENDS .size_t)
6468
add_header(suseconds_t HDR suseconds_t.h)
6569
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)

libc/include/llvm-libc-types/time_t.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Definition of the type time_t -------------------------------------===//
1+
//===-- Definition of the type time_t, for use during the libc build ------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -9,10 +9,10 @@
99
#ifndef LLVM_LIBC_TYPES_TIME_T_H
1010
#define LLVM_LIBC_TYPES_TIME_T_H
1111

12-
#if (defined(__arm__) || defined(_M_ARM))
13-
typedef __INTPTR_TYPE__ time_t;
12+
#ifdef LIBC_TYPES_TIME_T_IS_32_BIT
13+
#include "time_t_32.h"
1414
#else
15-
typedef __INT64_TYPE__ time_t;
15+
#include "time_t_64.h"
1616
#endif
1717

1818
#endif // LLVM_LIBC_TYPES_TIME_T_H
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of the type time_t -------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_TIME_T_32_H
10+
#define LLVM_LIBC_TYPES_TIME_T_32_H
11+
12+
typedef __INT32_TYPE__ time_t;
13+
14+
#endif // LLVM_LIBC_TYPES_TIME_T_32_H
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of the type time_t -------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_TIME_T_64_H
10+
#define LLVM_LIBC_TYPES_TIME_T_64_H
11+
12+
typedef __INT64_TYPE__ time_t;
13+
14+
#endif // LLVM_LIBC_TYPES_TIME_T_64_H

0 commit comments

Comments
 (0)