如何从macOS上的源代码构建R?

but5z9lq  于 2023-03-05  发布在  Mac
关注(0)|答案(1)|浏览(181)

我看过很多关于Stack Overflow和其他地方的指南,用于从macOS上的源代码构建R * packages *。
但是在macOS上从源代码构建R * 本身需要哪些步骤?基于英特尔和ARM的Mac之间有何不同,即在苹果芯片(M1或M2)上的过程有何不同?
为了简单起见,我想将注意力集中在当前接收安全补丁的macOS版本上:大苏尔, Monterey ,文图拉。

我所尝试的

我在一台M1 Mac上运行Big Sur,获得了R源代码here的每日快照,并安装了here所列的必备工具,即Xcode和GNU Fortran。
目前为止我试过

$ curl -LO https://stat.ethz.ch/R/daily/R-patched.tar.gz
$ tar -xvf R-patched.tar.gz
$ cd R-patched
$ ./configure

错误

configure: error: No Fortran compiler found

所以我想需要额外的设置。

h79rfbju

h79rfbju1#

  • [由于这个答案是新的,我将感谢评论/测试...]*

有关此主题的权威信息来源是R Installation and Admistration manual("R-admin")和R for macOS Developers web page。此答案为"非官方"摘要,随着macOS和CRAN构建系统的发展,其中部分内容可能会"最终"过时。与往常一样,sudo风险自负。

背景

作为GNU项目的一部分,R使用Autotools构建系统,任何为Linux开发过软件的人都应该很熟悉。在大多数Linux系统上,安装R的依赖项都很容易,构建"就这样工作"。不幸的是,在macOS上根本不是这样,其中安装必备组件和正确配置构建可能需要仔细阅读文档。这使得许多macOS用户不愿尝试诊断和修补R本身的错误。
本指南试图尽可能简洁地介绍必要的步骤,而不涉及细节(无论是好是坏)。我的 * 希望 * 是一个简洁的指南将有助于使R-admin手册不那么吓人,特别是对于那些从未与Autotools交互过的人。

你需要什么

1.* * R源代码的副本.**如果您的系统上安装了Subversion(例如,可以尝试svn --version),那么您可以使用以下命令从R的Subversion仓库中检出最新的R-devel

$ svn checkout https://svn.r-project.org/R/trunk/ R-devel

或最新的R补丁

$ svn checkout https://svn.r-project.org/R/branches/R-x-y-branch/ R-patched

对于R版本4.2,将x-y替换为4-2;对于R版本4.1,将4-1替换为4-1,依此类推。否则,您可以下载并解压缩here中可用的每日快照之一。例如:

$ curl -LO https://stat.ethz.ch/R/daily/R-patched.tar.gz
$ tar -xvf R-patched.tar.gz

如果你打算和R一起编译推荐的软件包(例如,Matrix),那么你需要下载相应的源代码。R有一个脚本可以完成这个任务:

$ cd R-devel # or R-patched
$ ./tools/rsync-recommended

如果您下载了其中一个每日快照,那么脚本可能什么也不做,因为包含包源代码的压缩包通常已经包含在快照中了。
1.* * Xcode命令行工具。**这些工具包括Apple Clang和构建包含C和C++代码的库所需的其他工具。您可以使用以下命令安装最新版本:

$ sudo xcode-select --install

您可以从浏览器here获取旧版本。(CRAN的R 4.2版本一直使用13.1版本。)
1.* * GNU Fortran.**为避免二进制不兼容,请 * 不要 * 依赖Homebrew的gfortran。下载并解压缩CRAN使用的二进制文件,该文件可通过here获得。
对于基于Intel的Mac电脑,如下所示:

$ curl -LO https://mac.r-project.org/tools/gfortran-8.2-Mojave.dmg
$ sudo hdiutil attach gfortran-8.2-Mojave.dmg
$ sudo installer -pkg /Volumes/gfortran-8.2-Mojave/gfortran-8.2-Mojave/gfortran.pkg -target /
$ sudo hdiutil detach /Volumes/gfortran-8.2-Mojave

对于基于ARM的Mac,它看起来像:

$ curl -LO https://mac.r-project.org/tools/gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz
$ sudo tar -xvf gfortran-12.0.1-20220312-is-darwin20-arm64.tar.xz -C /
$ sudo ln -sfn $(xcrun --show-sdk-path) /opt/R/arm64/gfortran/SDK

1.* * XQuartz。**尽管不是强制性的,但XQuartz提供了X Window System的实现,X11()图形设备、View()-ing和edit()-ing Dataframe 以及其他图形功能都需要该实现。请从XQuartz网页here下载并解压缩最新的二进制文件:

$ curl -LO https://github.com/XQuartz/XQuartz/releases/download/XQuartz-2.8.5/XQuartz-2.8.5.pkg
$ sudo installer -pkg XQuartz-2.8.5.pkg -target /

1.* * MacTeX。**LaTeX发行版不是强制性的,但用于构建PDF版本的R手册和R包帮助页面和小插图。如果您的系统上有5 - 10 GiB的空闲空间,可以从CTAN here下载MacTeX发行版:

$ curl -LO https://mirror.ctan.org/systems/mac/mactex/MacTeX.pkg
$ sudo installer -pkg MacTeX.pkg -target /

如果您更喜欢(小得多的)BasicTeX,那么您必须安装额外的TeX包,但这超出了本问题的范围。
1.* * 附加外部库、头文件和程序。**其中一些是必需的(libzlibbz2liblzmalibpcre2-8libcurl)。其他选项是可选的,但CRAN也会将其安装在其构建系统上(libjpeglibpnglibtifflibcairopkg-config).还有GNU Texinfo,除了MacTeX之外,CRAN还需要它来构建R手册的PDF和Info版本,因为它们是以Texinfo格式编写的。
Simon Urbanek的recipes构建系统提供了一个方便的机制来安装那些尚未成为macOS一部分的组件。

