blob: 7c46dd52a15ef4ab0da796f1bd9773c1a7a30855 [file] [log] [blame]
kelvin.zhangac22e652021-10-18 15:09:21 +08001#!/bin/bash
yang.li09520922022-01-12 15:51:51 +08002#
yang.liffa60e52022-01-11 14:38:56 +08003# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
yang.li09520922022-01-12 15:51:51 +08004#
yang.liffa60e52022-01-11 14:38:56 +08005# SPDX-License-Identifier: MIT
yang.li09520922022-01-12 15:51:51 +08006#
yang.liffa60e52022-01-11 14:38:56 +08007
kelvin.zhangf91d7b02021-10-26 16:47:17 +08008###############################################################
9# Function: Auto-generate root CMakeLists.txt and Kconfig according to manifest.xml.
10###############################################################
kelvin.zhangac22e652021-10-18 15:09:21 +080011
kelvin.zhangf91d7b02021-10-26 16:47:17 +080012cmake_file="$PWD/CMakeLists.txt"
13kconfig_file="$PWD/Kconfig"
Kelvin Zhang9a382242022-04-11 18:46:32 +080014build_dir="build_system"
15exclude_dirs="boot products docs"
kelvin.zhang5ef541b2021-10-21 13:24:48 +080016special_dirs="arch soc boards"
Kelvin Zhangce29a712022-06-09 17:25:22 +080017toolchain_dir="arch/$ARCH/toolchain"
Kelvin Zhang25ccae72021-12-07 16:42:12 +080018
Kelvin Zhang3290b1c2022-05-06 16:41:02 +080019DEFAULT_RTOS_SDK_MANIFEST="$PWD/products/$PRODUCT/rtos_sdk_manifest.xml"
Kelvin Zhang25ccae72021-12-07 16:42:12 +080020RTOS_SDK_MANIFEST_FILE="$kernel_BUILD_DIR/rtos_sdk_manifest.xml"
Xiaohu.Huang02b7f9f2022-01-20 18:42:07 +080021RTOS_SDK_MANIFEST_OLD_FILE="$kernel_BUILD_DIR/rtos_sdk_manifest_old.xml"
Kelvin Zhang24a365e2021-12-28 15:52:55 +080022STAMP="$kernel_BUILD_DIR/.stamp"
23
Kelvin Zhang5374de92022-01-14 10:35:43 +080024[ -n "$1" ] && BUILD_DIR=$1;
25RTOS_SDK_VERSION_FILE="$BUILD_DIR/sdk_ver.h"
26
27#COMPILE_TIME="$(shell date +%g.%V.%u" "%H:%M:%S)"
28COMPILE_TIME=`date +%F" "%T`
29
Kelvin Zhange2e04712022-02-09 17:39:35 +080030echo "#define CONFIG_BOARD_NAME \"$BOARD\"" > $RTOS_SDK_VERSION_FILE
31echo "#define CONFIG_PRODUCT_NAME \"$PRODUCT\"" >> $RTOS_SDK_VERSION_FILE
32echo "#define CONFIG_COMPILE_TIME \"$COMPILE_TIME\"" >> $RTOS_SDK_VERSION_FILE
33
Xueling Li255a6c12022-09-06 09:51:45 +080034# Check whether the project is a valid repo
35repo manifest 2>&1 | grep -q $build_dir
Kelvin Zhang3290b1c2022-05-06 16:41:02 +080036if [ "$?" -ne 0 ]; then
37 echo "Non-repo source code"
38 if [ -f $DEFAULT_RTOS_SDK_MANIFEST ]; then
39 echo "Use default manifest: $DEFAULT_RTOS_SDK_MANIFEST"
40 cp -f $DEFAULT_RTOS_SDK_MANIFEST $RTOS_SDK_MANIFEST_FILE
41 else
42 echo "Default manifest.xml not found!"
43 exit 0
44 fi
45else
46 echo "Generate manifest.xml"
47 repo manifest > $RTOS_SDK_MANIFEST_FILE
48 if [ ! -f $RTOS_SDK_MANIFEST_FILE ]; then
49 echo "Faild to save $RTOS_SDK_MANIFEST_FILE"
50 exit 1
51 fi
kelvin.zhangbdb01dd2021-11-02 10:14:45 +080052fi
Kelvin Zhang017fb4c2022-01-20 19:48:36 +080053
Kelvin Zhange2e04712022-02-09 17:39:35 +080054# Get SDK_VERSION
Kelvin Zhang5374de92022-01-14 10:35:43 +080055pattern="revision="
56keyline=`grep 'default .* revision' $RTOS_SDK_MANIFEST_FILE`
57for keyword in $keyline; do
58 let i++
59 if [[ $keyword == $pattern* ]]; then
60 SDK_VERSION=`echo ${keyword#*${pattern}} | sed 's/\"//g' | sed 's/\/>//g'`
61 break;
62 fi
63done
Kelvin Zhange2e04712022-02-09 17:39:35 +080064echo "#define CONFIG_VERSION_STRING \"$SDK_VERSION\"" >> $RTOS_SDK_VERSION_FILE
65
66if [ -s $RTOS_SDK_MANIFEST_OLD_FILE ] && [ -s $kconfig_file ] && [ $kconfig_file -ot $STAMP ]; then
67 is_update=`comm -3 <(sort $RTOS_SDK_MANIFEST_FILE) <(sort $RTOS_SDK_MANIFEST_OLD_FILE)`
68 if [ -z "$is_update" ]; then
69 exit 0
70 else
71 echo "Update top Kconfig and CMakelists.txt."
72 fi
73fi
74
75# Back up manifest.xml
76cp -arf $RTOS_SDK_MANIFEST_FILE $RTOS_SDK_MANIFEST_OLD_FILE
Kelvin Zhang5374de92022-01-14 10:35:43 +080077
Kelvin Zhang8cc560f2021-12-24 20:25:08 +080078if [[ "$PRODUCT" == aocpu ]]; then
79 sed -i '/path="drivers"/d' $RTOS_SDK_MANIFEST_FILE
80else
81 sed -i '/path="drivers_aocpu"/d' $RTOS_SDK_MANIFEST_FILE
82fi
kelvin.zhangbdb01dd2021-11-02 10:14:45 +080083
Kelvin Zhang33a3a872021-12-09 15:29:20 +080084# Write the fixed content to CMakeLists.txt
kelvin.zhangac22e652021-10-18 15:09:21 +080085cat <<EOF > $cmake_file
86enable_language(C CXX ASM)
87
kelvin.zhangac22e652021-10-18 15:09:21 +080088EOF
89
Kelvin Zhang33a3a872021-12-09 15:29:20 +080090# Clear Kconfig
kelvin.zhangac22e652021-10-18 15:09:21 +080091cat <<EOF > $kconfig_file
92EOF
93
Kelvin Zhangce29a712022-06-09 17:25:22 +080094# Write the toolchain options to Kconfig
95if [ -f $toolchain_dir/Kconfig ]; then
96 category=`basename $toolchain_dir`
97 toolchain_kconfig_path="arch/\${ARCH}/toolchain"
98 echo "menu \"${category^} Options\"" > $kconfig_file
99 echo "source \"$toolchain_kconfig_path/Kconfig\"" >> $kconfig_file
100 echo -e "endmenu\n" >> $kconfig_file
101fi
102
kelvin.zhang83a4aa32022-04-08 15:48:35 +0800103# Figure out the $relative_dir and its column
Kelvin Zhangecaacb32022-05-09 10:10:56 +0800104[ -z "$REPO_DIR" ] && REPO_DIR=$PWD
Kelvin Zhang33a3a872021-12-09 15:29:20 +0800105pattern="path="
106i=0
Kelvin Zhang9a382242022-04-11 18:46:32 +0800107keyline=`grep "path=\".*$build_dir\"" $RTOS_SDK_MANIFEST_FILE`
Kelvin Zhang33a3a872021-12-09 15:29:20 +0800108for keyword in $keyline; do
109 let i++
110 if [[ $keyword == $pattern* ]]; then
111 repo_path=`echo ${keyword#*${pattern}} | sed 's/\"//g' | sed 's/\/>//g'`
112 relative_dir=`dirname $repo_path`
kelvin.zhang83a4aa32022-04-08 15:48:35 +0800113 # Filter current project
Kelvin Zhangecaacb32022-05-09 10:10:56 +0800114 if [[ $REPO_DIR == *$relative_dir ]]; then
kelvin.zhang83a4aa32022-04-08 15:48:35 +0800115 break
116 fi
Kelvin Zhang33a3a872021-12-09 15:29:20 +0800117 fi
118done
119
120if [[ $relative_dir == . ]]; then
kelvin.zhang9e82d8f2021-10-26 20:26:04 +0800121 pattern="path="
122else
Kelvin Zhang33a3a872021-12-09 15:29:20 +0800123 pattern="path=\"${relative_dir}/"
kelvin.zhang9e82d8f2021-10-26 20:26:04 +0800124fi
125
Kelvin Zhange2e04712022-02-09 17:39:35 +0800126# Sort manifest.xml of RTOS SDK
Kelvin Zhang33a3a872021-12-09 15:29:20 +0800127sort -k $i $RTOS_SDK_MANIFEST_FILE -o $RTOS_SDK_MANIFEST_FILE
128
kelvin.zhangac22e652021-10-18 15:09:21 +0800129while IFS= read -r line
130do
kelvin.zhang83a4aa32022-04-08 15:48:35 +0800131 keyline=`echo "$line" | grep "$pattern"`
Kelvin Zhangecaacb32022-05-09 10:10:56 +0800132 [ -z "$keyline" ] && continue
kelvin.zhang83a4aa32022-04-08 15:48:35 +0800133
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800134 for keyword in $keyline; do
135 if [[ $keyword == path=* ]]; then
Kelvin Zhang25ccae72021-12-07 16:42:12 +0800136 repo_path=`echo ${keyword#*${pattern}} | sed 's/\"//g' | sed 's/\/>//g'`
kelvin.zhang5ef541b2021-10-21 13:24:48 +0800137
Kelvin Zhang22cb55e2022-06-08 20:21:00 +0800138 if [[ $repo_path == drivers* ]] || [[ $repo_path == benchmark* ]]; then
Kelvin Zhang20fb7ec2021-12-28 10:30:55 +0800139 category=`basename $repo_path | sed 's/_/ /g'`
140 category=`echo $category | sed "s/ $PRODUCT//g"`
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800141 else
142 category=`dirname $repo_path`
kelvin.zhangac22e652021-10-18 15:09:21 +0800143 fi
kelvin.zhang78a66712021-10-20 17:27:41 +0800144
Kelvin Zhang9a382242022-04-11 18:46:32 +0800145 # exclude some dirs
146 skip_flag=0
147 for exclude_dir in $exclude_dirs; do
148 if [[ $repo_path == $exclude_dir* ]]; then
149 skip_flag=1
150 break
151 fi
152 done
153 [ "$skip_flag" -eq 1 ] && continue
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800154
Kelvin Zhangaed13162022-01-11 16:31:13 +0800155 # substitute ARCH dirs with viarable
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800156 case $special_dirs in
157 *"$category"*) arch=`basename $repo_path`
158 if [ "$arch" == "$ARCH" ]; then
159 cmake_path="$category/\${ARCH}"
Kelvin Zhangce736e62022-03-02 14:17:40 +0800160 kconfig_path="$category/\${ARCH}"
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800161 else
162 continue
163 fi;;
164 * ) cmake_path=$repo_path
165 kconfig_path=$repo_path;;
166 esac
167
168 # Generate root CMakeLists.txt
169 if [ -f $repo_path/CMakeLists.txt ]; then
170 echo "add_subdirectory($cmake_path)" >> $cmake_file
171 fi
172
173 # Generate root Kconfig
174 if [ -f $repo_path/Kconfig ]; then
175 if [ "$last_category" != "$category" ]; then
176 if [ -n "$last_category" ]; then
177 echo -e "endmenu\n" >> $kconfig_file
178 fi
Kelvin Zhang24939422022-01-06 13:52:10 +0800179
Kelvin Zhangce29a712022-06-09 17:25:22 +0800180 if [ "$category" == "wcn" -o "$category" == "soc" ]; then
Kelvin Zhang24939422022-01-06 13:52:10 +0800181 echo "menu \"${category^^} Options\"" >> $kconfig_file
182 else
183 echo "menu \"${category^} Options\"" >> $kconfig_file
184 fi
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800185 fi
186
187 echo "source \"$kconfig_path/Kconfig\"" >> $kconfig_file
188 last_category=$category
189 fi
190 break;
kelvin.zhangac22e652021-10-18 15:09:21 +0800191 fi
Kelvin Zhangf441d5e2021-12-02 20:37:37 +0800192 done
Kelvin Zhang25ccae72021-12-07 16:42:12 +0800193done < "$RTOS_SDK_MANIFEST_FILE"
kelvin.zhangac22e652021-10-18 15:09:21 +0800194
195echo "endmenu" >> $kconfig_file
Kelvin Zhang24a365e2021-12-28 15:52:55 +0800196
Kelvin Zhang55457002021-12-29 14:26:17 +0800197sleep 1
Kelvin Zhang24a365e2021-12-28 15:52:55 +0800198touch $STAMP