cgo: add package directory to header include paths
Этот коммит содержится в:
родитель
c6069476a7
коммит
6cacafb8dc
8 изменённых файлов: 25 добавлений и 8 удалений
|
@ -32,6 +32,7 @@ func init() {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Triple string // LLVM target triple, e.g. x86_64-unknown-linux-gnu (empty string means default)
|
Triple string // LLVM target triple, e.g. x86_64-unknown-linux-gnu (empty string means default)
|
||||||
GC string // garbage collection strategy
|
GC string // garbage collection strategy
|
||||||
|
CFlags []string // flags to pass to cgo
|
||||||
DumpSSA bool // dump Go SSA, for compiler debugging
|
DumpSSA bool // dump Go SSA, for compiler debugging
|
||||||
Debug bool // add debug symbols for gdb
|
Debug bool // add debug symbols for gdb
|
||||||
RootDir string // GOROOT for TinyGo
|
RootDir string // GOROOT for TinyGo
|
||||||
|
@ -208,7 +209,8 @@ 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,
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(mainPath, ".go") {
|
if strings.HasSuffix(mainPath, ".go") {
|
||||||
_, err = lprogram.ImportFile(mainPath)
|
_, err = lprogram.ImportFile(mainPath)
|
||||||
|
|
|
@ -73,7 +73,7 @@ typedef unsigned long long _Cgo_ulonglong;
|
||||||
|
|
||||||
// processCgo extracts the `import "C"` statement from the AST, parses the
|
// processCgo extracts the `import "C"` statement from the AST, parses the
|
||||||
// comment with libclang, and modifies the AST to use this information.
|
// comment with libclang, and modifies the AST to use this information.
|
||||||
func (p *Package) processCgo(filename string, f *ast.File) error {
|
func (p *Package) processCgo(filename string, f *ast.File, cflags []string) error {
|
||||||
info := &fileInfo{
|
info := &fileInfo{
|
||||||
File: f,
|
File: f,
|
||||||
filename: filename,
|
filename: filename,
|
||||||
|
@ -106,7 +106,7 @@ func (p *Package) processCgo(filename string, f *ast.File) error {
|
||||||
// source location.
|
// source location.
|
||||||
info.importCPos = spec.Path.ValuePos
|
info.importCPos = spec.Path.ValuePos
|
||||||
|
|
||||||
err = info.parseFragment(cgoComment + cgoTypes)
|
err = info.parseFragment(cgoComment+cgoTypes, cflags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ import "C"
|
||||||
|
|
||||||
var globalFileInfo *fileInfo
|
var globalFileInfo *fileInfo
|
||||||
|
|
||||||
func (info *fileInfo) parseFragment(fragment string) error {
|
func (info *fileInfo) parseFragment(fragment string, cflags []string) error {
|
||||||
index := C.clang_createIndex(0, 1)
|
index := C.clang_createIndex(0, 1)
|
||||||
defer C.clang_disposeIndex(index)
|
defer C.clang_disposeIndex(index)
|
||||||
|
|
||||||
|
@ -36,11 +36,21 @@ func (info *fileInfo) parseFragment(fragment string) error {
|
||||||
Contents: fragmentC,
|
Contents: fragmentC,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convert Go slice of strings to C array of strings.
|
||||||
|
cmdargsC := C.malloc(C.size_t(len(cflags)) * C.size_t(unsafe.Sizeof(uintptr(0))))
|
||||||
|
defer C.free(cmdargsC)
|
||||||
|
cmdargs := (*[1<<30 - 1]*C.char)(cmdargsC)
|
||||||
|
for i, cflag := range cflags {
|
||||||
|
s := C.CString(cflag)
|
||||||
|
cmdargs[i] = s
|
||||||
|
defer C.free(unsafe.Pointer(s))
|
||||||
|
}
|
||||||
|
|
||||||
var unit C.CXTranslationUnit
|
var unit C.CXTranslationUnit
|
||||||
errCode := C.clang_parseTranslationUnit2(
|
errCode := C.clang_parseTranslationUnit2(
|
||||||
index,
|
index,
|
||||||
filenameC,
|
filenameC,
|
||||||
(**C.char)(unsafe.Pointer(uintptr(0))), 0, // command line args
|
(**C.char)(cmdargsC), C.int(len(cflags)), // command line args
|
||||||
&unsavedFile, 1, // unsaved files
|
&unsavedFile, 1, // unsaved files
|
||||||
C.CXTranslationUnit_None,
|
C.CXTranslationUnit_None,
|
||||||
&unit)
|
&unit)
|
||||||
|
|
|
@ -20,6 +20,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)
|
||||||
|
CFlags []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package holds a loaded package, its imports, and its parsed files.
|
// Package holds a loaded package, its imports, and its parsed files.
|
||||||
|
@ -290,7 +291,7 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
|
||||||
fileErrs = append(fileErrs, err)
|
fileErrs = append(fileErrs, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err = p.processCgo(path, f)
|
err = p.processCgo(path, f, append(p.CFlags, "-I"+p.Package.Dir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fileErrs = append(fileErrs, err)
|
fileErrs = append(fileErrs, err)
|
||||||
continue
|
continue
|
||||||
|
|
1
main.go
1
main.go
|
@ -44,6 +44,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
|
||||||
compilerConfig := compiler.Config{
|
compilerConfig := compiler.Config{
|
||||||
Triple: spec.Triple,
|
Triple: spec.Triple,
|
||||||
GC: config.gc,
|
GC: config.gc,
|
||||||
|
CFlags: spec.CFlags,
|
||||||
Debug: config.debug,
|
Debug: config.debug,
|
||||||
DumpSSA: config.dumpSSA,
|
DumpSSA: config.dumpSSA,
|
||||||
RootDir: sourceDir(),
|
RootDir: sourceDir(),
|
||||||
|
|
2
testdata/cgo/main.c
предоставленный
2
testdata/cgo/main.c
предоставленный
|
@ -1,3 +1,5 @@
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
int fortytwo() {
|
int fortytwo() {
|
||||||
return 42;
|
return 42;
|
||||||
}
|
}
|
||||||
|
|
3
testdata/cgo/main.go
предоставленный
3
testdata/cgo/main.go
предоставленный
|
@ -2,8 +2,7 @@ package main
|
||||||
|
|
||||||
/*
|
/*
|
||||||
int fortytwo(void);
|
int fortytwo(void);
|
||||||
typedef short myint;
|
#include "main.h"
|
||||||
int add(int a, int b);
|
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
|
2
testdata/cgo/main.h
предоставленный
Обычный файл
2
testdata/cgo/main.h
предоставленный
Обычный файл
|
@ -0,0 +1,2 @@
|
||||||
|
typedef short myint;
|
||||||
|
int add(int a, int b);
|
Загрузка…
Создание таблицы
Сослаться в новой задаче