1#!/bin/bash
2# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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.
14set -e
15target_os="linux"
16gn="gn"
17case "$OSTYPE" in
18  solaris*) echo "SOLARIS" ;;
19  darwin*)  target_os="macx" ;;
20  linux*)   target_os="linux"  ;;
21  bsd*)     echo "is bsd os" ;;
22  msys*)    target_os="windows" gn="gn.exe" ;;
23  *)        echo "unknown: $OSTYPE" ;;
24esac
25PRJ_ROOT_DIR=$(readlink -f -- "$(dirname $0)/")
26cd ${PRJ_ROOT_DIR}
27FORMAT_DIR_LIST=(
28    "${PRJ_ROOT_DIR}/build"
29    "${PRJ_ROOT_DIR}/gn"
30    "${PRJ_ROOT_DIR}/sdk"
31    "${PRJ_ROOT_DIR}/src"
32    "${PRJ_ROOT_DIR}/test"
33    "${PRJ_ROOT_DIR}/trace_extend/src"
34    "${PRJ_ROOT_DIR}/trace_extend/test"
35    "${PRJ_ROOT_DIR}/prebuilts/fuzz"
36    "${PRJ_ROOT_DIR}/prebuilts/linux"
37    "${PRJ_ROOT_DIR}/prebuilts/macx"
38    "${PRJ_ROOT_DIR}/prebuilts/patch_bounds_checking_function"
39    "${PRJ_ROOT_DIR}/prebuilts/patch_googletest"
40    "${PRJ_ROOT_DIR}/prebuilts/patch_hiperf"
41    "${PRJ_ROOT_DIR}/prebuilts/patch_libunwind"
42    "${PRJ_ROOT_DIR}/prebuilts/patch_perf_event"
43    "${PRJ_ROOT_DIR}/prebuilts/patch_protobuf"
44    "${PRJ_ROOT_DIR}/prebuilts/patch_sqlite"
45)
46echo "formatting code ..."
47for d in ${FORMAT_DIR_LIST[@]}; do
48    for f in $(find $d -type f -not -name '*sql.c' -regex '.*\.\(cpp\|hpp\|c\|h\)'); do
49        dos2unix $f
50        chmod -x $f
51        clang-format --verbose -i $f
52    done
53    for f in $(find $d -type f -not -name '*sql.c' -regex '.*\.\(gn\|gni\)'); do
54        ./prebuilts/$target_os/gn format $f
55    done
56done
57echo "formatting code over"
58