compiler: produce .hex files directly

This commit is contained in:
Ayke van Laethem
2018-09-14 20:27:04 +02:00
parent f41a8032e7
commit c763e9f1a6
4 changed files with 21 additions and 4 deletions
+15 -2
View File
@@ -89,6 +89,7 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool
// Link the object file with the system compiler.
executable := filepath.Join(dir, "main")
tmppath := executable // final file
args := append(spec.PreLinkArgs, "-o", executable, objfile)
cmd := exec.Command(spec.Linker, args...)
cmd.Stdout = os.Stdout
@@ -98,9 +99,21 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool
return err
}
if err := os.Rename(executable, outpath); err != nil {
if strings.HasSuffix(outpath, ".hex") {
// Get an Intel .hex file from the .elf file.
tmppath = filepath.Join(dir, "main.hex")
cmd := exec.Command(spec.Objcopy, "-O", "ihex", executable, tmppath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
}
if err := os.Rename(tmppath, outpath); err != nil {
// Moving failed. Do a file copy.
inf, err := os.Open(executable)
inf, err := os.Open(tmppath)
if err != nil {
return err
}
+2
View File
@@ -18,6 +18,7 @@ type TargetSpec struct {
BuildTags []string `json:"build-tags"`
Linker string `json:"linker"`
PreLinkArgs []string `json:"pre-link-args"`
Objcopy string `json:"objcopy"`
}
// Load a target specification
@@ -26,6 +27,7 @@ func LoadTarget(target string) (*TargetSpec, error) {
Triple: target,
BuildTags: []string{runtime.GOOS, runtime.GOARCH},
Linker: "cc",
Objcopy: "objcopy",
}
// See whether there is a target specification for this target (e.g.
+2 -1
View File
@@ -2,5 +2,6 @@
"llvm-target": "avr-atmel-none",
"build-tags": ["avr", "avr8", "atmega", "atmega328p", "js", "wasm"],
"linker": "avr-gcc",
"pre-link-args": ["-nostdlib", "-T", "targets/avr.ld", "-Wl,--gc-sections", "targets/avr.S"]
"pre-link-args": ["-nostdlib", "-T", "targets/avr.ld", "-Wl,--gc-sections", "targets/avr.S"],
"objcopy": "avr-objcopy"
}
+2 -1
View File
@@ -2,5 +2,6 @@
"llvm-target": "armv7m-none-eabi",
"build-tags": ["nrf", "nrf52", "nrf52832", "js", "wasm"],
"linker": "arm-none-eabi-gcc",
"pre-link-args": ["-nostdlib", "-nostartfiles", "-mcpu=cortex-m4", "-mthumb", "-T", "targets/arm.ld", "-Wl,--gc-sections", "-fno-exceptions", "-fno-unwind-tables", "-ffunction-sections", "-fdata-sections", "-Os", "-DNRF52832_XXAA", "-D__STARTUP_CLEAR_BSS", "-Ilib/CMSIS/CMSIS/Include", "lib/nrfx/mdk/gcc_startup_nrf51.S", "lib/nrfx/mdk/system_nrf52.c"]
"pre-link-args": ["-nostdlib", "-nostartfiles", "-mcpu=cortex-m4", "-mthumb", "-T", "targets/arm.ld", "-Wl,--gc-sections", "-fno-exceptions", "-fno-unwind-tables", "-ffunction-sections", "-fdata-sections", "-Os", "-DNRF52832_XXAA", "-D__STARTUP_CLEAR_BSS", "-Ilib/CMSIS/CMSIS/Include", "lib/nrfx/mdk/gcc_startup_nrf51.S", "lib/nrfx/mdk/system_nrf52.c"],
"objcopy": "arm-none-eabi-objcopy"
}