From 639ec1e6ee15cbcd3c244c6611769b5c9a6fb396 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 5 Apr 2020 18:19:26 +0200 Subject: [PATCH] builder: make sure -fshort-enums is used consistently The main change is in building the libraries, where -fshort-enums was passed on RISC-V while other C files weren't compiled with this setting. Note: the test already passed before this change, but it seems like a good idea to explicitly test for enum size consistency. There is also not a particular reason not to pass -fshort-enums on RISC-V. Perhaps it's better to do it there too (on baremetal targets that don't have to worry about binary compatibility). --- builder/builtins.go | 2 +- builder/library.go | 3 +++ builder/picolibc.go | 2 +- testdata/cgo/main.c | 2 ++ testdata/cgo/main.go | 3 +++ testdata/cgo/main.h | 2 ++ testdata/cgo/out.txt | 1 + 7 files changed, 13 insertions(+), 2 deletions(-) diff --git a/builder/builtins.go b/builder/builtins.go index a64a1d14..ed159e7e 100644 --- a/builder/builtins.go +++ b/builder/builtins.go @@ -158,7 +158,7 @@ var aeabiBuiltins = []string{ // For more information, see: https://compiler-rt.llvm.org/ var CompilerRT = Library{ name: "compiler-rt", - cflags: func() []string { return []string{"-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc"} }, + cflags: func() []string { return []string{"-Werror", "-Wall", "-std=c11", "-nostdlibinc"} }, sourceDir: "lib/compiler-rt/lib/builtins", sources: func(target string) []string { builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins diff --git a/builder/library.go b/builder/library.go index 0df1f146..a0d522a3 100644 --- a/builder/library.go +++ b/builder/library.go @@ -68,6 +68,9 @@ func (l *Library) Load(target string) (path string, err error) { // Precalculate the flags to the compiler invocation. args := append(l.cflags(), "-c", "-Oz", "-g", "-ffunction-sections", "-fdata-sections", "-Wno-macro-redefined", "--target="+target, "-fdebug-prefix-map="+dir+"="+remapDir) + if strings.HasPrefix(target, "arm") || strings.HasPrefix(target, "thumb") { + args = append(args, "-fshort-enums") + } if strings.HasPrefix(target, "riscv32-") { args = append(args, "-march=rv32imac", "-mabi=ilp32", "-fforce-enable-int128") } diff --git a/builder/picolibc.go b/builder/picolibc.go index 31e2f053..d3a095d6 100644 --- a/builder/picolibc.go +++ b/builder/picolibc.go @@ -12,7 +12,7 @@ var Picolibc = Library{ name: "picolibc", cflags: func() []string { picolibcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib/libc") - return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "-fshort-enums", "--sysroot=" + picolibcDir, "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"} + return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "--sysroot=" + picolibcDir, "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"} }, sourceDir: "lib/picolibc/newlib/libc", sources: func(target string) []string { diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c index 97b49155..8f3b0946 100644 --- a/testdata/cgo/main.c +++ b/testdata/cgo/main.c @@ -20,6 +20,8 @@ int globalUnionSize = sizeof(globalUnion); option_t globalOption = optionG; bitfield_t globalBitfield = {244, 15, 1, 2, 47, 5}; +int smallEnumWidth = sizeof(option2_t); + int fortytwo() { return 42; } diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go index 557dea5c..616bc5bf 100644 --- a/testdata/cgo/main.go +++ b/testdata/cgo/main.go @@ -111,6 +111,9 @@ func main() { println("option 2A:", C.option2A) println("option 3A:", C.option3A) + // Check that enums are considered the same width in C and CGo. + println("enum width matches:", unsafe.Sizeof(C.option2_t(0)) == uintptr(C.smallEnumWidth)) + // libc: test whether C functions work at all. buf1 := []byte("foobar\x00") buf2 := make([]byte, len(buf1)) diff --git a/testdata/cgo/main.h b/testdata/cgo/main.h index d38b3322..b4ac4ee8 100644 --- a/testdata/cgo/main.h +++ b/testdata/cgo/main.h @@ -134,6 +134,8 @@ extern int globalUnionSize; extern option_t globalOption; extern bitfield_t globalBitfield; +extern int smallEnumWidth; + // test duplicate definitions int add(int a, int b); extern int global; diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt index fcb68846..9da7386a 100644 --- a/testdata/cgo/out.txt +++ b/testdata/cgo/out.txt @@ -55,4 +55,5 @@ option F: 11 option G: 12 option 2A: 20 option 3A: 21 +enum width matches: true copied string: foobar