Mac下electron编译

官方教程:https://www.electronjs.org/docs/development/build-instructions-gn

1.获取源码

mkdir electron && cd electron
gclient config --name "src/electron" --unmanaged https://github.com/electron/electron
gclient sync --with_branch_heads --with_tags
# This will take a while, go get a coffee.

在获取源码时,需要外网访问权限,最重要的是需要网络稳定。下载源码时关键的一步是对git头的下载,大概有23G左右。在git头下载期间vpn重连导致下载速度变成很慢,这时可以重新执行命令下载了,如果过一段时间速度回升则可以继续下载,vpn断联大概率会导致之前代理下载的文件删除,需要重新下载。

如果git头已经下载完毕,这时候vpn断联可以通过删除除git头之外的文件,通过git先拉取一部分代码,因为删除文件会导致本地有未提交的git修改,可以通过以下命令强制覆盖本地修改。

git fetch --all                  # 拉取所有更新,不同步
git reset --hard origin/master   # 本地代码同步线上最新版本(会覆盖本地所有与远程仓库上同名的文件)
git pull                         # 再更新一次

git同步下来的代码并不完全是我们需要的代码,还需要通过gclient进行二次同步,不过再此之前我们可以先将electron的源码切换到我们想要编译的版本,例如v7.1.5,这样我们在执行gclient时可以直接同步electron-v7.1.5对应的源码。如果我们一切顺利代码一次性同步完后,在我们切换完electron版本后也要再执行以下的命令进行同步。

gclient sync --with_branch_heads --with_tags

如果electron的版本和gclient同步下来的chrome等的代码版本不对应,会导致一些错误发生,例如electron-v7.1.5编译时会使用jambo进行加速,但是在新的chrome中这个编译选项是禁止,即使我们禁用了jambo编译选项,但是又会导致其它的一些问题。

2.编译源码

cd src
export CHROMIUM_BUILDTOOLS_PATH=`pwd`/buildtools
# this next line is needed only if building with sccache
export GN_EXTRA_ARGS="${GN_EXTRA_ARGS} cc_wrapper=\"${PWD}/electron/external_binaries/sccache\""
gn gen out/Release --args="import(\"//electron/build/args/release.gn\") $GN_EXTRA_ARGS" --ide=xcode   # (mac)

sccache会帮助我们在删除编译文件后再次编译时进行加速
上面的命令会生成工程文件,下面的命令接着编译出目标文件

ninja -C out/Release electron
ninja -C out/Release electron:electron_dist_zip     # 编译出electron的同时将其打包成zip包

3.编译过程中出现的问题

1.typescript错误

这个问题是node的版本太低导致的问题,需要更新node版本

2.metal_api_proxy的MTLDeviceProxy错误

FAILED: obj/components/viz/common/metal_context_provider/metal_api_proxy.o 
/Users/chpross/programmierung/electron-gn/src/electron/external_binaries/sccache ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/components/viz/common/metal_context_provider/metal_api_proxy.o.d -DVIZ_METAL_CONTEXT_PROVIDER_IMPLEMENTATION -DDCHECK_ALWAYS_ON=1 -D_LIBCPP_HAS_NO_ALIGNED_ALLOCATION -DCR_XCODE_VERSION=1120 -DCR_CLANG_REVISION=\"373424-64a362e7-1\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCPP_ENABLE_NODISCARD -DCR_LIBCXX_REVISION=361348 -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DWEBP_EXTERN=extern -DENABLE_IPC_FUZZER -DUSE_EGL -DSK_GL -DSK_HAS_PNG_LIBRARY -DSK_HAS_WEBP_LIBRARY -DSK_USER_CONFIG_HEADER=\"../../skia/config/SkUserConfig.h\" -DSK_HAS_JPEG_LIBRARY -DSK_SUPPORT_GPU=1 -DSK_GPU_WORKAROUNDS_HEADER=\"gpu/config/gpu_driver_bug_workaround_autogen.h\" -DSK_BUILD_FOR_MAC -DSK_METAL -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DUSE_CHROMIUM_ICU=1 -DU_STATIC_IMPLEMENTATION -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DUCHAR_TYPE=uint16_t -I../.. -Igen -I../../third_party/libwebp/src -I../../third_party/skia -I../../third_party/boringssl/src/include -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../third_party/khronos -I../../gpu -I../../third_party/mesa_headers -fno-strict-aliasing -fstack-protector -fcolor-diagnostics -fmerge-all-constants -fcrash-diagnostics-dir=../../tools/clang/crashreports -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -fcomplete-member-pointers -arch x86_64 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -Xclang -fdebug-compilation-dir -Xclang . -no-canonical-prefixes -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wextra-semi -Wunguarded-availability -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-ignored-pragma-optimize -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-sizeof-array-div -O2 -fno-omit-frame-pointer -gdwarf-4 -g1 -isysroot ../../../../../../../Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.10.0 -fvisibility=hidden -Xclang -add-plugin -Xclang find-bad-constructs -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -std=c++14 -stdlib=libc++ -fobjc-call-cxx-cdtors -Wobjc-missing-property-synthesis -fno-exceptions -fno-rtti -fvisibility-inlines-hidden -include obj/components/viz/common/metal_context_provider/precompile.h-mm -c ../../components/viz/common/gpu/metal_api_proxy.mm -o obj/components/viz/common/metal_context_provider/metal_api_proxy.o
../../components/viz/common/gpu/metal_api_proxy.mm:224:17: error: auto property synthesis will not synthesize property 'hasUnifiedMemory' declared in protocol 'MTLDevice' [-Werror,-Wobjc-protocol-property-synthesis]
@implementation MTLDeviceProxy
                ^
../../../../../../../Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLDevice.h:353:27: note: property declared here
@property (readonly) BOOL hasUnifiedMemory API_AVAILABLE(macos(10.15), ios(13.0));
                          ^
............

这个问题是mac系统版本导致的,目前chrome的编译对Mac OS X 10.15支持不太好,其主要还是支持Mac OS X 10.14,官方也是建议使用Mac OS X 10.14的SDK进行编译,这个问题可以看:

https://stackoverflow.com/questions/58794301/errors-when-building-electron-from-source-with-gn-and-ninja-on-mac-os-10-15
https://bugs.chromium.org/p/chromium/issues/detail?id=1011827

主要分为两步:
(1)添加对Mac OS X 10.14的SDK的支持,在src/electron/build/args/all.gn中添加mac_sdk_path = "$path_to_MacOSX10.14_sdk_folder" ,SDK可以在https://github.com/phracker/MacOSX-SDKs/tree/master/MacOSX10.14.sdk此处下载;
(2)更新components/viz/common/gpu/metal_api_proxy.mm文件,此处更新可以看https://github.com/chromium/chromium/commit/961e7821b9356445f48e5ce2c077c40c2c6b2309

从下面两个Issue看,chrome目前应该是已经支持了Mac OS X 10.15的SDK编译,但是我编译的electron-v7.1.5应该是还未更新支持。

https://bugs.chromium.org/p/chromium/issues/detail?id=1023913
https://bugs.chromium.org/p/chromium/issues/detail?id=973128

猜你喜欢

转载自www.cnblogs.com/bloglearning/p/12951559.html