天天看點

CONFIGURE NuttX過程分析前言

前言

在使用Nuttx的過程中,第一步就是下載下傳好源代碼然後搭建編譯環境,具體可以參考這篇外國小哥的部落格CONFIGURE NuttX,具體的搭建過程就不詳細叙述了,這次主要是記錄下配置的過程。主要涉及的内容如下:

  • nuttx/tools/configure.sh分析
  • ${boardconfig}下的defconfig和Make.def

nuttx/tools/configure.sh

在明确開發闆類型以後,我們采用上面超連結裡面提到的步驟來配置:

  • cd nuttx/nuttx-code/nuttx/tools
  • **./configure.sh stm32f100rc_generic/nsh **

    到這裡我們就直接去看看./configure.sh到底幹了些什麼。。。

    先貼上腳本~

#!/bin/bash
# configure.sh
#
#   Copyright (C) 2007, 2008, 2011, 2015 Gregory Nutt. All rights reserved.
#   Author: Gregory Nutt <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

WD=`pwd`
TOPDIR="${WD}/.."		#通過目前路徑找到頂層目錄
USAGE="	

USAGE: ${0} [-d] [-a <app-dir>] <board-name>/<config-name>

Where:
  <board-name> is the name of the board in the configs directory
  <config-name> is the name of the board configuration sub-directory
  <app-dir> is the path to the apps/ directory, relative to the nuttx directory

"
#上面這段是用來告訴我們如何使用這個腳本的(使用說明)
           

接着來分析,先補充點小知識:

./configure.sh stm32f100rc_generic/nsh

-----------$0 ------------------------$1

就是說

$0=“./configure.sh”

$1=“stm32f100rc_generic/nsh”

好了繼續貼:

# Parse command arguments

unset boardconfig
unset appdir

while [ ! -z "$1" ]; do
  case "$1" in
    -d )                    #“stm32f100rc_generic/nsh”如果為路徑
      set -x                #設定代碼執行過程中回顯
      ;;
    -h )
      echo "$USAGE"         #“stm32f100rc_generic/nsh”如果為軟連結
      exit 0
      ;;
    -a )                    #“stm32f100rc_generic/nsh”如果存在
      shift
      appdir=$1
      ;;
    *)                      #\$1是任何符号,哪怕是空
      if [ ! -z "${boardconfig}" ]; then  #如果${boardconfig}已經存在了
        echo ""
        echo "<board/config> defined twice"
        echo "$USAGE"
        exit 1
      fi
      boardconfig=$1
      ;;
  esac
  shift
done
           

執行完這一段腳本,其實就是處理傳入的$1的參數,也就是目标闆的配置路徑,是否合法,并記錄路徑;

好,接着來執行下一段:

# Sanity checking

if [ -z "${boardconfig}" ]; then #${boardconfig}如果為空,也就說我們在開始的時候沒有傳入參數,例如這樣./configure.sh
  echo ""
  echo "Missing <board/config> argument"
  echo "$USAGE"
  exit 2
fi

configpath=${TOPDIR}/configs/${boardconfig}#一旦有目标闆的合法位址傳進來,那麼完整記錄到configpath
if [ ! -d "${configpath}" ]; then
  echo "Directory ${configpath} does not exist.  Options are:"
  echo ""
  echo "Select one of the following options for <board-name>:"
  configlist=`find ${TOPDIR}/configs -name defconfig`
  for defconfig in $configlist; do
    config=`dirname $defconfig | sed -e "s,${TOPDIR}/configs/,,g"`
    echo "  $config"
  done
  echo ""
  echo "$USAGE"
  exit 3
fi

#接下來的操作就是重點了,先講講目的,我們配置目标開發的最終其實就是操作三個檔案:
#1、Make.defs
#2、defconfig
#3、setenv.sh
#是以接下來說做的一切就是把目标闆下面的這兩個檔案設定到我們頂層目錄下。

src_makedefs="${configpath}/Make.defs"	#目标闆配置路徑下的Make.defs
dest_makedefs="${TOPDIR}/Make.defs"		#主目錄下的Make.defs

#${src_makedefs}如果沒有可讀權限的話,那就拜拜了,直接exit。
if [ ! -r "${src_makedefs}" ]; then
  echo "File \"${src_makedefs}\" does not exist"
  exit 4
fi

#${src_makedefs}可以讀的話,就把setenv.sh的路徑記下來吧~
src_setenv="${configpath}/setenv.sh"
unset have_setenv

#${src_setenv}通過讀權限來是别是腳本還是批處理檔案,然後記錄下頂層的setenv.sh路徑,儲存在dest_setenv
if [ -r "${src_setenv}" ]; then
  dest_setenv=${TOPDIR}/setenv.sh
  have_setenv=y
else
  src_setenv="${configpath}/setenv.bat"
  if [ -r "${src_setenv}" ]; then
    dest_setenv=${TOPDIR}/setenv.bat
    have_setenv=y
  else
    unset src_setenv
  fi
fi

#接下來目标defconfig
src_config="${configpath}/defconfig"
dest_config="${TOPDIR}/.config"

if [ ! -r "${src_config}" ]; then
  echo "File \"${src_config}\" does not exist"
  exit 6
fi

           
走玩上面這段腳本,我們基本上都能發現目的了,總結下,到目前為止都得到了哪些資訊:
WD=`pwd`
TOPDIR="${WD}/.."
USAGE="

