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

Add a vendored feature that uses a vendored copy #4

Closed
wants to merge 17 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libftdi1-vendored/libftdi"]
path = sys-vendored-lgpl/libftdi
url = git://developer.intra2net.com/libftdi
37 changes: 5 additions & 32 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
[package]
name = "libftdi1-sys"
version = "0.2.0"
authors = ["Denis Lisov <[email protected]>"]
keywords = ["bindings", "hardware","ftdi", "libftdi1"]
description = "FFI bindings for libftdi1"
license = "MIT"
homepage = "https://github.com/tanriol/libftdi1-sys"
repository = "https://github.com/tanriol/libftdi1-sys"
edition = "2018"

build = "build.rs"

[features]
default = ["bindgen"]

[dependencies]
cfg-if = "0.1.10"
libc = "0.2"

[build-dependencies]
cfg-if = "0.1.10"

[target.'cfg(not(all(windows, target_env="msvc")))'.build-dependencies]
pkg-config = "0.3"

[target.'cfg(all(windows, target_env="msvc"))'.build-dependencies]
vcpkg = "0.2"

[build-dependencies.bindgen]
version = "0.53.2"
optional = true
[workspace]
members = [
"sys",
"sys-vendored-lgpl",
]
502 changes: 502 additions & 0 deletions LICENSE-LGPL

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.
File renamed without changes.
14 changes: 14 additions & 0 deletions common/tests/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT

// This file is intended to be included as a module in integration tests after `use crate_name::*;`

use super::*;

#[test]
fn smoke_test() {
unsafe {
let context = ftdi_new();
assert!(!context.is_null());
ftdi_free(context);
}
}
40 changes: 40 additions & 0 deletions sys-vendored-lgpl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "libftdi1-sys-vendored-lgpl"
version = "1.0.0-alpha1"
authors = [
"Denis Lisov <[email protected]>",
"The libFTDI developers",
]
edition = "2018"
description = "The LGPL version of libftdi1-sys including a vendored copy of libftdi1. You should not use this crate directly."
repository = "https://github.com/tanriol/libftdi1-sys/"
license = "LGPL-2.1"
include = [
"/Cargo.toml",
"/lgpl-2.1.txt",
"/README.md",
"/src",
"/*.rs",
"/*.h",
"/libftdi/src/*.c",
"/libftdi/src/*.h",
]
publish = false # for now

[badges]
maintenance = { status = "passively-maintained" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2"

[build-dependencies]
cc = "1.0.50"
cfg-if = "0.1.10"

[target.'cfg(not(all(windows, target_env="msvc")))'.build-dependencies]
pkg-config = "0.3.7"

[target.'cfg(all(windows, target_env="msvc"))'.build-dependencies]
vcpkg = "0.2"
26 changes: 26 additions & 0 deletions sys-vendored-lgpl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# About

This crate contains and builds the vendored copy of `libftdi1` for use by `libftdi1-sys`.
Note that it does not provide any bindings on its own, but leaves that to `libftdi1-sys`.

The reason this crate exists is that `libftdi1` primary library components are licensed under `LGPL-2.1`, unlike `libftdi1-sys`, which is under `MIT OR Apache-2.0`.
Thus they are separated into two different crates that have different licenses to make this obvious to license checking tools.

If you see this crate in your dependency tree, LGPL 2.1 licensed code will be linked into the final executable.
If you're not sure whether that's fine for your purpose, please see the `GNU LGPL 2.1` license text [GNU LGPL 2.1][online] or in the `lgpl-2.1.txt` file in this crate and/or consult a lawyer.

[GNU LGPL 2.1]: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt

# Rust version support

This crate supports all Rust versions that support Rust 2018 edition (i.e. 1.31.0 and higher).

# Maintenance

This crate is passively maintained. There are a coulpe directions for possible contributions:
- support for building in non-Linux environments
- cross-compilation support

If a new upstream version is released, the following steps are needed to update:
- update the submodule to the new tag
- update the `ftdi_version_i.h` header file in the crate root based on the upstream template
33 changes: 33 additions & 0 deletions sys-vendored-lgpl/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT

use std::path::PathBuf;

fn main() {
// Find include locations
let include_paths = link_and_get_include_paths();

let mut libftdi = cc::Build::new();
for path in include_paths {
libftdi.include(path);
}
libftdi
.files(&["libftdi/src/ftdi.c", "libftdi/src/ftdi_stream.c"])
.include(".")
.compile("ftdi1-vendored");
}

cfg_if::cfg_if! {
if #[cfg(all(windows, target_env="msvc"))] {
fn link_and_get_include_paths() -> Vec<PathBuf> {
let libusb = vcpkg::find_package("libusb-1.0")
.expect("libusb is required for libftdi");
libusb.include_paths
}
} else {
fn link_and_get_include_paths() -> Vec<PathBuf> {
let libusb = pkg_config::probe_library("libusb-1.0")
.expect("libusb is required for libftdi");
libusb.include_paths
}
}
}
11 changes: 11 additions & 0 deletions sys-vendored-lgpl/ftdi_version_i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef FTDI_VERSION_INTERNAL_H
#define FTDI_VERSION_INTERNAL_H

#define FTDI_MAJOR_VERSION 1
#define FTDI_MINOR_VERSION 4
#define FTDI_MICRO_VERSION 0

const char FTDI_VERSION_STRING[] = "1.4";
const char FTDI_SNAPSHOT_VERSION[] = "unknown";

#endif
1 change: 1 addition & 0 deletions sys-vendored-lgpl/lgpl-2.1.txt
1 change: 1 addition & 0 deletions sys-vendored-lgpl/libftdi
Submodule libftdi added at d5c162
19 changes: 19 additions & 0 deletions sys-vendored-lgpl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

