Skip to content

Commit

Permalink
Set ZERO_AR_DATE=1 to eliminate timestamp from .a outputs
Browse files Browse the repository at this point in the history
Summary:
By default, the archive header produced by `ar q` embeds the current unix timestamp.

The GNU archiver provides `ar qD` to make a deterministic archive, which we already use in Buck if supported.

https://www.internalfb.com/code/fbsource/[b6ae1a41be80e4b62a2595c69f0502b7f71b3103]/fbcode/buck2/prelude/cxx/archive.bzl?lines=44-46

The darwin archiver does not support `qD`.

```lang=text
/bin/ar: illegal option -- D
usage:  ar -d [-TLsv] archive file ...
ar -m [-TLsv] archive file ...
ar -m [-abiTLsv] position archive file ...
ar -p [-TLsv] archive [file ...]
ar -q [-cTLsv] archive file ...
ar -r [-cuTLsv] archive file ...
ar -r [-abciuTLsv] position archive file ...
ar -t [-TLsv] archive [file ...]
ar -x [-ouTLsv] archive [file ...]
```

It instead supports the same behavior through an environment variable `ZERO_AR_DATE`.

Reviewed By: d16r

Differential Revision: D60680127

fbshipit-source-id: 628e12b5ea4b209ba92582722fe49bc8e24ad5c3
  • Loading branch information
David Tolnay authored and facebook-github-bot committed Aug 2, 2024
1 parent 272c8e2 commit b70b171
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cxx/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,24 @@ def _archive(ctx: AnalysisContext, name: str, args: cmd_args, thin: bool, prefer
else:
command.add(args)

# By default, the archive header produced by `ar q` embeds the current unix
# timestamp. With the GNU archiver we use `ar qD` (above in _archive_flags)
# to make it produce a deterministic archive by zeroing the timestamp, but
# other archivers do not support such a flag. Some implementations, notably
# Xcode's, instead support zeroing the timestamp by way of an environment
# variable.
env = {"ZERO_AR_DATE": "1"}

category = "archive"
if thin:
category = "archive_thin"
ctx.actions.run(command, category = category, identifier = name, prefer_local = prefer_local)
ctx.actions.run(
command,
category = category,
identifier = name,
env = env,
prefer_local = prefer_local,
)
return archive_output

def _archive_locally(ctx: AnalysisContext, linker_info: LinkerInfo) -> bool:
Expand Down

0 comments on commit b70b171

Please sign in to comment.