Skip to content

Commit

Permalink
Build v8 for Apple Silicon (#202)
Browse files Browse the repository at this point in the history
* Build v8 for darwin_arm64

* ./deps/build.py --arch arm64

* import rogchap.com/v8go/deps/darwin_arm64 in cgo.go

* Bump Go version

* revert to ubuntu-18.04

* PR comments

* update PR template to include arch

* Revert "./deps/build.py --arch arm64"

This reverts commit de01fba.

* Update V8 static library for macos-11 arm64 (#216)

Co-authored-by: dylanahsmith <dylanahsmith@users.noreply.github.com>

* Add changelog entry

Co-authored-by: Dylan Thacker-Smith <Dylan.Smith@shopify.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: dylanahsmith <dylanahsmith@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 28, 2021
1 parent 3e22137 commit ea7debf
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 24 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ jobs:
name: Tests on ${{ matrix.go-version }} ${{ matrix.platform }}
strategy:
matrix:
go-version: [1.12.17, 1.16.4]
platform: [ubuntu-latest, macos-latest, windows-latest]
go-version: [1.16.8, 1.17.1]
# We use macos-11 over macos-latest because macos-latest defaults to Catalina(10.15) and not Big Sur(11.0)
# We can switch to macos-latest whenever Big Sur becomes the default
# See https://github.com/actions/virtual-environments#available-environments
platform: [ubuntu-latest, macos-11, windows-latest]
runs-on: ${{ matrix.platform }}

steps:
- name: Install Go
uses: actions/setup-go@v2
Expand Down
32 changes: 21 additions & 11 deletions .github/workflows/v8build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ on: workflow_dispatch

jobs:
build:
name: Build V8 for ${{ matrix.platform }}
name: Build V8 for ${{ matrix.platform }} ${{ matrix.arch }}
strategy:
fail-fast: false
matrix:
platform: [ubuntu-18.04, macos-latest, windows-latest]
# We use macos-11 over macos-latest because macos-latest defaults to Catalina(10.15) and not Big Sur(11.0)
# We can switch to macos-latest whenever Big Sur becomes the default
# See https://github.com/actions/virtual-environments#available-environments
#
# We need xcode 12.4 or newer to cross compile between arm64/amd64
# https://github.com/actions/virtual-environments/blob/main/images/macos/macos-11-Readme.md#xcode
platform: [ubuntu-18.04, macos-11, windows-latest]
arch: [x86_64, arm64]
exclude:
- platform: ubuntu-18.04
arch: arm64
- platform: windows-latest
arch: arm64
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout
Expand All @@ -21,27 +33,25 @@ jobs:
shell: bash
- name: Build V8 linux
if: matrix.platform == 'ubuntu-18.04'
run: cd deps && ./build.py --no-clang
run: cd deps && ./build.py --no-clang --arch ${{ matrix.arch }}
- name: Build V8 macOS
if: matrix.platform == 'macos-latest'
run: cd deps && ./build.py
if: matrix.platform == 'macos-11'
run: cd deps && ./build.py --arch ${{ matrix.arch }}
- name: Add MSYS2 to PATH
if: matrix.platform == 'windows-latest'
run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
shell: bash
- name: Build V8 windows
if: matrix.platform == 'windows-latest'
run: cd deps; python build.py --no-clang
run: cd deps; python build.py --no-clang --arch ${{ matrix.arch }}
env:
MSYSTEM: MINGW64
DEPOT_TOOLS_WIN_TOOLCHAIN: 0
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Update V8 static library for ${{ matrix.platform }}
branch: v8-lib
commit-message: Update V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
branch-suffix: random
delete-branch: true
title: V8 static library for ${{ matrix.platform }}
body: Auto-generated pull request to build V8 for ${{ matrix.platform }}

title: V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
body: Auto-generated pull request to build V8 for ${{ matrix.platform }} ${{ matrix.arch }}
2 changes: 0 additions & 2 deletions .github/workflows/v8upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ jobs:
delete-branch: true
title: ${{steps.pr_metadata.outputs.pr_commit_message}}
body: ${{steps.pr_metadata.outputs.pr_body}}


1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for calling `IsExecutionTerminating` on isolate to check if execution is still terminating.
- Support for setting and getting internal fields for template object instances
- Support for CPU profiling
- Add V8 build for Apple Silicon

### Changed
- Removed error return value from NewIsolate which never fails
Expand Down
4 changes: 3 additions & 1 deletion cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ package v8go

// #cgo CXXFLAGS: -fno-rtti -fpic -std=c++14 -DV8_COMPRESS_POINTERS -DV8_31BIT_SMIS_ON_64BIT_ARCH -I${SRCDIR}/deps/include
// #cgo LDFLAGS: -pthread -lv8
// #cgo darwin LDFLAGS: -L${SRCDIR}/deps/darwin_x86_64
// #cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/deps/darwin_x86_64
// #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/deps/darwin_arm64
// #cgo linux LDFLAGS: -L${SRCDIR}/deps/linux_x86_64
// #cgo windows LDFLAGS: -L${SRCDIR}/deps/windows_x86_64 -static -ldbghelp -lssp -lwinmm -lz
import "C"
Expand All @@ -17,6 +18,7 @@ import "C"
// contain V8 libraries and headers which otherwise would be ignored.
// DO NOT REMOVE
import (
_ "rogchap.com/v8go/deps/darwin_arm64"
_ "rogchap.com/v8go/deps/darwin_x86_64"
_ "rogchap.com/v8go/deps/include"
_ "rogchap.com/v8go/deps/linux_x86_64"
Expand Down
29 changes: 24 additions & 5 deletions deps/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
import shutil
import argparse

valid_archs = ['arm64', 'x86_64']
# "x86_64" is called "amd64" on Windows
current_arch = platform.uname()[4].lower().replace("amd64", "x86_64")
default_arch = current_arch if current_arch in valid_archs else None

parser = argparse.ArgumentParser()
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--no-clang', dest='clang', action='store_false')
parser.add_argument('--arch',
dest='arch',
action='store',
choices=valid_archs,
default=default_arch,
required=default_arch is None)
parser.set_defaults(debug=False, clang=True)
args = parser.parse_args()

Expand Down Expand Up @@ -40,6 +51,8 @@
gn_args = """
is_debug=%s
is_clang=%s
target_cpu="%s"
v8_target_cpu="%s"
clang_use_chrome_plugins=false
use_custom_libcxx=false
use_sysroot=false
Expand Down Expand Up @@ -70,8 +83,12 @@ def cmd(args):

def os_arch():
u = platform.uname()
# "x86_64" is called "amd64" on Windows
return (u[0] + "_" + u[4]).lower().replace("amd64", "x86_64")
return u[0].lower() + "_" + args.arch

def v8_arch():
if args.arch == "x86_64":
return "x64"
return args.arch

def apply_mingw_patches():
v8_build_path = os.path.join(v8_path, "build")
Expand All @@ -96,7 +113,7 @@ def main():
v8deps()
if is_windows:
apply_mingw_patches()

gn_path = os.path.join(tools_path, "gn")
assert(os.path.exists(gn_path))
ninja_path = os.path.join(tools_path, "ninja" + (".exe" if is_windows else ""))
Expand All @@ -112,9 +129,11 @@ def main():
# compiled library by an order of magnitude and further slow down compilation
symbol_level = 1 if args.debug else 0
strip_debug_info = 'false' if args.debug else 'true'
gnargs = gn_args % (is_debug, is_clang, symbol_level, strip_debug_info)

arch = v8_arch()
gnargs = gn_args % (is_debug, is_clang, arch, arch, symbol_level, strip_debug_info)
gen_args = gnargs.replace('\n', ' ')

subprocess.check_call(cmd([gn_path, "gen", build_path, "--args=" + gen_args]),
cwd=v8_path,
env=env)
Expand Down
Binary file added deps/darwin_arm64/libv8.a
Binary file not shown.
3 changes: 3 additions & 0 deletions deps/darwin_arm64/vendor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package darwin_arm64 is required to provide support for vendoring modules
// DO NOT REMOVE
package darwin_arm64
2 changes: 1 addition & 1 deletion deps/depot_tools
Submodule depot_tools updated from 1dbd65 to c54d00
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module rogchap.com/v8go

go 1.12
go 1.16

0 comments on commit ea7debf

Please sign in to comment.