阅读:4840回复:7

shell脚本语法详解

楼主#
更多 发布于:2020-08-20 14:47
大家在安卓系统rom开发过程中,面对不同的客户,很多时候需要编写makefile脚本,这里推荐给大家一个shell语法全攻略网站:
http://c.biancheng.net/shell/base/


学习了这个,让你的脚本写的飞起!!!
无私分享啊
给我点赞吧

最新喜欢:

xhlqdzxhlqdz zhaoyf13zhaoyf...
If you have nothing to lose, then you can do anything.
沙发#
发布于:2020-08-20 17:59
脚本控制,编译飞起
[url]http://190.lsal.cn/195/1329.gif?0728100424873[/url]
板凳#
发布于:2020-08-24 09:03
我也是自己写个build.sh,再写个cfg文件,各种编译配置直接从cfg文件里面读取,一劳永逸,不用经常到各个mk里面去改动。
地板#
发布于:2021-08-20 10:15
Pazival:我也是自己写个build.sh,再写个cfg文件,各种编译配置直接从cfg文件里面读取,一劳永逸,不用经常到各个mk里面去改动。回到原帖
能否参考一下cfg文件怎么写的,我现在经常还要面临修改各个mk文件的麻烦
[url]http://190.lsal.cn/195/1329.gif?0728100424873[/url]
4楼#
发布于:2021-09-01 11:01
Re:build.cfg及build.sh

要用flash上传附件,我传不了啊。 大致思路就是build.cfg里面写一些配置,build.sh里面去读取这个配置export一些变量或者做一些linux上的操作,mk文件里面就能使用这些变量,比如:



_________________________________________________________ #######                                                                          BLUETOOTH                                                         ####### _________________________________________________________ #BOARD_HAVE_BLUETOOTH options true|false BOARD_HAVE_BLUETOOTH=false #BLUETOOTH_USR_RTK_BLUEDROID options true|false BLUETOOTH_USR_RTK_BLUEDROID=false _________________________________________________________ #######                                                                                  WIFI                                                                 ####### _________________________________________________________ #BOARD_HAVE_WIFI options true|false BOARD_HAVE_WIFI=true #ACS support, options y|n CONFIG_ACS=y build.sh里面写读取这个cfg里面的配置:



#!/bin/bash

echo
NO_TYPE_WARNNING="warning: Please specify build type, you can edit build.cfg to specify build type, option can be 'user|eng|userdebug', use -h to see help"
SECUREOS=false

while getopts abcdhj:lorsuv opt
do
  case "$opt" in
    a) ANDROID=true;; #all partitions
    b) BOOT=true;; #boot
    c) CLEAN=true;; #clean
    d) DTB=true;; #dtb
h) HELP=true;; #help
    j) J=$OPTARG;;
    l) LOGO=true;; #logo
    o) ODM=true;; #odm
    r) RECOVERY=true;; #recovery
    s) SYSTEM=true;; #system
    u) UBOOT=true;;
    v) VENDOR=true;; #vendor
    *) echo "Unkown option: $opt";;
  esac
done

#################### functions start ####################
test(){
echo "test is ok "
}

###### print help #####
print_help(){
echo
  echo "Usage: ./build.sh [opts]... [params]..."
  echo "To Build Uboot, Android, or Clean project"
  echo "  -a, build android"
  echo "  -c, make clean"
  echo "  -h, help"
  echo "  -j, -jN, default is 8, can specify to 16 depends on server ability"
  echo "  -u, build uboot"
  echo "For Example: "
  echo "./build.sh -acuj16"
  echo "which means--> make clean --> make uboot --> make android"
  echo "For build configurations, please edit build.cfg"
  echo "********** IMPORTANT **********"
  echo "If you intent to build a release version for customer, you MUST use full command options: ./build.sh -acuj16"
  echo
}

may_print_help(){
if [ "$HELP" = true ]; then
 print_help
 exit;
fi
}

###### print enviroment #####
print_env(){
 echo "====================== Envirenment Variables start ===================== "
 env
 echo "====================== Envirenment Variables end ===================== "
 echo
}

