CFLAGS 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. For example:
+in multiple directives are concatenated together. Options prefixed
+by $GOOS, $GOARCH, or $GOOS/$GOARCH are only defined in matching
+systems. For example:
// #cgo CFLAGS: -DPNG_DEBUG=1
+ // #cgo linux CFLAGS: -DLINUX=1
// #cgo LDFLAGS: -lpng
// #include <png.h>
import "C"
"go/parser"
"go/token"
"os"
+ "runtime"
"strconv"
"strings"
"unicode"
func (p *Package) ParseFlags(f *File, srcfile string) {
linesIn := strings.Split(f.Preamble, "\n", -1)
linesOut := make([]string, 0, len(linesIn))
+
+NextLine:
for _, line := range linesIn {
l := strings.TrimSpace(line)
if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(int(l[4])) {
fatal("%s: bad #cgo line: %s", srcfile, line)
}
- k := fields[0]
- v := strings.TrimSpace(fields[1])
+ var k string
+ kf := strings.Fields(fields[0])
+ switch len(kf) {
+ case 1:
+ k = kf[0]
+ case 2:
+ k = kf[1]
+ switch kf[0] {
+ case runtime.GOOS:
+ case runtime.GOARCH:
+ case runtime.GOOS + "/" + runtime.GOARCH:
+ default:
+ continue NextLine
+ }
+ default:
+ fatal("%s: bad #cgo option: %s", srcfile, fields[0])
+ }
+
if k != "CFLAGS" && k != "LDFLAGS" {
fatal("%s: unsupported #cgo option %s", srcfile, k)
}
+
+ v := strings.TrimSpace(fields[1])
args, err := splitQuoted(v)
if err != nil {
fatal("%s: bad #cgo option %s: %s", srcfile, k, err.String())