Skip to content

Commit a722204

Browse files
kngwyufmdkdd
authored andcommitted
add custom build support (#62)
Support build scripts When we're editing 'build.rs', its target kind in `cargo metadata` is 'custom-build', which we can not use as build arguments. However, that build script will be checked by `cargo check` if we build any target in the same package. So, if the selected target is a 'custom-build' one, we just pick another target from the same package, if any. This commit adds tests for the build script situation.
1 parent 089330a commit a722204

File tree

8 files changed

+59
-3
lines changed

8 files changed

+59
-3
lines changed

flycheck-rust.el

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ more information on setting your PATH with Emacs."))
108108
(let ((json-array-type 'list))
109109
(json-read)))
110110
.packages))
111-
(seq-mapcat (lambda (pkg)
111+
(seq-map (lambda (pkg)
112112
(let-alist pkg .targets))
113113
packages))))
114114

@@ -128,7 +128,8 @@ the closest matching target, or nil if no targets could be found.
128128
See http://doc.crates.io/manifest.html#the-project-layout for a
129129
description of the conventional Cargo project layout."
130130
(-when-let* ((manifest (flycheck-rust-find-manifest file-name))
131-
(targets (flycheck-rust-get-cargo-targets manifest)))
131+
(packages (flycheck-rust-get-cargo-targets manifest))
132+
(targets (-flatten-n 1 packages)))
132133
(let ((target
133134
(or
134135
;; If there is a target that matches the file-name exactly, pick
@@ -151,7 +152,16 @@ description of the conventional Cargo project layout."
151152
(file-name-directory manifest)))))
152153
;; If all else fails, just pick the first target
153154
(car targets))))
154-
(let-alist target (cons (flycheck-rust-normalize-target-kind .kind) .name)))))
155+
;; If target is 'custom-build', we pick another target from the same package (see GH-62)
156+
(when (string= "custom-build" (let-alist target (car .kind)))
157+
(setq target (->> packages
158+
;; find the same package as current build-script buffer
159+
(--find (--any? (equal target it) it))
160+
(--find (not (equal target it))))))
161+
(when target
162+
(let-alist target
163+
(cons (flycheck-rust-normalize-target-kind .kind) .name))))))
164+
155165

156166
(defun flycheck-rust-normalize-target-kind (kinds)
157167
"Return the normalized target name from KIND.

tests/build-script-test/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "build-script-test"
3+
version = "0.1.0"
4+
5+
[dependencies]
6+
7+
[workspace]
8+
members = ["lib-test"]

tests/build-script-test/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let a = ;
3+
println!("cargo:rustc-link-lib=bz2");
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "lib-test"
3+
version = "0.1.0"
4+
5+
[dependencies]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let a = 0;
3+
println!("cargo:rustc-link-lib=zzip");
4+
}
5+
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
assert_eq!(2 + 2, 4);
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

tests/test-rust-setup.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
(defun lib-crate-file (file-name)
4242
(expand-file-name file-name "tests/custom-lib-target"))
4343

44+
(defun build-script-crate-file (file-name)
45+
(expand-file-name file-name "tests/build-script-test"))
46+
4447
(describe
4548
"`flycheck-rust-find-cargo-target' associates"
4649

@@ -118,4 +121,14 @@
118121
(expect
119122
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
120123
:to-equal "lib"))
124+
125+
(it "'build.rs' to any target in the same workspace member (parent)"
126+
(expect
127+
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128+
:to-equal (cons "bin" "build-script-test")))
129+
130+
(it "'build.rs' to any target in the same workspace member (child)"
131+
(expect
132+
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133+
:to-equal (cons "lib" "lib-test")))
121134
)

0 commit comments

Comments
 (0)