1#!/usr/bin/python3 2# 3# Copyright (C) 2022 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import json 18import os 19import sys 20import textwrap 21 22import common 23 24_TEST_DIR = "build/orchestrator/test_workspace/inner_tree_1" 25_DEMO_RULES_TEMPLATE = """\ 26rule compile_c 27 command = mkdir -p ${out_dir} && g++ -c ${cflags} -o ${out} ${in} 28rule link_so 29 command = mkdir -p ${out_dir} && gcc -shared -o ${out} ${in} 30build %(OUT_DIR)s/libhello1/hello1.o: compile_c %(TEST_DIR)s/libhello1/hello1.c 31 out_dir = %(OUT_DIR)s/libhello1 32 cflags = -I%(TEST_DIR)s/libhello1/include 33build %(OUT_DIR)s/libhello1/libhello1.so: link_so %(OUT_DIR)s/libhello1/hello1.o 34 out_dir = %(OUT_DIR)s/libhello1 35build system: phony %(OUT_DIR)s/libhello1/libhello1.so 36""" 37 38 39class InnerBuildDemo(common.Commands): 40 def export_api_contributions(self, args): 41 contributions_dir = os.path.join(args.out_dir, "api_contributions") 42 os.makedirs(contributions_dir, exist_ok=True) 43 44 if "system" in args.api_domain: 45 reply = dict(name="publicapi", 46 api_domain="system", 47 version=1, 48 cc_libraries=[ 49 dict( 50 name="libhello1", 51 api=f"{_TEST_DIR}/libhello1.map.txt", 52 api_surfaces=["publicapi"], 53 headers=[ 54 dict( 55 arch="", 56 headers=[f"{_TEST_DIR}/hello1.h"], 57 root=f"{_TEST_DIR}", 58 name="libhello1_headers.contribution", 59 system=False) 60 ], 61 ) 62 ]) 63 with open(os.path.join(contributions_dir, "api_a-1.json"), 64 "w", 65 encoding='iso-8859-1') as f: 66 json.dump(reply, f, indent=4) 67 return 68 69 def analyze(self, args): 70 if "system" in args.api_domain: 71 # Nothing to export in this demo 72 # Write a fake inner_tree.ninja; what the inner tree would have 73 # generated. 74 with open(os.path.join(args.out_dir, "inner_tree.ninja"), 75 "w", 76 encoding='iso-8859-1') as f: 77 f.write( 78 textwrap.dedent(_DEMO_RULES_TEMPLATE % { 79 "OUT_DIR": args.out_dir, 80 "TEST_DIR": _TEST_DIR 81 })) 82 with open(os.path.join(args.out_dir, "build_targets.json"), 83 "w", 84 encoding='iso-8859-1') as f: 85 f.write( 86 textwrap.dedent("""\ 87 { 88 "staging": [ 89 { 90 "dest": "staging/system/lib/libhello1.so", 91 "obj": "libhello1/libhello1.so" 92 } 93 ] 94 }""")) 95 96 97def main(argv): 98 return InnerBuildDemo().Run(argv) 99 100 101if __name__ == "__main__": 102 sys.exit(main(sys.argv)) 103 104# vim: sts=4:ts=4:sw=4 105