custom_config(){
echo "========================== customize start =========================="

if [ "$PRODUCT_NAME" = "p230" ]; then
#DTVPlayer middleware config
if [ "$DVB_APP_CONFIG" = "dvbt" ]; then
export DVB_APP_CONFIG_DVBT=true
cp -v vendor/amlogic/apps/TvMiddleware/assets/tv_default_dvbt.cfg vendor/amlogic/apps/TvMiddleware/assets/tv_default.cfg
elif [ "$DVB_APP_CONFIG" = "isdbt" ]; then
export DVB_APP_CONFIG_ISDBT=true
cp -v vendor/amlogic/apps/TvMiddleware/assets/tv_default_isdbt.cfg vendor/amlogic/apps/TvMiddleware/assets/tv_default.cfg
fi
#tuner driver config
if [ "$DVB_TUNER_DRIVER" = "avl68xx" ]; then
export BOARD_COMPILE_DVB=true
export BUILD_WITH_TUNER_AVL68XX=true
cp -v custom/gxl_${PRODUCT_NAME}_1g_avl68xx.dts common/arch/arm64/boot/dts/amlogic/gxl_${PRODUCT_NAME}_1g.dts
cp -v custom/gxl_${PRODUCT_NAME}_2g_avl68xx.dts common/arch/arm64/boot/dts/amlogic/gxl_${PRODUCT_NAME}_2g.dts
elif [ "$DVB_TUNER_DRIVER" = "mxl683" ]; then
export BOARD_COMPILE_DVB=true
export BUILD_WITH_TUNER_MXL683=true
cp -v custom/gxl_${PRODUCT_NAME}_1g_mxl683.dts common/arch/arm64/boot/dts/amlogic/gxl_${PRODUCT_NAME}_1g.dts
cp -v custom/gxl_${PRODUCT_NAME}_2g_mxl683.dts common/arch/arm64/boot/dts/amlogic/gxl_${PRODUCT_NAME}_2g.dts
fi
fi

#其他的从cfg里面读取的配置
echo "========================== customize end ============================"
}

restore_config(){
  #把一些临时的修改还原
}

read_config(){
  #reading config from build.cfg
  echo "========== Reading build.cfg =========="
  echo
  while read line
    do
    comment=$(echo $line | cut -d "#" -f1);
 if [ -z $comment ]; then
  continue
 fi
 option=$(echo $line | cut -d "=" -f1);
 value=$(echo $line | cut -d "=" -f2);
 case $option in
  PRODUCT_NAME)
  PRODUCT_NAME=$value
  echo "PRODUCT_NAME is $value";;
  UBOOT_TARGET)
  UBOOT_TARGET=$value
  echo "UBOOT_TARGET is $value";;
  TYPE)
  TYPE=$value
  echo "TYPE is $value"
 if [[ "$TYPE" != "user" && "$TYPE" != "eng" && "$TYPE" != "userdebug" ]]; then
echo $NO_TYPE_WARNNING
 exit
fi;;
  DATA_UNIT)
  DATA_UNIT=$value
  echo "DATA_UNIT is $value";;
SECUREBOOT)
  if [ $value = "true" ]; then
  SECUREOS=true
export TARGET_USE_OPTEEOS=true
export SECUREBOOT=true
fi
echo "SECUREBOOT=$value";;
   BOARD_COMPILE)
 if [ $value = "atv" ]; then
   export BOARD_COMPILE_ATV=true
SECUREOS=true
 elif [ $value = "cts" ]; then
   export BOARD_COMPILE_CTS=true
 fi
 echo "BOARD_COMPILE=$value";;
DOLBY_SUPPORT)
 DOLBY_SUPPORT=$value
