...

Source file src/cuelang.org/go/cue/interpreter/wasm/testdata/gen.go

Documentation: cuelang.org/go/cue/interpreter/wasm/testdata

     1  // Copyright 2023 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"os"
    19  	"os/exec"
    20  	"path/filepath"
    21  )
    22  
    23  //go:generate go run gen.go
    24  
    25  func main() {
    26  	// We do not want to burden our users, not even our developers
    27  	// with a Rust dependency, so only build the Rust Wasm binaries
    28  	// if this variable is set.
    29  	if _, ok := os.LookupEnv("CUE_WASM_BUILD_RUST"); !ok {
    30  		return
    31  	}
    32  
    33  	cwd, _ := os.Getwd()
    34  	src := filepath.Join(cwd, "rust")
    35  	buildRust(src)
    36  
    37  	target := filepath.Join(src, "target")
    38  	bins := filepath.Join(target, "wasm32-unknown-unknown", "release")
    39  	cue := filepath.Join(cwd, "cue")
    40  	copyWasm(bins, cue)
    41  
    42  	os.RemoveAll(target)
    43  }
    44  
    45  func buildRust(srcDir string) {
    46  	cmd := exec.Command(
    47  		"cargo", "build", "--release", "--target", "wasm32-unknown-unknown",
    48  	)
    49  	cmd.Dir = srcDir
    50  	cmd.Run()
    51  }
    52  
    53  func copyWasm(binDir, wasmDir string) {
    54  	files, _ := filepath.Glob(filepath.Join(binDir, "*.wasm"))
    55  	for _, f := range files {
    56  		os.Rename(f, filepath.Join(wasmDir, filepath.Base(f)))
    57  	}
    58  }
    59  

View as plain text