Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm/softfloat): replace copy.sh with Go generator #3584

Merged
merged 10 commits into from
Jan 28, 2025
32 changes: 0 additions & 32 deletions gnovm/pkg/gnolang/internal/softfloat/copy.sh

This file was deleted.

96 changes: 96 additions & 0 deletions gnovm/pkg/gnolang/internal/softfloat/gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

func main() {
// Process softfloat64.go file
processSoftFloat64File()

// Process softfloat64_test.go file
processSoftFloat64TestFile()

// Run mvdan.cc/gofumpt
gofumpt()

fmt.Println("Files processed successfully.")
}

func processSoftFloat64File() {
// Read source file
content, err := os.ReadFile(fmt.Sprintf("%s/src/runtime/softfloat64.go", runtime.GOROOT()))
if err != nil {
log.Fatal("Error reading source file:", err)
}

// Prepare header
header := `// Code generated by github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat/gen. DO NOT EDIT.
// This file is copied from $GOROOT/src/runtime/softfloat64.go.
// It is the software floating point implementation used by the Go runtime.

`

// Combine header with content
newContent := header + string(content)

// Replace package name
newContent = strings.Replace(newContent, "package runtime", "package softfloat", 1)

// Write to destination file
err = os.WriteFile("runtime_softfloat64.go", []byte(newContent), 0o644)
if err != nil {
log.Fatal("Error writing to destination file:", err)
}
}

func processSoftFloat64TestFile() {
// Read source test file
content, err := os.ReadFile(fmt.Sprintf("%s/src/runtime/softfloat64_test.go", runtime.GOROOT()))
if err != nil {
log.Fatal("Error reading source test file:", err)
}

// Prepare header
header := `// Code generated by github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat/gen. DO NOT EDIT.
// This file is copied from $GOROOT/src/runtime/softfloat64_test.go.
// It is the tests for the software floating point implementation
// used by the Go runtime.

`

// Combine header with content
newContent := header + string(content)

// Replace package name and imports
newContent = strings.Replace(newContent, "package runtime_test", "package softfloat_test", 1)
newContent = strings.Replace(newContent, "\t. \"runtime\"", "\t\"runtime\"", 1)
newContent = strings.Replace(newContent, "GOARCH", "runtime.GOARCH", 1)

newContent = strings.Replace(newContent, "import (", "import (\n\t. \"github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat\"", 1)

// Write to destination file
err = os.WriteFile("runtime_softfloat64_test.go", []byte(newContent), 0o644)
if err != nil {
log.Fatal("Error writing to destination test file:", err)
}
}

func gofumpt() {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
rootPath, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
thehowl marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatal("error git rev-parse:", err)
}

cmd := exec.Command("go", "run", "-modfile", filepath.Join(strings.TrimSpace(string(rootPath)), "misc/devdeps/go.mod"), "mvdan.cc/gofumpt", "-w", ".")
_, err = cmd.Output()
if err != nil {
log.Fatal("error gofumpt:", err)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/internal/softfloat/softfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package softfloat

// This file mostly exports the functions from runtime_softfloat64.go

//go:generate sh copy.sh
//go:generate go run github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat/gen

const (
mask = 0x7FF
Expand Down
Loading