diff --git a/setup.py b/setup.py index c3d0942b..98a1545f 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ +import subprocess import sys import os from distutils.core import setup, Extension @@ -25,10 +26,60 @@ extra_setup_args["zip_safe"] = False # check if LuaJIT is in a subdirectory and build statically against it +def cmd_output(command): + """ + Returns the exit code and output of the program, as a triplet of the form + (exit_code, stdout, stderr). + """ + proc = subprocess.Popen(command, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = proc.communicate() + exit_code = proc.wait() + if exit_code != 0: + raise RuntimeError(stderr) + return stdout + +def check_luajit2_installed(): + try: + cmd_output('pkg-config luajit --exists') + except RuntimeError, e: + # pkg-config gives no stdout when it is given --exists and it cannot + # find the package, so we'll give it some better output + if not e.args[0]: + raise RuntimeError("pkg-config cannot find an installed luajit") + raise + + version_out = cmd_output('pkg-config luajit --modversion') + if version_out[0] != '2': + raise RuntimeError("Expected version 2+ of luajit, but found %s" % + version_out) + +def lua_include(): + cflag_out = cmd_output('pkg-config luajit --cflags-only-I') + + def trim_i(s): + if s.startswith('-I'): + return s[2:] + return s + return map(trim_i, filter(None, cflag_out.split())) + +def lua_libs(): + libs_out = cmd_output('pkg-config luajit --libs') + return filter(None, libs_out.split()) basedir = os.path.abspath(os.path.dirname(__file__)) def find_luajit_build(): + try: + check_luajit2_installed() + return dict(extra_objects=lua_libs(), include_dirs=lua_include()) + except RuntimeError, e: + print("Error finding installed luajit-2:") + print(e.args[0]) + print("Proceding with detection of local luajit-2") + static_libs = [] include_dirs = []