Skip to content

Commit

Permalink
create a .cargo/config so intellij uses the same target dir as cmake (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slyphon authored and Yao Yue committed Jul 30, 2018
1 parent e710712 commit d7dab43
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/.gitignore
@@ -1 +1,2 @@
Cargo.lock
.cargo/
7 changes: 7 additions & 0 deletions rust/CMakeLists.txt
@@ -1,5 +1,12 @@
file(WRITE CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}\n")

execute_process(
COMMAND /usr/bin/env "CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}" "/bin/bash" "${CMAKE_CURRENT_LIST_DIR}/scripts/setup.sh"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
RESULT_VARIABLE CARGO_RESULT
OUTPUT_VARIABLE CARGO_CONFIG_OUT
ERROR_VARIABLE CARGO_CONFIG_OUT)

if(HAVE_RUST)
add_subdirectory(ccommon_rs)
endif()
35 changes: 35 additions & 0 deletions rust/scripts/setup.sh
@@ -0,0 +1,35 @@
#!/bin/bash

# utility script run by cmake for writing a .cargo/config that points
# to the common 'target' directory under the CMAKE_BINARY_DIR. This
# allows automated tools (such as the intellij rust plugin or vcode
# rust integration) to share output and avoid recompiling between
# the command line and the IDE.
#
# it is assumed that this script is run with the CWD being the
# place where the .cargo dir should be created.

set -euo pipefail
IFS=$'\n\t'

die() { echo "fatal: $*" >&2; exit 1; }

if [[ -z "${CMAKE_BINARY_DIR:-}" ]]; then
die "CMAKE_BINARY_DIR must be set!"
fi

mkdir -p .cargo

cleanup() {
[[ -n "${TEMPFILE:-}" ]] && rm -rf "$TEMPFILE"
}
trap cleanup EXIT

TEMPFILE="$(mktemp '.cargo/config.XXXXXXXX')" || die "could not create tempfile"

cat > "$TEMPFILE" <<EOS
[build]
target-dir = "${CMAKE_BINARY_DIR}/target"
EOS

mv "$TEMPFILE" .cargo/config

0 comments on commit d7dab43

Please sign in to comment.