安装

以下所有说明适用于 Linux、macOS 和 Windows。

二进制文件

安装 LFortran 的推荐方法是使用 Conda。例如,按照你的平台的说明安装 Miniconda,安装 Conda。然后创建一个新环境(你可以选择任何名称,这里我们选择了lf)并激活它:

conda create -n lf
conda activate lf

然后通过以下方式安装 LFortran:

conda install lfortran -c conda-forge

现在 lf 环境有 lfortran 编译器可用,你可以通过执行 lfortran 启动交互式提示,或使用 lfortran -h 查看命令行选项。

Jupyter 内核是通过上面的命令自动安装的,所以在安装 Jupyter 本身之后:

conda install jupyter -c conda-forge

你可以通过执行以下命令来创建基于 Fortran 的 Jupyter 笔记本:

jupyter notebook

并选择 New->Fortran

从源代码构建

如果你只想自己或在包管理器(Spack、Conda、Debian 等)中安装 LFortran,建议使用此方法。源代码包含所有生成的文件,并且具有最小的依赖关系。

首先,我们必须安装依赖项,例如使用 Conda:

conda create -n lf python cmake llvmdev
conda activate lf

然后从 https://lfortran.org/download/ 下载源代码,例如:

wget https://lfortran.github.io/tarballs/dev/lfortran-0.9.0.tar.gz
tar xzf lfortran-0.9.0.tar.gz
cd lfortran-0.9.0

并构建:

cmake -DWITH_LLVM=yes -DCMAKE_INSTALL_PREFIX=`pwd`/inst .
make -j8
make install

This will install lfortran into inst/bin. It assumes that c++ and cc are available, which on Linux are typically the GNU C++/C compilers.

从 Git 构建

我们假设你安装了 C++ 编译器,以及 gitwget。在 Ubuntu 中,你还可以为 stacktraces 安装 binutils-dev

如果你没有安装 Conda,你可以在 Linux 上安装(在其他平台上类似):

wget --no-check-certificate https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p $HOME/conda_root
export PATH="$HOME/conda_root/bin:$PATH"

然后准备环境:

conda create -n lf -c conda-forge llvmdev=11.0.1 bison=3.4 re2c python cmake make toml
conda activate lf

克隆 LFortran git 存储库:

git clone https://github.com/lfortran/lfortran.git
cd lfortran

生成构建所需的文件(此步骤取决于 re2cbisonpython):

./build0.sh

现在的过程与从源 tarball 安装相同。例如在 Debug 模式下构建:

cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_LLVM=yes -DCMAKE_INSTALL_PREFIX=`pwd`/inst .
make -j8

运行测试:

ctest
./run_tests.py

运行交互式提示符:

./src/bin/lfortran

在 Windows 上使用 Visual Studio 从 Git 构建

安装 Visual Studio (MSVC),例如 2022 版本,可以免费下载社区版本:https://visualstudio.microsoft.com/downloads/ 。

使用来自 https://github.com/conda-forge/miniforge 的 Windows 安装程序安装 miniforge。

从桌面上启动 Miniforge Prompt。

在 shell 中,用以下方法初始化 MSVC 编译器:

call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd" -arch=x64

你可以选择通过以下方式测试 MSVC 是否工作:

cl /?
link /?

这两个命令都必须打印帮助信息(若干页)。

现在你可以下载并建立 LFortran:

git clone https://github.com/lfortran/lfortran.git
cd lfortran
conda env create -f environment_win.yml
conda activate lf
build0.bat
build1.bat

如果一切都编译好了,那么你就可以使用 LFortran,如下所示:

inst\bin\lfortran examples/expr2.f90
expr2.exe
inst\bin\lfortran

等等 。

注意:LFortran 目前使用 MSVC 的链接器程序(link),只有在运行上面的 MSVC bat 脚本时才能使用。如果你忘记激活它,LFortran 的链接就会失败。

注意:miniforge shell 似乎在运行某个版本的 git-bash(尽管它是cmd.exe),它有一些类似 unix 的文件系统挂载在 /usr,有几个命令可用,如lswhichgitvim。 由于这个原因,Conda 构建的 environment_win.yml 包含了所有需要的东西,包括 git

用 WSL 在 Windows上 从 Git 构建

  • 在 Windows 中搜索“打开或关闭 Windows 功能”。

  • 标记适用于Linux 的 Windows 子系统。

  • 按“确定”并重新启动计算机。

  • 到微软商店下载 Ubuntu 20.04,并启动它。

  • 运行下列程序语句。

wget  https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p $HOME/conda_root
export PATH="$HOME/conda_root/bin:$PATH"
  • 现在做以下工作来配置路径

sudo nano .bashrc
  • 然后转到文件底部,粘贴以下内容

export PATH="$HOME/conda_root/bin:$PATH"
  • 然后按 ctrl + O(保存),Enter(确认),ctrl + X(退出)

  • 之后,重新启动 Ubuntu

  • 运行以下内容

conda create -n lf -c conda-forge llvmdev=11.0.1 bison=3.4 re2c python cmake make toml
conda init bash
  • 再次重启 Ubuntu

