From abfa89b548d283c8e25285bd6aac0291cd6faf0a Mon Sep 17 00:00:00 2001 From: Caoruihong Date: Wed, 3 Nov 2021 15:48:22 +0800 Subject: [PATCH] feat(build): simplify hc-gen build Signed-off-by: Caoruihong Change-Id: Ie351052c98b975ea217ab2a51a6861e34654237e --- tools/hc-gen/BUILD.gn | 20 ++++++++ tools/hc-gen/build_hcs.py | 102 -------------------------------------- tools/hc-gen/hc_gen.gni | 67 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 102 deletions(-) create mode 100644 tools/hc-gen/BUILD.gn delete mode 100755 tools/hc-gen/build_hcs.py create mode 100644 tools/hc-gen/hc_gen.gni diff --git a/tools/hc-gen/BUILD.gn b/tools/hc-gen/BUILD.gn new file mode 100644 index 00000000..a1340fe0 --- /dev/null +++ b/tools/hc-gen/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# +# HDF is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. + +action("build_hc_gen") { + script = "/usr/bin/env" + if (defined(ohos_lite)) { + script = "//build/lite/run_shell_cmd.py" + } + outputs = [ "$target_out_dir/hc-gen" ] + args = [ + "make", + "-j", + "-C", + rebase_path("."), + "BUILD_DIR=" + rebase_path(target_out_dir), + ] +} diff --git a/tools/hc-gen/build_hcs.py b/tools/hc-gen/build_hcs.py deleted file mode 100755 index d67bbd24..00000000 --- a/tools/hc-gen/build_hcs.py +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -# -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following -# disclaimer in the documentation and/or other materials provided -# with the distribution. -# * Neither the name of Willow Garage, Inc. nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. - -import fcntl -import os -import sys -import argparse -import platform -import subprocess -import time - -_LOCK_FILE_NAME = "._lock" -_BUILD_DIR = 'build' -_LOCK_FILE = '' - - -def lock(): - global _LOCK_FILE - global _BUILD_DIR - global _LOCK_FILE_NAME - if not os.path.exists(_BUILD_DIR): - os.mkdir(_BUILD_DIR) - lock_file = os.path.join(_BUILD_DIR, _LOCK_FILE_NAME) - if not os.path.exists(lock_file): - with open(lock_file, 'w') as l_file: - l_file.write("lock") - l_file.close - print('hc-gen lock file ' + lock_file) - _LOCK_FILE = open(lock_file, 'r') - fcntl.flock(_LOCK_FILE.fileno(), fcntl.LOCK_EX) - - -def unlock(): - global _LOCK_FILE - _LOCK_FILE.close() - - -def exec_command(cmd): - process = subprocess.Popen(cmd) - process.wait() - ret_code = process.returncode - - if ret_code != 0: - raise Exception("{} failed, return code is {}".format(cmd, ret_code)) - - -def make_hc_gen(current_dir): - exec_command(['make', '-C', current_dir, '-j8']) - - -def prepare(current_dir): - make_hc_gen(current_dir) - - -def main(argv): - lock() - current_dir = os.path.split(os.path.realpath(__file__))[0] - hc_gen = os.path.join(current_dir, 'build', 'hc-gen') - - host_hc_gen = argv[1] - if (os.path.exists(host_hc_gen)): - build_hcs_cmd = argv[1:] - else: - prepare(current_dir) - build_hcs_cmd = [hc_gen] + argv[1:] - - prepare(current_dir) - exec_command(build_hcs_cmd) - unlock() - -if __name__ == '__main__': - sys.exit(main(sys.argv)) diff --git a/tools/hc-gen/hc_gen.gni b/tools/hc-gen/hc_gen.gni new file mode 100644 index 00000000..9a842baa --- /dev/null +++ b/tools/hc-gen/hc_gen.gni @@ -0,0 +1,67 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# +# HDF is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. + +template("hc_gen") { + assert(defined(invoker.sources), "sources are must") + action_foreach(target_name) { + deps = [ "//drivers/framework/tools/hc-gen:build_hc_gen" ] + script = "/usr/bin/env" + if (defined(ohos_lite)) { + script = "//build/lite/run_shell_cmd.py" + } + sources = invoker.sources + if (defined(invoker.hc_gen_hex) && invoker.hc_gen_hex) { + hc_flags = [ + "-b", + "-i", + "-a", + ] + output_suffix = "_hex.c" + output_suffix2 = ".hcb" + } else if (defined(invoker.hc_gen_c) && invoker.hc_gen_c) { + hc_flags = [ "-t" ] + output_suffix = ".c" + output_suffix2 = ".h" + } else { + hc_flags = [] + output_suffix = ".hcb" + } + if (defined(invoker.outputs)) { + outputs = invoker.outputs + } else { + outputs = [ "$target_gen_dir/{{source_name_part}}$output_suffix" ] + } + if (defined(output_suffix2)) { + outputs += [ "$target_gen_dir/{{source_name_part}}$output_suffix2" ] + } + assert(get_path_info(outputs[0], "file") == + "{{source_name_part}}$output_suffix", + "unexpected outputs: " + outputs[0]) + output_dir = get_path_info(outputs[0], "dir") + args = [ rebase_path(get_path_info("//drivers/framework/tools/hc-gen/", + "out_dir") + "/hc-gen") ] + args += hc_flags + args += [ + "-o", + rebase_path("$output_dir/{{source_name_part}}"), + "{{source}}", + ] + } +} + +template("hc_gen_c") { + hc_gen_c = true + hc_gen(target_name) { + forward_variables_from(invoker, "*") + } +} + +template("hc_gen_hex") { + hc_gen_hex = true + hc_gen(target_name) { + forward_variables_from(invoker, "*") + } +}