2021-01-14 08:34:20 +00:00
|
|
|
#
|
|
|
|
# Helper module to build macOS version of various source ports
|
2024-01-02 08:55:22 +00:00
|
|
|
# Copyright (C) 2020-2024 Alexey Lysiuk
|
2021-01-14 08:34:20 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import copy
|
|
|
|
import os
|
|
|
|
import re
|
2022-09-23 10:48:32 +00:00
|
|
|
import shlex
|
2021-01-14 08:34:20 +00:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import typing
|
2021-08-05 07:12:20 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from platform import machine
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
from ..state import BuildState
|
2023-01-01 08:47:03 +00:00
|
|
|
from ..utility import CommandLineOptions, symlink_directory
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Target:
|
|
|
|
DESTINATION_DEPS = 0
|
|
|
|
DESTINATION_OUTPUT = 1
|
|
|
|
|
|
|
|
def __init__(self, name=None):
|
|
|
|
self.name = name
|
|
|
|
self.destination = self.DESTINATION_DEPS
|
|
|
|
|
|
|
|
self.multi_platform = False
|
|
|
|
self.unsupported_architectures = ()
|
|
|
|
|
|
|
|
def prepare_source(self, state: BuildState):
|
2022-08-18 07:30:39 +00:00
|
|
|
""" Called when target is selected by name """
|
2021-01-14 08:34:20 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-18 07:28:49 +00:00
|
|
|
def initialize(self, state: BuildState):
|
2022-08-18 07:30:39 +00:00
|
|
|
""" Called on all targets except the selected one before prefix directory creation """
|
2022-08-18 07:28:49 +00:00
|
|
|
pass
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
def detect(self, state: BuildState) -> bool:
|
2022-08-18 07:30:39 +00:00
|
|
|
"""
|
|
|
|
Called when target is selected by source code directory
|
|
|
|
Called on all targets until match is found
|
|
|
|
"""
|
2021-01-14 08:34:20 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
2022-08-18 07:30:39 +00:00
|
|
|
""" Called before selected target is about to build """
|
2021-01-14 08:34:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
2022-08-18 07:30:39 +00:00
|
|
|
""" Does actual build """
|
2021-01-14 08:34:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def post_build(self, state: BuildState):
|
2022-08-18 07:30:39 +00:00
|
|
|
""" Called after selected target is built """
|
2021-01-14 08:34:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BuildTarget(Target):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
self.src_root = ''
|
|
|
|
self.multi_platform = True
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
os.makedirs(state.build_path, exist_ok=True)
|
|
|
|
|
2021-08-05 06:53:07 +00:00
|
|
|
env = state.environment
|
2021-07-22 06:51:13 +00:00
|
|
|
env['PATH'] = os.pathsep.join([
|
|
|
|
str(state.bin_path),
|
|
|
|
env['PATH'],
|
|
|
|
])
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-03-30 12:11:18 +00:00
|
|
|
if state.xcode:
|
|
|
|
return
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-09-01 08:41:08 +00:00
|
|
|
if c_compiler := state.c_compiler():
|
|
|
|
env['CC'] = str(c_compiler)
|
|
|
|
if cxx_compiler := state.cxx_compiler():
|
|
|
|
env['CXX'] = str(cxx_compiler)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-12-31 08:51:41 +00:00
|
|
|
for prefix in ('C', 'CPP', 'CXX', 'OBJC', 'OBJCXX'):
|
2023-11-03 11:10:02 +00:00
|
|
|
state.update_flags_environment_variable(f'{prefix}FLAGS', state.compiler_flags())
|
2021-03-30 12:11:18 +00:00
|
|
|
|
2023-11-03 11:10:02 +00:00
|
|
|
state.update_flags_environment_variable('LDFLAGS', state.linker_flags())
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-02-12 07:54:51 +00:00
|
|
|
# Avoid timestamp only differences in static libraries
|
|
|
|
env['ZERO_AR_DATE'] = '1'
|
|
|
|
|
2023-01-05 08:48:58 +00:00
|
|
|
def install(self, state: BuildState, options: typing.Optional[CommandLineOptions] = None, tool: str = 'gmake'):
|
2021-01-14 08:34:20 +00:00
|
|
|
if state.xcode:
|
|
|
|
return
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
if state.install_path.exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
shutil.rmtree(state.install_path)
|
|
|
|
|
2022-08-28 06:51:13 +00:00
|
|
|
args = [tool]
|
|
|
|
args += options and options.to_list() or ['install']
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
self.update_pc_files(state)
|
|
|
|
|
|
|
|
@staticmethod
|
2023-01-05 08:48:58 +00:00
|
|
|
def update_text_file(path: Path, processor: typing.Optional[typing.Callable] = None):
|
2021-01-14 08:34:20 +00:00
|
|
|
with open(path, 'r') as f:
|
|
|
|
content = f.readlines()
|
|
|
|
|
|
|
|
patched_content = []
|
|
|
|
|
|
|
|
for line in content:
|
|
|
|
patched_line = processor(line) if processor else line
|
|
|
|
|
|
|
|
if patched_line:
|
|
|
|
patched_content.append(patched_line)
|
|
|
|
|
2023-03-22 09:50:17 +00:00
|
|
|
if content == patched_content:
|
|
|
|
return
|
|
|
|
|
2023-03-22 09:54:15 +00:00
|
|
|
file_time = os.stat(path).st_mtime
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.writelines(patched_content)
|
|
|
|
|
2023-03-22 09:54:15 +00:00
|
|
|
os.utime(path, (file_time, file_time))
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
@staticmethod
|
2023-01-05 08:48:58 +00:00
|
|
|
def _update_variables_file(path: Path, prefix_value: str,
|
|
|
|
processor: typing.Optional[typing.Callable] = None, quotes: bool = True):
|
2021-01-14 08:34:20 +00:00
|
|
|
prefix = 'prefix='
|
2021-05-31 08:42:12 +00:00
|
|
|
exec_prefix = 'exec_prefix='
|
|
|
|
includedir = 'includedir='
|
|
|
|
libdir = 'libdir='
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-06-07 06:56:33 +00:00
|
|
|
def quote(value: str) -> str:
|
|
|
|
return f'"{value}"' if quotes else value
|
|
|
|
|
|
|
|
def patch_proc(line: str) -> str:
|
2021-01-14 08:34:20 +00:00
|
|
|
patched_line = line
|
|
|
|
|
|
|
|
if line.startswith(prefix):
|
2021-06-07 06:56:33 +00:00
|
|
|
patched_line = prefix + quote(prefix_value) + os.linesep
|
2021-05-31 08:42:12 +00:00
|
|
|
elif line.startswith(exec_prefix):
|
2021-06-07 06:56:33 +00:00
|
|
|
patched_line = exec_prefix + quote('${prefix}') + os.linesep
|
2021-05-31 08:42:12 +00:00
|
|
|
elif line.startswith(includedir):
|
2021-06-07 06:56:33 +00:00
|
|
|
patched_line = includedir + quote('${prefix}/include') + os.linesep
|
2021-05-31 08:42:12 +00:00
|
|
|
elif line.startswith(libdir):
|
2021-06-07 06:56:33 +00:00
|
|
|
patched_line = libdir + quote('${exec_prefix}/lib') + os.linesep
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if processor:
|
|
|
|
patched_line = processor(path, patched_line)
|
|
|
|
|
|
|
|
return patched_line
|
|
|
|
|
2021-06-07 06:56:33 +00:00
|
|
|
BuildTarget.update_text_file(path, patch_proc)
|
|
|
|
|
|
|
|
@staticmethod
|
2023-01-05 08:48:58 +00:00
|
|
|
def update_config_script(path: Path, processor: typing.Optional[typing.Callable] = None):
|
2021-06-07 06:56:33 +00:00
|
|
|
BuildTarget._update_variables_file(path, r'$(cd "${0%/*}/.."; pwd)', processor)
|
|
|
|
|
|
|
|
@staticmethod
|
2023-01-05 08:48:58 +00:00
|
|
|
def update_pc_file(path: Path, processor: typing.Optional[typing.Callable] = None):
|
2021-06-07 06:56:33 +00:00
|
|
|
BuildTarget._update_variables_file(path, '', processor, quotes=False)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def update_pc_files(self, state: BuildState):
|
|
|
|
for root, _, files in os.walk(state.install_path, followlinks=True):
|
|
|
|
for filename in files:
|
|
|
|
if filename.endswith('.pc'):
|
2021-08-01 13:14:43 +00:00
|
|
|
file_path = Path(root) / filename
|
2021-01-14 08:34:20 +00:00
|
|
|
BuildTarget.update_pc_file(file_path, self._process_pkg_config)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _process_pkg_config(pcfile: Path, line: str) -> str:
|
2021-01-14 08:34:20 +00:00
|
|
|
assert pcfile
|
|
|
|
return line
|
|
|
|
|
|
|
|
def write_pc_file(self, state: BuildState,
|
|
|
|
filename=None, name=None, description=None, version='',
|
|
|
|
requires='', requires_private='', libs='', libs_private='', cflags=''):
|
2021-07-22 06:51:13 +00:00
|
|
|
pkgconfig_path = state.install_path / 'lib/pkgconfig'
|
2021-01-14 08:34:20 +00:00
|
|
|
os.makedirs(pkgconfig_path, exist_ok=True)
|
|
|
|
|
|
|
|
if not filename:
|
|
|
|
filename = self.name + '.pc'
|
|
|
|
if not name:
|
|
|
|
name = self.name
|
|
|
|
if not description:
|
|
|
|
description = self.name
|
|
|
|
if not libs:
|
|
|
|
libs = '-l' + self.name
|
|
|
|
|
|
|
|
pc_content = f'''prefix=
|
|
|
|
exec_prefix=${{prefix}}
|
|
|
|
libdir=${{exec_prefix}}/lib
|
|
|
|
includedir=${{prefix}}/include
|
|
|
|
|
|
|
|
Name: {name}
|
|
|
|
Description: {description}
|
|
|
|
Version: {version}
|
|
|
|
Requires: {requires}
|
|
|
|
Requires.private: {requires_private}
|
|
|
|
Libs: -L${{libdir}} {libs}
|
|
|
|
Libs.private: {libs_private}
|
|
|
|
Cflags: -I${{includedir}} {cflags}
|
|
|
|
'''
|
2021-07-22 06:51:13 +00:00
|
|
|
with open(pkgconfig_path / filename, 'w') as f:
|
2021-01-14 08:34:20 +00:00
|
|
|
f.write(pc_content)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_platform_header(state: BuildState, header: str):
|
2021-07-22 06:51:13 +00:00
|
|
|
include_path = state.install_path / 'include'
|
2021-01-14 08:34:20 +00:00
|
|
|
header_parts = header.rsplit(os.sep, 1)
|
2021-02-11 13:51:34 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
if len(header_parts) == 1:
|
|
|
|
header_parts.insert(0, '')
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
common_header = include_path / header
|
|
|
|
platform_header = include_path / header_parts[0] / f'_aedi_{state.architecture()}_{header_parts[1]}'
|
2021-02-11 13:51:34 +00:00
|
|
|
shutil.move(common_header, platform_header)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-02-11 13:51:34 +00:00
|
|
|
with open(common_header, 'w') as f:
|
2021-01-14 08:34:20 +00:00
|
|
|
f.write(f'''
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#if defined(__x86_64__)
|
2021-02-11 13:51:34 +00:00
|
|
|
# include "_aedi_x86_64_{header_parts[1]}"
|
2021-01-14 08:34:20 +00:00
|
|
|
#elif defined(__aarch64__)
|
2021-02-11 13:51:34 +00:00
|
|
|
# include "_aedi_arm64_{header_parts[1]}"
|
2021-01-14 08:34:20 +00:00
|
|
|
#else
|
|
|
|
# error Unknown architecture
|
|
|
|
#endif
|
|
|
|
''')
|
|
|
|
|
2023-01-05 08:48:58 +00:00
|
|
|
def copy_to_bin(self, state: BuildState,
|
|
|
|
filename: typing.Optional[str] = None, new_filename: typing.Optional[str] = None):
|
2021-07-22 06:51:13 +00:00
|
|
|
bin_path = state.install_path / 'bin'
|
2021-01-14 08:34:20 +00:00
|
|
|
os.makedirs(bin_path, exist_ok=True)
|
|
|
|
|
|
|
|
if not filename:
|
|
|
|
filename = self.name
|
|
|
|
if not new_filename:
|
|
|
|
new_filename = filename
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
src_path = state.build_path / filename
|
|
|
|
dst_path = bin_path / new_filename
|
2021-01-14 08:34:20 +00:00
|
|
|
shutil.copy(src_path, dst_path)
|
|
|
|
|
|
|
|
|
|
|
|
class MakeTarget(BuildTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
2021-03-16 11:23:48 +00:00
|
|
|
self.tool = 'gmake'
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
super().configure(state)
|
|
|
|
|
2021-01-17 10:37:35 +00:00
|
|
|
symlink_directory(state.source, state.build_path)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
|
|
|
assert not state.xcode
|
|
|
|
|
|
|
|
args = [
|
|
|
|
self.tool,
|
|
|
|
'-j', state.jobs,
|
|
|
|
]
|
2021-09-01 08:41:08 +00:00
|
|
|
|
|
|
|
if c_compiler := state.c_compiler():
|
|
|
|
args.append(f'CC={c_compiler}')
|
|
|
|
if cxx_compiler := state.cxx_compiler():
|
|
|
|
args.append(f'CXX={cxx_compiler}')
|
|
|
|
|
2021-08-05 06:59:49 +00:00
|
|
|
args += state.options.to_list()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
work_path = state.build_path / self.src_root
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=work_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
|
2021-08-14 09:31:34 +00:00
|
|
|
class ConfigureMakeTarget(MakeTarget):
|
2021-01-14 08:34:20 +00:00
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
super().configure(state)
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
work_path = state.build_path / self.src_root
|
|
|
|
configure_path = work_path / 'configure'
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
common_args = [
|
|
|
|
configure_path,
|
2021-07-22 06:51:13 +00:00
|
|
|
f'--prefix={state.install_path}',
|
2021-01-14 08:34:20 +00:00
|
|
|
]
|
2021-08-05 06:59:49 +00:00
|
|
|
common_args += state.options.to_list()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
disable_dependency_tracking = '--disable-dependency-tracking'
|
|
|
|
host = '--host=' + state.host()
|
|
|
|
|
|
|
|
args = copy.copy(common_args)
|
|
|
|
args.append(host)
|
|
|
|
args.append(disable_dependency_tracking)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Try with host and disabled dependency tracking first
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=work_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# If it fails, try with disabled dependency tracking only
|
|
|
|
args = copy.copy(common_args)
|
|
|
|
args.append(disable_dependency_tracking)
|
|
|
|
|
|
|
|
try:
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=work_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# Use only common command line arguments
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(common_args, check=True, cwd=work_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
2021-08-14 09:31:34 +00:00
|
|
|
# Clear configure script options
|
|
|
|
state.options = CommandLineOptions()
|
|
|
|
|
|
|
|
super().build(state)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CMakeTarget(BuildTarget):
|
2021-08-12 10:00:08 +00:00
|
|
|
cached_project_name = None
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
def detect(self, state: BuildState) -> bool:
|
2021-08-12 10:00:08 +00:00
|
|
|
if CMakeTarget.cached_project_name:
|
|
|
|
project_name = CMakeTarget.cached_project_name
|
2021-01-14 08:34:20 +00:00
|
|
|
else:
|
2021-08-12 10:00:08 +00:00
|
|
|
cmakelists_path = state.source / self.src_root / 'CMakeLists.txt'
|
|
|
|
|
|
|
|
if not cmakelists_path.exists():
|
|
|
|
return False
|
|
|
|
|
|
|
|
for line in open(cmakelists_path).readlines():
|
|
|
|
project_name = CMakeTarget._extract_project_name(line)
|
|
|
|
if project_name:
|
|
|
|
project_name = project_name.lower()
|
|
|
|
project_name = project_name.replace(' ', '-')
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2021-08-12 10:00:54 +00:00
|
|
|
if project_name.startswith('lib'):
|
|
|
|
project_name = project_name[3:]
|
|
|
|
|
2021-08-12 10:00:08 +00:00
|
|
|
CMakeTarget.cached_project_name = project_name
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
return project_name == self.name
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _extract_project_name(line: str):
|
|
|
|
project_name = None
|
|
|
|
|
|
|
|
# Try to get project name without whitespaces in it
|
2021-08-13 07:19:17 +00:00
|
|
|
match = re.search(r'^\s*project\s*\(\s*(\w[\w-]+)', line, re.IGNORECASE)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if not match:
|
|
|
|
# Try to get project name that contains whitespaces
|
2021-08-13 07:19:17 +00:00
|
|
|
match = re.search(r'^\s*project\s*\(\s*"?(\w[\s\w-]+)"?', line, re.IGNORECASE)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if match:
|
|
|
|
project_name = match.group(1)
|
|
|
|
|
|
|
|
return project_name
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
super().configure(state)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'cmake',
|
|
|
|
'-DCMAKE_BUILD_TYPE=Release',
|
2021-07-22 06:51:13 +00:00
|
|
|
f'-DCMAKE_INSTALL_PREFIX={state.install_path}',
|
|
|
|
f'-DCMAKE_PREFIX_PATH={state.prefix_path}',
|
2021-01-14 08:34:20 +00:00
|
|
|
]
|
|
|
|
|
2023-10-08 09:49:59 +00:00
|
|
|
opts = state.options
|
2023-11-03 11:10:02 +00:00
|
|
|
opts['CMAKE_C_FLAGS'] += state.compiler_flags()
|
|
|
|
opts['CMAKE_CXX_FLAGS'] += state.compiler_flags()
|
|
|
|
opts['CMAKE_EXE_LINKER_FLAGS'] += state.linker_flags()
|
|
|
|
opts['CMAKE_SHARED_LINKER_FLAGS'] += state.linker_flags()
|
2023-10-08 07:34:55 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
if state.xcode:
|
|
|
|
args.append('-GXcode')
|
|
|
|
else:
|
|
|
|
args.append('-GUnix Makefiles')
|
2021-09-01 08:41:08 +00:00
|
|
|
|
|
|
|
if c_compiler := state.c_compiler():
|
|
|
|
args.append(f'-DCMAKE_C_COMPILER={c_compiler}')
|
|
|
|
if cxx_compiler := state.cxx_compiler():
|
|
|
|
args.append(f'-DCMAKE_CXX_COMPILER={cxx_compiler}')
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
architecture = state.architecture()
|
|
|
|
if architecture != machine():
|
|
|
|
args.append('-DCMAKE_SYSTEM_NAME=Darwin')
|
|
|
|
args.append('-DCMAKE_SYSTEM_PROCESSOR=' + 'aarch64' if architecture == 'arm64' else architecture)
|
|
|
|
|
2022-07-02 08:00:02 +00:00
|
|
|
sdk_path = state.sdk_path()
|
|
|
|
if sdk_path:
|
|
|
|
args.append(f'-DCMAKE_OSX_SYSROOT={sdk_path}')
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
os_version = state.os_version()
|
|
|
|
if os_version:
|
2021-06-25 07:03:53 +00:00
|
|
|
args.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=' + str(os_version))
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-10-08 09:49:59 +00:00
|
|
|
args += opts.to_list(CommandLineOptions.CMAKE_RULES)
|
2021-07-22 06:51:13 +00:00
|
|
|
args.append(state.source / self.src_root)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
|
|
|
if state.xcode:
|
2021-08-04 06:40:31 +00:00
|
|
|
args = ['cmake', '--open', '.']
|
2021-01-14 08:34:20 +00:00
|
|
|
else:
|
2021-03-16 11:23:48 +00:00
|
|
|
args = ['gmake', '-j', state.jobs]
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if state.verbose:
|
|
|
|
args.append('VERBOSE=1')
|
|
|
|
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
2021-05-01 08:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigureMakeDependencyTarget(ConfigureMakeTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
def post_build(self, state: BuildState):
|
2024-01-03 10:57:56 +00:00
|
|
|
state.build_path /= self.src_root
|
2021-05-01 08:54:52 +00:00
|
|
|
self.install(state)
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigureMakeStaticDependencyTarget(ConfigureMakeDependencyTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
2021-08-05 06:59:49 +00:00
|
|
|
def configure(self, state: BuildState):
|
|
|
|
state.options['--enable-shared'] = 'no'
|
|
|
|
super().configure(state)
|
2021-05-01 08:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CMakeStaticDependencyTarget(CMakeTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
2021-08-05 06:59:49 +00:00
|
|
|
def configure(self, state: BuildState):
|
|
|
|
state.options['BUILD_SHARED_LIBS'] = 'NO'
|
|
|
|
super().configure(state)
|
2021-05-01 08:54:52 +00:00
|
|
|
|
|
|
|
def post_build(self, state: BuildState):
|
|
|
|
self.install(state)
|
2021-06-02 09:48:58 +00:00
|
|
|
|
2023-07-20 09:10:14 +00:00
|
|
|
def keep_module_target(self, state: BuildState, target: str, module_paths: typing.Sequence[Path] = ()):
|
2021-06-20 11:43:17 +00:00
|
|
|
import_patterns = (
|
2023-07-20 09:09:06 +00:00
|
|
|
r'list\s*\(APPEND\s+_cmake_import_check_targets\s+(?P<target>[\w:-]+)[\s)]',
|
|
|
|
r'list\s*\(APPEND\s+_cmake_import_check_files_for_(?P<target>[\w:-]+)\s',
|
2021-06-20 11:43:17 +00:00
|
|
|
)
|
|
|
|
import_regexes = [re.compile(regex, re.IGNORECASE) for regex in import_patterns]
|
2021-06-02 09:48:58 +00:00
|
|
|
|
2021-06-20 11:43:17 +00:00
|
|
|
def _keep_target(line: str):
|
|
|
|
for regex in import_regexes:
|
|
|
|
match = regex.match(line)
|
|
|
|
|
2021-07-25 09:04:58 +00:00
|
|
|
if match and match.group('target') != target:
|
|
|
|
return None
|
2021-06-20 11:43:17 +00:00
|
|
|
|
|
|
|
return line
|
|
|
|
|
2023-07-20 09:10:14 +00:00
|
|
|
probe_modules = False
|
2021-06-20 11:43:17 +00:00
|
|
|
|
2023-07-20 09:10:14 +00:00
|
|
|
if not module_paths:
|
|
|
|
default_modules_path = state.install_path / 'lib' / 'cmake' / self.name
|
|
|
|
default_module_name = 'targets-release.cmake'
|
|
|
|
module_paths = (
|
|
|
|
default_modules_path / default_module_name,
|
|
|
|
default_modules_path / (self.name + default_module_name)
|
|
|
|
)
|
|
|
|
probe_modules = True
|
2021-06-20 11:43:17 +00:00
|
|
|
|
2023-07-20 09:10:14 +00:00
|
|
|
for module_path in module_paths:
|
|
|
|
if not probe_modules or module_path.exists():
|
2021-06-20 11:43:17 +00:00
|
|
|
self.update_text_file(module_path, _keep_target)
|
2022-09-23 10:48:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SingleExeCTarget(MakeTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
self.options = ()
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
super().configure(state)
|
|
|
|
|
|
|
|
for option in self.options:
|
|
|
|
state.options[option] = None
|
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
|
|
|
c_compiler = state.c_compiler()
|
|
|
|
assert c_compiler
|
|
|
|
|
|
|
|
args = [str(c_compiler), '-O3', '-o', self.name] + state.options.to_list()
|
|
|
|
|
|
|
|
for var in ('CFLAGS', 'LDFLAGS'):
|
|
|
|
args += shlex.split(state.environment[var])
|
|
|
|
|
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
|
|
|
|
|
|
|
def post_build(self, state: BuildState):
|
|
|
|
self.copy_to_bin(state)
|
2023-03-27 08:01:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MesonTarget(BuildTarget):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
def configure(self, state: BuildState):
|
|
|
|
super().configure(state)
|
|
|
|
|
2023-03-30 13:38:07 +00:00
|
|
|
args = [
|
|
|
|
state.bin_path / 'meson',
|
|
|
|
'setup',
|
|
|
|
f'--prefix={state.install_path}',
|
|
|
|
'--buildtype=release',
|
|
|
|
'--default-library=static',
|
|
|
|
]
|
|
|
|
|
|
|
|
if state.xcode:
|
|
|
|
args.append('--backend=xcode')
|
|
|
|
else:
|
|
|
|
cross_file_path = state.build_path / (state.architecture() + '.txt')
|
|
|
|
self._write_cross_file(cross_file_path, state)
|
|
|
|
args.append(f'--cross-file={cross_file_path}')
|
|
|
|
|
|
|
|
args += state.options.to_list(CommandLineOptions.CMAKE_RULES)
|
|
|
|
args.append(state.build_path)
|
|
|
|
args.append(state.source)
|
|
|
|
|
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
|
|
|
|
|
|
|
def build(self, state: BuildState):
|
|
|
|
if state.xcode:
|
|
|
|
args = ['open', f'{self.name}.xcodeproj']
|
|
|
|
else:
|
|
|
|
args = [state.bin_path / 'meson', 'compile']
|
|
|
|
|
|
|
|
if state.verbose:
|
|
|
|
args.append('--verbose')
|
|
|
|
|
|
|
|
subprocess.run(args, check=True, cwd=state.build_path, env=state.environment)
|
|
|
|
|
|
|
|
def post_build(self, state: BuildState):
|
|
|
|
self.install(state, tool=state.bin_path / 'meson')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _write_cross_file(path: Path, state: BuildState):
|
2023-03-27 08:01:04 +00:00
|
|
|
c_compiler = state.c_compiler()
|
|
|
|
assert c_compiler
|
|
|
|
|
|
|
|
cxx_compiler = state.cxx_compiler()
|
|
|
|
assert cxx_compiler
|
|
|
|
|
|
|
|
cpu = state.architecture()
|
|
|
|
cpu_family = 'arm' if 'arm64' == cpu else cpu
|
|
|
|
|
2023-03-30 13:38:07 +00:00
|
|
|
with open(path, 'w') as f:
|
2023-03-27 08:01:04 +00:00
|
|
|
f.write(f'''
|
|
|
|
[binaries]
|
|
|
|
c = '{c_compiler}'
|
|
|
|
cpp = '{cxx_compiler}'
|
|
|
|
objc = '{c_compiler}'
|
|
|
|
objcpp = '{cxx_compiler}'
|
|
|
|
pkgconfig = '{state.prefix_path}/bin/pkg-config'
|
|
|
|
strip = '/usr/bin/strip'
|
|
|
|
|
|
|
|
[host_machine]
|
|
|
|
system = 'darwin'
|
|
|
|
cpu_family = '{cpu_family}'
|
|
|
|
cpu = '{cpu}'
|
|
|
|
endian = 'little'
|
|
|
|
''')
|