Skip to content

Commit

Permalink
resolves rogchap#170 build v8go for musl libc
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogztter authored and ggrossetie committed Nov 28, 2022
1 parent 1f00b50 commit f60e94a
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 151 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/gnalpinebuild.yml
@@ -0,0 +1,35 @@
name: GN Alpine Build

on:
workflow_dispatch:

jobs:
build-alpine:
name: Build GN for Alpine x86_64
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build container images
run: docker buildx build --platform=linux/amd64 --output=type=docker -t gn-builder-alpine -f deps/alpine-gn-builder.Dockerfile deps
- name: Copy files
run: |
id=$(docker create gn-builder-alpine)
docker cp $id:/usr/local/bin/gn deps/alpine_x86_64/gn
docker rm -v $id
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Update GN static library for Alpine x86_64
branch-suffix: random
delete-branch: true
title: GN static library for Alpine x86_64
body: Auto-generated pull request to build GN for Alpine x86_64
base: master
87 changes: 60 additions & 27 deletions .github/workflows/v8build.yml
Expand Up @@ -14,32 +14,65 @@ jobs:
#
# 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]
arch: [x86_64, arm64]
platform: [ ubuntu-18.04, macos-11 ]
arch: [ x86_64, arm64 ]
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: Install g++-aarch64-linux-gnu
if: matrix.platform == 'ubuntu-18.04' && matrix.arch == 'arm64'
run: sudo apt update && sudo apt install g++-aarch64-linux-gnu -y
- name: Build V8 linux
if: matrix.platform == 'ubuntu-18.04'
run: cd deps && ./build.py --no-clang --arch ${{ matrix.arch }}
- name: Build V8 macOS
if: matrix.platform == 'macos-11'
run: cd deps && ./build.py --arch ${{ matrix.arch }}
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Update V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
branch-suffix: random
delete-branch: true
title: V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
body: Auto-generated pull request to build V8 for ${{ matrix.platform }} ${{ matrix.arch }}
- 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: Install g++-aarch64-linux-gnu
if: matrix.platform == 'ubuntu-18.04' && matrix.arch == 'arm64'
run: sudo apt update && sudo apt install g++-aarch64-linux-gnu -y
- name: Build V8 linux
if: matrix.platform == 'ubuntu-18.04'
run: cd deps && ./build.py --no-clang --arch ${{ matrix.arch }}
- name: Build V8 macOS
if: matrix.platform == 'macos-11'
run: cd deps && ./build.py --arch ${{ matrix.arch }}
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Update V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
branch-suffix: random
delete-branch: true
title: V8 static library for ${{ matrix.platform }} ${{ matrix.arch }}
body: Auto-generated pull request to build V8 for ${{ matrix.platform }} ${{ matrix.arch }}
build-alpine:
name: Build V8 for Alpine x86_64
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
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: Get V8 deps linux
run: cd deps && ./get_v8deps.py
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build container images
run: docker buildx build --platform=linux/amd64 --output=type=docker -t v8-builder-alpine -f deps/alpine-v8-builder.Dockerfile deps
- name: Copy files
run: |
id=$(docker create v8-builder-alpine)
docker cp $id:alpine_x86_64/. deps/alpine_x86_64/
docker rm -v $id
- name: Create PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: Update V8 static library for Alpine x86_64
branch-suffix: random
delete-branch: true
title: V8 static library for Alpine x86_64
body: Auto-generated pull request to build V8 for Alpine x86_64
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,3 +10,4 @@ deps/darwin-x86_64/libv8_debug.a
c.out

/v8go.test
__pycache__
2 changes: 2 additions & 0 deletions cgo.go
Expand Up @@ -12,12 +12,14 @@ package v8go
// #cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/deps/darwin_arm64
// #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/deps/linux_x86_64 -ldl
// #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/deps/linux_arm64 -ldl
// #cgo linux,amd64,musl-gcc LDFLAGS: -L${SRCDIR}/deps/alpine_x86_64 -ldl
import "C"

