2021-01-17 04:31:48 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2021-05-12 20:09:34 +00:00
|
|
|
The current tooling supported in CMake, Homebrew, and Qt5 are insufficient for
|
2021-01-17 04:31:48 +00:00
|
|
|
creating macOS universal binaries automatically for applications like Dolphin
|
|
|
|
which have more complicated build requirements (like different libraries, build
|
|
|
|
flags and source files for each target architecture).
|
|
|
|
|
|
|
|
So instead, this script manages the configuration and compilation of distinct
|
|
|
|
builds and project files for each target architecture and then merges the two
|
|
|
|
binaries into a single universal binary.
|
|
|
|
|
|
|
|
Running this script will:
|
|
|
|
1) Generate Xcode project files for the ARM build (if project files don't
|
|
|
|
already exist)
|
|
|
|
2) Generate Xcode project files for the x64 build (if project files don't
|
|
|
|
already exist)
|
|
|
|
3) Build the ARM project for the selected build_target
|
|
|
|
4) Build the x64 project for the selected build_target
|
2021-05-12 20:09:34 +00:00
|
|
|
5) Generate universal .app packages combining the ARM and x64 packages
|
|
|
|
6) Use the lipo tool to combine the binary objects inside each of the
|
2021-01-17 04:31:48 +00:00
|
|
|
packages into universal binaries
|
2021-05-12 20:09:34 +00:00
|
|
|
7) Code sign the final universal binaries using the specified
|
2021-01-17 04:31:48 +00:00
|
|
|
codesign_identity
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import filecmp
|
|
|
|
import glob
|
2021-04-17 23:53:08 +00:00
|
|
|
import json
|
2021-05-02 00:57:39 +00:00
|
|
|
import multiprocessing
|
2021-01-17 04:31:48 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# The config variables listed below are the defaults, but they can be
|
|
|
|
# overridden by command line arguments see parse_args(), or run:
|
|
|
|
# BuildMacOSUniversalBinary.py --help
|
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
|
|
|
|
# Location of destination universal binary
|
|
|
|
"dst_app": "universal/",
|
|
|
|
# Build Target (dolphin-emu to just build the emulator and skip the tests)
|
|
|
|
"build_target": "ALL_BUILD",
|
|
|
|
|
2021-05-12 20:09:34 +00:00
|
|
|
# Location for CMake to search for files (default is for homebrew)
|
|
|
|
"arm64_cmake_prefix": "/opt/homebrew",
|
|
|
|
"x86_64_cmake_prefix": "/usr/local",
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
# Locations to qt5 directories for arm and x64 libraries
|
|
|
|
# The default values of these paths are taken from the default
|
|
|
|
# paths used for homebrew
|
2021-05-12 20:09:34 +00:00
|
|
|
"arm64_qt5_path": "/opt/homebrew/opt/qt5",
|
|
|
|
"x86_64_qt5_path": "/usr/local/opt/qt5",
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
# Identity to use for code signing. "-" indicates that the app will not
|
|
|
|
# be cryptographically signed/notarized but will instead just use a
|
|
|
|
# SHA checksum to verify the integrity of the app. This doesn't
|
|
|
|
# protect against malicious actors, but it does protect against
|
|
|
|
# running corrupted binaries and allows for access to the extended
|
|
|
|
# permisions needed for ARM builds
|
2021-05-12 20:09:34 +00:00
|
|
|
"codesign_identity": "-",
|
2021-04-17 23:53:08 +00:00
|
|
|
# Entitlements file to use for code signing
|
2021-01-17 04:31:48 +00:00
|
|
|
"entitlements": "../Source/Core/DolphinQt/DolphinEmu.entitlements",
|
|
|
|
|
|
|
|
# Minimum macOS version for each architecture slice
|
|
|
|
"arm64_mac_os_deployment_target": "11.0.0",
|
2021-05-02 00:57:39 +00:00
|
|
|
"x86_64_mac_os_deployment_target": "10.12.0",
|
|
|
|
|
|
|
|
# CMake Generator to use for building
|
|
|
|
"generator": "Unix Makefiles",
|
2021-05-12 20:09:34 +00:00
|
|
|
"build_type": "Release",
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Architectures to build for. This is explicity left out of the command line
|
|
|
|
# config options for several reasons:
|
|
|
|
# 1) Adding new architectures will generally require more code changes
|
|
|
|
# 2) Single architecture builds should utilize the normal generated cmake
|
|
|
|
# project files rather than this wrapper script
|
|
|
|
|
|
|
|
ARCHITECTURES = ["x86_64", "arm64"]
|
|
|
|
|
|
|
|
|
2021-04-17 23:53:08 +00:00
|
|
|
def parse_args(conf=DEFAULT_CONFIG):
|
2021-01-17 04:31:48 +00:00
|
|
|
"""
|
|
|
|
Parses the command line arguments into a config dictionary.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"--target",
|
|
|
|
help="Build target in generated project files",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf["build_target"],
|
2021-01-17 04:31:48 +00:00
|
|
|
dest="build_target")
|
2021-05-02 00:57:39 +00:00
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"-G",
|
|
|
|
help="CMake Generator to use for creating project files",
|
2021-05-02 00:57:39 +00:00
|
|
|
default=conf["generator"],
|
|
|
|
dest="generator")
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"--build_type",
|
|
|
|
help="CMake build type [Debug, Release, RelWithDebInfo, MinSizeRel]",
|
2021-05-02 00:57:39 +00:00
|
|
|
default=conf["build_type"],
|
|
|
|
dest="build_type")
|
2021-01-17 04:31:48 +00:00
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"--dst_app",
|
|
|
|
help="Directory where universal binary will be stored",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf["dst_app"])
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"--entitlements",
|
|
|
|
help="Path to .entitlements file for code signing",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf["entitlements"])
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
"--codesign",
|
|
|
|
help="Code signing identity to use to sign the applications",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf["codesign_identity"],
|
2021-01-17 04:31:48 +00:00
|
|
|
dest="codesign_identity")
|
|
|
|
|
|
|
|
for arch in ARCHITECTURES:
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
f"--{arch}_cmake_prefix",
|
|
|
|
help="Folder for cmake to search for packages",
|
2021-05-08 21:22:21 +00:00
|
|
|
default=conf[arch+"_cmake_prefix"],
|
|
|
|
dest=arch+"_cmake_prefix")
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
f"--{arch}_qt5_path",
|
|
|
|
help=f"Install path for {arch} qt5 libraries",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf[arch+"_qt5_path"])
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
parser.add_argument(
|
2021-05-12 20:09:34 +00:00
|
|
|
f"--{arch}_mac_os_deployment_target",
|
|
|
|
help=f"Deployment architecture for {arch} slice",
|
2021-04-17 23:53:08 +00:00
|
|
|
default=conf[arch+"_mac_os_deployment_target"])
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
return vars(parser.parse_args())
|
|
|
|
|
|
|
|
|
|
|
|
def lipo(path0, path1, dst):
|
2021-05-12 20:09:34 +00:00
|
|
|
if subprocess.call(["lipo", "-create", "-output", dst, path0, path1]) != 0:
|
|
|
|
print(f"WARNING: {path0} and {path1} cannot be lipo'd")
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
shutil.copy(path0, dst)
|
|
|
|
|
|
|
|
|
2021-05-12 20:09:34 +00:00
|
|
|
def recursive_merge_binaries(src0, src1, dst):
|
2021-01-17 04:31:48 +00:00
|
|
|
"""
|
|
|
|
Merges two build trees together for different architectures into a single
|
|
|
|
universal binary.
|
|
|
|
|
|
|
|
The rules for merging are:
|
|
|
|
|
|
|
|
1) Files that exist in either src tree are copied into the dst tree
|
|
|
|
2) Files that exist in both trees and are identical are copied over
|
|
|
|
unmodified
|
|
|
|
3) Files that exist in both trees and are non-identical are lipo'd
|
|
|
|
4) Symlinks are created in the destination tree to mirror the hierarchy in
|
|
|
|
the source trees
|
|
|
|
"""
|
|
|
|
|
2021-05-22 22:18:43 +00:00
|
|
|
# Check that all files present in the folder are of the same type and that
|
|
|
|
# links link to the same relative location
|
|
|
|
for newpath0 in glob.glob(src0+"/*"):
|
|
|
|
filename = os.path.basename(newpath0)
|
|
|
|
newpath1 = os.path.join(src1, filename)
|
|
|
|
if not os.path.exists(newpath1):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.islink(newpath0) and os.path.islink(newpath1):
|
|
|
|
if os.path.relpath(newpath0,src0) == os.path.relpath(newpath1,src1):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.isdir(newpath0) and os.path.isdir(newpath1):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# isfile() can be true for links so check that both are not links
|
|
|
|
# before checking if they are both files
|
|
|
|
if (not os.path.islink(newpath0)) and (not os.path.islink(newpath1)):
|
|
|
|
if os.path.isfile(newpath0) and os.path.isfile(newpath1):
|
|
|
|
continue
|
|
|
|
|
|
|
|
raise Exception(f"{newpath0} and {newpath1} cannot be " +
|
|
|
|
"merged into a universal binary because they are of " +
|
|
|
|
"incompatible types. Perhaps the installed libraries" +
|
|
|
|
" are from different versions for each architecture")
|
|
|
|
|
2021-01-17 04:31:48 +00:00
|
|
|
for newpath0 in glob.glob(src0+"/*"):
|
|
|
|
filename = os.path.basename(newpath0)
|
|
|
|
newpath1 = os.path.join(src1, filename)
|
|
|
|
new_dst_path = os.path.join(dst, filename)
|
|
|
|
if os.path.islink(newpath0):
|
2021-04-17 23:53:08 +00:00
|
|
|
# Symlinks will be fixed after files are resolved
|
2021-01-17 04:31:48 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not os.path.exists(newpath1):
|
2021-05-22 22:18:43 +00:00
|
|
|
if os.path.isdir(newpath0):
|
|
|
|
shutil.copytree(newpath0, new_dst_path)
|
|
|
|
else:
|
|
|
|
shutil.copy(newpath0, new_dst_path)
|
|
|
|
|
2021-01-17 04:31:48 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.isdir(newpath1):
|
|
|
|
os.mkdir(new_dst_path)
|
2021-05-12 20:09:34 +00:00
|
|
|
recursive_merge_binaries(newpath0, newpath1, new_dst_path)
|
2021-01-17 04:31:48 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if filecmp.cmp(newpath0, newpath1):
|
|
|
|
shutil.copy(newpath0, new_dst_path)
|
|
|
|
else:
|
|
|
|
lipo(newpath0, newpath1, new_dst_path)
|
|
|
|
|
2021-04-17 23:53:08 +00:00
|
|
|
# Loop over files in src1 and copy missing things over to dst
|
2021-01-17 04:31:48 +00:00
|
|
|
for newpath1 in glob.glob(src1+"/*"):
|
2021-04-17 22:50:41 +00:00
|
|
|
filename = os.path.basename(newpath1)
|
2021-01-17 04:31:48 +00:00
|
|
|
newpath0 = os.path.join(src0, filename)
|
|
|
|
new_dst_path = os.path.join(dst, filename)
|
2021-04-17 22:50:41 +00:00
|
|
|
if (not os.path.exists(newpath0)) and (not os.path.islink(newpath1)):
|
|
|
|
if os.path.isdir(newpath1):
|
|
|
|
shutil.copytree(newpath1, new_dst_path)
|
|
|
|
else:
|
|
|
|
shutil.copy(newpath1, new_dst_path)
|
2021-01-17 04:31:48 +00:00
|
|
|
|
2021-04-17 23:53:08 +00:00
|
|
|
# Fix up symlinks for path0
|
2021-01-17 04:31:48 +00:00
|
|
|
for newpath0 in glob.glob(src0+"/*"):
|
|
|
|
filename = os.path.basename(newpath0)
|
|
|
|
new_dst_path = os.path.join(dst, filename)
|
|
|
|
if os.path.islink(newpath0):
|
|
|
|
relative_path = os.path.relpath(os.path.realpath(newpath0), src0)
|
|
|
|
os.symlink(relative_path, new_dst_path)
|
2021-04-17 23:53:08 +00:00
|
|
|
# Fix up symlinks for path1
|
2021-01-17 04:31:48 +00:00
|
|
|
for newpath1 in glob.glob(src1+"/*"):
|
|
|
|
filename = os.path.basename(newpath1)
|
|
|
|
new_dst_path = os.path.join(dst, filename)
|
|
|
|
newpath0 = os.path.join(src0, filename)
|
|
|
|
if os.path.islink(newpath1) and not os.path.exists(newpath0):
|
|
|
|
relative_path = os.path.relpath(os.path.realpath(newpath1), src1)
|
|
|
|
os.symlink(relative_path, new_dst_path)
|
|
|
|
|
|
|
|
|
|
|
|
def build(config):
|
|
|
|
"""
|
|
|
|
Builds the project with the parameters specified in config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
print("Building config:")
|
|
|
|
print(json.dumps(config, indent=4))
|
|
|
|
|
|
|
|
# Configure and build single architecture builds for each architecture
|
|
|
|
for arch in ARCHITECTURES:
|
|
|
|
if not os.path.exists(arch):
|
|
|
|
os.mkdir(arch)
|
2021-04-17 23:53:08 +00:00
|
|
|
|
|
|
|
env = os.environ.copy()
|
2021-05-12 20:09:34 +00:00
|
|
|
env["Qt5_DIR"] = config[arch+"_qt5_path"]
|
|
|
|
env["CMAKE_OSX_ARCHITECTURES"] = arch
|
|
|
|
env["CMAKE_PREFIX_PATH"] = config[arch+"_cmake_prefix"]
|
2021-05-08 21:22:21 +00:00
|
|
|
|
|
|
|
# Add the other architecture's prefix path to the ignore path so that
|
|
|
|
# CMake doesn't try to pick up the wrong architecture's libraries when
|
|
|
|
# cross compiling.
|
|
|
|
ignore_path = ""
|
|
|
|
for a in ARCHITECTURES:
|
|
|
|
if a != arch:
|
|
|
|
ignore_path = config[a+"_cmake_prefix"]
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
subprocess.check_call([
|
2021-05-12 20:09:34 +00:00
|
|
|
"cmake", "../../", "-G", config["generator"],
|
|
|
|
"-DCMAKE_BUILD_TYPE=" + config["build_type"],
|
2021-05-02 08:50:43 +00:00
|
|
|
# System name needs to be specified for CMake to use
|
|
|
|
# the specified CMAKE_SYSTEM_PROCESSOR
|
2021-05-12 20:09:34 +00:00
|
|
|
"-DCMAKE_SYSTEM_NAME=Darwin",
|
|
|
|
"-DCMAKE_PREFIX_PATH="+config[arch+"_cmake_prefix"],
|
|
|
|
"-DCMAKE_SYSTEM_PROCESSOR="+arch,
|
|
|
|
"-DCMAKE_IGNORE_PATH="+ignore_path,
|
|
|
|
"-DCMAKE_OSX_DEPLOYMENT_TARGET="
|
2021-01-17 04:31:48 +00:00
|
|
|
+ config[arch+"_mac_os_deployment_target"],
|
2021-05-12 20:09:34 +00:00
|
|
|
"-DMACOS_CODE_SIGNING_IDENTITY="
|
|
|
|
+ config["codesign_identity"],
|
|
|
|
"-DMACOS_CODE_SIGNING_IDENTITY_UPDATER="
|
|
|
|
+ config["codesign_identity"],
|
2021-01-17 04:31:48 +00:00
|
|
|
'-DMACOS_CODE_SIGNING="ON"'
|
|
|
|
],
|
2021-04-17 23:53:08 +00:00
|
|
|
env=env, cwd=arch)
|
2021-01-17 04:31:48 +00:00
|
|
|
|
2021-05-02 00:57:39 +00:00
|
|
|
threads = multiprocessing.cpu_count()
|
2021-05-12 20:09:34 +00:00
|
|
|
subprocess.check_call(["cmake", "--build", ".",
|
|
|
|
"--config", config["build_type"],
|
|
|
|
"--parallel", f"{threads}"], cwd=arch)
|
2021-01-17 04:31:48 +00:00
|
|
|
|
2021-04-17 23:53:08 +00:00
|
|
|
dst_app = config["dst_app"]
|
2021-01-17 04:31:48 +00:00
|
|
|
|
|
|
|
if os.path.exists(dst_app):
|
|
|
|
shutil.rmtree(dst_app)
|
|
|
|
|
2021-04-17 23:53:08 +00:00
|
|
|
# Create and codesign the universal binary/
|
2021-01-17 04:31:48 +00:00
|
|
|
os.mkdir(dst_app)
|
2021-04-17 23:53:08 +00:00
|
|
|
|
|
|
|
# Source binary trees to merge together
|
2021-05-02 00:57:39 +00:00
|
|
|
src_app0 = ARCHITECTURES[0]+"/Binaries/"
|
|
|
|
src_app1 = ARCHITECTURES[1]+"/Binaries/"
|
2021-04-17 23:53:08 +00:00
|
|
|
|
2021-05-12 20:09:34 +00:00
|
|
|
recursive_merge_binaries(src_app0, src_app1, dst_app)
|
2021-01-17 04:31:48 +00:00
|
|
|
for path in glob.glob(dst_app+"/*"):
|
2021-05-12 20:09:34 +00:00
|
|
|
if os.path.isdir(path) and os.path.splitext(path)[1] != ".app":
|
2021-05-02 00:57:39 +00:00
|
|
|
continue
|
|
|
|
|
2021-01-17 04:31:48 +00:00
|
|
|
subprocess.check_call([
|
2021-05-12 20:09:34 +00:00
|
|
|
"codesign",
|
|
|
|
"-d",
|
|
|
|
"--force",
|
|
|
|
"-s",
|
2021-01-17 04:31:48 +00:00
|
|
|
config["codesign_identity"],
|
2021-05-12 20:09:34 +00:00
|
|
|
"--options=runtime",
|
|
|
|
"--entitlements", config["entitlements"],
|
|
|
|
"--deep",
|
|
|
|
"--verbose=2",
|
2021-01-17 04:31:48 +00:00
|
|
|
path])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
conf = parse_args()
|
|
|
|
build(conf)
|
|
|
|
print("Built Universal Binary successfully!")
|