Skip to content

Commit 639ec1e

Browse files
committed
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).
1 parent 8333c17 commit 639ec1e

File tree

7 files changed

+13
-2
lines changed

7 files changed

+13
-2
lines changed

builder/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ var aeabiBuiltins = []string{
158158
// For more information, see: https://compiler-rt.llvm.org/
159159
var CompilerRT = Library{
160160
name: "compiler-rt",
161-
cflags: func() []string { return []string{"-Werror", "-Wall", "-std=c11", "-fshort-enums", "-nostdlibinc"} },
161+
cflags: func() []string { return []string{"-Werror", "-Wall", "-std=c11", "-nostdlibinc"} },
162162
sourceDir: "lib/compiler-rt/lib/builtins",
163163
sources: func(target string) []string {
164164
builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins

builder/library.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (l *Library) Load(target string) (path string, err error) {
6868

6969
// Precalculate the flags to the compiler invocation.
7070
args := append(l.cflags(), "-c", "-Oz", "-g", "-ffunction-sections", "-fdata-sections", "-Wno-macro-redefined", "--target="+target, "-fdebug-prefix-map="+dir+"="+remapDir)
71+
if strings.HasPrefix(target, "arm") || strings.HasPrefix(target, "thumb") {
72+
args = append(args, "-fshort-enums")
73+
}
7174
if strings.HasPrefix(target, "riscv32-") {
7275
args = append(args, "-march=rv32imac", "-mabi=ilp32", "-fforce-enable-int128")
7376
}

builder/picolibc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Picolibc = Library{
1212
name: "picolibc",
1313
cflags: func() []string {
1414
picolibcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib/libc")
15-
return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "-fshort-enums", "--sysroot=" + picolibcDir, "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"}
15+
return []string{"-Werror", "-Wall", "-std=gnu11", "-D_COMPILING_NEWLIB", "--sysroot=" + picolibcDir, "-I" + picolibcDir + "/tinystdio", "-I" + goenv.Get("TINYGOROOT") + "/lib/picolibc-include"}
1616
},
1717
sourceDir: "lib/picolibc/newlib/libc",
1818
sources: func(target string) []string {

testdata/cgo/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ int globalUnionSize = sizeof(globalUnion);
2020
option_t globalOption = optionG;
2121
bitfield_t globalBitfield = {244, 15, 1, 2, 47, 5};
2222

23+
int smallEnumWidth = sizeof(option2_t);
24+
2325
int fortytwo() {
2426
return 42;
2527
}

testdata/cgo/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func main() {
111111
println("option 2A:", C.option2A)
112112
println("option 3A:", C.option3A)
113113

114+
// Check that enums are considered the same width in C and CGo.
115+
println("enum width matches:", unsafe.Sizeof(C.option2_t(0)) == uintptr(C.smallEnumWidth))
116+
114117
// libc: test whether C functions work at all.
115118
buf1 := []byte("foobar\x00")
116119
buf2 := make([]byte, len(buf1))

testdata/cgo/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ extern int globalUnionSize;
134134
extern option_t globalOption;
135135
extern bitfield_t globalBitfield;
136136

137+
extern int smallEnumWidth;
138+
137139
// test duplicate definitions
138140
int add(int a, int b);
139141
extern int global;

testdata/cgo/out.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ option F: 11
5555
option G: 12
5656
option 2A: 20
5757
option 3A: 21
58+
enum width matches: true
5859
copied string: foobar

0 commit comments

Comments
 (0)