shadow_build_common/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md
#![deny(unsafe_op_in_unsafe_fn)]

use std::path::Path;

pub struct ShadowBuildCommon {
    deps: Option<system_deps::Dependencies>,
    build_src_root: Box<Path>,
    src_root: Box<Path>,
}

impl ShadowBuildCommon {
    pub fn new(repo_root: &Path, system_deps: Option<system_deps::Dependencies>) -> Self {
        let src_root = {
            let mut p = repo_root.to_path_buf();
            p.push("src");
            p.into_boxed_path()
        };

        let build_src_root = {
            let mut p = repo_root.to_path_buf();
            p.push("build");
            p.push("src");
            p.into_boxed_path()
        };

        // Conservatively re-run build scripts if anything in their package directory
        // changes.
        println!("cargo:rerun-if-changed=.");

        Self {
            deps: system_deps,
            build_src_root,
            src_root,
        }
    }

    pub fn cc_build(&self) -> cc::Build {
        let mut b = cc::Build::new();
        println!("cargo:rerun-if-env-changed=CC");
        println!("cargo:rerun-if-env-changed=CXX");
        println!("cargo:rerun-if-env-changed=CFLAGS");
        println!("cargo:rerun-if-env-changed=CXXFLAGS");

        // When adding flags here, consider using `add_compile_options`
        // in the root CMakeLists.txt instead, where they will be picked
        // up both here and in our remaining pure C targets.
        b.define("_GNU_SOURCE", None)
            .include(&*self.build_src_root)
            .include(&*self.src_root)
            // Disable extra warnings (-Wall, -Wextra) until if and when they're
            // fixed in our C code.
            .warnings(false)
            // By default, *don't* convert any remaining warnings into errors (-Werror).
            // -Werror is currently enabled here via CFLAGS, which
            // cmake sets depending on the option SHADOW_WERROR.
            .warnings_into_errors(false);

        if let Some(deps) = &self.deps {
            b.includes(deps.all_include_paths());
        }

        if let Some("true") = std::env::var("DEBUG").ok().as_deref() {
            b.flag("-DDEBUG")
                // we only check for unused functions when builing in debug mode since some
                // functions are only called when logging, which can be #ifdef'd out in
                // release mode
                .flag("-Wunused-function");
        } else {
            b.flag("-DNDEBUG");
        }

        b
    }

    #[cfg(feature = "bindgen")]
    pub fn bindgen_builder(&self) -> bindgen::Builder {
        let mut builder = bindgen::Builder::default()
            // Tell cargo to invalidate the built crate whenever any of the
            // included header files changed.
            .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
            .clang_args(&[
                &format!("-I{}", self.build_src_root.to_str().unwrap()),
                &format!("-I{}", self.src_root.to_str().unwrap()),
                "-D_GNU_SOURCE",
            ])
            //# used to generate #[must_use] annotations)
            .enable_function_attribute_detection();

        if let Some(deps) = &self.deps {
            for path in deps.all_include_paths() {
                builder = builder.clang_args(&[format!("-I{}", path.to_str().unwrap())]);
            }
        }
        builder
    }

    #[cfg(feature = "cbindgen")]
    pub fn cbindgen_base_config(&self) -> cbindgen::Config {
        let header = "
/*
 * The Shadow Simulator
 * See LICENSE for licensing information
 */
// clang-format off";

        cbindgen::Config {
            language: cbindgen::Language::C,
            line_length: 100,
            documentation_style: cbindgen::DocumentationStyle::C99,
            macro_expansion: cbindgen::MacroExpansionConfig {
                bitflags: true,
            },
            header: Some(header.into()),
            autogen_warning: Some(
                "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
                    .into(),
            ),
            enumeration: cbindgen::EnumConfig {
                prefix_with_name: true,
                rename_variants: cbindgen::RenameRule::ScreamingSnakeCase,
                ..cbindgen::EnumConfig::default()
            },
            function: cbindgen::FunctionConfig {
                must_use: Some("__attribute__((warn_unused_result))".into()),
                no_return: Some("__attribute__((noreturn))".into()),
                ..cbindgen::FunctionConfig::default()
            },
            export: cbindgen::ExportConfig {
                rename: std::collections::HashMap::from([
                    ("timeval".into(), "struct timeval".into()),
                    ("timespec".into(), "struct timespec".into()),
                ]),
                // All types.
                item_types: vec![
                    cbindgen::ItemType::Enums,
                    cbindgen::ItemType::Constants,
                    cbindgen::ItemType::Globals,
                    cbindgen::ItemType::Structs,
                    cbindgen::ItemType::Unions,
                    cbindgen::ItemType::Typedefs,
                    cbindgen::ItemType::OpaqueItems,
                    cbindgen::ItemType::Functions,
                ],
                ..cbindgen::ExportConfig::default()
            },
            ..Default::default()
        }
    }
}

#[cfg(feature = "cbindgen")]
pub trait CBindgenExt {
    fn get_mut(&mut self) -> &mut cbindgen::Config;

    // Export the given types opaquely.
    //
    // This overrides cbindgen's behavior of making any `repr(C)` type
    // non-opaque.
    // https://github.com/eqrion/cbindgen/issues/104
    fn add_opaque_types(&mut self, types: &[&str]) {
        let c = self.get_mut();
        if types.is_empty() {
            return;
        }
        if c.after_includes.is_none() {
            c.after_includes.replace("".into());
        }
        for t in types {
            c.after_includes
                .as_mut()
                .unwrap()
                .push_str(&format!("typedef struct {t} {t};\n"));
            c.export.exclude.push((*t).into());
        }
    }
}

#[cfg(feature = "cbindgen")]
impl CBindgenExt for cbindgen::Config {
    fn get_mut(&mut self) -> &mut cbindgen::Config {
        self
    }
}