USAGE: ${0} [-d] [-a <app-dir>] <board-name>/<config-name>

Where:
  <board-name> is the name of the board in the configs directory
  <config-name> is the name of the board configuration sub-directory
  <app-dir> is the path to the apps/ directory, relative to the nuttx directory

"

# Parse command arguments

unset boardconfig
unset appdir

boardconfig=$1

# Sanity checking

configpath=${TOPDIR}/configs/${boardconfig}

src_makedefs="${configpath}/Make.defs"
dest_makedefs="${TOPDIR}/Make.defs"

src_setenv="${configpath}/setenv.sh"

unset have_setenv
dest_setenv=${TOPDIR}/setenv.sh
have_setenv=y

src_config="${configpath}/defconfig"
dest_config="${TOPDIR}/.config"

           

好的繼續往下,腳步不要停~~~~~~

# Extract values needed from the defconfig file.  We need:
# (1) The CONFIG_WINDOWS_NATIVE setting to know it this is target for a
#     native Windows (meaning that we want setenv.bat vs setenv.sh and we need
#     to use backslashes in the CONFIG_APPS_DIR setting).
# (2) The CONFIG_APPS_DIR setting to see if there is a configured location for the
#     application directory.  This can be overridden from the command line.

winnative=`grep CONFIG_WINDOWS_NATIVE= "${src_config}" | cut -d'=' -f2`

defappdir=y
if [ -z "${appdir}" ]; then
  quoted=`grep "^CONFIG_APPS_DIR=" "${src_config}" | cut -d'=' -f2`
  if [ ! -z "${quoted}" ]; then
    appdir=`echo ${quoted} | sed -e "s/\"//g"`
    defappdir=n
  fi
fi
#啥意思有點看不懂,沒事抓重點:
#quoted=`grep "^CONFIG_APPS_DIR=" "${src_config}" | cut -d'=' -f2`
#appdir=`echo ${quoted} | sed -e "s/\"//g"`
#defappdir=n


# Check for the apps/ directory in the usual place if appdir was not provided

if [ -z "${appdir}" ]; then

  # Check for a version file

  unset CONFIG_VERSION_STRING
  if [ -x "${TOPDIR}/.version" ]; then
    . "${TOPDIR}/.version"
  fi

  # Check for an unversioned apps/ directory

  if [ -d "${TOPDIR}/../apps" ]; then
    appdir="../apps"
  else
    # Check for a versioned apps/ directory

    if [ -d "${TOPDIR}/../apps-${CONFIG_VERSION_STRING}" ]; then
      appdir="../apps-${CONFIG_VERSION_STRING}"
    fi
  fi
fi
           

饒了一大圈最後就是要從"${configpath}/defconfig"找到看看有沒有定義appdir的路徑~~~

繼續往下:

# For checking the apps dir path, we need a POSIX version of the relative path.
#還是在設定關于appdir
posappdir=`echo "${appdir}" | sed -e 's/\\\\/\\//g'`
winappdir=`echo "${appdir}" | sed -e 's/\\//\\\\/g'`

# If appsdir was provided (or discovered) then make sure that the apps/
# directory exists

if [ ! -z "${appdir}" -a ! -d "${TOPDIR}/${posappdir}" ]; then
  echo "Directory \"${TOPDIR}/${posappdir}\" does not exist"
  exit 7
fi

# Okay... Everything looks good.  Setup the configuration
#我來翻譯,搞了大半天終于設定好了各種路徑資訊,來吧~挽起袖子直接幹吧!!!
#{code1} || {code2},啥意思{code1}執行正确了,後面就不用執行了,否則{code2}執行
#幹啥呢,就是把${src_makedefs}複制到${dest_makedefs},再把權限改成644;

install -m 644 "${src_makedefs}" "${dest_makedefs}" || \
  { echo "Failed to copy \"${src_makedefs}\"" ; exit 7 ; }
if [ "X${have_setenv}" = "Xy" ]; then
  install "${src_setenv}" "${dest_setenv}" || \
    { echo "Failed to copy ${src_setenv}" ; exit 8 ; }
  chmod 755 "${dest_setenv}"
fi
install -m 644 "${src_config}" "${dest_config}" || \
  { echo "Failed to copy \"${src_config}\"" ; exit 9 ; }
#處理完Make.defs、setenv.sh、defconfig。大功告成,後面的就是提示資訊了

# If we did not use the CONFIG_APPS_DIR that was in the defconfig config file,
# then append the correct application information to the tail of the .config
# file

if [ "X${defappdir}" = "Xy" ]; then
  # In-place edit can mess up permissions on Windows
  # sed -i -e "/^CONFIG_APPS_DIR/d" "${dest_config}"
  sed -e "/^CONFIG_APPS_DIR/d" "${dest_config}" > "${dest_config}-temp"
  mv "${dest_config}-temp" "${dest_config}"

  echo "" >> "${dest_config}"
  echo "# Application configuration" >> "${dest_config}"
  echo "" >> "${dest_config}"
  if [ "X${winnative}" = "Xy" ]; then
    echo "CONFIG_APPS_DIR=\"$winappdir\"" >> "${dest_config}"
  else
    echo "CONFIG_APPS_DIR=\"$posappdir\"" >> "${dest_config}"
  fi
fi

           
./configure 到這裡就執行完了,是以能看出來的就是,主要是把目标闆下的3個預設檔案copy到頂層目錄下的指定檔案中去,接下在make的時候就能起作用啦。