From 2677e1118d5fdad3c9528c3dc6bfb132af681867 Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Wed, 10 Oct 2018 13:00:20 +0000 Subject: [PATCH] Improve lib template on Travis This commit contains changes on the generated lib template to build cross-platform libs. With this changes, I was able to setup a [Travis] build with GitHub [Releases] of a cross-platform lib. It is not possible yet to build a different crate type only using command line args. It requires a modification on `Cargo.toml` to include new types of library outputs. I've already opened an issue on [cargo](https://github.com/rust-lang/cargo/issues/6160) to see what should be the case here. Meanwhile, lib authors must change `Cargo.toml` and include the extra `crate-type` attribute with all the libs. Sadly, this also means that `LTO` optimisation is not available if the lib uses `lib` or `rlib` attributes. To avoid modifying the `Cargo.toml`, the sugestion would be that on a future PR we could add to the deploy script a modification on the `Cargo.toml` manifest to include the corresponding crate-type, and only such crate type for that target. This means we would be able to bring LTO and output smaller libs. Realated to: - https://github.com/yoshuawuyts/crossgen/issues/11 [Travis]: https://travis-ci.org/bltavares/rust-over-jna-example/builds/439439854 [Releases]: https://github.com/bltavares/rust-over-jna-example/releases/tag/initial Signed-off-by: Bruno Tavares --- templates/lib/before_deploy.sh | 40 ++++++++++++++++++++++++++-------- templates/lib/travis.yml | 2 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/templates/lib/before_deploy.sh b/templates/lib/before_deploy.sh index d6dca4d..a44b684 100644 --- a/templates/lib/before_deploy.sh +++ b/templates/lib/before_deploy.sh @@ -6,21 +6,43 @@ set -ex main() { local src=$(pwd) \ - stage= + stage \ + linking_args case $TRAVIS_OS_NAME in - linux) - stage=$(mktemp -d) - ;; - osx) - stage=$(mktemp -d -t tmp) - ;; + linux) + stage=$(mktemp -d) + ;; + osx) + stage=$(mktemp -d -t tmp) + ;; esac test -f Cargo.lock || cargo generate-lockfile - cross rustc --bin $PKG_NAME --target $TARGET --release -- -C lto - cp target/$TARGET/release/$PKG_NAME $stage/ + # TODO: combine with -C lto + case $TYPE in + static) + linking_args="--crate-type staticlib" + ;; + *) + linking_args="--crate-type cdylib" + ;; + esac + + cross rustc --lib --target $TARGET --release -- $linking_args + + case $TYPE-$TRAVIS_OS_NAME in + static-*) + cp target/$TARGET/release/lib$PKG_NAME.a $stage/ + ;; + *-osx) + cp target/$TARGET/release/lib$PKG_NAME.dylib $stage/ + ;; + *) + cp target/$TARGET/release/lib$PKG_NAME.so $stage/ + ;; + esac cd $stage tar czf $src/$CRATE_NAME-$TRAVIS_TAG-$TARGET.tar.gz * diff --git a/templates/lib/travis.yml b/templates/lib/travis.yml index 2aa845e..e363ad2 100644 --- a/templates/lib/travis.yml +++ b/templates/lib/travis.yml @@ -12,7 +12,7 @@ matrix: include: - env: TARGET=armv7-unknown-linux-gnueabihf rust: nightly - - env: TARGET=x86_64-unknown-linux-musl + - env: TARGET=x86_64-unknown-linux-musl TYPE=static rust: nightly - env: TARGET=x86_64-apple-darwin rust: nightly