Search code examples
c++unix-ar

ar: not keeping file modification times


I have a docker instance running Ubuntu 16.4 LTS. I'm doing C++ builds and using ar/ranlib to produce my .a files. This is losing file modification times.

This is my Makefile rule to manage the archive file:

${LIB}: ${LIB_OBJ}
    ar -cvr ${LIB} ${LIB_OBJ}
    ranlib ${LIB}

And a little output afterwards:

^ ar tv libmylib.a
rw-r--r-- 0/0 129000 Jan  1 00:00 1970 XMLStringWriter.o
rw-r--r-- 0/0  99664 Jan  1 00:00 1970 FileUtilities.o
...

^ ar --version
GNU ar (GNU Binutils for Ubuntu) 2.26.1
^ ranlib --version
GNU ranlib (GNU Binutils for Ubuntu) 2.26.1

The system time is correct, and the .o files have proper update times. Furthermore, I do the same build for running on Mac, and the resulting libmylib-mac.a has proper update times with the exact same Makefile, just a different LIB target.

This isn't causing any real problems, but it's annoying. I don't see options to use to the ar command, and a web search didn't find an obvious discussion.

Solutions?

Also, should I eliminate the ranlib command and add the -s (lower case s) option to force a symbol table build?


Solution

  • Ubuntu has this fantastic idea that it is a good idea to make the -D option the default for ar. AFAICS, it is a complete nuisance, breaking 40+ years of Unix history for something that brings no benefit to me — it is very annoying.

    The -D option is for 'deterministic' mode and makes the UID, GID and modification time of the files in the archive all 0 (and it uses consistent file modes on all files). The alternative is -U for 'do not operate in deterministic mode'. There doesn't seem to be an environment variable or similar that can be set to choose between the modes.

    Actually, it probably isn't just Ubuntu — see the Linux manual for ar(1) for a description. However, the Linux manual mentions:

    If binutils was configured with --enable-deterministic-archives, then this mode is on by default. It can be disabled with the U modifier, below.

    In fact, the Ubuntu manual has the same words, but Ubuntu has chosen to make it the default — a decision which I think is unhelpful and counter-productive.