From a0b25ceddeda71d21e38dc7763fa754714930cdd Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Tue, 18 Dec 2018 15:34:09 -0200 Subject: [PATCH] Improve lib template on Travis (#19) * 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 * Define the type of artifact to be produced by cargo. s command `cargo` subcommand is exclusively intended to be used to help with [working arround](https://github.com/rust-lang/rust/issues/51009) how crate types are defined, in order to help with cross-platform builds. It is currently [not possible](https://github.com/rust-lang/cargo/issues/6160) to define a single `crate-type` override on `cargo build`, which causes libs intended to be used on other languages to compile more than one type of crate. This commit adds a dependency on this [new CLI app](https://github.com/bltavares/cargo-crate-type) to be able to workaround the limitations of Cargo. We are now able to enable the `-C lto` optimization again, as we will only compile a single type of artifact Signed-off-by: Bruno Tavares --- templates/lib/before_deploy.sh | 36 ++++++++++++++++++++++++---------- templates/lib/install.sh | 3 +++ templates/lib/travis.yml | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/templates/lib/before_deploy.sh b/templates/lib/before_deploy.sh index d6dca4d..b84b6d1 100644 --- a/templates/lib/before_deploy.sh +++ b/templates/lib/before_deploy.sh @@ -5,22 +5,38 @@ PKG_NAME="{{PKG_NAME}}" set -ex main() { - local src=$(pwd) \ - stage= + local src=$(pwd) stage 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/ + if [[ "$TYPE" == "static" ]]; then + cargo crate-type static + else + cargo crate-type dynamic + fi + + cross rustc --lib --target $TARGET --release -- -C lto + + 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/install.sh b/templates/lib/install.sh index a5e8415..4ae5759 100644 --- a/templates/lib/install.sh +++ b/templates/lib/install.sh @@ -38,6 +38,9 @@ main() { # Install test dependencies rustup component add rustfmt-preview rustup component add clippy-preview + + # For defining the type of lib to produce: dynamic OR static + cargo install --force cargo-crate-type } main diff --git a/templates/lib/travis.yml b/templates/lib/travis.yml index 9d15fba..3ac23b1 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