rust+cmake in vscode

工程结构

顶层CMakeLists.txt

project(vs_code_learn)

add_subdirectory(rust_dir)

rust_dir

rust_dir/CMakeLists.txt

project(rust_cmake_shadow_name)

# clear CARGO_TARGET_DIR, set it in build script for shadow building
set(ENV{CARGO_TARGET_DIR} )
message(STATUS $ENV{CARGO_TARGET_DIR})

# build on shadow directory: PROJECT_BUILD_DIR
set(PROJECT_BUILD_DIR "${CMAKE_BINARY_DIR}/rust")
file(WRITE "${CMAKE_BINARY_DIR}/rust/spaceholder")

# use ExternalProject to do custom tasks in cmake
# https://cmake.org/cmake/help/v3.14/module/ExternalProject.html
include(ExternalProject)

ExternalProject_Add(
  rust_project_name
  URL ${PROJECT_SOURCE_DIR}
  DOWNLOAD_COMMAND ""
  CONFIGURE_COMMAND ""
# use script build_script.bash to do the building work
  BUILD_COMMAND  bash "${PROJECT_SOURCE_DIR}/build_script.bash" "${PROJECT_SOURCE_DIR}/Cargo.toml" "${PROJECT_BUILD_DIR}"
  INSTALL_COMMAND ""
  LOG_BUILD ON
)

# force update
ExternalProject_Add_Step(
  rust_project_name 
  forceconfigure
  COMMAND ${CMAKE_COMMAND} -E echo "Force configure of rust_project_name"

  DEPENDEES update
  DEPENDERS configure
  ALWAYS 1
)

rust_dir/build_script.bash

#!/bin/bash

export CARGO_TARGET_DIR="$2"
cargo build --manifest-path "$1"

rust_dir/Cargo.toml

[package]
name = "package_name"
version = "x.y.z"
authors = [ "name " ]

[[bin]]
name = "bin_name"
path = "src/.rs"

rust_dir/Cargo.lock

rust_dir/src/.rs

.vscode(本地配置目录)

{
  #...
  "tasks": [
    {
      "label": "cmake-config",
      "type": "shell",
      "command": "export CURDIR=$(pwd) && mkdir -p ${workspaceFolder}/build/$(basename ${fileDirname}) && cd ${workspaceFolder}/build/$(basename ${fileDirname}) && cmake-gui ${fileDirname}",
      "problemMatcher": []
    },
    {
      "label": "cmake-build",
      "type": "shell",
      "command": "export CURDIR=$(pwd) && mkdir -p ${workspaceFolder}/build/$(basename ${fileDirname}) && cd ${workspaceFolder}/build/$(basename ${fileDirname}) && cmake ${fileDirname} && cmake --build .",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": [
        "$eslint-compact"
      ]
    },
  ]
  # ...
}

你可能感兴趣的:(rust+cmake in vscode)