URI:
       tcross-pcc - scripts - various script and utils
  HTML git clone git://z3bra.org/scripts
   DIR Log
   DIR Files
   DIR Refs
       ---
       tcross-pcc (5364B)
       ---
            1 #!/bin/sh -ex
            2 #
            3 # Couple of useful links:
            4 # + http://pcc.ludd.ltu.se/cross-compiler/
            5 # + http://wiki.osdev.org/Cross-Compiler_Successful_Builds
            6 # + https://git.framasoft.org/Ypnose/solyste/blob/master/scripts/create-crossenv
            7 # + http://kegel.com/crosstool/
            8 # + https://github.com/GregorR/musl-cross/tree/master/patches
            9 #
           10 # 0. download and extract sources
           11 # 1. patch everything that require patching
           12 # 2. install linux headers
           13 # 3. build binutils
           14 # 4. build musl
           15 # 5. build pcc with gcc
           16 # 6. add pkg-config wrapper
           17 
           18 # cross compiler environment
           19 MARCH=$(uname -m)
           20 TRIPLE=${TRIPLE:-${MARCH}-linux-musl}
           21 PREFIX=${PREFIX:-${HOME}/cross/pcc-${MARCH}}
           22 BLDDIR=${BLDDIR:-${HOME}/cross/build}
           23 SRCDIR=${SRCDIR:-${HOME}/cross/source}
           24 PATCHD=${PATCHD:-${HOME}/cross/patches}
           25 
           26 # compilation variables
           27 PATH="${PREFIX}/bin:${PATH}"
           28 CFLAGS="-Os -fomit-frame-pointer -pipe"
           29 CXXFLAGS="${CFLAGS}"
           30 CPPFLAGS="${CFLAGS}"
           31 LDFLAGS="-Wl,--as-needed"
           32 MAKEFLAGS="-j8"
           33 
           34 # versions
           35 PCCV=${PCCV:-1.1.0}
           36 BINV=${BINV:-2.25}
           37 MUSLV=${MUSLV:-1.1.10}
           38 KERNV=${KERNV:-4.1.4}
           39 
           40 
           41 # source mirrors
           42 PCCMIRROR=ftp://pcc.ludd.ltu.se/pub/pcc-releases
           43 GNUMIRROR=ftp://ftp.gnu.org/gnu
           44 MUSLMIRROR=http://www.musl-libc.org/releases
           45 LINUXMIRROR=https://www.kernel.org/pub/linux/kernel/v4.x
           46 
           47 # Preparing sources
           48 mkdir -p "${SRCDIR}" "${BLDDIR}" "${PREFIX}"
           49 cd "${SRCDIR}"
           50 
           51 # 
           52 # ┏━┓ 
           53 # ┃┃┃ 
           54 # ┗━┛╹
           55 # Get all GNU tarballs
           56 grab_sources() {
           57 curl -# "${PCCMIRROR}/pcc-${PCCV}.tgz"                          | tar xz
           58 curl -# "${PCCMIRROR}/pcc-libs-${PCCV}.tgz"                     | tar xz
           59 curl -# "${GNUMIRROR}/binutils/binutils-${BINV}.tar.gz"         | tar xz
           60 curl -# "${MUSLMIRROR}/musl-${MUSLV}.tar.gz"                    | tar xz
           61 curl -# "${LINUXMIRROR}/linux-${KERNV}.tar.xz"                  | tar xJ
           62 }
           63 
           64 #
           65 # ╺┓  
           66 #  ┃  
           67 # ╺┻╸╹
           68 # Patch all source trees.
           69 # This will take all patches in $PATCHD matching the programs
           70 patch_sources() {
           71 for DIR in $(find "${SRCDIR}" -maxdepth 1 -type d); do
           72         cd "${DIR}"
           73         cat "${PATCHD}/$(basename ${DIR})"-*.diff | patch -Np1
           74 done
           75 }
           76 
           77 #
           78 # ┏━┓ 
           79 # ┏━┛ 
           80 # ┗━╸╹
           81 # Get linux headers
           82 install_headers() {
           83 cd "${SRCDIR}/linux-${KERNV}"
           84 make ARCH=$(uname -m) headers_check
           85 make ARCH=$(uname -m) INSTALL_HDR_PATH=${PREFIX}/${TRIPLE} headers_install
           86 rm -r "${SRCDIR}/linux-${KERNV}"
           87 }
           88 
           89 #
           90 # ┏━┓ 
           91 # ╺━┫ 
           92 # ┗━┛╹
           93 # Build binutils and install them to ${PREFIX}
           94 install_binutils() {
           95 mkdir -p "${BLDDIR}/binutils"
           96 cd "${BLDDIR}/binutils"
           97 ${SRCDIR}/binutils-${BINV}/configure --target=${TRIPLE} \
           98                                  --prefix=${PREFIX} \
           99                                  --with-sysroot=${PREFIX}/${TRIPLE} \
          100                                  --disable-nls \
          101                                  --disable-shared \
          102                                  --disable-multilib
          103 make configure-host
          104 make LDFLAGS="${LDFLAGS} -all-static -static"
          105 make install
          106 rm -rf "${BLDDIR}/binutils"
          107 rm -rf "${SRCDIR}/binutils-${BINV}"
          108 }
          109 
          110 #
          111 # ╻ ╻ 
          112 # ┗━┫ 
          113 #   ╹╹
          114 # Build the musl libc
          115 install_musl() {
          116 cd "${SRCDIR}/musl-${MUSLV}"
          117 ./configure --prefix=${PREFIX}/${TRIPLE} \
          118             --target=${TRIPLE} \
          119             --disable-gcc-wrapper \
          120             --disable-debug \
          121             --disable-shared \
          122             --disable-warning
          123 make LDFLAGS="${LDFLAGS}"
          124 make install
          125 rm -rf "${SRCDIR}/musl-${MUSLV}"
          126 }
          127 
          128 #
          129 # ┏━╸ 
          130 # ┗━┓ 
          131 # ┗━┛╹
          132 # Build pcc linked against musl
          133 install_pcc() {
          134 mkdir -p "${BLDDIR}/pcc"
          135 mkdir -p "${BLDDIR}/pcc-libs"
          136 cd "${BLDDIR}/pcc"
          137 ${SRCDIR}/pcc-${PCCV}/configure --target=${TRIPLE} \
          138                                 --with-libdir=${PREFIX}/${TRIPLE}/lib \
          139                                 --with-incdir=${PREFIX}/${TRIPLE}/include \
          140                                 --prefix=${PREFIX}
          141 
          142 make LDFLAGS="${LDFLAGS} -static" \
          143      PCCLIBDIR=${PREFIX}/${TRIPLE}/pcc/lib \
          144      PCCINCDIR=${PREFIX}/${TRIPLE}/pcc/include
          145 make PCCLIBDIR=${PREFIX}/${TRIPLE}/pcc/lib \
          146      PCCINCDIR=${PREFIX}/${TRIPLE}/pcc/include \
          147      install
          148 rm -rf "${BLDDIR}/pcc"
          149 rm -rf "${SRCDIR}/pcc-${PCCV}"
          150 }
          151 
          152 #
          153 # ┏━┓ 
          154 # ┣━┓ 
          155 # ┗━┛╹
          156 # Build pcc libraries against musl
          157 install_pcc_libs() {
          158 cd "${BLDDIR}/pcc-libs"
          159 ${SRCDIR}/pcc-libs-${PCCV}/configure --target=${TRIPLE} \
          160                                      --prefix=${PREFIX}
          161 
          162 make PCCLIBDIR=${PREFIX}/${TRIPLE}/pcc/lib \
          163      PCCINCDIR=${PREFIX}/${TRIPLE}/pcc/include
          164 make PCCLIBDIR=${PREFIX}/${TRIPLE}/pcc/lib \
          165      PCCINCDIR=${PREFIX}/${TRIPLE}/pcc/include \
          166      install
          167 rm -rf "${BLDDIR}/pcc-libs"
          168 rm -rf "${SRCDIR}/pcc-libs-${PCCV}"
          169 }
          170 
          171 #
          172 # ┏━┓ 
          173 #   ┃ 
          174 #   ╹╹
          175 # Add pkg-config wrapper
          176 install_pkgconfig() {
          177 cat << EOF > "${PREFIX}/bin/${TRIPLE}-pkg-config"
          178 #!/bin/sh
          179 export PKG_CONFIG_SYSROOT_DIR=${PREFIX}/${TRIPLE}
          180 export PKG_CONFIG_LIBDIR=${PREFIX}/${TRIPLE}/usr/lib/pkgconfig
          181 export PKG_CONFIG_PATH=\$PKG_CONFIG_LIBDIR
          182 
          183 exec pkg-config --static "\$@"
          184 EOF
          185 chmod 755 "${PREFIX}/bin/${TRIPLE}-pkg-config"
          186 }
          187 
          188 grab_sources
          189 patch_sources
          190 install_headers
          191 install_binutils
          192 install_musl
          193 install_pcc
          194 install_pcc_libs
          195 install_pkgconfig
          196 
          197 # clean environment
          198 rm -rf "${SRCDIR}"
          199 rm -rf "${BLDDIR}"
          200 rm -rf "${PREFIX}/share"
          201 rm -f  "${PREFIX}/lib/libiberty.a"
          202 
          203 cat << EOF | tee ${PREFIX}/README
          204 TRIPLET :       $TRIPLE
          205 PREFIX  :       $PREFIX
          206 PCC     :       $PCCV
          207 BINUTILS:       $BINV
          208 MUSL    :       $MUSLV
          209 KERNEL  :       $KERNV
          210 EOF