2021-01-22 19:44:05 +03:00
#!/usr/bin/env bash
2019-09-24 09:56:39 +03:00
set -eu
2019-12-21 00:49:27 +03:00
2023-01-05 20:55:14 +03:00
SCRIPT = " $( realpath $( dirname " ${ BASH_SOURCE [0] } " ) ) "
2022-05-20 00:28:43 +03:00
if [ -z " ${ SERENITY_STRIPPED_ENV :- } " ] ; then
exec " ${ SCRIPT } /.strip_env.sh " " ${ @ } "
fi
unset SERENITY_STRIPPED_ENV
2022-04-29 23:21:40 +03:00
export MAKEJOBS = " ${ MAKEJOBS :- $( nproc) } "
2022-12-11 20:36:04 +03:00
export CMAKE_BUILD_PARALLEL_LEVEL = " $MAKEJOBS "
2021-03-11 21:50:44 +03:00
2022-10-23 16:22:42 +03:00
buildstep( ) {
local buildstep_name = $1
shift
2022-11-24 13:44:09 +03:00
if [ " $# " -eq '0' ] ; then
2022-10-23 16:22:42 +03:00
" ${ buildstep_name } "
else
" $@ "
fi 2>& 1 | sed $'s|^|\x1b[34m[' " ${ port } / ${ buildstep_name } " $']\x1b[39m |'
2022-10-25 08:38:51 +03:00
local return_code = ${ PIPESTATUS [0] }
if [ ${ return_code } != 0 ] ; then
echo -e " \x1b[1;31mError in step ${ port } / ${ buildstep_name } (status= ${ return_code } )\x1b[0m "
fi
return ${ return_code }
2022-10-23 16:22:42 +03:00
}
buildstep_intro( ) {
echo -e " \x1b[1;32m=> $@ \x1b[0m "
}
2022-04-21 19:58:02 +03:00
target_env( ) {
2022-10-22 05:53:50 +03:00
if [ -f " ${ SCRIPT } /.hosted_defs.sh " ] ; then
. " ${ SCRIPT } /.hosted_defs.sh "
elif [ " $( uname -s) " = "SerenityOS" ] ; then
export SERENITY_ARCH = " $( uname -m) "
export SERENITY_INSTALL_ROOT = ""
else
>& 2 echo "Error: .hosted_defs.sh is missing and we are not running on Serenity."
exit 1
fi
2022-04-21 19:58:02 +03:00
}
target_env
DESTDIR = " ${ SERENITY_INSTALL_ROOT } "
2021-04-18 10:13:16 +03:00
enable_ccache( ) {
2022-06-11 23:18:59 +03:00
if [ " ${ USE_CCACHE :- true } " = "true" ] && command -v ccache & >/dev/null; then
2021-04-27 18:10:18 +03:00
ccache_tooldir = " ${ SERENITY_BUILD_DIR } /ccache "
mkdir -p " $ccache_tooldir "
2022-06-12 01:29:05 +03:00
for tool in cc clang gcc c++ clang++ g++; do
2022-07-07 20:22:44 +03:00
name = " ${ SERENITY_ARCH } -pc-serenity- ${ tool } "
if ! command -v " ${ name } " >/dev/null; then
continue
fi
ln -sf " $( command -v ccache) " " ${ ccache_tooldir } / ${ name } "
2022-06-12 01:29:05 +03:00
done
2021-04-27 18:10:18 +03:00
export PATH = " ${ ccache_tooldir } : $PATH "
2021-04-18 10:13:16 +03:00
fi
}
2022-04-21 19:58:02 +03:00
enable_ccache
2021-04-12 00:21:01 +03:00
host_env( ) {
export CC = " ${ HOST_CC } "
export CXX = " ${ HOST_CXX } "
2022-12-31 21:40:19 +03:00
export LD = " ${ HOST_LD } "
2021-04-12 00:21:01 +03:00
export AR = " ${ HOST_AR } "
export RANLIB = " ${ HOST_RANLIB } "
export PATH = " ${ HOST_PATH } "
2022-01-08 15:50:11 +03:00
export READELF = " ${ HOST_READELF } "
2022-02-21 00:58:26 +03:00
export OBJCOPY = " ${ HOST_OBJCOPY } "
2022-05-07 19:29:03 +03:00
export STRIP = " ${ HOST_STRIP } "
2022-06-12 01:32:24 +03:00
export CXXFILT = " ${ HOST_CXXFILT } "
2021-04-12 00:21:01 +03:00
export PKG_CONFIG_DIR = " ${ HOST_PKG_CONFIG_DIR } "
export PKG_CONFIG_SYSROOT_DIR = " ${ HOST_PKG_CONFIG_SYSROOT_DIR } "
export PKG_CONFIG_LIBDIR = " ${ HOST_PKG_CONFIG_LIBDIR } "
2021-04-18 10:13:16 +03:00
enable_ccache
2021-04-12 00:21:01 +03:00
}
2021-04-03 06:53:41 +03:00
2023-04-22 16:09:01 +03:00
installedpackagesdb = " ${ DESTDIR } /usr/Ports/installed.db "
2019-05-28 01:02:29 +03:00
2022-04-29 23:21:40 +03:00
makeopts = ( " -j ${ MAKEJOBS } " )
2021-09-27 01:16:18 +03:00
installopts = ( )
2021-09-26 21:46:41 +03:00
configscript = configure
2021-09-27 01:16:18 +03:00
configopts = ( )
2021-09-26 21:46:41 +03:00
useconfigure = false
2022-05-25 13:51:13 +03:00
config_sub_paths = ( "config.sub" )
config_guess_paths = ( "config.guess" )
2022-01-08 16:32:29 +03:00
use_fresh_config_sub = false
2022-02-25 22:26:15 +03:00
use_fresh_config_guess = false
2021-09-27 01:16:18 +03:00
depends = ( )
2021-09-26 21:46:41 +03:00
patchlevel = 1
launcher_name =
launcher_category =
launcher_command =
2022-10-16 04:11:55 +03:00
launcher_workdir =
2021-09-26 21:46:41 +03:00
launcher_run_in_terminal = false
icon_file =
2019-09-24 09:56:39 +03:00
. " $@ "
shift
: " ${ workdir : = $port - $version } "
2019-05-28 02:25:29 +03:00
2022-07-07 19:54:55 +03:00
PORT_META_DIR = " $( pwd ) "
2022-10-22 05:53:50 +03:00
if [ [ -z ${ SERENITY_BUILD_DIR :- } ] ] ; then
PORT_BUILD_DIR = " ${ PORT_META_DIR } "
else
PORT_BUILD_DIR = " ${ SERENITY_BUILD_DIR } /Ports/ ${ port } "
fi
2022-07-07 19:54:55 +03:00
mkdir -p " ${ PORT_BUILD_DIR } "
cd " ${ PORT_BUILD_DIR } "
2023-09-02 07:32:27 +03:00
# 1 = url
# 2 = sha256sum
FILES_SIMPLE_PATTERN = '^(https?:\/\/.+)#([0-9a-f]{64})$'
2023-09-02 10:18:50 +03:00
# 1 = repository
# 2 = revision
FILES_GIT_PATTERN = '^git\+(.+)#(.+)$'
2022-01-12 22:37:55 +03:00
cleanup_git( ) {
echo " WARNING: Reverting changes to $workdir as we are in dev mode! "
run git clean -xffd >/dev/null 2>& 1
}
2022-09-17 00:43:16 +03:00
# Make sure to clean up the git repository of the port afterwards.
2022-01-12 22:37:55 +03:00
if [ -n " ${ IN_SERENITY_PORT_DEV :- } " ] ; then
echo "WARNING: All changes to the workdir in the current state (inside ./package.sh dev) are temporary!"
echo " They will be reverted once the command exits!"
trap "run cleanup_git" EXIT
fi
2019-09-24 09:56:39 +03:00
run_nocd( ) {
2022-01-08 16:32:29 +03:00
echo " + $@ (nocd) " >& 2
2019-05-28 03:58:36 +03:00
( " $@ " )
}
2021-05-20 22:04:40 +03:00
2019-09-24 09:56:39 +03:00
run( ) {
echo " + $@ "
( cd " $workdir " && " $@ " )
2019-05-28 01:02:29 +03:00
}
2021-05-20 22:04:40 +03:00
2021-04-20 19:36:03 +03:00
run_replace_in_file( ) {
2022-02-23 17:00:08 +03:00
if [ " $( uname -s) " = "SerenityOS" ] ; then
run sed -i " $1 " $2
else
run perl -p -i -e " $1 " $2
fi
2019-09-24 09:56:39 +03:00
}
2021-05-20 22:04:40 +03:00
2023-01-09 01:16:04 +03:00
sed_in_place( ) {
if [ " $( uname -s) " = "Darwin" ] ; then
sed -i '' " ${ @ } "
else
sed -i " ${ @ } "
fi
}
2022-01-08 16:32:29 +03:00
get_new_config_sub( ) {
config_sub = " ${ 1 :- config .sub } "
2022-03-15 15:36:51 +03:00
if [ ! -f " $workdir / $config_sub " ] ; then
>& 2 echo " Error: Downloaded $config_sub does not replace an existing file! "
exit 1
fi
2022-01-08 16:32:29 +03:00
if ! run grep -q serenity " $config_sub " ; then
2022-05-25 13:44:25 +03:00
run do_download_file "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub" " ${ config_sub } " false
2022-01-08 16:32:29 +03:00
fi
}
2022-02-25 22:26:15 +03:00
get_new_config_guess( ) {
config_guess = " ${ 1 :- config .guess } "
2022-03-15 15:36:51 +03:00
if [ ! -f " $workdir / $config_guess " ] ; then
>& 2 echo " Error: Downloaded $config_guess does not replace an existing file! "
exit 1
fi
2022-02-25 22:26:15 +03:00
if ! run grep -q SerenityOS " $config_guess " ; then
2022-05-25 13:44:25 +03:00
run do_download_file "https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess" " ${ config_guess } " false
2022-02-25 22:26:15 +03:00
fi
}
2022-01-08 16:32:29 +03:00
ensure_new_config_sub( ) {
2022-05-25 13:51:13 +03:00
for path in " ${ config_sub_paths [@] } " ; do
get_new_config_sub " ${ path } "
done
2022-01-08 16:32:29 +03:00
}
2022-02-25 22:26:15 +03:00
ensure_new_config_guess( ) {
2022-05-25 13:51:13 +03:00
for path in " ${ config_guess_paths [@] } " ; do
get_new_config_guess " ${ path } "
done
2022-02-25 22:26:15 +03:00
}
2021-05-20 22:04:40 +03:00
ensure_build( ) {
# Sanity check.
if [ ! -f " ${ DESTDIR } /usr/lib/libc.so " ] ; then
echo "libc.so could not be found. This likely means that SerenityOS:"
echo "- has not been built and/or installed yet"
echo "- has been installed in an unexpected location"
echo " The currently configured build directory is ${ SERENITY_BUILD_DIR } . Resolve this issue and try again. "
exit 1
fi
}
2021-06-06 05:45:20 +03:00
install_main_icon( ) {
if [ -n " $icon_file " ] && [ -n " $launcher_command " ] ; then
2021-08-04 11:50:27 +03:00
local launcher_binary = " ${ launcher_command %% * } "
install_icon " $icon_file " " ${ launcher_binary } "
2021-06-06 05:45:20 +03:00
fi
}
install_icon( ) {
if [ " $# " -lt 2 ] ; then
echo "Syntax: install_icon <icon> <launcher>"
exit 1
fi
2021-08-04 11:50:27 +03:00
local icon = " $1 "
local launcher = " $2 "
command -v convert >/dev/null || true
local convert_exists = $?
command -v identify >/dev/null || true
local identify_exists = $?
if [ " ${ convert_exists } " != "0" ] || [ " ${ identify_exists } " != 0 ] ; then
echo 'Unable to install icon: missing convert or identify, did you install ImageMagick?'
return
2021-06-06 05:45:20 +03:00
fi
2021-08-04 11:50:27 +03:00
for icon_size in "16x16" "32x32" ; do
2023-08-19 02:11:33 +03:00
index = $( run identify -format '%p;%wx%h\n' " $icon " | grep " $icon_size " | cut -d";" -f1 | head -n1)
2021-08-04 11:50:27 +03:00
if [ -n " $index " ] ; then
run convert " ${ icon } [ ${ index } ] " " app- ${ icon_size } .png "
else
2021-08-04 12:20:31 +03:00
run convert " $icon [0] " -resize $icon_size " app- ${ icon_size } .png "
2021-08-04 11:50:27 +03:00
fi
done
2022-02-21 00:58:26 +03:00
run $OBJCOPY --add-section serenity_icon_s = "app-16x16.png" " ${ DESTDIR } ${ launcher } "
run $OBJCOPY --add-section serenity_icon_m = "app-32x32.png" " ${ DESTDIR } ${ launcher } "
2021-06-06 05:45:20 +03:00
}
2021-06-04 00:39:01 +03:00
install_main_launcher( ) {
if [ -n " $launcher_name " ] && [ -n " $launcher_category " ] && [ -n " $launcher_command " ] ; then
2022-10-16 04:11:55 +03:00
install_launcher " $launcher_name " " $launcher_category " " $launcher_command " " $launcher_workdir "
2021-06-04 00:39:01 +03:00
fi
}
2021-04-20 19:36:03 +03:00
install_launcher( ) {
2022-10-16 04:11:55 +03:00
if [ " $# " -lt 4 ] ; then
echo "Syntax: install_launcher <name> <category> <command> <workdir>"
2021-06-04 00:39:01 +03:00
exit 1
2021-04-20 19:36:03 +03:00
fi
2021-08-04 14:18:31 +03:00
local launcher_name = " $1 "
local launcher_category = " $2 "
local launcher_command = " $3 "
2022-10-16 04:11:55 +03:00
local launcher_workdir = " $4 "
2021-08-04 14:18:31 +03:00
local launcher_filename = " ${ launcher_name ,, } "
2021-06-04 00:39:01 +03:00
launcher_filename = " ${ launcher_filename // / } "
2021-08-04 14:18:31 +03:00
local icon_override = ""
2021-06-04 00:39:01 +03:00
case " $launcher_command " in
*\ *)
mkdir -p $DESTDIR /usr/local/libexec
launcher_executable = " /usr/local/libexec/ $launcher_filename "
cat >" $DESTDIR / $launcher_executable " <<SCRIPT
2021-04-20 19:36:03 +03:00
#!/bin/sh
exec $( printf '%q ' $launcher_command )
SCRIPT
2021-06-04 00:39:01 +03:00
chmod +x " $DESTDIR / $launcher_executable "
2021-08-04 14:18:31 +03:00
icon_override = " IconPath= ${ launcher_command %% * } "
2021-06-04 00:39:01 +03:00
; ;
*)
launcher_executable = " $launcher_command "
; ;
esac
2021-04-20 19:36:03 +03:00
mkdir -p $DESTDIR /res/apps
2021-06-04 00:39:01 +03:00
cat >$DESTDIR /res/apps/$launcher_filename .af <<CONFIG
2021-04-20 19:36:03 +03:00
[ App]
Name = $launcher_name
2021-06-04 00:39:01 +03:00
Executable = $launcher_executable
2021-04-20 19:36:03 +03:00
Category = $launcher_category
2022-10-16 04:11:55 +03:00
WorkingDirectory = $launcher_workdir
2021-07-20 00:06:09 +03:00
RunInTerminal = $launcher_run_in_terminal
2021-08-04 14:18:31 +03:00
${ icon_override }
2021-04-20 19:36:03 +03:00
CONFIG
}
2019-09-24 09:56:39 +03:00
# Checks if a function is defined. In this case, if the function is not defined in the port's script, then we will use our defaults. This way, ports don't need to include these functions every time, but they can override our defaults if needed.
func_defined( ) {
2019-11-03 12:05:02 +03:00
PATH = command -V " $1 " > /dev/null 2>& 1
2019-09-24 09:56:39 +03:00
}
2020-02-04 03:07:27 +03:00
2021-07-20 16:40:41 +03:00
func_defined pre_fetch || pre_fetch( ) {
:
}
2020-02-04 03:07:27 +03:00
func_defined post_fetch || post_fetch( ) {
:
}
2022-01-08 16:32:29 +03:00
do_download_file( ) {
local url = " $1 "
local filename = " $2 "
local accept_existing = " ${ 3 :- true } "
2023-07-05 15:04:26 +03:00
if $accept_existing && [ -f " $filename " ] ; then
echo " $filename already exists "
return
fi
2022-01-08 16:32:29 +03:00
echo " Downloading URL: ${ url } "
2023-07-05 15:04:26 +03:00
if which curl; then
run_nocd curl ${ curlopts :- } " $url " -L -o " $filename "
2022-01-08 16:32:29 +03:00
else
2023-07-05 15:04:26 +03:00
run_nocd pro " $url " > " $filename "
2022-01-08 16:32:29 +03:00
fi
}
2023-07-12 01:55:13 +03:00
fetch_simple( ) {
url = " ${ 1 } "
2023-08-07 22:10:39 +03:00
checksum = " ${ 2 } "
2021-07-20 16:40:41 +03:00
2023-08-07 22:10:39 +03:00
filename = " $( basename " ${ url } " ) "
2023-08-07 14:49:25 +03:00
2023-07-12 01:55:13 +03:00
tried_download_again = 0
2023-07-12 01:45:34 +03:00
2023-07-12 01:55:13 +03:00
while true; do
do_download_file " ${ url } " " ${ PORT_META_DIR } / ${ filename } "
2021-04-25 13:48:19 +03:00
2023-07-12 01:55:13 +03:00
actual_checksum = " $( sha256sum " ${ PORT_META_DIR } / ${ filename } " | cut -f1 -d' ' ) "
2021-04-25 13:48:19 +03:00
2023-07-12 01:55:13 +03:00
if [ " ${ actual_checksum } " = " ${ checksum } " ] ; then
break
fi
2021-04-25 13:48:19 +03:00
2023-07-12 01:55:13 +03:00
echo " SHA256 checksum of downloaded file ' ${ filename } ' does not match! "
echo " Expected: ${ checksum } "
echo " Actual: ${ actual_checksum } "
rm -f " ${ PORT_META_DIR } / ${ filename } "
echo "Removed erroneous download."
if [ " ${ tried_download_again } " -eq 1 ] ; then
echo "Please run script again."
exit 1
fi
echo "Trying to download the file again."
tried_download_again = 1
done
2021-04-25 13:48:19 +03:00
2023-07-12 01:55:13 +03:00
if [ ! -f " $workdir " /.${ filename } _extracted ] ; then
case " $filename " in
*.tar.gz| *.tar.bz| *.tar.bz2| *.tar.xz| *.tar.lz| *.tar.zst| .tbz*| *.txz| *.tgz)
run_nocd tar -xf " ${ PORT_META_DIR } / ${ filename } "
run touch " . ${ filename } _extracted "
; ;
*.gz)
run_nocd gunzip " ${ PORT_META_DIR } / ${ filename } "
run touch " . ${ filename } _extracted "
; ;
*.zip)
run_nocd bsdtar xf " ${ PORT_META_DIR } / ${ filename } " || run_nocd unzip -qo " ${ PORT_META_DIR } / ${ filename } "
run touch " . ${ filename } _extracted "
; ;
*)
echo " Note: no case for file $filename . "
cp " ${ PORT_META_DIR } / ${ filename } " ./
; ;
esac
fi
}
2021-04-11 02:01:20 +03:00
2023-09-02 10:18:50 +03:00
fetch_git( ) {
repository = " ${ 1 } "
revision = " ${ 2 } "
directory = " $( basename " ${ repository } " ) "
backing_copy = " ${ PORT_META_DIR } / ${ directory } "
working_copy = " ${ PORT_BUILD_DIR } / ${ directory } "
run_nocd git init --bare " ${ backing_copy } "
run_nocd git -C " ${ backing_copy } " config core.autocrlf false
run_nocd git -C " ${ backing_copy } " worktree prune
run_nocd git -C " ${ backing_copy } " fetch --tags " ${ repository } " " ${ revision } "
revision = " $( git -C " ${ backing_copy } " rev-parse FETCH_HEAD) "
if [ ! -e " ${ working_copy } /.git " ] ; then
run_nocd git -C " ${ backing_copy } " worktree add " ${ working_copy } " " ${ revision } "
fi
old_revision = ""
if [ -e " ${ backing_copy } /refs/tags/source " ] ; then
old_revision = " $( git -C " ${ working_copy } " rev-parse refs/tags/source) "
fi
if ! [ " ${ old_revision } " = " ${ revision } " ] ; then
run_nocd git -C " ${ working_copy } " clean -ffdx
run_nocd git -C " ${ working_copy } " reset --hard
run_nocd git -C " ${ working_copy } " tag --no-sign -f source " ${ revision } "
run_nocd git -C " ${ working_copy } " checkout " ${ revision } "
fi
}
2023-09-02 10:21:01 +03:00
fetch( ) {
2023-07-12 01:55:13 +03:00
pre_fetch
for f in " ${ files [@] } " ; do
2023-09-02 07:32:27 +03:00
if [ [ " ${ f } " = ~ ${ FILES_SIMPLE_PATTERN } ] ] ; then
url = " ${ BASH_REMATCH [1] } "
sha256sum = " ${ BASH_REMATCH [2] } "
fetch_simple " ${ url } " " ${ sha256sum } "
continue
fi
2023-09-02 10:18:50 +03:00
if [ [ " ${ f } " = ~ ${ FILES_GIT_PATTERN } ] ] ; then
repository = " ${ BASH_REMATCH [1] } "
revision = " ${ BASH_REMATCH [2] } "
fetch_git " ${ repository } " " ${ revision } "
continue
fi
2023-09-02 07:32:27 +03:00
echo " error: Unknown syntax for files entry ' ${ f } ' "
exit 1
2019-12-23 15:24:56 +03:00
done
2020-02-04 03:07:27 +03:00
post_fetch
}
2022-02-11 17:19:24 +03:00
func_defined pre_install || pre_install( ) {
:
}
2021-06-10 19:57:29 +03:00
func_defined pre_patch || pre_patch( ) {
:
}
2023-09-02 10:21:01 +03:00
patch_internal( ) {
2023-09-02 10:18:50 +03:00
if [ -n " ${ IN_SERENITY_PORT_DEV :- } " ] ; then
return
fi
2020-02-04 03:07:27 +03:00
# patch if it was not yet patched (applying patches multiple times doesn't work!)
2023-09-02 10:18:50 +03:00
if [ -d " ${ PORT_META_DIR } /patches " ] ; then
2022-07-07 19:54:55 +03:00
for filepath in " ${ PORT_META_DIR } " /patches/*.patch; do
2020-02-04 03:07:27 +03:00
filename = $( basename $filepath )
2023-09-02 10:18:50 +03:00
if [ -f " $workdir " /.${ filename } _applied ] ; then
continue
fi
if [ -e " ${ workdir } /.git " ] ; then
run git am --keep-cr --keep-non-patch " ${ filepath } "
else
2020-02-04 03:07:27 +03:00
run patch -p" $patchlevel " < " $filepath "
run touch .${ filename } _applied
fi
2019-09-24 09:56:39 +03:00
done
2019-05-28 19:55:49 +03:00
fi
2023-09-02 10:18:50 +03:00
if [ -e " ${ workdir } /.git " ] ; then
run git tag --no-sign -f patched
fi
2019-05-28 19:55:49 +03:00
}
2020-04-15 15:54:23 +03:00
func_defined pre_configure || pre_configure( ) {
2022-01-21 14:53:13 +03:00
:
2020-04-15 15:54:23 +03:00
}
2019-09-24 09:56:39 +03:00
func_defined configure || configure( ) {
2021-01-22 19:09:40 +03:00
chmod +x " ${ workdir } " /" $configscript "
2022-10-22 05:53:50 +03:00
if [ [ -n " ${ SERENITY_SOURCE_DIR :- } " ] ] ; then
run ./" $configscript " --host= " ${ SERENITY_ARCH } -pc-serenity " " ${ configopts [@] } "
else
run ./" $configscript " --build= " ${ SERENITY_ARCH } -pc-serenity " " ${ configopts [@] } "
fi
2019-05-28 19:55:49 +03:00
}
2021-02-01 21:40:19 +03:00
func_defined post_configure || post_configure( ) {
:
}
2019-09-24 09:56:39 +03:00
func_defined build || build( ) {
2021-09-27 01:16:18 +03:00
run make " ${ makeopts [@] } "
2019-05-28 11:25:39 +03:00
}
2019-09-24 09:56:39 +03:00
func_defined install || install( ) {
2021-09-27 01:16:18 +03:00
run make DESTDIR = $DESTDIR " ${ installopts [@] } " install
2019-05-28 02:25:29 +03:00
}
2020-03-25 17:54:30 +03:00
func_defined post_install || post_install( ) {
2022-10-23 16:22:42 +03:00
:
2020-03-25 17:54:30 +03:00
}
2022-09-13 14:16:28 +03:00
clean( ) {
2023-05-09 02:14:43 +03:00
rm -rf " ${ PORT_BUILD_DIR } / " *
2019-05-28 01:02:29 +03:00
}
2022-09-13 14:16:28 +03:00
clean_dist( ) {
2023-07-10 14:10:29 +03:00
for f in " ${ files [@] } " ; do
2023-09-02 07:32:27 +03:00
if [ [ " ${ f } " = ~ ${ FILES_SIMPLE_PATTERN } ] ] ; then
url = " ${ BASH_REMATCH [1] } "
filename = $( basename " $url " )
rm -f " ${ PORT_META_DIR } / ${ filename } "
continue
fi
2023-09-02 10:18:50 +03:00
if [ [ " ${ f } " = ~ ${ FILES_GIT_PATTERN } ] ] ; then
repository = " ${ BASH_REMATCH [1] } "
directory = $( basename " $repository " )
rm -rf " ${ PORT_META_DIR } / ${ directory } "
continue
fi
2023-09-02 07:32:27 +03:00
echo " error: Unknown syntax for files entry ' ${ f } ' "
exit 1
2019-09-24 09:56:39 +03:00
done
2019-05-28 01:02:29 +03:00
}
2022-09-13 14:16:28 +03:00
clean_all( ) {
2022-09-13 13:40:04 +03:00
clean
clean_dist
2019-05-28 01:02:29 +03:00
}
2019-09-24 09:56:39 +03:00
addtodb( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Adding $port $version to database of installed ports... "
2021-10-20 01:14:54 +03:00
if [ -n " $( package_install_state $port $version ) " ] ; then
2022-10-23 16:22:42 +03:00
echo " Note: Skipped because $port $version is already installed. "
2021-10-20 01:14:54 +03:00
return
2019-09-24 09:56:39 +03:00
fi
2021-10-20 01:14:54 +03:00
if [ " ${ 1 :- } " = "--auto" ] ; then
2023-04-22 16:09:01 +03:00
echo " auto $port $version " >> " $installedpackagesdb "
2019-09-24 09:56:39 +03:00
else
2023-04-22 16:09:01 +03:00
echo " manual $port $version " >> " $installedpackagesdb "
2021-10-20 01:14:54 +03:00
fi
if [ " ${# depends [@] } " -gt 0 ] ; then
2023-04-22 16:09:01 +03:00
echo " dependency $port ${ depends [@] } " >> " $installedpackagesdb "
2021-10-20 01:14:54 +03:00
fi
echo " Successfully installed $port $version . "
}
2023-04-22 16:09:01 +03:00
ensure_installedpackagesdb( ) {
if [ ! -f " $installedpackagesdb " ] ; then
mkdir -p " $( dirname $installedpackagesdb ) "
touch " $installedpackagesdb "
2019-09-24 09:56:39 +03:00
fi
2019-05-28 01:02:29 +03:00
}
2021-10-20 01:14:54 +03:00
package_install_state( ) {
local port = $1
local version = ${ 2 :- }
2023-04-22 16:09:01 +03:00
ensure_installedpackagesdb
grep -E " ^(auto|manual) $port $version " " $installedpackagesdb " | cut -d' ' -f1
2021-10-20 01:14:54 +03:00
}
2019-09-24 09:56:39 +03:00
installdepends( ) {
2021-09-27 01:16:18 +03:00
for depend in " ${ depends [@] } " ; do
2023-02-02 00:05:40 +03:00
if [ -n " $( package_install_state $depend ) " ] ; then
continue
2019-09-24 09:56:39 +03:00
fi
2023-02-02 00:05:40 +03:00
# Split colon separated string into a list
IFS = ':' read -ra port_directories <<< " $SERENITY_PORT_DIRS "
for port_dir in " ${ port_directories [@] } " ; do
if [ -d " ${ port_dir } / $depend " ] ; then
( cd " ${ port_dir } / $depend " && ./package.sh --auto)
continue 2
fi
done
>& 2 echo " Error: Dependency $depend could not be found. "
exit 1
2019-09-24 09:56:39 +03:00
done
2019-05-28 05:02:42 +03:00
}
2019-09-24 09:56:39 +03:00
uninstall( ) {
2021-10-20 01:14:54 +03:00
if [ " $( package_install_state $port ) " != "manual" ] ; then
2019-09-24 09:56:39 +03:00
>& 2 echo " Error: $port is not installed. Cannot uninstall. "
2021-10-20 01:14:54 +03:00
return
elif [ ! -f plist ] ; then
>& 2 echo "Error: This port does not have a plist yet. Cannot uninstall."
return
2019-09-24 09:56:39 +03:00
fi
2021-10-20 01:14:54 +03:00
for f in ` cat plist` ; do
case $f in
*/)
run rmdir " ${ DESTDIR } / $f " || true
; ;
*)
run rm -rf " ${ DESTDIR } / $f "
; ;
esac
done
# Without || true, mv will not be executed if you are uninstalling your only remaining port.
2023-04-22 16:09:01 +03:00
grep -v " ^manual $port " " $installedpackagesdb " > installed.db.tmp || true
mv installed.db.tmp " $installedpackagesdb "
2019-09-24 09:56:39 +03:00
}
2021-01-21 11:31:31 +03:00
do_installdepends( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Installing dependencies of $port ... "
2019-09-24 09:56:39 +03:00
installdepends
2021-01-21 11:31:31 +03:00
}
do_fetch( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Fetching $port ... "
buildstep fetch
2019-09-24 09:56:39 +03:00
}
2020-02-04 03:07:27 +03:00
do_patch( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Patching $port ... "
buildstep pre_patch
buildstep patch_internal
2020-02-04 03:07:27 +03:00
}
2019-09-24 09:56:39 +03:00
do_configure( ) {
2021-05-20 22:04:40 +03:00
ensure_build
2019-09-24 09:56:39 +03:00
if [ " $useconfigure " = "true" ] ; then
2022-10-23 16:22:42 +03:00
buildstep_intro " Configuring $port ... "
2022-01-21 14:53:13 +03:00
if " $use_fresh_config_sub " ; then
2022-10-23 16:22:42 +03:00
buildstep ensure_new_config_sub
2022-01-21 14:53:13 +03:00
fi
2022-02-25 22:26:15 +03:00
if " $use_fresh_config_guess " ; then
2022-10-23 16:22:42 +03:00
buildstep ensure_new_config_guess
2022-02-25 22:26:15 +03:00
fi
2022-10-23 16:22:42 +03:00
buildstep pre_configure
buildstep configure
buildstep post_configure
2019-09-24 09:56:39 +03:00
else
2022-10-23 16:22:42 +03:00
buildstep configure echo "This port does not use a configure script. Skipping configure step."
2019-09-24 09:56:39 +03:00
fi
}
do_build( ) {
2021-05-20 22:04:40 +03:00
ensure_build
2022-10-23 16:22:42 +03:00
buildstep_intro " Building $port ... "
buildstep build
2019-09-24 09:56:39 +03:00
}
do_install( ) {
2021-05-20 22:04:40 +03:00
ensure_build
2022-10-23 16:22:42 +03:00
buildstep pre_install
buildstep_intro " Installing $port ... "
buildstep install
buildstep install_main_launcher
buildstep install_main_icon
buildstep post_install
2019-09-24 09:56:39 +03:00
addtodb " ${ 1 :- } "
}
do_clean( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Cleaning build directory for $port ... "
buildstep clean
2019-09-24 09:56:39 +03:00
}
do_clean_dist( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Cleaning dist files for $port ... "
buildstep clean_dist
2019-09-24 09:56:39 +03:00
}
do_clean_all( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Cleaning all for $port ... "
buildstep clean_all
2019-09-24 09:56:39 +03:00
}
do_uninstall( ) {
2022-10-23 16:22:42 +03:00
buildstep_intro " Uninstalling $port ... "
buildstep uninstall
2019-09-24 09:56:39 +03:00
}
2021-04-23 14:53:53 +03:00
do_showproperty( ) {
2022-01-17 12:49:23 +03:00
while [ $# -gt 0 ] ; do
if ! declare -p " ${ 1 } " > /dev/null 2>& 1; then
echo " Property ' $1 ' is not set. " >& 2
exit 1
fi
property_declaration = " $( declare -p " ${ 1 } " ) "
if [ [ " $property_declaration " = ~ "declare -a" ] ] ; then
prop_array = " ${ 1 } [@] "
# Some magic to avoid empty arrays being considered unset.
echo " ${ !prop_array+ " ${ !prop_array } " } "
else
echo ${ !1 }
fi
printf '\n'
shift
done
2021-04-21 23:53:02 +03:00
}
2019-09-24 09:56:39 +03:00
do_all( ) {
2021-01-21 11:31:31 +03:00
do_installdepends
2019-09-24 09:56:39 +03:00
do_fetch
2020-02-04 03:07:27 +03:00
do_patch
2019-09-24 09:56:39 +03:00
do_configure
do_build
do_install " ${ 1 :- } "
}
2019-05-28 01:02:29 +03:00
2021-07-08 01:43:44 +03:00
do_shell( ) {
do_installdepends
do_fetch
do_patch
cd " $workdir "
bash
echo "End of package shell. Back to the User shell."
}
2022-01-12 22:37:55 +03:00
do_generate_patch_readme( ) {
2022-07-07 19:54:55 +03:00
if [ ! -d " ${ PORT_META_DIR } /patches " ] ; then
2022-01-12 22:37:55 +03:00
>& 2 echo " Error: Port $port does not have any patches "
exit 1
fi
2022-07-07 19:54:55 +03:00
if [ -f " ${ PORT_META_DIR } /patches/ReadMe.md " ] ; then
2022-01-12 22:37:55 +03:00
read -N1 -rp \
"A ReadMe.md already exists, overwrite? (N/y) " should_overwrite
echo
if [ " ${ should_overwrite ,, } " != y ] ; then
>& 2 echo " Not overwriting Ports/ $port /patches/ReadMe.md "
exit 0
fi
fi
2022-09-17 01:32:05 +03:00
# An existing patches directory but no actual patches presumably means that we just deleted all patches,
# so remove the ReadMe file accordingly.
if [ -z " $( find -L " ${ PORT_META_DIR } /patches " -maxdepth 1 -name '*.patch' -print -quit) " ] ; then
>& 2 echo " Port $port does not have any patches, deleting ReadMe... "
rm -f " ${ PORT_META_DIR } /patches/ReadMe.md "
exit 0
fi
2022-04-14 11:30:23 +03:00
local tempdir = " $( pwd ) /.patches.tmp "
rm -fr " $tempdir "
mkdir " $tempdir "
2022-01-12 22:37:55 +03:00
2022-07-07 19:54:55 +03:00
pushd " ${ PORT_META_DIR } /patches "
2022-01-12 22:37:55 +03:00
2022-07-07 19:54:55 +03:00
echo " # Patches for $port on SerenityOS " > ReadMe.md
echo >> ReadMe.md
2022-01-12 22:37:55 +03:00
local count = 0
for patch in *.patch; do
git mailinfo \
" $tempdir / $patch .msg " \
/dev/null \
< " $patch " \
> " $tempdir / $patch .info " \
2> " $tempdir / $patch .error " \
|| {
rc = $?
>& 2 echo " Failed to extract patch info from $patch "
>& 2 echo " git returned $rc and said: "
>& 2 cat " $tempdir / $patch .error "
exit 1
}
(
grep 'Subject: ' " $tempdir / $patch .info " | sed -e 's/Subject: \(.*\)$/\1/'
echo
cat " $tempdir / $patch .msg "
) > " $tempdir / $patch .desc "
if [ ! -s " $tempdir / $patch .desc " ] ; then
>& 2 echo " WARNING: $patch does not contain a valid git patch or is missing a commit message, and is going to be skipped! "
continue
fi
{
echo " ## \` $patch \` "
echo
2022-05-21 21:44:27 +03:00
sed -e '/^Co-Authored-By: /d' < " $tempdir / $patch .desc "
2022-01-12 22:37:55 +03:00
echo
} >> ReadMe.md
count = $(( count + 1 ))
done
popd
>& 2 echo " Successfully generated entries for $count patch(es) in patches/ReadMe.md. "
}
launch_user_shell( ) {
env \
IN_SERENITY_PORT_DEV = " $port " \
" ${ SHELL :- bash } " || \
true
}
prompt_yes_no( ) {
read -N1 -rp \
" $1 (N/y) " result
2>& 1 echo
if [ " ${ result ,, } " = = y ] ; then
return 0
else
return 1
fi
}
2022-05-16 14:56:07 +03:00
prompt_yes_no_default_yes( ) {
read -N1 -rp \
" $1 (Y/n) " result
2>& 1 echo
if [ " ${ result ,, } " = = n ] ; then
return 1
else
return 0
fi
}
2022-01-12 22:37:55 +03:00
do_dev( ) {
if [ -n " ${ IN_SERENITY_PORT_DEV :- } " ] ; then
>& 2 echo " Error: Already in dev environment for $IN_SERENITY_PORT_DEV "
exit 1
fi
2023-05-09 02:17:52 +03:00
if [ " ${ 1 :- } " != "--no-depends" ] ; then
do_installdepends
fi
2023-09-02 10:18:50 +03:00
if [ -d " $workdir " ] && [ ! -e " $workdir /.git " ] ; then
2023-07-02 05:03:49 +03:00
if prompt_yes_no "- Would you like to clean the working directory (i.e. ./package.sh clean)?" ; then
2023-05-09 02:17:52 +03:00
do_clean
fi
fi
2023-04-19 05:46:42 +03:00
local force_patch_regeneration = 'false'
[ -d " $workdir " ] || {
2022-09-17 00:43:16 +03:00
do_fetch
pushd " $workdir "
2023-09-02 10:18:50 +03:00
if [ ! -e ".git" ] ; then
2022-01-12 22:37:55 +03:00
git init .
2022-06-16 19:56:08 +03:00
git config core.autocrlf false
2022-06-01 01:59:55 +03:00
git add --all --force
2022-01-12 22:37:55 +03:00
git commit -a -m 'Initial import'
2023-09-07 00:50:31 +03:00
git tag --no-sign source
2022-01-12 22:37:55 +03:00
fi
2022-09-17 00:43:16 +03:00
2022-09-16 21:01:30 +03:00
if [ -d " ${ PORT_META_DIR } /patches " ] && [ -n " $( find -L " ${ PORT_META_DIR } /patches " -maxdepth 1 -name '*.patch' -print -quit) " ] ; then
2022-07-07 19:54:55 +03:00
for patch in " ${ PORT_META_DIR } " /patches/*.patch; do
2023-09-02 10:34:46 +03:00
if ! git am --keep-cr --keep-non-patch --3way " $patch " ; then
# The patch didn't apply, oh no!
# `git am` already printed instructions, so drop into a shell for the user to follow them.
launch_user_shell
force_patch_regeneration = 'true'
2022-01-12 22:37:55 +03:00
2022-01-24 15:28:56 +03:00
if ! git diff --quiet >/dev/null 2>& 1; then
>& 2 echo "- It appears that there are uncommitted changes from applying the previous patch:"
for line in $( git diff --color= always) ; do
echo " | $line "
done
if prompt_yes_no "- Would you like to drop them before moving on to the next patch?" ; then
git clean -xf
else
>& 2 echo "- The uncommitted changes will be committed with the next patch or left in the tree."
fi
2022-01-12 22:37:55 +03:00
fi
2023-09-02 10:34:46 +03:00
fi
2022-01-12 22:37:55 +03:00
done
fi
2022-09-17 01:19:34 +03:00
2023-09-02 10:18:50 +03:00
git tag --no-sign -f patched
2022-09-17 01:19:34 +03:00
2022-01-12 22:37:55 +03:00
popd
2023-04-19 05:46:42 +03:00
}
2022-01-12 22:37:55 +03:00
2023-09-02 10:18:50 +03:00
[ -e " $workdir /.git " ] || {
2022-09-17 00:43:16 +03:00
>& 2 echo " $workdir does not appear to be a git repository. "
>& 2 echo "If you want to use './package.sh dev', please run './package.sh clean' first."
exit 1
2022-01-12 22:37:55 +03:00
}
2022-06-05 04:04:43 +03:00
pushd " $workdir "
2022-01-12 22:37:55 +03:00
launch_user_shell
2022-06-05 04:04:43 +03:00
popd >/dev/null 2>& 1
2022-01-12 22:37:55 +03:00
2023-09-07 00:46:11 +03:00
local original_hash = " $( git -C " $workdir " rev-parse refs/tags/patched) "
2022-09-17 00:43:16 +03:00
local current_hash = " $( git -C " $workdir " rev-parse HEAD) "
2022-01-12 22:37:55 +03:00
2022-09-17 01:21:13 +03:00
# If the hashes are the same, we have no changes, otherwise generate patches
2023-04-19 05:46:42 +03:00
if [ " $original_hash " != " $current_hash " ] || [ " ${ force_patch_regeneration } " = "true" ] ; then
2022-09-17 01:21:13 +03:00
>& 2 echo " Note: Regenerating patches as there are changed commits in the port repo (started at $original_hash , now is $current_hash ) "
2022-07-07 19:54:55 +03:00
rm -fr " ${ PORT_META_DIR } " /patches/*.patch
2023-09-07 00:46:11 +03:00
git -C " $workdir " format-patch --no-numbered --zero-commit --no-signature --full-index refs/tags/source -o " $( realpath " ${ PORT_META_DIR } /patches " ) "
2022-01-12 22:37:55 +03:00
do_generate_patch_readme
fi
}
2021-04-11 02:01:20 +03:00
NO_GPG = false
parse_arguments( ) {
if [ -z " ${ 1 :- } " ] ; then
do_all
2021-10-20 01:14:40 +03:00
return
2021-04-11 02:01:20 +03:00
fi
2021-10-20 01:14:40 +03:00
case " $1 " in
2023-05-09 02:17:52 +03:00
build| clean| clean_all| clean_dist| configure| dev| fetch| generate_patch_readme| install| installdepends| patch| shell| showproperty| uninstall)
2021-10-20 01:14:40 +03:00
method = $1
shift
do_${ method } " $@ "
; ;
--auto)
do_all $1
; ;
--no-gpg-verification)
NO_GPG = true
shift
parse_arguments $@
; ;
2022-01-09 04:16:31 +03:00
interactive)
export PS1 = " (serenity):\w $ "
bash --norc
; ;
2021-10-20 01:14:40 +03:00
*)
2023-05-09 02:13:41 +03:00
>& 2 echo " I don't understand $1 ! Supported arguments: build, clean, clean_all, clean_dist, configure, dev, fetch, generate_patch_readme, install, installdepends, interactive, patch, shell, showproperty, uninstall. "
2021-10-20 01:14:40 +03:00
exit 1
; ;
esac
2021-04-11 02:01:20 +03:00
}
parse_arguments $@