cgo: add support for CFLAGS in .c files

This patch adds support for passing CFLAGS added in #cgo lines of the
CGo preprocessing phase to the compiler when compiling C files inside
packages. This is expected and convenient but didn't work before.
This commit is contained in:
Ayke van Laethem
2021-03-12 17:47:06 +01:00
committed by Ron Evans
parent 896a848001
commit 1bed192de0
9 changed files with 38 additions and 25 deletions
+3 -2
View File
@@ -430,7 +430,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
job := &compileJob{
description: "compile extra file " + path,
run: func(job *compileJob) error {
result, err := compileAndCacheCFile(abspath, dir, config)
result, err := compileAndCacheCFile(abspath, dir, config.CFlags(), config)
job.result = result
return err
},
@@ -443,12 +443,13 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// TODO: do this as part of building the package to be able to link the
// bitcode files together.
for _, pkg := range lprogram.Sorted() {
pkg := pkg
for _, filename := range pkg.CFiles {
abspath := filepath.Join(pkg.Dir, filename)
job := &compileJob{
description: "compile CGo file " + abspath,
run: func(job *compileJob) error {
result, err := compileAndCacheCFile(abspath, dir, config)
result, err := compileAndCacheCFile(abspath, dir, pkg.CFlags, config)
job.result = result
return err
},
+2 -2
View File
@@ -57,7 +57,7 @@ import (
// depfile but without invalidating its name. For this reason, the depfile is
// written on each new compilation (even when it seems unnecessary). However, it
// could in rare cases lead to a stale file fetched from the cache.
func compileAndCacheCFile(abspath, tmpdir string, config *compileopts.Config) (string, error) {
func compileAndCacheCFile(abspath, tmpdir string, cflags []string, config *compileopts.Config) (string, error) {
// Hash input file.
fileHash, err := hashFile(abspath)
if err != nil {
@@ -121,7 +121,7 @@ func compileAndCacheCFile(abspath, tmpdir string, config *compileopts.Config) (s
return "", err
}
depTmpFile.Close()
flags := config.CFlags()
flags := append([]string{}, cflags...) // copy cflags
flags = append(flags, "-MD", "-MV", "-MTdeps", "-MF", depTmpFile.Name()) // autogenerate dependencies
flags = append(flags, "-c", "-o", objTmpFile.Name(), abspath)
if config.Options.PrintCommands {