Skip to content

Commit

Permalink
Merge pull request #180 from Hywan/feat-cross-compilation
Browse files Browse the repository at this point in the history
feat(cross-compilation) Allow cross-compilation!
  • Loading branch information
Hywan authored Feb 5, 2021
2 parents 9f9f1fb + 15998ca commit e8bffc8
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
12 changes: 8 additions & 4 deletions wasmer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (self *Config) inner() *C.wasm_config_t {
// config.UseJITEngine()
//
func (self *Config) UseJITEngine() {
C.wasm_config_set_engine(self._inner, C.JIT)
C.wasm_config_set_engine(self.inner(), C.JIT)
}

// UseNativeEngine sets the engine to Native in the configuration.
Expand All @@ -38,7 +38,7 @@ func (self *Config) UseJITEngine() {
// config.UseNativeEngine()
//
func (self *Config) UseNativeEngine() {
C.wasm_config_set_engine(self._inner, C.NATIVE)
C.wasm_config_set_engine(self.inner(), C.NATIVE)
}

// UseCraneliftCompiler sets the compiler to Cranelift in the configuration.
Expand All @@ -47,7 +47,7 @@ func (self *Config) UseNativeEngine() {
// config.UseCraneliftCompiler()
//
func (self *Config) UseCraneliftCompiler() {
C.wasm_config_set_engine(self._inner, C.CRANELIFT)
C.wasm_config_set_engine(self.inner(), C.CRANELIFT)
}

// UseLLVMCompiler sets the compiler to LLVM in the configuration.
Expand All @@ -56,5 +56,9 @@ func (self *Config) UseCraneliftCompiler() {
// config.UseLLVMCompiler()
//
func (self *Config) UseLLVMCompiler() {
C.wasm_config_set_engine(self._inner, C.LLVM)
C.wasm_config_set_engine(self.inner(), C.LLVM)
}

func (self *Config) UseTarget(target *Target) {
C.wasm_config_set_target(self.inner(), target.inner())
}
21 changes: 21 additions & 0 deletions wasmer/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ func TestNativeEngine(t *testing.T) {

testEngine(t, NewNativeEngine())
}

func TestEngineWithTarget(t *testing.T) {
triple, err := NewTriple("aarch64-unknown-linux-gnu")
assert.NoError(t, err)

cpuFeatures := NewCpuFeatures()
assert.NoError(t, err)

target := NewTarget(triple, cpuFeatures)

config := NewConfig()
config.UseTarget(target)

engine := NewEngineWithConfig(config)
store := NewStore(engine)

module, err := NewModule(store, testGetBytes("tests.wasm"))
assert.NoError(t, err)

_ = module
}
101 changes: 101 additions & 0 deletions wasmer/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package wasmer

// #include <wasmer_wasm.h>
import "C"
import "runtime"

type Target struct {
_inner *C.wasm_target_t
}

func newTarget(target *C.wasm_target_t) *Target {
self := &Target{
_inner: target,
}

runtime.SetFinalizer(self, func(self *Target) {
C.wasm_target_delete(self.inner())
})

return self
}

func NewTarget(triple *Triple, cpuFeatures *CpuFeatures) *Target {
return newTarget(C.wasm_target_new(triple.inner(), cpuFeatures.inner()))
}

func (self *Target) inner() *C.wasm_target_t {
return self._inner
}

type Triple struct {
_inner *C.wasm_triple_t
}

func newTriple(triple *C.wasm_triple_t) *Triple {
self := &Triple{
_inner: triple,
}

runtime.SetFinalizer(self, func(self *Triple) {
C.wasm_triple_delete(self.inner())
})

return self
}

func NewTriple(triple string) (*Triple, error) {
cTripleName := newName(triple)
defer C.wasm_name_delete(&cTripleName)

cTriple := C.wasm_triple_new(&cTripleName)

if cTriple == nil {
return nil, newErrorFromWasmer()
}

return newTriple(cTriple), nil
}

func NewTripleFromHost() *Triple {
return newTriple(C.wasm_triple_new_from_host())
}

func (self *Triple) inner() *C.wasm_triple_t {
return self._inner
}

type CpuFeatures struct {
_inner *C.wasm_cpu_features_t
}

func newCpuFeatures(cpu_features *C.wasm_cpu_features_t) *CpuFeatures {
self := &CpuFeatures{
_inner: cpu_features,
}

runtime.SetFinalizer(self, func(self *CpuFeatures) {
C.wasm_cpu_features_delete(self.inner())
})

return self
}

func NewCpuFeatures() *CpuFeatures {
return newCpuFeatures(C.wasm_cpu_features_new())
}

func (self *CpuFeatures) Add(feature string) error {
cFeature := newName(feature)
defer C.wasm_name_delete(&cFeature)

if !C.wasm_cpu_features_add(self.inner(), &cFeature) {
return newErrorFromWasmer()
}

return nil
}

func (self *CpuFeatures) inner() *C.wasm_cpu_features_t {
return self._inner
}
18 changes: 18 additions & 0 deletions wasmer/target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package wasmer

import (
"github.com/stretchr/testify/assert"
"testing"
)

func testTarget(t *testing.T) {
triple, err := NewTriple("x86_64-apple-darwin")
assert.NoError(t, err)

cpuFeatures := NewCpuFeatures()
cpuFeatures.Add("sse2")

target := NewTarget(triple, cpuFeatures)

_ = target
}

0 comments on commit e8bffc8

Please sign in to comment.