Skip to content

Commit

Permalink
fix(gnovm/softfloat): replace copy.sh with Go generator (#3584)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgan <morgan@morganbaz.com>
Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent ad93c33 commit a885c78
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 37 deletions.
32 changes: 0 additions & 32 deletions gnovm/pkg/gnolang/internal/softfloat/copy.sh

This file was deleted.

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

import (
"errors"
"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 gitRoot() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
p := wd
for {
if s, e := os.Stat(filepath.Join(p, ".git")); e == nil && s.IsDir() {
return p, nil
}

if strings.HasSuffix(p, string(filepath.Separator)) {
return "", errors.New("root git not found")
}

p = filepath.Dir(p)
}
}

func gofumpt() {
rootPath, err := gitRoot()
if err != nil {
log.Fatal("error finding git root:", err)
}

cmd := exec.Command("go", "run", "-modfile", filepath.Join(strings.TrimSpace(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

0 comments on commit a885c78

Please sign in to comment.