mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
build/wasm: use golang1.16 go:embed mechanism (#5123)
With the embed directive, we no longer need our custom code that predates Go 1.16. Also, with the release of 1.19, we no longer desire compatibility with anything predating 1.16, so this cleanup becomes possible. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
EXCEPTIONS=(
|
||||
"internal/compiler/wasm/opa/opa.go"
|
||||
"internal/compiler/wasm/opa/opa.wasm"
|
||||
"internal/compiler/wasm/opa/callgraph.csv"
|
||||
)
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
// Copyright 2018 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type params struct {
|
||||
Output string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var params params
|
||||
executable := path.Base(os.Args[0])
|
||||
|
||||
command := &cobra.Command{
|
||||
Use: executable,
|
||||
Short: executable + " <opa.wasm path>",
|
||||
RunE: func(_ *cobra.Command, args []string) error {
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("provide path of opa.wasm and callgraph.csv files")
|
||||
}
|
||||
return run(params, args)
|
||||
},
|
||||
}
|
||||
|
||||
command.Flags().StringVarP(¶ms.Output, "output", "o", "", "set path of output file (default: stdout)")
|
||||
|
||||
if err := command.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(params params, args []string) error {
|
||||
|
||||
var out io.Writer
|
||||
|
||||
if params.Output != "" {
|
||||
f, err := os.Create(params.Output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
out = f
|
||||
} else {
|
||||
out = os.Stdout
|
||||
}
|
||||
|
||||
_, err := out.Write([]byte(`// Copyright 2018 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// THIS FILE IS GENERATED. DO NOT EDIT.
|
||||
|
||||
// Package opa contains bytecode for the OPA-WASM library.
|
||||
package opa
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
bytesOnce sync.Once
|
||||
bs []byte
|
||||
callGraphCSVOnce sync.Once
|
||||
callGraphCSV []byte
|
||||
)
|
||||
|
||||
// Bytes returns the OPA-WASM bytecode.
|
||||
func Bytes() ([]byte, error) {
|
||||
var err error
|
||||
bytesOnce.Do(func() {
|
||||
gr, err := gzip.NewReader(bytes.NewBuffer(gzipped))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bs, err = ioutil.ReadAll(gr)
|
||||
})
|
||||
return bs, err
|
||||
}
|
||||
|
||||
// CallGraphCSV returns a CSV representation of the
|
||||
// OPA-WASM bytecode's call graph: 'caller,callee'
|
||||
func CallGraphCSV() ([]byte, error) {
|
||||
var err error
|
||||
callGraphCSVOnce.Do(func() {
|
||||
cg, err := gzip.NewReader(bytes.NewBuffer(gzippedCallGraphCSV))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
callGraphCSV, err = ioutil.ReadAll(cg)
|
||||
})
|
||||
return callGraphCSV, err
|
||||
}
|
||||
`))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = out.Write([]byte(`var gzipped = []byte("`))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := output(out, args[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := out.Write([]byte(`")
|
||||
`)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = out.Write([]byte(`var gzippedCallGraphCSV = []byte("`))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := output(out, args[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := out.Write([]byte(`")
|
||||
`)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func output(out io.Writer, filename string) error {
|
||||
in, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer in.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
gw := gzip.NewWriter(&buf)
|
||||
_, err = io.Copy(gw, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gw.Close()
|
||||
|
||||
for _, b := range buf.Bytes() {
|
||||
if _, err := out.Write([]byte(`\x`)); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := out.Write(asciihex(b)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var digits = "0123456789ABCDEF"
|
||||
|
||||
func asciihex(b byte) []byte {
|
||||
lo := digits[(b & 0x0F)]
|
||||
hi := digits[(b >> 4)]
|
||||
return []byte{hi, lo}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -113,10 +113,7 @@ func unquote(s string) (string, error) {
|
||||
}
|
||||
|
||||
func (c *Compiler) removeUnusedCode() error {
|
||||
cgCSV, err := opa.CallGraphCSV()
|
||||
if err != nil {
|
||||
return fmt.Errorf("csv unpack: %w", err)
|
||||
}
|
||||
cgCSV := opa.CallGraphCSV()
|
||||
r := csv.NewReader(bytes.NewReader(cgCSV))
|
||||
r.LazyQuotes = true
|
||||
cg, err := r.ReadAll()
|
||||
|
||||
@@ -322,11 +322,8 @@ func (c *Compiler) Compile() (*module.Module, error) {
|
||||
// are about to be compiled.
|
||||
func (c *Compiler) initModule() error {
|
||||
|
||||
bs, err := opa.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bs := opa.Bytes()
|
||||
var err error
|
||||
c.module, err = encoding.ReadModule(bytes.NewReader(bs))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -63,11 +63,7 @@ func TestRoundtrip(t *testing.T) {
|
||||
|
||||
func TestRoundtripOPA(t *testing.T) {
|
||||
|
||||
bs, err := opa.Bytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bs := opa.Bytes()
|
||||
module1, err := ReadModule(bytes.NewBuffer(bs))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -21,6 +21,3 @@ func main() {
|
||||
// Capabilities + built-in metadata file generation:
|
||||
//go:generate build/gen-run-go.sh internal/cmd/genopacapabilities/main.go capabilities.json
|
||||
//go:generate build/gen-run-go.sh internal/cmd/genbuiltinmetadata/main.go builtin_metadata.json
|
||||
|
||||
// WASM base binary generation:
|
||||
//go:generate build/gen-run-go.sh internal/cmd/genopawasm/main.go -o internal/compiler/wasm/opa/opa.go internal/compiler/wasm/opa/opa.wasm internal/compiler/wasm/opa/callgraph.csv
|
||||
|
||||
Reference in New Issue
Block a user