Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
No bug - Revendor rust dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Servo VCS Sync committed Dec 7, 2017
1 parent 2e33cc5 commit 89d1e42
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 4 deletions.
1 change: 1 addition & 0 deletions third_party/rust/libudev-sys/.cargo-checksum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"files":{".travis.yml":"6e29c10e6c5b96b621e725b7af2fef291f431a47cef81efcf51073caabe57b35","Cargo.toml":"50ad3af7147b0e6b28f9bebb22033f388b5ed518bf7da73f178cc72bd062b8b0","LICENSE":"871afd9d691846de71e0b83812ba9c7ff00bc7b3ad102dedcaa109f2246d52ad","README.md":"01b006297beb0d6ce9aff02f7df5f34a05057dce7251e805100b6b623d793502","build.rs":"c7731bfbcd76e383fe40a71de83f27793519035234434a582e7687b6a1a74712","cross-build.sh":"a903117c37fe62160eb724668e23b97a6aae3f22a41f766ce77657107970e578","examples/hwdb_query.rs":"1c730f3fa9d04ef41800971ea5931e1d109c741ab49f02db89bbebd74b95b9a8","examples/list_devices.rs":"6e9935756e3c1e5101592f54165bf968f7999e34dc5d4002d95779fc0b0eaaed","examples/monitor.rs":"e36aef4c66369ba3dbb22f6221805ee26a068dde442eddf36915ca66676c9bf7","src/lib.rs":"aab80388d2f9e53bea69f23dc514aa4acf614b25d0052ccc5996392641250952"},"package":"249a1e347fa266dc3184ebc9b4dc57108a30feda16ec0b821e94b42be20b9355"}
23 changes: 23 additions & 0 deletions third_party/rust/libudev-sys/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
sudo: false

language: rust
rust:
- 1.0.0
- stable
- beta
- nightly

matrix:
allow_failures:
- rust: nightly

addons:
apt:
packages:
- build-essential
- libudev-dev

before_install:
- pkg-config --list-all
- pkg-config --libs libudev
- pkg-config --modversion libudev
19 changes: 19 additions & 0 deletions third_party/rust/libudev-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "libudev-sys"
version = "0.1.3"
authors = ["David Cuddeback <david.cuddeback@gmail.com>"]
description = "FFI bindings to libudev"
license = "MIT"
homepage = "https://github.com/dcuddeback/libudev-sys"
repository = "https://github.com/dcuddeback/libudev-sys"
readme = "README.md"
keywords = ["udev", "hardware", "bindings", "sysfs", "systemd"]

build = "build.rs"
links = "libudev"

[dependencies]
libc = "0.2"

[build-dependencies]
pkg-config = "0.3.2"
20 changes: 20 additions & 0 deletions third_party/rust/libudev-sys/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2015 David Cuddeback

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 80 additions & 0 deletions third_party/rust/libudev-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Libudev Rust Bindings

The `libudev-sys` crate provides declarations and linkage for the `libudev` C library. Following the
`*-sys` package conventions, the `libudev-sys` crate does not define higher-level abstractions over
the native `libudev` library functions.

## Dependencies
In order to use the `libudev-sys` crate, you must have a Linux system with the `libudev` library
installed where it can be found by `pkg-config`. To install `libudev` on Debian-based Linux
distributions, execute the following command:

```
sudo apt-get install libudev-dev
```

`libudev` is a Linux-specific package. It is not available for Windows, OSX, or other operating
systems.

