macOS下使用vagrant編譯WebRTC Android 源碼

前言

本文的前提條件:

  • 你需要編譯WebRTC Android版本源碼
  • 你只有macOS,沒有Linux環(huán)境

安裝Linux虛擬機

安裝vagrant和VitualBox

Vagrant是一款用于構建及配置虛擬開發(fā)環(huán)境的軟件,基于Ruby, 主要以命令行的方式運行。 主要配合使用Oracle的開源Virtual Box 虛擬化系統(tǒng)。

分別在官網(wǎng)下載并安裝這兩個軟件。

注意:安裝VirtualBox時若遇到:

The Installation Failed

需要在設置中允許安裝Oracle的軟件

詳細請參考:Fixing ‘The Installation Failed’ VirtualBox Error on Mac High Sierra

創(chuàng)建Linux虛擬機(Ubuntu16.04)

mkdir webrtc-builder
cd webrtc-builder
vagrant init ubuntu/xenial64

編輯配置文件(重要)

這一步需要將虛擬機的配置設定好,否則使用默認配置后面編譯會遇到諸多問題。

首先我們在webrtc-builder目錄下打開配置文件Vagrantfile

vi Vagrantfile

這是我已經(jīng)配置好的配置文件(省略了注釋,方便閱讀)

Vagrant.configure("2") do |config| 
  config.vm.box = "ubuntu/xenial64"
  config.disksize.size = '45GB'
  config.vm.provider "virtualbox" do |v|
    v.memory = 4096
    v.cpus = 2
  end
  config.vm.network "private_network", ip: "192.168.50.4"
  config.vm.synced_folder "/Volumes/fss/webrtc_linux", "/home/vagrant/webrtc-android-linux", type: :nfs
end

以下是配置文件中需要注意的幾點:

  • config.disksize.size

    磁盤容量,默認是10GB,由于WebRTC Android的源碼有將近20G左右,后續(xù)編譯還會生成一些文件,建議將size設置為40G以上。

  • v.memory

    內(nèi)存大小,默認是1024MB。建議將其設置為4096MB,否則編譯時可能會遇到ninja: build stopped: subcommand failed.問題(內(nèi)存不足)

  • config.vm.synced_folder

    共享目錄,為了方便之后的訪問,這里需要將虛擬機中的目錄共享出來。

    "/Volumes/fss/webrtc_linux":這里是mac上的地址(需要新建卷宗,稍后我會講到)

    "/home/vagrant/webrtc-android-linux":這是虛擬機中對應的目錄

    注意一:type為nfs需要設置config.vm.network "private_network", ip: "192.168.50.4",這里根據(jù)我的這個配置設定即可。

    注意二:默認macOS中的APFS文件系統(tǒng)是大小寫不敏感的,而WebRTC源碼中使用了大小寫來進行區(qū)分,這樣的話后面在編譯時會出現(xiàn)一些問題,導致找不到路徑。解決辦法是在磁盤工具(Disk Utinity)中新建一個卷宗(例如fss),選擇文件系統(tǒng)為APFS(Case-sensitive)(中文是大小寫敏感)。之后將掛載的目錄設置為此卷宗上的目錄即可。

配置文件編輯好后,即可開機。

附上vagrant常用命令:

vagrant up #開機
vagrant halt #關機
vagrant reload #類似重啟(重新加載配置文件)
vagrant ssh #ssh登錄到虛擬機

在編譯之前,我們先ssh登錄到虛擬機:

vagrant ssh

接下來的操作在虛擬機上進行。

WebRTC Android 源碼編譯

準備工作

官方教程: Reference Link

Depot Tools

depot_toolsChromium官方提供的工具包,主要用于Chromium開發(fā)。

clone depot_tools repository:

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

將剛才clone下來的目錄添加到運行環(huán)境中??梢栽诮K端進行單次export,也可以通過配置文件設置全局生效(具體請Google)

這里的/path/to/depot_tools換成自己的對應路徑

export PATH=/path/to/depot_tools:$PATH

下載源碼

mkdir webrtc-android-linux
cd webrtc-android-linux
fetch --nohooks webrtc_android
gclient sync

新建一個工作目錄

fetch Android版本的源碼

fetch完成后,再和遠端代碼庫進行同步,即完成了源碼的下載。

注意:此過程需要保持網(wǎng)絡暢通(可以訪問google)。下載下來的源碼大概有將近20G(一半是WebRTC源碼,另外一部分是Android的NDK和SDK等特殊部分)

安裝依賴

./src/build/install-build-deps.sh
./src/build/install-build-deps-android.sh

編譯

使用GN生成工程

cd src
gn gen out/Debug --args='target_os="android" target_cpu="arm"'

參數(shù)可以修改,詳細請參考官網(wǎng)。

編譯

ninja -C out/Debug

全量編譯,用時較長,與機器性能有關系。

使用和測試

編譯完成后,在out/Debug下生成編譯好的工程。

可以在out/Debug/apks/目錄下找到AppRTCMobile.apk, 安裝到手機即可測試。

附問題總結

