Skip to content

Commit

Permalink
Handle ASM features more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmooney committed Apr 21, 2022
1 parent 5e92bd1 commit eac0fe5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 22 deletions.
1 change: 0 additions & 1 deletion usdt-impl/build.rs

This file was deleted.

90 changes: 90 additions & 0 deletions usdt-impl/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2022 Oxide Computer Company
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::env;

use version_check;

#[derive(Copy, Clone)]
enum Backend {
// Standard (read: illumos) probe registration
Standard,
// MacOS linker-aware probe registration
Linker,
// Provide probe macros, but probes are no-ops (dtrace-less OSes)
NoOp,
}

fn main() {
println!("cargo:rerun-if-changed=build.rs");

// `asm` feature was stabilized in 1.59
let have_stable_asm = version_check::is_min_version("1.59").unwrap_or(false);
// XXX: `asm_sym` feature is not yet stable
let have_stable_asm_sym = false;

// Are we being built with a compiler which allows feature flags (nightly)
let is_nightly = version_check::is_feature_flaggable().unwrap_or(false);

let feat_asm = env::var_os("CARGO_FEATURE_ASM").is_some();
let feat_strict_asm = env::var_os("CARGO_FEATURE_STRICT_ASM").is_some();

let backend = match env::var("CARGO_CFG_TARGET_OS").ok().as_deref() {
Some("macos") if feat_asm => {
if have_stable_asm && have_stable_asm_sym {
Backend::Linker
} else if feat_strict_asm || is_nightly {
if !have_stable_asm {
println!("cargo:rustc-cfg=usdt_need_feat_asm");
}
if !have_stable_asm_sym {
println!("cargo:rustc-cfg=usdt_need_feat_asm_sym");
}
Backend::Linker
} else {
Backend::NoOp
}
}
Some("illumos") | Some("solaris") if feat_asm => {
if have_stable_asm {
Backend::Standard
} else if feat_strict_asm || is_nightly {
println!("cargo:rustc-cfg=usdt_need_feat_asm");
Backend::Standard
} else {
Backend::NoOp
}
}
_ => Backend::NoOp,
};

// Since visibility of the `asm!()` macro differs between the nightly feature and the
// stabilized version, the consumer requires information about its availability
if have_stable_asm {
println!("cargo:rustc-cfg=usdt_stable_asm");
}

match backend {
Backend::NoOp => {
println!("cargo:rustc-cfg=usdt_backend_noop");
}
Backend::Linker => {
println!("cargo:rustc-cfg=usdt_backend_linker");
}
Backend::Standard => {
println!("cargo:rustc-cfg=usdt_backend_standard");
}
}
}
33 changes: 12 additions & 21 deletions usdt-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(all(not(usdt_stable_asm), feature = "asm"), feature(asm))]
#![cfg_attr(usdt_need_feat_asm, feature(asm))]
#![cfg_attr(usdt_need_feat_asm_sym, feature(asm_sym))]

use serde::Deserialize;
use std::cell::RefCell;
use thiserror::Error;

#[cfg(all(
feature = "asm",
any(
all(not(target_os = "linux"), not(target_os = "macos")),
feature = "des",
)
))]
// Probe record parsing required for standard backend (and `des` feature used by `dusty util)
#[cfg(any(usdt_backend_standard, feature = "des"))]
pub mod record;

#[cfg_attr(any(target_os = "linux", not(feature = "asm")), allow(dead_code))]
mod common;

#[cfg_attr(
feature = "asm",
cfg_attr(target_os = "linux", path = "empty.rs"),
cfg_attr(target_os = "macos", path = "linker.rs"),
cfg_attr(
all(not(target_os = "linux"), not(target_os = "macos")),
path = "no-linker.rs"
)
)]
#[cfg_attr(not(feature = "asm"), path = "empty.rs")]
#[cfg_attr(usdt_backend_noop, path = "empty.rs")]
#[cfg_attr(usdt_backend_linker, path = "linker.rs")]
#[cfg_attr(usdt_backend_standard, path = "no-linker.rs")]
mod internal;

// Since the `empty` is mostly a no-op, parts of the common code will go unused when it is
// selected for use.
#[cfg_attr(usdt_backend_noop, allow(dead_code))]
mod common;

/// Register an application's probe points with DTrace.
///
/// This function collects information about the probe points defined in an application and ensures
Expand Down

0 comments on commit eac0fe5

Please sign in to comment.