build.py 6.29 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1 2
#!/usr/bin/env python3

3
import os, os.path
4
import sys, subprocess
Max Kellermann's avatar
Max Kellermann committed
5

6 7
if len(sys.argv) < 4:
    print("Usage: build.py SDK_PATH NDK_PATH ABI [configure_args...]", file=sys.stderr)
Max Kellermann's avatar
Max Kellermann committed
8 9 10 11
    sys.exit(1)

sdk_path = sys.argv[1]
ndk_path = sys.argv[2]
12 13
android_abi = sys.argv[3]
configure_args = sys.argv[4:]
Max Kellermann's avatar
Max Kellermann committed
14 15 16 17 18 19 20 21 22

if not os.path.isfile(os.path.join(sdk_path, 'tools', 'android')):
    print("SDK not found in", ndk_path, file=sys.stderr)
    sys.exit(1)

if not os.path.isdir(ndk_path):
    print("NDK not found in", ndk_path, file=sys.stderr)
    sys.exit(1)

23 24 25 26
android_abis = {
    'armeabi-v7a': {
        'arch': 'arm-linux-androideabi',
        'ndk_arch': 'arm',
27
        'toolchain_arch': 'arm-linux-androideabi',
28 29 30
        'llvm_triple': 'armv7-none-linux-androideabi',
        'cflags': '-march=armv7-a -mfpu=vfp -mfloat-abi=softfp',
    },
31 32 33 34 35 36 37 38

    'x86': {
        'arch': 'i686-linux-android',
        'ndk_arch': 'x86',
        'toolchain_arch': 'x86',
        'llvm_triple': 'i686-none-linux-android',
        'cflags': '-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32',
    },
39 40
}

41
# select the NDK target
42 43
abi_info = android_abis[android_abi]
arch = abi_info['arch']
44

Max Kellermann's avatar
Max Kellermann committed
45
# the path to the MPD sources
46
mpd_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]) or '.', '..'))
47
sys.path[0] = os.path.join(mpd_path, 'python')
Max Kellermann's avatar
Max Kellermann committed
48 49

# output directories
50
from build.dirs import lib_path, tarball_path, src_path
51

52
arch_path = os.path.join(lib_path, arch)
53
build_path = os.path.join(arch_path, 'build')
Max Kellermann's avatar
Max Kellermann committed
54 55 56 57 58 59

# build host configuration
build_arch = 'linux-x86_64'

# set up the NDK toolchain

60
class AndroidNdkToolchain:
61
    def __init__(self, tarball_path, src_path, build_path,
62
                 use_cxx):
63 64 65 66
        self.tarball_path = tarball_path
        self.src_path = src_path
        self.build_path = build_path

67
        ndk_arch = abi_info['ndk_arch']
68
        ndk_platform = 'android-14'
69 70 71 72 73

        # select the NDK compiler
        gcc_version = '4.9'

        ndk_platform_path = os.path.join(ndk_path, 'platforms', ndk_platform)
74
        sysroot = os.path.join(ndk_path, 'sysroot')
75
        target_root = os.path.join(ndk_platform_path, 'arch-' + ndk_arch)
76 77 78 79 80 81 82

        install_prefix = os.path.join(arch_path, 'root')

        self.arch = arch
        self.install_prefix = install_prefix
        self.sysroot = sysroot

83
        toolchain_path = os.path.join(ndk_path, 'toolchains', abi_info['toolchain_arch'] + '-' + gcc_version, 'prebuilt', build_arch)
84
        llvm_path = os.path.join(ndk_path, 'toolchains', 'llvm', 'prebuilt', build_arch)
85
        llvm_triple = abi_info['llvm_triple']
86

87
        common_flags = '-Os -g'
88
        common_flags += ' -fPIC'
89
        common_flags += ' ' + abi_info['cflags']
90 91

        toolchain_bin = os.path.join(toolchain_path, 'bin')
92 93 94 95
        llvm_bin = os.path.join(llvm_path, 'bin')
        self.cc = os.path.join(llvm_bin, 'clang')
        self.cxx = os.path.join(llvm_bin, 'clang++')
        common_flags += ' -target ' + llvm_triple + ' -integrated-as -gcc-toolchain ' + toolchain_path
96

97 98
        common_flags += ' -fvisibility=hidden -fdata-sections -ffunction-sections'

99
        self.ar = os.path.join(toolchain_bin, arch + '-ar')
100
        self.ranlib = os.path.join(toolchain_bin, arch + '-ranlib')
