2017-06-21 18:22:32 +02:00
# $FreeBSD$
#
# This file contains logic to ease porting of Rust packages or
# binaries using the `cargo` command.
#
# Feature: cargo
# Usage: USES=cargo
# Valid ARGS: none
#
2019-04-17 09:33:34 +02:00
# MAINTAINER: rust@FreeBSD.org
2017-06-21 18:22:32 +02:00
. i f ! d e f i n e d ( _ I N C L U D E _ U S E S _ C A R G O _ M K )
_INCLUDE_USES_CARGO_MK = yes
. i f ! e m p t y ( c a r g o _ A R G S )
IGNORE += USES = cargo takes no arguments
. e n d i f
# List of static dependencies. The format is cratename-version.
# CARGO_CRATES will be downloaded from MASTER_SITES_CRATESIO.
CARGO_CRATES ?=
# List of features to build (space separated list).
CARGO_FEATURES ?=
# Name of the local directory for vendoring crates.
CARGO_VENDOR_DIR ?= ${ WRKSRC } /cargo-crates
# Default path for cargo manifest.
CARGO_CARGOTOML ?= ${ WRKSRC } /Cargo.toml
CARGO_CARGOLOCK ?= ${ WRKSRC } /Cargo.lock
# Save crates inside ${DISTDIR}/rust/crates by default.
CARGO_DIST_SUBDIR ?= rust/crates
# Generate list of DISTFILES.
. f o r _ c r a t e i n $ { C A R G O _ C R A T E S }
2019-02-15 13:04:50 +01:00
MASTER_SITES += CRATESIO/${ _crate : C /^(.*)-[0-9].*/ \1 / } /${ _crate : C /^.*-([0-9].*)/ \1 / } :cargo_${ _crate : S /-//g : S /.//g }
2017-06-21 18:22:32 +02:00
DISTFILES += ${ CARGO_DIST_SUBDIR } /${ _crate } .tar.gz:cargo_${ _crate : S /-//g : S /.//g }
. e n d f o r
# Build dependencies.
CARGO_BUILDDEP ?= yes
.if ${CARGO_BUILDDEP : tl } == "yes "
2019-05-24 05:25:11 +02:00
BUILD_DEPENDS += ${ RUST_DEFAULT } >= 1.35.0:lang/${ RUST_DEFAULT }
2017-06-21 18:22:32 +02:00
. e n d i f
lang/rust: Install Cargo + use bundled crates
This port now provides Cargo. This is the recommended now because Cargo
won't be provided separately in the future.
To build Cargo, we set `extended = true` in `config.toml`. As a side
effect, this flag also installs Rust source code. The port has a new
`SOURCES` option (disabled by default) to keep those sources.
As a consequence of this, `devel/cargo` is removed. Several ports
and Makefiles in Mk were updated to depend on `lang/rust` instead of
`devel/cargo`.
The other big change in this patch is the use of the bundled crates,
instead of relying on Cargo's registry (which was part of the distfiles,
in order to allow offline builds). So now, we don't need to prepare the
registry when updating this port.
This has several other benefits:
* It fixes the build with sudo(8).
* It fixes the use of the ino-64 patch (it was not applied to the
registry, thus not used).
Compilation errors were fixed in the ino-64 patch.
Various `.cargo-checksum.json` files are updated after the sources are
patched (FBSD10_FIX, ino-64, and so on). This fixes builds which were
failing with errors such as:
error: the listed checksum of `.../rustc-1.19.0-src/src/vendor/lzma-sys/xz-5.2.3/build-aux/config.rpath` has changed:
expected: c8b4c017079da9dfb3086a0583e60ffe736184d89005dc5973f0bb0fd17c04bb
actual: 561b00eb30ecaef2c9da17bc195e7d2a7ea63facea38ea9849fbb0ed340bebba
PR: 221088
Reported by: joneum@, nwhitehorn@, romain@,
Ekaterina Vaartis <vaartis@cock.li>,
david@catwhisker.org,
fullermd@over-yonder.net,
rum1cro@yandex.ru,
w.schwarzenfeld@utanet.at
Differential Revision: https://reviews.freebsd.org/D11783
2017-09-15 20:01:17 +02:00
# Location of cargo binary (default to lang/rust's Cargo binary)
2017-06-21 18:22:32 +02:00
CARGO_CARGO_BIN ?= ${ LOCALBASE } /bin/cargo
# Location of the cargo output directory.
CARGO_TARGET_DIR ?= ${ WRKDIR } /target
# Environment for cargo
# - CARGO_HOME: local cache of the registry index
# - CARGO_BUILD_JOBS: configure number of jobs to run
# - CARGO_TARGET_DIR: location of where to place all generated artifacts
# - RUSTC: path of rustc binary (default to lang/rust)
# - RUSTDOC: path of rustdoc binary (default to lang/rust)
# - RUSTFLAGS: custom flags to pass to all compiler invocations that Cargo performs
CARGO_ENV += \
CARGO_HOME = ${ WRKDIR } /cargo-home \
CARGO_BUILD_JOBS = ${ MAKE_JOBS_NUMBER } \
CARGO_TARGET_DIR = ${ CARGO_TARGET_DIR } \
RUSTC = ${ LOCALBASE } /bin/rustc \
RUSTDOC = ${ LOCALBASE } /bin/rustdoc \
2019-03-14 18:31:45 +01:00
RUSTFLAGS = " ${ RUSTFLAGS } -C linker= ${ CC : Q } ${ LDFLAGS : C /.+/-C link-arg=&/ } "
2017-06-21 18:22:32 +02:00
2017-11-29 12:06:11 +01:00
# Adjust -C target-cpu if -march/-mcpu is set by bsd.cpu.mk
. i f ${ARCH} = = amd64 || ${ ARCH } = = i386
RUSTFLAGS += ${ CFLAGS : M -march=* : S /-march=/-C target-cpu=/ }
. e l s e
RUSTFLAGS += ${ CFLAGS : M -mcpu=* : S /-mcpu=/-C target-cpu=/ }
. e n d i f
2019-03-02 01:35:33 +01:00
. i f ${ARCH} = = powerpc64
USE_GCC ?= yes
. e n d i f
2017-06-21 18:22:32 +02:00
# Helper to shorten cargo calls.
CARGO_CARGO_RUN = \
cd ${ WRKSRC } && ${ SETENV } ${ MAKE_ENV } ${ CARGO_ENV } \
${ CARGO_CARGO_BIN }
# User arguments for cargo targets.
CARGO_BUILD_ARGS ?=
CARGO_INSTALL_ARGS ?=
CARGO_TEST_ARGS ?=
CARGO_UPDATE_ARGS ?=
# Use module targets ?
CARGO_BUILD ?= yes
CARGO_CONFIGURE ?= yes
CARGO_INSTALL ?= yes
CARGO_TEST ?= yes
2019-03-25 07:28:20 +01:00
# Set CARGO_USE_GIT{HUB,LAB} to yes if your application requires
# some dependencies from git repositories hosted on GitHub or
# GitLab instances. All Cargo.toml files will be patched to point
# to the right offline sources based on what is defined in
# {GH,GL}_TUPLE. This makes sure that cargo does not attempt to
# access the network during the build.
CARGO_USE_GITHUB ?= no
CARGO_USE_GITLAB ?= no
2017-06-21 18:22:32 +02:00
# Manage crate features.
. i f ! e m p t y ( C A R G O _ F E A T U R E S )
CARGO_BUILD_ARGS += --features= '${CARGO_FEATURES}'
CARGO_INSTALL_ARGS += --features= '${CARGO_FEATURES}'
CARGO_TEST_ARGS += --features= '${CARGO_FEATURES}'
. e n d i f
2017-10-15 16:05:04 +02:00
. i f ! d e f i n e d ( W I T H _ D E B U G )
2017-06-21 18:22:32 +02:00
CARGO_BUILD_ARGS += --release
CARGO_TEST_ARGS += --release
. e l s e
CARGO_INSTALL_ARGS += --debug
. e n d i f
2018-06-14 11:56:02 +02:00
.if ${CARGO_CRATES : Mbacktrace -sys -[0-9]*}
BUILD_DEPENDS += gmake:devel/gmake
. e n d i f
.if ${CARGO_CRATES : Mcmake -[0-9]*}
BUILD_DEPENDS += cmake:devel/cmake
. e n d i f
.if ${CARGO_CRATES : Mgettext -sys -[0-9]*}
CARGO_ENV += GETTEXT_BIN_DIR = ${ LOCALBASE } /bin \
GETTEXT_INCLUDE_DIR = ${ LOCALBASE } /include \
GETTEXT_LIB_DIR = ${ LOCALBASE } /lib
. e n d i f
2019-02-15 10:33:48 +01:00
.for libc in ${CARGO_CRATES : Mlibc -[0-9]*}
2018-06-14 11:56:02 +02:00
# FreeBSD 12.0 changed ABI: r318736 and r320043
# https://github.com/rust-lang/libc/commit/78f93220d70e
# https://github.com/rust-lang/libc/commit/969ad2b73cdc
2019-02-15 10:33:48 +01:00
_libc_VER = ${ libc : C /.*-// }
2018-06-14 11:56:02 +02:00
. if ${_libc_VER : R :R } == 0 && (${_libc_VER :R :E } < 2 | | ${_libc_VER :R :E } == 2 && ${_libc_VER :E } < 38)
2019-02-15 10:33:48 +01:00
DEV_WARNING += " CARGO_CRATES= ${ libc } may be unstable on FreeBSD 12.0. Consider updating to the latest version (higher than 0.2.37). "
2018-06-14 11:56:02 +02:00
. e n d i f
2019-02-28 19:47:28 +01:00
. if ${_libc_VER : R :R } == 0 && (${_libc_VER :R :E } < 2 | | ${_libc_VER :R :E } == 2 && ${_libc_VER :E } < 49)
DEV_WARNING += " CARGO_CRATES= ${ libc } may be unstable on aarch64 or not build on armv6, armv7, powerpc64. Consider updating to the latest version (higher than 0.2.49). "
. e n d i f
2018-06-14 11:56:02 +02:00
. u n d e f _ l i b c _ V E R
2019-02-15 10:33:48 +01:00
. e n d f o r
2018-06-14 11:56:02 +02:00
.if ${CARGO_CRATES : Mlibgit 2-sys -[0-9]*}
2017-06-21 18:22:32 +02:00
# Use the system's libgit2 instead of building the bundled version
CARGO_ENV += LIBGIT2_SYS_USE_PKG_CONFIG = 1
. e n d i f
2018-09-21 13:16:08 +02:00
.if ${CARGO_CRATES : Mlibssh 2-sys -[0-9]*}
# Use the system's libssh2 instead of building the bundled version
CARGO_ENV += LIBSSH2_SYS_USE_PKG_CONFIG = 1
. e n d i f
2018-06-14 11:56:02 +02:00
.if ${CARGO_CRATES : Monig_sys -[0-9]*}
# onig_sys always prefers the system library but will try to link
# statically with it. Since devel/oniguruma doesn't provide a static
# library it'll link to libonig.so instead. Strictly speaking setting
# RUSTONIG_SYSTEM_LIBONIG is not necessary, but will force onig_sys to
# always use the system's libonig as returned by `pkg-config oniguruma`.
CARGO_ENV += RUSTONIG_SYSTEM_LIBONIG = 1
. e n d i f
2018-11-29 12:05:39 +01:00
.if ${CARGO_CRATES : Mopenssl -0.[0-9].*}
# FreeBSD 12.0 updated base OpenSSL in r339270:
# https://github.com/sfackler/rust-openssl/commit/276577553501
. i f ! e x i s t s ( $ { P A T C H D I R } / p a t c h - o p e n s s l - 1 . 1 . 1 ) # skip if backported
_openssl_VER = ${ CARGO_CRATES : Mopenssl -0.[0-9].* : C /.*-// }
. if ${_openssl_VER : R :R } == 0 && (${_openssl_VER :R :E } < 10 | | ${_openssl_VER :R :E } == 10 && ${_openssl_VER :E } < 4)
DEV_WARNING += "CARGO_CRATES=openssl-0.10.3 or older do not support OpenSSL 1.1.1. Consider updating to the latest version."
. e n d i f
. e n d i f
. u n d e f _ o p e n s s l _ V E R
. e n d i f
2018-06-14 11:56:02 +02:00
.if ${CARGO_CRATES : Mopenssl -sys -[0-9]*}
2017-06-21 18:22:32 +02:00
# Make sure that openssl-sys can find the correct version of OpenSSL
CARGO_ENV += OPENSSL_LIB_DIR = ${ OPENSSLLIB } \
OPENSSL_INCLUDE_DIR = ${ OPENSSLINC }
. e n d i f
2018-06-14 11:56:02 +02:00
.if ${CARGO_CRATES : Mpkg -config -[0-9]*}
2017-06-21 18:22:32 +02:00
. i n c l u d e "${USESDIR}/pkgconfig.mk"
. e n d i f
_USES_extract += 600:cargo-extract
cargo-extract :
# target for preparing crates directory. It will put all crates in
# the local crates directory.
@${ ECHO_MSG } " ===> Moving crates to ${ CARGO_VENDOR_DIR } "
@${ MKDIR } ${ CARGO_VENDOR_DIR }
. f o r _ c r a t e i n $ { C A R G O _ C R A T E S }
@${ MV } ${ WRKDIR } /${ _crate } ${ CARGO_VENDOR_DIR } /${ _crate }
@${ PRINTF } '{"package":"%s","files":{}}' \
$$ ( ${ SHA256 } -q ${ DISTDIR } /${ CARGO_DIST_SUBDIR } /${ _crate } .tar.gz) \
> ${ CARGO_VENDOR_DIR } /${ _crate } /.cargo-checksum.json
2019-05-24 09:57:11 +02:00
@if [ -r ${ CARGO_VENDOR_DIR } /${ _crate } /Cargo.toml.orig ] ; then \
${ MV } ${ CARGO_VENDOR_DIR } /${ _crate } /Cargo.toml.orig \
${ CARGO_VENDOR_DIR } /${ _crate } /Cargo.toml.orig-cargo; \
fi
2017-06-21 18:22:32 +02:00
. e n d f o r
2019-03-25 07:28:20 +01:00
_CARGO_GIT_PATCH_CARGOTOML =
2017-06-21 18:22:32 +02:00
.if ${CARGO_USE_GITHUB : tl } == "yes "
2019-03-25 07:28:20 +01:00
. for _group in ${GH_TUPLE : C @^[^:]*:[^:]*:[^:]*:(([^:/]*)?)((/.*)?)@\2@}
_CARGO_GIT_PATCH_CARGOTOML := ${ _CARGO_GIT_PATCH_CARGOTOML } \
2017-06-21 18:22:32 +02:00
-e 's@git = "(https|http|git)://github.com/${GH_ACCOUNT_${_group}}/${GH_PROJECT_${_group}}(\.git)?"@path = "${WRKSRC_${_group}}"@'
2019-03-25 07:28:20 +01:00
. e n d f o r
. e n d i f
.if ${CARGO_USE_GITLAB : tl } == "yes "
. for _group in ${GL_TUPLE : C @^(([^:]*://[^:/]*(:[0-9]{1, 5})?(/[^:]*[^/])?:)?)([^:]*):([^:]*):([^:]*)(:[^:/]*)((/.*)?)@\8@:S /^://}
_CARGO_GIT_PATCH_CARGOTOML := ${ _CARGO_GIT_PATCH_CARGOTOML } \
-e 's@git = "${GL_SITE_${_group}}/${GL_ACCOUNT_${_group}}/${GL_PROJECT_${_group}}(\.git)?"@path = "${WRKSRC_${_group}}"@'
. e n d f o r
. e n d i f
. i f ! e m p t y ( _ C A R G O _ G I T _ P A T C H _ C A R G O T O M L )
_USES_patch += 600:cargo-patch-git
2017-06-21 18:22:32 +02:00
2019-03-25 07:28:20 +01:00
cargo-patch-git :
@${ FIND } ${ WRKDIR } -name Cargo.toml -type f -exec \
${ SED } -i.dist -E ${ _CARGO_GIT_PATCH_CARGOTOML } { } +
2017-06-21 18:22:32 +02:00
. e n d i f
.if !target(do-configure) && ${CARGO_CONFIGURE : tl } == "yes "
# configure hook. Place a config file for overriding crates-io index
# by local source directory.
do-configure :
@${ MKDIR } ${ WRKDIR } /.cargo
@${ ECHO_CMD } "[source.cargo]" > ${ WRKDIR } /.cargo/config
@${ ECHO_CMD } " directory = ' ${ CARGO_VENDOR_DIR } ' " >> ${ WRKDIR } /.cargo/config
@${ ECHO_CMD } "[source.crates-io]" >> ${ WRKDIR } /.cargo/config
@${ ECHO_CMD } "replace-with = 'cargo'" >> ${ WRKDIR } /.cargo/config
@if ! ${ GREP } -qF '[profile.release]' ${ CARGO_CARGOTOML } ; then \
${ ECHO_CMD } "" >> ${ CARGO_CARGOTOML } ; \
${ ECHO_CMD } "[profile.release]" >> ${ CARGO_CARGOTOML } ; \
${ ECHO_CMD } "opt-level = 2" >> ${ CARGO_CARGOTOML } ; \
${ ECHO_CMD } "debug = false" >> ${ CARGO_CARGOTOML } ; \
fi
@${ CARGO_CARGO_RUN } update \
--manifest-path ${ CARGO_CARGOTOML } \
--verbose \
${ CARGO_UPDATE_ARGS }
. e n d i f
.if !target(do-build) && ${CARGO_BUILD : tl } == "yes "
do-build :
@${ CARGO_CARGO_RUN } build \
--manifest-path ${ CARGO_CARGOTOML } \
--verbose \
${ CARGO_BUILD_ARGS }
. e n d i f
.if !target(do-install) && ${CARGO_INSTALL : tl } == "yes "
do-install :
@${ CARGO_CARGO_RUN } install \
2018-12-16 13:08:32 +01:00
--path . \
2017-06-21 18:22:32 +02:00
--root " ${ STAGEDIR } ${ PREFIX } " \
--verbose \
${ CARGO_INSTALL_ARGS }
@${ RM } -- " ${ STAGEDIR } ${ PREFIX } /.crates.toml "
. e n d i f
.if !target(do-test) && ${CARGO_TEST : tl } == "yes "
do-test :
@${ CARGO_CARGO_RUN } test \
--manifest-path ${ CARGO_CARGOTOML } \
--verbose \
${ CARGO_TEST_ARGS }
. e n d i f
#
# Helper targets for port maintainers
#
2018-09-04 09:26:02 +02:00
# cargo-crates will output the crates list from Cargo.lock. If there
# is no Cargo.lock for some reason, try and generate it first.
2017-06-21 18:22:32 +02:00
cargo-crates : extract
2018-09-04 09:26:02 +02:00
@if [ ! -r " ${ CARGO_CARGOLOCK } " ] ; then \
${ ECHO_MSG } " ===> ${ CARGO_CARGOLOCK } not found. Trying to generate it... " ; \
${ CARGO_CARGO_RUN } generate-lockfile \
--manifest-path ${ CARGO_CARGOTOML } \
--verbose; \
fi
2017-06-21 18:22:32 +02:00
@${ SETENV } USE_GITHUB = ${ USE_GITHUB } \
${ AWK } -f ${ SCRIPTSDIR } /cargo-crates.awk ${ CARGO_CARGOLOCK }
# cargo-crates-licenses will try to grab license information from
# all downloaded crates.
cargo-crates-licenses : configure
@${ FIND } ${ CARGO_VENDOR_DIR } -name 'Cargo.toml' -maxdepth 2 \
-exec ${ GREP } -H '^license' { } \; \
| ${ SED } \
-e 's@^${CARGO_VENDOR_DIR}/@@' \
-e 's@/Cargo.toml:license.*= *"@|@' \
2018-06-14 11:56:02 +02:00
-e 's@"$$@@g' | sort | /usr/bin/column -t -s '|'
2017-06-21 18:22:32 +02:00
. e n d i f