2021-01-14 08:34:20 +00:00
|
|
|
#
|
|
|
|
# Helper module to build macOS version of various source ports
|
|
|
|
# Copyright (C) 2020-2021 Alexey Lysiuk
|
|
|
|
#
|
|
|
|
# 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
|
2021-07-25 09:39:48 +00:00
|
|
|
from .target import Target, targets
|
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]
|
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
|
|
|
state.jobs = arguments.jobs and arguments.jobs or \
|
|
|
|
subprocess.check_output(['sysctl', '-n', 'hw.ncpu']).decode('ascii').strip()
|
|
|
|
|
|
|
|
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
|
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
|
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()
|
|
|
|
|
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
|
2021-08-05 06:53:07 +00:00
|
|
|
state.environment = os.environ
|
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)
|
|
|
|
|
|
|
|
Builder._merge_install_paths(install_paths, base_install_path)
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _merge_file(src: Path, src_sub_paths: typing.Sequence[Path], dst_path: Path):
|
|
|
|
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]
|
|
|
|
subprocess.check_call(args)
|
|
|
|
|
|
|
|
# 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]
|
2021-01-14 08:34:20 +00:00
|
|
|
subprocess.check_call(args)
|
|
|
|
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)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _merge_missing_files(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
|
|
|
|
|
|
|
|
Builder._merge_install_paths(shifted_src_paths, dst_path, missing_files_only=True)
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _merge_install_paths(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():
|
2021-07-22 06:51:13 +00:00
|
|
|
Builder._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:
|
|
|
|
Builder._merge_file(src, src_sub_paths, dst_path)
|
|
|
|
|
|
|
|
if not missing_files_only:
|
|
|
|
Builder._merge_missing_files(src_paths, dst_path)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
group = parser.add_argument_group()
|
|
|
|
group.add_argument('--xcode', action='store_true', help='generate Xcode project instead of build')
|
2021-01-17 10:37:35 +00:00
|
|
|
group.add_argument('--source-path', metavar='path',
|
|
|
|
help='path to store downloaded and checked out source code')
|
2021-01-14 08:34:20 +00:00
|
|
|
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('--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.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
|
|
|
|
|
|
|
return parser.parse_args(args)
|