Skip to content

Commit

Permalink
Improve lib template on Travis (#19)
Browse files Browse the repository at this point in the history
* 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](rust-lang/cargo#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:
- #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 <connect+github@bltavares.com>

* 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](rust-lang/rust#51009)
how crate types are defined, in order to help with cross-platform
builds.

It is currently [not possible](rust-lang/cargo#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 <connect+github@bltavares.com>
  • Loading branch information
bltavares authored and yoshuawuyts committed Dec 18, 2018
1 parent 6628825 commit a0b25ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
36 changes: 26 additions & 10 deletions templates/lib/before_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
3 changes: 3 additions & 0 deletions templates/lib/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion templates/lib/travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0b25ce

Please sign in to comment.