1#!/bin/bash
2# Copyright (c) 2024 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16
17WORK_SPACE=$(cd $(dirname $0); pwd)
18
19COMMOND_TYPE=$1
20OHOS_GLUE_DIR=$2
21OHOS_GLUE_LOG_DIR=$3
22
23INTERFACE_DIR=${WORK_SPACE}/ohos_interface
24INTERFACE_INCLUDE_DIR=${INTERFACE_DIR}/include
25INTERFACE_OHOS_GLUE_DIR=${INTERFACE_DIR}/ohos_glue
26
27os_type=$(uname)
28arch_type=$(uname -m)
29if [ "$os_type" == "Darwin" ]; then
30  if [ "$arch_type" == "x86_64" ]; then
31    CLANG_FORMAT_DIR=${WORK_SPACE}/../../../prebuilts/clang/ohos/darwin-x86_64/llvm/bin
32  else
33    CLANG_FORMAT_DIR=${WORK_SPACE}/../../../prebuilts/clang/ohos/darwin-arm64/llvm/bin
34  fi
35else
36  CLANG_FORMAT_DIR=${WORK_SPACE}/../../../prebuilts/clang/ohos/linux-x86_64/llvm/bin
37fi
38
39handle_copy_dir() {
40  local src_dir=$1
41  local dst_dir=$2
42
43  local parent_dir=$(dirname ${dst_dir})
44  [ -n "${dst_dir}" ] && rm -rf ${dst_dir} && mkdir -p ${parent_dir}
45
46  if [ -d ${src_dir} ] && [ "$(ls -A ${src_dir})" ]; then
47    cp -rf ${src_dir} ${dst_dir}
48  else
49    echo "${src_dir} is not exist or empty" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
50  fi
51}
52
53handle_copy_files() {
54  echo "begin to copy ohos glue files,module name is $1" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
55
56  if [ "$1" = "base" ]; then
57    handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/base ${OHOS_GLUE_DIR}/base
58    handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/scripts ${OHOS_GLUE_DIR}/scripts
59    return
60  fi
61
62  local dir_name=ohos_$1
63  rm -rf ${OHOS_GLUE_DIR}/${dir_name} && mkdir -p ${OHOS_GLUE_DIR}/${dir_name}
64  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/${dir_name}/include ${OHOS_GLUE_DIR}/${dir_name}/include
65  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/${dir_name}/bridge/webview ${OHOS_GLUE_DIR}/${dir_name}/bridge
66  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/${dir_name}/cpptoc/webview ${OHOS_GLUE_DIR}/${dir_name}/cpptoc
67  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/${dir_name}/ctocpp/webview ${OHOS_GLUE_DIR}/${dir_name}/ctocpp
68}
69
70handle_copy_include() {
71  echo "begin to copy ohos interface files" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
72
73  cp ${INTERFACE_INCLUDE_DIR}/ohos_nweb/* ${WORK_SPACE}/ohos_nweb/include
74
75  handle_copy_dir ${INTERFACE_INCLUDE_DIR}/ohos_adapter ${WORK_SPACE}/ohos_adapter/interfaces
76}
77
78handle_copy_commond() {
79  local curr_time=$(date +"%Y-%m-%d %H:%M:%S")
80  echo "start time is ${curr_time}" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
81  echo "work space is ${WORK_SPACE}" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
82  echo "ohos glue dir is ${OHOS_GLUE_DIR}" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
83
84  handle_copy_include
85
86  handle_copy_files base
87  handle_copy_files nweb
88  handle_copy_files adapter
89}
90
91handle_develop_commond() {
92  local ohos_glue_dir=${1}/${2}
93  [ -n "${ohos_glue_dir}" ] && rm -rf ${ohos_glue_dir} && mkdir -p ${ohos_glue_dir}
94
95  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/base ${ohos_glue_dir}/base
96  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/scripts ${ohos_glue_dir}/scripts
97  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_nweb/include ${ohos_glue_dir}/ohos_nweb/include
98  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_nweb/bridge/${2} ${ohos_glue_dir}/ohos_nweb/bridge
99  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_nweb/cpptoc/${2} ${ohos_glue_dir}/ohos_nweb/cpptoc
100  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_nweb/ctocpp/${2} ${ohos_glue_dir}/ohos_nweb/ctocpp
101  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_adapter/include ${ohos_glue_dir}/ohos_adapter/include
102  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_adapter/bridge/${2} ${ohos_glue_dir}/ohos_adapter/bridge
103  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_adapter/cpptoc/${2} ${ohos_glue_dir}/ohos_adapter/cpptoc
104  handle_copy_dir ${INTERFACE_OHOS_GLUE_DIR}/ohos_adapter/ctocpp/${2} ${ohos_glue_dir}/ohos_adapter/ctocpp
105
106  local file_list=$(find ${ohos_glue_dir}/ohos_*/include -name "*.h")
107  for file in $file_list
108  do
109    clang-format -style="{PointerAlignment: Right}" -i $file
110  done
111
112  python3 ${ohos_glue_dir}/scripts/translator.py ${2}
113
114  file_list=$(find ${ohos_glue_dir} -type f \( -name "*.h" -o -name "*.cpp" \))
115  for file in $file_list
116  do
117    clang-format -style=file -i $file
118  done
119}
120
121handle_translate_commond() {
122  OHOS_GLUE_MODULE=$1
123  if [ "${OHOS_GLUE_MODULE}" = "base" ]; then
124    return
125  fi
126
127  local curr_time=$(date +"%Y-%m-%d %H:%M:%S")
128  echo "start time is ${curr_time}" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
129  echo "begin to translate ohos glue file,module name is ${OHOS_GLUE_MODULE}" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
130
131  local dir_name=ohos_${OHOS_GLUE_MODULE}
132  local file_list=$(find ${OHOS_GLUE_DIR}/${dir_name}/include -name "*.h")
133  for file in $file_list
134  do
135    ${CLANG_FORMAT_DIR}/clang-format -style="{PointerAlignment: Right}" -i $file
136  done
137
138  python3 ${OHOS_GLUE_DIR}/scripts/translator.py webview $dir_name >> ${OHOS_GLUE_LOG_DIR}/prepare.log
139}
140
141case "${COMMOND_TYPE}" in
142  "copy")
143    handle_copy_commond
144    ;;
145  "develop")
146    OHOS_GLUE_LOG_DIR=./
147    dir_name=$(date +"%Y%m%d%H%M%S")
148    handle_develop_commond ${dir_name} webcore
149    handle_develop_commond ${dir_name} webview
150    ;;
151  "translate")
152    args=${@:4}
153    handle_translate_commond $args
154    ;;
155  *)
156    echo "commond ${COMMOND_TYPE} is invalid" >> ${OHOS_GLUE_LOG_DIR}/prepare.log
157esac
158