Skip to content

Commit

Permalink
Try to match cc_library logic for selecting linker outputs.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: f8645982ebeeb9e331fa262dd9ed70d9d4654d93
  • Loading branch information
jmillikin committed Nov 28, 2024
1 parent 31a8f8c commit 54bff5f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/rules_flex.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ flex(
## flex_cc_library

<pre>
flex_cc_library(<a href="#flex_cc_library-name">name</a>, <a href="#flex_cc_library-deps">deps</a>, <a href="#flex_cc_library-flex_options">flex_options</a>, <a href="#flex_cc_library-include_prefix">include_prefix</a>, <a href="#flex_cc_library-src">src</a>, <a href="#flex_cc_library-strip_include_prefix">strip_include_prefix</a>)
flex_cc_library(<a href="#flex_cc_library-name">name</a>, <a href="#flex_cc_library-deps">deps</a>, <a href="#flex_cc_library-flex_options">flex_options</a>, <a href="#flex_cc_library-include_prefix">include_prefix</a>, <a href="#flex_cc_library-linkstatic">linkstatic</a>, <a href="#flex_cc_library-src">src</a>, <a href="#flex_cc_library-strip_include_prefix">strip_include_prefix</a>)
</pre>

Generate a C/C++ library for a Flex lexical analyzer.
Expand Down Expand Up @@ -78,6 +78,7 @@ cc_binary(
| <a id="flex_cc_library-deps"></a>deps | A list of other C/C++ libraries to depend on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="flex_cc_library-flex_options"></a>flex_options | Additional options to pass to the <code>flex</code> command.<br><br>These will be added to the command args immediately before the source file. | List of strings | optional | <code>[]</code> |
| <a id="flex_cc_library-include_prefix"></a>include_prefix | A prefix to add to the path of the generated header.<br><br>See [<code>cc_library.include_prefix</code>](https://bazel.build/reference/be/c-cpp#cc_library.include_prefix) for more details. | String | optional | <code>""</code> |
| <a id="flex_cc_library-linkstatic"></a>linkstatic | Disable creation of a shared library output.<br><br>See [<code>cc_library.linkstatic</code>](https://bazel.build/reference/be/c-cpp#cc_library.linkstatic) for more details. | Boolean | optional | <code>False</code> |
| <a id="flex_cc_library-src"></a>src | A Flex source file.<br><br>The source's file extension will determine whether Flex operates in C or C++ mode:<ul> <li>Inputs with file extension <code>.l</code> generate outputs <code>{name}.c</code> and <code>{name}.h</code>. </li><li>Inputs with file extension <code>.ll</code>, <code>.l++</code>, <code>.lxx</code>, or <code>.lpp</code> generate output <code>{name}.cc</code>. This is equivalent to invoking Flex as <code>flex++</code>.</ul>The C++ output depends on <code>FlexLexer.h</code>, which is part of the Flex source distribution and may be obtained from the Flex toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | required |</li> |
| <a id="flex_cc_library-strip_include_prefix"></a>strip_include_prefix | A prefix to strip from the path of the generated header.<br><br>See [<code>cc_library.strip_include_prefix</code>](https://bazel.build/reference/be/c-cpp#cc_library.strip_include_prefix) for more details. | String | optional | <code>""</code> |

Expand Down
45 changes: 41 additions & 4 deletions flex/rules/flex_cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,49 @@ def _cc_library(ctx, flex_result):
**compile_kwargs
)

cc_supports_dynamic_linker = cc_common.is_enabled(
feature_configuration = cc_feature_configuration,
feature_name = "supports_dynamic_linker",
)

allow_dynamic_library = all([
cc_supports_dynamic_linker,
not ctx.attr.linkstatic,
])

(cc_linking_context, cc_linking_outputs) = cc_common.create_linking_context_from_compilation_outputs(
name = ctx.attr.name,
actions = ctx.actions,
feature_configuration = cc_feature_configuration,
cc_toolchain = cc_toolchain,
compilation_outputs = cc_compilation_outputs,
linking_contexts = [cc_deps.linking_context],
disallow_dynamic_library = not allow_dynamic_library,
)

is_windows = cc_common.is_enabled(
feature_configuration = cc_feature_configuration,
feature_name = "targets_windows",
)

outs = []
if cc_linking_outputs.library_to_link.static_library:
outs.append(cc_linking_outputs.library_to_link.static_library)
if cc_linking_outputs.library_to_link.dynamic_library:
outs.append(cc_linking_outputs.library_to_link.dynamic_library)
if cc_linking_outputs.library_to_link != None:
libs = cc_linking_outputs.library_to_link
if libs.static_library != None:
outs.append(libs.static_library)
if libs.pic_static_library != None:
outs.append(libs.pic_static_library)

if not is_windows:
if libs.resolved_symlink_dynamic_library != None:
outs.append(libs.resolved_symlink_dynamic_library)
elif libs.dynamic_library != None:
outs.append(libs.dynamic_library)

if libs.resolved_symlink_interface_library != None:
outs.append(libs.resolved_symlink_interface_library)
elif libs.interface_library != None:
outs.append(libs.interface_library)

return struct(
outs = depset(direct = outs),
Expand Down Expand Up @@ -136,6 +165,14 @@ for more details.
See [`cc_library.strip_include_prefix`](https://bazel.build/reference/be/c-cpp#cc_library.strip_include_prefix)
for more details.
""",
),
"linkstatic": attr.bool(
default = False,
doc = """Disable creation of a shared library output.
See [`cc_library.linkstatic`](https://bazel.build/reference/be/c-cpp#cc_library.linkstatic)
for more details.
""",
),
"_cc_toolchain": attr.label(
Expand Down

0 comments on commit 54bff5f

Please sign in to comment.