cgo: add package directory to header include paths

This commit is contained in:
Ayke van Laethem
2018-11-30 13:01:20 +01:00
parent c6069476a7
commit 6cacafb8dc
8 changed files with 25 additions and 8 deletions
+3 -1
View File
@@ -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
@@ -208,7 +209,8 @@ func (c *Compiler) Compile(mainPath string) error {
MaxAlign: int64(c.targetData.PrefTypeAlignment(c.i8ptrType)),
},
},
Dir: wd,
Dir: wd,
CFlags: c.CFlags,
}
if strings.HasSuffix(mainPath, ".go") {
_, err = lprogram.ImportFile(mainPath)
+2 -2
View File
@@ -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
}
+12 -2
View File
@@ -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)
+2 -1
View File
@@ -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
+1
View File
@@ -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
View File
@@ -1,3 +1,5 @@
#include "main.h"
int fortytwo() {
return 42;
}
+1 -2
View File
@@ -2,8 +2,7 @@ package main
/*
int fortytwo(void);
typedef short myint;
int add(int a, int b);
#include "main.h"
*/
import "C"
+2
View File
@@ -0,0 +1,2 @@
typedef short myint;
int add(int a, int b);