Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame^] | 1 | # buildifier: disable=module-docstring |
| 2 | load("//rust:defs.bzl", "rust_common") |
| 3 | |
| 4 | def _wasm_bindgen_transition(_settings, _attr): |
| 5 | """The implementation of the `wasm_bindgen_transition` transition |
| 6 | |
| 7 | Args: |
| 8 | _settings (dict): A dict {String:Object} of all settings declared |
| 9 | in the inputs parameter to `transition()` |
| 10 | _attr (dict): A dict of attributes and values of the rule to which |
| 11 | the transition is attached |
| 12 | |
| 13 | Returns: |
| 14 | dict: A dict of new build settings values to apply |
| 15 | """ |
| 16 | return {"//command_line_option:platforms": str(Label("//rust/platform:wasm"))} |
| 17 | |
| 18 | wasm_bindgen_transition = transition( |
| 19 | implementation = _wasm_bindgen_transition, |
| 20 | inputs = [], |
| 21 | outputs = ["//command_line_option:platforms"], |
| 22 | ) |
| 23 | |
| 24 | def _import_macro_dep_bootstrap_transition(_settings, _attr): |
| 25 | """The implementation of the `import_macro_dep_bootstrap_transition` transition. |
| 26 | |
| 27 | This transition modifies the config to start using the fake macro |
| 28 | implementation, so that the macro itself can be bootstrapped without |
| 29 | creating a dependency cycle, even while every Rust target has an implicit |
| 30 | dependency on the "import" macro (either real or fake). |
| 31 | |
| 32 | Args: |
| 33 | _settings (dict): a dict {String:Object} of all settings declared in the |
| 34 | inputs parameter to `transition()`. |
| 35 | _attr (dict): A dict of attributes and values of the rule to which the |
| 36 | transition is attached. |
| 37 | |
| 38 | Returns: |
| 39 | dict: A dict of new build settings values to apply. |
| 40 | """ |
| 41 | return {"@rules_rust//rust/settings:use_real_import_macro": False} |
| 42 | |
| 43 | import_macro_dep_bootstrap_transition = transition( |
| 44 | implementation = _import_macro_dep_bootstrap_transition, |
| 45 | inputs = [], |
| 46 | outputs = ["@rules_rust//rust/settings:use_real_import_macro"], |
| 47 | ) |
| 48 | |
| 49 | def _with_import_macro_bootstrapping_mode_impl(ctx): |
| 50 | target = ctx.attr.target[0] |
| 51 | return [target[rust_common.crate_info], target[rust_common.dep_info]] |
| 52 | |
| 53 | with_import_macro_bootstrapping_mode = rule( |
| 54 | implementation = _with_import_macro_bootstrapping_mode_impl, |
| 55 | attrs = { |
| 56 | "target": attr.label( |
| 57 | cfg = import_macro_dep_bootstrap_transition, |
| 58 | allow_single_file = True, |
| 59 | mandatory = True, |
| 60 | executable = False, |
| 61 | ), |
| 62 | "_allowlist_function_transition": attr.label( |
| 63 | default = Label("//tools/allowlists/function_transition_allowlist"), |
| 64 | ), |
| 65 | }, |
| 66 | ) |
| 67 | |
| 68 | def _without_process_wrapper_transition_impl(_settings, _attr): |
| 69 | """This transition allows rust_* rules to invoke rustc without process_wrapper.""" |
| 70 | return { |
| 71 | "//rust/settings:use_process_wrapper": False, |
| 72 | } |
| 73 | |
| 74 | without_process_wrapper_transition = transition( |
| 75 | implementation = _without_process_wrapper_transition_impl, |
| 76 | inputs = [], |
| 77 | outputs = ["//rust/settings:use_process_wrapper"], |
| 78 | ) |
| 79 | |
| 80 | def _without_process_wrapper_impl(ctx): |
| 81 | executable = ctx.executable.target |
| 82 | link_name = ctx.label.name |
| 83 | |
| 84 | # Append .exe if on windows |
| 85 | if executable.extension: |
| 86 | link_name = link_name + "." + executable.extension |
| 87 | link = ctx.actions.declare_file(link_name) |
| 88 | ctx.actions.symlink( |
| 89 | output = link, |
| 90 | target_file = executable, |
| 91 | ) |
| 92 | return [ |
| 93 | DefaultInfo( |
| 94 | executable = link, |
| 95 | ), |
| 96 | ] |
| 97 | |
| 98 | without_process_wrapper = rule( |
| 99 | implementation = _without_process_wrapper_impl, |
| 100 | attrs = { |
| 101 | "target": attr.label( |
| 102 | cfg = without_process_wrapper_transition, |
| 103 | allow_single_file = True, |
| 104 | mandatory = True, |
| 105 | executable = True, |
| 106 | ), |
| 107 | "_allowlist_function_transition": attr.label( |
| 108 | default = Label("//tools/allowlists/function_transition_allowlist"), |
| 109 | ), |
| 110 | }, |
| 111 | ) |