[rust] Generate a rust-project.json file for rust-analyzer

Rust-analyzer can read a rust-project.json file describing the crates in
a build instead of using Cargo.toml files. Adds build system support for
generating such a file describing all of the Rust crates in the build.

Test: build.py
Bug: 196474053
Change-Id: I295d04c3f06e438635957469c5683f1159ef0faf
diff --git a/app/trusty/user-tasks.mk b/app/trusty/user-tasks.mk
index 73d9d00..fc8d0db 100644
--- a/app/trusty/user-tasks.mk
+++ b/app/trusty/user-tasks.mk
@@ -72,6 +72,9 @@
 
 GLOBAL_USER_RUSTFLAGS += -L dependency=$(TRUSTY_LIBRARY_BUILDDIR)
 
+GLOBAL_CRATE_COUNT := 0
+RUST_ANALYZER_CRATES :=
+
 # Save userspace-global variables so we can restore kernel state
 TRUSTY_KERNEL_SAVED_ARCH := $(ARCH)
 TRUSTY_KERNEL_SAVED_ALLOW_FP_USE := $(ALLOW_FP_USE)
@@ -182,6 +185,29 @@
 
 endif
 
+
+#
+# Build a rust-project.json for rust-analyzer
+#
+RUST_PROJECT_JSON := $(BUILDDIR)/rust-project.json
+define RUST_PROJECT_JSON_CONTENTS :=
+{
+	"crates": [
+		$(call STRIP_TRAILING_COMMA,$(RUST_ANALYZER_CRATES))
+	]
+}
+endef
+RUST_PROJECT_JSON_CONTENTS := $(subst $(NEWLINE),\n,$(RUST_PROJECT_JSON_CONTENTS))
+.PHONY: $(RUST_PROJECT_JSON)
+$(RUST_PROJECT_JSON): CONTENTS := $(RUST_PROJECT_JSON_CONTENTS)
+$(RUST_PROJECT_JSON):
+	@$(MKDIR)
+	@echo Creating rust-project.json for rust-analyzer
+	$(NOECHO)echo -e '$(CONTENTS)' > $@
+
+EXTRA_BUILDDEPS += $(RUST_PROJECT_JSON)
+
+
 # Restore kernel state
 ARCH := $(TRUSTY_KERNEL_SAVED_ARCH)
 ALLOW_FP_USE := $(TRUSTY_KERNEL_SAVED_ALLOW_FP_USE)
@@ -233,3 +259,7 @@
 LOADABLE_APP_LIST :=
 TRUSTY_LOADABLE_USER_TASKS :=
 TEST_PACKAGE_ZIP :=
+RUST_PROJECT_JSON :=
+RUST_PROJECT_JSON_CONTENTS :=
+RUST_ANALYZER_CRATES :=
+GLOBAL_CRATE_COUNT :=