cgo: add package directory to header include paths

Этот коммит содержится в:
Ayke van Laethem 2018-11-30 13:01:20 +01:00
родитель c6069476a7
коммит 6cacafb8dc
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
8 изменённых файлов: 25 добавлений и 8 удалений

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

@ -32,6 +32,7 @@ func init() {
type Config struct {
Triple string // LLVM target triple, e.g. x86_64-unknown-linux-gnu (empty string means default)
GC string // garbage collection strategy
CFlags []string // flags to pass to cgo
DumpSSA bool // dump Go SSA, for compiler debugging
Debug bool // add debug symbols for gdb
RootDir string // GOROOT for TinyGo
@ -209,6 +210,7 @@ func (c *Compiler) Compile(mainPath string) error {
},
},
Dir: wd,
CFlags: c.CFlags,
}
if strings.HasSuffix(mainPath, ".go") {
_, 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
// 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{
File: f,
filename: filename,
@ -106,7 +106,7 @@ func (p *Package) processCgo(filename string, f *ast.File) error {
// source location.
info.importCPos = spec.Path.ValuePos
err = info.parseFragment(cgoComment + cgoTypes)
err = info.parseFragment(cgoComment+cgoTypes, cflags)
if err != nil {
return err
}

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

@ -20,7 +20,7 @@ import "C"
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)
defer C.clang_disposeIndex(index)
@ -36,11 +36,21 @@ func (info *fileInfo) parseFragment(fragment string) error {
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
errCode := C.clang_parseTranslationUnit2(
index,
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
C.CXTranslationUnit_None,
&unit)

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

@ -20,6 +20,7 @@ type Program struct {
fset *token.FileSet
TypeChecker types.Config
Dir string // current working directory (for error reporting)
CFlags []string
}
// 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)
continue
}
err = p.processCgo(path, f)
err = p.processCgo(path, f, append(p.CFlags, "-I"+p.Package.Dir))
if err != nil {
fileErrs = append(fileErrs, err)
continue

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

@ -44,6 +44,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
compilerConfig := compiler.Config{
Triple: spec.Triple,
GC: config.gc,
CFlags: spec.CFlags,
Debug: config.debug,
DumpSSA: config.dumpSSA,
RootDir: sourceDir(),

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

@ -1,3 +1,5 @@
#include "main.h"
int fortytwo() {
return 42;
}

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

@ -2,8 +2,7 @@ package main
/*
int fortytwo(void);
typedef short myint;
int add(int a, int b);
#include "main.h"
*/
import "C"

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

@ -0,0 +1,2 @@
typedef short myint;
int add(int a, int b);