前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >​Substrate 环境安装提速文档

​Substrate 环境安装提速文档

作者头像
MikeLoveRust
发布2019-08-19 12:00:21
1.1K0
发布2019-08-19 12:00:21
举报

Substrate 环境安装提速文档(Mike版,仅限Debian/Ubuntu Linux 和 Mac brew)

================================

这是一份提速文档:)

这是一篇非官方的 Substrate 环境安装文档,因为我发现 Substrate 官方的安装脚本中有以下几个问题:

  1. 执行了一些重复工作
  2. 对网络要求较高(可能需要fq)
  3. 没有使用缓存

对于咋们墙内的同学来说,完成一次安装,就像经历了一次地狱。

下面切入正题。

官方的一键安装脚本是这个:

代码语言:javascript
复制
curl https://getsubstrate.io -sSf | bash -s

我们看一下这个脚本里面什么内容:

代码语言:javascript
复制
if [[ "$OSTYPE" == "linux-gnu" ]]; then
  if [[ `whoami` == "root" ]]; then
    MAKE_ME_ROOT=
  else
    MAKE_ME_ROOT=sudo
  fi

  if [ -f /etc/redhat-release ]; then
    echo "Redhat Linux detected."
    echo "This OS is not supported with this script at present. Sorry."
    echo "Please refer to https://github.com/paritytech/substrate for setup information."
  elif [ -f /etc/SuSE-release ]; then
    echo "Suse Linux detected."
    echo "This OS is not supported with this script at present. Sorry."
    echo "Please refer to https://github.com/paritytech/substrate for setup information."
  elif [ -f /etc/arch-release ]; then
    echo "Arch Linux detected."
    $MAKE_ME_ROOT pacman -Syu --needed cmake gcc openssl-1.0 pkgconf git clang
    export OPENSSL_LIB_DIR="/usr/lib/openssl-1.0";
    export OPENSSL_INCLUDE_DIR="/usr/include/openssl-1.0"
  elif [ -f /etc/mandrake-release ]; then
    echo "Mandrake Linux detected."
    echo "This OS is not supported with this script at present. Sorry."
    echo "Please refer to https://github.com/paritytech/substrate for setup information."
  elif [ -f /etc/debian_version ]; then
    echo "Ubuntu/Debian Linux detected."
    $MAKE_ME_ROOT apt update
    $MAKE_ME_ROOT apt install -y cmake pkg-config libssl-dev git gcc build-essential clang libclang-dev
  else
    echo "Unknown Linux distribution."
    echo "This OS is not supported with this script at present. Sorry."
    echo "Please refer to https://github.com/paritytech/substrate for setup information."
  fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
  echo "Mac OS (Darwin) detected."

  if ! which brew >/dev/null 2>&1; then
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  fi

  brew install openssl cmake llvm
elif [[ "$OSTYPE" == "freebsd"* ]]; then
  echo "FreeBSD detected."
  echo "This OS is not supported with this script at present. Sorry."
  echo "Please refer to https://github.com/paritytech/substrate for setup information."
else
  echo "Unknown operating system."
  echo "This OS is not supported with this script at present. Sorry."
  echo "Please refer to https://github.com/paritytech/substrate for setup information."
fi

if ! which rustup >/dev/null 2>&1; then
  curl https://sh.rustup.rs -sSf | sh -s -- -y
  source ~/.cargo/env
  rustup default stable
else
  rustup update
fi

function install_substrate {
  g=`mktemp -d`
  git clone https://github.com/paritytech/substrate $g
  pushd $g
  ./scripts/init.sh
  ./scripts/build.sh
  cargo install --force --path . substrate
  popd
}

if [[ "$1" == "--fast" ]]; then
  echo "Skipped cargo install of 'substrate' and 'subkey'"
  echo "You can install manually by cloning the https://github.com/paritytech/substrate repo,"
  echo "building the Wasm, and using cargo to install 'substrate' and 'subkey' from the repo path."
else 
  cargo install --force --git https://github.com/paritytech/substrate subkey
  install_substrate
fi

f=`mktemp -d`
git clone https://github.com/paritytech/substrate-up $f
cp -a $f/substrate-* ~/.cargo/bin
cp -a $f/polkadot-* ~/.cargo/bin

echo "Run source ~/.cargo/env now to update environment"

简单分析一下后,可以整理出一个更高效的安装步骤。

1. 安装系统依赖包

Linux

