joeylib2/installer/lib/config.sh

66 lines
2.3 KiB
Bash

# shellcheck shell=bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# Generates toolchains/env.sh (for shells and scripts) and
# toolchains/config.mk (for make) from the compEnv output of every installed
# component, so both see exactly the same tool paths. Sourced after
# lib/common.sh.
# writeConfig -- regenerate both files from the installed components.
writeConfig() {
local id
local kind
local name
local value
local vars=()
local paths=()
local envTmp
local mkTmp
case "$JOEY_REPO" in
*" "*) die "the repository path contains a space ($JOEY_REPO); make cannot build from there" ;;
esac
for id in $(componentIds); do
[ -n "$(stampRead "$id")" ] || continue
while IFS=$'\t' read -r kind name value; do
case "$kind" in
var) vars+=("$name=$value") ;;
path) paths+=("$name") ;;
esac
done < <(compRun "$id" compEnv)
done
mkdir -p "$JOEY_TOOLCHAINS"
envTmp="$(mktemp "$JOEY_TOOLCHAINS/env.sh.XXXXXX")"
mkTmp="$(mktemp "$JOEY_TOOLCHAINS/config.mk.XXXXXX")"
{
printf '# Generated by install.sh -- do not edit. For a shell:\n'
printf '# source toolchains/env.sh\n'
printf "export JOEY_TOOLCHAINS='%s'\n" "$JOEY_TOOLCHAINS"
for value in "${vars[@]}"; do
printf "export %s='%s'\n" "${value%%=*}" "${value#*=}"
done
# Prepend each directory once, even when this file is sourced again.
for value in "${paths[@]}"; do
printf 'case ":$PATH:" in *":%s:"*) ;; *) PATH='"'"'%s'"'"':"$PATH" ;; esac\n' "$value" "$value"
done
printf 'export PATH\n'
} > "$envTmp"
{
printf '# Generated by install.sh -- do not edit. make reads it by itself.\n'
printf 'export JOEY_TOOLCHAINS := %s\n' "$JOEY_TOOLCHAINS"
for value in "${vars[@]}"; do
printf 'export %s := %s\n' "${value%%=*}" "${value#*=}"
done
if [ ${#paths[@]} -gt 0 ]; then
printf 'export PATH := %s:$(PATH)\n' "$(IFS=:; printf '%s' "${paths[*]}")"
fi
} > "$mkTmp"
chmod 644 "$envTmp" "$mkTmp"
mv "$envTmp" "$JOEY_TOOLCHAINS/env.sh"
mv "$mkTmp" "$JOEY_TOOLCHAINS/config.mk"
}