OverviewFilesCommitsIssuesDocsReleasesPeers

hyperswarm-peer / swarmd / build.sh

136 lines · bash ref:
#!/bin/bash
# swarmd — pure C/C++. No npm, no Node. The generated wire in schema/ is
# committed; regenerate it only with `npm run build:schema` when the wire
# changes (see README.md).
set -e
cd "$(dirname "$0")"

KB=/storage/projects/code/hypercore-kb/holepunchto/repos
PEER=..

# ⛔ A PLAIN BUILD DOES NOT LOAD ON EVERY FLEET MACHINE, and there was no way to
# ask for one that does. Measured: a g++ build here needs GLIBC_2.38; ashatron
# has 2.35. ../tools/zig-cxx targets glibc 2.28, which is the documented answer
# in daemon/build.sh and had no equivalent here.
#
#   CC=../tools/zig-cc CXX=../tools/zig-cxx OBJ=/tmp/swarmd-obj-zig ./build.sh
#
# ⚠ USE A SEPARATE OBJ DIR. Mixing toolchains in one cache links objects built
# against different libcs, which is the half-updated-cache failure wearing a new
# hat.
CC=${CC:-gcc}
CXX=${CXX:-g++}
INC="-Isrc -Ischema -Iclient -I$PEER/src -I$PEER/liboverlay/include -I$PEER/thirdparty/libudx/include -I$KB/tier5/libcompact/include -I$KB/tier4/libutf/include -I$KB/tier4/librpc/include"
OBJ=${OBJ:-/tmp/swarmd-obj}
mkdir -p "$OBJ"

# The holepunch C libraries. Sources compiled in directly rather than via CMake:
# swarmd is one small daemon, and a cmake-fetch tree for three static libs costs
# more than it explains.
echo "Building libcompact / libutf / librpc..."
CLIBS=""
for c in "$KB"/tier5/libcompact/src/*.c; do
  o="$OBJ/compact_$(basename "$c" .c).o"
  "$CC" -std=c99 -O2 -I"$KB/tier5/libcompact/include" -I"$KB/tier4/libutf/include" -c -o "$o" "$c" 2>/dev/null
  CLIBS="$CLIBS $o"
done
for c in "$KB"/tier4/libutf/src/*.c "$KB"/tier4/libutf/src/*/*.c; do
  [ -f "$c" ] || continue
  o="$OBJ/utf_$(echo "$c" | md5sum | cut -c1-8).o"
  "$CC" -std=c99 -O2 -I"$KB/tier4/libutf/include" -c -o "$o" "$c" 2>/dev/null || continue
  CLIBS="$CLIBS $o"
