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).
Этот коммит содержится в:
Ayke van Laethem 2020-04-05 18:19:26 +02:00 коммит произвёл Ayke
родитель 8333c171f4
коммит 639ec1e6ee
7 изменённых файлов: 13 добавлений и 2 удалений

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

@ -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

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

@ -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")
}

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

@ -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 {

2
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;
}

3
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))

2
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;

1
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