//! *Attention*: this is an internal crate, you should depend on `libftdi1-sys` instead!
//!
//! libftdi1 is a library for working with FTDI chips like FT232BM, FT245BM,
//! FT2232C, FT2232D, FT245R, FT232H, FT230X The documentation for it is
//! available at http://www.intra2net.com/en/developer/libftdi/documentation/
//!
//! This crate includes the LGPL code for libftdi1 (the vendored copy that `libftdi1-sys[vendored]`
//! uses and thus is licensed under LGPL 2.1. If you're seeing this crate in your dependency
//! tree, this means that LGPL 2.1 licensed code will be compiled into your executable.
//!
//! For more detail see the README.md file in this crate and/or its git repository.

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!("pregenerated.rs");
1 change: 1 addition & 0 deletions sys-vendored-lgpl/src/pregenerated.rs
5 changes: 5 additions & 0 deletions sys-vendored-lgpl/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MIT

use libftdi1_sys_vendored_lgpl::*;

mod shared;
1 change: 1 addition & 0 deletions sys-vendored-lgpl/tests/shared/mod.rs
34 changes: 34 additions & 0 deletions sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "libftdi1-sys"
version = "1.0.0-alpha1"
authors = ["Denis Lisov <[email protected]>"]
keywords = ["bindings", "hardware","ftdi", "libftdi1"]
categories = ["external-ffi-bindings", "hardware-support"]
description = "FFI bindings for libftdi1"
license = "MIT"
homepage = "https://github.com/tanriol/libftdi1-sys"
repository = "https://github.com/tanriol/libftdi1-sys"
edition = "2018"

build = "build.rs"

[features]
vendored = ["libftdi1-sys-vendored-lgpl"]

[dependencies]
cfg-if = "0.1.10"
libc = "0.2"
libftdi1-sys-vendored-lgpl = {version = "1.0.0-alpha1", optional = true, path = "../sys-vendored-lgpl"}

[build-dependencies]
cfg-if = "0.1.10"

[target.'cfg(not(all(windows, target_env="msvc")))'.build-dependencies]
pkg-config = "0.3.7"

[target.'cfg(all(windows, target_env="msvc"))'.build-dependencies]
vcpkg = "0.2"

[build-dependencies.bindgen]
version = "0.53.2"
optional = true
1 change: 1 addition & 0 deletions sys/LICENSE-MIT
10 changes: 8 additions & 2 deletions build.rs → sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: MIT

cfg_if::cfg_if! {
if #[cfg(feature = "bindgen")] {
use bindgen;
Expand All @@ -9,15 +11,19 @@ cfg_if::cfg_if! {

fn main() {
cfg_if::cfg_if! {
if #[cfg(all(windows, target_env="msvc"))] {
if #[cfg(feature = "vendored")] {
// Do nothing, building and linking to the library is on `libftdi1-vendored-lgpl`
} else if #[cfg(all(windows, target_env="msvc"))] {
vcpkg::find_package("libftdi1").unwrap();
} else {
pkg_config::Config::new().atleast_version("1.4").probe("libftdi1").unwrap();
}
}

cfg_if::cfg_if! {
if #[cfg(feature = "bindgen")] {
if #[cfg(feature = "vendored")] {
// Do nothing, binding generation is on `libftdi1-vendored-lgpl`
} else if #[cfg(feature = "bindgen")] {
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.default_enum_style(bindgen::EnumVariation::NewType{ is_bitfield : false })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: MIT

//! A simple test script for testing the loopback features
//! of the FTDI DLP-HS-FPGA3's default FPGA firmware.
//! Mainly used as a compile test
Expand Down
70 changes: 70 additions & 0 deletions sys/examples/echo-tester.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT

//! Test with remote echo hardware
//! Mainly used as a compile test

use libftdi1_sys as ftdi;
use std::os::raw::c_int;

fn write_unchecked(context: *mut ftdi::ftdi_context, data: Vec<u8>) {
unsafe {
ftdi::ftdi_write_data(context, data.as_ptr(), data.len() as i32);
}
}

fn read_unchecked(context: *mut ftdi::ftdi_context, max_bytes: c_int) -> Vec<u8> {
let mut data: Vec<u8> = vec![0; max_bytes as usize];
unsafe {
let bytes_read = ftdi::ftdi_read_data(context, data.as_mut_ptr(), max_bytes);
data.truncate(bytes_read as usize);
}
data
}

// No error checking here, beware!
fn main() {
println!("Starting tester...");
let context = unsafe { ftdi::ftdi_new() };
let interface = if std::env::args().nth(1) == Some("--interface-b".into()) {
ftdi::ftdi_interface::INTERFACE_B
} else {
ftdi::ftdi_interface::INTERFACE_A
};
unsafe {
ftdi::ftdi_set_interface(context, interface);
}

if unsafe { ftdi::ftdi_usb_open(context, 0x0403, 0x6010) } == 0 {
println!("Device found and opened");
unsafe {
ftdi::ftdi_usb_reset(context);
ftdi::ftdi_usb_purge_buffers(context);
ftdi::ftdi_set_latency_timer(context, 2);
ftdi::ftdi_set_baudrate(context, 115_200);
}

// Junk test
let buffer = read_unchecked(context, 1024);
if buffer.len() > 0 {
println!("Junk in line: {:?}", buffer);
}

for num in 0u16..256 {
let num = num as u8;

// Loopback test
write_unchecked(context, vec![num]);
let reply = read_unchecked(context, 100);
if reply != vec![num] {
println!("Wrong loopback reply {:?} (expected {:?}", reply, vec![num]);
}
}
println!("Testing finished");
} else {
println!("Cannot find/open device, runtime tests are NOP");
}

unsafe {
ftdi::ftdi_free(context);
}
}
Loading