如何在macOS High Sierra源码安装R/RStudio并添加--mretpoline缓解Spectre 2?
-mretpoline on macOS High Sierra for Spectre v2 Mitigation Got it, let's break this down step by step—you're on the right track with compiling from source, and the key here is passing the -mretpoline flag to your compiler during the build process, even if it's not explicitly called out in R's configure or makefile templates.
For R
R's build system respects standard compiler environment variables, so you don't need to edit any configure or makefile files directly. Here's what to do:
Set compiler flags before running
./configure
Open your terminal, navigate to your R source directory, and run these commands to export the necessary flags to your environment:export CFLAGS="-mretpoline ${CFLAGS:-}" export CXXFLAGS="-mretpoline ${CXXFLAGS:-}" export LDFLAGS="-mretpoline ${LDFLAGS:-}"The
${VAR:-}syntax ensures you don't overwrite any existing flags you might have set previously.Run configure as usual
Now execute the configure script with any custom options you need (like specifying an install path):./configure [--prefix=/path/to/your/r/install]Verify the flag was picked up
After configure finishes, check the generatedMakeconffile (not the.intemplate) in the source directory. Look for lines starting withCFLAGS =orCXXFLAGS =—you should see-mretpolineincluded in the list of flags.Compile and install
Proceed with the standard build steps:make make install
For RStudio
RStudio uses CMake for its build system, so you'll pass the flag either via environment variables or directly in the CMake command:
Option 1: Use environment variables (same as R)
Before running CMake, export the same flags to your environment:export CFLAGS="-mretpoline ${CFLAGS:-}" export CXXFLAGS="-mretpoline ${CXXFLAGS:-}"Then run CMake with your usual options (make sure you've installed prerequisites like Qt first, per RStudio's build docs):
cmake -DCMAKE_INSTALL_PREFIX=/path/to/your/rstudio/install ..Option 2: Pass flags directly to CMake
If you prefer to set them explicitly in the CMake command, use these variables:cmake -DCMAKE_C_FLAGS="-mretpoline" -DCMAKE_CXX_FLAGS="-mretpoline" -DCMAKE_INSTALL_PREFIX=/path/to/your/rstudio/install ..Compile and install
Finish with the standard build commands:make make install
Quick Checks to Ensure It Worked
- For R: Open your installed R and run
system("echo $CFLAGS", intern=TRUE)or check the compiler info withsessionInfo()—you should see references to your compiler and the-mretpolineflag if it was applied correctly. - For RStudio: The most reliable way is confirming you passed the flags during the CMake step, but you can also check the binary's build context with
otool -l /path/to/rstudio/bin/rstudio | grep -A 10 LC_VERSION_MIN_MACOSXto verify the build environment matches your setup.
A quick note: Make sure your installed LLVM/GCC version actually supports -mretpoline—since your UCL professor mentioned it's deployed in both, High Sierra's updated default clang or a homebrew-installed GCC should handle it fine. If you run into compiler errors about an unrecognized flag, you might need to update your compiler first.
内容的提问来源于stack exchange,提问作者ginjaemocoes

