Commit 84485361 authored by Alberto García Hierro's avatar Alberto García Hierro Committed by Ian Lance Taylor

cmd/go: Add support for including C++ files in packages

* Add a CXXFiles field to Package, which includes .cc, .cpp and .cxx  files.
* CXXFiles are compiled using g++, which can be overridden using the CXX environment variable.
* Include .hh, .hpp and .hxx files in HFiles.
* Add support for CPPFLAGS (used for both C and C++) and CXXFLAGS (used only for C++) in cgo directive.
* Changed pkg-config cgo directive to modify CPPFLAGS rather than CFLAGS, so both C and C++ files get any flag returned by pkg-config --cflags.

Fixes #1476.

R=iant, r
CC=bradfitz, gobot, golang-dev, iant, minux.ma, remyoudompheng, seb.binet
https://golang.org/cl/8248043
parent ab5c762a
......@@ -24,7 +24,7 @@ the C parts of the package. For example:
// #include <errno.h>
import "C"
CFLAGS and LDFLAGS may be defined with pseudo #cgo directives
CPPFLAGS, CFLAGS, CXXFLAGS and LDFLAGS may be defined with pseudo #cgo directives
within these comments to tweak the behavior of gcc. Values defined
in multiple directives are concatenated together. Options prefixed
by $GOOS, $GOARCH, or $GOOS/$GOARCH are only defined in matching
......@@ -36,7 +36,7 @@ systems. For example:
// #include <png.h>
import "C"
Alternatively, CFLAGS and LDFLAGS may be obtained via the pkg-config
Alternatively, CPPFLAGS and LDFLAGS may be obtained via the pkg-config
tool using a '#cgo pkg-config:' directive followed by the package names.
For example:
......@@ -44,7 +44,7 @@ For example:
// #include <png.h>
import "C"
The CGO_CFLAGS and CGO_LDFLAGS environment variables are added
The CGO_CPPFLAGS, CGO_CFLAGS, CGO_CXXFLAGS and CGO_LDFLAGS environment variables are added
to the flags derived from these directives. Package-specific flags should
be set using the directives, not the environment variables, so that builds
work in unmodified environments.
......
This diff is collapsed.
......@@ -318,14 +318,17 @@ which calls strings.Join. The struct being passed to the template is:
CgoFiles []string // .go sources files that import "C"
IgnoredGoFiles []string // .go sources ignored due to build constraints
CFiles []string // .c source files
HFiles []string // .h source files
CXXFiles []string // .cc, .cxx and .cpp source files
HFiles []string // .h, .hh, .hpp and .hxx source files
SFiles []string // .s source files
SysoFiles []string // .syso object files to add to archive
SwigFiles []string // .swig files
SwigCXXFiles []string // .swigcxx files
// Cgo directives
CgoCPPFLAGS []string // cgo: flags for C preprocessor
CgoCFLAGS []string // cgo: flags for C compiler
CgoCXXFLAGS []string // cgo: flags for C++ compiler
CgoLDFLAGS []string // cgo: flags for linker
CgoPkgConfig []string // cgo: pkg-config names
......
......@@ -46,14 +46,17 @@ which calls strings.Join. The struct being passed to the template is:
CgoFiles []string // .go sources files that import "C"
IgnoredGoFiles []string // .go sources ignored due to build constraints
CFiles []string // .c source files
HFiles []string // .h source files
CXXFiles []string // .cc, .cxx and .cpp source files
HFiles []string // .h, .hh, .hpp and .hxx source files
SFiles []string // .s source files
SysoFiles []string // .syso object files to add to archive
SwigFiles []string // .swig files
SwigCXXFiles []string // .swigcxx files
// Cgo directives
CgoCPPFLAGS []string // cgo: flags for C preprocessor
CgoCFLAGS []string // cgo: flags for C compiler
CgoCXXFLAGS []string // cgo: flags for C++ compiler
CgoLDFLAGS []string // cgo: flags for linker
CgoPkgConfig []string // cgo: pkg-config names
......
......@@ -40,14 +40,17 @@ type Package struct {
CgoFiles []string `json:",omitempty"` // .go sources files that import "C"
IgnoredGoFiles []string `json:",omitempty"` // .go sources ignored due to build constraints
CFiles []string `json:",omitempty"` // .c source files
HFiles []string `json:",omitempty"` // .h source files
CXXFiles []string `json:",omitempty"` // .cc, .cpp and .cxx source files
HFiles []string `json:",omitempty"` // .h, .hh, .hpp and .hxx source files
SFiles []string `json:",omitempty"` // .s source files
SysoFiles []string `json:",omitempty"` // .syso system object files added to package
SwigFiles []string `json:",omitempty"` // .swig files
SwigCXXFiles []string `json:",omitempty"` // .swigcxx files
// Cgo directives
CgoCPPFLAGS []string `json:",omitempty"` // cgo: flags for C preprocessor
CgoCFLAGS []string `json:",omitempty"` // cgo: flags for C compiler
CgoCXXFLAGS []string `json:",omitempty"` // cgo: flags for C++ compiler
CgoLDFLAGS []string `json:",omitempty"` // cgo: flags for linker
CgoPkgConfig []string `json:",omitempty"` // cgo: pkg-config names
......@@ -98,12 +101,15 @@ func (p *Package) copyBuild(pp *build.Package) {
p.CgoFiles = pp.CgoFiles
p.IgnoredGoFiles = pp.IgnoredGoFiles
p.CFiles = pp.CFiles
p.CXXFiles = pp.CXXFiles
p.HFiles = pp.HFiles
p.SFiles = pp.SFiles
p.SysoFiles = pp.SysoFiles
p.SwigFiles = pp.SwigFiles
p.SwigCXXFiles = pp.SwigCXXFiles
p.CgoCPPFLAGS = pp.CgoCPPFLAGS
p.CgoCFLAGS = pp.CgoCFLAGS
p.CgoCXXFLAGS = pp.CgoCXXFLAGS
p.CgoLDFLAGS = pp.CgoLDFLAGS
p.CgoPkgConfig = pp.CgoPkgConfig
p.Imports = pp.Imports
......@@ -389,6 +395,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
p.CgoFiles,
p.IgnoredGoFiles,
p.CFiles,
p.CXXFiles,
p.HFiles,
p.SFiles,
p.SysoFiles,
......@@ -481,6 +488,11 @@ func (p *Package) usesSwig() bool {
return len(p.SwigFiles) > 0 || len(p.SwigCXXFiles) > 0
}
// usesCgo returns whether the package needs to run cgo
func (p *Package) usesCgo() bool {
return len(p.CgoFiles) > 0
}
// swigSoname returns the name of the shared library we create for a
// SWIG input file.
func (p *Package) swigSoname(file string) string {
......@@ -611,7 +623,7 @@ func isStale(p *Package, topRoot map[string]bool) bool {
return false
}
srcs := stringList(p.GoFiles, p.CFiles, p.HFiles, p.SFiles, p.CgoFiles, p.SysoFiles)
srcs := stringList(p.GoFiles, p.CFiles, p.CXXFiles, p.HFiles, p.SFiles, p.CgoFiles, p.SysoFiles)
for _, src := range srcs {
if olderThan(filepath.Join(p.Dir, src)) {
return true
......
......@@ -657,10 +657,16 @@ func (x *Indexer) addFile(filename string, goFile bool) (file *token.File, ast *
var whitelisted = map[string]bool{
".bash": true,
".c": true,
".cc": true,
".cpp": true,
".cxx": true,
".css": true,
".go": true,
".goc": true,
".h": true,
".hh": true,
".hpp": true,
".hxx": true,
".html": true,
".js": true,
".out": true,
......
......@@ -353,16 +353,19 @@ type Package struct {
CgoFiles []string // .go source files that import "C"
IgnoredGoFiles []string // .go source files ignored for this build
CFiles []string // .c source files
HFiles []string // .h source files
CXXFiles []string // .cc, .cpp and .cxx source files
HFiles []string // .h, .hh, .hpp and .hxx source files
SFiles []string // .s source files
SysoFiles []string // .syso system object files to add to archive
SwigFiles []string // .swig files
SwigCXXFiles []string // .swigcxx files
// Cgo directives
CgoPkgConfig []string // Cgo pkg-config directives
CgoCPPFLAGS []string // Cgo CPPFLAGS directives
CgoCFLAGS []string // Cgo CFLAGS directives
CgoCXXFLAGS []string // Cgo CXXFLAGS directives
CgoLDFLAGS []string // Cgo LDFLAGS directives
CgoPkgConfig []string // Cgo pkg-config directives
// Dependency information
Imports []string // imports from GoFiles, CgoFiles
......@@ -600,7 +603,7 @@ Found:
}
switch ext {
case ".go", ".c", ".s", ".h", ".S", ".swig", ".swigcxx":
case ".go", ".c", ".cc", ".cxx", ".cpp", ".s", ".h", ".hh", ".hpp", ".hxx", ".S", ".swig", ".swigcxx":
// tentatively okay - read to make sure
case ".syso":
// binary objects to add to package archive
......@@ -643,7 +646,10 @@ Found:
case ".c":
p.CFiles = append(p.CFiles, name)
continue
case ".h":
case ".cc", ".cpp", ".cxx":
p.CXXFiles = append(p.CXXFiles, name)
continue
case ".h", ".hh", ".hpp", ".hxx":
p.HFiles = append(p.HFiles, name)
continue
case ".s":
......@@ -851,8 +857,8 @@ func (ctxt *Context) shouldBuild(content []byte) bool {
}
// saveCgo saves the information from the #cgo lines in the import "C" comment.
// These lines set CFLAGS and LDFLAGS and pkg-config directives that affect
// the way cgo's C code is built.
// These lines set CPPCFLAGS, CFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives
// that affect the way cgo's C code is built.
//
// TODO(rsc): This duplicates code in cgo.
// Once the dust settles, remove this code from cgo.
......@@ -910,6 +916,10 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
switch verb {
case "CFLAGS":
di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
case "CPPFLAGS":
di.CgoCPPFLAGS = append(di.CgoCPPFLAGS, args...)
case "CXXFLAGS":
di.CgoCXXFLAGS = append(di.CgoCXXFLAGS, args...)
case "LDFLAGS":
di.CgoLDFLAGS = append(di.CgoLDFLAGS, args...)
case "pkg-config":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment