libgwmodel 开发环境配置

开发环境首推 Docker 容器开发环境。开发者只需要创建一个容器后在容器中开发即可,无需过多担心环境的问题。如果不使用容器开发,可参考仓库中的 [GitHub Action Workflow](https://github.com/GWmodel-Lab/libgwmodel/blob/master/.github/workflows/main.yml) 编写的配置环境的步骤进行环境配置。

# Docker {#docker}

使用 Docker 容器开发环境,推荐安装以下软件:

1. VSCode。访问官网 https://code.visualstudio.com/ 下载最新版本。
2. Docker。根据系统的不同,有不同的安装方法:
   1. Windows 系统请访问 https://docs.docker.com/desktop/install/windows-install/ 根据教程安装。大致步骤为:安装 WSL2 (Windows Subsystem for Linux version 2)、安装 Linux 后台、下载 Docker Desktop 并安装。
   2. macOS 请访问 https://www.docker.com/products/docker-desktop/ 根据芯片类型选择下载。
   3. Linux 系统请访问 https://docs.docker.com/desktop/install/linux-install/ 并根据教程安装。
3. Git。根据系统的不同,有不同的安装方法:
   1. Windows 系统请访问 https://git-scm.com/downloads 下载安装包。
   2. macOS 使用 Homebrew 安装即可,命令为 `brew install git` 或者访问 https://git-scm.com/downloads 下载 macOS 系统的安装包。
   3. Linux 系统可以使用包管理器安装。例如 Ubuntu 下为 `apt install git` 。
4. R。请访问 http://gwmodel.whu.edu.cn/mirrors/CRAN/ 根据系统选择下载。下载后运行 R 语言命令 `install.packages("GWmodel")` 安装 GWmodel 包,用于生成测试数据。

软件安装完成后,将仓库 https://github.com/GWmodel-Lab/libgwmodel 克隆到本地计算机上,使用 VSCode 打开,VSCode 会提示是否在 Docker Container 中打开,选择是,让计算机构建开发容器。

如果还不熟悉 Git 的使用, 请下载 http://gwmodel.whu.edu.cn/docs/GitTutorial/GitTutorial.mkv 并观看 Git 培训录像。

# Linux (以Ubuntu为例)

1. 使用包管理器安装以下包: `build-essential`, `cmake`, `libopenblas-dev`
2. 编译安装 Armadillo,具体方法为

   ```bash
   mkdir armadillo && \
   wget -qO- http://sourceforge.net/projects/arma/files/armadillo-9.900.6.tar.xz | \
   tar -Jxf- --directory=armadillo --strip-components=1 && \
   cmake -B armadillo/build -S armadillo -DCMAKE_INSTALL_PREFIX=/usr -DDETECT_HDF5=OFF -DBUILD_SHARED_LIBS=OFF . && \
   cmake --build armadillo/build --config Release && \
   cmake --install armadillo/build
   ```
3. 安装 Catch2 ,具体方法为

   ```bash
   mkdir catch2 && \
   wget -qO- https://github.com/catchorg/Catch2/archive/refs/tags/v3.2.1.tar.gz | \
   tar -zxf- --directory=catch2 --strip-components=1 && \
   cmake -B catch2/build -S catch2 -DCMAKE_INSTALL_PREFIX=/usr . && \
   cmake --build catch2/build --config Release && \
   cmake --install catch2/build
   ```
4. 使用 CMake 构建项目,并进行开发

# Windows

## 推荐方式

在 Windows 上配置环境比较复杂,建议在 Windows 上安装 Docker 并使用容器开发。具体做法请参考 [Docker](#docker) 一节。

## 本地开发方式

如果因为 WSL 和 Docker 的问题无法在容器中开发,需要在本地开发,可以按照如下方式配置环境:

1. 安装 Visual Studio 并安装 C++ 桌面开发工具;
2. 访问 https://cmake.org/download/ 安装 CMake;
3. 安装依赖库 GSL 、 Armadillo 和 Catch2。建议使用 Vcpkg 包管理器进行安装。根据[相关博客](https://solarianprogrammer.com/2020/01/26/getting-started-gsl-gnu-scientific-library-windows-macos-linux/#gsl_installation_windows),首先需要安装 vcpkg 管理器,然后安装依赖库并指定 x64 版本。

   ```powershell
   git clone https://github.com/microsoft/vcpkg.git
   cd vcpkg
   .\bootstrap-vcpkg.bat
   .\vcpkg integrate install
   .\vcpkg install gsl:x64-windows openblas[threads]:x64-windows armadillo:x64-windows catch2:x64-windows
   ```
4. 克隆并转到内核库代码文件夹,使用 CMake 构建项目,构建时需要将上述库的安装目录作为参数传递给 CMake

   ```powershell
   cmake -B build -DCMAKE_TOOLCHAIN_FILE={vcpkg根目录}/scripts/buildsystems/vcpkg.cmake .
   cmake --build build
   ```

> 如果不使用 Vcpkg,那么需要自行编译安装依赖库,并在使用 CMake 配置时添加 `-DCMAKE_PREFIX_PATH` 参数指定依赖库安装路径。GWmodel Lab 内部提供了除 Boost 以外其他库的 Windows 预编译文件。请下载 NAS 的 `GWmodelS项目/usr` 文件夹。
> {.is-info}

# macOS

同样建议优先使用 Docker 容器。如果要使用本地开发环境,与 Windows 类似,同样需要安装 Armadillo、GSL、OpenBLAS、Boost 库。一般情况下,都可以使用 `brew install` 命令进行安装,如果不行请参考官方文档从源码编译安装。

1. 安装 Homebrew 包管理器。由于 Homebrew 使用了 GitHub 作为仓库,国内使用可能会遇到很多问题,因此安装方法请参考 [USTC 镜像源说明文档](http://mirrors.ustc.edu.cn/help/brew.git.html) 进行ovjd。
2. 使用 brew 安装下列依赖包

   ```bash
   brew install gsl armadillo catch2
   ```

然后使用 CMake 构建项目并编译即可。