echo "DOLBY_SUPPORT=$value";;
BOARD_HAVE_BLUETOOTH)
if [ $value = "true" ]; then
export BOARD_HAVE_BLUETOOTH=true
fi
echo "BOARD_HAVE_BLUETOOTH=$value";;
BLUETOOTH_USR_RTK_BLUEDROID)
if [ "$BOARD_HAVE_BLUETOOTH" = "true" ]; then
if [ $value = "true" ]; then
export BLUETOOTH_USR_RTK_BLUEDROID=true
export BOARD_HAVE_BLUETOOTH_RTK=true
fi
echo "BLUETOOTH_USR_RTK_BLUEDROID=$value"
fi;;
BOARD_HAVE_WIFI)
if [ $value = "true" ]; then
export BOARD_HAVE_WIFI=true
fi
echo "BOARD_HAVE_WIFI=$value";;
CONFIG_ACS)
export CONFIG_ACS=$value
echo "CONFIG_ACS=$value";;
BOARD_PLAYREADY_LEVEL)
 if [ $value = 1 ]; then
   SECUREOS=true
   export BOARD_PLAYREADY_LEVEL=1
 elif [ $value = 3 ]; then
   export BOARD_PLAYREADY_LEVEL=3
 fi
 echo "BOARD_PLAYREADY_LEVEL=$value";;
BOARD_WIDEVINE_OEMCRYPTO_LEVEL)
 if [ $value = 1 ]; then
   SECUREOS=true
   export BOARD_WIDEVINE_OEMCRYPTO_LEVEL=1
 elif [ $value = 3 ]; then
   export BOARD_WIDEVINE_OEMCRYPTO_LEVEL=3
 fi
 echo "BOARD_WIDEVINE_OEMCRYPTO_LEVEL=$value";;
BUILD_WITH_VIEWRIGHT_WEB)
 if [ $value = "false" ]; then
   export BUILD_WITH_VIEWRIGHT_WEB=false
 elif [ $value = "true" ]; then
   SECUREOS=true
   export BUILD_WITH_VIEWRIGHT_WEB=true
 fi
 echo "BUILD_WITH_VIEWRIGHT_WEB=$value";;
BUILD_WITH_VIEWRIGHT_STB)
 if [ $value = "false" ]; then
   export BUILD_WITH_VIEWRIGHT_STB=false
 elif [ $value = "true" ]; then
   SECUREOS=true
   export BUILD_WITH_VIEWRIGHT_STB=true
 fi
 echo "BUILD_WITH_VIEWRIGHT_STB=$value";;
PPPOE)
echo "PPPOE=$value"
if [ $value = "true" ]; then
export BUILD_WITH_PPPOE=true
fi;;
PPPOE_DISPLAY)
echo "PPPOE_DISPLAY=$value"
if [ $value = "true" ]; then
export PPPOE_DISPLAY=true
fi;;
DVB_APP_CONFIG)
export DVB_APP_CONFIG=$value
echo "DVB_APP_CONFIG=$value";;
DVB_TUNER_DRIVER)
DVB_TUNER_DRIVER=$value
echo "DVB_TUNER_DRIVER=$value";;
ROOTPERMISSION)
ROOTPERMISSION=$value
echo "ROOTPERMISSION=$value"
export ROOTPERMISSION=$value;;
OPENGAPPS)
echo "OPENGAPPS=$value"
if [ $value = "pico" ]; then
export OPENGAPPS=$value
fi;;
NEED_FACTORY_TEST)
echo "NEED_FACTORY_TEST=$value"
export NEED_FACTORY_TEST=$value;;
PACKAGE_NAME_OF_LAUNCHER)
echo "PACKAGE_NAME_OF_LAUNCHER=$value"
export PACKAGE_NAME_OF_LAUNCHER=$value;;
SYSTEM_VERSION_NUMBER)
echo "SYSTEM_VERSION_NUMBER=$value"
export SYSTEM_VERSION_NUMBER=$value;;
MODEL_ID)
echo "MODEL_ID=$value"
export MODEL_ID=$value;;
OTA_UPDATE_REQUEST_URL)
echo "OTA_UPDATE_REQUEST_URL=$value"
export OTA_UPDATE_REQUEST_URL=$value;;
OPERATOR)
echo "OPERATOR=$value"
export OPERATOR=$value;;
MODEL)
echo "MODEL=$value"
export MODEL=$value;;
 esac
done < build.cfg
echo
echo "========== Reading build.cfg end =========="
echo
}

##### set_environment #####
set_environment(){
source build/envsetup.sh
  print_env
  lunch $PRODUCT_NAME-$TYPE-$DATA_UNIT
}

