cross compile toolchain on ubuntu 2204

bash 复制代码
#!/bin/bash
##-------------------------------------------------------------
## Configuration variables
##-------------------------------------------------------------
root_dir="/home/prometheus/module-test/cross-compile"
target="powerpc-eabi"

clean_install=true

build_binutils=true
build_gcc_static=true
build_newlib=true
build_gcc=true

# gcc dependency
isl_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.15.tar.bz2"
mpfr_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2"
mpc_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz"
gmp_tar_link="ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2"

# use gcc492, confirmed ok
binutils_tar_link="http://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz"
#binutils_tar_link="https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.gz"
#gcc_tar_link="http://mirrors.kernel.org/gnu/gcc/gcc-6.1.0/gcc-4.9.2.tar.gz"
gcc_tar_link="https://mirrors.kernel.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.gz"
newlib_tar_link="ftp://sourceware.org/pub/newlib/newlib-2.4.0.tar.gz"

##-------------------------------------------------------------
## Function definitions
##-------------------------------------------------------------
function announce {
  now=$(date +"%T")
  echo
  echo "[$now] $1"
  echo
}

pushd () {
  command pushd "$@" > /dev/null
}

popd () {
  command popd "$@" > /dev/null
}

# download_and_extract (URL, tarball_name, target_dir)
download_and_extract  () {
  local tarball="${1##*/}"

  # Eat up strings after 2 dots i.e.   .tar.gz
  local tarball_dir="${tarball%.*}"
  local tarball_dir="${tarball_dir%.*}"

  # Clean up older downloads
  rm -rf $tarball_dir
  rm -rf $2

  # download
  wget -nc $1

  # extract
  tar xf $tarball
  mv $tarball_dir $2
}

##-------------------------------------------------------------
## Script main
##-------------------------------------------------------------

install_dir="${root_dir}/install"
PATH=${PATH}:$install_dir/bin
PATH=${PATH}:$install_dir

announce "Moving to root=$root_dir"
cd $root_dir

