Source code for temci.build.linker

"""
Enables the randomization of the link order during the building of programs.
It's used to create a wrapper for `ld` (@see ../scripts/ld).

An implementation of this wrapper in C++ is given in the ../scripts/linker directory.
This python implementation is only the fall back solution if the C++ version isn't available.

The link order randomization only works for compilers that use the `ld` tool.
"""

import random
import typing as t
import os, json, subprocess




[docs]def process_linker(call: t.List[str]): """ Uses the passed `ld` arguments to randomize the link order during linking. It's configured by environment variables. :param call: arguments for `ld` """ config = json.loads(os.environ["RANDOMIZATION"]) if "RANDOMIZATION" in os.environ else {} randomize = "linker" in config and config["linker"] ld_tool = config["used_ld"] if "used_ld" in config else "/usr/bin/ld" if randomize: for i in range(0, 6): try: link(call, randomize=randomize, ld_tool=ld_tool) except OSError: continue return os.system("{} {}".format(ld_tool, " ".join(call[1:])))