NuttX 安装脚本

(2014年4月12日修改)
(嵌入式 实时操作系统 rtos nuttx 开发环境 交叉工具链 shell脚本)

NuttX 安装脚本

nuttx_building_v12.sh
转载请注明出处: http://blog.csdn.net/zhumaill/article/details/22821843

已测试的 Linux 发行版:
        linuxmint-16-cinnamon-64bit/32bit
        ubuntu-13.10-amd64/i386
        debian-7.4-gnome-amd64/i386
        debian-7.4-xfce-amd64/i386(要使用 Qt Creator 或 Eclipse 需额外的设置)
        crunchbang-11-amd64/i686
#!/bin/bash

#nuttx7.1在linuxmint-16-cinnamon-64bit上测试通过

#设置
BASEDIR=`pwd`
TOPDIR=my_nuttx
BOARD=shenzhou
TARGETCONFIG=shenzhou/nsh
COMMIT=nuttx-7.1

gmp_name=gmp-5.1.3.tar.xz
mpfr_name=mpfr-3.1.2.tar.xz
mpc_name=mpc-1.0.2.tar.gz
binutils_name=binutils-2.24.tar.bz2
gcc_name=gcc-4.8.2.tar.bz2
gmp_basename=`echo $gmp_name | awk -F. 'NF-=2' OFS=.`
mpfr_basename=`echo $mpfr_name | awk -F. 'NF-=2' OFS=.`
mpc_basename=`echo $mpc_name | awk -F. 'NF-=2' OFS=.`
binutils_basename=`echo $binutils_name | awk -F. 'NF-=2' OFS=.`
gcc_basename=`echo $gcc_name | awk -F. 'NF-=2' OFS=.`

#“按任意键继续。。。”的处理函数
get_char()
{
  SAVEDSTTY=`stty -g`
  stty -echo
  stty raw
  dd if=/dev/tty bs=1 count=1 2> /dev/null
  stty -raw
  stty echo
  stty $SAVEDSTTY
}

usage()
{
cat <<EOF
  -d             下载安装并创建压缩包->编译nuttx
  -e             从本地压缩包安装->编译nuttx
  -m             编译nuttx

EOF
}

if [ -z "$1" ]; then
  usage
  exit 1
fi

DOWNLOAD=no
EXTRACT=no
MAKE_NUTTX=no

while getopts debcma OPT
do
  case "$OPT" in
    d) DOWNLOAD=yes; MAKE_NUTTX=yes;;
    e) EXTRACT=yes; MAKE_NUTTX=yes;;
    m) MAKE_NUTTX=yes;;
    *) usage; exit 1;;
  esac
done



if [ $DOWNLOAD = 'yes' ] || [ $EXTRACT = 'yes' ]; then

