Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support linking to libraries from a vcpkg installation #1009

Merged
merged 3 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/vcpkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: vcpkg

on:
push:
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Install cargo-vcpkg
run: cargo install cargo-vcpkg
- name: Install dependencies
run: cargo vcpkg build
- name: Build SDL2
shell: bash
env:
CI_BUILD_FEATURES: "use-vcpkg static-link gfx image ttf mixer"
RUST_TEST_THREADS: 1
run: |
set -xeuo pipefail
rustc --version
cargo --version
cargo build --features "${CI_BUILD_FEATURES}"
cargo build --examples --features "${CI_BUILD_FEATURES}"
cargo test --features "${CI_BUILD_FEATURES}"
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ttf = ["sdl2-sys/ttf"]

use-bindgen = ["sdl2-sys/use-bindgen"]
use-pkgconfig = ["sdl2-sys/use-pkgconfig"]
use-vcpkg = ["sdl2-sys/use-vcpkg"]
use_mac_framework = ["sdl2-sys/use_mac_framework"]
bundled = ["sdl2-sys/bundled"]
static-link = ["sdl2-sys/static-link"]
Expand Down Expand Up @@ -148,3 +149,14 @@ name = "window-properties"
[[example]]
required-features = ["raw-window-handle"]
name = "raw-window-handle-with-wgpu"

[package.metadata.vcpkg]
dependencies = ["sdl2"]

# dependencies required when building examples and tests for this crate
dev-dependencies = ["sdl2", "sdl2-image[libjpeg-turbo,tiff,libwebp]", "sdl2-ttf", "sdl2-gfx", "sdl2-mixer"]
git = "https://github.com/microsoft/vcpkg"
rev = "a0518036077baa4"

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ You might also need a C compiler (`gcc`).
#### Static linking in Linux

You can choose to link SDL2 statically instead of dynamically with the `static-link` feature.
However on Linux, unless you use the "bundled" feature, your operating system has no built-in way to find the resources needed to link statically SDL2 from your system.

You need to add the feature `use-pkgconfig`, so that rustc knows where to look for your SDL2 libraries and its dependencies for static linking.
On Linux, you will need to additionally do one of the following:
* use the `bundled` feature
* use the feature `use-pkgconfig` so that rustc knows where to look for your SDL2 libraries and its dependencies for static linking. This is required because there is no built-in way to find the resources needed to link statically SDL2 from your system
* install development libraries with [vcpkg][vcpkg]. Instructions to generate a static binary on Linux and other operating systems using vcpkg are [here][cargo-vcpkg-usage]

### Mac OS X
#### If you are using homebrew
Expand Down Expand Up @@ -117,6 +118,10 @@ default = []
use_sdl2_mac_framework = ["sdl2/use_mac_framework"]
```

#### Static linking on macOS using vcpkg

Instructions to generate a static binary on macOS and other operating systems using [vcpkg][vcpkg] are [here][cargo-vcpkg-usage].

### Windows with build script

1. Download mingw and msvc development libraries from
Expand Down Expand Up @@ -248,7 +253,7 @@ These files are not currently included with the windows-gnu toolchain, but can b
You will find the aforementioned libraries under `mingw64/x86_64-w64-mingw32/lib/` (for x86_64) or `mingw32/i686-w64-mingw32/lib/` (for i686). Copy them to your toolchain's `lib` directory (the same one you copied the SDL .a files to).

### Windows (MSVC with vcpkg)
1. Install [MS build tools](https://visualstudio.microsoft.com/downloads/) and [vcpkg](https://github.com/microsoft/vcpkg)
1. Install [MS build tools](https://visualstudio.microsoft.com/downloads/) and [vcpkg][vcpkg]
2. Install the needed SDL2 libs: `vcpkg.exe install sdl2-ttf:x64-windows sdl2:x64-windows`
3. Open a x64 native tools prompt (x64 Native Tools Command Prompt for VS 2019)
4. set env vars:
Expand Down Expand Up @@ -289,10 +294,39 @@ SET LIB=%LIB%;C:\Users\my_user\dev\vcpkg\installed\x64-windows\lib

#### Static linking with MSVC

The MSVC development libraries provided by http://libsdl.org/ don't include a static library. This means that if you want to use the `static-link` feature with the windows-msvc toolchain, you have to either
The MSVC development libraries provided by http://libsdl.org/ don't include a static library. This means that if you want to use the `static-link` feature with the windows-msvc toolchain, you have to do one of

- build an SDL2 static library yourself and copy it to your toolchain's `lib` directory; or
- also enable the `bundled` feature, which will build a static library for you.
- also enable the `bundled` feature, which will build a static library for you; or
- use a static SDL2 library from vcpkg as described below.

### Windows, Linux and macOS with vcpkg

Another method of getting the development libraries is with [vcpkg][vcpkg]. To set up a project to build a static binary on Windows (MSVC), Linux or macOS that is buildable like this:
```sh
cargo install cargo-vcpkg
cargo vcpkg build
cargo build
```

add the following your `Cargo.toml`:

```toml
[dependencies.sdl2]
version = "0.34"
default-features = false
features = ["ttf","image","gfx","mixer","static-link","use-vcpkg"]

