Skip to content

Add support for cc_library.always_link#2294

Closed
steeve wants to merge 1 commit intomasterfrom
unknown repository
Closed

Add support for cc_library.always_link#2294
steeve wants to merge 1 commit intomasterfrom
unknown repository

Conversation

@steeve
Copy link
Copy Markdown
Contributor

@steeve steeve commented Nov 26, 2019

This is a rebased version of #1879.
I couldn't reopen the #1879 since the branch was force pushed.

This is a naive, yet functional approach that's compatible with bazel >= 0.23.

Signed-off-by: Steeve Morin <steeve@zen.ly>
"js": ("", ""),
})

def get_static_lib(lib):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Rename to cc_static_lib.

Also, please add a doc comment:

"""cc_static_lib returns a static library to link if one is available.

Args:
    lib: a library from CcInfo.linking_context.libraries_to_link.

Returns: either static_library, pic_static_library, interface_library, or None.
"""

static_lib = get_static_lib(lib)
if static_lib == None and lib.dynamic_library:
dylib = lib.dynamic_library
# If both static and dynamic variants are available, Bazel will only give
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this comment can be deleted.

# If both static and dynamic variants are available, Bazel will only give
# us the static variant. We'll get one file for each transitive dependency,
# so the same file may appear more than once.
if has_simple_shared_lib_extension(dylib.basename):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't we still need to check for the lib prefix? We remove it unconditionally below.

elif has_versioned_shared_lib_extension(dylib.basename):
# With a versioned shared library, we must use the full filename,
# otherwise the library will not be found by the linker.
libname = ":%s" % lib.basename
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

dylib I think, here and on the two lines below.

for goos in GOOS.keys()
}
_WHOLE_ARCHIVE_FLAGS_BY_GOOS.update({
"darwin": ("-Wl,-all_load", ""),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this actually an OS difference or a GCC / clang difference? If it's the latter, we should check the compiler name (unless there's a better way to test for that). I think that might be in go.cgo_tools.c_compiler_path. After all, you can have gcc on macOS and clang on Linux.

@jayconrod
Copy link
Copy Markdown
Collaborator

Error from CI on Linux:

(18:00:34) ERROR: /workdir/tests/core/cgo/BUILD.bazel:83:1: in go_library rule //tests/core/cgo:versioned_dylib_client:
--
  | Traceback (most recent call last):
  | File "/workdir/tests/core/cgo/BUILD.bazel", line 83
  | go_library(name = 'versioned_dylib_client')
  | File "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/external/io_bazel_rules_go/go/private/rules/library.bzl", line 42, in _go_library_impl
  | go.archive(go, source)
  | File "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/external/io_bazel_rules_go/go/private/actions/archive.bzl", line 78, in go.archive
  | cgo_configure(go, <6 more arguments>)
  | File "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/ec321eb2cc2d0f8f91b676b6d4c66c29/external/io_bazel_rules_go/go/private/rules/cgo.bzl", line 152, in cgo_configure
  | lib.basename
  | object of type 'LibraryToLink' has no field 'basename'

@jayconrod
Copy link
Copy Markdown
Collaborator

Closing inactive PRs.

I think this is superseded by #2584.

@jayconrod jayconrod closed this Oct 9, 2020
yushan26 pushed a commit to yushan26/rules_go that referenced this pull request Jun 16, 2025
Previously if you pulled multiple wheels of the same dep, even ones not
affected by patches would be processed, which is expensive for larger
wheels because of the unzipping / re-zipping.

Fixes bazel-contrib/rules_python#2263
fmeum pushed a commit that referenced this pull request Sep 5, 2025
…hen using alwayslink = True (#4438)

<!-- Thanks for sending a PR! Before submitting:

1. If this is your first PR, please read CONTRIBUTING.md and sign the
CLA
   first. We cannot review code without a signed CLA.
2. Please file an issue *first*. All features and most bug fixes should
have
an associated issue with a design discussed and decided upon. Small bug
   fixes and documentation improvements don't need issues.
3. New features and bug fixes must have tests. Documentation may need to
be updated. If you're unsure what to update, send the PR, and we'll
discuss
   in review.
4. Note that PRs updating dependencies and new Go versions are not
accepted.
   Please file an issue instead.
-->

**What type of PR is this?**

Bug fix

**What does this PR do? Why is it needed?**

Fixes static linking against libraries that have `alwayslink = True`
specified. Fixes C++'s dynamic initialization of static variables when
using cgo

**Which issues(s) does this PR fix?**

Fixes #1486

---

Related to #2584 and
#2294
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.

3 participants