Skip to content

Commit

Permalink
fix(aws-lc-sys): add links attribute to Cargo.toml (#277)
Browse files Browse the repository at this point in the history
* fix(aws-lc-sys): add `links` attribute to Cargo.toml

* fix failing ci tasks

* use toml_edit to read expected links value
  • Loading branch information
camshaft authored Nov 16, 2023
1 parent a3a2086 commit 27d447f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ members = [
"aws-lc-rs",
"aws-lc-sys",
"aws-lc-fips-sys",
"aws-lc-rs-testing"
"aws-lc-rs-testing",
"aws-lc-sys-testing",
]
resolver = "2"

Expand Down
15 changes: 15 additions & 0 deletions aws-lc-sys-testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "aws-lc-sys-testing"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
aws-lc-sys = { path = "../aws-lc-sys" }

[build-dependencies]
cc = "1"
toml_edit = "0.21"

[package.metadata.cargo-udeps.ignore]
normal = [ "aws-lc-sys" ] # the sys crate is only used through a C library build
26 changes: 26 additions & 0 deletions aws-lc-sys-testing/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

use toml_edit::Document;

fn main() {
let cargo_toml = std::fs::read_to_string("../aws-lc-sys/Cargo.toml").unwrap();
let cargo_toml = cargo_toml.parse::<Document>().unwrap();

let links = cargo_toml["package"]["links"].as_str().unwrap();

// ensure that the include path is exported and set up correctly
cc::Build::new()
.include(env(format!("DEP_{}_INCLUDE", links.to_uppercase())))
.file("src/testing.c")
.compile("aws_ls_sys_testing");

// ensure the libcrypto artifact is linked
println!("cargo:rustc-link-lib={}_crypto", links);
}

fn env<S: AsRef<str>>(s: S) -> String {
let s = s.as_ref();
println!("cargo:rerun-if-env-changed={s}");
std::env::var(s).unwrap_or_else(|_| panic!("missing env var {s}"))
}
18 changes: 18 additions & 0 deletions aws-lc-sys-testing/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

use std::ffi::c_int;

extern "C" {
fn testing_evp_key_type(nid: c_int) -> c_int;
}

fn main() {
let v = unsafe { testing_evp_key_type(123) };
println!("Hello EVP {v}!");
}

#[test]
fn link_test() {
let _ = unsafe { testing_evp_key_type(123) };
}
7 changes: 7 additions & 0 deletions aws-lc-sys-testing/src/testing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <openssl/is_awslc.h>
#include <openssl/evp.h>

int testing_evp_key_type(int nid)
{
return EVP_PKEY_type(nid);
}
2 changes: 2 additions & 0 deletions aws-lc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "aws-lc-sys"
description = "AWS-LC is a general-purpose cryptographic library maintained by the AWS Cryptography team for AWS and their customers. It іs based on code from the Google BoringSSL project and the OpenSSL project."
version = "0.12.0"
links = "aws_lc_0_12_0"
authors = ["AWS-LC"]
edition = "2021"
repository = "https://github.com/aws/aws-lc-rs"
Expand Down Expand Up @@ -51,6 +52,7 @@ bindgen = ["dep:bindgen"] # Generate the bindings on the targetted platform as a
[build-dependencies]
cmake = "0.1.48"
dunce = "1.0"
fs_extra = "1"

This comment has been minimized.

Copy link
@ctz

ctz Nov 17, 2023

Contributor

This should be fs_extra = "1.3" because, for example, CopyOptions::skip_exist does not exist in version 1.

Ideally the aws-lc-rs would have a -Z minimal-versions job to validate these versions do select usable versions.


[target.'cfg(any(all(target_os = "macos", target_arch = "x86_64"), all(target_os = "linux", target_arch = "x86"), all(target_os = "linux", target_arch = "x86_64"), all(target_os = "linux", target_arch = "aarch64")))'.build-dependencies]
bindgen = { version = "0.69.1", optional = true }
Expand Down
75 changes: 51 additions & 24 deletions aws-lc-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,16 @@ impl OutputLibType {

impl OutputLib {
fn libname(self, prefix: Option<&str>) -> String {
format!(
"{}{}",
if let Some(pfix) = prefix { pfix } else { "" },
match self {
OutputLib::Crypto => "crypto",
OutputLib::Ssl => "ssl",
OutputLib::RustWrapper => {
"rust_wrapper"
}
}
)
let name = match self {
OutputLib::Crypto => "crypto",
OutputLib::Ssl => "ssl",
OutputLib::RustWrapper => "rust_wrapper",
};
if let Some(prefix) = prefix {
format!("{prefix}_{name}")
} else {
name.to_string()
}
}
}

Expand Down Expand Up @@ -200,7 +199,7 @@ fn prepare_cmake_build(manifest_dir: &PathBuf, build_prefix: String) -> cmake::C
}

fn build_rust_wrapper(manifest_dir: &PathBuf) -> PathBuf {
prepare_cmake_build(manifest_dir, prefix_string())
prepare_cmake_build(manifest_dir, prefix_string() + "_")
.configure_arg("--no-warn-unused-cli")
.build()
}
Expand Down Expand Up @@ -374,18 +373,10 @@ fn main() {
RustWrapper.libname(Some(&prefix))
);

for include_path in [
get_rust_include_path(&manifest_dir),
get_generated_include_path(&manifest_dir),
get_aws_lc_include_path(&manifest_dir),
] {
println!("cargo:include={}", include_path.display());
}
if let Some(include_paths) = get_aws_lc_sys_includes_path() {
for path in include_paths {
println!("cargo:include={}", path.display());
}
}
println!(
"cargo:include={}",
setup_include_paths(&out_dir, &manifest_dir).display()
);

println!("cargo:rerun-if-changed=builder/");
println!("cargo:rerun-if-changed=aws-lc/");
Expand All @@ -407,3 +398,39 @@ fn check_dependencies() {
"Required build dependency is missing. Halting build."
);
}

fn setup_include_paths(out_dir: &Path, manifest_dir: &Path) -> PathBuf {
let mut include_paths = vec![
get_rust_include_path(manifest_dir),
get_generated_include_path(manifest_dir),
get_aws_lc_include_path(manifest_dir),
];

if let Some(extra_paths) = get_aws_lc_sys_includes_path() {
include_paths.extend(extra_paths);
}

let include_dir = out_dir.join("include");
std::fs::create_dir_all(&include_dir).unwrap();

// iterate over all of the include paths and copy them into the final output
for path in include_paths {
for child in std::fs::read_dir(path).into_iter().flatten().flatten() {
if child.file_type().map_or(false, |t| t.is_file()) {
let _ = std::fs::copy(
child.path(),
include_dir.join(child.path().file_name().unwrap()),
);
continue;
}

// prefer the earliest paths
let options = fs_extra::dir::CopyOptions::new()
.skip_exist(true)
.copy_inside(true);
let _ = fs_extra::dir::copy(child.path(), &include_dir, &options);
}
}

include_dir
}

0 comments on commit 27d447f

Please sign in to comment.