// These imports forces `go mod vendor` to pull in all the folders that
// contain V8 libraries and headers which otherwise would be ignored.
// DO NOT REMOVE
import (
_ "rogchap.com/v8go/deps/alpine_x86_64"
_ "rogchap.com/v8go/deps/darwin_arm64"
_ "rogchap.com/v8go/deps/darwin_x86_64"
_ "rogchap.com/v8go/deps/include"
Expand Down
31 changes: 31 additions & 0 deletions deps/alpine-gn-builder.Dockerfile
@@ -0,0 +1,31 @@
#
# Build GN for alpine (this is a build dependency)
#
FROM alpine:3.16.3 as gn-builder

# This is the GN commit that we want to build.
# Most commits will probably build just fine but this happened to be the latest commit when I did this.
ARG GN_COMMIT=1c4151ff5c1d6fbf7fa800b8d4bb34d3abc03a41

RUN \
apk add --update --virtual .gn-build-dependencies \
alpine-sdk \
binutils-gold \
clang \
curl \
git \
llvm12 \
ninja \
python3 \
tar \
xz \
# Quick fixes: we need the LLVM tooling in $PATH, and we also have to use gold instead of ld.
&& PATH=$PATH:/usr/lib/llvm12/bin \
&& cp -f /usr/bin/ld.gold /usr/bin/ld \
# Clone and build gn
&& git clone https://gn.googlesource.com/gn /tmp/gn \
&& git -C /tmp/gn checkout ${GN_COMMIT} \
&& cd /tmp/gn \
&& python3 build/gen.py \
&& ninja -C out \
&& cp -f /tmp/gn/out/gn /usr/local/bin/gn
30 changes: 30 additions & 0 deletions deps/alpine-v8-builder.Dockerfile
@@ -0,0 +1,30 @@
#
# Building V8 for alpine
#
FROM alpine:3.16.3 as v8

COPY depot_tools ./depot_tools
COPY include ./include
COPY v8 ./v8
COPY .gclient compile_v8.py ./
COPY alpine_x86_64 ./alpine_x86_64

RUN \
apk add --update --virtual .v8-build-dependencies \
bash \
curl \
g++ \
gcc \
glib-dev \
icu-dev \
libstdc++ \
linux-headers \
make \
ninja \
python3 \
tar \
xz \
&& cp alpine_x86_64/gn depot_tools/gn \
&& ln -s /usr/bin/python3 /usr/bin/python \
# Compile V8
&& ./compile_v8.py --no-clang --arch x86_64
Binary file added deps/alpine_x86_64/gn
Binary file not shown.
Binary file added deps/alpine_x86_64/libv8.a
Binary file not shown.
3 changes: 3 additions & 0 deletions deps/alpine_x86_64/vendor.go
@@ -0,0 +1,3 @@
// Package alpine_x86_64 is required to provide support for vendoring modules
// DO NOT REMOVE
package alpine_x86_64
126 changes: 3 additions & 123 deletions deps/build.py
Expand Up @@ -4,6 +4,8 @@
import subprocess
import shutil
import argparse
from get_v8deps import v8deps
from compile_v8 import v8compile

valid_archs = ['arm64', 'x86_64']
# "x86_64" is called "amd64" on Windows
Expand All @@ -22,131 +24,9 @@
parser.set_defaults(debug=False, clang=True)
args = parser.parse_args()

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",
"url" : "https://chromium.googlesource.com/v8/v8.git",
"deps_file" : "DEPS",
"managed" : False,
"custom_deps" : {
# These deps are unnecessary for building.
"v8/testing/gmock" : None,
"v8/test/wasm-js" : None,
"v8/third_party/android_tools" : None,
"v8/third_party/catapult" : None,
"v8/third_party/colorama/src" : None,
"v8/tools/gyp" : None,
"v8/tools/luci-go" : None,
},
"custom_vars": {
"build_for_node" : True,
},
},
]

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
symbol_level=%s
strip_debug_info=%s
is_component_build=false
v8_monolithic=true
v8_use_external_startup_data=false
treat_warnings_as_errors=false
v8_embedder_string="-v8go"
v8_enable_gdbjit=false
v8_enable_i18n_support=true
icu_use_data_file=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(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].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")
apply_patch("0000-add-mingw-main-code-changes", v8_path)
apply_patch("0001-add-mingw-toolchain", 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():
out_path = os.path.join(v8_path, "build", "util", "LASTCHANGE")
subprocess.check_call(["python", "build/util/lastchange.py", "-o", out_path], cwd=v8_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" + (".exe" if is_windows else ""))
assert(os.path.exists(ninja_path))

build_path = os.path.join(deps_path, ".build", os_arch())
env = os.environ.copy()

is_debug = 'true' if args.debug else 'false'
is_clang = 'true' if args.clang else 'false'
# symbol_level = 1 includes line number information
# symbol_level = 2 can be used for additional debug information, but it can increase the
# 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'

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)
subprocess.check_call([ninja_path, "-v", "-C", build_path, "v8_monolith"],
cwd=v8_path,
env=env)

lib_fn = os.path.join(build_path, "obj/libv8_monolith.a")
dest_path = os.path.join(deps_path, os_arch())
if not os.path.exists(dest_path):
os.makedirs(dest_path)
dest_fn = os.path.join(dest_path, 'libv8.a')
shutil.copy(lib_fn, dest_fn)
v8compile(args.debug, args.clang, args.arch)


if __name__ == "__main__":
Expand Down

0 comments on commit f60e94a

Please sign in to comment.