[package.metadata.vcpkg]
dependencies = ["sdl2", "sdl2-image[libjpeg-turbo,tiff,libwebp]", "sdl2-ttf", "sdl2-gfx", "sdl2-mixer"]
git = "https://github.com/microsoft/vcpkg"
rev = "a0518036077baa4"

[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
```

More information on the `cargo vcpkg` tool is [here][cargo-vcpkg].

# Installation

Expand Down Expand Up @@ -629,3 +663,6 @@ Any Pull Request is welcome, however small your contribution may be ! There are,
[dep-sdl2-include-issue]: https://github.com/Rust-SDL2/rust-sdl2/pull/968
[gl-rs]: https://github.com/bjz/gl-rs
[pdev-issue]: https://github.com/PistonDevelopers/rust-empty/issues/175
[vcpkg]: https://github.com/microsoft/vcpkg
[cargo-vcpkg]: https://crates.io/crates/cargo-vcpkg
[cargo-vcpkg-usage]: #Windows,-Linux-and-macOS-with-vcpkg
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
In this file will be listed the changes, especially the breaking ones that one should be careful of
when upgrading from a version of rust-sdl2 to another.

### Unreleased

[PR #1009](https://github.com/Rust-SDL2/rust-sdl2/pull/1009)
Add support for linking to development libraries from vcpkg, and automatically setting up a vcpkg installation using `cargo-vcpkg`.

### v0.34.1

[PR #1004](https://github.com/Rust-SDL2/rust-sdl2/pull/1004) + [PR #1005](https://github.com/Rust-SDL2/rust-sdl2/pull/1005):
Expand Down
5 changes: 5 additions & 0 deletions sdl2-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ optional = true
version = "^0.3"
optional = true

[build-dependencies.vcpkg]
version = "^0.2.10"
optional = true

[build-dependencies.cmake]
version = "^0.1"
optional = true
Expand All @@ -50,6 +54,7 @@ cfg-if = "^0.1"

default = []
use-pkgconfig = ["pkg-config"]
use-vcpkg = ["vcpkg"]
use-bindgen = ["bindgen"]
static-link = []
use_mac_framework = []
Expand Down
34 changes: 33 additions & 1 deletion sdl2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ fn get_pkg_config() {
}
}

#[cfg(feature = "use-vcpkg")]
fn get_vcpkg_config() {
vcpkg::find_package("sdl2").unwrap();
if cfg!(feature = "image") {
vcpkg::find_package("sdl2-image").unwrap();
}
if cfg!(feature = "ttf") {
vcpkg::find_package("sdl2-ttf").unwrap();
}
if cfg!(feature = "mixer") {
vcpkg::find_package("sdl2-mixer").unwrap();
}
if cfg!(feature = "gfx") {
vcpkg::find_package("sdl2-gfx").unwrap();
}
}

// returns the location of the downloaded source
#[cfg(feature = "bundled")]
fn download_sdl2() -> PathBuf {
Expand Down Expand Up @@ -281,6 +298,15 @@ fn compute_include_paths() -> Vec<String> {
};
}

#[cfg(feature = "vcpkg")] {
// don't print the "cargo:xxx" directives, we're just trying to get the include paths here
let vcpkg_library = vcpkg::Config::new().cargo_metadata(false).probe("sdl2").unwrap();
for path in vcpkg_library.include_paths {
include_paths.push(format!("{}", path.display()));
};
}


include_paths
}

Expand All @@ -289,6 +315,12 @@ fn link_sdl2(target_os: &str) {
// prints the appropriate linking parameters when using pkg-config
// useless when using "bundled"
get_pkg_config();
}

#[cfg(all(feature = "use-vcpkg", not(feature = "bundled")))] {
// prints the appropriate linking parameters when using pkg-config
// useless when using "bundled"
get_vcpkg_config();
}

#[cfg(not(feature = "static-link"))] {
Expand Down Expand Up @@ -318,7 +350,7 @@ fn link_sdl2(target_os: &str) {
}

#[cfg(feature = "static-link")] {
if cfg!(feature = "bundled") || cfg!(feature = "use-pkgconfig") == false {
if cfg!(feature = "bundled") || (cfg!(feature = "use-pkgconfig") == false && cfg!(feature = "use-vcpkg") == false) {
println!("cargo:rustc-link-lib=static=SDL2main");
println!("cargo:rustc-link-lib=static=SDL2");
}
Expand Down