conda activate lf
sudo apt update
sudo apt-get install build-essential
sudo apt-get install zlib1g-dev libzstd-dev
sudo apt install clang
  • 你可以使用 cd /mnt/[drive letter]/[windows location]将路径改为 Windows 位置。

  • 例如:cd mnt/c/Users/name/source/repos/

  • 现在克隆 LFortran 的 git 仓库

git clone https://github.com/lfortran/lfortran.git
cd lfortran
  • 运行以下命令

conda activate lf
./build0.sh
cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_LLVM=yes -DCMAKE_INSTALL_PREFIX=`pwd`/inst .\
make -j8
  • 如果一切都能编译,你可以使用 LFortran,如下所示

./src/bin/lfortran ./examples/expr2.f90
./expr2.out
  • 运行交互式提示符

./src/bin/lfortran
  • 运行测试

ctest
./run_tests.py

启用 Jupyter 内核

要安装 Jupyter 内核,还要安装以下 Conda 软件包:

conda install xeus=3.0.5 xeus-zmq=1.0.2 xtl nlohmann_json cppzmq

并通过 -DWITH_XEUS=yes 启用内核,然后安装到 $CONDA_PREFIX。比如:

cmake \
    -DCMAKE_BUILD_TYPE=Debug \
    -DWITH_LLVM=yes \
    -DWITH_XEUS=yes \
    -DCMAKE_PREFIX_PATH="$CONDA_PREFIX" \
    -DCMAKE_INSTALL_PREFIX="$CONDA_PREFIX" \
    .
cmake --build . -j4 --target install

要使用它,请安装 Jupyter(conda install jupyter)并测试是否找到 LFortran 内核:

jupyter kernelspec list --json

然后启动 Jupyter notebook,如下所示:

jupyter notebook

单击 New - > Fortran。启动 jupyter LFortran 终端控制台:

jupyter console --kernel=fortran

使用 Nix 从 Git 构建

确保精确环境和依赖关系的方法之一是使用 nix。这将确保系统的依赖性不会干扰到开发环境。如果你愿意,你可以在 nix-shell 环境下报告 bug,以方便别人复制。

带有 root 权限

我们从获得 nix 开始。下面的多用户安装将在任何装有 Linux 发行版、MacOS 或 Windows(通过 WSL)的机器上工作:

sh <(curl -L https://nixos.org/nix/install) --daemon

不带有 root 权限

如果你想不给nix提供机器的root权限,在Linux发行版上,我们可以使用 nix-portable.。

wget https://github.com/DavHau/nix-portable/releases/download/v003/nix-portable

现在只要在所有 nix-shell 命令前加上 NP_RUNTIME=bwrap . /nix-portable。所以:

# Do not
nix-shell --run "bash"
# Do
NP_RUNTIME=bwrap ./nix-portable nix-shell --run "bash"

开发

现在进入开发环境:

nix-shell --run "bash" --cores 4 -j4 --pure ci/shell.nix

--pure 标志确保在环境中不使用系统依赖。

构建步骤与 ci 相同:

./build0.sh
./build1.sh

要把编译环境从 gcc(默认)改为 clang,我们可以使用 --argstr

nix-shell --run "bash" --cores 4 -j4 --pure ci/shell.nix --argstr clangOnly "yes"

关于依赖性的说明

我们鼓励终端用户(和发行版)使用来自 https://lfortran.org/download/的tarball,它只依赖于LLVM、CMake和C++编译器。

这个 tarball 是由我们的 CI(持续集成)自动生成的,包含一些自动生成的文件:解析器、AST 和 ASR 节点,由 ASDL 翻译器生成(需要 Python)。

在开发 LFortran 本身时要使用来自 git 的指令。

不使用 Conda 的用户注意

以下是在开发模式下安装此版本库的必要依赖,

堆栈跟踪

LFortran 可以在出现未处理的异常时打印堆栈跟踪,也可以在任何编译器错误时使用 --show-stacktrace 选项。这对开发编译器本身很有帮助,可以看到 LFortran 中的问题所在。默认情况下,堆栈跟踪支持是关闭的,要启用它,需要在每个平台上按照下面的说明安装先决条件后,用-DWITH_STACKTRACE=yes cmake 选项编译 LFortran。

Ubuntu

在 Ubuntu 系统,apt install binutils-dev

macOS

如果你在 macOS 上使用默认的 Clang 编译器,那么堆栈跟踪应该正好在基于 Intel 和 M1 的 macOS 上工作(CMake 构建系统自动调用 dsymtuil 工具和我们的 Python 脚本来存储调试信息,更多细节见 src/bin/CMakeLists.txt)。如果不能工作,请报告一个错误。

如果你不喜欢默认的方式,另一个选择是使用bintutils。为此,首先安装 Spack,然后:

spack install binutils
spack find -p binutils

最后一条命令将显示已安装的 binutils 软件包的完整路径。把这个路径添加到你的 shell 配置文件中,例如:

export CMAKE_PREFIX_PATH_LFORTRAN=/Users/ondrej/repos/spack/opt/spack/darwin-catalina-broadwell/apple-clang-11.0.0/binutils-2.36.1-wy6osfm6bp2323g3jpv2sjuttthwx3gd

并使用 -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH_LFORTRAN;$CONDA_PREFIX" cmake 选项编译 LFortran。$CONDA_PREFIX 是在你使用 Conda 安装了一些其他的依赖项(如 llvm)的情况下出现的,否则你可以把它删除。