Skip to content

Commit

Permalink
use build ARGUMENT to make workspace dir arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
georglauterbach committed May 4, 2024
1 parent 83662e6 commit e605bd3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
34 changes: 26 additions & 8 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ SHELL [ "/bin/bash", "-eE", "-u", "-o", "pipefail", "-c" ]

USER root

# Refer to `devcontainer.json` on why `WORKSPACE_DIR_ARG´ exists.
ARG WORKSPACE_DIR_ARG=/workspaces/uncore
# Because `ENV` is required to have a fixed value and it cannot
# be overwritten like `ARG`, we use a preceding `ARG` to set the
# ENV. This is done to have the environment variable available
# inside the container later, during "run time" of the container.
ENV WORKSPACE_DIR="${WORKSPACE_DIR_ARG}"
# This environment variable is a shortcut to files we cache
# inside the Development Container.
ENV DEV_CONTAINER_FILES_DIR="${WORKSPACE_DIR}/code/.dev_container"

# We start by preparing a "base" layer with common packages,
# directories, etc.
RUN <<EOM
Expand Down Expand Up @@ -45,7 +56,7 @@ RUN <<EOM
# 2. contain the repository files (mounted from the host)
#
# respectively.
mkdir /rustup /workspaces
mkdir -p /rustup "${DEV_CONTAINER_FILES_DIR}"
export RUSTUP_HOME='/rustup'
export CARGO_HOME='/rustup'

Expand Down Expand Up @@ -78,18 +89,25 @@ ENV PATH="/rustup/bin:${PATH}"
# image and the hst); hence, we use a different output directory.
ENV CARGO_TARGET_DIR=dev-container/target

# Moreover, we do not want to interfere with Cargo's output
# directory of the host (in case someone want to use both this container
# image and the hst); hence, we use a different output directory.
ENV CARGO_TARGET_DIR="${DEV_CONTAINER_FILES_DIR}/target"

# This environment variable ensure Cargo stores its index in
# `/workspaces/uncore/code/.cargo/home`. This is crucial in
# a directory we prepared exclusively for this task (which is also
# persisted on disk but ignored by git).This is crucial in
# ensuring we do not loose caches when the container is
# restarted (because the files are stored on the host).
ENV CARGO_HOME=/workspaces/uncore/code/dev-container/cargo-home
ENV CARGO_HOME="${DEV_CONTAINER_FILES_DIR}/cargo_home"
# We also specify where Rustup puts its components; this is
# required to also not have Rustup always download components,
# but instead store them on the host disk (see `CARGO_HOME`).
ENV RUSTUP_HOME=/workspaces/uncore/code/dev-container/rustup-home
# but instead store them on the host disk (see `CARGO_HOME`) (i.e.
# this directory is also persisted on disk but ignored by git))
ENV RUSTUP_HOME="${DEV_CONTAINER_FILES_DIR}/rustup_home"

# `/workspaces/uncore` is the directory we mount the respository root
# directory to, in order to compile and run unCORE.
WORKDIR /workspaces/uncore
# This is the location where we mount the repository into and also
# where we will work.
WORKDIR "${WORKSPACE_DIR}"

USER ubuntu
19 changes: 15 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
{
"name": "unCORE",
"build": {
"dockerfile": "Dockerfile"
// We build from the `Dockerfile` inside the `.devcontainer/` directory
"dockerfile": "Dockerfile",
"args": {
// We use this build-argument during the build process to be able to work with
// arbitrary paths (for mounting this workspace into the container) inside
// the Development Container. Moreover, it provides us with the ability to
// fully automate creation of required directories, etc.
// Inside the Dockerfile, we also provide a fallback value that is equal to
// the default location that Development Containers use for mounting a
// workspace.
"WORKSPACE_DIR_ARG": "${containerWorkspaceFolder}"
},
},
"updateRemoteUserUID": true,
"remoteUser": "ubuntu",
"containerUser": "ubuntu",
"containerEnv": {
"UNCORE_DEV_CONTAINER": "true"
},
// The repository root directory is mounted to `/workspaces/uncore`
// The repository root directory is mounted to `${WORKSPACE_DIR}`
// implicitly (by the dev-container spec), and we want to navigate there and
// then into the code sub-directory to immediately download the correct components
// (that Cargo detects from the workspace setup in this directory).
"postStartCommand": {
"show version of rustup": "rustup --version",
"show version of cargo and rustc": "cd /workspaces/uncore/code ; cargo --version ; rustc --version ;",
"show version of cargo and rustc": "cd \"${WORKSPACE_DIR}/code\" ; cargo --version ; rustc --version ;",
"show version of mold": "mold --version",
"set up VS Code tasks": "mkdir -p /workspaces/uncore/.vscode && cp /workspaces/uncore/.devcontainer/vscode/* /workspaces/uncore/.vscode/"
"set up VS Code tasks": "mkdir -p \"${WORKSPACE_DIR}/.vscode\" && cp --no-clobber \"${WORKSPACE_DIR}/.devcontainer/vscode/\"* \"${WORKSPACE_DIR}/.vscode/\""
},
"customizations": {
"vscode": {
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ documentation/site
# -----------------------------------------------

code/target/
code/dev-container/
code/.dev_container/

code/.cargo/home/
code/.cargo/rustup/

0 comments on commit e605bd3

Please sign in to comment.