summaryrefslogtreecommitdiffstats
path: root/include/rust-package.mk
blob: 5b13f73d2c523adb92adb9409f03c0184117d86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2023 Luca Barbato and Donald Hoskins

# Variables (all optional) to be set in package Makefiles:
#
# RUST_PKG_FEATURES - list of options, default empty
#
#   Space or comma separated list of features to activate
#
#   e.g. RUST_PKG_FEATURES:=enable-foo,with-bar
#
#
# RUST_PKG_LOCKED - Assert that `Cargo.lock` will remain unchanged
#                   (Enabled by default)
#
#   Disable it if you want to have up-to-date dependencies
#
#   e.g. RUST_PKG_LOCKED:=0


ifeq ($(origin RUST_INCLUDE_DIR),undefined)
  RUST_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
endif
include $(RUST_INCLUDE_DIR)/rust-values.mk

RUST_PKG_LOCKED ?= 1
PKG_BUILD_DEPENDS += $(RUST_PKG_BUILD_DEPENDS)
PKG_CONFIG_DEPENDS += $(RUST_PKG_CONFIG_DEPENDS)

ifeq ($(strip $(PKG_BUILD_PARALLEL)),)
  PKG_BUILD_PARALLEL:=1
endif

CARGO_PKG_VARS= \
	$(CARGO_PKG_CONFIG_VARS) \
	CC=$(TARGET_CC_NOCACHE) \
	HOST_CC=$(HOSTCC_NOCACHE) \
	CXX=$(TARGET_CXX_NOCACHE) \
	HOST_CXX=$(HOSTCXX_NOCACHE) \
	MAKEFLAGS="$(PKG_JOBS)"

CARGO_PKG_ARGS = $(if $(PKG_SKIP_DOWNLOAD),,--offline)

ifeq ($(strip $(RUST_PKG_LOCKED)),1)
  CARGO_PKG_ARGS += --locked
endif

# $(1) path within PKG_BUILD_DIR (optional, defaults to MAKE_PATH)
# $(2) additional arguments to cargo (optional)
#
# Cargo builds use --offline; packages must supply a vendor tarball with
# .cargo/config.toml pointing to vendor/.  Generate vendor tarballs with
# scripts/gen-cargo-vendor-tarball.sh.  A source tree override (git-src,
# USE_SOURCE_DIR) has no vendor tarball, so Cargo fetches the crates then.
define Build/Compile/Cargo/Default
	+cd "$(PKG_BUILD_DIR)/$(if $(strip $(1)),$(strip $(1)),$(strip $(MAKE_PATH)))" && \
	$(CARGO_PKG_VARS) \
	$(CARGO) $(3) -v \
		--target $(RUSTC_TARGET_ARCH) \
		--profile $(CARGO_PKG_PROFILE) \
		$(if $(strip $(RUST_PKG_FEATURES)),--features "$(strip $(RUST_PKG_FEATURES))") \
		$(if $(filter --jobserver%,$(PKG_JOBS)),,-j1) \
		$(CARGO_PKG_ARGS) \
		$(2)
endef

define Build/Compile/Cargo
	$(call Build/Compile/Cargo/Default,$(1),$(2),install --root $(PKG_INSTALL_DIR) --path .)
endef

define Build/Compile/Cargo/Build
	$(call Build/Compile/Cargo/Default,$(1),$(2),build --manifest-path Cargo.toml)
endef

define RustBinPackage
  ifndef Package/$(1)/install
    define Package/$(1)/install
	$$(INSTALL_DIR) $$(1)/usr/bin/
	$$(INSTALL_BIN) $$(PKG_INSTALL_DIR)/bin/* $$(1)/usr/bin/
    endef
  endif
endef

Build/Compile=$(call Build/Compile/Cargo)