all: compile and link using clang, where possible

Этот коммит содержится в:
Ayke van Laethem 2018-11-22 13:24:13 +01:00
родитель 4a8ced590b
коммит 62d74d8329
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
15 изменённых файлов: 133 добавлений и 100 удалений

Просмотреть файл

@ -195,7 +195,7 @@ func loadBuiltins(target string) (path string, err error) {
objpath := filepath.Join(dir, objname+".o") objpath := filepath.Join(dir, objname+".o")
objs = append(objs, objpath) objs = append(objs, objpath)
srcpath := filepath.Join(builtinsDir, name) srcpath := filepath.Join(builtinsDir, name)
cmd := exec.Command(commands["clang"], "-c", "-Oz", "-g", "-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc", "--target="+target, "-o", objpath, srcpath) cmd := exec.Command(commands["clang"], "-c", "-Oz", "-g", "-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc", "-ffunction-sections", "-fdata-sections", "--target="+target, "-o", objpath, srcpath)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Dir = dir cmd.Dir = dir

Просмотреть файл

@ -24,11 +24,10 @@ ARM LLVM backend (which is even used in the propietary C compiler from ARM).
Compiling to object code should be supported out of the box, but compiling the Compiling to object code should be supported out of the box, but compiling the
final binary and flashing it needs some extra tools. final binary and flashing it needs some extra tools.
* binutils (``arm-none-eabi-objcopy``) for producing .hex files for * binutils (``arm-none-eabi-ld``, ``arm-none-eabi-objcopy``) for linking and
flashing. for producing .hex files for flashing.
* GCC (``arm-none-eabi-gcc``) for linking object files. * Clang 7 (``clang-7``) for building assembly files and the `compiler
* Clang 7 (``clang-7``) for building the `compiler runtime library runtime library <https://compiler-rt.llvm.org/>`_ .
<https://compiler-rt.llvm.org/>`_.
* The flashing tool for the particular chip, like ``openocd`` or * The flashing tool for the particular chip, like ``openocd`` or
``nrfjprog``. ``nrfjprog``.

25
main.go
Просмотреть файл

@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"syscall" "syscall"
@ -161,14 +162,30 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
cachePath, _ = filepath.Split(librt) cachePath, _ = filepath.Split(librt)
} }
// Link the object file with the system compiler. // Prepare link command.
executable := filepath.Join(dir, "main") executable := filepath.Join(dir, "main")
tmppath := executable // final file tmppath := executable // final file
args := append(spec.PreLinkArgs, "-o", executable, objfile) ldflags := append(spec.LDFlags, "-o", executable, objfile)
if spec.RTLib == "compiler-rt" { if spec.RTLib == "compiler-rt" {
args = append(args, "-L", cachePath, "-lrt-"+spec.Triple) ldflags = append(ldflags, "-L", cachePath, "-lrt-"+spec.Triple)
} }
cmd := exec.Command(spec.Linker, args...)
// Compile extra files.
for i, path := range spec.ExtraFiles {
outpath := filepath.Join(dir, "extra-"+strconv.Itoa(i)+"-"+filepath.Base(path)+".o")
cmd := exec.Command(spec.Compiler, append(spec.CFlags, "-c", "-o", outpath, path)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = sourceDir()
err := cmd.Run()
if err != nil {
return err
}
ldflags = append(ldflags, outpath)
}
// Link the object files together.
cmd := exec.Command(spec.Linker, ldflags...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Dir = sourceDir() cmd.Dir = sourceDir()

Просмотреть файл

@ -21,9 +21,12 @@ type TargetSpec struct {
Inherits []string `json:"inherits"` Inherits []string `json:"inherits"`
Triple string `json:"llvm-target"` Triple string `json:"llvm-target"`
BuildTags []string `json:"build-tags"` BuildTags []string `json:"build-tags"`
Compiler string `json:"compiler"`
Linker string `json:"linker"` Linker string `json:"linker"`
RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt) RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt)
PreLinkArgs []string `json:"pre-link-args"` CFlags []string `json:"cflags"`
LDFlags []string `json:"ldflags"`
ExtraFiles []string `json:"extra-files"`
Objcopy string `json:"objcopy"` Objcopy string `json:"objcopy"`
Emulator []string `json:"emulator"` Emulator []string `json:"emulator"`
Flasher string `json:"flash"` Flasher string `json:"flash"`
@ -41,15 +44,18 @@ func (spec *TargetSpec) copyProperties(spec2 *TargetSpec) {
spec.Triple = spec2.Triple spec.Triple = spec2.Triple
} }
spec.BuildTags = append(spec.BuildTags, spec2.BuildTags...) spec.BuildTags = append(spec.BuildTags, spec2.BuildTags...)
if spec2.Compiler != "" {
spec.Compiler = spec2.Compiler
}
if spec2.Linker != "" { if spec2.Linker != "" {
spec.Linker = spec2.Linker spec.Linker = spec2.Linker
} }
if spec2.RTLib != "" { if spec2.RTLib != "" {
spec.RTLib = spec2.RTLib spec.RTLib = spec2.RTLib
} }
if len(spec2.PreLinkArgs) != 0 { spec.CFlags = append(spec.CFlags, spec2.CFlags...)
spec.PreLinkArgs = spec2.PreLinkArgs spec.LDFlags = append(spec.LDFlags, spec2.LDFlags...)
} spec.ExtraFiles = append(spec.ExtraFiles, spec2.ExtraFiles...)
if spec2.Objcopy != "" { if spec2.Objcopy != "" {
spec.Objcopy = spec2.Objcopy spec.Objcopy = spec2.Objcopy
} }
@ -146,7 +152,7 @@ func LoadTarget(target string) (*TargetSpec, error) {
Triple: target, Triple: target,
BuildTags: []string{runtime.GOOS, runtime.GOARCH}, BuildTags: []string{runtime.GOOS, runtime.GOARCH},
Linker: "cc", Linker: "cc",
PreLinkArgs: []string{"-no-pie"}, // WARNING: clang < 5.0 requires -nopie LDFlags: []string{"-no-pie"}, // WARNING: clang < 5.0 requires -nopie
Objcopy: "objcopy", Objcopy: "objcopy",
GDB: "gdb", GDB: "gdb",
GDBCmds: []string{"run"}, GDBCmds: []string{"run"},

Просмотреть файл

@ -2,14 +2,15 @@
"inherits": ["avr"], "inherits": ["avr"],
"llvm-target": "avr-atmel-none", "llvm-target": "avr-atmel-none",
"build-tags": ["arduino", "atmega328p", "atmega", "avr5"], "build-tags": ["arduino", "atmega328p", "atmega", "avr5"],
"pre-link-args": [ "cflags": [
"-nostartfiles", "-mmcu=atmega328p"
"-mmcu=avr5", ],
"ldflags": [
"-Wl,--defsym=_bootloader_size=512", "-Wl,--defsym=_bootloader_size=512",
"-Wl,--defsym=_stack_size=512", "-Wl,--defsym=_stack_size=512",
"-T", "src/device/avr/atmega328p.ld", "-T", "src/device/avr/atmega328p.ld"
"-T", "targets/avr.ld", ],
"-Wl,--gc-sections", "extra-files": [
"targets/avr.S", "targets/avr.S",
"src/device/avr/atmega328p.s" "src/device/avr/atmega328p.s"
], ],

Просмотреть файл

@ -1,4 +1,7 @@
/* Unused, but here to silence a linker warning. */
ENTRY(Reset_Handler)
/* define output sections */ /* define output sections */
SECTIONS SECTIONS
{ {

Просмотреть файл

@ -1,5 +1,10 @@
{ {
"build-tags": ["avr", "js", "wasm"], "build-tags": ["avr", "js", "wasm"],
"compiler": "avr-gcc",
"linker": "avr-gcc", "linker": "avr-gcc",
"objcopy": "avr-objcopy" "objcopy": "avr-objcopy",
"ldflags": [
"-T", "targets/avr.ld",
"-Wl,--gc-sections"
]
} }

Просмотреть файл

@ -2,16 +2,14 @@
"inherits": ["cortex-m"], "inherits": ["cortex-m"],
"llvm-target": "armv7m-none-eabi", "llvm-target": "armv7m-none-eabi",
"build-tags": ["bluepill", "stm32f103xx", "stm32"], "build-tags": ["bluepill", "stm32f103xx", "stm32"],
"pre-link-args": [ "cflags": [
"-nostdlib", "--target=armv7m-none-eabi",
"-nostartfiles", "-Qunused-arguments"
"-mcpu=cortex-m3", ],
"-mthumb", "ldflags": [
"-T", "targets/stm32.ld", "-T", "targets/stm32.ld"
"-Wl,--gc-sections", ],
"-fno-exceptions", "-fno-unwind-tables", "extra-files": [
"-ffunction-sections", "-fdata-sections",
"-Os",
"src/device/stm32/stm32f103xx.s" "src/device/stm32/stm32f103xx.s"
], ],
"flash": "openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c 'program {hex} reset exit'" "flash": "openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c 'program {hex} reset exit'"

Просмотреть файл

@ -1,7 +1,18 @@
{ {
"build-tags": ["tinygo.arm", "js", "wasm"], "build-tags": ["tinygo.arm", "js", "wasm"],
"linker": "arm-none-eabi-gcc", "compiler": "clang-7",
"linker": "arm-none-eabi-ld",
"rtlib": "compiler-rt", "rtlib": "compiler-rt",
"cflags": [
"-Oz",
"-mthumb",
"-fshort-enums",
"-fno-exceptions", "-fno-unwind-tables",
"-ffunction-sections", "-fdata-sections"
],
"ldflags": [
"--gc-sections"
],
"objcopy": "arm-none-eabi-objcopy", "objcopy": "arm-none-eabi-objcopy",
"gdb": "arm-none-eabi-gdb" "gdb": "arm-none-eabi-gdb"
} }

Просмотреть файл

@ -26,7 +26,7 @@ Default_Handler:
.set \handler, Default_Handler .set \handler, Default_Handler
.endm .endm
.section .isr_vector .section .isr_vector, "a", %progbits
.global __isr_vector .global __isr_vector
// Interrupt vector as defined by Cortex-M, starting with the stack top. // Interrupt vector as defined by Cortex-M, starting with the stack top.
// On reset, SP is initialized with *0x0 and PC is loaded with *0x4, loading // On reset, SP is initialized with *0x0 and PC is loaded with *0x4, loading

Просмотреть файл

@ -2,14 +2,15 @@
"inherits": ["avr"], "inherits": ["avr"],
"llvm-target": "avr-atmel-none", "llvm-target": "avr-atmel-none",
"build-tags": ["digispark", "attiny85", "attiny", "avr2", "avr25"], "build-tags": ["digispark", "attiny85", "attiny", "avr2", "avr25"],
"pre-link-args": [ "cflags": [
"-nostartfiles", "-mmcu=attiny85"
"-mmcu=attiny85", ],
"ldflags": [
"-Wl,--defsym=_bootloader_size=2180", "-Wl,--defsym=_bootloader_size=2180",
"-Wl,--defsym=_stack_size=128", "-Wl,--defsym=_stack_size=128",
"-T", "src/device/avr/attiny85.ld", "-T", "src/device/avr/attiny85.ld"
"-T", "targets/avr.ld", ],
"-Wl,--gc-sections", "extra-files": [
"targets/avr.S", "targets/avr.S",
"src/device/avr/attiny85.s" "src/device/avr/attiny85.s"
], ],

Просмотреть файл

@ -2,18 +2,16 @@
"inherits": ["cortex-m"], "inherits": ["cortex-m"],
"llvm-target": "armv6m-none-eabi", "llvm-target": "armv6m-none-eabi",
"build-tags": ["nrf51822", "nrf51", "nrf"], "build-tags": ["nrf51822", "nrf51", "nrf"],
"pre-link-args": [ "cflags": [
"-nostdlib", "--target=armv6m-none-eabi",
"-nostartfiles", "-Qunused-arguments",
"-mcpu=cortex-m0",
"-mthumb",
"-T", "targets/nrf51.ld",
"-Wl,--gc-sections",
"-fno-exceptions", "-fno-unwind-tables",
"-ffunction-sections", "-fdata-sections",
"-Os",
"-DNRF51", "-DNRF51",
"-Ilib/CMSIS/CMSIS/Include", "-Ilib/CMSIS/CMSIS/Include"
],
"ldflags": [
"-T", "targets/nrf51.ld"
],
"extra-files": [
"lib/nrfx/mdk/system_nrf51.c", "lib/nrfx/mdk/system_nrf51.c",
"src/device/nrf/nrf51.s" "src/device/nrf/nrf51.s"
] ]

Просмотреть файл

@ -2,18 +2,16 @@
"inherits": ["cortex-m"], "inherits": ["cortex-m"],
"llvm-target": "armv7em-none-eabi", "llvm-target": "armv7em-none-eabi",
"build-tags": ["nrf52", "nrf"], "build-tags": ["nrf52", "nrf"],
"pre-link-args": [ "cflags": [
"-nostdlib", "--target=armv7em-none-eabi",
"-nostartfiles", "-Qunused-arguments",
"-mcpu=cortex-m4",
"-mthumb",
"-T", "targets/nrf52.ld",
"-Wl,--gc-sections",
"-fno-exceptions", "-fno-unwind-tables",
"-ffunction-sections", "-fdata-sections",
"-Os",
"-DNRF52832_XXAA", "-DNRF52832_XXAA",
"-Ilib/CMSIS/CMSIS/Include", "-Ilib/CMSIS/CMSIS/Include"
],
"ldflags": [
"-T", "targets/nrf52.ld"
],
"extra-files": [
"lib/nrfx/mdk/system_nrf52.c", "lib/nrfx/mdk/system_nrf52.c",
"src/device/nrf/nrf52.s" "src/device/nrf/nrf52.s"
] ]

Просмотреть файл

@ -2,18 +2,16 @@
"inherits": ["cortex-m"], "inherits": ["cortex-m"],
"llvm-target": "armv7em-none-eabi", "llvm-target": "armv7em-none-eabi",
"build-tags": ["nrf52840", "nrf"], "build-tags": ["nrf52840", "nrf"],
"pre-link-args": [ "cflags": [
"-nostdlib", "--target=armv7em-none-eabi",
"-nostartfiles", "-Qunused-arguments",
"-mcpu=cortex-m4",
"-mthumb",
"-T", "targets/nrf52840.ld",
"-Wl,--gc-sections",
"-fno-exceptions", "-fno-unwind-tables",
"-ffunction-sections", "-fdata-sections",
"-Os",
"-DNRF52840_XXAA", "-DNRF52840_XXAA",
"-Ilib/CMSIS/CMSIS/Include", "-Ilib/CMSIS/CMSIS/Include"
],
"ldflags": [
"-T", "targets/nrf52840.ld"
],
"extra-files": [
"lib/nrfx/mdk/system_nrf52840.c", "lib/nrfx/mdk/system_nrf52840.c",
"src/device/nrf/nrf52840.s" "src/device/nrf/nrf52840.s"
] ]

Просмотреть файл

@ -2,16 +2,14 @@
"inherits": ["cortex-m"], "inherits": ["cortex-m"],
"llvm-target": "armv7m-none-eabi", "llvm-target": "armv7m-none-eabi",
"build-tags": ["qemu", "lm3s6965"], "build-tags": ["qemu", "lm3s6965"],
"pre-link-args": [ "cflags": [
"-nostdlib", "--target=armv7m-none-eabi",
"-nostartfiles", "-Qunused-arguments"
"-mcpu=cortex-m0", ],
"-mthumb", "ldflags": [
"-T", "targets/lm3s6965.ld", "-T", "targets/lm3s6965.ld"
"-Wl,--gc-sections", ],
"-fno-exceptions", "-fno-unwind-tables", "extra-files": [
"-ffunction-sections", "-fdata-sections",
"-Os",
"targets/cortex-m.s" "targets/cortex-m.s"
], ],
"emulator": ["qemu-system-arm", "-machine", "lm3s6965evb", "-semihosting", "-nographic", "-kernel"] "emulator": ["qemu-system-arm", "-machine", "lm3s6965evb", "-semihosting", "-nographic", "-kernel"]