echo "安装依赖项"
sudo apt-get -d update 2>&1 | tee apt-get_update.log
if (grep -q -E '^E:|^Err|^W:' apt-get_update.log); then
  if (grep -q '/var/lib/apt/lists/lock' apt-get_update.log); then
    sudo rm -vf /var/lib/apt/lists/*
    sudo apt-get update
    if [ $? -ne 0 ]; then
      echo "更新源出现错误,请稍候重试1"
      exit 1
    fi
  else
    echo "更新源出现错误,请稍候重试2"
    exit 1
  fi
fi
sudo apt-get install -y git build-essential bison flex gcc-multilib zlib1g-dev gperf libncurses5-dev texinfo autoconf automake libtool curl gettext libgmp-dev pkg-config
if [ $? -ne 0 ]; then
  echo "安装依赖项出现错误,请稍候重试1"
  exit 1
fi
sudo apt-get install -y  libgmp3-dev libmpc-dev libmpfr-dev  #linuxmint、ubuntu不需要
if [ $? -ne 0 ]; then
  echo "安装依赖项出现错误,请稍候重试2"
  exit 1
fi
#echo "按任意键继续。。。" && char=`get_char`



if [ $DOWNLOAD = 'yes' ]; then
  echo "下载安装并创建压缩包"
  which proz > /dev/null 2>&1
  if [ $? -ne 0 ]; then
  echo "安装多线程下载工具"
    wget -c http://dxdown1.onlinedown.net/down/prozilla-2.0.4.tar.zip
    unzip prozilla-2.0.4.tar.zip prozilla-2.0.4.tar.bz2
    tar -xf prozilla-2.0.4.tar.bz2
    rm prozilla-2.0.4.tar.zip prozilla-2.0.4.tar.bz2
    cd prozilla-2.0.4
    sed -i 's/DL_Window:://' src/download_win.h
    ./configure
    make
    sudo make install
    cd ..
  fi
  which proz > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "安装多线程下载工具出现错误,中止运行"
    exit 1
  fi
  if [ -d "$TOPDIR" ]; then
    echo "错误:目录$TOPDIR已经存在"
    exit 1
  fi
  mkdir $TOPDIR
  cd $TOPDIR

  down_cmd='proz -k=20 '
  host1='http://mirrors.ustc.edu.cn/gnu'
  #host2='http://mirror.bjtu.edu.cn/gnu'
  $down_cmd$host1/gmp/$gmp_name
  $down_cmd$host1/mpfr/$mpfr_name
  $down_cmd$host1/mpc/$mpc_name
  $down_cmd$host1/binutils/$binutils_name
  $down_cmd$host1/gcc/$gcc_basename/$gcc_name

  session_id=`curl http://sourceforge.net/p/nuttx/git/ci/$COMMIT/tree/  | awk -F \" '/_session_id/{for(i=1; i<=NF; i++) if($i ~ /value/) print $i+1}'`
  for ((i=1; i<=10; i++)); do
    echo "获取http下载地址第$i次尝试"
    nuttx_http_url=`curl -d "path=&_session_id=$session_id" http://sourceforge.net/p/nuttx/git/ci/$COMMIT/tarball | awk -F \' '/window.location.href/{for(i=1; i<=NF; i++) if($i ~ /nuttx-git-/) print $i}'`
    if [ -n "$nuttx_http_url" ]; then
      break
    fi
    sleep 4s
  done
  echo "nuttx_http_url=$nuttx_http_url"

  $down_cmd$nuttx_http_url
  if [ $? -ne 0 ]; then
    echo "http下载失败,使用git下载"
    git clone -b $COMMIT git://git.code.sf.net/p/nuttx/git nuttx-code
    if [ $? -ne 0 ]; then
      echo "git下载失败,中止执行"
      exit 1
    fi
    mv nuttx-code/* .
    rm -rf nuttx-code
  else
    nuttx_http_pkg=`echo $nuttx_http_url | awk -F '/' '{print $NF}'`
    nuttx_http_dir=`echo $nuttx_http_pkg | awk -F '.' '{print $1}'`
    unzip -q $nuttx_http_pkg 
    mv $nuttx_http_dir/* .
    rm -rf $nuttx_http_pkg $nuttx_http_dir
  fi

  cd ..
  echo "打包..."
  tar -zcf $TOPDIR-$(date +%Y%m%d-%H%M%S).tar.gz $TOPDIR
                     
else
  echo "从本地压缩包安装"
  nuttx_pkg=`find . -maxdepth 1 -type f -name "$TOPDIR-????????-??????.tar.gz" | sort -r | head -1`
  if [ -z "$nuttx_pkg" ]; then
    echo "错误:没有找到压缩包,中止执行"
    exit 1
  fi
  if [ -d "$TOPDIR" ]; then
    echo "错误:目录$TOPDIR已经存在"
    exit 1
  fi
  echo "解包..."
  tar -xf $nuttx_pkg
fi
#echo "按任意键继续。。。" && char=`get_char`

echo "编译安装kconfig-frontends"
cd $BASEDIR/$TOPDIR/misc/tools/kconfig-frontends
./bootstrap
./configure --enable-mconf
LD_RUN_PATH=/usr/local/lib make
if [ $? -ne 0 ]; then
  echo "编译安装kconfig-frontends出现错误,中止执行"
  exit 1
fi
#echo "按任意键继续。。。" && char=`get_char`
sudo make install

echo "编译库"
cd $BASEDIR/$TOPDIR
tar -xf $gmp_name
tar -xf $mpfr_name
tar -xf $mpc_name

mkdir b-gmp
cd b-gmp
../$gmp_basename/configure && make && sudo make install
cd ..
mkdir b-mpfr
cd b-mpfr
../$mpfr_basename/configure && make && sudo make install
cd ..
mkdir b-mpc
cd b-mpc
../$mpc_basename/configure && make && sudo make install
cd ..

echo "nuttx配置"
cd $BASEDIR/$TOPDIR/nuttx/tools
./configure.sh $TARGETCONFIG
if [ $? -ne 0 ]; then
  echo "nuttx配置出现错误,中止执行"
  exit 1
fi

echo "删除多余的目录"
cd $BASEDIR/$TOPDIR/nuttx
cp -r ../nuttx ../nuttx_backup

del_dir=`find configs -maxdepth 1 -type d`
del_dir=`echo $del_dir | sed "s,configs,,"`
del_dir=`echo $del_dir | sed "s,configs/$BOARD,,"`
for i in $del_dir; do
  echo "删除$i"
  rm -rf $i
  sed -i "s,^source \"$i/Kconfig\",# &," configs/Kconfig
done

del_dir=`find arch -maxdepth 1 -type d`
del_dir=`echo $del_dir | sed "s,arch,,"`
retain_dir=`awk -F \" '/^CONFIG_ARCH=/{print $2}' .config`
del_dir=`echo $del_dir | sed "s,arch/$retain_dir,,"`
for i in $del_dir; do
  echo "删除$i"
  rm -rf $i
  sed -i "s,^source $i/Kconfig,# &," arch/Kconfig
done

if [ $retain_dir="arm" ]; then
  del_dir=`find arch/arm/src -maxdepth 1 -type d`
  del_dir=`echo $del_dir | sed "s,arch/arm/src,,"`
  del_dir=`echo $del_dir | sed "s,arch/arm/src/common,,"`
  retain_dir=`awk -F \" '/^CONFIG_ARCH_FAMILY=/{print $2}' .config`
  del_dir=`echo $del_dir | sed "s,arch/arm/src/$retain_dir,,"`
  retain_dir=`awk -F \" '/^CONFIG_ARCH_CHIP=/{print $2}' .config`
  del_dir=`echo $del_dir | sed "s,arch/arm/src/$retain_dir,,"`
  for i in $del_dir; do
    echo "删除$i"
    rm -rf $i
    sed -i "s,^source $i/Kconfig,# &," arch/arm/Kconfig
  done
fi
#echo "按任意键继续。。。" && char=`get_char`

echo "编译buildroot"
cd $BASEDIR/$TOPDIR/nuttx
sed -i 's,^CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYW=y,# CONFIG_ARMV7M_TOOLCHAIN_CODESOURCERYW is not set,' .config
sed -i 's,^# CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT is not set,CONFIG_ARMV7M_TOOLCHAIN_BUILDROOT=y,' .config
make context
if [ $? -ne 0 ]; then
  echo "编译buildroot出现错误,中止执行1"
  exit 1
fi
#echo "按任意键继续。。。" && char=`get_char`

cd $BASEDIR/$TOPDIR/misc/buildroot
sed -i 's,$(WGET) -P $(DL_DIR) $(BINUTILS_SITE)/$(BINUTILS_SOURCE),mv $(TOPDIR)/../../$(BINUTILS_SOURCE) $(DL_DIR),' toolchain/binutils/binutils.mk
sed -i 's,$(WGET) -P $(DL_DIR) $(GCC_SITE)/$(GCC_SOURCE),mv $(TOPDIR)/../../$(GCC_SOURCE) $(DL_DIR),' toolchain/gcc/gcc-nuttx-4.x.mk
cp configs/cortexm3-eabi-defconfig-4.8.2 .config
sed -i 's,BR2_PACKAGE_NXFLAT=y,# BR2_PACKAGE_NXFLAT is not set,' .config
sed -i '/BR2_PACKAGE_GENROMFS/a# BR2_PACKAGE_KCONFIG_FRONTENDS is not set\n# BR2_NCONF_FRONTEND is not set\n# BR2_GCONF_FRONTEND is not set\n# BR2_QCONF_FRONTEND is not set' .config

make oldconfig
if [ $? -ne 0 ]; then
  echo "编译buildroot出现错误,中止执行2"
  exit 1
fi
#echo "按任意键继续。。。" && char=`get_char`
make
if [ $? -ne 0 ]; then
  echo "编译buildroot出现错误,中止执行3"
  exit 1
fi
#echo "按任意键继续。。。" && char=`get_char`

fi  #end [ $DOWNLOAD = 'yes' ] || [ $EXTRACT = 'yes' ]



if [ $MAKE_NUTTX = 'yes' ]; then

echo "编译nuttx"
cd $BASEDIR/$TOPDIR/nuttx
sed -i 's,^# CONFIG_RAW_BINARY is not set,CONFIG_RAW_BINARY=y,' .config
sed -i 's,^CONFIG_TASK_NAME_SIZE=0,CONFIG_TASK_NAME_SIZE=16,' .config
sed -i 's,^CONFIG_NSH_IPADDR=0x0a000002,CONFIG_NSH_IPADDR=0xc0a8020a,' .config    #192.168.2.10
sed -i 's,^CONFIG_NSH_DRIPADDR=0x0a000001,CONFIG_NSH_DRIPADDR=0xc0a80201,' .config    #192.168.2.1
if !(grep -q '	@echo "DUMP: $(BIN).out"' Makefile.unix); then
  sed -i '/pass2:/,/^$/{
    /^$/i\	@echo "DUMP: $(BIN).out"
    /^$/i\	$(Q) $(OBJDUMP) -x $(BIN) > $(HOME)/$(BIN).out
    /^$/i\	@echo "DUMP: $(BIN).S"
    /^$/i\	$(Q) $(OBJDUMP) -d -j .text -j .init_section -j .ARM.exidx -j .data -j .bss $(BIN) > $(HOME)/$(BIN).S
    /^$/i\	cp -f $(BIN) $(BIN).hex $(BIN).bin $(HOME)
  }' Makefile.unix
fi

TOOLCHAIN_BIN=$BASEDIR/$TOPDIR/misc/buildroot/build_arm_nofpu/staging_dir/bin
TOOLS_DIR=$BASEDIR/$TOPDIR/nuttx/configs/$BOARD/tools
if !(grep -q "PATH=\"$TOOLCHAIN_BIN:$TOOLS_DIR:\$PATH\"" $HOME/.profile); then
  echo >> $HOME/.profile
  echo PATH=\"$TOOLCHAIN_BIN:$TOOLS_DIR:\$PATH\" >> $HOME/.profile  #重起生效
fi
if !(echo $PATH | grep -q "$TOOLCHAIN_BIN:$TOOLS_DIR"); then
  export PATH=$TOOLCHAIN_BIN:$TOOLS_DIR:$PATH
fi

make

fi  #end MAKE_NUTTX

time=$SECONDS
hour=$[$time/3600]
min=$[$[$time/60]%60]
sec=$[$time%60]
echo "执行时间:$hour小时$min分$sec秒"

使用上述脚本安装 NuttX 之后,在 Qt Creator 或 Eclipse 中以导入现有项目的方式新建项目,无需其它设置,点击构建按钮即可进行编译。

你可能感兴趣的:(shell脚本,开发环境,NuttX,RTOS,交叉工具链)