2021-01-14 08:34:20 +00:00
|
|
|
#
|
|
|
|
# Helper module to build macOS version of various source ports
|
2023-01-01 08:46:34 +00:00
|
|
|
# Copyright (C) 2020-2023 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 argparse
|
|
|
|
import os
|
|
|
|
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-03-21 10:52:54 +00:00
|
|
|
from .target import targets
|
|
|
|
from .target.base import Target
|
2021-08-05 07:12:20 +00:00
|
|
|
from .utility import (
|
|
|
|
OS_VERSION_ARM64,
|
|
|
|
OS_VERSION_X86_64,
|
|
|
|
CaseInsensitiveDict,
|
|
|
|
CommandLineOptions,
|
|
|
|
TargetPlatform,
|
|
|
|
symlink_directory,
|
|
|
|
)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Builder(object):
|
|
|
|
def __init__(self, args: list):
|
|
|
|
self._targets = CaseInsensitiveDict({target.name: target for target in targets()})
|
|
|
|
|
|
|
|
arguments = self._parse_arguments(args)
|
|
|
|
|
|
|
|
state = self._state = BuildState()
|
|
|
|
state.xcode = arguments.xcode
|
|
|
|
state.verbose = arguments.verbose
|
|
|
|
|
2021-08-04 06:38:54 +00:00
|
|
|
self._platforms: typing.List[TargetPlatform] = []
|
2021-01-14 08:34:20 +00:00
|
|
|
self._populate_platforms(arguments)
|
|
|
|
|
|
|
|
state.platform = self._platforms[0]
|
|
|
|
|
2022-08-16 09:25:55 +00:00
|
|
|
if arguments.temp_path:
|
|
|
|
state.temp_path = Path(arguments.temp_path).absolute()
|
|
|
|
|
|
|
|
state.environment['TMPDIR'] = str(state.temp_path) + os.sep
|
|
|
|
os.makedirs(state.temp_path, exist_ok=True)
|
|
|
|
|
2021-03-02 16:06:20 +00:00
|
|
|
if arguments.source_path:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.source_path = Path(arguments.source_path).absolute()
|
2021-03-02 16:06:20 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
if arguments.target:
|
2021-01-16 09:54:50 +00:00
|
|
|
self._target = self._targets[arguments.target]
|
2021-07-22 06:51:13 +00:00
|
|
|
state.source = state.source_path / self._target.name
|
2021-01-14 08:34:20 +00:00
|
|
|
state.external_source = False
|
2022-08-16 09:11:04 +00:00
|
|
|
|
|
|
|
os.makedirs(state.source_path, exist_ok=True)
|
2021-01-14 08:34:20 +00:00
|
|
|
else:
|
2021-01-17 10:37:35 +00:00
|
|
|
assert arguments.source
|
2021-07-22 06:51:13 +00:00
|
|
|
state.source = Path(arguments.source).absolute()
|
2021-01-14 08:34:20 +00:00
|
|
|
state.external_source = True
|
|
|
|
self._detect_target()
|
|
|
|
|
2022-08-18 07:28:49 +00:00
|
|
|
for target in self._targets.values():
|
|
|
|
if target != self._target:
|
|
|
|
target.initialize(state)
|
|
|
|
|
2021-01-16 09:54:50 +00:00
|
|
|
del self._targets
|
|
|
|
|
2021-03-02 16:06:27 +00:00
|
|
|
if arguments.build_path:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.build_path = Path(arguments.build_path).absolute()
|
2021-03-02 16:06:27 +00:00
|
|
|
else:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.build_path = state.root_path / 'build' / self._target.name / ('xcode' if state.xcode else 'make')
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-03-02 16:06:27 +00:00
|
|
|
if arguments.output_path:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.output_path = Path(arguments.output_path).absolute()
|
2021-03-02 16:06:27 +00:00
|
|
|
else:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.output_path = state.root_path / 'output'
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
self._environment = state.environment
|
|
|
|
|
2022-08-22 07:50:18 +00:00
|
|
|
state.static_moltenvk = arguments.static_moltenvk
|
2022-12-28 10:48:17 +00:00
|
|
|
state.quasi_glib = arguments.quasi_glib
|
2022-08-25 10:02:10 +00:00
|
|
|
state.jobs = arguments.jobs and arguments.jobs or self._get_default_job_count()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
def _get_default_job_count(self):
|
|
|
|
args = ('sysctl', '-n', 'hw.ncpu')
|
|
|
|
result = subprocess.run(args, check=True, env=self._environment, stdout=subprocess.PIPE)
|
|
|
|
return result.stdout.decode('ascii').strip()
|
2022-08-21 07:54:50 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
def _populate_platforms(self, arguments):
|
|
|
|
state = self._state
|
|
|
|
|
2021-08-07 12:57:05 +00:00
|
|
|
def adjust_sdk_path(path: str) -> Path:
|
2021-01-14 08:34:20 +00:00
|
|
|
if path:
|
2021-08-07 12:57:05 +00:00
|
|
|
return Path(path).absolute()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
sdk_probe_path = state.root_path / 'sdk' / f'MacOSX{os_version}.sdk'
|
|
|
|
return sdk_probe_path if sdk_probe_path.exists() else None
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if not arguments.disable_x64:
|
2021-06-25 07:04:38 +00:00
|
|
|
os_version = arguments.os_version_x64 if arguments.os_version_x64 else OS_VERSION_X86_64
|
2023-01-01 08:47:22 +00:00
|
|
|
assert os_version >= OS_VERSION_X86_64, f'macOS {os_version} is not supported'
|
2021-01-14 08:34:20 +00:00
|
|
|
sdk_path = adjust_sdk_path(arguments.sdk_path_x64)
|
|
|
|
platform = TargetPlatform('x86_64', 'x86_64-apple-darwin', os_version, sdk_path, state.prefix_path)
|
|
|
|
self._platforms.append(platform)
|
|
|
|
|
|
|
|
if not arguments.disable_arm:
|
2021-06-25 07:04:38 +00:00
|
|
|
os_version = arguments.os_version_arm if arguments.os_version_arm else OS_VERSION_ARM64
|
2023-01-01 08:47:22 +00:00
|
|
|
assert os_version >= OS_VERSION_ARM64, f'macOS {os_version} is not supported'
|
2021-01-14 08:34:20 +00:00
|
|
|
sdk_path = adjust_sdk_path(arguments.sdk_path_arm)
|
|
|
|
platform = TargetPlatform('arm64', 'aarch64-apple-darwin', os_version, sdk_path, state.prefix_path)
|
|
|
|
self._platforms.append(platform)
|
|
|
|
|
|
|
|
assert len(self._platforms) > 0
|
|
|
|
|
|
|
|
# Put native platform first in the list of platforms
|
|
|
|
if self._platforms[0].architecture == machine():
|
|
|
|
return
|
|
|
|
|
|
|
|
for platform in self._platforms:
|
|
|
|
if platform.architecture == machine():
|
|
|
|
native_platform = platform
|
|
|
|
self._platforms.remove(platform)
|
|
|
|
self._platforms.insert(0, native_platform)
|
|
|
|
break
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
state = self._state
|
2021-01-16 09:54:50 +00:00
|
|
|
target = self._target
|
2021-01-14 08:34:20 +00:00
|
|
|
target.prepare_source(state)
|
|
|
|
|
|
|
|
if target.destination == Target.DESTINATION_DEPS:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.install_path = state.deps_path / target.name
|
2021-01-14 08:34:20 +00:00
|
|
|
elif target.destination == Target.DESTINATION_OUTPUT:
|
2021-07-22 06:51:13 +00:00
|
|
|
state.install_path = state.output_path / target.name
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
assert state.install_path
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
if not state.xcode and state.install_path.exists():
|
2021-04-02 07:46:54 +00:00
|
|
|
shutil.rmtree(state.install_path)
|
|
|
|
|
|
|
|
self._create_prefix_directory()
|
|
|
|
|
2023-03-18 10:17:13 +00:00
|
|
|
if version := state.source_version():
|
|
|
|
print(f'Building {version}')
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
if target.multi_platform and not state.xcode:
|
|
|
|
self._build_multiple_platforms(target)
|
|
|
|
else:
|
|
|
|
self._build(target)
|
|
|
|
|
|
|
|
def _build(self, target: Target):
|
|
|
|
state = self._state
|
2022-08-21 07:54:50 +00:00
|
|
|
state.environment = self._environment.copy()
|
2021-08-05 06:59:49 +00:00
|
|
|
state.options = CommandLineOptions()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
target.configure(state)
|
2021-01-16 09:55:41 +00:00
|
|
|
target.build(state)
|
|
|
|
target.post_build(state)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-05 07:00:33 +00:00
|
|
|
def _build_multiple_platforms(self, target: Target):
|
|
|
|
assert target.multi_platform
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
state = self._state
|
|
|
|
base_build_path = state.build_path
|
|
|
|
base_install_path = state.install_path
|
|
|
|
install_paths = []
|
|
|
|
|
|
|
|
for platform in self._platforms:
|
2021-08-05 07:00:33 +00:00
|
|
|
if platform.architecture in target.unsupported_architectures:
|
2021-01-14 08:34:20 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
state.platform = platform
|
2021-07-22 06:51:13 +00:00
|
|
|
state.build_path = base_build_path / ('build_' + platform.architecture)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if platform.architecture == machine():
|
|
|
|
state.native_build_path = state.build_path
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
state.install_path = base_build_path / ('install_' + platform.architecture)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
self._build(target)
|
|
|
|
|
|
|
|
install_paths.append(state.install_path)
|
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
self._merge_install_paths(install_paths, base_install_path)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _compare_files(paths: typing.Sequence[Path]) -> bool:
|
2021-01-14 08:34:20 +00:00
|
|
|
content = None
|
|
|
|
|
|
|
|
for path in paths:
|
2021-07-22 06:51:13 +00:00
|
|
|
if not path.exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
return False
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
with path.open('rb') as f:
|
2021-01-14 08:34:20 +00:00
|
|
|
if content:
|
|
|
|
if content != f.read():
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
def _merge_file(self, src: Path, src_sub_paths: typing.Sequence[Path], dst_path: Path):
|
2023-03-30 14:09:28 +00:00
|
|
|
if src.is_symlink():
|
|
|
|
shutil.copy(src_sub_paths[0], dst_path, follow_symlinks=False)
|
|
|
|
return
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
with open(src, 'rb') as f:
|
2021-01-14 08:34:20 +00:00
|
|
|
header = f.read(8)
|
|
|
|
|
|
|
|
is_executable = header[:4] == b'\xcf\xfa\xed\xfe'
|
|
|
|
is_library = header == b'!<arch>\n'
|
|
|
|
|
|
|
|
if is_executable or is_library:
|
|
|
|
# Merge executable and library files
|
2021-07-22 06:51:13 +00:00
|
|
|
dst_file = dst_path / src.name
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-04 06:38:54 +00:00
|
|
|
args: typing.List[typing.Union[str, Path]] = ['lipo']
|
2021-01-14 08:34:20 +00:00
|
|
|
args += src_sub_paths
|
|
|
|
args += ['-create', '-output', dst_file]
|
2022-08-25 10:02:10 +00:00
|
|
|
subprocess.run(args, check=True, env=self._environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
# Apply ad-hoc code signing on executable files outside of application bundles
|
2021-07-22 06:51:13 +00:00
|
|
|
if is_executable and '.app/Contents/' not in str(src):
|
2021-08-04 06:40:31 +00:00
|
|
|
args = ['codesign', '--sign', '-', dst_file]
|
2022-08-25 10:02:10 +00:00
|
|
|
subprocess.run(args, check=True, env=self._environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
else:
|
|
|
|
if not Builder._compare_files(src_sub_paths):
|
2021-07-22 06:51:13 +00:00
|
|
|
print(f'WARNING: Source files for {dst_path / src.name} don\'t match')
|
2021-01-14 08:34:20 +00:00
|
|
|
shutil.copy(src_sub_paths[0], dst_path)
|
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
def _merge_missing_files(self, src_paths: typing.Sequence[Path], dst_path: Path):
|
2021-01-14 08:34:20 +00:00
|
|
|
shifted_src_paths = [path for path in src_paths]
|
|
|
|
last_path_index = len(src_paths) - 1
|
|
|
|
|
|
|
|
for _ in range(last_path_index):
|
|
|
|
shifted_src_paths.append(shifted_src_paths[0])
|
|
|
|
del shifted_src_paths[0]
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
if not shifted_src_paths[0].exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
continue
|
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
self._merge_install_paths(shifted_src_paths, dst_path, missing_files_only=True)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-25 10:02:10 +00:00
|
|
|
def _merge_install_paths(self, src_paths: typing.Sequence[Path], dst_path: Path, missing_files_only=False):
|
2021-01-14 08:34:20 +00:00
|
|
|
if len(src_paths) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not missing_files_only:
|
2021-07-22 06:51:13 +00:00
|
|
|
if dst_path.exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
shutil.rmtree(dst_path)
|
|
|
|
|
|
|
|
os.makedirs(dst_path, exist_ok=True)
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
for src in src_paths[0].iterdir():
|
|
|
|
src_sub_paths = [path / src.name for path in src_paths]
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if src.is_dir():
|
2022-08-25 10:02:10 +00:00
|
|
|
self._merge_install_paths(src_sub_paths, dst_path / src.name, missing_files_only)
|
2021-01-14 08:34:20 +00:00
|
|
|
elif src.name.endswith('.la'):
|
|
|
|
# Skip libtool files
|
|
|
|
continue
|
|
|
|
elif missing_files_only:
|
|
|
|
for src_sub_path in src_sub_paths[1:]:
|
2021-07-22 06:51:13 +00:00
|
|
|
if not src_sub_path.exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
shutil.copy(src_sub_paths[0], dst_path)
|
|
|
|
else:
|
2022-08-25 10:02:10 +00:00
|
|
|
self._merge_file(src, src_sub_paths, dst_path)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
if not missing_files_only:
|
2022-08-25 10:02:10 +00:00
|
|
|
self._merge_missing_files(src_paths, dst_path)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def _create_prefix_directory(self):
|
|
|
|
state = self._state
|
|
|
|
os.makedirs(state.prefix_path, exist_ok=True)
|
|
|
|
|
|
|
|
cleanup = True
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
for dep in state.deps_path.iterdir():
|
2021-01-14 08:34:20 +00:00
|
|
|
if dep.is_dir():
|
2021-07-22 06:51:13 +00:00
|
|
|
symlink_directory(dep, state.prefix_path, cleanup)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
# Do symlink cleanup only once
|
|
|
|
cleanup = False
|
|
|
|
|
2022-06-20 07:31:13 +00:00
|
|
|
Builder._remove_empty_directories(state.prefix_path)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _remove_empty_directories(path: Path) -> int:
|
|
|
|
content: typing.List[str] = os.listdir(path)
|
|
|
|
count = len(content)
|
|
|
|
removed = 0
|
|
|
|
|
|
|
|
for entry in content:
|
|
|
|
abspath = path / entry
|
|
|
|
|
|
|
|
if os.path.isdir(abspath):
|
|
|
|
removed += Builder._remove_empty_directories(abspath)
|
|
|
|
|
|
|
|
if count == removed:
|
|
|
|
os.rmdir(path)
|
|
|
|
removed = 1
|
|
|
|
|
|
|
|
return removed
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
def _detect_target(self):
|
|
|
|
for name, target in self._targets.items():
|
|
|
|
if target.detect(self._state):
|
2021-01-16 09:54:50 +00:00
|
|
|
self._target = self._targets[name]
|
2021-01-14 08:34:20 +00:00
|
|
|
break
|
|
|
|
|
2021-01-16 09:54:50 +00:00
|
|
|
assert self._target
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def _parse_arguments(self, args: list):
|
|
|
|
assert self._targets
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='*ZDoom binary dependencies for macOS')
|
|
|
|
|
2021-08-04 06:40:31 +00:00
|
|
|
excl_group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
excl_group.add_argument('--target', choices=self._targets.keys(), help='target to build')
|
|
|
|
excl_group.add_argument('--source', metavar='path', help='path to target\'s source code')
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-12-28 10:46:19 +00:00
|
|
|
group = parser.add_argument_group('Configuration options')
|
2021-01-14 08:34:20 +00:00
|
|
|
group.add_argument('--xcode', action='store_true', help='generate Xcode project instead of build')
|
|
|
|
group.add_argument('--os-version-x64', metavar='version', help='macOS deployment version for x86_64')
|
|
|
|
group.add_argument('--os-version-arm', metavar='version', help='macOS deployment version for ARM64')
|
|
|
|
group.add_argument('--verbose', action='store_true', help='enable verbose build output')
|
|
|
|
group.add_argument('--jobs', help='number of parallel compilation jobs')
|
|
|
|
|
2021-08-04 06:40:31 +00:00
|
|
|
excl_group = parser.add_mutually_exclusive_group()
|
|
|
|
excl_group.add_argument('--disable-x64', action='store_true', help='disable x86_64 support')
|
|
|
|
excl_group.add_argument('--disable-arm', action='store_true', help='disable ARM64 support')
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-12-28 10:46:19 +00:00
|
|
|
group = parser.add_argument_group('Paths')
|
|
|
|
group.add_argument('--source-path', metavar='path',
|
|
|
|
help='path to store downloaded and checked out source code')
|
|
|
|
group.add_argument('--build-path', metavar='path', help='target build path')
|
|
|
|
group.add_argument('--output-path', metavar='path', help='output path for main targets')
|
|
|
|
group.add_argument('--temp-path', metavar='path', help='path to temporary files directory')
|
|
|
|
group.add_argument('--sdk-path-x64', metavar='path', help='path to macOS SDK for x86_64')
|
|
|
|
group.add_argument('--sdk-path-arm', metavar='path', help='path to macOS SDK for ARM64')
|
|
|
|
|
|
|
|
group = parser.add_argument_group('Hacks')
|
|
|
|
group.add_argument('--static-moltenvk', action='store_true', help='link with static MoltenVK library')
|
2022-12-28 10:48:17 +00:00
|
|
|
group.add_argument('--quasi-glib', action='store_true', help='link with QuasiGlib library')
|
2022-12-28 10:46:19 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
return parser.parse_args(args)
|