#!/usr/bin/env bash set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" base=$PWD source_dir=${MARS_SOURCE_DIR:-} prefix=${PREFIX:-/usr/local} [[ $prefix = /usr/local ]] || { echo 'PREFIX must be /usr/local for native service' >&2; exit 2; } rm -f upstream/build-complete for tool in cc make cmake git pkg-config python3 node; do command -v "$tool" >/dev/null || { echo "Missing build tool: $tool. See GUIDE.md." >&2; exit 1; } done pkg-config --exists json-c openssl "sqlite3 >= 3.31" || { echo 'Install libjson-c-dev, libssl-dev and libsqlite3-dev.' >&2; exit 1; } printf '#include \n' | cc -E -x c - >/dev/null || { echo 'Install libcivetweb-dev.' >&2; exit 1; } for adapter in mars_admin.inc mars_acl.inc mars_scripts.inc mars_trustee.inc; do test -s "upstream-adapter/$adapter" || { echo "Missing adapter: $adapter" >&2; exit 1; } done make check-release native mkdir -p upstream if [[ ! -d upstream/original ]]; then if [[ -n $source_dir ]]; then cp -a -- "$source_dir" upstream/original else git clone --depth 1 --branch main https://github.com/davidrg/mars_nwe.git upstream/original fi fi if [[ -d upstream/original/.git ]]; then git -C upstream/original rev-parse HEAD > upstream/source-revision.txt; else echo 'Locally supplied source tree' > upstream/source-revision.txt; fi [[ ! -e upstream/patched ]] || { echo 'Remove upstream/patched and upstream/build to rebuild from original (or run ./rebuild.sh).' >&2; exit 2; } cp -a upstream/original upstream/patched ./patch-upstream.sh upstream/patched cmake_args=() if cmake --version | head -1 | grep -q 'version 4\.'; then cmake_args+=(-DCMAKE_POLICY_VERSION_MINIMUM=3.5); fi # Old C code needs legacy inline semantics, tentative definitions and GCC compatibility. cmake -S upstream/patched -B upstream/build "${cmake_args[@]}" \ -DCMAKE_INSTALL_PREFIX="$prefix" -DCMAKE_INSTALL_SBINDIR=sbin \ -DCMAKE_INSTALL_LOCALSTATEDIR=/var -DENABLE_INTERNAL_RIP_SAP=ON \ -DCMAKE_C_FLAGS="-std=gnu99 -fcommon -fgnu89-inline -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -Wno-error=implicit-function-declaration" \ -DMARS_PCAP_LIBRARY="$base/build/libmarspcap.a" cmake --build upstream/build --parallel "${JOBS:-$(getconf _NPROCESSORS_ONLN)}" printf '%s\n' "$prefix" > upstream/install-prefix.txt printf '%s\n' "$(cat upstream/source-revision.txt)" > upstream/build-complete printf '\nBuild finished. Run sudo ./install.sh (review configuration before starting).\n'