Skip to content

一些开发用半自动化脚本

南国微雪 edited this page Jun 17, 2023 · 40 revisions

TL;DR

export USERNAME=你的gh用户名
cd ~
mkdir src pkg .bin
touch .bin/prepare.sh
touch .bin/gib.sh  # 这里 gib 取的是加拿大口音的 give
chmod +x ~/.bin/*.sh

sudo pacman -Sy --noconfirm pkgctl devtools-riscv64  # -Syu & reboot first, if necessary
git clone git@github.com:$USERNAME/archriscv-packages.git
cd archriscv-packages
git remote add upstream https://github.com/felixonmars/archriscv-packages.git

cat >> ~/.bashrc << EOF
alias rv64build="extra-riscv64-build -- -d ~/pkg:/var/cache/pacman/pkg"
alias nohuprv64build="nohup extra-riscv64-build -- -d ~/pkg:/var/cache/pacman/pkg &"
alias prep=". ~/.bin/prepare.sh"
alias gib=". ~/.bin/gib.sh"
EOF

cat >> ~/.bin/gib.sh << EOF
#!/usr/bin/bash -e

[ \$# -eq 0 ] && echo "You need to provide a package name." && exit 1

cd ~/archriscv-packages

echo "Pulling from upstream (Fast-Forward Only)..."

git checkout master
git pull --ff-only upstream master:master
# make sure origin is up-to-date
git push

cd ~/src

pkgctl repo clone "\$1" || true

cd "\$1" || exit 1

if [ -e ~/archriscv-packages/"\$1"/riscv64.patch ]
then
  cp ~/archriscv-packages/"\$1"/riscv64.patch .
  pushd ~/archriscv-packages/"\$1"/
  find ~+ -type f -name "*.patch" | sed -e "s|\$HOME|~|g" | awk -F " " '{print "  cp " \$1 " ."}'
  popd
else
  echo "No existing riscv64 patch file found."
fi

# 将第一个 x86_64(`arch=(x86_64)`)换成 riscv64 并保留格式,如果 arch 是 any 则不改
sed "0,/x86_64/s//riscv64/" -i ./PKGBUILD

. /usr/share/makepkg/util.sh
. ./PKGBUILD
for key in "\${validpgpkeys[@]}"; do
    echo "Receiving key \${key}..."
    # try both servers as some keys exist one place and others another
    # we also want to always try to receive keys to pick up any update
    gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys "\$key" || true
    gpg --keyserver hkps://keys.openpgp.org --recv-keys "\$key" || true
done

colorize

is_array source && \\
    printf "\${RED}source = \${ALL_OFF}%s\n" "\${source[@]}" # 方便检查源码是 .tar.*z 还是 git+,可以删掉
is_array _commit && \\
    printf "\${RED}_commit = \${ALL_OFF}%s\n" "\${_commit[@]}"
EOF

cat >> ~/.bin/prepare.sh << EOF
#!/usr/bin/bash

if [ \$# -eq 0 ]
  then
    echo "You need to provide a PKGBUILD file, or a directory that contains a PKGBUILD file."
  else

cd "\$(dirname "\$1")"

PREPARE_SRC_DIR=\$(pwd)

if [ \$? -eq 0 ]
 then

sed "0,/riscv64/s//x86_64/" -i ./PKGBUILD  # 把 arch 换回去

git diff --no-prefix --relative ./PKGBUILD | tail -n +3 > ./riscv64.patch  # 去掉 git diff 生成的 header,确保 patch 干净
cat ./riscv64.patch  # 给你看看 diff 长啥样

fname=\$(basename "\$PWD")  # 拿到包名

mkdir ~/prepare_tmp_dir
cp ./riscv64.patch ~/prepare_tmp_dir/
cd ~/archriscv-packages

echo "Pulling from upstream (Fast-Forward Only)..."

git checkout master
git pull --ff-only upstream master:master
# make sure origin is up-to-date
git push
git checkout -b "\$fname" || git checkout "\$fname"
mkdir "\$fname"
cd "\$fname"

TARGET_DIR=\$(pwd)

mv ~/prepare_tmp_dir/riscv64.patch .
rmdir ~/prepare_tmp_dir

echo "Done. Now you can do:"
echo ""
echo "# first, copy necessary patches to this dir:"

cd \$PREPARE_SRC_DIR
# Yes, I'm a shell magician
find ~+ -type f -name "*.patch" | sed -e "s|\$HOME|~|g" | awk -F " " '{print "  cp " \$1 " ."}'
cd \$TARGET_DIR

echo ""
echo "# then:"
echo "  git add ."
echo "  git commit -m \"addpkg: \$fname\""
echo "  git push -u origin \$fname"
echo ""

  fi  # if [ \$? -eq 0 ] ends here
fi  # if [ \$# -eq 0 ] ends here
EOF

cd ~
source .bashrc

动机

  • 准备阶段,把 cd ~/s<TAB> pkgctl repo clone name cd name/r<TAB>/<TAB> vim PKGBUILD 缩成一个命令
  • 修包阶段,把 updpkgsums extra-riscv64-build 参数我忘了 缩成一个命令
  • 提交阶段,把 git diff > riscv64.patch vim riscv64.patch cd ~/ar<TAB> git checkout -b name mkdir name cp ~/so<TAB>/name/r<TAB>/<TAB>/ri<TAB> name cd name ga gm gp -u origin name 缩成一个命令

在开始之前

可以根据自己的环境和需求来修改:

  1. gib.sh 中的源码路径 ~/src
  2. build 用的临时文件夹 ~/pkg
  3. 你自己的 archriscv-packages 的 fork 所在的路径 ~/archriscv-packages
    sudo pacman -Sy --noconfirm pkgctl devtools-riscv64  # -Syu & reboot first, if necessary
    cd ~
    mkdir src pkg
    git clone git@github.com:$USERNAME/archriscv-packages.git
    cd archriscv-packages
    git remote add upstream https://github.com/felixonmars/archriscv-packages.git
  4. 其它

准备工作

mkdir ~/.bin
touch ~/.bin/prepare.sh
touch ~/.bin/gib.sh  # 这里 gib 取的是加拿大口音的 give
chmod +x ~/.bin/*.sh

同时请确保 ~/prepare_tmp_dir 不存在,并且 ~/.bin 不在 你的 $PATH 环境变量中。

用法

  1. (在任意目录,拉取源码)gib rocksdb
  2. (在 repos/* 目录,生成 diff 并 push 到新的 branch)push .(但实际上 commit 和 push 操作需要你手动确认)
  3. (在 repos/* 目录,尝试 build)rv64build

部署

  • ~/.bashrc
# append
alias rv64build="extra-riscv64-build -- -d ~/pkg:/var/cache/pacman/pkg"  # 这里 ~/pkg 按环境配置可以替换成任意空文件夹
alias nohuprv64build="nohup extra-riscv64-build -- -d ~/pkg:/var/cache/pacman/pkg &"
alias prep=". ~/.bin/prepare.sh"  # alias 可以随便改
alias gib=". ~/.bin/gib.sh"
  • ~/.bin/gib.sh
#!/usr/bin/bash -e

[ $# -eq 0 ] && echo "You need to provide a package name." && exit 1

cd ~/archriscv-packages

echo "Pulling from upstream (Fast-Forward Only)..."

git checkout master
git pull --ff-only upstream master:master
# make sure origin is up-to-date
git push

cd ~/src

pkgctl repo clone "$1" || true

cd "$1" || exit 1

if [ -e ~/archriscv-packages/"$1"/riscv64.patch ]
then
  cp ~/archriscv-packages/"$1"/riscv64.patch .
  pushd ~/archriscv-packages/"$1"/
  find ~+ -type f -name "*.patch" | sed -e "s|$HOME|~|g" | awk -F " " '{print "  cp " $1 " ."}'
  popd
else
  echo "No existing riscv64 patch file found."
fi

# 将第一个 x86_64(`arch=(x86_64)`)换成 riscv64 并保留格式,如果 arch 是 any 则不改
sed "0,/x86_64/s//riscv64/" -i ./PKGBUILD

. /usr/share/makepkg/util.sh
. ./PKGBUILD
for key in "${validpgpkeys[@]}"; do
    echo "Receiving key ${key}..."
    # try both servers as some keys exist one place and others another
    # we also want to always try to receive keys to pick up any update
    gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys "$key" || true
    gpg --keyserver hkps://keys.openpgp.org --recv-keys "$key" || true
done

colorize

is_array source && \
    printf "${RED}source = ${ALL_OFF}%s\n" "${source[@]}" # 方便检查源码是 .tar.*z 还是 git+,可以删掉
is_array _commit && \
    printf "${RED}_commit = ${ALL_OFF}%s\n" "${_commit[@]}"
  • ~/.bin/prepare.sh
#!/usr/bin/bash

if [ $# -eq 0 ]
  then
    echo "You need to provide a PKGBUILD file, or a directory that contains a PKGBUILD file."
  else

cd "$(dirname "$1")"

PREPARE_SRC_DIR=$(pwd)

if [ $? -eq 0 ]
 then

sed "0,/riscv64/s//x86_64/" -i ./PKGBUILD  # 把 arch 换回去

git diff --no-prefix --relative ./PKGBUILD | tail -n +3 > ./riscv64.patch  # 去掉 git diff 生成的 header,确保 patch 干净
cat ./riscv64.patch  # 给你看看 diff 长啥样

fname=$(basename "$PWD")  # 拿到包名

mkdir ~/prepare_tmp_dir
cp ./riscv64.patch ~/prepare_tmp_dir/
cd ~/archriscv-packages

echo "Pulling from upstream (Fast-Forward Only)..."

git checkout master
git pull --ff-only upstream master:master
# make sure origin is up-to-date
git push
git checkout -b "$fname" || git checkout "$fname"
mkdir "$fname"
cd "$fname"

TARGET_DIR=$(pwd)

mv ~/prepare_tmp_dir/riscv64.patch .
rmdir ~/prepare_tmp_dir

echo "Done. Now you can do:"
echo ""
echo "# first, copy necessary patches to this dir:"

cd $PREPARE_SRC_DIR
# Yes, I'm a shell magician
find ~+ -type f -name "*.patch" | sed -e "s|$HOME|~|g" | awk -F " " '{print "  cp " $1 " ."}'
cd $TARGET_DIR

echo ""
echo "# then:"
echo "  git add ."
echo "  git commit -m \"addpkg: $fname\""
echo "  git push -u origin $fname"
echo ""

  fi  # if [ $? -eq 0 ] ends here
fi  # if [ $# -eq 0 ] ends here
Clone this wiki locally