diff --git a/main.go b/main.go index 2b772a08..9f1b489d 100644 --- a/main.go +++ b/main.go @@ -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 } diff --git a/target.go b/target.go index ef37deb1..1240fea7 100644 --- a/target.go +++ b/target.go @@ -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. diff --git a/targets/arduino.json b/targets/arduino.json index 1890b9ee..2be51759 100644 --- a/targets/arduino.json +++ b/targets/arduino.json @@ -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" } diff --git a/targets/pca10040.json b/targets/pca10040.json index cc17d784..6ec3d33c 100644 --- a/targets/pca10040.json +++ b/targets/pca10040.json @@ -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" }