rogchap / v8go

Execute JavaScript from Go
https://rogchap.com/v8go
BSD 3-Clause "New" or "Revised" License
3.21k stars 219 forks source link

Build v8go for musl libc (e.g. for alpine linux) #170

Open krhubert opened 3 years ago

krhubert commented 3 years ago

Hi!

Is it possible to include alpine os to build pipeline?

dylanahsmith commented 3 years ago

Do you mean Alpine Linux? There is already a linux build of v8go. Are you having any issues with using the pre-built v8 library for linux that is bundled with v8go?

krhubert commented 3 years ago

Do you mean Alpine Linux? Yes, I've updated PR title.

There is already a linux build of v8go. Are you having any issues with using the pre-built v8 library for linux that is bundled with v8go?

Yes, I do. It seems that I have to rebuild v8 for Alpine linux (probably because its different std c lib). Below you can see a go code (simple app copied from your README page) and dockerfile. I've also attached the build log.

What I found is an old thread how to build v8 on alpine. It would be great to have a precompiled version for alpine linux like there are for Linux, macOS and Windows.

 > [6/6] RUN go build:
#8 7.953 # rogchap.com/v8go
#8 7.953 /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: /go/pkg/mod/rogchap.com/v8go@v0.6.0/deps/linux_x86_64/libv8.a(preparser.o): in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > __gnu_cxx::__to_xstring<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char>(int (*)(char*, unsigned long, char const*, __va_list_tag*), unsigned long, char const*, ...) [clone .constprop.347]':
....
#8 7.953 source-location.cc:(.text._ZN9__gnu_cxx12__to_xstringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEET_PFiPT0_mPKS8_P13__va_list_tagEmSB_z.constprop.26+0xb8): undefined reference to `__vsnprintf_chk'
#8 7.953 /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: /go/pkg/mod/rogchap.com/v8go@v0.6.0/deps/linux_x86_64/libv8.a(crc_folding.o): in function `Cr_z_crc_fold_copy':
#8 7.953 crc_folding.c:(.text.Cr_z_crc_fold_copy+0x78): undefined reference to `__memcpy_chk'
#8 7.953 collect2: error: ld returned 1 exit status
------
executor failed running [/bin/sh -c go build]: exit code: 2
FROM golang:1.17-alpine

WORKDIR /app

COPY go.mod go.sum main.go /app
RUN go mod download

RUN apk add build-base
RUN go build
package main

import (
    _ "embed"
    "fmt"

    "rogchap.com/v8go"
)

func main() {
    ctx, _ := v8go.NewContext()                             // creates a new V8 context with a new Isolate aka VM
    ctx.RunScript("const add = (a, b) => a + b", "math.js") // executes a script on the global context
    ctx.RunScript("const result = add(3, 4)", "main.js")    // any functions previously added to the context can be called
    val, _ := ctx.RunScript("result", "value.js")           // return a value in JavaScript back to Go
    fmt.Printf("addition result: %s", val)
}
dylanahsmith commented 3 years ago

I see, it isn't actually creating a fully static executable because of dependencies on the system's libc. If v8 were statically compiled against musl, then perhaps we could completely get rid of the external libc dependency and make the pre-compiled code portable to any linux distribution.

dylanahsmith commented 3 years ago

Note that the Go build constraints don't have builtin build constraint support for distinguishing between linux distributions or libc implementations. So even if we wanted a separate pre-compiled v8 build for alpine linux, it doesn't seem like it could just be automatically be used. However, it does seem like custom build tags could be specified using the go build -tags command line argument, which could be used to customize the build, which could be an alternative if we aren't able to get a universal static build of v8 for linux.

Vaelatern commented 1 year ago

This also breaks 32bit arm and 32bit x86, and musl libc.

Is there no better way to build this on demand?

wuzyk commented 1 year ago

@dylanahsmith @krhubert Hi, guys! Is there any update on the issue? Or Is there any concerns to merge the PR?