From 22b4d59fc21d761c4777e00dbc55e76ce0964166 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 15:24:21 +0200 Subject: [PATCH 1/7] allow to fetch gn even when --no-sync-deps --- build_skia.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build_skia.py b/build_skia.py index 5b6ffb3..6d26c46 100644 --- a/build_skia.py +++ b/build_skia.py @@ -150,6 +150,7 @@ def _split_archive_base_and_format(parser, fname): parser.add_argument("-a", "--archive-file", default=None) parser.add_argument("--no-virtualenv", dest="make_virtualenv", action="store_false") parser.add_argument("--no-sync-deps", dest="sync_deps", action="store_false") + parser.add_argument("--no-fetch-gn", dest="fetch_gn", action="store_false") parser.add_argument("--gn-path", default=GN_PATH, help="default: %(default)s") args = parser.parse_args() @@ -175,6 +176,12 @@ def _split_archive_base_and_format(parser, fname): env=env, cwd=SKIA_SRC_DIR, ) + elif args.fetch_gn: + subprocess.check_call( + ["python", os.path.join("bin", "fetch-gn")], + env=env, + cwd=SKIA_SRC_DIR, + ) build_args = list(SKIA_BUILD_ARGS) if args.shared_lib: From bc0720b515b6b009436fb18338b68084c3a2453b Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 16:48:01 +0200 Subject: [PATCH 2/7] build_skia.py: rename venv2 -> venv --- build_skia.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build_skia.py b/build_skia.py index 6d26c46..3d92d10 100644 --- a/build_skia.py +++ b/build_skia.py @@ -161,7 +161,7 @@ def _split_archive_base_and_format(parser, fname): build_dir = os.path.abspath(args.build_dir) if args.make_virtualenv: - venv_dir = os.path.join(build_dir, "venv2") + venv_dir = os.path.join(build_dir, "venv") env = make_virtualenv(venv_dir) else: env = os.environ.copy() @@ -210,7 +210,7 @@ def _split_archive_base_and_format(parser, fname): os.rename(f, f.replace(".dll", "")) if args.archive_file is not None: - # we pack the whole build_dir except for the venv2/ subdir + # we pack the whole build_dir except for the venv/ subdir if args.make_virtualenv: shutil.rmtree(venv_dir) shutil.make_archive(archive_base, archive_fmt, build_dir) From d7e56fb6c752776299128c3b5f30dc0b5b0f8579 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 18:01:46 +0200 Subject: [PATCH 3/7] add __pycache__/ to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5c10209..234165a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist/ build/ libskia*.zip +__pycache__/ From a3871e47f76296c7dae62fe475eabdc6b641a1e4 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 18:03:13 +0200 Subject: [PATCH 4/7] add .venv to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 234165a..d7b1937 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ dist/ build/ libskia*.zip __pycache__/ +.venv* From 399e8ee7a520375542a1fdbe32ca1fe503140617 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 18:08:37 +0200 Subject: [PATCH 5/7] add support for universal2 macos target combining both x86_64 and arm64 architectures --- build_skia.py | 103 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 28 deletions(-) diff --git a/build_skia.py b/build_skia.py index 3d92d10..6e00ce5 100644 --- a/build_skia.py +++ b/build_skia.py @@ -55,8 +55,6 @@ SKIA_BUILD_ARGS.append("skia_enable_gpu=false") SKIA_BUILD_ARGS.append("skia_use_gl=false") -GN_PATH = os.path.join(SKIA_SRC_DIR, "bin", "gn" + EXE_EXT) - def make_virtualenv(venv_dir): from contextlib import closing @@ -127,6 +125,45 @@ def _split_archive_base_and_format(parser, fname): return fname.split(ext)[0], fmt +def build_skia( + src_dir, + build_dir, + build_args, + target_cpu=None, + env=None, + shared_lib=False, + gn_path=None, +): + src_dir = os.path.abspath(src_dir) + build_dir = os.path.abspath(build_dir) + if target_cpu: + build_args += ['target_cpu="{}"'.format(target_cpu)] + if shared_lib: + build_args += ["is_component_build=true"] + if gn_path is None: + gn_path = os.path.join(src_dir, "bin", "gn" + EXE_EXT) + + subprocess.check_call( + [ + gn_path, + "gen", + os.path.abspath(build_dir), + "--args={}".format(" ".join(build_args)), + ], + env=env, + cwd=src_dir, + ) + + subprocess.check_call(["ninja", "-C", build_dir], env=env) + + # when building skia.dll on windows with gn and ninja, the DLL import file + # is written as 'skia.dll.lib'; however, when linking it with the extension + # module, setuptools expects it to be named 'skia.lib'. + if sys.platform == "win32" and shared_lib: + for f in glob.glob(os.path.join(build_dir, "skia.dll.*")): + os.rename(f, f.replace(".dll", "")) + + if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( @@ -145,23 +182,26 @@ def _split_archive_base_and_format(parser, fname): "--target-cpu", default=None, help="The desired CPU architecture for the build (default: host)", - choices=["x86", "x64", "arm", "arm64", "mipsel"], + choices=["x86", "x64", "arm", "arm64", "mipsel", "universal2"], ) parser.add_argument("-a", "--archive-file", default=None) parser.add_argument("--no-virtualenv", dest="make_virtualenv", action="store_false") parser.add_argument("--no-sync-deps", dest="sync_deps", action="store_false") parser.add_argument("--no-fetch-gn", dest="fetch_gn", action="store_false") - parser.add_argument("--gn-path", default=GN_PATH, help="default: %(default)s") + parser.add_argument("--gn-path", default=None) args = parser.parse_args() + if args.target_cpu == "universal2" and sys.platform != "darwin": + parser.error("--target-cpu='universal2' only works on MacOS") + if args.archive_file is not None: archive_base, archive_fmt = _split_archive_base_and_format( parser, args.archive_file ) - build_dir = os.path.abspath(args.build_dir) + build_base_dir = os.path.abspath(args.build_dir) if args.make_virtualenv: - venv_dir = os.path.join(build_dir, "venv") + venv_dir = os.path.join(build_base_dir, "venv") env = make_virtualenv(venv_dir) else: env = os.environ.copy() @@ -183,34 +223,41 @@ def _split_archive_base_and_format(parser, fname): cwd=SKIA_SRC_DIR, ) - build_args = list(SKIA_BUILD_ARGS) - if args.shared_lib: - build_args.append("is_component_build=true") - if args.target_cpu: - build_args.append('target_cpu="{}"'.format(args.target_cpu)) + is_universal2 = args.target_cpu == "universal2" - subprocess.check_call( + builds = ( [ - args.gn_path, - "gen", - build_dir, - "--args={}".format(" ".join(build_args)), - ], - env=env, - cwd=SKIA_SRC_DIR, + (os.path.join(build_base_dir, target_cpu), target_cpu) + for target_cpu in ("x64", "arm64") + ] + if is_universal2 + else [(build_base_dir, args.target_cpu)] ) - subprocess.check_call(["ninja", "-C", build_dir], env=env) + for build_dir, target_cpu in builds: + build_skia( + SKIA_SRC_DIR, + build_dir, + SKIA_BUILD_ARGS, + target_cpu, + env, + args.shared_lib, + args.gn_path, + ) - # when building skia.dll on windows with gn and ninja, the DLL import file - # is written as 'skia.dll.lib'; however, when linking it with the extension - # module, setuptools expects it to be named 'skia.lib'. - if sys.platform == "win32" and args.shared_lib: - for f in glob.glob(os.path.join(build_dir, "skia.dll.*")): - os.rename(f, f.replace(".dll", "")) + if is_universal2: + # create universal binary by merging multiple archs with the 'lipo' tool: + # https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary + libname = "libskia" + ".so" if args.shared_lib else ".a" + libraries = [os.path.join(build_dir, libname) for build_dir, _ in builds] + dest_path = os.path.join(build_base_dir, libname) + subprocess.check_call(["lipo", "-create", "-output", dest_path] + libraries) + # confirm that we got a 'fat' binary + result = subprocess.check_output(["lipo", "-archs", dest_path]) + assert "x86_64 arm64" == result.decode().strip() if args.archive_file is not None: - # we pack the whole build_dir except for the venv/ subdir + # we pack the whole build_base_dir except for the venv/ subdir if args.make_virtualenv: shutil.rmtree(venv_dir) - shutil.make_archive(archive_base, archive_fmt, build_dir) + shutil.make_archive(archive_base, archive_fmt, build_base_dir) From 9fda07919ed6b78e0014ef67ad56c57e38441dd2 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 18:26:36 +0200 Subject: [PATCH 6/7] fix library extension when building static --- build_skia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_skia.py b/build_skia.py index 6e00ce5..57439bc 100644 --- a/build_skia.py +++ b/build_skia.py @@ -248,7 +248,7 @@ def build_skia( if is_universal2: # create universal binary by merging multiple archs with the 'lipo' tool: # https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary - libname = "libskia" + ".so" if args.shared_lib else ".a" + libname = "libskia." + ("so" if args.shared_lib else "a") libraries = [os.path.join(build_dir, libname) for build_dir, _ in builds] dest_path = os.path.join(build_base_dir, libname) subprocess.check_call(["lipo", "-create", "-output", dest_path] + libraries) From 8c1ad1f63065e0c1dfa65ecee2aa578da7d0a68f Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 30 Sep 2021 18:57:11 +0200 Subject: [PATCH 7/7] ci.yml: export ARCH=universal2 on macos --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a13026b..6817c3d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,8 @@ on: jobs: build-mac: runs-on: macos-latest + env: + ARCH: "universal2" steps: - uses: actions/checkout@v2 with: