Skip to content

Conversation

@yrom
Copy link
Contributor

@yrom yrom commented Nov 22, 2022

e.g., from https://bazel.build/docs/configurable-attributes#configurable-build-example:

# BUILD 

cc_binary(
    name = "mybinary",
    srcs = ["main.cc"],
    deps = select({
        ":arm_build": [":arm_lib"],
        ":x86_debug_build": [":x86_dev_lib"],
        "//conditions:default": [":generic_lib"],
    }),
)

config_setting(
    name = "arm_build",
    values = {"cpu": "arm"},
)

config_setting(
    name = "x86_debug_build",
    values = {
        "cpu": "x86",
        "compilation_mode": "dbg",
    },
)

refresh_compile_commands(
    name = "refresh_mybinary_commands",

    targets = select({
      ":arm_build":{ "//:mybinary": "--cpu=arm"},
      ":x86_debug_build":{ "//:mybinary": "-c dbg --cpu=x86"},
      "//conditions:default": { "//:mybinary": ""},
    }),
)

Run bazel //:refresh_mybinary_commands --cpu=arm will select the ":arm_build" config.

See more details: https://bazel.build/docs/configurable-attributes#faq-select-macro

@cpsauer cpsauer merged commit 80c1f65 into hedronvision:main Nov 23, 2022
@cpsauer
Copy link
Contributor

cpsauer commented Nov 23, 2022

@yrom, thank you! This is a great improvement. I appreciate your taking the time to leave things better than you found them.

[I wish bazel supported select introspection so we could perform the same argument type transformations on the select, but hey.]

Anyway, again, thank you.

@dambrosio
Copy link

dambrosio commented May 2, 2024

Bazel N00b here but wondering if there is a way to pass a target to refresh_compile_commands using select().

Something like:

# BUILD

config_setting(
    name = "refresh_comp_cmds_target",
    values = {"define": "target="},
)

refresh_compile_commands(
    name = "refresh_comp_cmds",
    targets = select({
        ":refresh_comp_cmds_target": {"$(target)": ""},
        "//condition:default": {"//...": ""},
    }),
)

When I try and run:
bazel run :refresh_comp_cmds --define target=//my_pkg:my_target
or
bazel run :refresh_comp_cmds

I get the following error:

ERROR: Traceback (most recent call last):
        File "/.../BUILD", line 21, column 25, in <toplevel>
                refresh_compile_commands(
        File "/.../external/hedron_compile_commands/refresh_compile_commands.bzl", line 82, column 37, in refresh_compile_commands
                for target, flags in targets.items()
Error: 'select' value has no field or method 'items'

This is on commit: a14ad3a

Any help would be appreciated and thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants