build_config.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 (force libdir=lib for consistency across platforms)
  39. if ! meson setup src builddir --prefix="$prefix" --libdir=lib -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 (force libdir=lib for consistency across platforms)
  70. if ! meson setup src builddir --prefix="$prefix" --libdir=lib -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. # Set PKG_CONFIG_PATH to find bundled Invercargill libraries
  101. export PKG_CONFIG_PATH="$prefix/lib/pkgconfig:$prefix/share/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
  102. # Configure with meson (USM's meson.build is at root, force libdir=lib for consistency)
  103. if ! meson setup builddir --prefix="$prefix" --libdir=lib >"$output_redirect" 2>&1; then
  104. log_error "Failed to configure USM"
  105. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  106. popd >/dev/null
  107. return 1
  108. fi
  109. # Build with ninja
  110. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  111. log_error "Failed to build USM"
  112. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  113. popd >/dev/null
  114. return 1
  115. fi
  116. # Install
  117. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  118. log_error "Failed to install USM"
  119. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  120. popd >/dev/null
  121. return 1
  122. fi
  123. popd >/dev/null
  124. log_info "USM built and installed successfully"
  125. return 0
  126. }
  127. # Generic build dispatcher
  128. build_component() {
  129. local component="$1"
  130. local src_dir="$2"
  131. local prefix="$3"
  132. case "$component" in
  133. "invercargill")
  134. build_invercargill "$src_dir" "$prefix"
  135. ;;
  136. "invercargill-json")
  137. build_invercargill_json "$src_dir" "$prefix"
  138. ;;
  139. "usm")
  140. build_usm "$src_dir" "$prefix"
  141. ;;
  142. *)
  143. log_error "Unknown component: $component"
  144. return 1
  145. ;;
  146. esac
  147. }
  148. # Install shim to /usr/bin
  149. install_shim() {
  150. local target_dir="$1"
  151. local shim_path="/usr/bin/usm"
  152. local sudo=""
  153. if ! is_root; then
  154. sudo=$(get_sudo)
  155. fi
  156. log_step "Installing shim to $shim_path..."
  157. local shim_content="#!/bin/bash
  158. export LD_LIBRARY_PATH=\"${target_dir}/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}\"
  159. exec \"${target_dir}/bin/usm\" \"\$@\"
  160. "
  161. if [[ -n "$sudo" ]]; then
  162. echo "$shim_content" | $sudo tee "$shim_path" >/dev/null
  163. $sudo chmod +x "$shim_path"
  164. else
  165. echo "$shim_content" > "$shim_path"
  166. chmod +x "$shim_path"
  167. fi
  168. log_info "Shim installed to $shim_path"
  169. }
  170. # Build all components in order
  171. build_all() {
  172. local src_base="$1"
  173. local prefix="$2"
  174. local total=${#BUILD_ORDER[@]}
  175. local current=0
  176. for component in "${BUILD_ORDER[@]}"; do
  177. ((current++))
  178. log_step "[$current/$total] Building $(get_component_name "$component")..."
  179. local src_dir
  180. if [[ "$(get_component_source "$component")" == "BUNDLED" ]]; then
  181. src_dir="$src_base/usm"
  182. else
  183. src_dir="$src_base/$(get_component_name "$component")"
  184. fi
  185. if ! build_component "$component" "$src_dir" "$prefix"; then
  186. log_error "Failed to build $(get_component_name "$component"). Installation halted."
  187. return 1
  188. fi
  189. done
  190. return 0
  191. }