| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #!/usr/bin/env bash
- # Build vectorapi natively on the dev host, with clang, including the runner.
- #
- # This exists because the host CAN build everything, contrary to a note this
- # repo carried for a long time. The blocker was never protobuf 35: it was a
- # stale protoc earlier in PATH. An old protoc emits generated code that
- # includes google/protobuf/map_field_inl.h, a header protobuf 35 removed, and
- # compiling that against the installed headers is what failed. A protoc
- # matching libprotobuf fixes it, and then the database client and all of
- # vectorapi build and link natively.
- #
- # What this buys: a ~100 s full build instead of a container round trip, clang's
- # diagnostics, a real compile_commands.json for clangd, and the ability to
- # compile runner/src/main.cpp, which previously only the container could.
- #
- # scripts/host-build.sh # configure + build
- # scripts/host-build.sh --test # + run the suite (serially; see below)
- # scripts/host-build.sh --client # also rebuild the database client first
- #
- # Release packages still come from packaging/build.sh and the Debian image.
- # This build is for development only: it links the host's protobuf/gRPC, not
- # the versions the .deb is built against.
- set -euo pipefail
- REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
- PREFIX="${SVAPI_HOST_PREFIX:-/data/dev/smartbotics/.hostprefix}"
- DB_SRC="${SVAPI_DB_SRC:-/data/dev/smartbotics/smartbotic-database}"
- BUILD="$REPO/build-host"
- WITH_CLIENT=0
- RUN_TESTS=0
- for a in "$@"; do
- case "$a" in
- --client) WITH_CLIENT=1 ;;
- --test) RUN_TESTS=1 ;;
- *) echo "unknown argument: $a" >&2; exit 2 ;;
- esac
- done
- # The protoc must match the installed libprotobuf. Comparing the two is the
- # whole point: a mismatch is the failure this script exists to prevent, and it
- # shows up as a missing header deep in generated code rather than as a version
- # complaint, so check it up front and say so plainly.
- lib_ver="$(pkg-config --modversion protobuf)"
- lib_major="${lib_ver%%.*}"
- find_protoc() {
- for p in "${SVAPI_PROTOC:-}" "$PREFIX/bin/protoc" /usr/bin/protoc "$(command -v protoc || true)"; do
- [[ -n "$p" && -x "$p" ]] || continue
- if [[ "$("$p" --version | awk '{print $2}' | cut -d. -f1)" == "$lib_major" ]]; then
- echo "$p"; return 0
- fi
- done
- return 1
- }
- if ! PROTOC="$(find_protoc)"; then
- cat >&2 <<EOF
- No protoc matching libprotobuf $lib_ver found.
- installed libprotobuf : $lib_ver (so major $lib_major)
- protoc in PATH : $(command -v protoc >/dev/null && protoc --version || echo "none")
- Get a matching one, then re-run:
- - Void: sudo xbps-install protobuf-$lib_major.<minor>
- ('protobuf-devel' does NOT ship protoc; the compiler is its own package)
- - or: download protoc-$lib_major.<minor>-linux-x86_64.zip from
- https://github.com/protocolbuffers/protobuf/releases and point
- SVAPI_PROTOC at its bin/protoc
- Note a stale protoc earlier in PATH (commonly ~/.local/bin/protoc) will be
- found before the system one. This script ignores PATH order and picks by
- version, but the database's own CMake does not.
- EOF
- exit 1
- fi
- echo "protoc: $PROTOC ($("$PROTOC" --version)) against libprotobuf $lib_ver"
- if (( WITH_CLIENT )); then
- [[ -d "$DB_SRC" ]] || { echo "database source not at $DB_SRC" >&2; exit 1; }
- echo "=== building libsmartbotic-db-client from $DB_SRC into $PREFIX ==="
- cmake -B /tmp/svapi-dbclient -G Ninja -S "$DB_SRC" \
- -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PREFIX" \
- -DPROTOC="$PROTOC" \
- -DSMARTBOTIC_DB_BUILD_CLIENT=ON -DSMARTBOTIC_DB_BUILD_SERVICE=OFF \
- -DSMARTBOTIC_DB_BUILD_CLI=OFF -DSMARTBOTIC_DB_BUILD_TESTS=OFF >/dev/null
- cmake --build /tmp/svapi-dbclient -j"$(nproc)"
- cmake --install /tmp/svapi-dbclient >/dev/null
- # `cmake --install` ships the library and headers but no CMake package, so
- # find_package(smartbotic-db-client) would fail: only the .deb carries a
- # config, written by the database's create-debs.sh. Write a relocatable one.
- libdir="$(cd "$PREFIX" && ls -d lib64 lib 2>/dev/null | head -1)"
- mkdir -p "$PREFIX/$libdir/cmake/smartbotic-db-client"
- cat > "$PREFIX/$libdir/cmake/smartbotic-db-client/smartbotic-db-clientConfig.cmake" <<'EOF'
- # Host build of smartbotic-db-client, not the Debian package. Relocatable:
- # every path derives from this file's location.
- include(CMakeFindDependencyMacro)
- find_dependency(Protobuf REQUIRED)
- find_dependency(gRPC CONFIG QUIET)
- if(NOT gRPC_FOUND)
- find_package(PkgConfig REQUIRED)
- pkg_check_modules(GRPC REQUIRED IMPORTED_TARGET grpc++)
- endif()
- get_filename_component(_sbdb_prefix "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
- if(NOT TARGET smartbotic::db-client)
- add_library(smartbotic::db-client SHARED IMPORTED)
- set_target_properties(smartbotic::db-client PROPERTIES
- IMPORTED_LOCATION "${_sbdb_prefix}/@LIBDIR@/libsmartbotic-db-client.so"
- INTERFACE_INCLUDE_DIRECTORIES "${_sbdb_prefix}/include")
- if(TARGET gRPC::grpc++)
- set_property(TARGET smartbotic::db-client APPEND PROPERTY
- INTERFACE_LINK_LIBRARIES protobuf::libprotobuf gRPC::grpc++)
- else()
- set_property(TARGET smartbotic::db-client APPEND PROPERTY
- INTERFACE_LINK_LIBRARIES protobuf::libprotobuf PkgConfig::GRPC)
- endif()
- endif()
- EOF
- sed -i "s|@LIBDIR@|$libdir|" \
- "$PREFIX/$libdir/cmake/smartbotic-db-client/smartbotic-db-clientConfig.cmake"
- fi
- [[ -d "$PREFIX/include/smartbotic" ]] || {
- echo "no database client at $PREFIX - run with --client first" >&2; exit 1; }
- echo "=== configuring $BUILD (clang, Debug, runner on) ==="
- cmake -B "$BUILD" -G Ninja -S "$REPO" \
- -DCMAKE_BUILD_TYPE=Debug \
- -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
- -DCMAKE_PREFIX_PATH="$PREFIX" \
- -DBUILD_TESTS=ON -DBUILD_WEBUI=OFF -DBUILD_RUNNER=ON
- cmake --build "$BUILD" -j"$(nproc)"
- ln -sfn build-host/compile_commands.json "$REPO/compile_commands.json"
- echo "compile_commands.json -> build-host (clangd is ready)"
- if (( RUN_TESTS )); then
- # SERIALLY, deliberately. The ApiFixture tests all share one database, one
- # fixed project name and the two global service collections, so running
- # them in parallel makes them delete each other's state.
- echo "=== ctest (serial) ==="
- ctest --test-dir "$BUILD" --output-on-failure
- fi
|