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

Make go_binary expose CcInfo #2445

Merged
merged 26 commits into from Mar 5, 2021
Merged

Make go_binary expose CcInfo #2445

merged 26 commits into from Mar 5, 2021

Conversation

steeve
Copy link
Contributor

@steeve steeve commented Apr 19, 2020

This PR makes go_binary propagate CcInfo.
This is an early draft to get the discussion going.

I'm quite happy so far it's pretty easy.

That said, I thought I would be able to have one CcInfo that exports both static and shared binaries, but I think that's not possible since it needs a transition.

Also, don't mind the cfg = "host" commit, I need it to test it for android rules.

At this moment, using c-shared makes Bazel 3.0.0 crash:

Expand
$ bazel test //tests/core/c_linkmodes:c-shared_test
FAILED: Build did NOT complete successfully (9 packages loaded, 6559 targets configured)
Internal error thrown during build. Printing stack trace: java.lang.RuntimeException: Unrecoverable error while evaluating node '//:ccbin BuildConfigurationValue.Key[993a664396fa04af94b42a9bca19b9fad7eb40aeb138000ab6e9ffc0e5219538] false' (requested by nodes )
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:515)
	at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:399)
	at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
	at com.google.devtools.build.lib.vfs.PathFragment.startsWith(PathFragment.java:299)
	at com.google.devtools.build.lib.rules.cpp.LibrariesToLinkCollector.addLinkerInputs(LibrariesToLinkCollector.java:247)
	at com.google.devtools.build.lib.rules.cpp.LibrariesToLinkCollector.collectLibrariesToLink(LibrariesToLinkCollector.java:205)
	at com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder.build(CppLinkActionBuilder.java:858)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.createDynamicLinkAction(CcLinkingHelper.java:759)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.createCcLinkActions(CcLinkingHelper.java:442)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.link(CcLinkingHelper.java:356)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.createTransitiveLinkingActions(CcBinary.java:873)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.init(CcBinary.java:529)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.create(CcBinary.java:282)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.create(CcBinary.java:96)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createRule(ConfiguredTargetFactory.java:484)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createConfiguredTarget(ConfiguredTargetFactory.java:190)
	at com.google.devtools.build.lib.skyframe.SkyframeBuildView.createConfiguredTarget(SkyframeBuildView.java:857)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.createConfiguredTarget(ConfiguredTargetFunction.java:898)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.compute(ConfiguredTargetFunction.java:338)
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:438)
	... 7 more
java.lang.RuntimeException: Unrecoverable error while evaluating node '//:ccbin BuildConfigurationValue.Key[993a664396fa04af94b42a9bca19b9fad7eb40aeb138000ab6e9ffc0e5219538] false' (requested by nodes )
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:515)
	at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:399)
	at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
	at com.google.devtools.build.lib.vfs.PathFragment.startsWith(PathFragment.java:299)
	at com.google.devtools.build.lib.rules.cpp.LibrariesToLinkCollector.addLinkerInputs(LibrariesToLinkCollector.java:247)
	at com.google.devtools.build.lib.rules.cpp.LibrariesToLinkCollector.collectLibrariesToLink(LibrariesToLinkCollector.java:205)
	at com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder.build(CppLinkActionBuilder.java:858)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.createDynamicLinkAction(CcLinkingHelper.java:759)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.createCcLinkActions(CcLinkingHelper.java:442)
	at com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.link(CcLinkingHelper.java:356)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.createTransitiveLinkingActions(CcBinary.java:873)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.init(CcBinary.java:529)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.create(CcBinary.java:282)
	at com.google.devtools.build.lib.rules.cpp.CcBinary.create(CcBinary.java:96)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createRule(ConfiguredTargetFactory.java:484)
	at com.google.devtools.build.lib.analysis.ConfiguredTargetFactory.createConfiguredTarget(ConfiguredTargetFactory.java:190)
	at com.google.devtools.build.lib.skyframe.SkyframeBuildView.createConfiguredTarget(SkyframeBuildView.java:857)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.createConfiguredTarget(ConfiguredTargetFunction.java:898)
	at com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.compute(ConfiguredTargetFunction.java:338)
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:438)
FAILED: Build did NOT complete successfully (9 packages loaded, 6559 targets configured)

What would be really cool is for the go_binary to be either c-shared or c-archive depending on the target which depends on it. The only way I think on how to do that would be to have a scaffold wrapper that would forward the original providers and add 2 splits that would switch to c-archive or c-shared.
UPDATED: Just tested, "forwarding" the providers doesn't work. I don't see how to do this without using another target + macro. Or perhaps making all outgoing attrs transition to c-shared/c-archive to properly fill CcInfo. That may dramatically increase the analysis time, though, since srcs and deps would exist 3 times: orignal, c-archive, c-shared.

