102 lines
3.2 KiB
Bash
Executable File
102 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc}
|
|
cxx=${CXX:-g++}
|
|
if [[ -z ${CXX:-} ]] && command -v ccache >/dev/null 2>&1; then
|
|
cxx="ccache $cxx"
|
|
fi
|
|
build_jobs=${CNC_SIM_BUILD_JOBS:-8}
|
|
if ! [[ "$build_jobs" =~ ^[1-9][0-9]*$ ]]; then
|
|
echo "CNC_SIM_BUILD_JOBS must be a positive integer: $build_jobs" >&2
|
|
exit 1
|
|
fi
|
|
cache_dir=${CNC_SIM_NATIVE_BRIDGE_OBJECT_CACHE_DIR-build/native-bridge-object-cache}
|
|
if [[ -z "$cache_dir" ]]; then
|
|
echo "CNC_SIM_NATIVE_BRIDGE_OBJECT_CACHE_DIR must not be empty" >&2
|
|
exit 1
|
|
fi
|
|
if [[ "$cache_dir" =~ [[:space:]] ]]; then
|
|
echo "CNC_SIM_NATIVE_BRIDGE_OBJECT_CACHE_DIR must not contain whitespace: $cache_dir" >&2
|
|
exit 1
|
|
fi
|
|
mkdir -p "$cache_dir"
|
|
cache_dir=$(cd "$cache_dir" && pwd)
|
|
if [[ "$cache_dir" == "/" ]]; then
|
|
echo "CNC_SIM_NATIVE_BRIDGE_OBJECT_CACHE_DIR must not be the filesystem root" >&2
|
|
exit 1
|
|
fi
|
|
exec 8>"$cache_dir/native-bridge-objects.lock"
|
|
flock 8
|
|
|
|
preflight_cache_dir=${CNC_SIM_PREFLIGHT_CACHE_DIR:-"$cache_dir/preflight-cache"}
|
|
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-inputs-cached.sh --cache-dir "$preflight_cache_dir" --root-only
|
|
linuxcnc_root=$(cd "$linuxcnc_root" && pwd)
|
|
|
|
source_output=$(./list-linuxcnc-bridge-smoke-project-sources.sh)
|
|
mapfile -t sources <<<"$source_output"
|
|
common_flag_output=$(
|
|
CNC_SIM_PREFLIGHT_CACHE_DIR="$preflight_cache_dir" \
|
|
LINUXCNC_ROOT="$linuxcnc_root" ./list-linuxcnc-source-common-cxxflags.sh \
|
|
--no-ulapi \
|
|
--core-include \
|
|
--core-src
|
|
)
|
|
mapfile -t common_flags <<<"$common_flag_output"
|
|
|
|
objects=()
|
|
source_index=0
|
|
for source in "${sources[@]}"; do
|
|
objects+=("$cache_dir/source_${source_index}.o")
|
|
source_index=$((source_index + 1))
|
|
done
|
|
|
|
signature="$cache_dir/native-bridge-objects.signature"
|
|
next_signature="$cache_dir/native-bridge-objects.signature.next"
|
|
{
|
|
printf 'CXX=%s\n' "$cxx"
|
|
printf 'PWD=%s\n' "$PWD"
|
|
printf 'LINUXCNC_ROOT=%s\n' "$linuxcnc_root"
|
|
printf 'BUILD_RULE_VERSION=1\n'
|
|
printf 'COMMON_FLAGS='
|
|
printf ' %s' "${common_flags[@]}"
|
|
printf '\n'
|
|
printf 'SOURCES:\n'
|
|
printf '%s\n' "${sources[@]}"
|
|
} > "$next_signature"
|
|
if [[ ! -f "$signature" ]] || ! cmp -s "$signature" "$next_signature"; then
|
|
rm -f "$cache_dir"/source_*.o "$cache_dir"/source_*.d
|
|
fi
|
|
mv "$next_signature" "$signature"
|
|
|
|
makefile="$cache_dir/native-bridge-objects.mk"
|
|
{
|
|
printf 'CXX := %s\n' "$cxx"
|
|
printf 'COMMON_FLAGS :='
|
|
printf ' %s' "${common_flags[@]}"
|
|
printf '\n\n'
|
|
printf 'OBJECTS :='
|
|
printf ' %s' "${objects[@]}"
|
|
printf '\n\n'
|
|
printf 'DEP_FILES :=\n'
|
|
for object in "${objects[@]}"; do
|
|
printf 'DEP_FILES += %s.d\n' "$object"
|
|
done
|
|
printf '\n.PHONY: all\n'
|
|
printf 'all: $(OBJECTS)\n\n'
|
|
|
|
source_index=0
|
|
for source in "${sources[@]}"; do
|
|
printf '%s: %s\n' "${objects[$source_index]}" "$source"
|
|
printf '\t@$(CXX) $(COMMON_FLAGS) -MMD -MP -MF %s.d -c %s -o %s\n\n' \
|
|
"${objects[$source_index]}" "$source" "${objects[$source_index]}"
|
|
source_index=$((source_index + 1))
|
|
done
|
|
printf '\n-include $(DEP_FILES)\n'
|
|
} > "$makefile"
|
|
make --output-sync=target -j"$build_jobs" -f "$makefile" >/dev/null
|
|
|
|
printf '%s\n' "${objects[@]}"
|