Skip to content

Commit 04163ac

Browse files
committed
compiler: add 'b_time64' base option
Introduce a new base option called `b_time64`. This is a boolean option, which defaults to `false` (in which case it has no effect). If set to true, this will set `_TIME_BITS=64` (or equivalent) for all compilations. This enables a workaround for the Y2038 problem. With `_TIME_BITS=64`, libc will switch `time_t` and other relevant time types to 64-bits, if not already 64-bit (i.e., this usually only affects 32-bit platforms). libc will also transparently switch to 64-bit syscalls. For more background information, there is an LWN article about it (from 10 years ago): https://lwn.net/Articles/664800/ Handling of this option is similar to _FILE_OFFSET_BITS=64, which meson sets by default. Unfortunately, `time_t` is much more often used in public APIs, and thus more likely to lead to ABI mismatches when mixing code compiled with and without the option.
1 parent 6174a49 commit 04163ac

File tree

5 files changed

+20
-1
lines changed

5 files changed

+20
-1
lines changed

docs/markdown/Builtin-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ available on all platforms or with all compilers:
232232
| b_pgo | off | off, generate, use | Use profile guided optimization |
233233
| b_sanitize | none | see below | Code sanitizer to use |
234234
| b_staticpic | true | true, false | Build static libraries as position independent |
235+
| b_time64 | false | true, false | Enable 64-bit time handling for 32-bit platforms (via `_TIME_BITS=64`) |
235236
| b_pie | false | true, false | Build position-independent executables (since 0.49.0) |
236237
| b_vscrt | from_buildtype | none, md, mdd, mt, mtd, from_buildtype, static_from_buildtype | VS runtime library to use (since 0.48.0) (static_from_buildtype since 0.56.0) |
237238

mesonbuild/compilers/compilers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ def get_base_compile_args(target: 'BuildTarget', compiler: 'Compiler', env: 'Env
330330
pass
331331
except KeyError:
332332
pass
333+
try:
334+
if env.coredata.get_option_for_target(target, 'b_time64'):
335+
args += compiler.get_time64_args()
336+
except (KeyError, AttributeError):
337+
pass
333338
return args
334339

335340
def get_base_link_args(target: 'BuildTarget',
@@ -1095,6 +1100,13 @@ def get_assert_args(self, disable: bool, env: 'Environment') -> T.List[str]:
10951100
"""
10961101
return []
10971102

1103+
def get_time64_args(self) -> T.List[str]:
1104+
"""Get arguments to enable 64-bit time handling for 32-bit platforms.
1105+
1106+
:return: A list of string arguments for this compiler
1107+
"""
1108+
return []
1109+
10981110
def get_crt_val(self, crt_val: str, buildtype: str) -> str:
10991111
if crt_val in options.MSCRT_VALS:
11001112
return crt_val

mesonbuild/compilers/mixins/clike.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,9 @@ def get_assert_args(self, disable: bool, env: 'Environment') -> T.List[str]:
13441344
return ['-DNDEBUG']
13451345
return []
13461346

1347+
def get_time64_args(self) -> T.List[str]:
1348+
return ['-D_TIME_BITS=64']
1349+
13471350
@functools.lru_cache(maxsize=None)
13481351
def can_compile(self, src: 'mesonlib.FileOrString') -> bool:
13491352
# Files we preprocess can be anything, e.g. .in

mesonbuild/modules/rust.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import mesonlib, mlog
1414
from ..build import (BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList,
1515
CustomTarget, InvalidArguments, Jar, StructuredSources, SharedLibrary, StaticLibrary)
16-
from ..compilers.compilers import are_asserts_disabled_for_subproject, lang_suffixes
16+
from ..compilers.compilers import are_asserts_disabled_for_subproject, get_option_value_for_target, lang_suffixes
1717
from ..interpreter.type_checking import (
1818
DEPENDENCIES_KW, LINK_WITH_KW, LINK_WHOLE_KW, SHARED_LIB_KWS, TEST_KWS, TEST_KWS_NO_ARGS,
1919
OUTPUT_KW, INCLUDE_DIRECTORIES, SOURCES_VARARGS, NoneType, in_set_validator
@@ -343,6 +343,8 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
343343
state.environment.get_source_dir(), state.environment.get_build_dir())])
344344
if are_asserts_disabled_for_subproject(state.subproject, state.environment):
345345
clang_args.append('-DNDEBUG')
346+
if get_option_value_for_target(state.environment, state.subproject, 'b_time64', False):
347+
clang_args.append('-D_TIME_BITS=64')
346348

347349
for de in kwargs['dependencies']:
348350
for i in de.get_include_dirs():

mesonbuild/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ def option_to_argparse(option: AnyOptionType, name: OptionKey, parser: argparse.
789789
UserComboOption(
790790
'b_ndebug', 'Disable asserts', 'false', choices=['true', 'false', 'if-release']),
791791
UserBooleanOption('b_staticpic', 'Build static libraries as position independent', True),
792+
UserBooleanOption('b_time64', 'Set _TIME_BITS=64', False),
792793
UserBooleanOption('b_pie', 'Build executables as position independent', False),
793794
UserBooleanOption('b_bitcode', 'Generate and embed bitcode (only macOS/iOS/tvOS)', False),
794795
UserComboOption(

0 commit comments

Comments
 (0)