build_config.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. # build_config.sh - Build and install configuration for USM and dependencies
  3. # Build order - dependencies must be built in this order
  4. BUILD_ORDER=("invercargill" "invercargill-json" "usm")
  5. # Component definitions
  6. declare -A COMPONENTS=(
  7. ["invercargill"]="Invercargill|invercargill-1|https://fabrica.unitatem.net/Tilo15/Invercargill.git"
  8. ["invercargill-json"]="Invercargill-Json|invercargill-json|https://git.sr.ht/~tilo15/Invercargill-Json"
  9. ["usm"]="USM|usm|BUNDLED"
  10. )
  11. # Component properties accessor functions
  12. get_component_name() {
  13. local key="$1"
  14. echo "${COMPONENTS[$key]}" | cut -d'|' -f1
  15. }
  16. get_component_vapi() {
  17. local key="$1"
  18. echo "${COMPONENTS[$key]}" | cut -d'|' -f2
  19. }
  20. get_component_source() {
  21. local key="$1"
  22. echo "${COMPONENTS[$key]}" | cut -d'|' -f3
  23. }
  24. # Get output redirection based on VERBOSE flag
  25. get_output_redirect() {
  26. if [[ "$VERBOSE" == "true" ]]; then
  27. echo "/dev/stdout"
  28. else
  29. echo "/dev/null"
  30. fi
  31. }
  32. # Build function for Invercargill
  33. build_invercargill() {
  34. local src_dir="$1"
  35. local prefix="$2"
  36. local output_redirect=$(get_output_redirect)
  37. pushd "$src_dir" >/dev/null || return 1
  38. # Configure with meson
  39. if ! meson setup src builddir --prefix="$prefix" -Ddefault_library=static >"$output_redirect" 2>&1; then
  40. log_error "Failed to configure Invercargill"
  41. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  42. popd >/dev/null
  43. return 1
  44. fi
  45. # Build with ninja
  46. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  47. log_error "Failed to build Invercargill"
  48. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  49. popd >/dev/null
  50. return 1
  51. fi
  52. # Install
  53. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  54. log_error "Failed to install Invercargill"
  55. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  56. popd >/dev/null
  57. return 1
  58. fi
  59. popd >/dev/null
  60. log_info "Invercargill built and installed successfully"
  61. return 0
  62. }
  63. # Build function for Invercargill-Json
  64. build_invercargill_json() {
  65. local src_dir="$1"
  66. local prefix="$2"
  67. local output_redirect=$(get_output_redirect)
  68. pushd "$src_dir" >/dev/null || return 1
  69. # Configure with meson
  70. if ! meson setup src builddir --prefix="$prefix" -Ddefault_library=static >"$output_redirect" 2>&1; then
  71. log_error "Failed to configure Invercargill-Json"
  72. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  73. popd >/dev/null
  74. return 1
  75. fi
  76. # Build with ninja
  77. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  78. log_error "Failed to build Invercargill-Json"
  79. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  80. popd >/dev/null
  81. return 1
  82. fi
  83. # Install
  84. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  85. log_error "Failed to install Invercargill-Json"
  86. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  87. popd >/dev/null
  88. return 1
  89. fi
  90. popd >/dev/null
  91. log_info "Invercargill-Json built and installed successfully"
  92. return 0
  93. }
  94. # Build function for USM
  95. build_usm() {
  96. local src_dir="$1"
  97. local prefix="$2"
  98. local output_redirect=$(get_output_redirect)
  99. pushd "$src_dir" >/dev/null || return 1
  100. # Configure with meson (USM's meson.build is at root, not in src/)
  101. if ! meson setup builddir --prefix="$prefix" >"$output_redirect" 2>&1; then
  102. log_error "Failed to configure USM"
  103. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  104. popd >/dev/null
  105. return 1
  106. fi
  107. # Build with ninja
  108. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  109. log_error "Failed to build USM"
  110. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  111. popd >/dev/null
  112. return 1
  113. fi
  114. # Install
  115. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  116. log_error "Failed to install USM"
  117. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  118. popd >/dev/null
  119. return 1
  120. fi
  121. popd >/dev/null
  122. log_info "USM built and installed successfully"
  123. return 0
  124. }
  125. # Generic build dispatcher
  126. build_component() {
  127. local component="$1"
  128. local src_dir="$2"
  129. local prefix="$3"
  130. case "$component" in
  131. "invercargill")
  132. build_invercargill "$src_dir" "$prefix"
  133. ;;
  134. "invercargill-json")
  135. build_invercargill_json "$src_dir" "$prefix"
  136. ;;
  137. "usm")
  138. build_usm "$src_dir" "$prefix"
  139. ;;
  140. *)
  141. log_error "Unknown component: $component"
  142. return 1
  143. ;;
  144. esac
  145. }
  146. # Install shim to /usr/bin
  147. install_shim() {
  148. local target_dir="$1"
  149. local shim_path="/usr/bin/usm"
  150. local sudo=""
  151. if ! is_root; then
  152. sudo=$(get_sudo)
  153. fi
  154. log_step "Installing shim to $shim_path..."
  155. local shim_content="#!/bin/bash
  156. exec ${target_dir}/bin/usm \"\$@\"
  157. "
  158. if [[ -n "$sudo" ]]; then
  159. echo "$shim_content" | $sudo tee "$shim_path" >/dev/null
  160. $sudo chmod +x "$shim_path"
  161. else
  162. echo "$shim_content" > "$shim_path"
  163. chmod +x "$shim_path"
  164. fi
  165. log_info "Shim installed to $shim_path"
  166. }
  167. # Build all components in order
  168. build_all() {
  169. local src_base="$1"
  170. local prefix="$2"
  171. local total=${#BUILD_ORDER[@]}
  172. local current=0
  173. for component in "${BUILD_ORDER[@]}"; do
  174. ((current++))
  175. log_step "[$current/$total] Building $(get_component_name "$component")..."
  176. local src_dir
  177. if [[ "$(get_component_source "$component")" == "BUNDLED" ]]; then
  178. src_dir="$src_base/usm"
  179. else
  180. src_dir="$src_base/$(get_component_name "$component")"
  181. fi
  182. if ! build_component "$component" "$src_dir" "$prefix"; then
  183. log_error "Failed to build $(get_component_name "$component"). Installation halted."
  184. return 1
  185. fi
  186. done
  187. return 0
  188. }