Skip to content
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

proposal: cmd/go: permit C files when not using cgo or SWIG #69639

Open
qmuntal opened this issue Sep 26, 2024 · 2 comments
Open

proposal: cmd/go: permit C files when not using cgo or SWIG #69639

qmuntal opened this issue Sep 26, 2024 · 2 comments
Labels
Milestone

Comments

@qmuntal
Copy link
Contributor

qmuntal commented Sep 26, 2024

Proposal Details

Background

The Go compiler doesn't allow to have C, C++, Objectice-C, and Fortran files in a package unless cgo or SWIG are used. This means that if a Go package contains any of these files, then CGO_ENABLED can't be 0 and there should be at least one Go file with the import "C" statement.

This restriction was added in Go 1.4 in a0785a5. My understanding is that its intention was to prohibit people from using the 6c compiler and guide them to cgo instead.

Proposal

Now that 6c nobody even knows what is is the 6c compiler, I propose to lift the aforementioned restriction.

Motivation

The main motivation here is to allow C files not used in cgo to be copied into the Go vendor directory. Note that only directories with at least one Go file are eligible to be copied to the vendor directory. This conflicts with the requirement of using having to use cgo when Go and C files coexist in the same directory, as cgo might not be desired to avoid compiling unnecessary files or even having runtime dependencies (i.e. C header) available at build time.

There is at least one real project that needs this: microsoft/retina has some Go packages that only exist to hold C files not used with cgo, but that are compiled on-the-fly as bpf objects so that the builder image doesn't require all the BPF machinery installed and to facilitate cross-compilations. These object will then be loaded and attached to the Linux kernel.

Currently microsoft/retina builds with CGO_ENABLED=0 to workaround the Go compiler restrictions, but in the near future cgo will have to be enabled due to other requirements, so this workaround won't work anymore.

@gopherbot gopherbot added this to the Proposal milestone Sep 26, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Sep 26, 2024
@mharbison72
Copy link
Contributor

FWIW, you can have *.c files in a package without enabling cgo if you put a build tag in the C file. That compiled the C file (or ignored it) based on CGO_ENABLED=x and the platform.

I never saw build tags in C files in the documentation- somebody replied to a question on SO about the error you mentioned by suggesting an "ignored" build tag, and I trial and errored from there. I have no idea how that will interact with vendoring code though. I agree that the error emitted without the build tag isn't helpful in figuring this out.

timraymond added a commit to microsoft/retina that referenced this issue Nov 5, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
github-merge-queue bot pushed a commit to microsoft/retina that referenced this issue Nov 11, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
github-merge-queue bot pushed a commit to microsoft/retina that referenced this issue Nov 11, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
timraymond added a commit to microsoft/retina that referenced this issue Nov 13, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
timraymond added a commit to microsoft/retina that referenced this issue Nov 13, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
github-merge-queue bot pushed a commit to microsoft/retina that referenced this issue Nov 15, 2024
It will soon become necessary to enable CGO in builds in order to use
the MS Go distribution. Disabling CGO was always somewhat of a hack
since we didn't need it anyway for eBPF. Now that we do, another
solution is necessary. This uses the `//go:build ignore` directive to
exclude all C source files from the Go toolchain. This is necessary even
within C source files even though these C source files exist within an
underscore-prefixed directory. Go's behavior here is likely erroneous,
and an issue has been filed for its repair:
golang/go#69639
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants