Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"hostRequirements": {
"cpus": 4
},
"runArgs": [
"--cap-add=SYS_PTRACE"
],
"customizations": {
"vscode": {
"settings": {
Expand All @@ -12,8 +15,11 @@
"editor.detectIndentation": false,
"r.lsp.diagnostics": false,
"r.plot.useHttpgd": true,
"r.rterm.linux": "/workspaces/r-dev-env/scripts/launch_r.sh",
"r.rpath.linux": "/usr/bin/R",
"r.rterm.linux": "/usr/bin/R",
"terminal.integrated.env.linux": {
"R_RPATH_LINUX": "${config:r.rpath.linux}"
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this, see below for getting the current version of R.

"terminal.integrated.sendKeybindingsToShell": true,
"svn.multipleFolders.enabled": true,
"workbench.editorAssociations": {
Expand All @@ -28,9 +34,10 @@
"johnstoncode.svn-scm",
"ms-vscode.cpptools",
"MS-vsliveshare.vsliveshare",
"natqe.reload"
"natqe.reload",
"vadimcn.vscode-lldb"
]
}
},
"postCreateCommand": "find . -wholename '*.git*' -type d -prune -o -type f -exec chown vscode:vscode {} \\; && sh /workspaces/r-dev-env/scripts/localscript.sh"
"postCreateCommand": "find . -wholename '.git*' -type d -prune -o -type f -exec chown vscode:vscode {} \\; && sh ./scripts/localscript.sh"
}
22 changes: 22 additions & 0 deletions .devcontainer/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug R (C debugging)",
"type": "cppdbg",
"request": "launch",
"program": "/workspaces/r-dev-env/scripts/launch_r.sh",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "R_RPATH_LINUX",
"value": "${config:r.rpath.linux}"
}
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not need this here either. The path to R is only needed when we launch the terminal. The debugger identifies the running R process with the process ID (as returned by Sys.getpid() in R).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the launch.json as I do in #261

"externalConsole": false,
"MIMode": "lldb"
}
]
}
25 changes: 20 additions & 5 deletions docs/tutorials/building_r.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,22 @@ mkdir -p $BUILDDIR
cd $BUILDDIR
```

**5) Configure the build**
**5) Set CFLAGS (Optional—For Debugging C Code)**

- **This step is optional and recommended for those who want to debug C code.**
- Set the `CFLAGS` environment variable before running configure:

```bash
CFLAGS="-g -O0"
```

- These flags modify the configuration defined in the next step,
so that when R is built, C code will be compiled with debugging
symbols (`-g`) and compiler optimizations will be disabled
(`-O0`) so that the structure of the code closely matches the
original source.

**6) Configure the build**

- After we change directory, we must run the configure script from the source
directory. This step takes ~1 minute on the codespace.
Expand All @@ -80,7 +95,7 @@ $TOP_SRCDIR/configure --with-valgrind-instrumentation=1

![alt text](../assets/rdev7.png)

**6) Build R**
**7) Build R**

Having configured R, we run `make` to build R. This take 5-10 minutes on the
codespace.
Expand All @@ -89,7 +104,7 @@ codespace.
make
```

**7) Check R**
**8) Check R**

Check that the build of R passes R's standard checks:

Expand All @@ -101,7 +116,7 @@ This takes a couple of minutes in the codespace. The check will stop with a
error message if any of the tests fail. If this happens, see [SVN
Help](./svn_help.md) for how to revert to a version that passes check.

**8) Make R terminals use the built R**
**9) Make R terminals use the built R**

Run the `which_r` script to set which R to use for R terminals in VSCode. When
prompted, enter the number corresponding to `r-devel`
Expand All @@ -125,7 +140,7 @@ built![^1]
selected version is saved in the VSCode settings, so will be saved when you stop
and restart the codespace.

**9) Make contributions**
**10) Make contributions**

- After having built the current development version of R, we can now make
changes to the source code and contribute to the project.
Expand Down
6 changes: 6 additions & 0 deletions scripts/allow_ptrace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <sys/prctl.h>

__attribute__((constructor))
static void allow_ptrace(void) {
prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
}
12 changes: 12 additions & 0 deletions scripts/launch_r.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
# Preload helper for ptrace
export LD_PRELOAD="$(dirname "$0")/allow_ptrace.so"

# VS Code sets the env var R_RPATH_LINUX to the path from r.rpath.linux
if [ -n "${R_RPATH_LINUX:-}" ] && [ -x "$R_RPATH_LINUX" ]; then
R_BINARY="$R_RPATH_LINUX"
else
R_BINARY="/usr/bin/R"
fi

exec "$R_BINARY" "$@"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to do it like this. Define launch_r.sh as

!/bin/bash
export LD_PRELOAD=/workspaces/r-dev-env/scripts/allow_ptrace.so
exec /usr/bin/R "$@"

then update which_r to replace the old path with the new path as required, e.g. (untested)

sed -i "s|^exec .*R|exec $selected_version|" launch_r.sh

This would be a new step in addition to updating "r.rpath.linux" in the VS Code settings.

For initial testing, just change the path manually, so you don't have to get everything working at once!

18 changes: 14 additions & 4 deletions scripts/localscript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ local_script(){
WORK_DIR=$PWD
VSCODE_DIR="$WORK_DIR/.vscode"
DEVCONTAINER_JSON="$WORK_DIR/.devcontainer/devcontainer.json"
SCRIPTS_DIR="$WORK_DIR/scripts"

# Create patch directory in workspace root ($PWD at start)
PATCHDIR="$WORK_DIR/patches"
mkdir -p $PATCHDIR
mkdir -p $VSCODE_DIR

# Copy the which_r and set_build_r function definitions to .bashrc
cat $WORK_DIR/scripts/which_r.sh >> ~/.bashrc
cat $WORK_DIR/scripts/set_build_r.sh >> ~/.bashrc
cat $SCRIPTS_DIR/which_r.sh >> ~/.bashrc
cat $SCRIPTS_DIR/set_build_r.sh >> ~/.bashrc

# Copy over the welcome message script to be run when bash terminal starts
cat $WORK_DIR/scripts/welcome_msg.sh >> ~/.bashrc
cat $SCRIPTS_DIR/welcome_msg.sh >> ~/.bashrc

#bash ~/.bashrc

# Remove git directory if it exists
rm -rf .git

Expand All @@ -38,3 +38,13 @@ fi

# Run the main function
local_script

# Remove the .vscode/settings.json file if it exists so that
# it does not interfere with the devcontainer.json
rm -f "$WORK_DIR/.vscode/settings.json"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not remove this file, this is the one that is being updated on line 32!


# 1. Build the ptrace helper library
gcc -shared -fPIC -o "$SCRIPTS_DIR/allow_ptrace.so" "$SCRIPTS_DIR/allow_ptrace.c"

# 2. Mark the wrapper executable
chmod +x "$SCRIPTS_DIR/launch_r.sh"
2 changes: 1 addition & 1 deletion scripts/which_r.sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only change what is necessary in this file: edit line 63 so it only changes r.rpath.linux and add the new call to sed to update launch_r.sh. Otherwise you risk introducing regressions, e.g. line 67 was added (by you!) in #255 to instruct people how to reload the help to use the version corresponding to the R version that has been switched to.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ which_r() {
fi

# Update settings.json with the chosen R path
updated_settings_data=$(cat "$settings_file_path" | jq --arg subdir "$selected_version" '."r.rterm.linux"=$subdir | ."r.rpath.linux"=$subdir')
updated_settings_data=$(cat "$settings_file_path" | jq --arg subdir "$selected_version" '."r.rpath.linux"=$subdir')
echo "$updated_settings_data" > "$settings_file_path"

echo "R terminal will now use version: $selected_version"
Expand Down