Skip to content

makefile:use local gcc when native compiling #3774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ningmingxiao
Copy link
Contributor

@ningmingxiao ningmingxiao commented Jul 30, 2025

fix

cgo: C compiler "x86_64-linux-gnu-gcc" not found: exec: "x86_64-linux-gnu-gcc": executable file not found in $PATH

@ningmingxiao ningmingxiao changed the title Local build support to build with flag CROSS_COMPILE Jul 30, 2025
@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

The idea is OK, but it should not need an extra variable to be set...

Maybe check go env GOOS (without GOOS set) versus $GOOS?

@ningmingxiao
Copy link
Contributor Author

ningmingxiao commented Jul 30, 2025

can you describe clearly ? thanks @afbjorklund

@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

We should be able to detect when doing a cross-compile, without requiring that information from the user?

The main issue is that the format of uname -s and GOOS is slightly different (as with uname -m and GOARCH)

Most Linux distributions feature both, so it has not been a problem.

i.e. both gcc and x86_64-linux-gnu-gcc

@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

Ah, we already have some handy variables to use:

# returns difference between GOOS GOARCH and GOHOSTOS GOHOSTARCH.
cross_compiling = $(call diff,$(GOOS) $(GOARCH),$(GOHOSTOS) $(GOHOSTARCH))
# returns true if cross_compiling is empty.
native_compiling = $(if $(cross_compiling),,true)

We don't need to set anything, for a native build.

i.e. there is no need to hardcode gcc, but let go

@ningmingxiao
Copy link
Contributor Author

ningmingxiao commented Jul 30, 2025

how about

CC = $(call to_uname_m,$(GOARCH))-linux-gnu-gcc
ifeq ($(native_compiling),true)
	CC = gcc
endif

my host doesn't have x86_64-linux-gnu-gcc
and some os have
x86_64-anolis-linux-gcc instead of x86_64-linux-gnu-gcc

@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

If your host needs something special, you can override CC

make GOOS=linux GOARCH=... CC=my-gcc

But you can simplify it to:

ifeq ($(cross_compiling),true)
# CC is required for cross-compiling on Linux.
CC = $(call to_uname_m,$(GOARCH))-linux-gnu-gcc
endif

Might need to move the declarations in the file, though.

EDIT: typo

Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
@ningmingxiao
Copy link
Contributor Author

done, thank you.

@ningmingxiao ningmingxiao changed the title support to build with flag CROSS_COMPILE makefile:use local gcc when native compiling Jul 30, 2025
@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

Tests are failing, so the new test is not working properly. (or the comment is wrong, and it always need CC?)

@afbjorklund
Copy link
Member

afbjorklund commented Jul 30, 2025

The cross_compiling variable is empty when false (and the diff, when true), unlike native_compiling <sigh>

+ifneq ($(cross_compiling),)
 # CC is required for cross-compiling on Linux.
 CC = $(call to_uname_m,$(GOARCH))-linux-gnu-gcc
+endif

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.

2 participants