LinuxSystem.py 2.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
#
#   Pyrex - Linux system interface
#

verbose = 0
gcc_pendantic = True
gcc_warnings_are_errors = True
gcc_all_warnings = True

10
import os, sys
William Stein's avatar
William Stein committed
11 12
from Cython.Utils import replace_suffix
from Cython.Compiler.Errors import PyrexError
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

version = "%s.%s" % sys.version[:2]
py_include_dirs = [
    "%s/include/python%s" % (sys.prefix, version)
]

compilers = ["gcc", "g++"]
compiler_options = \
    "-g -c -fno-strict-aliasing -Wno-long-double -no-cpp-precomp " \
    "-mno-fused-madd -fno-common -dynamic " \
    .split()
if gcc_pendantic:
    compiler_options.extend(["-pedantic", "-Wno-long-long"])
if gcc_warnings_are_errors:
    compiler_options.append("-Werror")
if gcc_all_warnings:
    compiler_options.append("-Wall")
    compiler_options.append("-Wno-unused-function")

linkers = ["gcc", "g++"]
linker_options = \
    "-shared" \
    .split()

class CCompilerError(PyrexError):
    pass

def c_compile(c_file, verbose_flag = 0, cplus = 0, obj_suffix = ".o"):
    #  Compile the given C source file to produce
    #  an object file. Returns the pathname of the
    #  resulting file.
    c_file = os.path.join(os.getcwd(), c_file)
    o_file = replace_suffix(c_file, obj_suffix)
    include_options = []
    for dir in py_include_dirs:
        include_options.append("-I%s" % dir)
    compiler = compilers[bool(cplus)]
    args = [compiler] + compiler_options + include_options + [c_file, "-o", o_file]
    if verbose_flag or verbose:
Stefan Behnel's avatar
Stefan Behnel committed
52
        print(" ".join(args))
53 54
    #print compiler, args ###
    status = os.spawnvp(os.P_WAIT, compiler, args)
Stefan Behnel's avatar
Stefan Behnel committed
55
    if status != 0:
56 57 58 59 60 61 62 63 64 65 66 67 68 69
        raise CCompilerError("C compiler returned status %s" % status)
    return o_file

def c_link(obj_file, verbose_flag = 0, extra_objects = [], cplus = 0):
    return c_link_list([obj_file] + extra_objects, verbose_flag, cplus)

def c_link_list(obj_files, verbose_flag = 0, cplus = 0):
    #  Link the given object files into a dynamically
    #  loadable extension file. Returns the pathname
    #  of the resulting file.
    out_file = replace_suffix(obj_files[0], ".so")
    linker = linkers[bool(cplus)]
    args = [linker] + linker_options + obj_files + ["-o", out_file]
    if verbose_flag or verbose:
Stefan Behnel's avatar
Stefan Behnel committed
70
        print(" ".join(args))
71
    status = os.spawnvp(os.P_WAIT, linker, args)
Stefan Behnel's avatar
Stefan Behnel committed
72
    if status != 0:
73 74
        raise CCompilerError("Linker returned status %s" % status)
    return out_file