Browse Source

vlcrs: add autotools infra for build and linking Rust modules

The autotools suite (automake, libtool, ...) does not support Rust
neither Cargo (Rust package manager and build system) so we need to have
a custom system for using them. We do this by having "hacky" script that
does the build with cargo and output an .a archive in the .libs
directory. And with this and another cargo command we create the .la
with the right information so that the build system can continue as
usual.
pull/169/head
Loïc 3 years ago
committed by Steve Lhomme
parent
commit
c8ce25a03f
  1. 25
      modules/Makefile.am
  2. 87
      modules/libtool_cargo.sh

25
modules/Makefile.am

@ -13,6 +13,31 @@ TESTS =
dist_noinst_SCRIPTS = module.rc.in
EXTRA_LTLIBRARIES =
if HAVE_RUST
LIBTOOL_CARGO_EXE = $(srcdir)/libtool_cargo.sh
CARGO_BUILD_ARGS = --target=@RUST_TARGET@ --release \
--crate-type=staticlib
LIBTOOL_CARGO = \
TARGET_CC=$(CC) TARGET_AR=$(AR) \
TARGET_CFLAGS="$(CFLAGS)" \
RUSTTARGET="@RUST_TARGET@" \
RUSTFLAGS="-C panic=abort -C opt-level=z" \
CARGO_TARGET_DIR="target-rust" \
$(LIBTOOL_CARGO_EXE) $(CARGO) rustc $(CARGO_BUILD_ARGS)
CARGO_PLUGINS_INV = ${LTLIBRARIES:%rs_plugin.la=%rs_plugin}
CARGO_PLUGINS_CUT = ${CARGO_PLUGINS_INV:%.la=}
CARGO_PLUGINS = ${CARGO_PLUGINS_CUT:%=%.la}
CARGO_DEPS = ${CARGO_PLUGINS:%_plugin.la=${top_builddir}/modules/.libs/%.d}
-include $(CARGO_DEPS)
endif
include common.am
include access/Makefile.am
include access/http/Makefile.am

87
modules/libtool_cargo.sh

@ -0,0 +1,87 @@
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# Copyright (C) 2022 Loïc Branstett <loic@videolabs.io>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
# This script generate a libtool convinence library from a Cargo (Rust project)
set -e # Automaticly exit on error
CARGO_RUSTC_CMD=${@:1:$#-2} # all other args
MODULE_PROJECT_DIR=${@:$#-1:1} # second last argument
LT_CONVENIENCE=${@:$#:1} # last argument
# Build the project a first time without anything specitial)
$CARGO_RUSTC_CMD --manifest-path="$MODULE_PROJECT_DIR/Cargo.toml"
# "Build" the project a second time and fetch the native-static-libs to link with
NATIVE_STATIC_LIBS=$($CARGO_RUSTC_CMD --manifest-path="$MODULE_PROJECT_DIR/Cargo.toml" --quiet -- --print native-static-libs 2>&1 | grep "native-static-libs" | sed "s/note: native-static-libs://" | sed "s/-lvlccore//")
STATIC_LIB_NAME=$(echo $LT_CONVENIENCE | sed "s/\.la/\.a/")
STATIC_LIB_DEP=$(echo $LT_CONVENIENCE | sed "s/\.la/\.d/")
CARGO_STATIC_LIB_PATH="$CARGO_TARGET_DIR/$RUSTTARGET/release/$STATIC_LIB_NAME"
CARGO_STATIC_DEP_PATH="$CARGO_TARGET_DIR/$RUSTTARGET/release/$STATIC_LIB_DEP"
LT_VERSION=$(../libtool --version | grep "libtool")
cat <<EOF > $LT_CONVENIENCE
# $LT_CONVENIENCE - a libtool library file
# Generated by $LT_VERSION
# The name that we can dlopen(3).
dlname=''
# Names of this library.
library_names=''
# The name of the static archive.
old_library='$STATIC_LIB_NAME'
# Linker flags that cannot go in dependency_libs.
inherited_linker_flags=''
# Libraries that this one depends upon.
dependency_libs=' $NATIVE_STATIC_LIBS'
# Names of additional weak libraries provided by this library
weak_library_names=''
# Version information
current=0
age=0
revision=0
# Is this an already installed library?
installed=no
# Should we warn about portability when linking against -modules?
shouldnotlink=no
# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir=''
EOF
mkdir -p "./.libs"
ln -sf "../$LT_CONVENIENCE" "./.libs/$LT_CONVENIENCE"
cp "$CARGO_STATIC_LIB_PATH" "./.libs/$STATIC_LIB_NAME"
echo -n "$LT_CONVENIENCE:" > "./.libs/$STATIC_LIB_DEP"
cat "$CARGO_STATIC_DEP_PATH" | cut -d ':' -f2 >> "./.libs/$STATIC_LIB_DEP"
Loading…
Cancel
Save