代码语言:javascript
复制
sudo apt update
sudo apt install -y cmake pkg-config libssl-dev git gcc build-essential clang libclang-dev

Mac

代码语言:javascript
复制
brew install openssl cmake llvm

2. 安装 Rust 工具链(如果之前已经装过 Rust 工具链,此步骤可跳过)

代码语言:javascript
复制
curl https://sh.rustup.rs -sSf | sh -s -- -y
source ~/.cargo/env
rustup default stable
rustup update nightly

可以看到,安装后,默认还是用的 stable 版本。这里提醒一下,如果之前装过 Rust 工具链,按官方的脚本走,会重做一次 rustup update。开发 substrate,不一定非要用最新的 Rust 版本(但是太老的估计也不行),nightly 版本几乎每日构建,所以你每执行一次 rustup update,就可能会重新下载新的构建版本,比较费时。

安装好了 rust 编译器后,依据网络流畅度,我们可以修改一下包管理器源地址为国内 rustcc 社区提供的源地址,把下面内容填充到你的 ~/.cargo/config 文件中(没有就创建一个):

代码语言:javascript
复制
[source.crates-io]
replace-with = "rustcc"

[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

后面的源码编译就会使用这个源地址。

3. 安装 substrate 的基础组件

代码语言:javascript
复制
假设我们工作目录是 ~/works/

3.1 安装 nightly 版本的 wasm32 target,执行

cd ~/works/
rustup target add wasm32-unknown-unknown --toolchain nightly

3.2 下载 wasm-gc 源码,安装

cd ~/works/
git clone https://github.com/alexcrichton/wasm-gc
cd wasm-gc
cargo +nightly build --release --target-dir=../nightly_target
cp -af ../nightly_target/release/wasm-gc ~/.cargo/bin

3.3 (这一步可选)下载 substrate 代码

git clone https://github.com/paritytech/substrate

3.4 (这一步可选)进入下载的 substrate 代码目录,编译安装 substrate 工具和 subkey 工具

cd ~/works/substrate
cargo build --release --target-dir=../target
cp -af ../target/release/substrate ~/.cargo/bin

cargo build -p subkey --release --target-dir=../target
cp -af ../target/release/subkey ~/.cargo/bin

说明:

  1. 在官方提供的脚本中,把 substrate 代码下载下来后,执行了一次,./scripts/init.sh,这里面又会做一次更新 Rust stable 版和 nightly 版的工作,浪费了很多时间;
  2. 上述指令中,注意区分 wasm-gc 是 nightly 版本编译的,substrate 和 subkey 是用 stable 版本编译的;
  3. 编译时,指定 --target-dir 可以充分共享编译中间文件(缓存);
  4. 下载代码,手动 build, 然后 cp,这样的方式,主要是可以充分利用编译缓存(这个缓存也是可以给其它项目用的,可以整台电脑都共用这两个缓存)。而直接 cargo install 并不能充分利用这种缓存机制。

4. 安装 substrate 工程辅助脚本

代码语言:javascript
复制
git clone https://github.com/paritytech/substrate-up
cp -af substrate-up/substrate-* ~/.cargo/bin
cp -af substrate-up/polkadot-* ~/.cargo/bin

环境和工具安装完毕。


如何加快我们自己的 substrate runtime 工程编译

====================================

官方 substrate-package 仓库地址:

https://github.com/substrate-developer-hub/substrate-package

官方给出的一个 node 模板的相关操作指令如下:

代码语言:javascript
复制
# Building

Install Rust:

```bash
curl https://sh.rustup.rs -sSf | sh
```

Install required tools:

```bash
./scripts/init.sh
```

Build the WebAssembly binary:

```bash
./scripts/build.sh
```

Build all native code:

```bash
cargo build

如果我们前面已经完成了 substrate 的环境安装,其实后面只需要执行第 3 步和第 4 步,于是可以优化成下面的指令:

代码语言:javascript
复制
假设我们的 node 模板代码在 ~/works/ 下。代码目录为  my_substrate_node

cd ~/works/my_substrate_node/

编译 wasm 文件

./scripts/build.sh

编译 native node

cargo build --target-dir=../target
或
cargo build --release --target-dir=../target

编译出的二进制文件在 ../target/ 中。

这样能充分利用前面的缓存,使得编译可以大大加速。如果前面没有缓存,这样操作,也可以在不同的项目间共享编译缓存。

完结。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Rust语言学习交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档