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