###### make uboot #####
build_uboot(){
  echo "====================== start building uboot ===================== "
  ##for secoure OS##
if [ "$SECUREOS" = "true" ]; then
 cp -v vendor/amlogic/tdk/secureos/gx/bl32.img uboot/fip/gxl/
else
rm -rvf uboot/fip/gxl/bl32.img
fi
  cd uboot/
  ./mk gxl_${UBOOT_TARGET}_v1
  cd ..
  cp -vrf uboot/fip/gxl/u-boot.bin device/amlogic/$PRODUCT_NAME/
  cp -vrf uboot/fip/gxl/u-boot.bin.sd.bin device/amlogic/$PRODUCT_NAME/upgrade/
  cp -vrf uboot/fip/gxl/u-boot.bin.usb.bl2 device/amlogic/$PRODUCT_NAME/upgrade/
  cp -vrf uboot/fip/gxl/u-boot.bin.usb.tpl device/amlogic/$PRODUCT_NAME/upgrade/
}

may_build_uboot(){
if [ "$UBOOT" = true ]; then
echo "====================== start building uboot image ===================== "
 build_uboot
fi
}

###### make ota package #####
build_android(){
  echo "====================== start building Android ===================== "
  if [ -z $J ]; then
    J=8
  fi
  echo "-jN = -j$J"
  make otapackage -j$J
}

may_build_bootimage(){
if [ "$BOOT" = true ]; then
echo "====================== start building boot image ===================== "
   make bootimage
fi
}

may_build_dtbimage(){
if [ "$DTB" = true ]; then
echo "====================== start building dtb image ===================== "
   make dtbimage
fi
}

may_build_android(){
if [ "$ANDROID" = true ]; then
   build_android
   rename_output_target
fi
}

may_build_clean(){
if [ "$CLEAN" = true ]; then
 echo "====================== start clean ===================== "
 cd uboot/
 make distclean
 cd ..
 make clean
 rm -rf out/
 restore_config
 ./clean.sh
fi
}

rename_output_target(){
if [ $? -eq 0 ]; then
out_dir="out/target/product/${PRODUCT_NAME}"
ota_dir="${out_dir}/obj/PACKAGING/target_files_intermediates"
if [ -d "$out_dir" ]; then
if [ -f "$out_dir/aml_upgrade_package.img" ]; then
NOW=$(date +%Y%m%d%H%M)
cp -v $out_dir/aml_upgrade_package.img out_img_ota_files/aml_upgrade_package_${PRODUCT_NAME}_$NOW.img
cp -v $ota_dir/${PRODUCT_NAME}-target_files-$(date +%Y%m%d).zip out_img_ota_files/${PRODUCT_NAME}-target_files-$NOW.zip
cp -v $out_dir/${PRODUCT_NAME}-ota-$(date +%Y%m%d).zip out_img_ota_files/${PRODUCT_NAME}-ota-$NOW.zip
fi
fi
croot
echo
fi
}

fire_up(){
#1. check if need to print help
may_print_help
#2. read configs from build.cfg
read_config
#3. set environment according to the reading result
set_environment
#4. check if need to make clean before build
may_build_clean
#5. do some customization job before making image
custom_config
#6. check if need to build uboot
may_build_uboot
#7. check if need to build kenel (boot iamge)
may_build_bootimage
#8. check if need to build dtb image
may_build_dtbimage
#9. check if need to build android
may_build_android
}

#################### functions end ####################

fire_up
5楼#
发布于:2021-09-01 11:03
这论坛的排版这是。。。
6楼#
发布于:2021-09-02 09:12
Pazival:这论坛的排版这是。。。回到原帖
可以长传到网盘或者GitHub gitee之类的再分享链接
[url]http://190.lsal.cn/195/1329.gif?0728100424873[/url]
7楼#
发布于:2021-09-02 10:22
我前段时间也改了下编译脚本,972都用marconi目录编译,基本通用功能都放在common目录的software或者marconi.mk里,差异化的功能放在每个项目mk中,因为客户的差异化需求无非就是bootloader, bootlogo, bootanimation, prebuilt app, 系统属性,语言,时区。
游客

返回顶部