jww
jww

Reputation: 102205

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.

Upvotes: 0

Views: 328

Answers (2)

Sergei Trofimovich
Sergei Trofimovich

Reputation: 46

Next release of GNU make will have --shuffle mode. It will allow you to execute prerequisites in random order to shake out missing dependencies by running $ make --shuffle.

The feature was recently added in https://savannah.gnu.org/bugs/index.php?62100 and so far is available only in GNU make's git tree.

Upvotes: 1

Andrea Biondo
Andrea Biondo

Reputation: 1686

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.

Upvotes: 3

Related Questions