> 文档中心 > Mac搭建OpenHarmonyOS(开源鸿蒙系统)编译环境

Mac搭建OpenHarmonyOS(开源鸿蒙系统)编译环境


从编译脚本上看是支持的mac的

case $(uname -s) in    Darwin) HOST_DIR="darwin-x86" HOST_OS="mac" ;;    Linux) HOST_DIR="linux-x86" HOST_OS="linux" ;;    *) echo "Unsupported host platform: $(uname -s)" RET=1 exit $RETesac
  • mac平台的只能在mac上编译(目前是)
  • Linux上可以交叉编译出win
    • 编译命令默认也是会编译出Linux和win两个平台的sdk

mac上有一些要求,比如必须安装xcode。
默认最小版本 10.12,但是高于11不行。

_sdk_min_from_env = getenv("FORCE_MAC_SDK_MIN")declare_args() {  # Minimum supported version of the Mac SDK.  if (_sdk_min_from_env == "") {    mac_sdk_min = "10.12"  } else {    mac_sdk_min = _sdk_min_from_env  }}

-下面脚本只匹配的 10和11(目前是这样的,当前时间:20220528)

sdk_dir = os.path.join( str(out.rstrip(), encoding="utf-8"), 'Platforms/MacOSX.platform/Developer/SDKs')    # Xcode must be installed, its license agreement must be accepted, and its    # command-line tools must be installed. Stand-alone installations (in    # /Library/Developer/CommandLineTools) are not supported.    # https://bugs.chromium.org/p/chromium/issues/detail?id=729990#c1    file_path = os.path.relpath("/path/to/Xcode.app")    if not os.path.isdir(sdk_dir) or not '.app/Contents/Developer' in sdk_dir: raise SdkError('Install Xcode, launch it, accept the license ' +  'agreement, and run `sudo xcode-select -s %s` ' % file_path +  'to continue.')    sdks = [re.findall('^MacOSX(1[0,1]\.\d+)\.sdk$', s) for s in     os.listdir(sdk_dir)]    sdks = [s[0] for s in sdks if s]  # [['10.5'], ['10.6']] => ['10.5', '10.6']    sdks = [s for s in sdks  # ['10.5', '10.6'] => ['10.6']     if list(parse_version(s)) >= list(parse_version(min_sdk_version))]

环境

  • MacOS:10.12 - 11应该都可以,系统版本12目前不行

  • Xcode:必须安装

    • 我的 Version 11.5 (11E608c)
  • gitee账号

    • https://gitee.com/
  • git

    • 参考后文
  • git-lfs

    • 参考后文
  • python3

    • 参考后文
  • repo

    • 参考后文
  • node

    • 参考后文
  • Java

    • 参考后文

Ubuntu

需要 Ubuntu 环境的可以参考下面链接

https://www.jianshu.com/p/24be266a1c4e

Brew

如果没有brew,百度安装

如果有提示,请按照提示操作

重置brew.git

cd "$(brew --repo)"git remote set-url origin https://github.com/Homebrew/brew.git

重置核心软件仓库

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"git remote set-url origin https://github.com/Homebrew/homebrew-core.git

重置Homebrew cask软件仓库

cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-caskgit remote set-url origin https://github.com/Homebrew/homebrew-cask

python

  • Version:3+

https://www.python.org/

brew install python3

git

gitee账号信息

  • 生成公私钥
ssh-keygen -t rsa -C "message"
  • 注册gitee

  • 将本机ssh公钥放置gitee上

install git

  • mac
brew install git

configure git

git config --global user.name "binny"git config --global user.email "694285349@qq.com"git config --global credential.helper store

install git-lfs

https://gitee.com/mirrors/git-lfs?_from=gitee_search

install repo

mkdir -p ~/.binPATH="${HOME}/bin:${PATH}"curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > ~/.bin/repochmod a+rx ~/.bin/repopip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

download master

Download

repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify

Sync

repo sync -c -d -j8 --force-sync --fail-fast -q --no-tags --prune ; repo forall -j8 -c "git lfs pull"
  • 或者
repo sync -c
  • 同步大文件,好像是编译器之类的,没仔细看日志
repo forall -c 'git lfs pull'

这一步有输出警告,应该不影响,输出警告的文件:

vim /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/resource_tracker.py
finally: # all processes have terminated; cleanup any remaining resources for rtype, rtype_cache in cache.items():     if rtype_cache:  try:      warnings.warn('resource_tracker: There appear to be %d '      'leaked %s objects to clean up at shutdown' %      (len(rtype_cache), rtype))  except Exception:      pass     for name in rtype_cache:  # For some reason the process which created and registered this  # resource has failed to unregister it. Presumably it has  # died.  We therefore unlink it.  try:      try:   _CLEANUP_FUNCS[rtype](name)      except Exception as e:   warnings.warn('resource_tracker: %r: %s' % (name, e))  finally:      pass

brew 安装工具包

我开始在 mac 上大环境的时候,也是对着 Linux 上的名字安装了一部分。缺少去网上搜吧,下图是我本机的,仅供参考(下面的不都是 ohos 需要的)

ohos % brew list==> Formulaebrotli      coreutils   git     icu4clibunistring    mpdecimal   openssl@1.1 readline    wgetc-ares      gdbm git-lfs     libidn2     libuvnode@12     pcre2sqlite      xzca-certificates gettext     gmp     libnghttp2  macos-term-size openjdk@17  python@3.9  tree
brew update
brew install wget
  • md5sum等
brew install coreutils

java环境

  • 没有的话,自己安装,这个我以前装过,装 Java 环境,好装,百度吧
brew install openjdk@17
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
java -version

prebuilts

  • 执行prebuilts,在源码根目录下执行,安装编译器及二进制工具(注意文件名和路径,同事给我的名字有的都不一样了)
sudo bash build/prebuilts_download.sh –no-check-certificatie-skip-ssl
  • 缺少啥就装啥。
sudo npm i -g nan

版本编译

  • 编译 SDK
sudo ./build.sh --product-name ohos-sdk
  • 单独安装的不支持
# Xcode must be installed, its license agreement must be accepted, and its# command-line tools must be installed. Stand-alone installations (in# /Library/Developer/CommandLineTools) are not supported.# https://bugs.chromium.org/p/chromium/issues/detail?id=729990#c1
  • 完成
[OHOS INFO] [28/28] STAMP obj/build/core/gn/build_ohos_sdk.stamp[OHOS INFO] c targets overlap rate statistics[OHOS INFO] subsystem    files NO.   percentage  builds NO.  percentage  overlap rate[OHOS INFO] graphic      8    0.1%   12    0.1%    1.50[OHOS INFO] hiviewdfx    4    0.0%    6    0.1%    1.50[OHOS INFO] multimedia   2    0.0%    3    0.0%    1.50[OHOS INFO] startup      2    0.0%    3    0.0%    1.50[OHOS INFO] global      13    0.2%   14    0.2%    1.08[OHOS INFO] third_party      3590    44.7%3597    44.6%   1.00[OHOS INFO] thirdparty3590    44.7%3597    44.6%   1.00[OHOS INFO] arkui     1395    17.4%1397    17.3%   1.00[OHOS INFO] aafwk39    0.5%   39    0.5%    1.00[OHOS INFO] ark  67    0.8%   67    0.8%    1.00[OHOS INFO] developtools61    0.8%   61    0.8%    1.00[OHOS INFO] securec     78    1.0%   78    1.0%    1.00[OHOS INFO] utils50    0.6%   50    0.6%    1.00[OHOS INFO][OHOS INFO] c overall build overlap rate: 1.00[OHOS INFO][OHOS INFO][OHOS INFO] sdk build success[OHOS INFO] cost time: 0:01:39=====build  successful=====
  • 编译成功后,如果有文件权限问题
sudo chmod -R 777 out
  • 位置
/Users/binny/ohos/out/sdk/ohos-sdk/darwin
=====build  successful=====2022-05-29 06:09:17++++++++++++++++++++++++++++++++++++++++ohos % tree -L 2 -d  /Users/binny/ohos/out/sdk/ohos-sdk/Users/binny/ohos/out/sdk/ohos-sdk└── darwin    ├── ets    ├── js    ├── previewer    └── toolchains5 directoriesohos %