Skip to content

Commit

Permalink
Build for static V8 library on Windows (#102)
Browse files Browse the repository at this point in the history
* build static V8 library on Windows

* Update V8 static library for windows-latest (#6)

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

* Update V8 static library for ubuntu-latest (#5)

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

* add Windows to test matrix

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
cleiner and github-actions[bot] committed Apr 9, 2021
1 parent 25d7afa commit 6941971
Show file tree
Hide file tree
Showing 16 changed files with 2,276 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
go-version: [1.12.17, 1.15.7]
platform: [ubuntu-latest, macos-latest]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}

steps:
Expand All @@ -23,6 +23,10 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout
uses: actions/checkout@v2
- name: Add MINGW to PATH
if: matrix.platform == 'windows-latest'
run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
shell: bash
- name: Go Test
run: go test -v -coverprofile c.out ./...
- name: Upload coverage to Codecov
Expand Down
15 changes: 14 additions & 1 deletion .github/workflows/v8build.yml
Expand Up @@ -7,20 +7,33 @@ jobs:
name: Build V8 for ${{ matrix.platform }}
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: true
fetch-depth: 1
- name: Update depot_tools fetch config
run: cd deps/depot_tools && git config --unset-all remote.origin.fetch; git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
shell: bash
- name: Build V8 linux
if: matrix.platform == 'ubuntu-latest'
run: cd deps && ./build.py --no-clang
- name: Build V8 macOS
if: matrix.platform == 'macos-latest'
run: cd deps && ./build.py
- 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
env:
MSYSTEM: MINGW64
DEPOT_TOOLS_WIN_TOOLCHAIN: 0
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
Expand Down
16 changes: 6 additions & 10 deletions README.md
Expand Up @@ -122,25 +122,21 @@ please join the [**#v8go**](https://gophers.slack.com/channels/v8go) channel on

### Windows

While no prebuilt static V8 library is included for Windows, MSYS2 provides a package containing
a dynamically linked V8 library that works.
In order to build a project using v8go on Windows, Go requires a gcc compiler to be installed.

To set this up:
1. Install MSYS2 (https://www.msys2.org/)
2. Add the Mingw-w64 bin to your PATH environment variable (`C:\msys64\mingw64\bin` by default)
3. Open MSYS2 MSYS and execute `pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-v8`
4. This will allow building projects that depend on `v8go`, but, in order to actually run them,
you will need to copy the `snapshot_blob.bin` file from the Mingw-w64 bin folder to your program's
working directory (which is typically wherever `main.go` is)
3. Open MSYS2 MSYS and execute `pacman -S mingw-w64-x86_64-toolchain`

V8 requires 64-bit on Windows, therefore will not work on 32-bit systems.
V8 requires 64-bit on Windows, therefore it will not work on 32-bit systems.

## V8 dependency

V8 version: **8.9.255.20** (March 2021)

In order to make `v8go` usable as a standard Go package, prebuilt static libraries of V8
are included for Linux and macOS ie. you *should not* require to build V8 yourself.
are included for Linux, macOS and Windows ie. you *should not* require to build V8 yourself.

Due to security concerns of binary blobs hiding malicious code, the V8 binary is built via CI *ONLY*.

Expand Down Expand Up @@ -182,8 +178,8 @@ Make sure *NOT* to add your build of the binary, as this will be build via CI.
1) Because the build is so long, this is not automatically triggered. Go to the [V8
Build](https://github.com/rogchap/v8go/actions?query=workflow%3A%22V8+Build%22) Github Action, Select "Run workflow",
and select your pushed branch eg. `v8_7_upgrade`.
1) Once built, this should open 2 PRs against your branch to add the `libv8.a` for both macOS and linux; merge these PRs
into your branch. You are now ready to raise the PR against `master` with the latest version of V8.
1) Once built, this should open 3 PRs against your branch to add the `libv8.a` for Linux, macOS and Windows; merge
these PRs into your branch. You are now ready to raise the PR against `master` with the latest version of V8.

### Formatting

Expand Down
6 changes: 3 additions & 3 deletions cgo.go
Expand Up @@ -6,12 +6,11 @@ package v8go

//go:generate clang-format -i --verbose -style=Chromium v8go.h v8go.cc

// #cgo CXXFLAGS: -fno-rtti -fpic -std=c++14 -DV8_COMPRESS_POINTERS -DV8_31BIT_SMIS_ON_64BIT_ARCH
// #cgo darwin linux CXXFLAGS: -I${SRCDIR}/deps/include
// #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 windows LDFLAGS: -lv8_libplatform
// #cgo darwin LDFLAGS: -L${SRCDIR}/deps/darwin_x86_64
// #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"

// These imports forces `go mod vendor` to pull in all the folders that
Expand All @@ -23,4 +22,5 @@ import (
_ "rogchap.com/v8go/deps/include/cppgc"
_ "rogchap.com/v8go/deps/include/libplatform"
_ "rogchap.com/v8go/deps/linux_x86_64"
_ "rogchap.com/v8go/deps/windows_x86_64"
)
37 changes: 33 additions & 4 deletions deps/build.py
Expand Up @@ -14,6 +14,7 @@
deps_path = os.path.dirname(os.path.realpath(__file__))
v8_path = os.path.join(deps_path, "v8")
tools_path = os.path.join(deps_path, "depot_tools")
is_windows = platform.system().lower() == "windows"

gclient_sln = [
{ "name" : "v8",
Expand Down Expand Up @@ -53,25 +54,53 @@
v8_enable_i18n_support=false
v8_enable_test_features=false
v8_untrusted_code_mitigations=false
exclude_unwind_tables=true
"""

def v8deps():
spec = "solutions = %s" % gclient_sln
env = os.environ.copy()
env["PATH"] = tools_path + os.pathsep + env["PATH"]
subprocess.check_call(["gclient", "sync", "--spec", spec],
subprocess.check_call(cmd(["gclient", "sync", "--spec", spec]),
cwd=deps_path,
env=env)

def cmd(args):
return ["cmd", "/c"] + args if is_windows else args

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

def apply_mingw_patches():
v8_build_path = os.path.join(v8_path, "build")
apply_patch("0000-add-mingw-main-code-changes", v8_path)
apply_patch("0001-add-mingw-toolchain", v8_build_path)
apply_patch("0002-fix-mingw-unwind-tables", v8_build_path)
update_last_change()
zlib_path = os.path.join(v8_path, "third_party", "zlib")
zlib_src_gn = os.path.join(deps_path, os_arch(), "zlib.gn")
zlib_dst_gn = os.path.join(zlib_path, "BUILD.gn")
shutil.copy(zlib_src_gn, zlib_dst_gn)

def apply_patch(patch_name, working_dir):
patch_path = os.path.join(deps_path, os_arch(), patch_name + ".patch")
subprocess.check_call(["git", "apply", "-v", patch_path], cwd=working_dir)

def update_last_change():
import v8.build.util.lastchange as lastchange
out_path = os.path.join(v8_path, "build", "util", "LASTCHANGE")
lastchange.main(["lastchange", "-o", out_path])

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")
ninja_path = os.path.join(tools_path, "ninja" + (".exe" if is_windows else ""))
assert(os.path.exists(ninja_path))

build_path = os.path.join(deps_path, ".build", os_arch())
Expand All @@ -82,7 +111,7 @@ def main():
gnargs = gn_args % (is_debug, is_clang)
gen_args = gnargs.replace('\n', ' ')

subprocess.check_call([gn_path, "gen", build_path, "--args=" + gen_args],
subprocess.check_call(cmd([gn_path, "gen", build_path, "--args=" + gen_args]),
cwd=v8_path,
env=env)
subprocess.check_call([ninja_path, "-v", "-C", build_path, "v8_monolith"],
Expand Down
2 changes: 1 addition & 1 deletion deps/depot_tools
Submodule depot_tools updated from d071f2 to da7687
Binary file modified deps/linux_x86_64/libv8.a
Binary file not shown.
2 changes: 2 additions & 0 deletions deps/windows_x86_64/.gitattributes
@@ -0,0 +1,2 @@
*.patch text eol=lf
*.gn text eol=lf

0 comments on commit 6941971

Please sign in to comment.