python/build/cmake.py: use CMAKE_TOOLCHAIN_FILE

Let cmake know that we're cross-crompiling.
This commit is contained in:
Max Kellermann 2021-09-20 22:54:54 +02:00
parent e4f933361e
commit 2699889342
3 changed files with 41 additions and 12 deletions

View File

@ -83,6 +83,7 @@ class AndroidNdkToolchain:
install_prefix = os.path.join(arch_path, 'root')
self.arch = arch
self.actual_arch = arch
self.install_prefix = install_prefix
llvm_path = os.path.join(ndk_path, 'toolchains', 'llvm', 'prebuilt', build_arch)

View File

@ -1,35 +1,62 @@
import os
import subprocess
from build.project import Project
def __cmake_compiler_args(language, compiler):
def __write_cmake_compiler(f, language, compiler):
s = compiler.split(' ', 1)
result = []
if len(s) == 2:
result.append(f'-DCMAKE_{language}_COMPILER_LAUNCHER={s[0]}')
print(f'set(CMAKE_{language}_COMPILER_LAUNCHER {s[0]})', file=f)
compiler = s[1]
result.append(f'-DCMAKE_{language}_COMPILER={compiler}')
return result
print(f'set(CMAKE_{language}_COMPILER {compiler})', file=f)
def __write_cmake_toolchain_file(f, toolchain):
if '-darwin' in toolchain.actual_arch:
cmake_system_name = 'Darwin'
elif toolchain.is_windows:
cmake_system_name = 'Windows'
else:
cmake_system_name = 'Linux'
f.write(f"""
set(CMAKE_SYSTEM_NAME {cmake_system_name})
set(CMAKE_SYSTEM_PROCESSOR {toolchain.actual_arch.split('-', 1)[0]})
set(CMAKE_C_COMPILER_TARGET {toolchain.actual_arch})
set(CMAKE_CXX_COMPILER_TARGET {toolchain.actual_arch})
set(CMAKE_C_FLAGS "{toolchain.cflags} {toolchain.cppflags}")
set(CMAKE_CXX_FLAGS "{toolchain.cxxflags} {toolchain.cppflags}")
""")
__write_cmake_compiler(f, 'C', toolchain.cc)
__write_cmake_compiler(f, 'CXX', toolchain.cxx)
def configure(toolchain, src, build, args=()):
cross_args = []
if toolchain.is_windows:
cross_args.append('-DCMAKE_SYSTEM_NAME=Windows')
cross_args.append('-DCMAKE_RC_COMPILER=' + toolchain.windres)
# Several targets need a sysroot to prevent pkg-config from
# looking for libraries on the build host (TODO: fix this
# properly); but we must not do that on Android because the NDK
# has a sysroot already
if '-android' not in toolchain.actual_arch and '-darwin' not in toolchain.actual_arch:
cross_args.append('-DCMAKE_SYSROOT=' + toolchain.install_prefix)
os.makedirs(build, exist_ok=True)
cmake_toolchain_file = os.path.join(build, 'cmake_toolchain_file')
with open(cmake_toolchain_file, 'w') as f:
__write_cmake_toolchain_file(f, toolchain)
configure = [
'cmake',
src,
'-DCMAKE_TOOLCHAIN_FILE=' + cmake_toolchain_file,
'-DCMAKE_INSTALL_PREFIX=' + toolchain.install_prefix,
'-DCMAKE_BUILD_TYPE=release',
] + \
__cmake_compiler_args('C', toolchain.cc) + \
__cmake_compiler_args('CXX', toolchain.cxx) + \
[
'-DCMAKE_C_FLAGS=' + toolchain.cflags + ' ' + toolchain.cppflags,
'-DCMAKE_CXX_FLAGS=' + toolchain.cxxflags + ' ' + toolchain.cppflags,
'-GNinja',
] + cross_args + args

View File

@ -38,6 +38,7 @@ class CrossGccToolchain:
def __init__(self, toolchain_path, arch,
tarball_path, src_path, build_path, install_prefix):
self.arch = arch
self.actual_arch = arch
self.tarball_path = tarball_path
self.src_path = src_path
self.build_path = build_path