| # Copyright 2018 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # Calculates offsets of fields in C structures for use in assembly. |
| template("generate_offsets") { |
| # There are 3 targets involved: |
| # 1. have the compiler calculate the offsets |
| # 2. extract those offsets to a header file |
| # 3. include the offsets in the build for validation |
| obj_target = "${target_name}__obj" |
| header_target = "${target_name}__header" |
| validate_target = target_name |
| |
| # Have the compiler calculate the offsets and store that information in the |
| # object file to extract later. |
| source_set(obj_target) { |
| forward_variables_from(invoker, |
| [ |
| "cflags", |
| "cflags_c", |
| "defines", |
| "deps", |
| "public_deps", |
| "sources", |
| "testonly", |
| ]) |
| |
| # Disable LTO so we get an object file. |
| configs -= [ |
| "//build:sections", |
| "//build:lto", |
| ] |
| defines = [ "GEN_OFFSETS" ] |
| visibility = [ ":${header_target}" ] |
| } |
| |
| # Extract the offset information we've had the compiler emit into the object |
| # file. |
| action_foreach("${header_target}") { |
| forward_variables_from(invoker, |
| [ |
| "sources", |
| "testonly", |
| ]) |
| script = "//build/image/extract_offsets.py" |
| deps = [ |
| ":${obj_target}", |
| ] |
| args = [ |
| "--tool_prefix", |
| arch_tool_prefix, |
| "--input", |
| rebase_path("${target_out_dir}/${obj_target}.{{source_name_part}}.o"), |
| "--output", |
| rebase_path("${root_gen_dir}/inc/{{source_name_part}}.h"), |
| ] |
| outputs = [ |
| "${root_gen_dir}/inc/{{source_name_part}}.h", |
| ] |
| visibility = [ ":${validate_target}" ] |
| } |
| |
| # Include the offset source file in the build so the extracted offsets can be |
| # validated. |
| source_set(validate_target) { |
| forward_variables_from(invoker, |
| [ |
| "cflags", |
| "cflags_c", |
| "defines", |
| "deps", |
| "public_deps", |
| "sources", |
| "testonly", |
| ]) |
| |
| deps = [ |
| ":${header_target}", |
| ] |
| } |
| } |