build_config.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. # Set PKG_CONFIG_PATH to find bundled Invercargill library
  70. export PKG_CONFIG_PATH="$prefix/lib/pkgconfig:$prefix/share/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
  71. # Set XDG_DATA_DIRS for GObject Introspection and Vala to find .gir and .vapi files
  72. # Include default system paths to ensure system vapi files are found
  73. export XDG_DATA_DIRS="$prefix/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}:/usr/local/share:/usr/share"
  74. # Configure with meson (force libdir=lib for consistency across platforms)
  75. if ! meson setup src builddir --prefix="$prefix" --libdir=lib -Ddefault_library=static >"$output_redirect" 2>&1; then
  76. log_error "Failed to configure Invercargill-Json"
  77. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  78. popd >/dev/null
  79. return 1
  80. fi
  81. # Build with ninja
  82. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  83. log_error "Failed to build Invercargill-Json"
  84. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  85. popd >/dev/null
  86. return 1
  87. fi
  88. # Install
  89. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  90. log_error "Failed to install Invercargill-Json"
  91. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  92. popd >/dev/null
  93. return 1
  94. fi
  95. popd >/dev/null
  96. log_info "Invercargill-Json built and installed successfully"
  97. return 0
  98. }
  99. # Build function for USM
  100. build_usm() {
  101. local src_dir="$1"
  102. local prefix="$2"
  103. local output_redirect=$(get_output_redirect)
  104. pushd "$src_dir" >/dev/null || return 1
  105. # Set PKG_CONFIG_PATH to find bundled Invercargill libraries
  106. export PKG_CONFIG_PATH="$prefix/lib/pkgconfig:$prefix/share/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
  107. # Set XDG_DATA_DIRS for GObject Introspection and Vala to find .gir and .vapi files
  108. # Include default system paths to ensure system vapi files are found
  109. export XDG_DATA_DIRS="$prefix/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}:/usr/local/share:/usr/share"
  110. # Configure with meson (USM's meson.build is at root, force libdir=lib for consistency)
  111. if ! meson setup builddir --prefix="$prefix" --libdir=lib >"$output_redirect" 2>&1; then
  112. log_error "Failed to configure USM"
  113. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  114. popd >/dev/null
  115. return 1
  116. fi
  117. # Build with ninja
  118. if ! ninja -C builddir >"$output_redirect" 2>&1; then
  119. log_error "Failed to build USM"
  120. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  121. popd >/dev/null
  122. return 1
  123. fi
  124. # Install
  125. if ! ninja -C builddir install >"$output_redirect" 2>&1; then
  126. log_error "Failed to install USM"
  127. [[ "$VERBOSE" != "true" ]] && log_info "Run with --verbose for details"
  128. popd >/dev/null
  129. return 1
  130. fi
  131. popd >/dev/null
  132. log_info "USM built and installed successfully"
  133. return 0
  134. }
  135. # Generic build dispatcher
  136. build_component() {
  137. local component="$1"
  138. local src_dir="$2"
  139. local prefix="$3"
  140. case "$component" in
  141. "invercargill")
  142. build_invercargill "$src_dir" "$prefix"
  143. ;;
  144. "invercargill-json")
  145. build_invercargill_json "$src_dir" "$prefix"
  146. ;;
  147. "usm")
  148. build_usm "$src_dir" "$prefix"
  149. ;;
  150. *)
  151. log_error "Unknown component: $component"
  152. return 1
  153. ;;
  154. esac
  155. }
  156. # Install shim to /usr/bin
  157. install_shim() {
  158. local target_dir="$1"
  159. local shim_path="/usr/bin/usm"
  160. local sudo=""
  161. if ! is_root; then
  162. sudo=$(get_sudo)
  163. fi
  164. log_step "Installing shim to $shim_path..."
  165. local shim_content="#!/bin/bash
  166. export LD_LIBRARY_PATH=\"${target_dir}/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}\"
  167. exec \"${target_dir}/bin/usm\" \"\$@\"
  168. "
  169. if [[ -n "$sudo" ]]; then
  170. echo "$shim_content" | $sudo tee "$shim_path" >/dev/null
  171. $sudo chmod +x "$shim_path"
  172. else
  173. echo "$shim_content" > "$shim_path"
  174. chmod +x "$shim_path"
  175. fi
  176. log_info "Shim installed to $shim_path"
  177. }
  178. # Build all components in order
  179. build_all() {
  180. local src_base="$1"
  181. local prefix="$2"
  182. local total=${#BUILD_ORDER[@]}
  183. local current=0
  184. for component in "${BUILD_ORDER[@]}"; do
  185. ((current++))
  186. log_step "[$current/$total] Building $(get_component_name "$component")..."
  187. local src_dir
  188. if [[ "$(get_component_source "$component")" == "BUNDLED" ]]; then
  189. src_dir="$src_base/usm"
  190. else
  191. src_dir="$src_base/$(get_component_name "$component")"
  192. fi
  193. if ! build_component "$component" "$src_dir" "$prefix"; then
  194. log_error "Failed to build $(get_component_name "$component"). Installation halted."
  195. return 1
  196. fi
  197. done
  198. return 0
  199. }