2020-04-25 10:05:12 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-04-25 14:31:52 +00:00
|
|
|
#
|
|
|
|
# Helper module to build macOS version of various source ports
|
|
|
|
# Copyright (C) 2020 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 sys
|
|
|
|
|
|
|
|
if sys.hexversion < 0x3070000:
|
|
|
|
print('Build module requires Python 3.7 or newer')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2020-04-25 10:05:12 +00:00
|
|
|
import argparse
|
2020-04-26 08:42:30 +00:00
|
|
|
import re
|
2020-04-25 10:05:12 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
2020-04-26 13:29:07 +00:00
|
|
|
import tarfile
|
2020-04-25 10:05:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Target(object):
|
2020-05-07 13:00:20 +00:00
|
|
|
_SETUP_PREFIX = '_setup_'
|
|
|
|
|
|
|
|
def __init__(self, name: str):
|
2020-04-25 10:05:12 +00:00
|
|
|
self.name = name
|
2020-05-07 13:00:20 +00:00
|
|
|
self.url = None
|
|
|
|
self.cmake_options = {}
|
|
|
|
self.post_build = None
|
|
|
|
|
|
|
|
def setup(self, builder: 'Builder'):
|
|
|
|
setup_func = getattr(Target, Target._SETUP_PREFIX + self.name)
|
|
|
|
setup_func(self, builder)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_target_names() -> list:
|
|
|
|
targets = []
|
|
|
|
|
|
|
|
for member in dir(Target):
|
|
|
|
if member.startswith(Target._SETUP_PREFIX):
|
|
|
|
targets.append(member[len(Target._SETUP_PREFIX):])
|
|
|
|
|
|
|
|
return targets
|
2020-04-25 13:42:22 +00:00
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_targets() -> dict:
|
2020-05-07 13:00:20 +00:00
|
|
|
return {name: Target(name) for name in Target.get_target_names()}
|
|
|
|
|
|
|
|
def _setup_gzdoom(self, builder: 'Builder'):
|
|
|
|
self.url = 'https://github.com/coelckers/gzdoom.git'
|
|
|
|
self.post_build = Target._copy_moltenvk
|
|
|
|
self._assign_zdoom_raze_cmake_options(builder)
|
|
|
|
|
|
|
|
def _setup_qzdoom(self, builder: 'Builder'):
|
|
|
|
self.url = 'https://github.com/madame-rachelle/qzdoom.git'
|
|
|
|
self.post_build = Target._copy_moltenvk
|
|
|
|
self._assign_zdoom_raze_cmake_options(builder)
|
|
|
|
|
|
|
|
def _setup_lzdoom(self, builder: 'Builder'):
|
|
|
|
self.url = 'https://github.com/drfrag666/gzdoom.git'
|
|
|
|
self._assign_zdoom_raze_cmake_options(builder)
|
|
|
|
|
|
|
|
def _setup_raze(self, builder: 'Builder'):
|
|
|
|
self.url = 'https://github.com/coelckers/Raze.git'
|
|
|
|
self._assign_zdoom_raze_cmake_options(builder)
|
|
|
|
|
2020-05-08 07:41:45 +00:00
|
|
|
def _setup_zandronum(self, builder: 'Builder'):
|
|
|
|
self.url = 'https://github.com/TorrSamaho/zandronum.git'
|
|
|
|
opts = self.cmake_options
|
|
|
|
opts['CMAKE_EXE_LINKER_FLAGS'] = '-framework AudioUnit -framework Carbon -framework IOKit'
|
|
|
|
# Linking to FluidSynth is disabled because Zandronum doesn't support FluidSynth 2.x
|
|
|
|
# opts['DYN_FLUIDSYNTH'] = 'NO'
|
|
|
|
opts['FMOD_INCLUDE_DIR'] = builder.include_path
|
|
|
|
opts['FMOD_LIBRARY'] = builder.lib_path + 'libfmodex.dylib'
|
|
|
|
# TODO: create app bundle in post build
|
|
|
|
|
2020-05-07 13:00:20 +00:00
|
|
|
def _assign_zdoom_raze_cmake_options(self, builder: 'Builder'):
|
|
|
|
extra_libs = (
|
|
|
|
'mpg123',
|
|
|
|
|
|
|
|
# FluidSynth with dependencies
|
|
|
|
'fluidsynth',
|
|
|
|
'instpatch-1.0',
|
|
|
|
'glib-2.0',
|
|
|
|
'gobject-2.0',
|
|
|
|
'intl',
|
|
|
|
'ffi',
|
|
|
|
'pcre',
|
|
|
|
|
|
|
|
# Sndfile with dependencies
|
|
|
|
'sndfile',
|
|
|
|
'ogg',
|
|
|
|
'vorbis',
|
|
|
|
'vorbisenc',
|
|
|
|
'FLAC',
|
|
|
|
'opus',
|
2020-05-07 11:52:56 +00:00
|
|
|
)
|
2020-05-07 13:00:20 +00:00
|
|
|
|
|
|
|
linker_args = '-framework AudioUnit -framework AudioToolbox -framework Carbon ' \
|
|
|
|
'-framework CoreAudio -framework CoreMIDI -framework CoreVideo -liconv'
|
|
|
|
|
|
|
|
for lib in extra_libs:
|
|
|
|
linker_args += f' {builder.lib_path}lib{lib}.a'
|
|
|
|
|
|
|
|
opts = self.cmake_options
|
|
|
|
opts['CMAKE_EXE_LINKER_FLAGS'] = linker_args
|
|
|
|
opts['FORCE_INTERNAL_ZLIB'] = 'YES'
|
|
|
|
opts['FORCE_INTERNAL_BZIP2'] = 'YES'
|
|
|
|
opts['PK3_QUIET_ZIPDIR'] = 'YES'
|
|
|
|
opts['DYN_OPENAL'] = 'NO'
|
|
|
|
# Explicit OpenAL configuration to avoid selection of Apple's framework
|
|
|
|
opts['OPENAL_INCLUDE_DIR'] = builder.include_path
|
|
|
|
opts['OPENAL_LIBRARY'] = builder.lib_path + 'libopenal.a'
|
2020-05-07 11:52:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _copy_moltenvk(builder: 'Builder'):
|
|
|
|
molten_lib = 'libMoltenVK.dylib'
|
|
|
|
src_path = builder.lib_path + molten_lib
|
|
|
|
dst_path = builder.build_path
|
|
|
|
|
|
|
|
if builder.xcode:
|
|
|
|
# TODO: Support other configurations
|
|
|
|
dst_path += 'Debug' + os.sep
|
|
|
|
|
|
|
|
dst_path += builder.target.name + '.app/Contents/MacOS' + os.sep
|
|
|
|
os.makedirs(dst_path, exist_ok=True)
|
|
|
|
|
|
|
|
dst_path += molten_lib
|
|
|
|
|
|
|
|
if not os.path.exists(dst_path):
|
|
|
|
copy_func = builder.xcode and os.symlink or shutil.copy
|
|
|
|
copy_func(src_path, dst_path)
|
|
|
|
|
2020-04-25 13:42:22 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
class Builder(object):
|
|
|
|
def __init__(self, args: list):
|
|
|
|
self.root_path = os.path.dirname(os.path.abspath(__file__)) + os.sep
|
|
|
|
self.deps_path = self.root_path + 'deps' + os.sep
|
|
|
|
self.prefix_path = self.root_path + 'prefix' + os.sep
|
|
|
|
self.include_path = self.prefix_path + 'include' + os.sep
|
|
|
|
self.lib_path = self.prefix_path + 'lib' + os.sep
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 13:00:20 +00:00
|
|
|
arguments = Builder._parse_arguments(args)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
self.xcode = arguments.xcode
|
|
|
|
self.checkout_commit = arguments.checkout_commit
|
|
|
|
self.generate = not arguments.skip_generate
|
|
|
|
self.rebuild_prefix = arguments.rebuild_prefix
|
|
|
|
self.build_path = arguments.build_path
|
|
|
|
self.sdk_path = arguments.sdk_path
|
|
|
|
self.create_package = arguments.create_package
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 13:00:20 +00:00
|
|
|
targets = Target.create_targets()
|
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
if arguments.target:
|
|
|
|
self.target = targets[arguments.target]
|
|
|
|
self.source_path = self.root_path + 'source' + os.sep + self.target.name
|
2020-04-25 14:53:43 +00:00
|
|
|
else:
|
2020-05-07 10:32:30 +00:00
|
|
|
assert arguments.source_path
|
|
|
|
self.source_path = arguments.source_path
|
|
|
|
self._detect_target(targets)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
if not self.build_path:
|
|
|
|
self.build_path = self.root_path + 'build' + os.sep + self.target.name + \
|
|
|
|
os.sep + (self.xcode and 'xcode' or 'make')
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
self.source_path += os.sep
|
|
|
|
self.build_path += os.sep
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 13:00:20 +00:00
|
|
|
self.target.setup(self)
|
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
def run(self):
|
|
|
|
self._create_prefix_directory()
|
|
|
|
self._prepare_source()
|
|
|
|
self._generate_cmake()
|
|
|
|
self._build_target()
|
|
|
|
self._make_package()
|
|
|
|
|
|
|
|
def _create_prefix_directory(self):
|
2020-05-07 10:32:30 +00:00
|
|
|
if os.path.exists(self.prefix_path):
|
|
|
|
if self.rebuild_prefix:
|
|
|
|
shutil.rmtree(self.prefix_path)
|
|
|
|
else:
|
|
|
|
return
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
os.makedirs(self.include_path)
|
|
|
|
os.makedirs(self.lib_path)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
for dep in os.scandir(self.deps_path):
|
|
|
|
if not dep.is_dir():
|
|
|
|
continue
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
dep_include_path = dep.path + os.sep + 'include' + os.sep
|
|
|
|
dep_lib_path = dep.path + os.sep + 'lib' + os.sep
|
2020-04-25 13:42:22 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
if not os.path.exists(dep_include_path) or not os.path.exists(dep_lib_path):
|
|
|
|
continue
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
for dep_include in os.scandir(dep_include_path):
|
|
|
|
os.symlink(dep_include.path, self.include_path + dep_include.name)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
for dep_lib in os.scandir(dep_lib_path):
|
|
|
|
os.symlink(dep_lib.path, self.lib_path + dep_lib.name)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
def _prepare_source(self):
|
2020-05-07 10:32:30 +00:00
|
|
|
if not os.path.exists(self.source_path):
|
|
|
|
args = ('git', 'clone', self.target.url, self.source_path)
|
|
|
|
subprocess.check_call(args, cwd=self.root_path)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
if self.checkout_commit:
|
|
|
|
args = ['git', 'checkout', self.checkout_commit]
|
|
|
|
subprocess.check_call(args, cwd=self.source_path)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
def _generate_cmake(self):
|
2020-05-07 10:32:30 +00:00
|
|
|
if not self.generate:
|
|
|
|
return
|
2020-04-25 13:42:22 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
environ = os.environ
|
|
|
|
environ['PATH'] = environ['PATH'] + os.pathsep + '/Applications/CMake.app/Contents/bin'
|
|
|
|
|
|
|
|
os.makedirs(self.build_path, exist_ok=True)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'cmake',
|
|
|
|
self.xcode and '-GXcode' or '-GUnix Makefiles',
|
|
|
|
'-DCMAKE_BUILD_TYPE=Release',
|
|
|
|
'-DCMAKE_PREFIX_PATH=' + self.prefix_path,
|
|
|
|
]
|
|
|
|
|
|
|
|
if self.sdk_path:
|
|
|
|
args.append('-DCMAKE_OSX_SYSROOT=' + self.sdk_path)
|
|
|
|
|
2020-05-07 13:00:20 +00:00
|
|
|
for cmake_arg_name, cmake_arg_value in self.target.cmake_options.items():
|
|
|
|
args.append(f'-D{cmake_arg_name}={cmake_arg_value}')
|
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
args.append(self.source_path)
|
|
|
|
|
|
|
|
subprocess.check_call(args, cwd=self.build_path, env=environ)
|
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
def _build_target(self):
|
2020-05-07 10:32:30 +00:00
|
|
|
if self.xcode:
|
|
|
|
# TODO: support case-sensitive file system
|
|
|
|
args = ('open', self.target.name + '.xcodeproj')
|
|
|
|
else:
|
|
|
|
jobs = subprocess.check_output(['sysctl', '-n', 'hw.ncpu']).decode('ascii').strip()
|
|
|
|
args = ('make', '-j', jobs)
|
2020-04-25 10:05:12 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
subprocess.check_call(args, cwd=self.build_path)
|
2020-04-26 13:29:07 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
if self.target.post_build:
|
|
|
|
self.target.post_build(self)
|
2020-04-26 13:29:07 +00:00
|
|
|
|
2020-05-07 11:52:56 +00:00
|
|
|
def _make_package(self):
|
2020-05-07 10:32:30 +00:00
|
|
|
if not self.create_package or self.xcode:
|
|
|
|
return
|
2020-04-26 13:29:07 +00:00
|
|
|
|
2020-05-07 10:32:30 +00:00
|
|
|
args = ['git', 'describe', '--tags']
|
|
|
|
version = subprocess.check_output(args, cwd=self.source_path).decode('ascii').strip()
|
|
|
|
package_path = f'{self.build_path}{self.target.name}-{version}.tar.bz2'
|
|
|
|
name_pos = len(self.build_path) - 1
|
|
|
|
|
|
|
|
if os.path.exists(package_path):
|
|
|
|
os.remove(package_path)
|
|
|
|
|
|
|
|
def tar_filter(tarinfo):
|
|
|
|
tarinfo.name = tarinfo.name[name_pos:]
|
|
|
|
tarinfo.uname = tarinfo.gname = "root"
|
|
|
|
tarinfo.uid = tarinfo.gid = 0
|
|
|
|
return tarinfo
|
|
|
|
|
|
|
|
with tarfile.open(package_path, 'w:bz2') as package:
|
|
|
|
bundle_path = self.build_path + self.target.name + '.app'
|
|
|
|
package.add(bundle_path, filter=tar_filter)
|
|
|
|
|
|
|
|
def _detect_target(self, targets: dict):
|
|
|
|
cmakelists_path = self.source_path + os.sep + 'CMakeLists.txt'
|
|
|
|
project_name = None
|
|
|
|
|
|
|
|
for line in open(cmakelists_path).readlines():
|
|
|
|
match = re.search(r'project\s*\(\s*(\w+)\s*\)', line, re.IGNORECASE)
|
|
|
|
if match:
|
|
|
|
project_name = match.group(1).lower()
|
|
|
|
break
|
|
|
|
|
|
|
|
assert project_name
|
|
|
|
self.target = targets[project_name]
|
|
|
|
|
|
|
|
@staticmethod
|
2020-05-07 13:00:20 +00:00
|
|
|
def _parse_arguments(args: list):
|
2020-05-07 10:32:30 +00:00
|
|
|
parser = argparse.ArgumentParser(description='*ZDoom binary dependencies for macOS')
|
|
|
|
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
2020-05-07 13:00:20 +00:00
|
|
|
group.add_argument('--target', choices=Target.get_target_names(), help='target to build')
|
2020-05-07 10:32:30 +00:00
|
|
|
group.add_argument('--source-path', metavar='path', help='path to target\'s source code')
|
|
|
|
|
|
|
|
group = parser.add_argument_group()
|
|
|
|
group.add_argument('--xcode', action='store_true', help='generate Xcode project instead of build')
|
|
|
|
group.add_argument('--checkout-commit', metavar='commit',
|
|
|
|
help='target\'s source code commit or tag to checkout')
|
|
|
|
group.add_argument('--build-path', metavar='path', help='target build path')
|
|
|
|
group.add_argument('--sdk-path', metavar='path', help='path to macOS SDK')
|
|
|
|
group.add_argument('--skip-generate', action='store_true', help='do not generate build environment')
|
|
|
|
group.add_argument('--create-package', action='store_true', help='create deployment package')
|
|
|
|
group.add_argument('--rebuild-prefix', action='store_true', help='rebuild prefix path')
|
|
|
|
|
|
|
|
return parser.parse_args(args)
|
|
|
|
|
2020-04-25 10:05:12 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-05-07 11:52:56 +00:00
|
|
|
Builder(sys.argv[1:]).run()
|