Perhaps it's best to leave linkmode exposed and enable CcInfo accordingly.

Ideally, even, it's go_library which would expose CcInfo. That of course creates the question of the sandwich (go -> C -> go), but I guess that's less of an issue.
Update: not possible since if one gives multiple go_library to cc_* then things won't go well.

All in all, I gotta say it's pretty nice to write this and it just works:

go_binary(
    name = "main",
    srcs = ["main.go"],
    cgo = True,
    linkmode = "c-archive",
)

cc_binary(
    name = "ccbin",
    srcs = ["test.c"],
    deps = [":main"],
)

Refs #2433

@steeve
Copy link
Contributor Author

steeve commented Apr 19, 2020

it could be possible to use cc_common.link to emulate c-shared. This way, go_binary expose both c-archive and c-shared at the same time.

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

Couple quick comments.

Based on your comments, this seems more complicated than I thought it would be. Sorry I probably won't have bandwidth for the research and design work before the next minor release.

@@ -81,6 +88,34 @@ def _go_binary_impl(ctx):
),
]

if go.cgo_tools and go.mode.link in (LINKMODE_C_ARCHIVE, LINKMODE_C_SHARED):
cgo_exports = ctx.actions.declare_file("%s.h" % name)
concat_args = ctx.actions.args()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's a bug that we currently concatenate header files (#2131). The go command doesn't do this. We should just generate one header from the main package.

We probably need to gather other indirectly included headers from cgo packages and cdeps though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a very good point regarding transitive cc dependencies. i'll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think iterating on cdeps and merging ccinfo should be enough, i'll add a test case.

go/private/cc.bzl Outdated Show resolved Hide resolved
go/private/context.bzl Show resolved Hide resolved
@steeve
Copy link
Contributor Author

steeve commented Apr 20, 2020

Thank you for reviewing! Do you know who to ping regarding the bazel crash?

Also, thinking about it this weekend, I think it's best to leave linkmode being set by the user and act accordinly in emit_binary instead of it being dynamically inferred via the cc linkmode. I think Go users are already familiar with c-shared and c-archive so I think it's best not to hide it. At least not now.

@jayconrod
Copy link
Contributor

About the Bazel crash, probably open a new issue and tag hlopko and katre. If you can reproduce it to a small case that stands alone (doesn't require rules_go) that will make it more likely to get fixed (I believe the Bazel team is very understaffed at the moment).

About linkmode, agree 100% about leaving linkmode as set by the user. I'd prefer to keep the go_binary semantics close to what they are now, but just eliminate the need for the cc_import wrapper around it.

@steeve
Copy link
Contributor Author

steeve commented Apr 22, 2020

Quick update, apparently this is a bug in bazel. Will update the PR once I get more information.

@steeve
Copy link
Contributor Author

steeve commented Aug 20, 2020

According to bazelbuild/bazel#11885, the fix is expected to land in Bazel 3.5.0, will resume once released.

@jayconrod
Copy link
Contributor

Going through inactive PRs.

It looks like this was blocked on Bazel 3.5.0, which is out now.

Please update if you want to move forward with this.

@steeve
Copy link
Contributor Author

steeve commented Oct 10, 2020

Thanks, how would you advise for this to work on earlier supported bazel versions?

@jayconrod
Copy link
Contributor

I think it would be reasonable to bump the minimum Bazel version to 3.5.0 at this point. 3.x.x has been out for a while without incompatible changes, and people have had time to upgrade from 2.x.x.

You can bump the minimum version by editing .bazelci/presubmit.yml (only ubuntu1604 is tested with 2.2.0, the current minimum) and README.rst. That's probably best done as a separate PR.

@Nickersoft
Copy link

@steeve Is there any update on this? :)

@steeve
Copy link
Contributor Author

steeve commented Jan 12, 2021

Not yet because we are blocked from upgrading rules_go because of some technicalities. Soon I hope!

@steeve
Copy link
Contributor Author

steeve commented Feb 14, 2021

I have rebased this PR, things remaining:

@steeve
Copy link
Contributor Author

steeve commented Feb 14, 2021

  • I cannot for the life of me figure out why the shared_library isn't part of the sandbox for c-shared_test: it is in the runfiles, it is in the manifest, but yet not the FS

This was due to bazelbuild/bazel#12304, fixed by adding a install_name flag when c-shared on darwin

  • I'm not sure on how to fix the header concatenation and using the _cgo_exports from the main package.

Used a symlink to the first header from archive.cgo_exports, as per #2433