### Cross-Compiling
To link to a cross-compiled version of the native `libudev` library, it's necessary to set several
environment variables to configure `pkg-config` to work with a cross-compiler's sysroot. [Autotools
Mythbuster](https://autotools.io/) has a good explanation of [supporting
cross-compilation](https://autotools.io/pkgconfig/cross-compiling.html) with `pkg-config`.

However, Rust's [`pkg-config` build helper](https://github.com/alexcrichton/pkg-config-rs) doesn't
support calling a `$CHOST`-prefixed `pkg-config`. It will always call `pkg-config` without a prefix.
To cross-compile `libudev-sys` with the `pkg-config` build helper, one must define the environment
variables `PKG_CONFIG_DIR`, `PKG_CONFIG_LIBDIR`, and `PKG_CONFIG_SYSROOT_DIR` for the *default*
`pkg-config`. It's also necessary to set `PKG_CONFIG_ALLOW_CROSS` to tell Rust's `pkg-config` helper
that it's okay to proceed with a cross-compile.

To adapt the `pkg-config` wrapper in the Autotools Mythbuster guide so that it works with Rust, one
will end up with a script similar to the following:

```sh
#!/bin/sh

SYSROOT=/build/root

export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}
export PKG_CONFIG_ALLOW_CROSS=1

cargo build
```

## Usage
Add `libudev-sys` as a dependency in `Cargo.toml`:

```toml
[dependencies]
libudev-sys = "0.1.1"
```

Import the `libudev_sys` crate and use the functions as they're defined in the native `libudev`
library. See the [`libudev` API documention](http://www.freedesktop.org/software/systemd/libudev/)
for more usage information.

```rust
extern crate libudev_sys as ffi;
```

The `libudev-sys` build script detects the presence of native `libudev` functions and exports the
functions found to exist. `libudev-sys` exports this information from its build script, which Cargo
will provide to the build scripts of dependent packages in the form of environment variables:

* `DEP_LIBUDEV_HWDB={true,false}`: The native `libudev` library has `udev_hwdb_*` functions. They will be
exported by `libudev-sys`.

### Finding Help
Since `libudev-sys` does nothing more than export symbols from the native `libudev` library, the
best source for help is the information already available for the native `libudev`:

* [API Documentation](http://www.freedesktop.org/software/systemd/libudev/)

## License
Copyright © 2015 David Cuddeback

Distributed under the [MIT License](LICENSE).
48 changes: 48 additions & 0 deletions third_party/rust/libudev-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
extern crate pkg_config;

use std::fs::{self,File};
use std::path::Path;
use std::process::Command;

use std::io::prelude::*;

fn check_func(function_name: &str) -> bool {
let test_file_path = format!("target/build/check_{}.rs", function_name);
let test_file_name = Path::new(&test_file_path[..]);

fs::create_dir_all("target/build").unwrap();

{
let mut test_file = File::create(test_file_name).unwrap();

writeln!(&mut test_file, "extern \"C\" {{").unwrap();
writeln!(&mut test_file, " fn {}();", function_name).unwrap();
writeln!(&mut test_file, "}}").unwrap();
writeln!(&mut test_file, "").unwrap();
writeln!(&mut test_file, "fn main() {{").unwrap();
writeln!(&mut test_file, " unsafe {{").unwrap();
writeln!(&mut test_file, " {}();", function_name).unwrap();
writeln!(&mut test_file, " }}").unwrap();
writeln!(&mut test_file, "}}").unwrap();
}

let output = Command::new("rustc").
arg(test_file_name).
arg("--out-dir").arg("target/build/").
arg("-l").arg("udev").
output().unwrap();

output.status.success()
}

fn main() {
pkg_config::find_library("libudev").unwrap();

if check_func("udev_hwdb_new") {
println!("cargo:rustc-cfg=hwdb");
println!("cargo:hwdb=true");
}
else {
println!("cargo:hwdb=false");
}
}
9 changes: 9 additions & 0 deletions third_party/rust/libudev-sys/cross-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

SYSROOT=${HOME}/x-tools/armv6-rpi-linux-gnueabi/armv6-rpi-linux-gnueabi/sysroot

export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${SYSROOT}/usr/share/pkgconfig/
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}

export PKG_CONFIG_ALLOW_CROSS=1
70 changes: 70 additions & 0 deletions third_party/rust/libudev-sys/examples/hwdb_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
extern crate libudev_sys as udev;
extern crate libc;


#[cfg(hwdb)] use std::env;
#[cfg(hwdb)] use std::str;
#[cfg(hwdb)] use std::ffi::{CString,CStr};
#[cfg(hwdb)] use libc::c_char;

#[cfg(hwdb)]
fn main() {

let args = env::args().collect::<Vec<_>>();

if args.len() != 2 {
println!("Usage: hwdb_query <modalias>");
return;
}

let query = CString::new(args[1].clone()).unwrap();

unsafe {
let udev = udev::udev_new();

if !udev.is_null() {
let hwdb = udev::udev_hwdb_new(udev);

if !hwdb.is_null() {
query_hwdb(hwdb, &query);
udev::udev_hwdb_unref(hwdb);
}

udev::udev_unref(udev);
}
}
}

#[cfg(hwdb)]
unsafe fn query_hwdb(hwdb: *mut udev::udev_hwdb, query: &CString) {
println!("{:>30}: {:?}", "query", query);

udev::udev_hwdb_ref(hwdb);
print_results(udev::udev_hwdb_get_properties_list_entry(hwdb, query.as_ptr(), 0));
udev::udev_hwdb_unref(hwdb);
}

#[cfg(hwdb)]
unsafe fn print_results(list_entry: *mut udev::udev_list_entry) {
if list_entry.is_null() {
return;
}

let key = unwrap_str(udev::udev_list_entry_get_name(list_entry));
let val = unwrap_str(udev::udev_list_entry_get_value(list_entry));

println!("{:>30}: {}", key, val);

print_results(udev::udev_list_entry_get_next(list_entry));
}

#[cfg(hwdb)]
unsafe fn unwrap_str<'a>(ptr: *const c_char) -> &'a str {
str::from_utf8(CStr::from_ptr(ptr).to_bytes()).unwrap()
}


#[cfg(not(hwdb))]
fn main() {
println!("hwdb is not supported by native libudev");
}
Loading

0 comments on commit 89d1e42

Please sign in to comment.