build-nam-lv2.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/sh
  2. set -eu
  3. repository_root=${RIGFORGE_ROOT:-/workspace}
  4. state_file="$repository_root/config/desired-state.yaml"
  5. artifact_directory="$repository_root/artifacts/nam-lv2/arm64/current"
  6. staged_directory=
  7. work_directory=
  8. cleanup() {
  9. if [ -n "$staged_directory" ] && [ -e "$staged_directory" ]; then
  10. rm -rf "$staged_directory"
  11. fi
  12. if [ -n "$work_directory" ] && [ -e "$work_directory" ]; then
  13. rm -rf "$work_directory"
  14. fi
  15. }
  16. trap cleanup EXIT
  17. trap 'exit 129' HUP
  18. trap 'exit 130' INT
  19. trap 'exit 143' TERM
  20. case "$(dpkg --print-architecture)" in
  21. arm64) ;;
  22. *) printf '%s\n' "This build task requires an ARM64 container." >&2; exit 1 ;;
  23. esac
  24. python3 - "$repository_root" "$state_file" <<'PY'
  25. import sys
  26. from pathlib import Path
  27. import yaml
  28. repository_root = Path(sys.argv[1])
  29. sys.path.insert(0, str(repository_root / "scripts"))
  30. from validate_desired_state import validate_state
  31. with Path(sys.argv[2]).open(encoding="utf-8") as stream:
  32. state = yaml.safe_load(stream)
  33. errors = validate_state(state)
  34. if errors:
  35. raise SystemExit("Invalid desired state: " + "; ".join(errors))
  36. PY
  37. set -- $(python3 - "$state_file" <<'PY'
  38. import sys
  39. import yaml
  40. with open(sys.argv[1], encoding="utf-8") as stream:
  41. nam = yaml.safe_load(stream)["deployment"]["namlv2"]
  42. print(nam["source_url"])
  43. print(nam["tag"])
  44. PY
  45. )
  46. source_url=$1
  47. tag=$2
  48. mkdir -p "$(dirname "$artifact_directory")"
  49. previous_directory="$artifact_directory.previous"
  50. if [ -e "$artifact_directory" ] || [ -e "$previous_directory" ]; then
  51. printf '%s\n' "Refusing to overwrite an existing NAM LV2 artifact; move it aside first." >&2
  52. exit 1
  53. fi
  54. work_directory=$(mktemp -d /tmp/nam-lv2-build.XXXXXX)
  55. staged_directory=$(mktemp -d "$(dirname "$artifact_directory")/.build.XXXXXX")
  56. source_directory="$work_directory/source"
  57. git clone --no-checkout "$source_url" "$source_directory"
  58. git -C "$source_directory" checkout --detach "refs/tags/$tag"
  59. git -C "$source_directory" submodule update --init --recursive
  60. source_commit=$(git -C "$source_directory" rev-parse HEAD)
  61. neural_audio_commit=$(git -C "$source_directory/deps/NeuralAudio" rev-parse HEAD)
  62. # Do not inherit the release workflow's -static-libstdc++/-static-libgcc flags.
  63. unset CFLAGS CXXFLAGS
  64. "$(command -v cmake)" -S "$source_directory" -B "$work_directory/build" \
  65. -DCMAKE_BUILD_TYPE=Release \
  66. -DBUILD_NAMCORE=ON \
  67. -DBUILD_INTERNAL_STATIC_WAVENET=ON \
  68. -DBUILD_INTERNAL_STATIC_LSTM=ON \
  69. -DMULTIFRAME_8X8_CONVOLUTION=8
  70. "$(command -v cmake)" --build "$work_directory/build" --target copy_binaries -j2
  71. bundle="$work_directory/build/neural_amp_modeler.lv2"
  72. if [ ! -d "$bundle" ] || [ ! -f "$bundle/neural_amp_modeler.so" ]; then
  73. printf '%s\n' "NAM LV2 build did not produce the expected bundle." >&2
  74. exit 1
  75. fi
  76. install -d "$staged_directory/neural_amp_modeler.lv2"
  77. cp -a "$bundle/." "$staged_directory/neural_amp_modeler.lv2/"
  78. ldd "$staged_directory/neural_amp_modeler.lv2/neural_amp_modeler.so" \
  79. > "$staged_directory/runtime-dependencies.txt"
  80. if grep -F 'not found' "$staged_directory/runtime-dependencies.txt"; then
  81. printf '%s\n' "Unresolved runtime dependency in NAM LV2 build." >&2
  82. exit 1
  83. fi
  84. if ! grep -F 'libstdc++.so.6 =>' "$staged_directory/runtime-dependencies.txt" >/dev/null; then
  85. printf '%s\n' "NAM LV2 must use the target's shared libstdc++ runtime." >&2
  86. exit 1
  87. fi
  88. binary_sha256=$(sha256sum "$staged_directory/neural_amp_modeler.lv2/neural_amp_modeler.so" | cut -d ' ' -f 1)
  89. cmake_version=$("$(command -v cmake)" --version | sed -n '1s/^cmake version //p')
  90. compiler_version=$(c++ --version | sed -n '1p')
  91. cat > "$staged_directory/provenance.json" <<EOF
  92. {
  93. "source_url": "$source_url",
  94. "tag": "$tag",
  95. "source_commit": "$source_commit",
  96. "neural_audio_commit": "$neural_audio_commit",
  97. "platform": "linux/aarch64",
  98. "compiler": "$compiler_version",
  99. "cmake": "$cmake_version",
  100. "linkage": "system libstdc++ and libgcc_s",
  101. "artifact": "neural_amp_modeler.lv2/neural_amp_modeler.so",
  102. "artifact_sha256": "$binary_sha256"
  103. }
  104. EOF
  105. chmod 0755 "$staged_directory"
  106. chmod 0644 "$staged_directory/provenance.json" "$staged_directory/runtime-dependencies.txt"
  107. mv "$staged_directory" "$artifact_directory"
  108. staged_directory=
  109. printf 'Built NAM LV2 %s at %s\n' "$tag" "$source_commit"
  110. printf 'Artifact: %s/neural_amp_modeler.lv2\n' "$artifact_directory"
  111. printf 'SHA-256: %s\n' "$binary_sha256"