build.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. set -e
  3. if [ -z "$ROOT_DIR" ]; then
  4. pushd $(dirname $(readlink -f "$BASH_SOURCE")) > /dev/null
  5. ROOT_DIR=$(cd .. && pwd)
  6. popd > /dev/null
  7. fi
  8. SRC_BIN=$ROOT_DIR/_output/bin
  9. SRC_BUILD=$ROOT_DIR/build
  10. OUTPUT_DIR=$ROOT_DIR/_output/rpms
  11. PKG=$1
  12. BIN_PATH=${2:-/opt/yunion/bin}
  13. if [ -z "$PKG" ]; then
  14. echo "Usage: $0 <package>"
  15. exit 1
  16. fi
  17. BIN="$SRC_BIN/$PKG"
  18. ROOT="$SRC_BUILD/$PKG"
  19. if [ ! -x "$BIN" ]; then
  20. echo "$BIN not exists"
  21. exit 1
  22. fi
  23. if [ ! -x "$ROOT" ]; then
  24. echo "$ROOT not exists"
  25. exit 1
  26. fi
  27. . $ROOT/vars
  28. BUILDROOT=$(mktemp -d 2>/dev/null || mktemp -d -t 'yunion')
  29. function cleanup {
  30. rm -rf "$BUILDROOT"
  31. echo "Deleted temp working directory $BUILDROOT"
  32. }
  33. # register the cleanup function to be called on the EXIT signal
  34. trap cleanup EXIT
  35. echo "Build root ${BUILDROOT}"
  36. # BRANCH=$(git rev-parse --abbrev-ref HEAD)
  37. # TAG=$(git describe --exact-match --tags)
  38. if [ -z "$VERSION" ]; then
  39. TAG=$(git describe --abbrev=0 --tags || echo testing)
  40. VERSION=${TAG/\//-}
  41. if [[ "$VERSION" =~ ^v0. ]]; then
  42. VERSION=${VERSION/v0./}
  43. elif [[ "$VERSION" =~ ^v ]]; then
  44. VERSION=${VERSION/v/}
  45. fi
  46. VERSION=${VERSION//-/.}
  47. fi
  48. RELEASE=`date +"%y%m%d%H"`
  49. SPEC_DIR=$BUILDROOT/SPECS
  50. SPEC_FILE=$SPEC_DIR/${PKG}.spec
  51. RPM_DIR=$BUILDROOT/RPMS
  52. if [ -z "$OWNER" ]; then
  53. OWNER=yunion
  54. fi
  55. if [ -z "$SERVICE" ]; then
  56. SERVICE="0"
  57. fi
  58. mkdir -p $SPEC_DIR
  59. echo "# Yunion RPM spec
  60. %global owner $OWNER
  61. %global pkgname yunion-$PKG
  62. %global homedir /var/run/%{owner}
  63. %global use_systemd $SERVICE
  64. Name: %{pkgname}
  65. Version: $VERSION
  66. Release: $RELEASE
  67. Summary: %{pkgname}
  68. Group: Unspecified
  69. License: GPL
  70. URL: https://www.yunion.io/doc
  71. $(for req in "${REQUIRES[@]}"; do echo Requires: "$req"; done)
  72. %if %{use_systemd}
  73. Requires: systemd
  74. BuildRequires: systemd
  75. %endif
  76. %description
  77. $DESCRIPTION
  78. %prep
  79. %build
  80. %install
  81. install -D -m 0755 $BIN \$RPM_BUILD_ROOT$BIN_PATH/$PKG
  82. for bin in $EXTRA_BINS; do
  83. install -D -m 0755 $SRC_BIN/\$bin \$RPM_BUILD_ROOT$BIN_PATH/\$bin
  84. done
  85. if [ -d $ROOT/root ]; then
  86. rsync -a $ROOT/root/ \$RPM_BUILD_ROOT
  87. fi
  88. %pre" > $SPEC_FILE
  89. if [ -f $ROOT/preinst ]; then
  90. cat $ROOT/preinst >> $SPEC_FILE
  91. else
  92. echo "
  93. %if %{use_systemd}
  94. getent group %{owner} >/dev/null || /usr/sbin/groupadd -r %{owner}
  95. getent passwd %{owner} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d %{homedir} -M -g %{owner} %{owner}
  96. %endif
  97. " >> $SPEC_FILE
  98. fi
  99. echo "
  100. %post" >> $SPEC_FILE
  101. if [ -f $ROOT/postinst ]; then
  102. cat $ROOT/postinst >> $SPEC_FILE
  103. else
  104. echo "
  105. %if %{use_systemd}
  106. /usr/bin/systemctl preset %{pkgname}.service >/dev/null 2>&1 ||:
  107. %endif
  108. " >> $SPEC_FILE
  109. fi
  110. echo "
  111. %preun" >> $SPEC_FILE
  112. if [ -f $ROOT/prerm ]; then
  113. cat $ROOT/prerm >> $SPEC_FILE
  114. else
  115. echo "
  116. %if %{use_systemd}
  117. /usr/bin/systemctl --no-reload disable %{pkgname}.service >/dev/null 2>&1 || :
  118. /usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
  119. %endif
  120. " >> $SPEC_FILE
  121. fi
  122. echo "
  123. %postun" >> $SPEC_FILE
  124. if [ -f $ROOT/postrm ]; then
  125. cat $ROOT/postrm >> $SPEC_FILE
  126. else
  127. echo "
  128. %if %{use_systemd}
  129. /usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
  130. %endif
  131. " >> $SPEC_FILE
  132. fi
  133. echo -n "
  134. %files
  135. %doc
  136. $BIN_PATH/$PKG
  137. " >> $SPEC_FILE
  138. for b in $EXTRA_BINS; do
  139. echo $BIN_PATH/$b >> $SPEC_FILE
  140. done
  141. if [ -d $ROOT/root/ ]; then
  142. find $ROOT/root/ -type f | sed -e "s:$ROOT/root::g" >> $SPEC_FILE
  143. fi
  144. TARGET=
  145. case "$GOARCH" in
  146. "arm64" | "aarch64" | "arm")
  147. TARGET="--target aarch64-redhat-linux"
  148. ;;
  149. "amd64" | "x86" | "i686" | "i386" | "x86_64")
  150. TARGET="--target x86_64-redhat-linux"
  151. ;;
  152. "riscv64")
  153. TARGET="--target riscv64-linux"
  154. ;;
  155. esac
  156. rpmbuild --define "_topdir $BUILDROOT" -bb $SPEC_FILE $TARGET
  157. find $RPM_DIR -type f | while read f; do
  158. d="$(dirname "$f")"
  159. d="$OUTPUT_DIR/${d#$RPM_DIR}"
  160. mkdir -p "$d"
  161. cp $f $d
  162. done