我遇到的兩個比較大的問題都發(fā)生在編譯階段。

  • 編譯到7000/11838左右時卡住,killed,報錯:ninja: build stopped: subcommand failed Android

    vagrant@ubuntu-xenial:~/webrtc-checkout-android/src$ ninja -C out/Debug
    ninja: Entering directory `out/Debug'
    [6902/11838] CXX obj/pc/peerconnection/peer_connection.o
    FAILED: obj/pc/peerconnection/peer_connection.o 
    ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/pc/peerconnection/peer_connection.o.d -D_GNU_SOURCE -DANDROID -DHAVE_SYS_UIO_H -DANDROID_NDK_VERSION_ROLL=r20_1 -DCR_CLANG_REVISION=\"llvmorg-12-init-1771-g1bd7046e-3\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -D_LIBCPP_DEBUG=0 -DCR_LIBCXX_REVISION=375504 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DWEBRTC_ENABLE_PROTOBUF=1 -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE -DRTC_ENABLE_VP9 -DHAVE_SCTP -DWEBRTC_ARCH_ARM -DWEBRTC_ARCH_ARM_V7 -DWEBRTC_HAS_NEON -DWEBRTC_LIBRARY_IMPL -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1 -DWEBRTC_POSIX -DWEBRTC_LINUX -DWEBRTC_ANDROID -DABSL_ALLOCATOR_NOTHROW=1 -DHAVE_SCTP -I../.. -Igen -I../../third_party/abseil-cpp -I../../third_party/libyuv/include -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -funwind-tables -fPIC -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers -ffunction-sections -fno-short-enums --target=arm-linux-androideabi16 -DHAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC=1 -march=armv7-a -mfloat-abi=softfp -mtune=generic-armv7-a -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Xclang -fdebug-compilation-dir -Xclang . -no-canonical-prefixes -mfpu=neon -mthumb -Wall -Werror -Wextra -Wimplicit-fallthrough -Wunreachable-code -Wthread-safety -Wextra-semi -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-psabi -Wno-ignored-pragma-optimize -Wno-implicit-int-float-conversion -Wno-final-dtor-non-final-class -Wno-builtin-assume-aligned-alignment -Wno-deprecated-copy -Wno-non-c-typedef-for-linkage -Wmax-tokens -Oz -fno-ident -fdata-sections -ffunction-sections -fomit-frame-pointer -gdwarf-3 -g1 -fdebug-info-for-profiling -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -Wglobal-constructors -Wc++11-narrowing -Wimplicit-fallthrough -Wthread-safety -Winconsistent-missing-override -Wundef -Wunused-lambda-capture -Wno-undefined-bool-conversion -Wno-tautological-undefined-compare -std=c++14 -fno-trigraphs -Wno-trigraphs -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fvisibility-inlines-hidden -Wnon-virtual-dtor -Woverloaded-virtual -c ../../pc/peer_connection.cc -o obj/pc/peerconnection/peer_connection.o
    Killed
    [6904/11838] CXX obj/pc/peerconnection/rtc_stats_collector.o
    ninja: build stopped: subcommand failed.
    

    查詢了一下,許多人在編譯Android源碼時遇到過這個問題,說是因為系統(tǒng)同時打開的文件數(shù)有限。我根據(jù)這個鏈接修改了一下,將文件數(shù)擴大到20480后依然出現(xiàn)問題。

    隨后我查看了虛擬機的配置,發(fā)現(xiàn)它的運存也很小,這個原因也可能導致killed。因此將運存調(diào)整到4096MB。

  • Invalid argument問題

    vagrant@ubuntu-xenial:~/webrtc-android-linux/src$ ninja -C out/Debug
    ninja: Entering directory `out/Debug'
    [6519/11838] ACTION //third_party/android_deps:b...der(//build/toolchain/android:android_clang_arm)
    FAILED: obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar 
    python ../../build/android/gyp/ijar.py clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar obj/third_party/android_deps/backport_util_concurrent_backport_util_concurrent.ijar.jar
    Traceback (most recent call last):
      File "../../build/android/gyp/ijar.py", line 34, in <module>
        main()
      File "../../build/android/gyp/ijar.py", line 30, in main
        build_utils.CheckOutput([ijar_bin, in_jar, f.name])
      File "/home/vagrant/webrtc-android-linux/src/build/android/gyp/util/build_utils.py", line 254, in CheckOutput
        raise CalledProcessError(cwd, args, stdout + stderr)
    util.build_utils.CalledProcessError: Command failed: ( cd /home/vagrant/webrtc-android-linux/src/out/Debug; clang_x64/ijar ../../third_party/android_deps/libs/backport_util_concurrent_backport_util_concurrent/backport-util-concurrent-3.1.jar /home/vagrant/webrtc-android-linux/src/out/Debug/obj/third_party/android_deps/tmp15NzRwbackport_util_concurrent_backport_util_concurrent.ijar.jar )
    mmap(): Invalid argument
    
    [6521/11838] CXX clang_x64/obj/tools/android/md5sum/md5sum_bin/md5sum.o
    ninja: build stopped: subcommand failed.
    

    這個問題是設置了共享目錄之后出現(xiàn)的(當時映射的mac本地目錄是APFS大小寫不敏感)。

    查看這個報錯,應該是在out/Debug中查找不到路徑,那就是路徑有誤。

    后續(xù)將掛載目錄改為APFS大小寫敏感即可。

參考

WebRTC 開發(fā)實踐:編譯 Android 平臺源碼

How to build webrtc library for Android in OSX host

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容