host-build.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. # Build vectorapi natively on the dev host, with clang, including the runner.
  3. #
  4. # This exists because the host CAN build everything, contrary to a note this
  5. # repo carried for a long time. The blocker was never protobuf 35: it was a
  6. # stale protoc earlier in PATH. An old protoc emits generated code that
  7. # includes google/protobuf/map_field_inl.h, a header protobuf 35 removed, and
  8. # compiling that against the installed headers is what failed. A protoc
  9. # matching libprotobuf fixes it, and then the database client and all of
  10. # vectorapi build and link natively.
  11. #
  12. # What this buys: a ~100 s full build instead of a container round trip, clang's
  13. # diagnostics, a real compile_commands.json for clangd, and the ability to
  14. # compile runner/src/main.cpp, which previously only the container could.
  15. #
  16. # scripts/host-build.sh # configure + build
  17. # scripts/host-build.sh --test # + run the suite (serially; see below)
  18. # scripts/host-build.sh --client # also rebuild the database client first
  19. #
  20. # Release packages still come from packaging/build.sh and the Debian image.
  21. # This build is for development only: it links the host's protobuf/gRPC, not
  22. # the versions the .deb is built against.
  23. set -euo pipefail
  24. REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  25. PREFIX="${SVAPI_HOST_PREFIX:-/data/dev/smartbotics/.hostprefix}"
  26. DB_SRC="${SVAPI_DB_SRC:-/data/dev/smartbotics/smartbotic-database}"
  27. BUILD="$REPO/build-host"
  28. WITH_CLIENT=0
  29. RUN_TESTS=0
  30. for a in "$@"; do
  31. case "$a" in
  32. --client) WITH_CLIENT=1 ;;
  33. --test) RUN_TESTS=1 ;;
  34. *) echo "unknown argument: $a" >&2; exit 2 ;;
  35. esac
  36. done
  37. # The protoc must match the installed libprotobuf. Comparing the two is the
  38. # whole point: a mismatch is the failure this script exists to prevent, and it
  39. # shows up as a missing header deep in generated code rather than as a version
  40. # complaint, so check it up front and say so plainly.
  41. lib_ver="$(pkg-config --modversion protobuf)"
  42. lib_major="${lib_ver%%.*}"
  43. find_protoc() {
  44. for p in "${SVAPI_PROTOC:-}" "$PREFIX/bin/protoc" /usr/bin/protoc "$(command -v protoc || true)"; do
  45. [[ -n "$p" && -x "$p" ]] || continue
  46. if [[ "$("$p" --version | awk '{print $2}' | cut -d. -f1)" == "$lib_major" ]]; then
  47. echo "$p"; return 0
  48. fi
  49. done
  50. return 1
  51. }
  52. if ! PROTOC="$(find_protoc)"; then
  53. cat >&2 <<EOF
  54. No protoc matching libprotobuf $lib_ver found.
  55. installed libprotobuf : $lib_ver (so major $lib_major)
  56. protoc in PATH : $(command -v protoc >/dev/null && protoc --version || echo "none")
  57. Get a matching one, then re-run:
  58. - Void: sudo xbps-install protobuf-$lib_major.<minor>
  59. ('protobuf-devel' does NOT ship protoc; the compiler is its own package)
  60. - or: download protoc-$lib_major.<minor>-linux-x86_64.zip from
  61. https://github.com/protocolbuffers/protobuf/releases and point
  62. SVAPI_PROTOC at its bin/protoc
  63. Note a stale protoc earlier in PATH (commonly ~/.local/bin/protoc) will be
  64. found before the system one. This script ignores PATH order and picks by
  65. version, but the database's own CMake does not.
  66. EOF
  67. exit 1
  68. fi
  69. echo "protoc: $PROTOC ($("$PROTOC" --version)) against libprotobuf $lib_ver"
  70. if (( WITH_CLIENT )); then
  71. [[ -d "$DB_SRC" ]] || { echo "database source not at $DB_SRC" >&2; exit 1; }
  72. echo "=== building libsmartbotic-db-client from $DB_SRC into $PREFIX ==="
  73. cmake -B /tmp/svapi-dbclient -G Ninja -S "$DB_SRC" \
  74. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PREFIX" \
  75. -DPROTOC="$PROTOC" \
  76. -DSMARTBOTIC_DB_BUILD_CLIENT=ON -DSMARTBOTIC_DB_BUILD_SERVICE=OFF \
  77. -DSMARTBOTIC_DB_BUILD_CLI=OFF -DSMARTBOTIC_DB_BUILD_TESTS=OFF >/dev/null
  78. cmake --build /tmp/svapi-dbclient -j"$(nproc)"
  79. cmake --install /tmp/svapi-dbclient >/dev/null
  80. # `cmake --install` ships the library and headers but no CMake package, so
  81. # find_package(smartbotic-db-client) would fail: only the .deb carries a
  82. # config, written by the database's create-debs.sh. Write a relocatable one.
  83. libdir="$(cd "$PREFIX" && ls -d lib64 lib 2>/dev/null | head -1)"
  84. mkdir -p "$PREFIX/$libdir/cmake/smartbotic-db-client"
  85. cat > "$PREFIX/$libdir/cmake/smartbotic-db-client/smartbotic-db-clientConfig.cmake" <<'EOF'
  86. # Host build of smartbotic-db-client, not the Debian package. Relocatable:
  87. # every path derives from this file's location.
  88. include(CMakeFindDependencyMacro)
  89. find_dependency(Protobuf REQUIRED)
  90. find_dependency(gRPC CONFIG QUIET)
  91. if(NOT gRPC_FOUND)
  92. find_package(PkgConfig REQUIRED)
  93. pkg_check_modules(GRPC REQUIRED IMPORTED_TARGET grpc++)
  94. endif()
  95. get_filename_component(_sbdb_prefix "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
  96. if(NOT TARGET smartbotic::db-client)
  97. add_library(smartbotic::db-client SHARED IMPORTED)
  98. set_target_properties(smartbotic::db-client PROPERTIES
  99. IMPORTED_LOCATION "${_sbdb_prefix}/@LIBDIR@/libsmartbotic-db-client.so"
  100. INTERFACE_INCLUDE_DIRECTORIES "${_sbdb_prefix}/include")
  101. if(TARGET gRPC::grpc++)
  102. set_property(TARGET smartbotic::db-client APPEND PROPERTY
  103. INTERFACE_LINK_LIBRARIES protobuf::libprotobuf gRPC::grpc++)
  104. else()
  105. set_property(TARGET smartbotic::db-client APPEND PROPERTY
  106. INTERFACE_LINK_LIBRARIES protobuf::libprotobuf PkgConfig::GRPC)
  107. endif()
  108. endif()
  109. EOF
  110. sed -i "s|@LIBDIR@|$libdir|" \
  111. "$PREFIX/$libdir/cmake/smartbotic-db-client/smartbotic-db-clientConfig.cmake"
  112. fi
  113. [[ -d "$PREFIX/include/smartbotic" ]] || {
  114. echo "no database client at $PREFIX - run with --client first" >&2; exit 1; }
  115. echo "=== configuring $BUILD (clang, Debug, runner on) ==="
  116. cmake -B "$BUILD" -G Ninja -S "$REPO" \
  117. -DCMAKE_BUILD_TYPE=Debug \
  118. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
  119. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
  120. -DCMAKE_PREFIX_PATH="$PREFIX" \
  121. -DBUILD_TESTS=ON -DBUILD_WEBUI=OFF -DBUILD_RUNNER=ON
  122. cmake --build "$BUILD" -j"$(nproc)"
  123. ln -sfn build-host/compile_commands.json "$REPO/compile_commands.json"
  124. echo "compile_commands.json -> build-host (clangd is ready)"
  125. if (( RUN_TESTS )); then
  126. # SERIALLY, deliberately. The ApiFixture tests all share one database, one
  127. # fixed project name and the two global service collections, so running
  128. # them in parallel makes them delete each other's state.
  129. echo "=== ctest (serial) ==="
  130. ctest --test-dir "$BUILD" --output-on-failure
  131. fi