cgo: support builtin #include headers

Add support for header files bundled with the compiler by copying them
into the release tarball.
Этот коммит содержится в:
Ayke van Laethem 2019-04-20 15:14:48 +02:00 коммит произвёл Ron Evans
родитель d396abb690
коммит 2f2d62cc0c
10 изменённых файлов: 49 добавлений и 18 удалений

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

@ -47,6 +47,18 @@ commands:
command: | command: |
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure --vendor-only dep ensure --vendor-only
llvm-source-linux:
steps:
- restore_cache:
keys:
- llvm-source-8-v2
- run:
name: "Fetch LLVM source"
command: make llvm-source
- save_cache:
key: llvm-source-8-v2
paths:
- llvm
smoketest: smoketest:
steps: steps:
- smoketest-no-avr - smoketest-no-avr
@ -86,6 +98,7 @@ commands:
keys: keys:
- go-cache-{{ checksum "Gopkg.lock" }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }} - go-cache-{{ checksum "Gopkg.lock" }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }}
- go-cache-{{ checksum "Gopkg.lock" }} - go-cache-{{ checksum "Gopkg.lock" }}
- llvm-source-linux
- dep - dep
- run: go install . - run: go install .
- run: go test -v - run: go test -v
@ -121,16 +134,7 @@ commands:
keys: keys:
- go-cache-{{ checksum "Gopkg.lock" }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }} - go-cache-{{ checksum "Gopkg.lock" }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }}
- go-cache-{{ checksum "Gopkg.lock" }} - go-cache-{{ checksum "Gopkg.lock" }}
- restore_cache: - llvm-source-linux
keys:
- llvm-source-8-v2
- run:
name: "Fetch LLVM source"
command: make llvm-source
- save_cache:
key: llvm-source-8-v2
paths:
- llvm
- restore_cache: - restore_cache:
keys: keys:
- llvm-build-8-v2 - llvm-build-8-v2

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

