| 1 | #!/bin/bash |
| 2 | # Exit immediately if any command fails. This ensures CI properly reports build |
| 3 | # failures instead of continuing to create empty/broken artifacts. |
| 4 | set -e |
| 5 | |
| 6 | # Copyright 2023 LiveKit, Inc. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
| 20 | |
| 21 | arch="" |
| 22 | profile="release" |
| 23 | |
| 24 | while [ "$#" -gt 0 ]; do |
| 25 | case "$1" in |
| 26 | --arch) |
| 27 | arch="$2" |
| 28 | if [ "$arch" != "x64" ] && [ "$arch" != "arm64" ]; then |
| 29 | echo "Error: Invalid value for --arch. Must be 'x64' or 'arm64'." |
| 30 | exit 1 |
| 31 | fi |
| 32 | shift 2 |
| 33 | ;; |
| 34 | --profile) |
| 35 | profile="$2" |
| 36 | if [ "$profile" != "debug" ] && [ "$profile" != "release" ]; then |
| 37 | echo "Error: Invalid value for --profile. Must be 'debug' or 'release'." |
| 38 | exit 1 |
| 39 | fi |
| 40 | shift 2 |
| 41 | ;; |
| 42 | *) |
| 43 | echo "Error: Unknown argument '$1'" |
| 44 | exit 1 |
| 45 | ;; |
| 46 | esac |
| 47 | done |
| 48 | |
| 49 | if [ -z "$arch" ]; then |
| 50 | echo "Error: --arch must be set." |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | echo "Building LiveKit WebRTC - Linux" |
| 55 | echo "Arch: $arch" |
| 56 | echo "Profile: $profile" |
| 57 | |
| 58 | if [ ! -e "$(pwd)/depot_tools" ] |
| 59 | then |
| 60 | git clone --depth 1 https://chromium.googlesource.com/chromium/tools/depot_tools.git |
| 61 | fi |
| 62 | |
| 63 | export COMMAND_DIR=$(cd $(dirname $0); pwd) |
| 64 | export PATH="$(pwd)/depot_tools:$PATH" |
| 65 | export OUTPUT_DIR="$(pwd)/src/out-$arch-$profile" |
| 66 | export ARTIFACTS_DIR="$(pwd)/linux-$arch-$profile" |
| 67 | |
| 68 | if [ ! -e "$(pwd)/src" ] |
| 69 | then |
| 70 | gclient sync -D --no-history |
| 71 | fi |
| 72 | |
| 73 | source "$COMMAND_DIR/apply_patch.sh" |
| 74 | |
| 75 | cd src |
| 76 | apply_patch "$COMMAND_DIR/patches/add_licenses.patch" |
| 77 | apply_patch "$COMMAND_DIR/patches/fix_license_json_parsing.patch" |
| 78 | apply_patch "$COMMAND_DIR/patches/ssl_verify_callback_with_native_handle.patch" |
| 79 | apply_patch "$COMMAND_DIR/patches/add_deps.patch" |
| 80 | apply_patch "$COMMAND_DIR/patches/fix_desktop_capture_compile.patch" |
| 81 | apply_patch "$COMMAND_DIR/patches/external_audio_source.patch" |
| 82 | apply_patch "$COMMAND_DIR/patches/fix_pipewire_utils_compile.patch" |
| 83 | |
| 84 | # Disable CREL (compact relocations). Chromium's build enables experimental |
| 85 | # CREL via -Wa,--crel which causes segfaults on aarch64-linux (and is known |
| 86 | # broken on arm32 and s390x too). |
| 87 | # See: https://crbug.com/376278218 |
| 88 | # See: https://github.com/zed-industries/zed/pull/51433#discussion_r2944567608 |
| 89 | apply_patch "$COMMAND_DIR/patches/disable_crel.patch" build |
| 90 | |
| 91 | cd third_party |
| 92 | |
| 93 | apply_patch "$COMMAND_DIR/patches/david_disable_gun_source_macro.patch" |
| 94 | |
| 95 | cd libyuv |
| 96 | |
| 97 | apply_patch "$COMMAND_DIR/patches/disable_sme_for_libyuv.patch" |
| 98 | |
| 99 | cd ../../.. |
| 100 | |
| 101 | mkdir -p "$ARTIFACTS_DIR/lib" |
| 102 | |
| 103 | python3 "./src/build/linux/sysroot_scripts/install-sysroot.py" --arch="$arch" |
| 104 | |
| 105 | debug="false" |
| 106 | if [ "$profile" = "debug" ]; then |
| 107 | debug="true" |
| 108 | fi |
| 109 | |
| 110 | # Note: use_clang_modules=false is required to avoid C++ module compilation issues. |
| 111 | # Without this flag, the build may fail partway through, resulting in missing |
| 112 | # or incomplete artifacts. |
| 113 | # |
| 114 | # The C++ standard library choice is an ABI contract with webrtc-sys: |
| 115 | # |
| 116 | # use_custom_libcxx=true builds against Chromium's hermetic libc++, whose |
| 117 | # headers we ship in the artifacts (see below) so webrtc-sys compiles against |
| 118 | # byte-identical std types. The alternative -- letting each side use its own |
| 119 | # host stdlib -- is unsound now that WebRTC puts std types like std::span in |
| 120 | # public API signatures: libstdc++ reordered std::span's members after GCC 10, |
| 121 | # so a span built by the host and read inside libwebrtc.a had its pointer and |
| 122 | # size swapped, turning a 14-byte DataChannel::Send into new uint8_t[93TB]. |
| 123 | args="is_debug=$debug \ |
| 124 | target_os=\"linux\" \ |
| 125 | target_cpu=\"$arch\" \ |
| 126 | rtc_enable_protobuf=false \ |
| 127 | treat_warnings_as_errors=false \ |
| 128 | use_llvm_libatomic=false \ |
| 129 | use_custom_libcxx=true \ |
| 130 | use_custom_libcxx_for_host=true \ |
| 131 | use_clang_modules=false \ |
| 132 | rtc_include_tests=false \ |
| 133 | rtc_build_tools=false \ |
| 134 | rtc_build_examples=false \ |
| 135 | rtc_libvpx_build_vp9=true \ |
| 136 | enable_libaom=true \ |
| 137 | is_component_build=false \ |
| 138 | enable_stripping=true \ |
| 139 | ffmpeg_branding=\"Chrome\" \ |
| 140 | rtc_use_h264=true \ |
| 141 | rtc_use_h265=true \ |
| 142 | rtc_use_pipewire=true \ |
| 143 | symbol_level=0 \ |
| 144 | enable_iterator_debugging=false \ |
| 145 | use_rtti=true \ |
| 146 | rtc_use_x11=true" |
| 147 | |
| 148 | # generate ninja files |
| 149 | gn gen "$OUTPUT_DIR" --root="src" --args="${args}" |
| 150 | |
| 151 | # build static library |
| 152 | ninja -C "$OUTPUT_DIR" :default |
| 153 | |
| 154 | # Build the hermetic libc++/libc++abi for the *target* explicitly. |
| 155 | # |
| 156 | # :default does not: nothing here links a target binary (no tests, tools or |
| 157 | # examples, and libwebrtc is a static library), so the libc++ headers get used |
| 158 | # but its own translation units are never compiled. On x64 that went unnoticed |
| 159 | # because use_custom_libcxx_for_host builds libc++ for the host toolchain, which |
| 160 | # in a native build shares $OUTPUT_DIR/obj with the target — so the host copy |
| 161 | # landed in the archive below by accident. Cross-compiling arm64 puts the host |
| 162 | # copy under $OUTPUT_DIR/clang_x64/obj instead, and the arm64 artifact shipped |
| 163 | # with every out-of-line std::__Cr symbol undefined (std::__Cr::locale, |
| 164 | # __libcpp_verbose_abort, __cxa_throw, ...). Nothing in this build notices; it |
| 165 | # only surfaces when a downstream crate links webrtc-sys. |
| 166 | # |
| 167 | # Ask ninja for the archive paths rather than hardcoding a GN label, so this |
| 168 | # fails loudly instead of silently no-opping if libc++ ever moves. Host copies |
| 169 | # live under a toolchain subdir, so anchoring at obj/ keeps them out. |
| 170 | libcxx_archives=$(ninja -C "$OUTPUT_DIR" -t targets all \ |
| 171 | | grep -oE '^obj/[^:]*/libc\+\+(abi)?\.a' | sort -u) |
| 172 | if [ -z "$libcxx_archives" ]; then |
| 173 | echo "Error: no hermetic libc++ static library in the ninja graph for $OUTPUT_DIR." >&2 |
| 174 | echo " use_custom_libcxx=true is an ABI contract with webrtc-sys; aborting." >&2 |
| 175 | exit 1 |
| 176 | fi |
| 177 | echo "Building hermetic libc++: $libcxx_archives" |
| 178 | ninja -C "$OUTPUT_DIR" $libcxx_archives |
| 179 | |
| 180 | # make libwebrtc.a |
| 181 | # don't include nasm |
| 182 | # Start from scratch: `ar -rc` only replaces members it is given, so members left |
| 183 | # over from a previous build with different args would survive into the archive. |
| 184 | rm -f "$ARTIFACTS_DIR/lib/libwebrtc.a" |
| 185 | ar -rc "$ARTIFACTS_DIR/lib/libwebrtc.a" `find "$OUTPUT_DIR/obj" -name '*.o' -not -path "*/third_party/nasm/*"` |
| 186 | src/third_party/llvm-build/Release+Asserts/bin/llvm-objcopy --redefine-syms="$COMMAND_DIR/boringssl_prefix_symbols.txt" "$ARTIFACTS_DIR/lib/libwebrtc.a" |
| 187 | |
| 188 | # The archive is the whole standard library for webrtc-sys (build.rs passes |
| 189 | # cpp_link_stdlib(None), since mixing in the host libstdc++ would be a second, |
| 190 | # incompatible stdlib). Check the out-of-line half is really in there instead of |
| 191 | # publishing an artifact that only fails at the downstream link step. One nm pass |
| 192 | # over an 80MB archive is enough for both markers: libc++ and libc++abi land in |
| 193 | # obj/ together or not at all. |
| 194 | libcxx_markers=$(src/third_party/llvm-build/Release+Asserts/bin/llvm-nm --defined-only \ |
| 195 | "$ARTIFACTS_DIR/lib/libwebrtc.a" 2>/dev/null \ |
| 196 | | grep -oE '__libcpp_verbose_abort|__cxa_throw' | sort -u | tr '\n' ' ') |
| 197 | for sym in __libcpp_verbose_abort __cxa_throw; do |
| 198 | case " $libcxx_markers " in |
| 199 | *" $sym "*) ;; |
| 200 | *) |
| 201 | echo "Error: $ARTIFACTS_DIR/lib/libwebrtc.a defines no $sym." >&2 |
| 202 | echo " The hermetic libc++/libc++abi objects are missing from the archive." >&2 |
| 203 | exit 1 |
| 204 | ;; |
| 205 | esac |
| 206 | done |
| 207 | |
| 208 | # License generation is optional - may fail with some Python versions |
| 209 | # Use vpython3 from depot_tools for consistent Python version |
| 210 | vpython3 "./src/tools_webrtc/libs/generate_licenses.py" \ |
| 211 | --target :default "$OUTPUT_DIR" "$OUTPUT_DIR" || echo "Warning: License generation failed (non-critical)" |
| 212 | |
| 213 | cp "$OUTPUT_DIR/obj/webrtc.ninja" "$ARTIFACTS_DIR" |
| 214 | cp "$OUTPUT_DIR/obj/modules/desktop_capture/desktop_capture.ninja" "$ARTIFACTS_DIR" |
| 215 | cp "$OUTPUT_DIR/args.gn" "$ARTIFACTS_DIR" |
| 216 | |
| 217 | cp "$OUTPUT_DIR/LICENSE.md" "$ARTIFACTS_DIR" |
| 218 | |
| 219 | cd src |
| 220 | find . -name "*.h" -print | cpio -pd "$ARTIFACTS_DIR/include" |
| 221 | find . -name "*.inc" -print | cpio -pd "$ARTIFACTS_DIR/include" |
| 222 | |
| 223 | # Ship Chromium's hermetic libc++ so webrtc-sys can compile against the exact |
| 224 | # same standard library that is baked into libwebrtc.a (see use_custom_libcxx |
| 225 | # above). The find calls cannot do this: libc++ headers have no extension. |
| 226 | # Paths mirror the -isystem/-I flags in build/config/c++/BUILD.gn, so build.rs |
| 227 | # can point at them the same way the WebRTC build does. |
| 228 | for inc in third_party/libc++/src/include third_party/libc++abi/src/include; do |
| 229 | mkdir -p "$ARTIFACTS_DIR/include/$inc" |
| 230 | cp -R "$inc/." "$ARTIFACTS_DIR/include/$inc/" |
| 231 | done |
| 232 | mkdir -p "$ARTIFACTS_DIR/include/buildtools/third_party/libc++" |
| 233 | cp buildtools/third_party/libc++/__config_site \ |
| 234 | buildtools/third_party/libc++/__assertion_handler \ |
| 235 | "$ARTIFACTS_DIR/include/buildtools/third_party/libc++/" |