run_riscv.sh 7.48 KB
#!/bin/bash

LLVM_NAME=llvm
LLVM_VERSION=13.0.0
LLVM_SERVER="https://github.com/llvm/llvm-project.git"
#LLVM_GIT_BRANCH="release/10.x"
#LLVM_GIT_COMMIT="ef32c611aa214dea855364efd7ba451ec5ec3f74"
#LLVM_GIT_COMMIT="3186b18b998124a6b577f8274a10b7ee8b634c18"

# release/13.x (2021/09/06)
LLVM_GIT_BRANCH="release/13.x"
LLVM_GIT_COMMIT="181739213aa00de40a5bc6fbb1c578ce25b78d39"

DAITEQ_BRANCH="daiteq"

TGT_TRIPLE=riscv64-daiteq-none-elf
HOST_TRIPLE=x86_64-linux-gnu

CURDIR="$(pwd)"
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# number of cores used for make
MAKE_CORES=$(nproc --all)


DIR_SRCS=${CURDIR}/source
DIR_BLD=${CURDIR}/build-riscv
DIR_INST=${CURDIR}/install-riscv
DIR_PTCH=${SCRIPTDIR}/patches
DIR_DEBUG=${CURDIR}/debug-riscv

GENDIRS="${DIR_SRCS} ${DIR_BLD} ${DIR_INST}"

#DN_BUUNP=${DIR_UPCK}/${BU_NAME}-${BU_VERSION}

CCMAKE_ARGS="-D CMAKE_BUILD_TYPE=Release \
             -D BUILD_SHARED_LIBS=ON \
             -D LLVM_ENABLE_Z3_SOLVER=OFF \
             -D CMAKE_INSTALL_PREFIX=${DIR_INST} \
             -D LLVM_DEFAULT_TARGET_TRIPLE=${TGT_TRIPLE} \
             -D LLVM_EXTERNAL_CLANG_SOURCE_DIR=${DIR_SRCS}/clang \
             -D LLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR=${DIR_SRCS}/clang-tools-extra \
             -D LLVM_EXTERNAL_COMPILER_RT_SOURCE_DIR=${DIR_SRCS}/compiler-rt \
             -D LLVM_INCLUDE_DOCS=OFF \
             -D LLVM_ENABLE_OCAMLDOC=OFF \
             -D LLVM_INSTALL_BINUTILS_SYMLINKS=ON \
             -D LLVM_TARGETS_TO_BUILD=RISCV \
             -D LLVM_TARGET_ARCH=riscv64 \
             -D LLVM_TOOL_CLANG_BUILD=ON \
             -D LLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=ON \
             -D CLANG_INCLUDE_TESTS=ON \
             -D CLANG_VENDOR=daiteq-esa \
             -D LLVM_TOOL_COMPILER_RT_BUILD=OFF"
#             -D COMPILER_RT_USE_BUILTINS_LIBRARY=ON 

CCMAKE_DEBUG_ARGS="-D CMAKE_BUILD_TYPE:STRING=Debug \
             -D LLVM_ENABLE_ASSERTIONS:BOOL=ON \
             -D BUILD_SHARED_LIBS=ON \
             -D LLVM_ENABLE_Z3_SOLVER=OFF \
             -D CMAKE_INSTALL_PREFIX=${DIR_INST} \
             -D LLVM_DEFAULT_TARGET_TRIPLE=${TGT_TRIPLE} \
             -D LLVM_EXTERNAL_CLANG_SOURCE_DIR=${DIR_SRCS}/clang \
             -D LLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR=${DIR_SRCS}/clang-tools-extra \
             -D LLVM_EXTERNAL_COMPILER_RT_SOURCE_DIR=${DIR_SRCS}/compiler-rt \
             -D LLVM_INCLUDE_DOCS=OFF \
             -D LLVM_ENABLE_OCAMLDOC=OFF \
             -D LLVM_INSTALL_BINUTILS_SYMLINKS=ON \
             -D LLVM_TARGETS_TO_BUILD=RISCV \
             -D LLVM_TARGET_ARCH=riscv64 \
             -D LLVM_TOOL_CLANG_BUILD=ON \
             -D LLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=ON \
             -D CLANG_INCLUDE_TESTS=ON \
             -D CLANG_VENDOR=daiteq-esa \
             -D LLVM_TOOL_COMPILER_RT_BUILD=OFF"


prepare_dirs() {
  for d in ${GENDIRS}
  do
    echo "Create directory ${d}"
    mkdir -p ${d}
  done
}

download_source() {
  mkdir -p ${DIR_SRCS}
  if [ "$(ls -A ${DIR_SRCS})" ]
  then
    echo "Repository is already cloned ... check consistency"
  else
    git clone --progress -n --branch ${LLVM_GIT_BRANCH} ${LLVM_SERVER} ${DIR_SRCS}
  fi
  cd ${DIR_SRCS}
  # is there already working branch
  if [ $(git branch --list ${DAITEQ_BRANCH}) ]
  then
    # already exists
    git checkout ${DAITEQ_BRANCH}
  else
    # no work branch
    git reset --hard ${LLVM_GIT_COMMIT}
    git branch  ${DAITEQ_BRANCH}
    git checkout ${DAITEQ_BRANCH}
  fi
  cd ${CURDIR}
}

