1#!/bin/bash
2
3# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License
15
16set -e
17
18BPFTOOL_DIR=${1}
19BASE_DIR=${2}
20DEST_DIR=${3}
21
22echo "BPFTOOL_DIR:"${BPFTOOL_DIR}
23echo "BASE_DIR:"${BASE_DIR}
24echo "DEST_DIR:"${DEST_DIR}
25
26if [[ ! -d "${BASE_DIR}" ]]
27then
28    echo ${BASE_DIR}": no such directory"
29    exit -1
30fi
31
32if [[ ! -d "${DEST_DIR}" ]]
33then
34    echo ${DEST_DIR}": no such directory"
35    exit -1
36fi
37
38BPF_OBJ_SETS=$(ls ${BASE_DIR} | grep '\.bpf')
39echo "bpf object directories: "${BPF_OBJ_SETS}
40
41for obj_set in ${BPF_OBJ_SETS}
42do 
43    echo "current bpf object directory name: "${obj_set}
44    BPF_OBJ_DIR=${BASE_DIR}"/"${obj_set}
45    echo "current bpf object directory path: "${BPF_OBJ_DIR}
46    BPF_OBJECTS=$(ls ${BPF_OBJ_DIR} | grep '\.bpf\.o')
47    echo "bpf object file names: "${BPF_OBJECTS}
48    for obj in ${BPF_OBJECTS}
49    do
50        echo "current bpf object file name: "${obj}
51        OBJ_PATH=${BPF_OBJ_DIR}"/"${obj}
52        echo "current bpf object file path: "${OBJ_PATH}
53        SKEL_NAME=${obj/bpf.o/skel.h}
54        echo "current skeleton name: "${SKEL_NAME}
55        SKEL_PATH=${DEST_DIR}"/"${SKEL_NAME}
56        echo "current skeleton path: "${SKEL_PATH}
57        ${BPFTOOL_DIR} gen skeleton ${OBJ_PATH} > ${SKEL_PATH}
58        if [ ! -s "${SKEL_PATH}" ]
59        then
60            echo ${SKEL_PATH}" is empty"
61            rm -rf ${SKEL_PATH}
62            exit -1
63        fi
64
65        replacement="#include \"libbpf.h\""
66        sed -i "s/#include <bpf\/libbpf.h>/${replacement}/g" ${SKEL_PATH}
67    done
68done