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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/// Tells Cargo to enable a `$feature`.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-cfg=$feature");
/// ```
///
/// # Examples
///
/// Useful for conditionally enabling certain code to run.
///
/// ```
/// # struct Cargo;
/// # impl Cargo {
/// #   fn can_bench(&self) -> bool { true }
/// # }
/// # let cargo = Cargo;
/// if cargo.can_bench() {
///     cargo_emit::rustc_cfg!("bench");
/// }
/// ```
///
/// Then outside of `build.rs`:
///
/// ```
/// #[cfg(bench)]
/// mod benches {
///     // ...
/// }
/// ```
#[macro_export]
macro_rules! rustc_cfg {
    ($feature:expr $(, $($args:tt)*)?) => {
        $crate::pair!("rustc-cfg", $feature $(, $($args)+)?);
    };
}

/// Tells Cargo to assign `$var` for the environment variable for `$key`.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-env=$var=$value");
/// ```
///
/// # Examples
///
/// Useful for injecting environment variables during the build.
///
/// The `$var` and `$value` parameters get concatenated into a single formatting
/// string. Formatting runtime values can be done by passing subsequent values.
///
/// ```
/// let git_rev_hash = //
/// # "0000111122223333444455556666777788889999";
/// cargo_emit::rustc_env!("MY_HASH", "{}", git_rev_hash);
/// ```
#[macro_export]
macro_rules! rustc_env {
    ($var:expr, $value:expr $(, $($args:tt)*)?) => {
        $crate::pair!("rustc-env", concat!($var, "=", $value) $(, $($args)+)?);
    };
}

/// Tells Cargo to pass `$flags` to the compiler.
///
/// As of this writing, only `-l` and `-L` flags are supported.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-flags=$flags");
/// ```
///
/// # Examples
///
/// The `$flags`  get concatenated into a single formatting
/// string. Formatting runtime values can be done by passing subsequent values.
///
/// ```
/// let git_rev_hash = //
/// # "0000111122223333444455556666777788889999";
/// cargo_emit::rustc_env!("MY_HASH", "{}", git_rev_hash);
/// ```
#[macro_export]
macro_rules! rustc_flags {
    ($($flags:literal),+ $(,)?) => {
        $($crate::pair!("rustc-flags", $flags);)+
    };
    ($($flags:expr),+ $(,)?) => {
        $($crate::pair!("rustc-flags", "{}", $flags);)+
    };
}

/// Tells Cargo to pass `-C link-arg=$flag` to the compiler.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-cdylib-link-arg=$flag");
/// ```
///
/// # Examples
///
/// Runtime arguments can be passed directly.
///
/// ```
/// let flag1 = // ...
/// # "";
/// let flag2 = // ...
/// # "";
/// cargo_emit::rustc_cdylib_link_arg!(flag1, flag2);
/// ```
#[macro_export]
macro_rules! rustc_cdylib_link_arg {
    ($($flag:literal),+ $(,)?) => {
        $($crate::pair!("rustc-cdylib-link-arg", $flag);)+
    };
    ($($flag:expr),+ $(,)?) => {
        $($crate::pair!("rustc-cdylib-link-arg", "{}", $flag);)+
    };
}

/// Tells Cargo to pass `$lib` to the compiler as a `-l` flag.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-link-lib=[$kind=]$name");
/// ```
///
/// # Examples
///
/// Useful for telling the linker what libraries should be linked.
///
/// ```
/// cargo_emit::rustc_link_lib! {
///     "ssl", // same as `=> "dylib"`
///     "ruby" => "static",
///     "CoreFoundation" => "framework",
/// }
/// ```
#[macro_export]
macro_rules! rustc_link_lib {
    ($name:literal $(,)?) => {
        $crate::pair!("rustc-link-lib", $name);
    };
    ($name:expr $(,)?) => {
        $crate::pair!("rustc-link-lib", "{}", $name);
    };
    ($name:literal => $kind:literal $(,)?) => {
        $crate::pair!("rustc-link-lib", concat!($kind, "=", $name));
    };
    ($name:expr => $kind:expr $(,)?) => {
        $crate::pair!("rustc-link-lib", "{}={}", $kind, $name);
    };
    ($($name:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $($crate::rustc_link_lib!($name $(=> $kind)?);)+
    } };
}

/// Tells Cargo to pass `$path` to the compiler as a `-L` flag.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-link-search=[$kind=]$path");
/// ```
///
/// # Examples
///
/// Useful for telling the linker where a library can be found.
///
/// ```
/// cargo_emit::rustc_link_search!(
///     "path/to/ssl/lib/", // same as `=> "all"`
///     "path/to/ruby/lib/" => "native",
/// );
/// ```
#[macro_export]
macro_rules! rustc_link_search {
    ($path:literal $(,)?) => {
        $crate::pair!("rustc-link-search", $path);
    };
    ($path:expr $(,)?) => {
        $crate::pair!("rustc-link-search", "{}", $path);
    };
    ($path:literal => $kind:literal $(,)?) => {
        $crate::pair!("rustc-link-search", concat!($kind, "=", $path));
    };
    ($path:expr => $kind:expr $(,)?) => {
        $crate::pair!("rustc-link-search", "{}={}", $kind, $path);
    };
    ($($path:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $($crate::rustc_link_lib!($path $(=> $kind)?);)+
    } };
}