Search code examples
gnu-maketargetstatic-order-fiasco

Randomize Make goals for a target


I have a C++ library and it has a few of C++ static objects. The library could suffer from C++ static initialization fiasco. I'm trying to vet unforeseen translation unit dependencies by randomizing the order of the *.o files during a build.

I visited 2.3 How make Processes a Makefile in the GNU manual and it tells me:

Goals are the targets that make strives ultimately to update. You can override this behavior using the command line (see Arguments to Specify the Goals) ...

I also followed to 9.2 Arguments to Specify the Goals, but a treatment was not provided. It did not surprise me.

Is it possible to have Make randomize its goals? If so, then how do I do it?

If not, are there any alternatives? This is in a test environment, so I have more tools available to me than just GNUmake.

Thanks in advance.


Solution

  • This is really implementation-defined, but GNU Make will process targets from left to right.

    Say you have an OBJS variable with the objects you want to randomize, you could write something like (using e.g. shuf):

    RAND_OBJS := $(shell shuf -e -- $(OBJS))
    random_build: $(RAND_OBJS)
    

    This holds as long as you're not using parallel make (-j option). If you are the order will still be randomized, but it will also depend on number of jobs, system load, current phase of the moon, etc.