No need, copied the idea from #2433 and put it in the rule, instead of the action.

@steeve
Copy link
Contributor Author

steeve commented Feb 14, 2021

I also chose to remove the .cc targets and macros directly, since they are not documented anyway, and would allow using select() on linkmode (which is not possible today).

cc @Nickersoft

@steeve
Copy link
Contributor Author

steeve commented Feb 14, 2021

Now all tests are passing, exempt on RBE for some reason:

exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //tests/core/c_linkmodes:c-shared_test
-----------------------------------------------------------------------------
/b/f/w/bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core/c_linkmodes/c-shared_test: error while loading shared libraries: libadder_shared.so: cannot open shared object file: No such file or directory

Help appreciated

@steeve steeve marked this pull request as ready for review February 15, 2021 00:33
Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

This is really cool. Couple small comments, but it looks very good.

I'll try to reproduce the test failure in RBE to see what's up.

README.rst Outdated Show resolved Hide resolved
go/private/actions/link.bzl Outdated Show resolved Hide resolved
@@ -212,56 +205,3 @@ def _include_unique(opts, flag, include, seen):
return
seen[include] = True
opts.extend([flag, include])

Copy link
Contributor

Choose a reason for hiding this comment

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

A handful of people are probably using this, but it's undocumented, so it's probably okay to remove it.

I think users will just need to depend directly on the go_binary target instead of the .cc target, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Actually I'm going to add an alias that's created regardless (so that it doesn't have to use linkmode as a macro).

Copy link
Contributor Author

@steeve steeve Feb 20, 2021

Choose a reason for hiding this comment

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

I have added a .cc alias to all go_binary with a manual tag and a deprecation message just in case. I can remove it but I felt it was harmless.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a good approach. Let's also mention this in https://github.com/bazelbuild/rules_go/wiki/Deprecation-schedule and say that it will be removed in v0.27.0.

@jayconrod
Copy link
Contributor

Actually I was able to reproduce the test failure without RBE on my Linux VM (Debian based). Not sure why it didn't show up on the Ubuntu 18.04 builder.

Looking at the binary, it does link against that library, but I'm not sure the runpath is getting set correctly. It's $ORIGIN/../../../_solib_k8/../../../k8-fastbuild-ST-da2e477cada9/bin/_solib_k8/_U_S_Stests_Score_Sc_Ulinkmodes_Cadder_Ushared___Utests_Score_Sc_Ulinkmodes_Sadder_Ushared_U. That second ../../.. is suspicious.

ldd output:

$ ldd bazel-bin/tests/core/c_linkmodes/c-shared_test
	linux-vdso.so.1 (0x00007fffd7f14000)
	libadder_shared.so => not found
	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe27a74b000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe27a607000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe27a5e5000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe27a420000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fe27a93a000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe27a404000)

readelf output:

$ readelf -d bazel-bin/tests/core/c_linkmodes/c-shared_test
Dynamic section at offset 0xd58 contains 32 entries:
  Tag        Type                         Name/Value
 0x0000000000000003 (PLTGOT)             0x1fd0
 0x0000000000000002 (PLTRELSZ)           72 (bytes)
 0x0000000000000017 (JMPREL)             0x660
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000007 (RELA)               0x5a0
 0x0000000000000008 (RELASZ)             192 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffff9 (RELACOUNT)          3
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000006 (SYMTAB)             0x298
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000005 (STRTAB)             0x3a0
 0x000000000000000a (STRSZ)              401 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x538
 0x0000000000000001 (NEEDED)             Shared library: [libadder_shared.so]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x6a8
 0x000000000000000d (FINI)               0x894
 0x000000000000001a (FINI_ARRAY)         0x1d48
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x0000000000000019 (INIT_ARRAY)         0x1d50
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../../../_solib_k8/../../../k8-fastbuild-ST-da2e477cada9/bin/_solib_k8/_U_S_Stests_Score_Sc_Ulinkmodes_Cadder_Ushared___Utests_Score_Sc_Ulinkmodes_Sadder_Ushared_U]
 0x000000000000001e (FLAGS)              BIND_NOW
 0x000000006ffffffb (FLAGS_1)            Flags: NOW PIE
 0x000000006ffffff0 (VERSYM)             0x568
 0x000000006ffffffe (VERNEED)            0x580
 0x000000006fffffff (VERNEEDNUM)         1
 0x0000000000000000 (NULL)               0x0

@steeve
Copy link
Contributor Author

steeve commented Feb 20, 2021

Indeed, I am reproducing on debian:10 in docker.

Here is the runfile sandbox:

bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/MANIFEST
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core/c_linkmodes
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core/c_linkmodes/adder_shared_
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core/c_linkmodes/adder_shared_/libadder_shared.so
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/tests/core/c_linkmodes/c-shared_test
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/_solib_k8
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/_solib_k8/_U_S_Stests_Score_Sc_Ulinkmodes_Cadder_Ushared___Utests_Score_Sc_Ulinkmodes_Sadder_Ushared_U
bazel-out/k8-fastbuild/bin/tests/core/c_linkmodes/c-shared_test.runfiles/io_bazel_rules_go/_solib_k8/_U_S_Stests_Score_Sc_Ulinkmodes_Cadder_Ushared___Utests_Score_Sc_Ulinkmodes_Sadder_Ushared_U/libadder_shared.so

@steeve
Copy link
Contributor Author

steeve commented Feb 20, 2021

I am not sure where to look since this rpath is generated by the cc_binary link action. Any idea?

Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Deprecates "cat $files > $out".

Signed-off-by: Steeve Morin <steeve@zen.ly>
No shell required, and works on platforms without bash.

Signed-off-by: Steeve Morin <steeve@zen.ly>
…ation_context

They won't accept a None, only empty values.

Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Also, expose only the first cgo header via a symlink.

Shamelessly copied from #2682

Signed-off-by: Steeve Morin <steeve@zen.ly>
@steeve
Copy link
Contributor Author

steeve commented Feb 20, 2021

I figured out the issue with the so library, and it has to do with a missing rpath.

You can see the dirty details at d632fd3 and e89e8f7

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

Alright so it seems I have opened a can of worms with 86ff842.

For some reason, in //tests/legacy/examples/cgo:cgo_lib_test, the runtime sandbox doesn't include symlinks and only has the .so with a _solib_k8/libtests_Slegacy_Sexamples_Scgo_Scc_Udependency_Slibversion.so at the root of the sandbox, instead of a more readable symlink.

The current sandbox is:

cgo_lib_test.runfiles/
cgo_lib_test.runfiles/MANIFEST
cgo_lib_test.runfiles/io_bazel_rules_go/tests/legacy/examples/cgo/cgo_lib_test_/cgo_lib_test
cgo_lib_test.runfiles/io_bazel_rules_go/_solib_k8/_U_S_Stests_Slegacy_Sexamples_Scgo_Scc_Udependency_Cc_Uversion_Uimport___Utests_Slegacy_Sexamples_Scgo_Scc_Udependency/libc_version_so.so
cgo_lib_test.runfiles/io_bazel_rules_go/_solib_k8/libtests_Slegacy_Sexamples_Scgo_Scc_Udependency_S

Whereas it could/should be:

cgo_lib_test.runfiles/
cgo_lib_test.runfiles/MANIFEST
cgo_lib_test.runfiles/io_bazel_rules_go/tests/legacy/examples/cgo/cc_dependency/libc_version_so.so -> ../../../../../../_solib_k8/_U_S_Stests_Slegacy_Sexamples_Scgo_Scc_Udependency_Cc_Uversion_Uimport___Utests_Slegacy_Sexamples_Scgo_Scc_Udependency/libc_version_so.so
cgo_lib_test.runfiles/io_bazel_rules_go/tests/legacy/examples/cgo/cgo_lib_test_/cgo_lib_test
cgo_lib_test.runfiles/io_bazel_rules_go/_solib_k8/_U_S_Stests_Slegacy_Sexamples_Scgo_Scc_Udependency_Cc_Uversion_Uimport___Utests_Slegacy_Sexamples_Scgo_Scc_Udependency/libc_version_so.so
cgo_lib_test.runfiles/io_bazel_rules_go/_solib_k8/libtests_Slegacy_Sexamples_Scgo_Scc_Udependency_S

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

I have not yet figured out what triggers the "good" behaviour.

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

I mean, I could make the CI green, but I feel there's something more to this that's worth investigating.

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

In my digging in the rabbit hole, I think I found a way to rewrite the cgo flag dance in a cleaner way:

This bit of code has all the //tests/core/cgo/... tests passing:

    final_cc_info = cc_common.merge_cc_infos(
        direct_cc_infos = [
            d[CcInfo]
            for d in cdeps
        ],
    )
    cpp_vars = cc_common.create_compile_variables(
        cc_toolchain = go.cgo_tools.cc_toolchain,
        feature_configuration = go.cgo_tools.feature_configuration,
        preprocessor_defines = final_cc_info.compilation_context.defines,
        include_directories = final_cc_info.compilation_context.includes,
        quote_include_directories = final_cc_info.compilation_context.quote_includes,
        system_include_directories = final_cc_info.compilation_context.system_includes,
        user_compile_flags = cppopts,
        use_pic = go.mode != LINKMODE_NORMAL,
    )
    cppopts = cc_common.get_memory_inefficient_command_line(
        feature_configuration = go.cgo_tools.feature_configuration,
        action_name = C_COMPILE_ACTION_NAME,
        variables = cpp_vars,
    )

    c_vars = cc_common.create_compile_variables(
        cc_toolchain = go.cgo_tools.cc_toolchain,
        feature_configuration = go.cgo_tools.feature_configuration,
        preprocessor_defines = final_cc_info.compilation_context.defines,
        include_directories = final_cc_info.compilation_context.includes,
        quote_include_directories = final_cc_info.compilation_context.quote_includes,
        system_include_directories = final_cc_info.compilation_context.system_includes,
        user_compile_flags = copts,
        use_pic = go.mode != LINKMODE_NORMAL,
    )
    copts = cc_common.get_memory_inefficient_command_line(
        feature_configuration = go.cgo_tools.feature_configuration,
        action_name = C_COMPILE_ACTION_NAME,
        variables = c_vars,
    )

    cxx_vars = cc_common.create_compile_variables(
        cc_toolchain = go.cgo_tools.cc_toolchain,
        feature_configuration = go.cgo_tools.feature_configuration,
        preprocessor_defines = final_cc_info.compilation_context.defines,
        include_directories = final_cc_info.compilation_context.includes,
        quote_include_directories = final_cc_info.compilation_context.quote_includes,
        system_include_directories = final_cc_info.compilation_context.system_includes,
        user_compile_flags = cxxopts,
        use_pic = go.mode != LINKMODE_NORMAL,
    )
    cxxopts = cc_common.get_memory_inefficient_command_line(
        feature_configuration = go.cgo_tools.feature_configuration,
        action_name = CPP_COMPILE_ACTION_NAME,
        variables = cxx_vars,
    )

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

What I'm going to do is revert the rpath stuff, so that this PR is green, and perhaps open a separate PR for leveraging cc_common in Cgo.

@steeve
Copy link
Contributor Author

steeve commented Feb 21, 2021

Alright, after some digging, indeed, here are the LinkerInput:

<LinkerInput(owner=//tests/core/cgo:darwin_imported_dylib, libraries=[<LibraryToLink(dynamic_library=File:[[<execution_root>]bazel-out/darwin-fastbuild/bin]_solib_darwin_x86_64/_U_S_Stests_Score_Scgo_Cdarwin_Uimported_Udylib___Utests_Score_Scgo/libimported.dylib, resolved_symlink_dynamic_library=File:[[<execution_root>]bazel-out/darwin-fastbuild/bin]tests/core/cgo/libimported.dylib, alwayslink=false)>, ], userLinkFlags=[], nonCodeInputs=[])>

The resolved_symlink_dynamic_library is what I was after, and IMHO what we should aim for.

@steeve
Copy link
Contributor Author

steeve commented Feb 22, 2021

Ok so I figured it out. We should be using the LibraryToLink.resolved_dynamic_library instead of the LibraryToLink.dynamic_library to get the vanity path. Submitting a patch.

@steeve steeve changed the title Propagate CcInfo Make go_binary expose CcInfo Feb 22, 2021
@steeve
Copy link
Contributor Author

steeve commented Feb 22, 2021

Hum so I got it working by running the test in bazel run, but not in bazel test

Adapted from #2682

Signed-off-by: Steeve Morin <steeve@zen.ly>
Since we now symlink to the first cgo header.

Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Use the vanity resolved_symlink_{dynamic,interface}_library. Then, move rpath
computation in its own file.
Simplify the relative walking from $ORIGIN a bit.

Signed-off-by: Steeve Morin <steeve@zen.ly>

Signed-off-by: Steeve Morin <steeve@zen.ly>
This wasn't documented, and so it will be removed in the near future. In the
mean time, let's be nice.

Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
@steeve
Copy link
Contributor Author

steeve commented Feb 22, 2021

And we are green! Greener, I might add.

Copy link
Contributor

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

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

This is awesome. Looks really good. One small comment. Merge when ready :)

go/private/rules/wrappers.bzl Outdated Show resolved Hide resolved
@@ -212,56 +205,3 @@ def _include_unique(opts, flag, include, seen):
return
seen[include] = True
opts.extend([flag, include])

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a good approach. Let's also mention this in https://github.com/bazelbuild/rules_go/wiki/Deprecation-schedule and say that it will be removed in v0.27.0.

@steeve
Copy link
Contributor Author

steeve commented Feb 22, 2021

All set!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants