Skip to content

Commit

Permalink
simplify assembler binary lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Nov 23, 2024
1 parent 1cad6c1 commit c72de60
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
16 changes: 4 additions & 12 deletions internal/assembler/asm6/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@ package asm6
import (
"fmt"
"os/exec"
"runtime"
"strings"
)

const (
assemblerName = "asm6f"
)
const assemblerName = "asm6f"

// AssembleUsingExternalApp calls the external assembler and linker to generate a .nes
// ROM from the given asm file.
func AssembleUsingExternalApp(asmFile, outputFile string) error {
assembler := assemblerName
if runtime.GOOS == "windows" {
assembler += ".exe"
}

if _, err := exec.LookPath(assembler); err != nil {
return fmt.Errorf("%s is not installed", assembler)
if _, err := exec.LookPath(assemblerName); err != nil {
return fmt.Errorf("%s is not installed", assemblerName)
}

cmd := exec.Command(assembler, asmFile, outputFile)
cmd := exec.Command(assemblerName, asmFile, outputFile)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("assembling file: %s: %w", strings.TrimSpace(string(out)), err)
}
Expand Down
20 changes: 6 additions & 14 deletions internal/assembler/ca65/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

"github.com/retroenv/nesgodisasm/internal/program"
Expand All @@ -27,21 +26,14 @@ type Config struct {
// AssembleUsingExternalApp calls the external assembler and linker to generate a .nes
// ROM from the given asm file.
func AssembleUsingExternalApp(asmFile, objectFile, outputFile string, conf Config) error {
assembler := assemblerName
linker := linkerName
if runtime.GOOS == "windows" {
assembler += ".exe"
linker += ".exe"
if _, err := exec.LookPath(assemblerName); err != nil {
return fmt.Errorf("%s is not installed", assemblerName)
}

if _, err := exec.LookPath(assembler); err != nil {
return fmt.Errorf("%s is not installed", assembler)
}
if _, err := exec.LookPath(linker); err != nil {
return fmt.Errorf("%s is not installed", linker)
if _, err := exec.LookPath(linkerName); err != nil {
return fmt.Errorf("%s is not installed", linkerName)
}

cmd := exec.Command(assembler, asmFile, "-o", objectFile)
cmd := exec.Command(assemblerName, asmFile, "-o", objectFile)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("assembling file: %s: %w", strings.TrimSpace(string(out)), err)
}
Expand All @@ -63,7 +55,7 @@ func AssembleUsingExternalApp(asmFile, objectFile, outputFile string, conf Confi
return fmt.Errorf("writing linker config: %w", err)
}

cmd = exec.Command(linker, "-C", configFile.Name(), "-o", outputFile, objectFile)
cmd = exec.Command(linkerName, "-C", configFile.Name(), "-o", outputFile, objectFile)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("linking file: %s: %w", string(out), err)
}
Expand Down
16 changes: 4 additions & 12 deletions internal/assembler/nesasm/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@ package nesasm
import (
"fmt"
"os/exec"
"runtime"
"strings"
)

const (
assemblerName = "nesasm"
)
const assemblerName = "nesasm"

// AssembleUsingExternalApp calls the external assembler and linker to generate a .nes
// ROM from the given asm file.
func AssembleUsingExternalApp(asmFile, outputFile string) error {
assembler := assemblerName
if runtime.GOOS == "windows" {
assembler += ".exe"
}

if _, err := exec.LookPath(assembler); err != nil {
return fmt.Errorf("%s is not installed", assembler)
if _, err := exec.LookPath(assemblerName); err != nil {
return fmt.Errorf("%s is not installed", assemblerName)
}

cmd := exec.Command(assembler, "-z", "-o", outputFile, asmFile)
cmd := exec.Command(assemblerName, "-z", "-o", outputFile, asmFile)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("assembling file: %s: %w", strings.TrimSpace(string(out)), err)
}
Expand Down

0 comments on commit c72de60

Please sign in to comment.