| #!/usr/bin/env python3 |
| # Copyright 2025 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. |
| |
| """Generates build helper files from `build.json`.""" |
| |
| import json |
| import os |
| |
| COPYRIGHT_HEADER = """# Copyright 2025 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. |
| """ |
| |
| |
| def read_build_json(file_path): |
| """Reads build.json, strips comments, and returns the parsed JSON data.""" |
| with open(file_path, "r") as f: |
| # Simple comment stripping for JSONC format |
| content = "".join(line for line in f if not line.strip().startswith("//")) |
| return json.loads(content) |
| |
| |
| def generate_sources_json(output_dir, data): |
| """Generates gen/sources.json.""" |
| output_path = os.path.join(output_dir, "sources.json") |
| with open(output_path, "w") as f: |
| json.dump(data, f, indent=4) |
| f.write("\n") |
| print(f"Generated {output_path}") |
| |
| |
| def generate_sources_bzl(output_dir, data): |
| """Generates gen/sources.bzl.""" |
| output_path = os.path.join(output_dir, "sources.bzl") |
| with open(output_path, "w") as f: |
| f.write(COPYRIGHT_HEADER) |
| f.write("\n\n") |
| f.write("# Automatically generated by generate_build_files.py.\n") |
| f.write("# Do not edit this file directly.\n") |
| f.write("\n") |
| f.write("EMBOSSC_SOURCES = [\n") |
| for source in data["embossc_sources"]: |
| f.write(f' "{source}",\n') |
| f.write("]\n\n") |
| f.write("EMBOSS_RUNTIME_CPP_SOURCES = [\n") |
| for source in data["emboss_runtime_cpp_sources"]: |
| f.write(f' "{source}",\n') |
| f.write("]\n") |
| print(f"Generated {output_path}") |
| |
| |
| def generate_sources_cmake(output_dir, data): |
| """Generates gen/sources.cmake.""" |
| output_path = os.path.join(output_dir, "sources.cmake") |
| with open(output_path, "w") as f: |
| f.write(COPYRIGHT_HEADER) |
| f.write("\n\n") |
| f.write("# Automatically generated by generate_build_files.py.\n") |
| f.write("# Do not edit this file directly.\n\n") |
| f.write("set(EMBOSSC_SOURCES\n") |
| for source in data["embossc_sources"]: |
| f.write(f' "{source}"\n') |
| f.write(")\n\n") |
| f.write("set(EMBOSS_RUNTIME_CPP_SOURCES\n") |
| for source in data["emboss_runtime_cpp_sources"]: |
| f.write(f' "{source}"\n') |
| f.write(")\n") |
| print(f"Generated {output_path}") |
| |
| |
| def generate_sources_gni(output_dir, data): |
| """Generates gen/sources.gni.""" |
| output_path = os.path.join(output_dir, "sources.gni") |
| with open(output_path, "w") as f: |
| f.write(COPYRIGHT_HEADER) |
| f.write("\n\n") |
| f.write("# Automatically generated by generate_build_files.py.\n") |
| f.write("# Do not edit this file directly.\n\n") |
| f.write("embossc_sources = [\n") |
| for source in data["embossc_sources"]: |
| f.write(f' "{source}",\n') |
| f.write("]\n\n") |
| f.write("emboss_runtime_cpp_sources = [\n") |
| for source in data["emboss_runtime_cpp_sources"]: |
| f.write(f' "{source}",\n') |
| f.write("]\n") |
| print(f"Generated {output_path}") |
| |
| |
| def main(): |
| workspace_root = os.environ.get("BUILD_WORKSPACE_DIRECTORY", os.getcwd()) |
| build_json_path = os.path.join(workspace_root, "build.json") |
| gen_dir = os.path.join(workspace_root, "gen") |
| |
| os.makedirs(gen_dir, exist_ok=True) |
| |
| data = read_build_json(build_json_path) |
| |
| generate_sources_json(gen_dir, data) |
| generate_sources_bzl(gen_dir, data) |
| generate_sources_cmake(gen_dir, data) |
| generate_sources_gni(gen_dir, data) |
| |
| |
| if __name__ == "__main__": |
| main() |