2021-09-20 22:54:54 +02:00
|
|
|
import os
|
2022-08-08 10:51:23 +02:00
|
|
|
import re
|
2020-05-26 21:08:29 +02:00
|
|
|
import subprocess
|
2023-09-16 23:23:18 +02:00
|
|
|
from typing import Optional, TextIO
|
|
|
|
from collections.abc import Mapping
|
2020-05-26 21:08:29 +02:00
|
|
|
|
|
|
|
from build.project import Project
|
2023-09-26 12:47:44 +02:00
|
|
|
from .toolchain import AnyToolchain
|
2020-05-26 21:08:29 +02:00
|
|
|
|
2023-09-16 23:23:18 +02:00
|
|
|
def __write_cmake_compiler(f: TextIO, language: str, compiler: str) -> None:
|
2021-07-29 09:32:21 +02:00
|
|
|
s = compiler.split(' ', 1)
|
|
|
|
if len(s) == 2:
|
2021-09-20 22:54:54 +02:00
|
|
|
print(f'set(CMAKE_{language}_COMPILER_LAUNCHER {s[0]})', file=f)
|
2021-07-29 09:32:21 +02:00
|
|
|
compiler = s[1]
|
2021-09-20 22:54:54 +02:00
|
|
|
print(f'set(CMAKE_{language}_COMPILER {compiler})', file=f)
|
|
|
|
|
2023-09-26 12:47:44 +02:00
|
|
|
def __write_cmake_toolchain_file(f: TextIO, toolchain: AnyToolchain) -> None:
|
2021-09-20 22:54:54 +02:00
|
|
|
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})
|
|
|
|
|
2022-08-22 10:28:10 +02:00
|
|
|
set(CMAKE_C_FLAGS_INIT "{toolchain.cflags} {toolchain.cppflags}")
|
|
|
|
set(CMAKE_CXX_FLAGS_INIT "{toolchain.cxxflags} {toolchain.cppflags}")
|
2021-09-20 22:54:54 +02:00
|
|
|
""")
|
|
|
|
__write_cmake_compiler(f, 'C', toolchain.cc)
|
|
|
|
__write_cmake_compiler(f, 'CXX', toolchain.cxx)
|
2021-07-29 09:32:21 +02:00
|
|
|
|
2022-08-08 10:51:23 +02:00
|
|
|
if cmake_system_name == 'Darwin':
|
|
|
|
# On macOS, cmake forcibly adds an "-isysroot" flag even if
|
|
|
|
# one is already present in the flags variable; this breaks
|
|
|
|
# cross-compiling for iOS, and can be worked around by setting
|
|
|
|
# the CMAKE_OSX_SYSROOT variable
|
|
|
|
# (https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_SYSROOT.html).
|
|
|
|
m = re.search(r'-isysroot +(\S+)', toolchain.cflags)
|
|
|
|
if m:
|
|
|
|
sysroot = m.group(1)
|
|
|
|
|
|
|
|
print(f'set(CMAKE_OSX_SYSROOT {sysroot})', file=f)
|
|
|
|
|
|
|
|
# search libraries and headers only in the sysroot, not on
|
|
|
|
# the build host
|
|
|
|
f.write(f"""
|
|
|
|
set(CMAKE_FIND_ROOT_PATH "{toolchain.install_prefix};{sysroot}")
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
|
|
|
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
|
|
|
""")
|
|
|
|
|
2023-09-26 12:47:44 +02:00
|
|
|
def configure(toolchain: AnyToolchain, src: str, build: str, args: list[str]=[], env: Optional[Mapping[str, str]]=None) -> None:
|
2020-05-26 21:08:29 +02:00
|
|
|
cross_args = []
|
|
|
|
|
|
|
|
if toolchain.is_windows:
|
|
|
|
cross_args.append('-DCMAKE_RC_COMPILER=' + toolchain.windres)
|
|
|
|
|
2021-09-20 22:54:54 +02:00
|
|
|
# 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)
|
|
|
|
|
2020-05-26 21:08:29 +02:00
|
|
|
configure = [
|
|
|
|
'cmake',
|
|
|
|
src,
|
|
|
|
|
2021-09-20 22:54:54 +02:00
|
|
|
'-DCMAKE_TOOLCHAIN_FILE=' + cmake_toolchain_file,
|
|
|
|
|
2020-05-26 21:08:29 +02:00
|
|
|
'-DCMAKE_INSTALL_PREFIX=' + toolchain.install_prefix,
|
|
|
|
'-DCMAKE_BUILD_TYPE=release',
|
|
|
|
|
|
|
|
'-GNinja',
|
|
|
|
] + cross_args + args
|
|
|
|
|
2022-08-22 10:31:14 +02:00
|
|
|
if env is None:
|
|
|
|
env = toolchain.env
|
|
|
|
else:
|
|
|
|
env = {**toolchain.env, **env}
|
|
|
|
|
2022-08-08 10:05:30 +02:00
|
|
|
print(configure)
|
2022-08-22 10:31:14 +02:00
|
|
|
subprocess.check_call(configure, env=env, cwd=build)
|
2020-05-26 21:08:29 +02:00
|
|
|
|
|
|
|
class CmakeProject(Project):
|
2023-09-16 23:23:18 +02:00
|
|
|
def __init__(self, url: str, md5: str, installed: str,
|
|
|
|
configure_args: list[str]=[],
|
|
|
|
windows_configure_args: list[str]=[],
|
|
|
|
env: Optional[Mapping[str, str]]=None,
|
2020-05-26 21:08:29 +02:00
|
|
|
**kwargs):
|
|
|
|
Project.__init__(self, url, md5, installed, **kwargs)
|
|
|
|
self.configure_args = configure_args
|
2021-03-12 21:56:32 +01:00
|
|
|
self.windows_configure_args = windows_configure_args
|
2022-08-22 10:31:14 +02:00
|
|
|
self.env = env
|
2020-05-26 21:08:29 +02:00
|
|
|
|
2023-09-26 12:47:44 +02:00
|
|
|
def configure(self, toolchain: AnyToolchain) -> str:
|
2020-05-26 21:08:29 +02:00
|
|
|
src = self.unpack(toolchain)
|
|
|
|
build = self.make_build_path(toolchain)
|
2021-03-12 21:56:32 +01:00
|
|
|
configure_args = self.configure_args
|
|
|
|
if toolchain.is_windows:
|
|
|
|
configure_args = configure_args + self.windows_configure_args
|
2022-08-22 10:31:14 +02:00
|
|
|
configure(toolchain, src, build, configure_args, self.env)
|
2020-05-26 21:08:29 +02:00
|
|
|
return build
|
|
|
|
|
2023-09-26 12:47:44 +02:00
|
|
|
def _build(self, toolchain: AnyToolchain) -> None:
|
2020-05-26 21:08:29 +02:00
|
|
|
build = self.configure(toolchain)
|
2022-08-08 10:05:30 +02:00
|
|
|
subprocess.check_call(['ninja', '-v', 'install'],
|
2020-05-26 21:08:29 +02:00
|
|
|
cwd=build, env=toolchain.env)
|