makerelease.lib 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. #
  2. # Release script library
  3. #
  4. # Copyright (c) 2012-2022 Michael Buesch <m@bues.ch>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. default_hook_pre_checkout()
  21. {
  22. true
  23. }
  24. default_hook_post_checkout()
  25. {
  26. cd "$1"
  27. find "$1" \( \
  28. \( -name 'makerelease*' \) -o \
  29. \( -name '.git*' \) \
  30. \) -print0 | xargs -0 rm -r
  31. }
  32. default_hook_pre_relocate_checkout()
  33. {
  34. true
  35. }
  36. default_hook_post_relocate_checkout()
  37. {
  38. true
  39. }
  40. default_hook_pre_documentation()
  41. {
  42. true
  43. }
  44. default_hook_post_documentation()
  45. {
  46. true
  47. }
  48. default_hook_pre_archives()
  49. {
  50. true
  51. }
  52. default_hook_post_archives()
  53. {
  54. true
  55. }
  56. default_hook_pre_doc_archives()
  57. {
  58. true
  59. }
  60. default_hook_doc_archives()
  61. {
  62. info "By default no documentation archives are created."
  63. info "Please override hook_doc_archives."
  64. }
  65. default_hook_post_doc_archives()
  66. {
  67. true
  68. }
  69. default_hook_testbuild()
  70. {
  71. cd "$1"
  72. if ! echo "$conf_testbuild" | grep -q no-configure &&\
  73. [ -x ./configure ]; then
  74. ./configure
  75. fi
  76. if ! echo "$conf_testbuild" | grep -q no-cmake &&\
  77. [ -r ./CMakeLists.txt ]; then
  78. cmake .
  79. fi
  80. if ! echo "$conf_testbuild" | grep -q no-make &&\
  81. [ -r ./GNUmakefile -o -r ./makefile -o -r ./Makefile ]; then
  82. make
  83. fi
  84. if ! echo "$conf_testbuild" | grep -q no-setuppy &&\
  85. [ -x ./setup.py ]; then
  86. ./setup.py --no-user-cfg build
  87. fi
  88. if [ -r ./Cargo.toml ]; then
  89. if ! echo "$conf_testbuild" | grep -q no-cargo-build; then
  90. info "Running cargo DEBUG build"
  91. do_cargo build
  92. info "Running cargo RELEASE build"
  93. do_cargo build --release
  94. fi
  95. if ! echo "$conf_testbuild" | grep -q no-cargo-examples; then
  96. info "Running cargo EXAMPLES build"
  97. do_cargo build --examples
  98. info "Running cargo EXAMPLES RELEASE build"
  99. do_cargo build --examples --release
  100. fi
  101. if ! echo "$conf_testbuild" | grep -q no-cargo-clippy; then
  102. info "Running cargo CLIPPY"
  103. do_cargo clippy -- --deny warnings
  104. if ! echo "$conf_testbuild" | grep -q no-cargo-tests; then
  105. info "Running cargo CLIPPY on tests"
  106. do_cargo clippy --tests -- --deny warnings
  107. fi
  108. if ! echo "$conf_testbuild" | grep -q no-cargo-examples; then
  109. info "Running cargo CLIPPY on examples"
  110. do_cargo clippy --examples -- --deny warnings
  111. fi
  112. fi
  113. if ! echo "$conf_testbuild" | grep -q no-cargo-audit; then
  114. info "Running cargo AUDIT"
  115. do_cargo audit --deny warnings
  116. fi
  117. fi
  118. }
  119. default_hook_regression_tests()
  120. {
  121. cd "$1"
  122. if [ -r ./Cargo.toml ]; then
  123. do_cargo test
  124. do_cargo test --examples
  125. fi
  126. }
  127. default_hook_pre_archive_signatures()
  128. {
  129. true
  130. }
  131. default_hook_post_archive_signatures()
  132. {
  133. true
  134. }
  135. default_hook_pre_tag()
  136. {
  137. true
  138. }
  139. default_hook_post_tag()
  140. {
  141. true
  142. }
  143. default_hook_pre_move_files()
  144. {
  145. true
  146. }
  147. default_hook_post_move_files()
  148. {
  149. true
  150. }
  151. default_hook_version()
  152. {
  153. cd "$1"
  154. if [ -r "./Cargo.toml" ]; then
  155. version="$(cargo pkgid --offline --package "$conf_package" "$conf_package" | cut -d'#' -f2)"
  156. else
  157. die "ERROR: Must supply hook_version()"
  158. fi
  159. }
  160. default_hook_pre_upload()
  161. {
  162. true
  163. }
  164. default_hook_post_upload()
  165. {
  166. true
  167. }
  168. default_hook_pre_debian_packages()
  169. {
  170. true
  171. }
  172. default_hook_post_debian_packages()
  173. {
  174. true
  175. }
  176. # Assign default hooks to actual hooks
  177. hook_pre_checkout() { default_hook_pre_checkout "$@"; }
  178. hook_post_checkout() { default_hook_post_checkout "$@"; }
  179. hook_pre_relocate_checkout() { default_hook_pre_relocate_checkout "$@"; }
  180. hook_post_relocate_checkout() { default_hook_post_relocate_checkout "$@"; }
  181. hook_pre_documentation() { default_hook_pre_documentation "$@"; }
  182. hook_post_documentation() { default_hook_post_documentation "$@"; }
  183. hook_pre_archives() { default_hook_pre_archives "$@"; }
  184. hook_post_archives() { default_hook_post_archives "$@"; }
  185. hook_pre_doc_archives() { default_hook_pre_doc_archives "$@"; }
  186. hook_doc_archives() { default_hook_doc_archives "$@"; }
  187. hook_post_doc_archives() { default_hook_post_doc_archives "$@"; }
  188. hook_testbuild() { default_hook_testbuild "$@"; }
  189. hook_regression_tests() { default_hook_regression_tests "$@"; }
  190. hook_pre_archive_signatures() { default_hook_pre_archive_signatures "$@"; }
  191. hook_post_archive_signatures() { default_hook_post_archive_signatures "$@"; }
  192. hook_pre_tag() { default_hook_pre_tag "$@"; }
  193. hook_post_tag() { default_hook_post_tag "$@"; }
  194. hook_pre_move_files() { default_hook_pre_move_files "$@"; }
  195. hook_post_move_files() { default_hook_post_move_files "$@"; }
  196. hook_get_version() { default_hook_version "$@"; }
  197. hook_pre_upload() { default_hook_pre_upload "$@"; }
  198. hook_post_upload() { default_hook_post_upload "$@"; }
  199. hook_pre_debian_packages() { default_hook_pre_debian_packages "$@"; }
  200. hook_post_debian_packages() { default_hook_post_debian_packages "$@"; }
  201. cleanup()
  202. {
  203. if [ -d "$tmpdir" ]; then
  204. if [ $opt_keeptmp -eq 0 ]; then
  205. rm -rf "$tmpdir"
  206. else
  207. info "Keeping temporary directory '$tmpdir' in place."
  208. fi
  209. fi
  210. }
  211. # $1=code
  212. abort()
  213. {
  214. cleanup
  215. exit $1
  216. }
  217. # $*=message
  218. die()
  219. {
  220. echo "$*"
  221. abort 1
  222. }
  223. terminating_signal()
  224. {
  225. die "Terminating signal received"
  226. }
  227. # $*=message
  228. info()
  229. {
  230. echo "--- $*"
  231. }
  232. # $*=message
  233. warn()
  234. {
  235. echo "--- WARNING: $*" >&2
  236. }
  237. is_dry_run()
  238. {
  239. [ $opt_dryrun -ne 0 ]
  240. }
  241. dry_run_prefix()
  242. {
  243. is_dry_run && echo -n "echo dry-run " || true
  244. }
  245. # $1=program_name, $2+=program_args
  246. dry_run()
  247. {
  248. $(dry_run_prefix) "$@"
  249. }
  250. # $1=program_name
  251. have_program()
  252. {
  253. which "$1" >/dev/null 2>&1
  254. }
  255. # $1=program_name, ($2=description)
  256. assert_program()
  257. {
  258. local bin="$1"
  259. local desc="$2"
  260. [ -n "$desc" ] || desc="$bin"
  261. have_program "$bin" || die "$bin not found. Please install $desc."
  262. }
  263. # $1=path_to_git_repo
  264. git_main_branch()
  265. {
  266. if GIT_DIR="$1" git branch | grep -qE '^\*? ?main$'; then
  267. echo "main"
  268. else
  269. echo "master"
  270. fi
  271. }
  272. # $1=hook_name, $2+=hook_parameters
  273. execute_hook()
  274. {
  275. local hook_name="hook_$1"
  276. local oldpwd="$(pwd)"
  277. shift
  278. set -e
  279. eval $hook_name "$@"
  280. set +e
  281. if [ "$(pwd)" != "$oldpwd" ]; then
  282. cd "$oldpwd" || die "execute_hook: Failed to switch back to old PWD '$oldpwd'"
  283. fi
  284. }
  285. maketemp()
  286. {
  287. local suffix="$1"
  288. mktemp --suffix="$suffix" --tmpdir="$tmpdir" "makerelease-tmpfile.XXXXXXXX"
  289. }
  290. detect_repos_type()
  291. {
  292. [ -z "$repos_type" -a -d "$srcdir/.git" ] && repos_type=git
  293. [ -z "$repos_type" ] && repos_type=none
  294. case "$repos_type" in
  295. none|git) ;; # ok
  296. *) die "Invalid \$repos_type=$repos_type" ;;
  297. esac
  298. }
  299. do_cargo()
  300. {
  301. local command="$1"
  302. shift
  303. local opt_package=
  304. if [ "$command" != "audit" ]; then
  305. if [ -n "$conf_package" ]; then
  306. local opt_package="--package $conf_package"
  307. fi
  308. fi
  309. info cargo "$command" $opt_package "$@"
  310. cargo "$command" $opt_package "$@"
  311. }
  312. make_checkout()
  313. {
  314. checkout_dir="$tmpdir/$project-checkout"
  315. mkdir -p "$checkout_dir" || die "Failed to make checkout dir"
  316. execute_hook pre_checkout "$checkout_dir"
  317. case "$repos_type" in
  318. none)
  319. info "Copying source tree"
  320. cp -r "$srcdir" "$checkout_dir" || \
  321. die "Failed to copy source tree"
  322. ;;
  323. git)
  324. info "Creating git checkout"
  325. assert_program git
  326. local branch="$(git_main_branch "$srcdir/.git")"
  327. [ -n "$opt_ref" ] && branch="$opt_ref"
  328. export GIT_DIR="$checkout_dir/.git"
  329. git clone --recursive --shared --no-checkout \
  330. "$srcdir/.git" "$checkout_dir" || \
  331. die "Failed to clone git repository"
  332. cd "$checkout_dir" || die "Internal error: cd"
  333. git checkout -b "__tmp_makerelease-$branch" "$branch" || \
  334. die "git checkout failed (1)"
  335. git checkout -f || \
  336. die "git checkout failed (2)"
  337. if [ -f "$checkout_dir/.gitmodules" ]; then
  338. git submodule update --init --recursive || \
  339. die "git submodule update failed."
  340. fi
  341. ;;
  342. *)
  343. die "checkout: Unknown repos_type"
  344. ;;
  345. esac
  346. execute_hook post_checkout "$checkout_dir"
  347. }
  348. detect_versioning()
  349. {
  350. version=
  351. release_name=
  352. execute_hook get_version "$checkout_dir/$srcsubdir"
  353. [ -n "$(echo "$version" | tr -d '.[:blank:]')" ] ||\
  354. die "\$version not set correctly in hook_get_version()"
  355. version="${version}${opt_extraversion}"
  356. [ -n "$release_name" ] || release_name="$project-$version"
  357. }
  358. relocate_checkout()
  359. {
  360. execute_hook pre_relocate_checkout "$tmpdir" "$checkout_dir"
  361. local new_checkout_dir="$tmpdir/$release_name"
  362. mv "$checkout_dir/$srcsubdir" "$new_checkout_dir" || \
  363. die "Failed to relocate checkout"
  364. rm -rf "$checkout_dir"
  365. checkout_dir="$new_checkout_dir"
  366. execute_hook post_relocate_checkout "$tmpdir" "$checkout_dir"
  367. }
  368. make_debian_packages()
  369. {
  370. [ $opt_nodebian -eq 0 ] || return
  371. [ $opt_nobuild -eq 0 ] || return
  372. [ -d "$checkout_dir/debian" ] || return
  373. info "Creating Debian packages"
  374. if ! have_program debuild; then
  375. warn "No 'debuild' available. Skipping Debian build."
  376. return
  377. fi
  378. grep -qe quilt "$checkout_dir/debian/source/format" &&\
  379. die "Debian package format 'quilt' is not supported."
  380. local ver_without_suffix="$(printf '%s' "$version" | grep -oEe '[^\-]+' | head -n1)"
  381. grep -qEe "${project}"'\s*\(\s*'"${ver_without_suffix}" \
  382. "$checkout_dir/debian/changelog" ||\
  383. die "Debian changelog does not contain the version $version."
  384. local debuild_dir="${checkout_dir}_debuild"
  385. cp -a "$checkout_dir" "$debuild_dir" ||\
  386. die "Failed to copy checkout_dir for debuild."
  387. execute_hook pre_debian_packages "$debuild_dir"
  388. cd "$debuild_dir" || die "Failed to cd to debuild_dir"
  389. local debuild_opts=
  390. if [ $opt_nosign -eq 0 -a -n "$DEB_SIGN_KEYID" ]; then
  391. info "(Signing Debian packages with key '$DEB_SIGN_KEYID', from DEB_SIGN_KEYID)"
  392. local debuild_opts="-k$DEB_SIGN_KEYID"
  393. elif [ $opt_nosign -eq 0 -a -n "$GPG_KEY_RELEASE" ]; then
  394. info "(Signing Debian packages with key '$GPG_KEY_RELEASE', from GPG_KEY_RELEASE)"
  395. local debuild_opts="-k$GPG_KEY_RELEASE"
  396. else
  397. info "(Creating unsigned Debian packages)"
  398. local debuild_opts="-uc -us"
  399. fi
  400. CFLAGS= CPPFLAGS= CXXFLAGS= LDFLAGS= debuild $debuild_opts ||\
  401. die "Failed to build debian package"
  402. local deb_archive_dir="$archive_dir/debian"
  403. mkdir -p "$deb_archive_dir" ||\
  404. die "Failed to create Debian archive directory"
  405. mv "$debuild_dir/../"*.deb "$deb_archive_dir/" ||\
  406. die "Failed to move .deb files to archive directory"
  407. mv "$debuild_dir/../"*.dsc "$deb_archive_dir/" ||\
  408. die "Failed to move .dsc files to archive directory"
  409. mv "$debuild_dir/../"*.changes "$deb_archive_dir/" ||\
  410. die "Failed to move .changes files to archive directory"
  411. execute_hook post_debian_packages "$debuild_dir" "$deb_archive_dir"
  412. }
  413. _gendoc_md_html()
  414. {
  415. local md="$1"
  416. local docname="$(basename "$md" .md)"
  417. local dir="$(dirname "$md")"
  418. local html="$dir/$docname.html"
  419. local tmpfile="$(maketemp .md)"
  420. echo "Generating $docname.md -> $docname.html ..."
  421. sed -e 's|\.md)|.html)|g' "$md" > "$tmpfile" ||\
  422. die "Failed to update links during markdown -> html"
  423. pandoc -s -M "title=$docname" -o "$html" "$tmpfile" ||\
  424. die "Failed to convert markdown -> html"
  425. }
  426. _gendoc_rst_md()
  427. {
  428. local rst="$1"
  429. local docname="$(basename "$rst" .rst)"
  430. local dir="$(dirname "$rst")"
  431. local md="$dir/$docname.md"
  432. local tmpfile="$(maketemp .rst)"
  433. echo "Generating $docname.rst -> $docname.md ..."
  434. sed -e 's|\.rst>`_|.md>`_|g' "$rst" > "$tmpfile" ||\
  435. die "Failed to update links during reStructuredText -> markdown"
  436. pandoc -s -M "title=$docname" -o "$md" "$tmpfile" ||\
  437. die "Failed to convert reStructuredText -> markdown"
  438. }
  439. _gendoc_rst_html()
  440. {
  441. local rst="$1"
  442. local docname="$(basename "$rst" .rst)"
  443. local dir="$(dirname "$rst")"
  444. local html="$dir/$docname.html"
  445. local tmpfile="$(maketemp .rst)"
  446. echo "Generating $docname.rst -> $docname.html ..."
  447. sed -e 's|\.rst>`_|.html>`_|g' "$rst" > "$tmpfile" ||\
  448. die "Failed to update links during reStructuredText -> html"
  449. pandoc -s -M "title=$docname" -o "$html" "$tmpfile" ||\
  450. die "Failed to convert reStructuredText -> html"
  451. }
  452. make_documentation()
  453. {
  454. [ $opt_nodoc -eq 0 ] || return
  455. info "Creating documentation"
  456. assert_program pandoc "pandoc converter"
  457. execute_hook pre_documentation "$checkout_dir"
  458. local old_IFS="$IFS"
  459. IFS='
  460. '
  461. # Build markdown -> html
  462. for file in $(find "$checkout_dir" -name '*.md'); do
  463. _gendoc_md_html "$file"
  464. done
  465. # Build reStructuredText -> markdown
  466. for file in $(find "$checkout_dir" -name '*.rst'); do
  467. _gendoc_rst_md "$file"
  468. done
  469. # Build reStructuredText -> html
  470. for file in $(find "$checkout_dir" -name '*.rst'); do
  471. _gendoc_rst_html "$file"
  472. done
  473. IFS="$old_IFS"
  474. execute_hook post_documentation "$checkout_dir"
  475. }
  476. make_archives()
  477. {
  478. archive_dir="$tmpdir/$project-archives"
  479. mkdir -p "$archive_dir" || die "Failed to create archive directory"
  480. setup_py_targets=
  481. info "Creating archives"
  482. execute_hook pre_archives "$archive_dir" "$checkout_dir"
  483. for artype in $opt_archives; do
  484. local archive=
  485. local compressor=
  486. case "$artype" in
  487. tar)
  488. archive="$release_name.tar"
  489. ;;
  490. tar.bz2)
  491. compressor="bzip2 -9"
  492. archive="$release_name.tar.bz2"
  493. ;;
  494. tar.gz)
  495. compressor="gzip -9"
  496. archive="$release_name.tar.gz"
  497. ;;
  498. tar.xz)
  499. compressor="xz -9"
  500. archive="$release_name.tar.xz"
  501. ;;
  502. tar.zst)
  503. compressor="zstd -T0 -19"
  504. archive="$release_name.tar.zst"
  505. ;;
  506. zip)
  507. archive="$release_name.zip"
  508. ;;
  509. 7z)
  510. archive="$release_name.7z"
  511. ;;
  512. py-*)
  513. local target="$(echo "$artype" | sed -e 's/py-//' | tr '-' '_')"
  514. setup_py_targets="$setup_py_targets $target"
  515. continue # Python archives are handled later
  516. ;;
  517. *)
  518. die "Internal error: archive type: $artype"
  519. ;;
  520. esac
  521. info "Creating $archive"
  522. cd "$tmpdir" || die "Internal error: cd"
  523. case "$artype" in
  524. zip)
  525. zip -9 -r "$archive" "$release_name" || \
  526. die "Failed to create ZIP archive"
  527. ;;
  528. 7z)
  529. "$SEVENZIP" -mx=9 a "$archive" "$release_name" || \
  530. die "Failed to create 7-ZIP archive"
  531. ;;
  532. tar)
  533. tar \
  534. --numeric-owner --owner=0 --group=0 \
  535. --mtime='1970-01-01 00:00Z' \
  536. --sort=name \
  537. -c "$release_name" \
  538. > "$archive" || \
  539. die "Failed to create tarball"
  540. ;;
  541. tar.*)
  542. tar \
  543. --numeric-owner --owner=0 --group=0 \
  544. --mtime='1970-01-01 00:00Z' \
  545. --sort=name \
  546. -c "$release_name" \
  547. | $compressor \
  548. > "$archive" || \
  549. die "Failed to create tarball"
  550. ;;
  551. *)
  552. die "Internal error: Archive type"
  553. ;;
  554. esac
  555. mv "$archive" "$archive_dir"/ || die "Failed to move archive"
  556. done
  557. # Handle Python build targets
  558. [ -f "./setup.py" ] && [ $opt_upload -ne 0 ] && setup_py_targets="$setup_py_targets sdist_gz"
  559. if [ -n "$setup_py_targets" ]; then
  560. cd "$checkout_dir" || die "Internal error: cd"
  561. [ -x "./setup.py" ] ||\
  562. die "Used Python archive target, but no executable setup.py found"
  563. rm -rf ./dist/ || die "Failed to delete ./dist/"
  564. mkdir "$archive_dir/python" || die "Failed to create Python archive subdir"
  565. local have_bdist_wininst=0
  566. local have_sdist=0
  567. local have_sdist_bz2=0
  568. local have_sdist_xz=0
  569. local have_sdist_zip=0
  570. for target in $setup_py_targets; do
  571. info "Building Python $target-package"
  572. local opts=
  573. local setup_target="$target"
  574. local doit=0
  575. if [ "$target" = "bdist_wininst" ]; then
  576. [ $have_bdist_wininst -ne 0 ] && continue
  577. local have_bdist_wininst=1
  578. local opts="--plat-name win32"
  579. local doit=1
  580. elif [ "$target" = "sdist" -o "$target" = "sdist_gz" ]; then
  581. [ $have_sdist -ne 0 ] && continue
  582. local have_sdist=1
  583. local setup_target="sdist"
  584. local opts="--formats=gztar --owner=root --group=root"
  585. local doit=1
  586. elif [ "$target" = "sdist_bz2" ]; then
  587. [ $have_sdist_bz2 -ne 0 ] && continue
  588. local have_sdist_bz2=1
  589. local setup_target="sdist"
  590. local opts="--formats=bztar --owner=root --group=root"
  591. local doit=1
  592. elif [ "$target" = "sdist_xz" ]; then
  593. [ $have_sdist_xz -ne 0 ] && continue
  594. local have_sdist_xz=1
  595. local setup_target="sdist"
  596. local opts="--formats=xztar --owner=root --group=root"
  597. local doit=1
  598. elif [ "$target" = "sdist_zip" ]; then
  599. [ $have_sdist_zip -ne 0 ] && continue
  600. local have_sdist_zip=1
  601. local setup_target="sdist"
  602. local opts="--formats=zip"
  603. local doit=1
  604. fi
  605. if [ $doit -ne 0 ]; then
  606. ./setup.py --no-user-cfg "$setup_target" $opts ||\
  607. die "Failed to build Python archive"
  608. fi
  609. done
  610. mv ./dist/* "$archive_dir/python/" || die "Failed to move Python archives"
  611. rmdir ./dist/ || die "Failed to delete ./dist/"
  612. fi
  613. execute_hook post_archives "$archive_dir" "$checkout_dir"
  614. }
  615. make_documentation_archives()
  616. {
  617. [ $opt_nodoc -eq 0 ] || return
  618. info "Packing documentation archives"
  619. execute_hook pre_doc_archives "$archive_dir" "$checkout_dir"
  620. execute_hook doc_archives "$archive_dir" "$checkout_dir"
  621. execute_hook post_doc_archives "$archive_dir" "$checkout_dir"
  622. }
  623. make_upload()
  624. {
  625. [ $opt_upload -eq 0 ] && return
  626. if [ -f "$checkout_dir/setup.py" ]; then
  627. # This is is a Python package.
  628. [ -n "$setup_py_targets" ] ||\
  629. die "PyPi-upload requested, but no Python packages have been built."
  630. info "Uploading $setup_py_targets archives to PyPi"
  631. assert_program twine
  632. local py_archive_dir="$archive_dir/python"
  633. execute_hook pre_upload "$checkout_dir" "$py_archive_dir"
  634. twine check "$py_archive_dir"/*.tar.gz* ||\
  635. die "twine check failed"
  636. dry_run twine upload --repository pypi "$py_archive_dir"/*.tar.gz* ||\
  637. die "twine upload failed"
  638. execute_hook post_upload "$checkout_dir" "$py_archive_dir"
  639. elif [ -f "$checkout_dir/Cargo.toml" ]; then
  640. # This is a rust/cargo package.
  641. info "Uploading archive to crates.io"
  642. assert_program cargo
  643. cd "$checkout_dir" || die "Internal error: cd"
  644. execute_hook pre_upload "$checkout_dir"
  645. local dry=
  646. is_dry_run && dry="--dry-run"
  647. do_cargo publish --allow-dirty $dry || die "cargo publish failed."
  648. execute_hook post_upload "$checkout_dir"
  649. else
  650. die "upload: Unknown package format."
  651. fi
  652. }
  653. make_testbuild()
  654. {
  655. [ $opt_nobuild -eq 0 ] || return
  656. info "Running test build"
  657. execute_hook testbuild "$checkout_dir"
  658. }
  659. make_regression_tests()
  660. {
  661. [ $opt_nobuild -eq 0 ] || return
  662. [ $opt_notests -eq 0 ] || return
  663. info "Running regression tests"
  664. execute_hook regression_tests "$checkout_dir"
  665. }
  666. # $1=directory
  667. gpg_sign_dir_recursive()
  668. {
  669. local dir="$1"
  670. # Don't sign files in the debian directory
  671. [ "$(basename "$dir")" = "debian" ] && return
  672. local path=
  673. for path in "$dir"/*; do
  674. if [ -d "$path" ]; then
  675. gpg_sign_dir_recursive "$path"
  676. continue
  677. fi
  678. [ -r "$path" ] || die "Sign: File not readable: $path"
  679. local filename="$(basename "$path")"
  680. local signature="$filename.asc"
  681. info "Creating signature $signature"
  682. cd "$dir" || die "Internal error: cd"
  683. local gpg=gpg
  684. have_program gpg2 && gpg=gpg2
  685. assert_program $gpg "GNU Privacy Guard"
  686. local gpg_opts=
  687. [ -n "$GPG_KEY_RELEASE" ] && gpg_opts="--default-key $GPG_KEY_RELEASE"
  688. $gpg $gpg_opts -ab "./$filename" || die "Failed to sign $filename"
  689. done
  690. }
  691. make_archive_signatures()
  692. {
  693. [ $opt_nosign -ne 0 ] && return
  694. execute_hook pre_archive_signatures "$archive_dir"
  695. gpg_sign_dir_recursive "$archive_dir"
  696. execute_hook post_archive_signatures "$archive_dir"
  697. }
  698. make_tag()
  699. {
  700. [ $conf_notag -ne 0 ] && return
  701. [ $opt_notag -ne 0 ] && return
  702. [ "$repos_type" = "none" ] && return
  703. info "Tagging repository"
  704. tag_name="$project-$version"
  705. tag_message="$project-$version release"
  706. execute_hook pre_tag "$srcdir"
  707. cd "$srcdir" || die "Internal error: cd"
  708. case "$repos_type" in
  709. git)
  710. assert_program git
  711. local opts=
  712. if [ $opt_nosign -eq 0 ]; then
  713. if [ -n "$GPG_KEY_RELEASE" ]; then
  714. opts="$opts -u $GPG_KEY_RELEASE"
  715. else
  716. opts="$opts -s" # default key
  717. fi
  718. else
  719. opts="$opts -a" # unsigned
  720. fi
  721. local branch="$(git_main_branch "$srcdir/.git")"
  722. [ -n "$opt_ref" ] && branch="$opt_ref"
  723. export GIT_DIR="$srcdir/.git"
  724. dry_run git tag $opts -m "$tag_message" "$tag_name" "$branch"
  725. ;;
  726. *)
  727. die "tagging: Unknown repos_type"
  728. ;;
  729. esac
  730. execute_hook post_tag "$srcdir"
  731. }
  732. move_files()
  733. {
  734. local target_dir="$srcdir/$srcsubdir/release-archives"
  735. info "Moving files"
  736. execute_hook pre_move_files "$archive_dir" "$srcdir/$srcsubdir"
  737. dry_run mkdir -p "$target_dir" || die "Failed to create target directory"
  738. dry_run cp -r "$archive_dir"/* "$target_dir"/ || \
  739. die "Failed to copy tarballs"
  740. execute_hook post_move_files "$archive_dir" "$srcdir/$srcsubdir"
  741. local dry=
  742. is_dry_run && dry=" (DRY RUN)"
  743. echo
  744. info "Built $project release ${version}${dry}"
  745. }
  746. help()
  747. {
  748. echo "Usage: $0 [OPTIONS]"
  749. echo
  750. echo "Environment:"
  751. echo " MAKERELEASE_LIB May be set to makerelease.lib"
  752. echo
  753. echo "Options:"
  754. echo " -y|--dry-run Do not make persistent changes"
  755. echo " -t|--no-tag Do not create the repository tag"
  756. echo " -s|--no-sign Do not sign"
  757. echo " -b|--no-build Do not run build. (Implies -T)"
  758. echo " -T|--no-tests Do not run regression tests"
  759. echo " -q|--quick Quick run. Equivalent to -t -s -b -T -d"
  760. echo " -D|--no-doc Do not build documentation"
  761. echo " -r|--ref REF Checkout version control reference REF"
  762. echo " -a|--archives TYPE,TYPE,... Archive type list. Default: $default_archives"
  763. echo " Possible types: tar, tar.bz2, tar.gz, tar.xz, tar.zst, zip, 7z"
  764. echo " For Python programs: py-sdist,"
  765. echo " py-sdist-gz, py-sdist-bz2, py-sdist-xz, py-dist-zip,"
  766. echo " py-bdist, py-bdist-wininst,"
  767. echo " py-bdist-dumb, py-bdist-rpm"
  768. echo " -d|--no-debian Do not build Debian packages."
  769. echo " -U|--upload Upload the archives to PyPi or crates.io"
  770. echo " -O|--upload-only Run the bare minimum to upload only."
  771. echo " Equivalent to: -U -t -b -T -d"
  772. echo " -V|--extraversion XX Append XX to version string"
  773. echo " -K|--keep-tmp Do not delete temporary files"
  774. echo " -h|--help Show this help text"
  775. }
  776. # This is the main function called from the main script.
  777. # Parameters to this functions must be the main script arguments.
  778. makerelease()
  779. {
  780. # Backwards compatibility for old default_compress option
  781. [ -n "$default_compress" -a -z "$default_archives" ] && default_archives="$default_compress"
  782. [ -n "$project" ] || die "\$project variable not set"
  783. [ -n "$srcdir" ] || die "\$srcdir variable not set"
  784. [ -n "$srcsubdir" ] || srcsubdir=""
  785. [ -n "$conf_package" ] || conf_package=""
  786. [ -n "$tmp_basedir" ] || tmp_basedir="/tmp"
  787. [ -n "$default_archives" ] || default_archives="tar.xz"
  788. [ -n "$conf_testbuild" ] || conf_testbuild=""
  789. [ -n "$conf_notag" ] || conf_notag=0
  790. trap terminating_signal TERM INT
  791. trap cleanup EXIT
  792. # Reproducible builds.
  793. export PYTHONHASHSEED=1
  794. export SOURCE_DATE_EPOCH=0
  795. local template="makerelease-$project.XXXXXXXX"
  796. tmpdir="$(mktemp -d --tmpdir="$tmp_basedir" "$template")"
  797. [ -d "$tmpdir" ] || die "Failed to create temporary directory"
  798. if have_program 7z; then
  799. SEVENZIP=7z
  800. elif have_program 7zz; then
  801. SEVENZIP=7zz
  802. else
  803. die "Program 7-Zip not found"
  804. fi
  805. opt_dryrun=0
  806. opt_notag=0
  807. opt_nosign=0
  808. opt_nobuild=0
  809. opt_nodebian=0
  810. opt_notests=0
  811. opt_nodoc=0
  812. opt_ref=
  813. opt_archives="$default_archives"
  814. opt_upload=0
  815. opt_extraversion=
  816. opt_keeptmp=0
  817. while [ $# -ge 1 ]; do
  818. case "$1" in
  819. --help|-h)
  820. help "$@"
  821. abort 0
  822. ;;
  823. -y|--dry-run)
  824. opt_dryrun=1
  825. ;;
  826. -t|--no-tag)
  827. opt_notag=1
  828. ;;
  829. -s|--no-sign)
  830. opt_nosign=1
  831. ;;
  832. -b|--no-build)
  833. opt_nobuild=1
  834. ;;
  835. -T|--no-tests)
  836. opt_notests=1
  837. ;;
  838. -q|--quick)
  839. opt_notag=1
  840. opt_nosign=1
  841. opt_nobuild=1
  842. opt_nodebian=1
  843. opt_notests=1
  844. ;;
  845. -D|--no-doc)
  846. opt_nodoc=1
  847. ;;
  848. -r|--ref)
  849. shift
  850. opt_ref=$1
  851. [ -n "$opt_ref" ] || die "Invalid --ref"
  852. ;;
  853. -a|--archives)
  854. shift
  855. opt_archives="$1"
  856. ;;
  857. -d|--no-debian)
  858. opt_nodebian=1
  859. ;;
  860. -U|--upload)
  861. opt_upload=1
  862. ;;
  863. -O|--upload-only)
  864. opt_notag=1
  865. opt_nobuild=1
  866. opt_nodebian=1
  867. opt_notests=1
  868. opt_upload=1
  869. ;;
  870. -V|--extraversion)
  871. shift
  872. opt_extraversion="$1"
  873. [ -n "$opt_extraversion" ] || die "Invalid --extraversion"
  874. ;;
  875. -K|--keep-tmp)
  876. opt_keeptmp=1
  877. ;;
  878. *)
  879. die "Invalid option: $1"
  880. ;;
  881. esac
  882. shift
  883. done
  884. opt_archives="$(echo "$opt_archives" | tr ',' ' ')"
  885. for artype in $opt_archives; do
  886. case "$artype" in
  887. tar|tar.bz2|tar.gz|tar.xz|tar.zst|zip|7z|py-sdist|py-sdist-gz|py-sdist-bz2|py-sdist-xz|py-sdist-zip|py-bdist|py-bdist-wininst|py-bdist-dump|py-bdist-rpm) ;; # ok
  888. *) die "Invalid archiving method: $artype" ;;
  889. esac
  890. done
  891. [ -n "$(echo "$opt_archives" | tr -d '[:blank:]')" ] ||\
  892. die "No archiving method selected"
  893. detect_repos_type
  894. make_checkout
  895. detect_versioning
  896. relocate_checkout
  897. make_documentation
  898. make_archives
  899. make_documentation_archives
  900. make_testbuild
  901. make_regression_tests
  902. make_debian_packages
  903. make_archive_signatures
  904. make_tag
  905. make_upload
  906. move_files
  907. cleanup
  908. }
  909. # vim: syntax=sh