Emit correct header in CGo c-archive#2874
Conversation
044427e to
06ef45c
Compare
|
I'd rather not merge a fix for this without an issue thoroughly explaining the bug being fixed plus tests that verify it works. cgo export headers are not well documented. The main issue about this is #2131. |
|
+1, I would really only be comfortable merging this if there were a testcase that failed before but passes now |
|
My own example is not public and quite difficult to minimize (cc target depending on go target depending on cc target depending on go target), but #2878 now provides a public example. I am unfortunately not sure how to write a reliable test for this issue. After all, the problem is that without this fix, @achew22 Do you have an idea how to deal with this situation? |
|
@jayconrod @achew22 Do you want me to add anything before you take another look at this? With #2878, there is now an example of a build that fails without this patch. |
|
That's great! I think next steps here before merging would be to add a testcase into the repo that demonstrates this behavior so that the purpose of this change is effectively documented. Thanks! |
06ef45c to
21ebb95
Compare
|
@achew22 Done, I have turned @glukasiknuro's example into a testcase that does compile and run with this PR, but fails to compile without it. Please let me know if you need anything else. |
2f1802b to
fc53cd9
Compare
|
Huh, looking at the test output I'm seeing: |
fc53cd9 to
af76cd7
Compare
Yes, I forgot to load |
af76cd7 to
e4e7222
Compare
|
@achew22 Sorry, I didn't notice that the CI checks were failing. This PR is ready for review now. The race detector check is failing on Windows though, do you know whether this is related to my changes? |
I don't think it's related to your changes. We have disabled it on |
b2cb6d3 to
af83807
Compare
|
@steeve That worked, thanks. Should be fully ready now. |
|
@achew22 The tests are all passing now, could you take another look? |
|
I resolved the merge conflict. |
|
This would fix #2971 |
A go_binary target with linkmode c-archive emits only a single _cgo_exports.h header instead of all such headers of all transitive dependencies. Currently, this header is taken to be the first element in a list obtained from the depset of these transitive _cgo_exports.h files via to_list(). While deterministic, this choice is not guaranteed to give the correct _cgo_exports.h (i.e., the one of the go_binary itself) as the depset iteration order is not specified. This commit ensures that the _cgo_exports.h of the target itself is added as the first element of the `direct` list of a depset with iteration order specified as `preorder` and will thus always be the first element when the depset is converted to a list. The commit adds a testcase that does not compile without it: If a Go library that depends on a CGo library (in this case x/sys/unix) is itself depended on by a cc_library as a c-archive, the emitted CGo header is that of the CGo library, not the Go library itself.
f51820a to
72319b3
Compare
|
@achew22 Do you think you could take a look? It seems that people keep hitting the cgo bug this fixes. |
This PR removes all of the custom version parsing functions where we try to make sense about the version (e.g. extracting major/minor versions). Whilst doing this I actually think that I made it easier to support bazel-contrib#2837.
What type of PR is this?
Bug fix
What does this PR do? Why is it needed?
A go_binary target with linkmode c-archive emits only a single
_cgo_exports.hheader instead of all such headers of all transitive dependencies.Currently, this header is taken to be the first element in a list obtained from the depset of these transitive
_cgo_exports.hfilesvia
to_list(). While deterministic, this choice is not guaranteed to give the correct_cgo_exports.h(i.e., the one of the go_binary itself) as the depset iteration order is not specified.This commit ensures that the
_cgo_exports.hof the target itself is added as the first element of thedirectlist of a depset with iteration order specified as"preorder"and will thus always be the first element when the depset is converted to a list.Which issues(s) does this PR fix?
The
CcInfohdrsexposed by ago_binaryusing CGo that itself has dependencies using CGo is essentially arbitrary (but deterministic) and thus may not define the exported symbols or even be empty.