$ git clone https://github.com/R-macos/recipes.git
$ cd recipes
$ sudo bash ./build.sh r-base-dev

Apple Silicon用户应将r-base-dev捆绑包中的组件安装在/opt/R/arm64下,而不是默认的/usr/local下:

$ git clone https://github.com/R-macos/recipes.git
$ cd recipes
$ export PREFIX=/opt/R/arm64
$ sudo -E bash ./build.sh r-base-dev

r-base-dev捆绑包不包括libreadlinelibpango,即使CRAN不使用它们,您也可能需要它们来实现相关功能。

$ sudo [-E] bash ./build.sh readline5

要显著改进图形设备中的文本渲染:

$ sudo [-E] bash ./build.sh pango

但是,请注意,Pango依赖于Meson构建系统,您可以使用pip3 install --user meson ninja获得它,但是mesonninja仍然必须在您的PATH上找到。

配置构建

在R源代码的顶层目录中,你会发现一个名为configure的脚本和一个名为config.site的文件。编辑config.site,使其包含下面的宏定义。
对于基于Intel的Mac:

CC=clang

OBJC=${CC}

CFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch x86_64 -falign-functions=64 -Wno-error=implicit-function-declaration"

CPPFLAGS=-I/usr/local/include

FC=/usr/local/gfortran/bin/gfortran

FFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -mtune=native"

LDFLAGS=-L/usr/local/lib

CXX=clang++

CXXFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch x86_64 -falign-functions=64"

R_LD_LIBRARY_PATH=/usr/local/lib

PKG_CONFIG=/usr/local/bin/pkg-config

PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/opt/X11/lib/pkgconfig:/opt/X11/share/pkgconfig

对于基于ARM的Mac:

CC=clang

OBJC=${CC}

CFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64 -Wno-error=implicit-function-declaration"

CPPFLAGS="-I/opt/R/arm64/include -I/usr/local/include"

FC=/opt/R/arm64/gfortran/bin/gfortran

FFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -mtune=native"

LDFLAGS="-L/opt/R/arm64/lib -L/usr/local/lib"

CXX=clang++

CXXFLAGS="-Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64"

R_LD_LIBRARY_PATH=/opt/R/arm64/lib:/usr/local/lib

PKG_CONFIG=/opt/R/arm64/bin/pkg-config

PKG_CONFIG_PATH=/opt/R/arm64/lib/pkgconfig:/opt/R/arm64/share/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/opt/X11/lib/pkgconfig:/opt/X11/share/pkgconfig

编辑完config.site后,请按如下所示运行configure,以大致像CRAN那样配置构建:

$ ./configure --enable-R-framework --enable-memory-profiling --x-includes=/opt/X11/include --x-libraries=/opt/X11/lib

请注意,如果您之前决定不运行./tools/rsync-recommended,则必须在命令行中添加--without-recommended-packages标志。在任何情况下,如果运行正常,则应该看到输出以类似于下面的摘要结尾:

R is now configured for aarch64-apple-darwin20.6.0

Source directory:            ../R-4.2
Installation directory:      /Library/Frameworks

C compiler:                  clang  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64 -Wno-error=implicit-function-declaration
Fortran fixed-form compiler: /opt/R/arm64/gfortran/bin/gfortran -fno-optimize-sibling-calls -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -mtune=native

Default C++ compiler:        clang++ -std=gnu++14  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64
C++11 compiler:              clang++ -std=gnu++11  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64
C++14 compiler:              clang++ -std=gnu++14  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64
C++17 compiler:              clang++ -std=gnu++17  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64
C++20 compiler:              clang++ -std=gnu++20  -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -arch arm64 -falign-functions=64
Fortran free-form compiler:  /opt/R/arm64/gfortran/bin/gfortran -fno-optimize-sibling-calls -Wall -g -O2 -pedantic -mmacosx-version-min=11.0 -mtune=native
Obj-C compiler:        clang -g -O2 -fobjc-exceptions

Interfaces supported:        X11, aqua
External libraries:          pcre2, readline, curl
Additional capabilities:     PNG, JPEG, TIFF, NLS, cairo, ICU
Options enabled:             framework, shared BLAS, R profiling, memory profiling

Capabilities skipped:
Options not enabled:

Recommended packages:        yes

在查阅./configure --help中的文档和R-admin中更详细的文档后,您可以决定修改传递给configure的标志。值得注意的是,如果您不打算在GUI(如www.example.com或RStudio.app)中运行R,则可以将--enable-R-framework替换为--enable-R-shlib。在这种情况下,您可以使用--prefix=设置安装前缀。R.app or RStudio.app. In that case, you can set the installation prefix with --prefix= , to install R under a directory different from the default /usr/local .
如果您已经配置了并且想要使用不同的标志重新配置,那么请从清理构建目录开始:

$ make distclean

R号楼

只需运行:

$ make

这将构建R,然后是所有推荐的包。要额外构建PDF或Info版本的手册,您还可以运行:

$ make pdf
$ make info

假设你有必要的工具,如上所述,现在你应该可以直接从build目录运行R了:

$ bin/R --version

检查内部版本

在安装R之前,建议您运行R的测试:

$ make check-devel

如果测试失败,则可以通过检查tests子目录中的任何.fail文件来诊断错误。

安装R

只需运行:

$ sudo make install

并且可选地:

$ sudo make install-pdf
$ sudo make install-info

如果使用--enable-R-framework进行配置,则可能需要将/Library/Frameworks/R.framework/Resources/bin添加到PATH中,以便可以在shell中运行R,而无需指定其位置。

卸载R

只需运行:

$ sudo make uninstall

相关问题