apply_patches() {
  if [ ! -e ${DIR_PTCH} ]
  then
    echo "There is not a directory with patches"
    exit 1
  fi
  cd ${DIR_SRCS}
  # change branch to ours
  git checkout ${DAITEQ_BRANCH}
  # reset to the source LLVM commit
  git reset --hard ${LLVM_GIT_COMMIT}
  # check applying patches
  
  for p in `ls ${DIR_PTCH}/*.txt | sort -V`
  do
    echo "Use patch ${p}"
    git apply --check --whitespace=fix ${p}
    echo vysledek = $?
    if [[ $? != 0 ]]; then
      echo " Patch cannot be applied."
      cd ${CURDIR}
      break
    fi
    git am --whitespace=fix ${p}
  done
  cd ${CURDIR}
}

do_configure() {
  if [ ! -e ${DIR_SRCS} ]
  then
    echo "There is no unpacked files with source codes"
    exit 1
  fi

  mkdir -p ${DIR_BLD}
  cd ${DIR_BLD}
  CONF=1
  if [ -f CMakeCache.txt ]
  then
    echo "Compilation is already configured - try it again."
    #CONF=0
  fi

  if [ ${CONF} -eq 1 ]
  then
    cmake ${CCMAKE_ARGS} ${DIR_SRCS}/llvm
    if [ $? -ne 0 ]
    then
      echo "E: configure failed"
      exit 1
    fi
  fi
}

build_source() {
  mkdir -p ${DIR_BLD}
  cd ${DIR_BLD}

  if [ ! -f Makefile ]
  then
    echo "Compilation is not configured - run configuration."
    do_configure
  fi

  make -j ${MAKE_CORES}
  if [ $? -ne 0 ]
  then
    echo "E: make failed"
    exit 1
  fi
  cd ${CURDIR}
}

build_debug() {
  if [ ! -e ${DIR_SRCS} ]
  then
    echo "There is no unpacked files with source codes"
    exit 1
  fi

  mkdir -p ${DIR_DEBUG}
  cd ${DIR_DEBUG}
  if [ -f CMakeCache.txt ]
  then
    echo "Compilation is already configured - try it again."
    #CONF=0
  else
    cmake ${CCMAKE_DEBUG_ARGS} ${DIR_SRCS}/llvm
    if [ $? -ne 0 ]
    then
      echo "E: configure failed"
      exit 1
    fi
  fi
  make -j ${MAKE_CORES}
  if [ $? -ne 0 ]
  then
    echo "E: make debug failed"
    exit 1
  fi
  cd ${CURDIR}
}

install_pkg() {
  mkdir -p ${DIR_INST}
  cd ${DIR_BLD}
  make install
  cd ${CURDIR}
}

clean_build() {
  echo "Removing build and install directories."
  rm -rf ${DIR_BLD} ${DIR_INST}
}

clean_unpacked() {
  echo "Removing patches from the repository."
  cd ${DIR_SRCS}
  # change branch to ours
  git checkout ${DAITEQ_BRANCH}
  # reset to the source LLVM commit
  git reset --hard ${LLVM_GIT_COMMIT}
  # check applying patches
  cd ${CURDIR}
}

distclean_all() {
  echo "Removing all download and generated files and directories."
  rm -rf ${GENDIRS}
}

# ##############################################################################
print_help() {
  echo "The script needs one argument - required operation:"
  echo "  all       - do all steps (get,patch,config,build,install)"
  echo "  get       - download source package if it doesn't exist"
  echo "  patch     - patch original package"
  echo "  config    - perform configuration in build directory (cmake)"
  echo "  build     - build LLVM package (make)"
  echo "  install   - install LLVM to the output directory"
  echo "  clean     - remove generated files (build and install dirs.)"
  echo "  packclean - remove unpacked files (unpack dir.)"
  echo "  distclean - remove all generated files and directories"
  echo "  debug     - build debug version of LLVM package"
}


# ##############################################################################
# main ...

echo "Script for preparing patched LLVM for RISC-V IP core extensions."

if [ "$#" -ne 1 ]
then
  print_help
  exit 0
fi

case $1 in
  "all" )
    #echo "do all"
    download_source
    apply_patches
    do_configure
    build_source
    install_pkg
    ;;
  "get" )
    #echo "do clone LLVM"
    download_source
    ;;
  "patch" )
    #echo "do patch"
    apply_patches
    ;;
  "config" )
    #echo "do configure"
    do_configure
    ;;
  "build" )
    #echo "do build"
    build_source
    ;;
  "install" )
    #echo "do install"
    install_pkg
    ;;
  "clean" )
    #echo "do clean"
    clean_build
    ;;
  "packclean" )
    #echo "do packclean"
    clean_unpacked
    ;;
  "distclean" )
    #echo "do distclean"
    distclean_all
    ;;
  "debug" )
    #echo "do debug"
    build_debug
    ;;
  * )
    echo "E: Unsupported operation"
    print_help
    exit 1
    ;;
esac