101 102 103
        self.nm = os.path.join(toolchain_bin, arch + '-nm')
        self.strip = os.path.join(toolchain_bin, arch + '-strip')

104 105
        self.cflags = common_flags
        self.cxxflags = common_flags
106 107 108
        self.cppflags = '--sysroot=' + sysroot + \
            ' -isystem ' + os.path.join(install_prefix, 'include') + \
            ' -isystem ' + os.path.join(sysroot, 'usr', 'include', arch) + \
109
            ' -D__ANDROID_API__=14'
110 111 112 113 114
        self.ldflags = '--sysroot=' + sysroot + \
            ' -L' + os.path.join(install_prefix, 'lib') + \
            ' -L' + os.path.join(target_root, 'usr', 'lib') + \
            ' -B' + os.path.join(target_root, 'usr', 'lib') + \
            ' ' + common_flags
115 116
        self.libs = ''

117
        self.is_arm = ndk_arch == 'arm'
118 119 120
        self.is_armv7 = self.is_arm and 'armv7' in self.cflags
        self.is_windows = False

121 122 123
        libcxx_path = os.path.join(ndk_path, 'sources/cxx-stl/llvm-libc++')
        libcxx_libs_path = os.path.join(libcxx_path, 'libs', android_abi)

124 125 126
        libstdcxx_flags = '-stdlib=libc++'
        libstdcxx_cxxflags = libstdcxx_flags + ' -isystem ' + os.path.join(libcxx_path, 'include') + ' -isystem ' + os.path.join(ndk_path, 'sources/android/support/include')
        libstdcxx_ldflags = libstdcxx_flags + ' -static-libstdc++ -L' + libcxx_libs_path
127 128

        if use_cxx:
129 130
            self.cxxflags += ' ' + libstdcxx_cxxflags
            self.ldflags += ' ' + libstdcxx_ldflags
131 132 133 134 135 136

        self.env = dict(os.environ)

        # redirect pkg-config to use our root directory instead of the
        # default one on the build host
        self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig')
Max Kellermann's avatar
Max Kellermann committed
137 138

# a list of third-party libraries to be used by MPD on Android
139
from build.libs import *
Max Kellermann's avatar
Max Kellermann committed
140
thirdparty_libs = [
141
    libmpdclient,
142 143 144 145 146 147 148
    libogg,
    libvorbis,
    opus,
    flac,
    libid3tag,
    ffmpeg,
    curl,
149
    libnfs,
150
    boost,
Max Kellermann's avatar
Max Kellermann committed
151 152 153 154
]

# build the third-party libraries
for x in thirdparty_libs:
155
    toolchain = AndroidNdkToolchain(tarball_path, src_path, build_path,
156
                                    use_cxx=x.use_cxx)
157 158
    if not x.is_installed(toolchain):
        x.build(toolchain)
Max Kellermann's avatar
Max Kellermann committed
159 160

# configure and build MPD
161
toolchain = AndroidNdkToolchain(tarball_path, src_path, build_path,
162
                                use_cxx=True)
Max Kellermann's avatar
Max Kellermann committed
163 164 165

configure = [
    os.path.join(mpd_path, 'configure'),
166 167 168 169 170 171 172 173
    'CC=' + toolchain.cc,
    'CXX=' + toolchain.cxx,
    'CFLAGS=' + toolchain.cflags,
    'CXXFLAGS=' + toolchain.cxxflags,
    'CPPFLAGS=' + toolchain.cppflags,
    'LDFLAGS=' + toolchain.ldflags,
    'LIBS=' + toolchain.libs,
    'AR=' + toolchain.ar,
174
    'RANLIB=' + toolchain.ranlib,
175 176 177 178
    'STRIP=' + toolchain.strip,
    '--host=' + toolchain.arch,
    '--prefix=' + toolchain.install_prefix,
    '--with-sysroot=' + toolchain.sysroot,
Max Kellermann's avatar
Max Kellermann committed
179 180 181 182
    '--with-android-sdk=' + sdk_path,

    '--enable-silent-rules',

183
    '--disable-icu',
Max Kellermann's avatar
Max Kellermann committed
184 185 186

] + configure_args

187 188 189 190
from build.cmdline import concatenate_cmdline_variables
configure = concatenate_cmdline_variables(configure,
    set(('CFLAGS', 'CXXFLAGS', 'CPPFLAGS', 'LDFLAGS', 'LIBS')))

191 192
subprocess.check_call(configure, env=toolchain.env)
subprocess.check_call(['/usr/bin/make', '--quiet', '-j12'], env=toolchain.env)