import os
import struct
import subprocess
from io import BytesIO
from mozpack.errors import errors
MACHO_SIGNATURES = [
0xFEEDFACE, 0xCEFAEDFE, 0xFEEDFACF, 0xCFFAEDFE, ]
FAT_SIGNATURE = 0xCAFEBABE
ELF_SIGNATURE = 0x7F454C46
UNKNOWN = 0
MACHO = 1
ELF = 2
def get_type(path_or_fileobj):
if hasattr(path_or_fileobj, "peek"):
f = BytesIO(path_or_fileobj.peek(8))
elif hasattr(path_or_fileobj, "read"):
f = path_or_fileobj
else:
f = open(path_or_fileobj, "rb")
signature = f.read(4)
if len(signature) < 4:
return UNKNOWN
signature = struct.unpack(">L", signature)[0]
if signature == ELF_SIGNATURE:
return ELF
if signature in MACHO_SIGNATURES:
return MACHO
if signature != FAT_SIGNATURE:
return UNKNOWN
num = f.read(4)
if len(num) < 4:
return UNKNOWN
num = struct.unpack(">L", num)[0]
if num < 20:
return MACHO
return UNKNOWN
def is_executable(path):
from buildconfig import substs
if not os.path.exists(path):
return False
if substs["OS_ARCH"] == "WINNT":
return path.lower().endswith((substs["DLL_SUFFIX"], substs["BIN_SUFFIX"]))
return get_type(path) != UNKNOWN
def may_strip(path):
from buildconfig import substs
return bool(substs.get("PKG_STRIP"))
def strip(path):
from buildconfig import substs
strip = substs["STRIP"]
flags = substs.get("STRIP_FLAGS", [])
cmd = [strip] + flags + [path]
if subprocess.call(cmd) != 0:
errors.fatal("Error executing " + " ".join(cmd))
def may_elfhack(path):
from buildconfig import substs
return (
"USE_ELF_HACK" in substs
and substs["USE_ELF_HACK"]
and path.endswith(substs["DLL_SUFFIX"])
and "COMPILE_ENVIRONMENT" in substs
and substs["COMPILE_ENVIRONMENT"]
)
def elfhack(path):
from buildconfig import topobjdir
cmd = [os.path.join(topobjdir, "build/unix/elfhack/elfhack"), path]
if subprocess.call(cmd) != 0:
errors.fatal("Error executing " + " ".join(cmd))