@ -86,13 +86,16 @@ test:
release: build/tinygo gen-device release: build/tinygo gen-device
@mkdir -p build/release/tinygo/bin @mkdir -p build/release/tinygo/bin
@mkdir -p build/release/tinygo/lib/clang/include
@mkdir -p build/release/tinygo/lib/CMSIS/CMSIS @mkdir -p build/release/tinygo/lib/CMSIS/CMSIS
@mkdir -p build/release/tinygo/lib/compiler-rt/lib @mkdir -p build/release/tinygo/lib/compiler-rt/lib
@mkdir -p build/release/tinygo/lib/nrfx @mkdir -p build/release/tinygo/lib/nrfx
@mkdir -p build/release/tinygo/pkg/armv6m-none-eabi @mkdir -p build/release/tinygo/pkg/armv6m-none-eabi
@mkdir -p build/release/tinygo/pkg/armv7m-none-eabi @mkdir -p build/release/tinygo/pkg/armv7m-none-eabi
@mkdir -p build/release/tinygo/pkg/armv7em-none-eabi @mkdir -p build/release/tinygo/pkg/armv7em-none-eabi
@echo copying source files
@cp -p build/tinygo build/release/tinygo/bin @cp -p build/tinygo build/release/tinygo/bin
@cp -p $(abspath $(CLANG_SRC))/lib/Headers/*.h build/release/tinygo/lib/clang/include
@cp -rp lib/CMSIS/CMSIS/Include build/release/tinygo/lib/CMSIS/CMSIS @cp -rp lib/CMSIS/CMSIS/Include build/release/tinygo/lib/CMSIS/CMSIS
@cp -rp lib/CMSIS/README.md build/release/tinygo/lib/CMSIS @cp -rp lib/CMSIS/README.md build/release/tinygo/lib/CMSIS
@cp -rp lib/compiler-rt/lib/builtins build/release/tinygo/lib/compiler-rt/lib @cp -rp lib/compiler-rt/lib/builtins build/release/tinygo/lib/compiler-rt/lib

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

@ -217,8 +217,9 @@ func (c *Compiler) Compile(mainPath string) error {
MaxAlign: int64(c.targetData.PrefTypeAlignment(c.i8ptrType)), MaxAlign: int64(c.targetData.PrefTypeAlignment(c.i8ptrType)),
}, },
}, },
Dir: wd, Dir: wd,
CFlags: c.CFlags, TinyGoRoot: c.RootDir,
CFlags: c.CFlags,
} }
if strings.HasSuffix(mainPath, ".go") { if strings.HasSuffix(mainPath, ".go") {
_, err = lprogram.ImportFile(mainPath) _, err = lprogram.ImportFile(mainPath)

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

@ -22,6 +22,7 @@ type Program struct {
fset *token.FileSet fset *token.FileSet
TypeChecker types.Config TypeChecker types.Config
Dir string // current working directory (for error reporting) Dir string // current working directory (for error reporting)
TinyGoRoot string // root of the TinyGo installation or root of the source code
CFlags []string CFlags []string
} }
@ -292,6 +293,16 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
} }
files = append(files, f) files = append(files, f)
} }
clangIncludes := ""
if len(p.CgoFiles) != 0 {
if _, err := os.Stat(filepath.Join(p.TinyGoRoot, "llvm", "tools", "clang", "lib", "Headers")); !os.IsNotExist(err) {
// Running from the source directory.
clangIncludes = filepath.Join(p.TinyGoRoot, "llvm", "tools", "clang", "lib", "Headers")
} else {
// Running from the installation directory.
clangIncludes = filepath.Join(p.TinyGoRoot, "lib", "clang", "include")
}
}
for _, file := range p.CgoFiles { for _, file := range p.CgoFiles {
path := filepath.Join(p.Package.Dir, file) path := filepath.Join(p.Package.Dir, file)
f, err := p.parseFile(path, parser.ParseComments) f, err := p.parseFile(path, parser.ParseComments)
@ -299,7 +310,7 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
fileErrs = append(fileErrs, err) fileErrs = append(fileErrs, err)
continue continue
} }
errs := p.processCgo(path, f, append(p.CFlags, "-I"+p.Package.Dir)) errs := p.processCgo(path, f, append(p.CFlags, "-I"+p.Package.Dir, "-I"+clangIncludes))
if errs != nil { if errs != nil {
fileErrs = append(fileErrs, errs...) fileErrs = append(fileErrs, errs...)
continue continue

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

@ -9,7 +9,10 @@
"cflags": [ "cflags": [
"-Oz", "-Oz",
"-mthumb", "-mthumb",
"-Werror",
"-fshort-enums", "-fshort-enums",
"-nostdlibinc",
"-Wno-macro-redefined",
"-fno-exceptions", "-fno-unwind-tables", "-fno-exceptions", "-fno-unwind-tables",
"-ffunction-sections", "-fdata-sections" "-ffunction-sections", "-fdata-sections"
], ],

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

@ -7,6 +7,8 @@
"linker": "wasm-ld", "linker": "wasm-ld",
"cflags": [ "cflags": [
"--target=wasm32", "--target=wasm32",
"-nostdlibinc",
"-Wno-macro-redefined",
"-Oz" "-Oz"
], ],
"ldflags": [ "ldflags": [

5
testdata/cgo/main.c предоставленный
Просмотреть файл

@ -1,8 +1,8 @@
#include "main.h" #include "main.h"
int global = 3; int global = 3;
_Bool globalBool = 1; bool globalBool = 1;
_Bool globalBool2 = 10; // test narrowing bool globalBool2 = 10; // test narrowing
float globalFloat = 3.1; float globalFloat = 3.1;
double globalDouble = 3.2; double globalDouble = 3.2;
_Complex float globalComplexFloat = 4.1+3.3i; _Complex float globalComplexFloat = 4.1+3.3i;
@ -11,6 +11,7 @@ _Complex double globalComplexLongDouble = 4.3+3.5i;
char globalChar = 100; char globalChar = 100;
void *globalVoidPtrSet = &global; void *globalVoidPtrSet = &global;
void *globalVoidPtrNull; void *globalVoidPtrNull;
int64_t globalInt64 = -(2LL << 40);
collection_t globalStruct = {256, -123456, 3.14, 88}; collection_t globalStruct = {256, -123456, 3.14, 88};
int globalStructSize = sizeof(globalStruct); int globalStructSize = sizeof(globalStruct);
short globalArray[3] = {5, 6, 7}; short globalArray[3] = {5, 6, 7};

1
testdata/cgo/main.go предоставленный
Просмотреть файл

@ -43,6 +43,7 @@ func main() {
println("char match:", C.globalChar == 100) println("char match:", C.globalChar == 100)
var voidPtr unsafe.Pointer = C.globalVoidPtrNull var voidPtr unsafe.Pointer = C.globalVoidPtrNull
println("void* match:", voidPtr == nil, C.globalVoidPtrNull == nil, (*C.int)(C.globalVoidPtrSet) == &C.global) println("void* match:", voidPtr == nil, C.globalVoidPtrNull == nil, (*C.int)(C.globalVoidPtrSet) == &C.global)
println("int64_t match:", C.globalInt64 == C.int64_t(-(2<<40)))
// complex types // complex types
println("struct:", C.int(unsafe.Sizeof(C.globalStruct)) == C.globalStructSize, C.globalStruct.s, C.globalStruct.l, C.globalStruct.f) println("struct:", C.int(unsafe.Sizeof(C.globalStruct)) == C.globalStructSize, C.globalStruct.s, C.globalStruct.l, C.globalStruct.f)

10
testdata/cgo/main.h предоставленный
Просмотреть файл

@ -1,3 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
typedef short myint; typedef short myint;
int add(int a, int b); int add(int a, int b);
typedef int (*binop_t) (int, int); typedef int (*binop_t) (int, int);
@ -27,10 +30,10 @@ void unionSetShort(short s);
void unionSetFloat(float f); void unionSetFloat(float f);
void unionSetData(short f0, short f1, short f2); void unionSetData(short f0, short f1, short f2);
// test globals // test globals and datatypes
extern int global; extern int global;
extern _Bool globalBool; extern bool globalBool;
extern _Bool globalBool2; extern bool globalBool2;
extern float globalFloat; extern float globalFloat;
extern double globalDouble; extern double globalDouble;
extern _Complex float globalComplexFloat; extern _Complex float globalComplexFloat;
@ -39,6 +42,7 @@ extern _Complex double globalComplexLongDouble;
extern char globalChar; extern char globalChar;
extern void *globalVoidPtrSet; extern void *globalVoidPtrSet;
extern void *globalVoidPtrNull; extern void *globalVoidPtrNull;
extern int64_t globalInt64;
extern collection_t globalStruct; extern collection_t globalStruct;
extern int globalStructSize; extern int globalStructSize;
extern short globalArray[3]; extern short globalArray[3];

1
testdata/cgo/out.txt предоставленный
Просмотреть файл

@ -16,6 +16,7 @@ complex double: (+4.200000e+000+3.400000e+000i)
complex long double: (+4.300000e+000+3.500000e+000i) complex long double: (+4.300000e+000+3.500000e+000i)
char match: true char match: true
void* match: true true true void* match: true true true
int64_t match: true
struct: true 256 -123456 +3.140000e+000 struct: true 256 -123456 +3.140000e+000
array: 5 6 7 array: 5 6 7
union: true union: true