mkdir -p $install_dir
if [ "$clean_install" = "true" ]; then
  pushd $install_dir
    rm -rf ./*
  popd
fi


#libs
#-------------------
announce "Working on libs..."
libs_dir="$root_dir/libs"


mkdir -p $libs_dir
pushd $libs_dir

  # download source
  download_and_extract $gmp_tar_link gmp
  download_and_extract $mpfr_tar_link mpfr
  download_and_extract $mpc_tar_link mpc
  download_and_extract $isl_tar_link isl

popd  # back to root dir
announce "Working on libs... Complete!"

if [ "$build_binutils" = "true" ]; then
  #binutils
  #-------------------
  announce "Working on binutils..."
  binutils_dir="$root_dir/binutils"
  binutils_src_dir="$binutils_dir/binutils-src"
  binutils_build_dir="$binutils_dir/build-$target"

  mkdir -p $binutils_dir
  pushd $binutils_dir

    # download source
    #git clone git://sourceware.org/git/binutils-gdb.git
    download_and_extract $binutils_tar_link binutils-src

    # link any required libraries
    ln -s $libs_dir/isl $binutils_src_dir/isl
    ln -s $libs_dir/gmp $binutils_src_dir/gmp

    mkdir -p $binutils_build_dir
    pushd $binutils_build_dir

      # perform cleanup
      pushd $binutils_src_dir
#        make distclean > /dev/null
      popd
#      rm -rf ./*

      # build and install
      $binutils_src_dir/configure --prefix=$install_dir --target=$target > log-configure-$target.log
      make -j2 > log-make-$target.log
      make install > log-install-$target.log
    popd

  popd  # back to root dir
  tree $install_dir > $install_dir/post-binutils-install.tree
  announce "Working on binutils... Complete!"
fi


if [ "$build_gcc_static" = "true" ]; then
  #gcc-static
  #-------------------
  announce "Working on gcc-static..."
  gcc_dir="$root_dir/gcc"
  gcc_src_dir="$gcc_dir/gcc-src"
  gcc_build_dir="$gcc_dir/build-$target"

  mkdir -p $gcc_dir
  pushd $gcc_dir

    # download source
    download_and_extract $gcc_tar_link gcc-src

    # link any required libraries
    ln -s $libs_dir/isl $gcc_src_dir/isl
    ln -s $libs_dir/gmp $gcc_src_dir/gmp
    ln -s $libs_dir/mpfr $gcc_src_dir/mpfr
    ln -s $libs_dir/mpc $gcc_src_dir/mpc

    mkdir -p $gcc_build_dir
    pushd $gcc_build_dir

      # perform cleanup
  #    rm -rf ./*

      # build and install
      announce "gcc-static configure"
CXXFLAGS="-fpermissive"      $gcc_src_dir/configure --prefix=$install_dir  --target=$target --enable-languages=c --disable-bootstrap --with-newlib > log-configure-$target.log
      announce "gcc-static make"
      make -j2 all-gcc > log-make-$target.log
      announce "gcc-static install"
      make install-gcc > log-install-$target.log

      tree $install_dir > $install_dir/post-gcc-static-install.tree

      announce "gcc-static-libgcc make"
      make -j2 -k all-target-libgcc
      announce "gcc-static-libgcc install"
      make -i install-target-libgcc
    popd

  popd  # back to root dir
  tree $install_dir > $install_dir/post-gcc-static-libgcc-install.tree
  announce "Working on gcc-static... Complete!"
fi

if [ "$build_newlib" = "true" ]; then
  #newlib
  #-------------------
  announce "Working on newlib..."
  newlib_dir="$root_dir/newlib"
  newlib_src_dir="$newlib_dir/newlib-src"
  newlib_build_dir="$newlib_dir/build-$target"

  mkdir -p $newlib_dir
  pushd $newlib_dir

    # download source
    download_and_extract $newlib_tar_link newlib-src

    mkdir -p $newlib_build_dir
    pushd $newlib_build_dir

      # perform cleanup
      #rm -rf ./*

      # build and install
      announce "newlib configure"
      $newlib_src_dir/configure --prefix=$install_dir --target=$target > log-configure-$target.log
      announce "newlib make"
      make -j2 all > log-make-$target.log
      announce "newlib install"
      make install > log-install-$target.log
    popd

  popd  # back to root dir
  tree $install_dir > $install_dir/post-newlib-install.tree
  announce "Working on newlib... Complete!"
fi

if [ "$build_gcc" = "true" ]; then
  #gcc
  #-------------------
  announce "Working on gcc..."
  gcc_dir="$root_dir/gcc"
  gcc_src_dir="$gcc_dir/gcc-src"
  gcc_build_dir="$gcc_dir/build-$target"

  pushd $gcc_build_dir

    # build and install
    announce "gcc configure"
 CXXFLAGS="-fpermissive"    $gcc_src_dir/configure --prefix=$install_dir --target=$target --enable-languages=c,c++ --disable-bootstrap --with-newlib > log-configure-$target.log
    announce "gcc make"
    make -j2 all > log-make-$target.log
    announce "gcc install"
    make install > log-install-$target.log

  popd  # back to root dir
  tree $install_dir > $install_dir/post-gcc-install.tree
  announce "Working on gcc... Complete!"
fi


announce "Cross toolchain ready!"
相关推荐
Hacker_Nightrain26 分钟前
linux 网络安全不完全笔记
linux·笔记·web安全
一入程序无退路30 分钟前
c语言传参数路径太长,导致无法获取参数
linux·c语言·数据库
打鱼又晒网40 分钟前
Linux网络 | 网络计算器客户端实现与Json的安装以及使用
linux·c++·网络协议·计算机网络
朝九晚五ฺ43 分钟前
【Linux探索学习】第二十三弹——理解文件系统:认识硬件、探索文件在硬件上的存储问题
linux·运维·学习
陌夏微秋1 小时前
STM32单片机芯片与内部47 STM32 CAN内部架构 介绍
数据库·stm32·单片机·嵌入式硬件·架构·信息与通信
-指短琴长-1 小时前
Linux从0到1——线程同步和互斥【互斥量/条件变量/信号量/PC模型】
linux·运维·服务器
梦呓01041 小时前
platform_msi使用
linux
A charmer1 小时前
畅游 Linux 开发天地:yum 与 vim 详解
linux·运维·服务器
梦想画家2 小时前
Ubuntu安装Apache Airflow详细指南
ubuntu·数据集成·airflow
小吴-斌2 小时前
Ubuntu 中安装 RabbitMQ 教程
linux·ubuntu·rabbitmq