meson.py 3.06 KB
Newer Older
1 2 3 4
import os.path, subprocess, sys

from build.project import Project

5 6 7
def make_cross_file(toolchain):
    if toolchain.is_windows:
        system = 'windows'
8
        windres = "windres = '%s'" % toolchain.windres
9 10
    else:
        system = 'linux'
11
        windres = ''
12 13 14 15 16

    if toolchain.is_arm:
        cpu_family = 'arm'
        if toolchain.is_armv7:
            cpu = 'armv7'
17
        else:
18 19 20 21 22 23 24 25
            cpu = 'armv6'
    elif toolchain.is_aarch64:
        cpu_family = 'aarch64'
        cpu = 'arm64-v8a'
    else:
        cpu_family = 'x86'
        if 'x86_64' in toolchain.arch:
            cpu = 'x86_64'
26
        else:
27
            cpu = 'i686'
28

29 30
    # TODO: support more CPUs
    endian = 'little'
31

32
    # TODO: write pkg-config wrapper
33

34 35 36 37
    path = os.path.join(toolchain.build_path, 'meson.cross')
    os.makedirs(toolchain.build_path, exist_ok=True)
    with open(path, 'w') as f:
        f.write("""
38 39 40 41 42
[binaries]
c = '%s'
cpp = '%s'
ar = '%s'
strip = '%s'
43
pkgconfig = '%s'
44
%s
45 46 47 48 49 50 51 52 53 54

[properties]
root = '%s'

c_args = %s
c_link_args = %s

cpp_args = %s
cpp_link_args = %s

55 56 57
# Keep Meson from executing Android-x86 test binariees
needs_exe_wrapper = true

58 59 60 61 62 63
[host_machine]
system = '%s'
cpu_family = '%s'
cpu = '%s'
endian = '%s'
            """ % (toolchain.cc, toolchain.cxx, toolchain.ar, toolchain.strip,
64
                   toolchain.pkg_config,
65
                   windres,
66 67
                   toolchain.install_prefix,
                   repr((toolchain.cppflags + ' ' + toolchain.cflags).split()),
68
                   repr(toolchain.ldflags.split() + toolchain.libs.split()),
69
                   repr((toolchain.cppflags + ' ' + toolchain.cxxflags).split()),
70
                   repr(toolchain.ldflags.split() + toolchain.libs.split()),
71
                   system, cpu_family, cpu, endian))
72
    return path
73

74 75 76 77 78 79 80
def configure(toolchain, src, build, args=()):
    cross_file = make_cross_file(toolchain)
    configure = [
        'meson',
        src, build,

        '--prefix', toolchain.install_prefix,
81

82 83 84 85
        # this is necessary because Meson uses Debian's build machine
        # MultiArch path (e.g. "lib/x86_64-linux-gnu") for cross
        # builds, which is obviously wrong
        '--libdir', 'lib',
86

87
        '--buildtype', 'plain',
88

89
        '--default-library=static',
90

91 92
        '--cross-file', cross_file,
    ] + args
93

94 95 96 97 98 99
    env = toolchain.env.copy()

    # Meson 0.54 requires the BOOST_ROOT environment variable
    env['BOOST_ROOT'] = toolchain.install_prefix

    subprocess.check_call(configure, env=env)
100

101 102 103 104 105 106 107 108 109 110
class MesonProject(Project):
    def __init__(self, url, md5, installed, configure_args=[],
                 **kwargs):
        Project.__init__(self, url, md5, installed, **kwargs)
        self.configure_args = configure_args

    def configure(self, toolchain):
        src = self.unpack(toolchain)
        build = self.make_build_path(toolchain)
        configure(toolchain, src, build, self.configure_args)
111 112 113 114 115 116
        return build

    def build(self, toolchain):
        build = self.configure(toolchain)
        subprocess.check_call(['ninja', 'install'],
                              cwd=build, env=toolchain.env)