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 hashlib
|
|
|
|
import os
|
2021-09-12 07:19:16 +00:00
|
|
|
import re
|
2021-01-14 08:34:20 +00:00
|
|
|
import shutil
|
|
|
|
import subprocess
|
2021-08-04 06:38:54 +00:00
|
|
|
import typing
|
2021-01-14 08:34:20 +00:00
|
|
|
import urllib.request
|
2021-08-05 07:12:20 +00:00
|
|
|
from pathlib import Path
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-11-04 11:07:36 +00:00
|
|
|
from .packaging.version import Version as StrictVersion
|
2021-08-05 06:59:49 +00:00
|
|
|
from .utility import CommandLineOptions
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
class BuildState:
|
|
|
|
def __init__(self):
|
2021-07-22 06:51:13 +00:00
|
|
|
self_path = Path(__file__)
|
|
|
|
self.root_path = self_path.parent.parent
|
|
|
|
self.deps_path = self.root_path / 'deps'
|
|
|
|
self.prefix_path = self.root_path / 'prefix'
|
|
|
|
self.bin_path = self.prefix_path / 'bin'
|
|
|
|
self.include_path = self.prefix_path / 'include'
|
|
|
|
self.lib_path = self.prefix_path / 'lib'
|
|
|
|
self.patch_path = self.root_path / 'patch'
|
|
|
|
self.source_path = self.root_path / 'source'
|
2022-08-16 09:25:55 +00:00
|
|
|
self.temp_path = self.root_path / 'temp'
|
2021-07-22 06:51:13 +00:00
|
|
|
|
|
|
|
self.source = Path()
|
2021-01-14 08:34:20 +00:00
|
|
|
self.external_source = True
|
|
|
|
|
|
|
|
self.build_path = None
|
|
|
|
self.native_build_path = None
|
|
|
|
|
|
|
|
self.output_path = None
|
|
|
|
self.install_path = None
|
|
|
|
|
2023-11-03 11:10:02 +00:00
|
|
|
self._compiler_flags = None
|
|
|
|
self._linker_flags = None
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
self.platform = None
|
|
|
|
self.xcode = False
|
|
|
|
self.verbose = False
|
|
|
|
self.jobs = 1
|
2022-12-28 10:48:17 +00:00
|
|
|
|
2022-08-22 07:49:48 +00:00
|
|
|
self.static_moltenvk = False
|
2022-12-28 10:48:17 +00:00
|
|
|
self.quasi_glib = False
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-05 06:53:07 +00:00
|
|
|
self.environment = os.environ.copy()
|
2021-08-05 06:59:49 +00:00
|
|
|
self.options = CommandLineOptions()
|
2021-08-05 06:53:07 +00:00
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
def architecture(self) -> str:
|
|
|
|
return self.platform.architecture if self.platform else ''
|
|
|
|
|
|
|
|
def host(self) -> str:
|
|
|
|
return self.platform.host if self.platform else ''
|
|
|
|
|
2021-06-25 07:03:53 +00:00
|
|
|
def os_version(self) -> StrictVersion:
|
|
|
|
return self.platform.os_version if self.platform else None
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-05 07:02:55 +00:00
|
|
|
def sdk_path(self) -> Path:
|
2021-09-01 08:41:08 +00:00
|
|
|
return self.platform.sdk_path if self.platform else None
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-09-12 07:19:16 +00:00
|
|
|
def sdk_version(self) -> typing.Union[StrictVersion, None]:
|
|
|
|
if sdk_path := self.sdk_path():
|
|
|
|
if match := re.search(r'/MacOSX(\d+.\d+).sdk', str(sdk_path), re.IGNORECASE):
|
|
|
|
return StrictVersion(match[1])
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-08-05 07:02:55 +00:00
|
|
|
def c_compiler(self) -> Path:
|
2021-09-01 08:41:08 +00:00
|
|
|
return self.platform.c_compiler if self.platform else None
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-05 07:02:55 +00:00
|
|
|
def cxx_compiler(self) -> Path:
|
2021-09-01 08:41:08 +00:00
|
|
|
return self.platform.cxx_compiler if self.platform else None
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-11-03 11:10:02 +00:00
|
|
|
def compiler_flags(self) -> str:
|
|
|
|
if not self._compiler_flags:
|
|
|
|
self._compiler_flags = f'-I{self.include_path} -ffile-prefix-map={self.source}/='
|
|
|
|
|
|
|
|
return self._compiler_flags
|
|
|
|
|
|
|
|
def linker_flags(self) -> str:
|
|
|
|
if not self._linker_flags:
|
|
|
|
self._linker_flags = f'-L{self.lib_path}'
|
|
|
|
|
|
|
|
# Fix for Xcode 15.0 known issue with the new linker
|
|
|
|
# https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes#Known-Issues
|
|
|
|
# Binaries using symbols with a weak definition crash at runtime on iOS 14/macOS 12 or older.
|
|
|
|
# This impacts primarily C++ projects due to their extensive use of weak symbols. (114813650) (FB13097713)
|
|
|
|
# Workaround: Bump the minimum deployment target to iOS 15, macOS 12, watchOS 8 or tvOS 15,
|
|
|
|
# or add -Wl,-ld_classic to the OTHER_LDFLAGS build setting.
|
|
|
|
|
|
|
|
ld_classic_arg = '-Wl,-ld_classic'
|
|
|
|
check_args = ('clang', '-xc++', ld_classic_arg, '-')
|
|
|
|
check_code = b'int main() {}'
|
|
|
|
|
|
|
|
if subprocess.run(check_args, capture_output=True, input=check_code).returncode == 0:
|
|
|
|
self._linker_flags += f' {ld_classic_arg}'
|
|
|
|
os.unlink('a.out')
|
|
|
|
|
|
|
|
return self._linker_flags
|
|
|
|
|
2023-01-05 08:48:58 +00:00
|
|
|
def checkout_git(self, url: str, branch: typing.Optional[str] = None):
|
2021-07-22 06:51:13 +00:00
|
|
|
if self.source.exists():
|
2021-04-10 11:35:12 +00:00
|
|
|
return
|
2021-03-13 14:53:14 +00:00
|
|
|
|
2021-04-10 11:35:12 +00:00
|
|
|
args = ('git', 'clone', '--recurse-submodules', url, self.source)
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=self.root_path, env=self.environment)
|
2021-04-10 11:35:12 +00:00
|
|
|
|
|
|
|
if branch:
|
|
|
|
args = ('git', 'checkout', '-b', branch, 'origin/' + branch)
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=self.source, env=self.environment)
|
2021-02-18 11:27:35 +00:00
|
|
|
|
2023-01-05 08:48:58 +00:00
|
|
|
def download_source(self, url: str, checksum: str, patches: typing.Union[tuple, list, str, None] = None):
|
2021-01-14 08:34:20 +00:00
|
|
|
if self.external_source:
|
|
|
|
return
|
|
|
|
|
2021-01-17 10:37:35 +00:00
|
|
|
os.makedirs(self.source, exist_ok=True)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
data, filepath = self._read_source_package(url)
|
|
|
|
self._verify_checksum(checksum, data, filepath)
|
|
|
|
|
|
|
|
first_path_component, extract_path = self._unpack_source_package(filepath)
|
2021-06-01 07:18:38 +00:00
|
|
|
|
|
|
|
if not patches:
|
|
|
|
pass
|
|
|
|
elif isinstance(patches, str):
|
|
|
|
self._apply_source_patch(extract_path, patches)
|
|
|
|
elif isinstance(patches, (tuple, list)):
|
|
|
|
for patch in patches:
|
|
|
|
self._apply_source_patch(extract_path, patch)
|
|
|
|
else:
|
|
|
|
assert False
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
# Adjust source and build paths according to extracted source code
|
2021-01-17 10:37:35 +00:00
|
|
|
self.source = extract_path
|
2021-07-22 06:51:13 +00:00
|
|
|
self.build_path = self.build_path / first_path_component
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-04 06:38:54 +00:00
|
|
|
def _read_source_package(self, url: str) -> typing.Tuple[bytes, Path]:
|
2021-01-14 08:34:20 +00:00
|
|
|
filename = url.rsplit(os.sep, 1)[1]
|
2021-07-22 06:51:13 +00:00
|
|
|
filepath = self.source / filename
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
if filepath.exists():
|
2021-01-14 08:34:20 +00:00
|
|
|
# Read existing source package
|
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
else:
|
|
|
|
# Download package with source code
|
|
|
|
print(f'Downloading {filename}')
|
|
|
|
|
|
|
|
response = urllib.request.urlopen(url)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(filepath, 'wb') as f:
|
|
|
|
data = response.read()
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
os.unlink(filepath)
|
|
|
|
raise
|
|
|
|
|
|
|
|
return data, filepath
|
|
|
|
|
|
|
|
@staticmethod
|
2021-07-22 06:51:13 +00:00
|
|
|
def _verify_checksum(checksum: str, data: bytes, filepath: Path) -> None:
|
2021-01-14 08:34:20 +00:00
|
|
|
file_hasher = hashlib.sha256()
|
|
|
|
file_hasher.update(data)
|
|
|
|
file_checksum = file_hasher.hexdigest()
|
|
|
|
|
|
|
|
if file_checksum != checksum:
|
2021-07-22 06:51:13 +00:00
|
|
|
filepath.unlink()
|
2021-01-14 08:34:20 +00:00
|
|
|
raise Exception(f'Checksum of {filepath} does not match, expected: {checksum}, actual: {file_checksum}')
|
|
|
|
|
2021-08-04 06:38:54 +00:00
|
|
|
def _unpack_source_package(self, filepath: Path) -> typing.Tuple[str, Path]:
|
2022-08-21 08:58:00 +00:00
|
|
|
args = ('tar', '-tf', filepath)
|
|
|
|
result = subprocess.run(args, check=True, env=self.environment, stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
file_paths_str = result.stdout.decode("utf-8")
|
2021-08-04 06:40:31 +00:00
|
|
|
file_paths = file_paths_str.split('\n')
|
2023-02-20 08:23:20 +00:00
|
|
|
file_paths.remove('')
|
|
|
|
assert len(file_paths) > 0
|
|
|
|
|
|
|
|
# Determine root path of source code to be extracted
|
|
|
|
# If all files and directories are stored in one top level directory, this directory is used as a root
|
|
|
|
# If there is no single top level directory, new root directory will be created
|
|
|
|
need_new_directory = False
|
2023-02-21 07:44:44 +00:00
|
|
|
first_path_component = ''
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-08-04 06:40:31 +00:00
|
|
|
for path in file_paths:
|
2023-02-20 08:23:20 +00:00
|
|
|
if os.sep not in path:
|
|
|
|
need_new_directory = True
|
2021-01-14 08:34:20 +00:00
|
|
|
break
|
2023-02-20 08:23:20 +00:00
|
|
|
else:
|
|
|
|
current_first_path_component = path[:path.find(os.sep)]
|
|
|
|
|
|
|
|
if first_path_component:
|
|
|
|
if first_path_component != current_first_path_component:
|
|
|
|
need_new_directory = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
first_path_component = current_first_path_component
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-02-20 08:23:20 +00:00
|
|
|
work_path = self.source
|
|
|
|
|
|
|
|
if need_new_directory:
|
|
|
|
first_path_component = Path(filepath.name).stem
|
|
|
|
work_path /= first_path_component
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
extract_path = self.source / first_path_component
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
if not extract_path.exists():
|
2023-02-20 08:23:20 +00:00
|
|
|
os.makedirs(work_path, exist_ok=True)
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
# Extract source code package
|
|
|
|
try:
|
2022-08-21 08:58:00 +00:00
|
|
|
args = ('tar', '-xf', filepath)
|
2023-02-20 08:23:20 +00:00
|
|
|
subprocess.run(args, check=True, cwd=work_path, env=self.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
except (IOError, subprocess.CalledProcessError):
|
|
|
|
shutil.rmtree(extract_path, ignore_errors=True)
|
|
|
|
raise
|
|
|
|
|
|
|
|
return first_path_component, extract_path
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
def _apply_source_patch(self, extract_path: Path, patch: str):
|
|
|
|
patch_path = self.patch_path / (patch + '.diff')
|
|
|
|
assert patch_path.exists()
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-02-19 10:26:25 +00:00
|
|
|
args = ['patch', '--strip=1', '--input=' + str(patch_path)]
|
|
|
|
|
2021-01-14 08:34:20 +00:00
|
|
|
# Check if patch is already applied
|
2023-02-19 10:26:25 +00:00
|
|
|
dry_run_args = args + ['--dry-run', '--force']
|
|
|
|
dry_run = subprocess.run(dry_run_args, cwd=extract_path, env=self.environment,
|
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2023-02-19 10:26:25 +00:00
|
|
|
if dry_run.returncode == 0:
|
2021-01-14 08:34:20 +00:00
|
|
|
# Patch wasn't applied yet, do it now
|
2022-08-21 08:58:00 +00:00
|
|
|
subprocess.run(args, check=True, cwd=extract_path, env=self.environment)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
|
|
|
def run_pkg_config(self, *args) -> str:
|
|
|
|
os.makedirs(self.build_path, exist_ok=True)
|
|
|
|
|
2021-07-22 06:51:13 +00:00
|
|
|
args = (self.bin_path / 'pkg-config',) + args
|
2022-08-21 08:58:00 +00:00
|
|
|
result = subprocess.run(args, check=True, cwd=self.build_path, env=self.environment, stdout=subprocess.PIPE)
|
2021-01-14 08:34:20 +00:00
|
|
|
|
2022-08-21 08:58:00 +00:00
|
|
|
return result.stdout.decode('utf-8').rstrip('\n')
|
2021-07-22 06:51:13 +00:00
|
|
|
|
2021-08-04 06:38:54 +00:00
|
|
|
def has_source_file(self, path: typing.Union[str, Path]):
|
2021-07-22 06:51:13 +00:00
|
|
|
return (self.source / path).exists()
|
2021-08-05 06:53:07 +00:00
|
|
|
|
2022-12-31 08:51:41 +00:00
|
|
|
def update_flags_environment_variable(self, name: str, value: str):
|
2021-08-05 06:53:07 +00:00
|
|
|
sdk_path = self.sdk_path()
|
|
|
|
if sdk_path:
|
2022-12-31 08:51:41 +00:00
|
|
|
value += f' -isysroot {sdk_path}'
|
2021-08-05 06:53:07 +00:00
|
|
|
|
|
|
|
os_version = self.os_version()
|
|
|
|
if os_version:
|
2022-12-31 08:51:41 +00:00
|
|
|
value += f' -mmacosx-version-min={os_version}'
|
|
|
|
|
|
|
|
env = self.environment
|
|
|
|
env[name] = env[name] + ' ' + value if name in env else value
|
2023-02-20 08:55:28 +00:00
|
|
|
|
|
|
|
def validate_minimum_version(self, version: str):
|
|
|
|
minimum_version = StrictVersion(version)
|
|
|
|
|
|
|
|
if os_version := self.os_version():
|
|
|
|
if os_version < minimum_version:
|
|
|
|
raise RuntimeError('Minimum OS version requirement is not met')
|
|
|
|
|
|
|
|
if sdk_version := self.sdk_version():
|
|
|
|
if sdk_version < minimum_version:
|
|
|
|
raise RuntimeError('Minimum SDK version requirement is not met')
|
2023-03-18 10:17:13 +00:00
|
|
|
|
|
|
|
def source_version(self):
|
|
|
|
version = ''
|
|
|
|
|
|
|
|
args = ('git', f'--git-dir={self.source}/.git', 'describe', '--tags')
|
|
|
|
git_describe = subprocess.run(args, env=self.environment, capture_output=True)
|
|
|
|
|
|
|
|
if git_describe.returncode == 0:
|
|
|
|
version = git_describe.stdout.decode('ascii')
|
|
|
|
|
|
|
|
return version
|