@@ -66,71 +66,16 @@ func cname(s string) string {
6666 return s
6767}
6868
69- // ParseFlags extracts #cgo CFLAGS and LDFLAGS options from the file
70- // preamble. Multiple occurrences are concatenated with a separating space,
71- // even across files .
72- func (p * Package ) ParseFlags ( f * File , srcfile string ) {
69+ // DiscardCgoDirectives processes the import C preamble, and discards
70+ // all #cgo CFLAGS and LDFLAGS directives, so they don't make their
71+ // way into _cgo_export.h .
72+ func (f * File ) DiscardCgoDirectives ( ) {
7373 linesIn := strings .Split (f .Preamble , "\n " )
7474 linesOut := make ([]string , 0 , len (linesIn ))
75-
76- NextLine:
7775 for _ , line := range linesIn {
7876 l := strings .TrimSpace (line )
7977 if len (l ) < 5 || l [:4 ] != "#cgo" || ! unicode .IsSpace (rune (l [4 ])) {
8078 linesOut = append (linesOut , line )
81- continue
82- }
83-
84- l = strings .TrimSpace (l [4 :])
85- fields := strings .SplitN (l , ":" , 2 )
86- if len (fields ) != 2 {
87- fatalf ("%s: bad #cgo line: %s" , srcfile , line )
88- }
89-
90- var k string
91- kf := strings .Fields (fields [0 ])
92- switch len (kf ) {
93- case 1 :
94- k = kf [0 ]
95- case 2 :
96- k = kf [1 ]
97- switch kf [0 ] {
98- case goos :
99- case goarch :
100- case goos + "/" + goarch :
101- default :
102- continue NextLine
103- }
104- default :
105- fatalf ("%s: bad #cgo option: %s" , srcfile , fields [0 ])
106- }
107-
108- args , err := splitQuoted (fields [1 ])
109- if err != nil {
110- fatalf ("%s: bad #cgo option %s: %s" , srcfile , k , err )
111- }
112- for _ , arg := range args {
113- if ! safeName (arg ) {
114- fatalf ("%s: #cgo option %s is unsafe: %s" , srcfile , k , arg )
115- }
116- }
117-
118- switch k {
119-
120- case "CFLAGS" , "LDFLAGS" :
121- p .addToFlag (k , args )
122-
123- case "pkg-config" :
124- cflags , ldflags , err := pkgConfig (args )
125- if err != nil {
126- fatalf ("%s: bad #cgo option %s: %s" , srcfile , k , err )
127- }
128- p .addToFlag ("CFLAGS" , cflags )
129- p .addToFlag ("LDFLAGS" , ldflags )
130-
131- default :
132- fatalf ("%s: unsupported #cgo option %s" , srcfile , k )
133-
13479 }
13580 }
13681 f .Preamble = strings .Join (linesOut , "\n " )
@@ -146,36 +91,6 @@ func (p *Package) addToFlag(flag string, args []string) {
14691 }
14792}
14893
149- // pkgConfig runs pkg-config and extracts --libs and --cflags information
150- // for packages.
151- func pkgConfig (packages []string ) (cflags , ldflags []string , err error ) {
152- for _ , name := range packages {
153- if len (name ) == 0 || name [0 ] == '-' {
154- return nil , nil , errors .New (fmt .Sprintf ("invalid name: %q" , name ))
155- }
156- }
157-
158- args := append ([]string {"pkg-config" , "--cflags" }, packages ... )
159- stdout , stderr , ok := run (nil , args )
160- if ! ok {
161- os .Stderr .Write (stderr )
162- return nil , nil , errors .New ("pkg-config failed" )
163- }
164- cflags , err = splitQuoted (string (stdout ))
165- if err != nil {
166- return
167- }
168-
169- args = append ([]string {"pkg-config" , "--libs" }, packages ... )
170- stdout , stderr , ok = run (nil , args )
171- if ! ok {
172- os .Stderr .Write (stderr )
173- return nil , nil , errors .New ("pkg-config failed" )
174- }
175- ldflags , err = splitQuoted (string (stdout ))
176- return
177- }
178-
17994// splitQuoted splits the string s around each instance of one or more consecutive
18095// white space characters while taking into account quotes and escaping, and
18196// returns an array of substrings of s or an empty list if s contains only white space.
0 commit comments