mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
add CGO_CFLAGS support (#5453)
* add CGO_CFLAGS support * the environment variable must be split * use shlex to handle double quotes * wrap error
This commit is contained in:
@@ -151,6 +151,8 @@ func Get(name string) string {
|
||||
panic("could not find cache dir: " + err.Error())
|
||||
}
|
||||
return filepath.Join(dir, "tinygo")
|
||||
case "CGO_CFLAGS":
|
||||
return os.Getenv("CGO_CFLAGS")
|
||||
case "CGO_ENABLED":
|
||||
// Always enable CGo. It is required by a number of targets, including
|
||||
// macOS and the rp2040.
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/google/shlex"
|
||||
"github.com/tinygo-org/tinygo/cgo"
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"github.com/tinygo-org/tinygo/goenv"
|
||||
@@ -506,6 +507,11 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
|
||||
var initialCFlags []string
|
||||
initialCFlags = append(initialCFlags, p.program.config.CFlags(true)...)
|
||||
initialCFlags = append(initialCFlags, "-I"+p.Dir)
|
||||
cgoCFlags, err := shlex.Split(goenv.Get("CGO_CFLAGS"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to split CGO_CFLAGS: %w", err)
|
||||
}
|
||||
initialCFlags = append(initialCFlags, cgoCFlags...)
|
||||
generated, headerCode, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.ImportPath, p.program.fset, initialCFlags, p.program.config.GOOS())
|
||||
p.CFlags = append(initialCFlags, cflags...)
|
||||
p.CGoHeaders = headerCode
|
||||
|
||||
Reference in New Issue
Block a user