diff --git a/.gitignore b/.gitignore index 002a280d..e4904ad3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ arpl*.vmdk *.zip .buildroot test*.sh -docker/Dockerfile -docker/cache +docker/syno-compiler/Dockerfile +cache *.bak *.o diff --git a/docker/Dockerfile.template b/docker/syno-compiler/Dockerfile.template similarity index 100% rename from docker/Dockerfile.template rename to docker/syno-compiler/Dockerfile.template diff --git a/docker/build.sh b/docker/syno-compiler/build.sh similarity index 86% rename from docker/build.sh rename to docker/syno-compiler/build.sh index e7296baa..e4c8cf78 100755 --- a/docker/build.sh +++ b/docker/syno-compiler/build.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +CACHE_DIR="cache" +PLATFORM_FILE="../../PLATFORMS" + ############################################################################### function trap_cancel() { echo "Press Control+C once more terminate the process (or wait 2s for it to restart)" @@ -26,26 +29,26 @@ function prepare() { declare -A PLATFORMS while read PLATFORM KVER; do PLATFORMS[${PLATFORM}]="${KVER}" - done <../PLATFORMS + done < ${PLATFORM_FILE} # Download toolkits - mkdir -p cache + mkdir -p ${CACHE_DIR} for PLATFORM in ${!PLATFORMS[@]}; do KVER="${PLATFORMS[${PLATFORM}]}" - echo -n "Checking cache/${TOOLKIT_VER}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz... " - if [ ! -f "cache/${TOOLKIT_VER}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" ]; then + echo -n "Checking ${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz... " + if [ ! -f "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" ]; then URL="https://global.download.synology.com/download/ToolChain/toolkit/${TOOLKIT_VER}/${PLATFORM}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" echo "Downloading ${URL}" - curl -L "${URL}" -o "cache/${TOOLKIT_VER}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" + curl -L "${URL}" -o "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" else echo "OK" fi - echo -n "Checking cache/${TOOLKIT_VER}/${PLATFORM}-toolchain.txz... " - if [ ! -f "cache/${TOOLKIT_VER}/${PLATFORM}-toolchain.txz" ]; then + echo -n "Checking ${CACHE_DIR}/${PLATFORM}-toolchain.txz... " + if [ ! -f "${CACHE_DIR}/${PLATFORM}-toolchain.txz" ]; then URL=${URLS["${PLATFORM}"]} echo "Downloading ${URL}" - curl -L "${URL}" -o "cache/${TOOLKIT_VER}/${PLATFORM}-toolchain.txz" + curl -L "${URL}" -o "${CACHE_DIR}/${PLATFORM}-toolchain.txz" else echo "OK" fi diff --git a/docker/files/etc/profile.d/login.sh b/docker/syno-compiler/files/etc/profile.d/login.sh similarity index 100% rename from docker/files/etc/profile.d/login.sh rename to docker/syno-compiler/files/etc/profile.d/login.sh diff --git a/docker/files/opt/do.sh b/docker/syno-compiler/files/opt/do.sh similarity index 100% rename from docker/files/opt/do.sh rename to docker/syno-compiler/files/opt/do.sh diff --git a/docker/syno-toolkit/Dockerfile b/docker/syno-toolkit/Dockerfile new file mode 100644 index 00000000..9f505608 --- /dev/null +++ b/docker/syno-toolkit/Dockerfile @@ -0,0 +1,15 @@ +FROM scratch +ARG PLATFORM +ARG TOOLKIT_VER +ARG CACHE_DIR + +ENV PLATFORM=${PLATFORM} TOOLKIT_VER=${TOOLKIT_VER} +ADD ${CACHE_DIR}/base_env-${TOOLKIT_VER}.txz / +ADD ${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.env.txz / +ADD ${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz / +ADD rootfs / + +WORKDIR /input +VOLUME /input /output + +ENTRYPOINT ["/usr/bin/do.sh"] diff --git a/docker/syno-toolkit/build.sh b/docker/syno-toolkit/build.sh new file mode 100755 index 00000000..e17769ca --- /dev/null +++ b/docker/syno-toolkit/build.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +set -e + +CACHE_DIR="cache" +PLATFORM_FILE="../../PLATFORMS" +TOOLKIT_VER=7.1 + +############################################################################### +function trap_cancel() { + echo "Press Control+C once more terminate the process (or wait 2s for it to restart)" + sleep 2 || exit 1 +} +trap trap_cancel SIGINT SIGTERM +cd `dirname $0` + +# Read platforms/kerver version +echo "Reading platforms" +declare -A PLATFORMS +while read PLATFORM KVER; do + PLATFORMS[${PLATFORM}]="${KVER}" +done < ${PLATFORM_FILE} + +# Download toolkits +mkdir -p ${CACHE_DIR} + +# Check base environment +echo -n "Checking ${CACHE_DIR}/base_env-${TOOLKIT_VER}.txz... " +if [ ! -f "${CACHE_DIR}/base_env-${TOOLKIT_VER}.txz" ]; then + URL="https://global.download.synology.com/download/ToolChain/toolkit/${TOOLKIT_VER}/${PLATFORM}/base_env-${TOOLKIT_VER}.txz" + echo "Downloading ${URL}" + curl -L "${URL}" -o "${CACHE_DIR}/base_env-${TOOLKIT_VER}.txz" +else + echo "OK" +fi + +# Check all platforms +for PLATFORM in ${!PLATFORMS[@]}; do + echo -n "Checking ${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz... " + if [ ! -f "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" ]; then + URL="https://global.download.synology.com/download/ToolChain/toolkit/${TOOLKIT_VER}/${PLATFORM}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" + echo "Downloading ${URL}" + curl -L "${URL}" -o "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.dev.txz" + else + echo "OK" + fi + echo -n "Checking ${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.env.txz... " + if [ ! -f "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.env.txz" ]; then + URL="https://global.download.synology.com/download/ToolChain/toolkit/${TOOLKIT_VER}/${PLATFORM}/ds.${PLATFORM}-${TOOLKIT_VER}.env.txz" + echo "Downloading ${URL}" + curl -L "${URL}" -o "${CACHE_DIR}/ds.${PLATFORM}-${TOOLKIT_VER}.env.txz" + else + echo "OK" + fi +done + +# Generate docker images +for PLATFORM in ${!PLATFORMS[@]}; do + docker buildx build . --build-arg PLATFORM=${PLATFORM} --build-arg TOOLKIT_VER=${TOOLKIT_VER} --build-arg CACHE_DIR="${CACHE_DIR}" \ + --tag fbelavenuto/syno-toolkit:${PLATFORM}-${TOOLKIT_VER} --load +done diff --git a/docker/syno-toolkit/rootfs/etc/profile.d/login.sh b/docker/syno-toolkit/rootfs/etc/profile.d/login.sh new file mode 100644 index 00000000..69629346 --- /dev/null +++ b/docker/syno-toolkit/rootfs/etc/profile.d/login.sh @@ -0,0 +1,10 @@ +[[ "$-" != *i* ]] && return +export LS_OPTIONS='--color=auto' +export SHELL='linux' +eval "`dircolors`" +alias ls='ls -F -h --color=always -v --author --time-style=long-iso' +alias ll='ls -l' +alias l='ls -l -a' +alias h='history 25' +alias j='jobs -l' +export PATH="/opt/${PLATFORM}/bin:${PATH}" \ No newline at end of file diff --git a/docker/syno-toolkit/rootfs/usr/bin/do.sh b/docker/syno-toolkit/rootfs/usr/bin/do.sh new file mode 100755 index 00000000..0c21d6e6 --- /dev/null +++ b/docker/syno-toolkit/rootfs/usr/bin/do.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +set -e + +############################################################################### +function compile-module { + echo -e "Compiling module for \033[7m${PLATFORM}\033[0m..." + cp -R /input /tmp + make -C ${KSRC} M=/tmp/input ${PLATFORM^^}-Y=y ${PLATFORM^^}-M=m modules + while read F; do + strip -g "${F}" + echo "Copying `basename ${F}`" + cp "${F}" "/output" + chown 1000.1000 "/output/`basename ${F}`" + done < <(find /tmp/input -name \*.ko) +} + +############################################################################### +function compile-lkm { + cp -R /input /tmp + make -C "/tmp/input" dev-v7 + strip -g "/tmp/input/redpill.ko" + mv "/tmp/input/redpill.ko" "/output/redpill-dev.ko" + chown 1000.1000 /output/redpill-dev.ko + make -C "/tmp/input" clean + make -C "/tmp/input" prod-v7 + strip -g "/tmp/input/redpill.ko" + mv "/tmp/input/redpill.ko" "/output/redpill-prod.ko" + chown 1000.1000 /output/redpill-prod.ko +} + +############################################################################### +# function compile-drivers { +# while read platform kver; do +# SRC_PATH="/opt/${platform}" +# echo "Compiling for ${platform}-${kver}" +# cd /opt/linux-${kver}/drivers +# while read dir; do +# if [ -f "${dir}/Makefile" ]; then +# echo "Driver `basename ${dir}`" +# grep "CONFIG_.*/.*" "${dir}/Makefile" | sed 's/.*\(CONFIG_[^)]*\).*/\1=n/g' > /tmp/env +# grep "CONFIG_.*\.o.*" "${dir}/Makefile" | sed 's/.*\(CONFIG_[^)]*\).*/\1=m/g' >> /tmp/env +# make -C "${SRC_PATH}" M=$(readlink -f "${dir}") clean +# cat /tmp/env | xargs -d '\n' make -C "${SRC_PATH}" M=$(readlink -f "${dir}") modules $@ +# fi +# done < <(find -type d) +# DST_PATH="/output/compiled-mods/${platform}-${kver}" +# mkdir -p "${DST_PATH}" +# while read f; do +# strip -g "${f}" +# mv "${f}" "${DST_PATH}" +# done < <(find -name \*.ko) +# done ()" + echo "Commands: shell | compile-module | compile-lkm" + exit 1 +fi +export KSRC="/usr/local/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu/sys-root/usr/lib/modules/DSM-${TOOLKIT_VER}/build" +export LINUX_SRC="/usr/local/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu/sys-root/usr/lib/modules/DSM-${TOOLKIT_VER}/build" +case $1 in + shell) shift && bash -l $@ ;; + compile-module) compile-module ;; + compile-lkm) compile-lkm ;; + # compile-drivers) compile-drivers ;; + *) echo "Command not recognized: $1" ;; +esac