Skip to content

add CGO_CFLAGS support#5453

Open
hectorchu wants to merge 2 commits into
tinygo-org:devfrom
hectorchu:cgo_cflags
Open

add CGO_CFLAGS support#5453
hectorchu wants to merge 2 commits into
tinygo-org:devfrom
hectorchu:cgo_cflags

Conversation

@hectorchu

Copy link
Copy Markdown

Allow CGO_CFLAGS to be set when running tinygo. Usually needed for specifying alternative libc includes.

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dgryski

dgryski commented Jun 8, 2026

Copy link
Copy Markdown
Member

Is this going to introduce any security issues? If we're pulling it purely from the users environment we're probably fine, but I just want to make sure we're not introducing any issues where additional flags are pulled from the cgo files in a way that could lead to local execution.

@hectorchu

Copy link
Copy Markdown
Author

No, it purely gets the env variable from the OS environment. This probably does not include anything from cgo files.

Comment thread loader/loader.go Outdated
@hectorchu hectorchu requested a review from deadprogram June 10, 2026 09:10

@aykevl aykevl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding CFLAGS splitting, I'm not convinced a simple strings.Fields is sufficient.
Take for example this code:

package main

// int x = FOO;
import "C"

func main() {
        println(C.x);
}

This works:

$ CGO_CFLAGS='-DFOO=3+3' go run .
6

This does not (which makes sense):

 CGO_CFLAGS='-DFOO=3 + 3' go run .
# runtime/cgo
gcc: warning: +: linker input file unused because linking not done
gcc: error: +: linker input file not found: No such file or directory
gcc: warning: 3: linker input file unused because linking not done
gcc: error: 3: linker input file not found: No such file or directory

...but this works, suggesting that it does take the quotes into account:

$ CGO_CFLAGS='"-DFOO=3 + 3"' go run .
6

It's probably a better idea to use the shlex module instead to split these values (also used in the cgo package for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants