makerelease.lib 26 KB

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