Emscripten下载和安装

注解

如果您不喜欢使用emsdk下载二进制文件,也可以 build Emscripten from source

注解

There are additional ways to install Emscripten than the instructions below, for example, using Homebrew on MacOS, the package manager on your linux distro. However, the emsdk is the only officially supported way to use Emscripten that is supported by the Emscripten project, and the only one that we constantly test (emsdk CI, Emscripten GitHub CI, Chromium CI). (While we don’t officially support other ways of getting Emscripten, we definitely appreciate the efforts by third parties to package Emscripten for users’ convenience, and we’d like to help out, please get in touch if you are such a packager!)

安装说明

首先,检查下面的关于 Platform-specific notes ,然后安装必要的软件包。

Emscripten SDK (emsdk) 的核心驱动程序是 Python 脚本。 您可以通过以下方式首次获取它

# Get the emsdk repo
git clone https://github.com/emscripten-core/emsdk.git

# Enter that directory
cd emsdk

注解

你也可以不使用Git来获取emsdk,而是在 Github 上下载它.

运行下面的 emsdk 命令可以从Github上获取最新版的工具,并设置为 active :

# Fetch the latest version of the emsdk (not needed the first time you clone)
git pull

# Download and install the latest SDK tools.
./emsdk install latest

# Make the "latest" SDK "active" for the current user. (writes .emscripten file)
./emsdk activate latest

# Activate PATH and other environment variables in the current terminal
source ./emsdk_env.sh

注解

在 Windows下,使用 emsdk 代替 ./emsdk 命令,emsdk_env.bat 代替 source ./emsdk_env.sh 命令。

注解

git pull 将获取标签的当前列表,但是那里可能还没有最新的标签。您可以运行 ./emsdk update-tags 以直接更新标签列表。

如果改变了SDK的位置(例如:使用USB带到别的计算机), 需要从新运行 ./emsdk activate latestsource ./emsdk_env.sh 命令。

Emsdk install targets

在上面的描述中,我们要求emsdk安装并激活 latest,这是最新的标记发行版。这通常就是您想要的。

您还可以通过指定特定版本来安装它,例如,

./emsdk install 1.38.45

注解

When installing old versions from before the build infrastructure rewrite (anything before 1.38.33), you need to write something like ./emsdk install sdk-1.38.20-64bit (add sdk- and -64bit) as that was the naming convention at the time.

您也可以指定要使用的后端,fastcomp``upstream``(不指定后端,则使用当前默认值),例如,

# Get a specific version using the upstream backend.
./emsdk install latest-upstream

# Get a specific version using the fastcomp backend.
./emsdk install 1.38.45-fastcomp

There are also “tip-of-tree builds”, which are the very latest code that passes integration tests on Chromium CI. This is updated much more frequently than tagged releases, but may be less stable (we tag releases manually using a more careful procedure). Tip-of-tree builds may be useful for continuous integration that uses the emsdk (as Emscripten’s GitHub CI does), and you may want to use it in your own CI as well, so that if you find a regression on your project you can report it and prevent it from reaching a tagged release. Tip-of-builds may also be useful if you want to test a feature that just landed but didn’t reach a release yet. To use a tip-of-tree build, use the tot target, and note that you must specify the backend explicitly,

# Get a tip-of-tree using the upstream backend.
./emsdk install tot-upstream

# Get a tip-of-tree using the fastcomp backend.
./emsdk install tot-fastcomp

(In the above examples we installed the various targets; remember to also activate them as in the full example from earlier.)

Platform-specific notes

Windows

  1. Install Python 2.7.12 or newer (older versions may not work due to a GitHub change with SSL).

注解

Instead of running emscripten on Windows directly, you can use the Windows Subsystem for Linux to run it in a Linux environment.

macOS

If you use the Emscripten SDK it includes a bundled version of Python 3. Otherwise you will need to manually install and use Python 3.5 or newer.

These instructions explain how to install all the required tools. You can test whether some of these are already installed on the platform and skip those steps.

  1. Install the Xcode Command Line Tools. These are a precondition for git.
  • Install Xcode from the macOS App Store.
  • In Xcode | Preferences | Downloads, install Command Line Tools.
  1. Install git:
  1. Install cmake if you do not have it yet:

Linux

注解

Emsdk does not install any tools to the system, or otherwise interact with Linux package managers. All file changes are done inside the emsdk/ directory.

  • Python, CMake, and Java are not provided by emsdk. The user is expected to install these beforehand with the system package manager:

    # Install Python
    sudo apt-get install python3
    
    # Install CMake (optional, only needed for tests and building Binaryen)
    sudo apt-get install cmake
    
    # Install Java (optional, only needed for Closure Compiler minification)
    sudo apt-get install default-jre
    

注解

If you want to use your system’s Node.js instead of the emsdk’s, it may be node instead of nodejs, and you can adjust the NODE_JS attribute of your .emscripten file to point to it.

  • Git is not installed automatically. Git is only needed if you want to use tools from one of the development branches emscripten-incoming or emscripten-master:

    # Install git
    sudo apt-get install git-core
    

Verifying the installation

The easiest way to verify the installation is to compile some code using Emscripten.

You can jump ahead to the Emscripten教程, but if you have any problems building you should run through the basic tests and troubleshooting instructions in 验证Emscripten开发环境.

Updating the SDK

小技巧

You only need to install the SDK once! After that you can update to the latest SDK at any time using Emscripten SDK (emsdk).

Type the following in a command prompt

# Fetch the latest registry of available tools.
./emsdk update

# Download and install the latest SDK tools.
./emsdk install latest

# Set up the compiler configuration to point to the "latest" SDK.
./emsdk activate latest

# Activate PATH and other environment variables in the current terminal
source ./emsdk_env.sh

The package manager can do many other maintenance tasks ranging from fetching specific old versions of the SDK through to using the versions of the tools on GitHub (or even your own fork). Check out all the possibilities in the “How to” guides.

Uninstalling the Emscripten SDK

If you want to remove the whole SDK, just delete the directory containing the SDK.

It is also possible to remove specific tools in the SDK using emsdk.

Using the Docker image

The entire Emscripten SDK is also available in the form of a docker image. For example:

docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) \
  emscripten/emsdk emcc helloworld.cpp -o helloworld.js

See the Docker Hub page for more details and examples.