done
for c in "$KB"/tier4/librpc/src/*.c; do
  o="$OBJ/rpc_$(basename "$c" .c).o"
  "$CC" -std=c99 -O2 -I"$KB/tier4/librpc/include" -I"$KB/tier5/libcompact/include" -I"$KB/tier4/libutf/include" -c -o "$o" "$c" 2>/dev/null || continue
  CLIBS="$CLIBS $o"
done

echo "Building generated wire..."
"$CC" -std=c99 -O2 $INC -c -o "$OBJ/swarmd_schema.o" schema/swarmd_schema.c
"$CC" -std=c99 -O2 $INC -c -o "$OBJ/swarmd_hrpc.o"   schema/swarmd_hrpc.c

echo "Building the client library..."
"$CXX" -std=c++17 -O2 $INC -c -o "$OBJ/client.o" client/client.cpp

echo "Building daemon sources..."
"$CXX" -std=c++17 -O2 $INC -c -o "$OBJ/uds_server.o" src/uds_server.cpp
"$CXX" -std=c++17 -O2 $INC -c -o "$OBJ/control.o"    src/control.cpp
"$CXX" -std=c++17 -O2 $INC -c -o "$OBJ/content.o"    src/content.cpp

# The transport. Reuses hyperswarm-peer's object cache when test/build_tests.sh
# has populated it; otherwise builds what swarmd needs.
echo "Building transport..."
TOBJ=${TOBJ:-/tmp/hsp-obj}
PEERSRC="noise_ik noise_handshake noise_stream dht_rpc holepuncher hyperdht dht_connector udx_transport swarm_manager protomux"
TLIBS=""
# ⛔ AGAINST THE NEWEST HEADER, NOT THE .cpp. This compared each object with its
# own source only, so a change to a shared HEADER — a class layout, or
# compact_encoding.h growing a new dependency — left every object here stale and
# silently reusable. That is the half-updated cache ../daemon/build.sh already
# refuses to link, and it was reachable from this script.
PEERNEW=$(ls -t "$PEER"/src/*.h "$PEER"/src/*.cpp | head -1)
for n in $PEERSRC; do
  o="$OBJ/peer_$n.o"
  if [ ! -f "$o" ] || [ "$PEERNEW" -nt "$o" ]; then
    "$CXX" -std=c++17 -O2 -D_GNU_SOURCE -I$PEER/src -I$PEER/thirdparty/libudx/include \
        -I$KB/tier5/libcompact/include -I$KB/tier4/libutf/include \
        -c -o "$o" "$PEER/src/$n.cpp"
  fi
  TLIBS="$TLIBS $o"
done
for c in "$PEER"/thirdparty/libudx/src/*.c; do
  b=$(basename "$c" .c)
  # NOT `[ ... ] && continue`: that returns 1 for every OTHER file and set -e kills the script.
  # Platform backends libudx picks at build time. io_uring needs headers we
  # do not require; io_win does not compile off Windows.
  case "$b" in io_uring|io_win) continue;; esac
  o="$OBJ/udx_$b.o"
  [ -f "$o" ] || gcc -O2 -D_GNU_SOURCE -I"$PEER/thirdparty/libudx/include" -c -o "$o" "$c" 2>/dev/null
  TLIBS="$TLIBS $o"
done

echo "Building daemon..."
"$CXX" -std=c++17 -O2 $INC -c -o "$OBJ/daemon.o" src/daemon.cpp
"$CXX" -std=c++17 -O2 $INC -o swarmd src/main.cpp \
    "$OBJ/daemon.o" "$OBJ/uds_server.o" "$OBJ/control.o" "$OBJ/content.o" \
    "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS $TLIBS \
    -lsodium -luv -lpthread

echo "Building gates..."
"$CXX" -std=c++17 -O2 $INC -o test/test_uds_backpressure \
    test/test_uds_backpressure.cpp "$OBJ/uds_server.o"

"$CXX" -std=c++17 -O2 $INC -o test/test_control \
    test/test_control.cpp "$OBJ/uds_server.o" "$OBJ/control.o" \
    "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS

# Drives the REAL Daemon (transport and all), so it links what swarmd links.
"$CXX" -std=c++17 -O2 $INC -o test/test_send_truth \
    test/test_send_truth.cpp "$OBJ/daemon.o" "$OBJ/uds_server.o" "$OBJ/control.o" \
    "$OBJ/content.o" \
    "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS $TLIBS \
    -lsodium -luv -lpthread

"$CXX" -std=c++17 -O2 $INC -o test/test_client \
    test/test_client.cpp "$OBJ/client.o" "$OBJ/daemon.o" "$OBJ/uds_server.o" \
    "$OBJ/control.o" "$OBJ/content.o" \
    "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS $TLIBS \
    -lsodium -luv -lpthread

echo "Building tools..."
"$CXX" -std=c++17 -O2 $INC -o tools/swarmd-probe tools/swarmd_probe.cpp \
    "$OBJ/client.o" "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS

"$CXX" -std=c++17 -O2 $INC -o tools/swarmd-ctl tools/swarmd_ctl.cpp \
    "$OBJ/client.o" "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS -lsodium

# swarmd-session: session-daemon's socket, swarmd's insides. Links the daemon
# too, because it runs one.
"$CXX" -std=c++17 -O2 $INC -o tools/swarmd-session tools/session_shim.cpp \
    "$OBJ/client.o" "$OBJ/daemon.o" "$OBJ/uds_server.o" "$OBJ/control.o" \
    "$OBJ/content.o" "$OBJ/swarmd_schema.o" "$OBJ/swarmd_hrpc.o" $CLIBS $TLIBS \
    -lsodium -luv -lpthread

echo "  OK"