Hash :
7ce5e573
Author :
Thomas de Grivel
Date :
2023-01-22T12:47:15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
#!/bin/sh
# Copyright 2023 Thomas de Grivel <thodg@kmx.io>
set -e
# Config
SHPKG_DIR="${SHPKG_DIR:-${HOME}/shpkg}"
if [ "x$CC" = "x" ]; then
if which cc; then
CC=cc
elif which gcc; then
CC=gcc
elif which egcc; then
CC=egcc
else
echo "C compiler not found." >&2
exit 1
fi
fi
SHPKG_PREFIX="$SHPKG_DIR"
SHPKG_TARGET="$(${CC} -v 2>&1 | grep '^Target: ' | head -n 1 | cut -c 9-)"
SHPKG_TARGET_DIR="${SHPKG_DIR}/target/${SHPKG_TARGET}"
SHPKG_FAKE_DIR="${SHPKG_TARGET_DIR}/fake"
SHPKG_INCLUDE_DIR="${SHPKG_DIR}/include"
SHPKG_LIB_DIR="${SHPKG_DIR}/lib"
SHPKG_OBJ_DIR="${SHPKG_TARGET_DIR}/obj"
SHPKG_PACKAGE_DIR="${SHPKG_TARGET_DIR}/packages"
SHPKG_SRC_DIR="${SHPKG_DIR}/src"
SHPKG_TAG_DIR="${SHPKG_TARGET_DIR}/tag"
SHPKG_VAR_DB_DIR="${SHPKG_DIR}/var/db/shpkg"
export CPPFLAGS="$CPPFLAGS -I${SHPKG_INCLUDE_DIR}"
export LDFLAGS="$LDFLAGS -L${SHPKG_LIB_DIR}"
export PKG_CONFIG_PATH="${SHPKG_LIB_DIR}/pkgconfig"
verbose () {
echo "$@"
"$@"
}
# Setup
if ! [ -d "$SHPKG_FAKE_DIR" ]; then
verbose mkdir -p "$SHPKG_FAKE_DIR"
fi
if ! [ -d "$SHPKG_PACKAGE_DIR" ]; then
verbose mkdir -p "$SHPKG_PACKAGE_DIR"
fi
if ! [ -d "$SHPKG_SRC_DIR" ]; then
verbose mkdir -p "$SHPKG_SRC_DIR"
fi
if ! [ -d "$SHPKG_VAR_DB_DIR" ]; then
verbose mkdir -p "$SHPKG_VAR_DB_DIR"
fi
# Source repository
SHPKG_REPO_INDEX="${SHPKG_DIR}/repo_index"
if ! [ -f "$SHPKG_REPO_INDEX" ]; then
echo "$SHPKG_REPO_INDEX: not found" >&2
exit 1
fi
shpkg_clone () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_PARENT_DIR="$(shpkg_src_parent_dir "$REPO")"
SHPKG_REPO_GIT_URL="$(shpkg_git_url "$REPO")"
SHPKG_REPO_BASENAME="$(basename "$SHPKG_REPO_SRC_DIR")"
if ! [ -d "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" ]; then
verbose mkdir -p "${SHPKG_SRC_DIR}/$SHPKG_REPO_PARENT_DIR"
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_PARENT_DIR" &&
verbose git clone "$SHPKG_REPO_GIT_URL" \
"$SHPKG_REPO_BASENAME" &&
cd "$SHPKG_REPO_BASENAME" &&
verbose git submodule init &&
verbose git submodule update
)
fi
done
}
shpkg_depends_on () {
shpkg_repo_info "$@" | cut -d ' ' -f 4 | tr ',' ' '
}
shpkg_fetch () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose git fetch
)
done
}
shpkg_repo_info () {
[ "x$#" = "x1" ] || return 1
grep "^$1 " "${SHPKG_REPO_INDEX}"
}
shpkg_git_url () {
shpkg_repo_info "$@" | cut -d ' ' -f 3
}
shpkg_pull () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose git pull; )
done
}
shpkg_remove () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_PARENT_DIR="$(shpkg_src_parent_dir "$REPO")"
verbose rm -rf "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR"
rmdir "${SHPKG_SRC_DIR}/$SHPKG_REPO_PARENT_DIR" 2>/dev/null || true
done
}
shpkg_src_dir () {
shpkg_repo_info "$@" | cut -d ' ' -f 2
}
shpkg_src_parent_dir () {
dirname "$(shpkg_src_dir "$REPO")"
}
shpkg_version () {
SHPKG_REPO_HASH="$(git show-ref --head HEAD | cut -d ' ' -f 1 | head -n 1)"
SHPKG_REPO_HEAD="$(git show-ref | grep "$SHPKG_REPO_HASH" | grep ' refs/heads/' | sed -e 's,^.* refs/heads/,,')"
echo "${SHPKG_REPO_HEAD}-${SHPKG_REPO_HASH}"
}
# Building from sources
shpkg_autogen () {
verbose shpkg_clone "$@"
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
if [ -x "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/autogen" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose ./autogen; )
elif [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/autogen.sh" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose sh autogen.sh; )
elif [ -x "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/bootstrap" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose ./bootstrap; )
fi
done
}
shpkg_build () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_VERSION="$(shpkg_version "$REPO")"
SHPKG_REPO_TAG_DIR="${SHPKG_TAG_DIR}/${SHPKG_REPO_SRC_DIR}/${SHPKG_REPO_VERSION}"
if ! [ -f "$SHPKG_REPO_TAG_DIR/build-done" ]; then
( verbose shpkg_configure "$REPO"; )
if [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/Makefile" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose make; )
elif [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/CMakeLists.txt" ]; then
cmake --build "${SHPKG_OBJ_DIR}/${SHPKG_REPO_SRC_DIR}"
fi
if ! [ -d "$SHPKG_REPO_TAG_DIR" ]; then
mkdir -p "$SHPKG_REPO_TAG_DIR"
fi
DATE="$(date +%F\ %T)"
echo "echo $DATE > $SHPKG_REPO_TAG_DIR/build-done"
echo "$DATE" > "$SHPKG_REPO_TAG_DIR/build-done"
fi
done
}
shpkg_configure () {
verbose shpkg_autogen "$@"
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_FAKE_DIR="$(shpkg_fake_dir "$REPO")"
if ! [ -d "$SHPKG_REPO_FAKE_DIR" ]; then
verbose mkdir -p "$SHPKG_REPO_FAKE_DIR"
fi
if [ -x "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/configure" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose ./configure --prefix "$SHPKG_PREFIX"; )
elif [ -x "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/configure.sh" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose ./configure.sh --prefix "$SHPKG_PREFIX"; )
elif [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/CMakeLists.txt" ]; then
cmake -S "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}" -B "${SHPKG_OBJ_DIR}/${SHPKG_REPO_SRC_DIR}" -D CMAKE_INSTALL_PREFIX:PATH="$SHPKG_PREFIX" -D BUILD_SHARED_LIBS=ON
fi
done
}
shpkg_fake () {
shpkg_build "$@"
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_FAKE_DIR="$(shpkg_fake_dir "$REPO")"
if [ -d "$SHPKG_REPO_FAKE_DIR" ]; then
verbose rm -rf "$SHPKG_REPO_FAKE_DIR"
fi
verbose mkdir -p "$SHPKG_REPO_FAKE_DIR"
if [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/Makefile" ]; then
( verbose cd "${SHPKG_SRC_DIR}/$SHPKG_REPO_SRC_DIR" &&
verbose make PREFIX="$SHPKG_REPO_FAKE_DIR" install; )
elif [ -f "${SHPKG_SRC_DIR}/${SHPKG_REPO_SRC_DIR}/CMakeLists.txt" ]; then
cmake --install "${SHPKG_OBJ_DIR}/${SHPKG_REPO_SRC_DIR}" --prefix "${SHPKG_REPO_FAKE_DIR}"
fi
done
}
shpkg_fake_dir () {
echo "${SHPKG_FAKE_DIR}/$(shpkg_src_dir "$REPO")"
}
shpkg_package () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_VERSION="$(shpkg_version "$REPO")"
SHPKG_REPO_PACKAGE="${SHPKG_PACKAGE_DIR}/${SHPKG_REPO_SRC_DIR}-${SHPKG_REPO_VERSION}.tgz"
if ! [ -f "$SHPKG_REPO_PACKAGE" ]; then
SHPKG_REPO_PACKAGE_DIR="$(dirname "$SHPKG_REPO_PACKAGE")"
SHPKG_REPO_FAKE_DIR="$(shpkg_fake_dir "$REPO")"
shpkg_fake "$REPO"
if ! [ -d "$SHPKG_REPO_PACKAGE_DIR" ]; then
verbose mkdir -p "$SHPKG_REPO_PACKAGE_DIR"
fi
if ! [ -d "$SHPKG_REPO_FAKE_DIR" ]; then
verbose mkdir -p "$SHPKG_REPO_FAKE_DIR"
fi
( verbose cd "$SHPKG_REPO_FAKE_DIR" &&
echo "pax -wz . > $SHPKG_REPO_PACKAGE" &&
pax -wz . > "$SHPKG_REPO_PACKAGE" )
fi
done
}
# Packages
shpkg_install () {
for REPO; do
SHPKG_REPO_SRC_DIR="$(shpkg_src_dir "$REPO")"
SHPKG_REPO_VERSION="$(shpkg_version "$REPO")"
SHPKG_REPO_PACKAGE="${SHPKG_PACKAGE_DIR}/${SHPKG_REPO_SRC_DIR}-${SHPKG_REPO_VERSION}.tgz"
SHPKG_REPO_INSTALLED="${SHPKG_VAR_DB_DIR}/installed/${SHPKG_REPO_SRC_DIR}"
SHPKG_REPO_INSTALLED_DIR="$(dirname "$SHPKG_REPO_INSTALLED")"
if ! [ -f "$SHPKG_REPO_INSTALLED" ] ||
! head -n 1 < "$SHPKG_REPO_INSTALLED" |
grep "Version: ${SHPKG_REPO_VERSION}" >/dev/null; then
( verbose shpkg_install_dependencies "$REPO"; )
( verbose shpkg_build "$REPO"; )
( verbose shpkg_package "$REPO"; )
( verbose cd "$SHPKG_DIR" &&
echo "pax -rdz < $SHPKG_REPO_PACKAGE" &&
pax -rdz < "$SHPKG_REPO_PACKAGE"; )
if ! [ -d "$SHPKG_REPO_INSTALLED_DIR" ]; then
verbose mkdir -p "$SHPKG_REPO_INSTALLED_DIR"
fi
echo "echo Version: ${SHPKG_REPO_VERSION} > $SHPKG_REPO_INSTALLED"
echo "Version: ${SHPKG_REPO_VERSION}" > "$SHPKG_REPO_INSTALLED"
fi
done
}
shpkg_install_dependencies () {
for REPO; do
SHPKG_REPO_DEPENDS_ON="$(shpkg_depends_on "$REPO")"
if [ "x$SHPKG_REPO_DEPENDS_ON" != "x" ]; then
( verbose shpkg_install $SHPKG_REPO_DEPENDS_ON; )
fi
done
}
shpkg_uninstall () {
for REPO; do
:
done
}
shpkg_upgrade () {
verbose shpkg_pull "$@"
verbose shpkg_build "$@"
verbose shpkg_package "$@"
verbose shpkg_install "$@"
}