├── .github └── workflows │ └── rust.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples └── macros.rs └── src ├── lib.rs ├── pair.rs ├── rerun_if_changed.rs ├── rerun_if_env_changed.rs ├── rustc_cdylib_link_arg.rs ├── rustc_cfg.rs ├── rustc_env.rs ├── rustc_flags.rs ├── rustc_link_arg.rs ├── rustc_link_arg_bin.rs ├── rustc_link_arg_bins.rs ├── rustc_link_lib.rs ├── rustc_link_search.rs └── warning.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | check: 14 | name: Cargo Check 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions-rs/toolchain@v1 19 | with: 20 | profile: minimal 21 | toolchain: stable 22 | override: true 23 | - uses: actions-rs/cargo@v1 24 | with: 25 | command: check 26 | 27 | test: 28 | name: Cargo Test 29 | runs-on: ubuntu-latest 30 | steps: 31 | - uses: actions/checkout@v2 32 | - uses: actions-rs/toolchain@v1 33 | with: 34 | profile: minimal 35 | toolchain: stable 36 | override: true 37 | components: rust-src 38 | - uses: actions-rs/cargo@v1 39 | with: 40 | command: test 41 | 42 | fmt: 43 | name: Cargo Format 44 | runs-on: ubuntu-latest 45 | steps: 46 | - uses: actions/checkout@v2 47 | - uses: actions-rs/toolchain@v1 48 | with: 49 | profile: minimal 50 | toolchain: stable 51 | override: true 52 | - run: rustup component add rustfmt 53 | - uses: actions-rs/cargo@v1 54 | with: 55 | command: fmt 56 | args: --all -- --check 57 | 58 | clippy: 59 | name: Cargo Clippy 60 | runs-on: ubuntu-latest 61 | steps: 62 | - uses: actions/checkout@v2 63 | - uses: actions-rs/toolchain@v1 64 | with: 65 | profile: minimal 66 | toolchain: stable 67 | override: true 68 | - run: rustup component add clippy 69 | - uses: actions-rs/cargo@v1 70 | with: 71 | command: clippy 72 | args: -- -D warnings 73 | - uses: actions-rs/cargo@v1 74 | with: 75 | command: clippy 76 | args: --example "macros" --all -- -W clippy::all -W clippy::pedantic -W clippy::restriction -D warnings 77 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/macos,linux,windows,rust 3 | # Edit at https://www.gitignore.io/?templates=macos,linux,windows,rust 4 | 5 | ### Linux ### 6 | *~ 7 | 8 | # temporary files which can be created if a process still has a handle open of a deleted file 9 | .fuse_hidden* 10 | 11 | # KDE directory preferences 12 | .directory 13 | 14 | # Linux trash folder which might appear on any partition or disk 15 | .Trash-* 16 | 17 | # .nfs files are created when an open file is removed but is still being accessed 18 | .nfs* 19 | 20 | ### macOS ### 21 | # General 22 | .DS_Store 23 | .AppleDouble 24 | .LSOverride 25 | 26 | # Icon must end with two \r 27 | Icon 28 | 29 | # Thumbnails 30 | ._* 31 | 32 | # Files that might appear in the root of a volume 33 | .DocumentRevisions-V100 34 | .fseventsd 35 | .Spotlight-V100 36 | .TemporaryItems 37 | .Trashes 38 | .VolumeIcon.icns 39 | .com.apple.timemachine.donotpresent 40 | 41 | # Directories potentially created on remote AFP share 42 | .AppleDB 43 | .AppleDesktop 44 | Network Trash Folder 45 | Temporary Items 46 | .apdisk 47 | 48 | ### Rust ### 49 | # Generated by Cargo 50 | # will have compiled files and executables 51 | /target/ 52 | 53 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 54 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 55 | Cargo.lock 56 | 57 | # These are backup files generated by rustfmt 58 | **/*.rs.bk 59 | 60 | ### Windows ### 61 | # Windows thumbnail cache files 62 | Thumbs.db 63 | Thumbs.db:encryptable 64 | ehthumbs.db 65 | ehthumbs_vista.db 66 | 67 | # Dump file 68 | *.stackdump 69 | 70 | # Folder config file 71 | [Dd]esktop.ini 72 | 73 | # Recycle Bin used on file shares 74 | $RECYCLE.BIN/ 75 | 76 | # Windows Installer files 77 | *.cab 78 | *.msi 79 | *.msix 80 | *.msm 81 | *.msp 82 | 83 | # Windows shortcuts 84 | *.lnk 85 | 86 | # End of https://www.gitignore.io/api/macos,linux,windows,rust 87 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog [![Crates.io][crate-badge]][crate] 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog] and this project adheres to 6 | [Semantic Versioning]. 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.2.1] - 2021-09-01 11 | 12 | ### Fixed 13 | 14 | - Added `#[allow(clippy::explicit_write)]` to prevent false-positives. 15 | - Added `examples/macros.rs` file and corresponding `cargo clippy --example "macros" -- -D warnings` CI job to prevent future clippy issues. 16 | 17 | ## [0.2.0] - 2021-08-31 18 | 19 | ### Fixed 20 | 21 | - Fixed recursion in `rustc_link_search!(…)`. 22 | - Removed redundant `$x:literal` macro patterns. 23 | 24 | ### Added 25 | 26 | - [`rustc_link_arg!`](https://docs.rs/cargo-emit/0.2.0/cargo_emit/macro.rustc_link_arg.html) 27 | - [`rustc_link_arg_bin!`](https://docs.rs/cargo-emit/0.2.0/cargo_emit/macro.rustc_link_arg_bin.html) 28 | - [`rustc_link_arg_bins!`](https://docs.rs/cargo-emit/0.2.0/cargo_emit/macro.rustc_link_arg_bins.html) 29 | - 40 snapshot tests (using [insta](https://crates.io/crates/insta)) 30 | - Optional `to:` parameter for writing into a `std::fmt::Write` or `io::fmt::Write`. 31 | 32 | # Removed 33 | 34 | - `#![no_std]`, since the project never worked on no_std to begin with. 35 | 36 | ## [0.1.1] - 2019-12-15 37 | 38 | ### Fixed 39 | 40 | - Links to docs for specific macros. 41 | 42 | ## 0.1.0 - 2019-12-15 43 | 44 | ### Added 45 | 46 | - [`pair!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.pair.html) 47 | - [`rerun_if_changed!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rerun_if_changed.html) 48 | - [`rerun_if_env_changed!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rerun_if_env_changed.html) 49 | - [`rustc_cdylib_link_arg!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_cdylib_link_arg.html) 50 | - [`rustc_cfg!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_cfg.html) 51 | - [`rustc_env!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_env.html) 52 | - [`rustc_flags!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_flags.html) 53 | - [`rustc_link_lib!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_lib.html) 54 | - [`rustc_link_search!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_search.html) 55 | - [`warning!`](https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.warning.html) 56 | 57 | [crate]: https://crates.io/crates/cargo-emit 58 | [crate-badge]: https://img.shields.io/crates/v/cargo-emit.svg 59 | 60 | [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ 61 | [Semantic Versioning]: http://semver.org/spec/v2.0.0.html 62 | 63 | [Unreleased]: https://github.com/nvzqz/static-assertions-rs/compare/v0.1.1...HEAD 64 | [0.1.1]: https://github.com/nvzqz/static-assertions-rs/compare/v0.1.0...v0.1.1 65 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cargo-emit" 3 | version = "0.2.1" 4 | authors = ["Nikolai Vazquez", "Vincent Esche"] 5 | license = "MIT OR Apache-2.0" 6 | readme = "README.md" 7 | homepage = "https://github.com/nvzqz/cargo-emit" 8 | repository = "https://github.com/nvzqz/cargo-emit" 9 | documentation = "https://docs.rs/cargo-emit" 10 | description = "Talk to Cargo easily at build time." 11 | categories = ["development-tools::build-utils", "no-std", "rust-patterns"] 12 | keywords = ["cargo", "build", "print", "warn"] 13 | include = ["Cargo.toml", "src", "README.md", "CHANGELOG.md", "LICENSE*"] 14 | edition = "2018" 15 | 16 | [dev-dependencies] 17 | insta = "1.7.2" 18 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nikolai Vazquez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 6 | 7 |

Cargo Emit

8 | 9 | Crates.io 10 | Downloads 11 | 12 | 13 | Build Status 14 | 15 | rustc ^1.31.0 16 |
17 | 18 | Become a Patron! 19 | 20 | 21 | Buy me a coffee 22 | 23 |

24 |
25 | 26 | Talk to Cargo easily at build time, brought to you by [Nikolai Vazquez]. 27 | 28 | This library provides: 29 | 30 | - Convenience macros for communicating with Cargo during the [`build.rs`] 31 | phrase. Cargo listens to certain [build script outputs] that dictate how 32 | it should behave. 33 | 34 | - An accessible location for seeing what script build outputs are available 35 | to emit. 36 | 37 | - Protection against typos that can be made when printing these formatted 38 | outputs directly yourself. Mistyping macro names will result in a compile 39 | failure. 40 | 41 | [Nikolai Vazquez]: https://twitter.com/NikolaiVazquez 42 | [`build.rs`]: https://doc.rust-lang.org/cargo/reference/build-scripts.html 43 | [build script outputs]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script 44 | 45 | ## Usage 46 | 47 | This crate exposes the following macros: 48 | 49 | | Macro | Output | 50 | |----------------------------------------|---------------------------------------| 51 | | [`pair!($key, $value)`] | `cargo:$key=$value` | 52 | | [`rerun_if_changed!($path)`] | `cargo:rerun-if-changed=$path` | 53 | | [`rerun_if_env_changed!($key)`] | `cargo:rerun-if-env-changed=$key` | 54 | | [`rustc_cdylib_link_arg!($flag)`] | `cargo:rustc-cdylib-link-arg=$flag` | 55 | | [`rustc_cfg!($feature)`] | `cargo:rustc-cfg=$feature` | 56 | | [`rustc_env!($key, $value)`] | `cargo:rustc-env=$key=$value` | 57 | | [`rustc_flags!($flags)`] | `cargo:rustc-flags=$flags` | 58 | | [`rustc_link_arg!($arg)`] | `cargo:rustc-link-arg=$arg` | 59 | | [`rustc_link_arg_bin!($bin => $arg)`] | `cargo:rustc-link-arg-bin=$bin=$arg` | 60 | | [`rustc_link_arg_bins!($arg)`] | `cargo:rustc-link-arg-bins=$arg` | 61 | | [`rustc_link_lib!($name => $kind)`] | `cargo:rustc-link-lib=$kind=$name` | 62 | | [`rustc_link_search!($path => $kind)`] | `cargo:rustc-link-search=$kind=$path` | 63 | | [`warning!($message)`] | `cargo:warning=$message` | 64 | 65 | [`pair!($key, $value)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.pair.html 66 | [`rerun_if_changed!($path)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rerun_if_changed.html 67 | [`rerun_if_env_changed!($key)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rerun_if_env_changed.html 68 | [`rustc_cdylib_link_arg!($flag)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_cdylib_link_arg.html 69 | [`rustc_cfg!($feature)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_cfg.html 70 | [`rustc_env!($key, $value)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_env.html 71 | [`rustc_flags!($flags)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_flags.html 72 | [`rustc_link_arg!($arg)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_link_arg.html 73 | [`rustc_link_arg_bin!($bin => $arg)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_link_arg_bin.html 74 | [`rustc_link_arg_bins!($arg)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_link_arg_bins.html 75 | [`rustc_link_lib!($name => $kind)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_link_lib.html 76 | [`rustc_link_search!($path => $kind)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.rustc_link_search.html 77 | [`warning!($message)`]: https://docs.rs/cargo-emit/latest/cargo_emit/macro.warning.html 78 | 79 | ## License 80 | 81 | This project is released under either: 82 | 83 | - [MIT License](https://github.com/nvzqz/cargo-emit/blob/master/LICENSE-MIT) 84 | 85 | - [Apache License (Version 2.0)](https://github.com/nvzqz/cargo-emit/blob/master/LICENSE-APACHE) 86 | -------------------------------------------------------------------------------- /examples/macros.rs: -------------------------------------------------------------------------------- 1 | //! Example used for testing for clippy warnings on CI. 2 | 3 | fn main() { 4 | cargo_emit::pair!("KEY", "VALUE"); 5 | cargo_emit::rerun_if_changed!("PATH"); 6 | cargo_emit::rerun_if_env_changed!("KEY"); 7 | cargo_emit::rustc_cdylib_link_arg!("FLAG"); 8 | cargo_emit::rustc_cfg!("FEATURE"); 9 | cargo_emit::rustc_env!("KEY", "VALUE"); 10 | cargo_emit::rustc_flags!("FLAGS"); 11 | cargo_emit::rustc_link_arg!("ARGUMENT"); 12 | cargo_emit::rustc_link_arg_bin!("BINARY" => "ARGUMENT"); 13 | cargo_emit::rustc_link_arg_bins!("ARGUMENT"); 14 | cargo_emit::rustc_link_arg_bin!("BINARY" => "ARGUMENT"); 15 | cargo_emit::rustc_link_lib!("NAME" => "KIND"); 16 | cargo_emit::rustc_link_search!("PATH" => "KIND"); 17 | cargo_emit::warning!("MESSAGE"); 18 | } 19 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //!
2 | //! 3 | //! Cargo Emit Logo 7 | //! 8 | //!

9 | //! 10 | //! Downloads 12 | //! 13 | //! 14 | //! Build Status 16 | //! 17 | //! rustc ^1.37.0 19 | //!

20 | //!
21 | //! 22 | //! Talk to Cargo easily at build time, brought to you by [Nikolai Vazquez]. 23 | //! 24 | //! This library provides: 25 | //! 26 | //! - Convenience macros for communicating with Cargo during the [`build.rs`] 27 | //! phrase. Cargo listens to certain [build script outputs] that dictate how 28 | //! it should behave. 29 | //! 30 | //! - An accessible location for seeing what script build outputs are available 31 | //! to emit. 32 | //! 33 | //! - Protection against typos that can be made when printing these formatted 34 | //! outputs directly yourself. Mistyping macro names will result in a compile 35 | //! failure. 36 | //! 37 | //! # Usage 38 | //! 39 | //! This crate is available [on crates.io][crate] and can be used by adding the 40 | //! following to your project's [`Cargo.toml`]: 41 | //! 42 | //! ```toml 43 | //! [build-dependencies] 44 | //! cargo-emit = "0.1" 45 | //! ``` 46 | //! 47 | //! and something like this to your [`build.rs`]: 48 | //! 49 | //! ``` 50 | //! # let should_warn = true; 51 | //! if should_warn { 52 | //! cargo_emit::warning!("(C-3PO voice) We're doomed"); 53 | //! } 54 | //! ``` 55 | //! 56 | //! **Note:** This library is meant to be used with [Rust 2018 edition][2018], 57 | //! so that `cargo_emit::` can be used to prefix macro calls. 58 | //! 59 | //! # Compatibility 60 | //! 61 | //! This crate is compatible with Rust 1.31+ in order to use the 62 | //! `$crate::macro!` feature introduced in [Rust 2018][2018]. 63 | //! 64 | //! # Examples 65 | //! 66 | //! Very thorough examples are provided in the docs for 67 | //! [each individual macro](#macros). 68 | //! 69 | //! # Donate 70 | //! 71 | //! This project is made freely available (as in free beer), but unfortunately 72 | //! not all beer is free! So, if you would like to buy me a beer (or coffee or 73 | //! *more*), then consider supporting my work that's benefited your project 74 | //! and thousands of others. 75 | //! 76 | //! 77 | //! Become a Patron! 78 | //! 79 | //! 80 | //! Buy me a coffee 81 | //! 82 | //! 83 | //! [Nikolai Vazquez]: https://twitter.com/NikolaiVazquez 84 | //! [build script outputs]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script 85 | //! [2018]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#rust-2018 86 | //! [crate]: https://crates.io/crates/cargo-emit 87 | //! [`Cargo.toml`]: https://doc.rust-lang.org/cargo/reference/manifest.html 88 | //! [`build.rs`]: https://doc.rust-lang.org/cargo/reference/build-scripts.html 89 | 90 | #![doc(html_root_url = "https://docs.rs/cargo-emit/0.1.1")] 91 | #![doc( 92 | html_logo_url = "https://raw.githubusercontent.com/nvzqz/cargo-emit/assets/logo.svg?sanitize=true" 93 | )] 94 | #![deny(missing_docs)] 95 | 96 | /// `cargo:$key=$value` 97 | mod pair; 98 | /// `cargo:rerun-if-changed=$path` 99 | mod rerun_if_changed; 100 | /// `cargo:rerun-if-env-changed=$key` 101 | mod rerun_if_env_changed; 102 | /// `cargo:rustc-cdylib-link-arg=$flag` 103 | mod rustc_cdylib_link_arg; 104 | /// `cargo:rustc-cfg=$feature` 105 | mod rustc_cfg; 106 | /// `cargo:rustc-env=$key=$value` 107 | mod rustc_env; 108 | /// `cargo:rustc-flags=$flags` 109 | mod rustc_flags; 110 | /// `cargo:rustc-link-arg=$arg` 111 | mod rustc_link_arg; 112 | /// `cargo:rustc-link-arg-bin=$bin=$arg` 113 | mod rustc_link_arg_bin; 114 | /// `cargo:rustc-link-arg-bins=$arg` 115 | mod rustc_link_arg_bins; 116 | /// `cargo:rustc-link-lib=$kind=$name` 117 | mod rustc_link_lib; 118 | /// `cargo:rustc-link-search=$kind=$path` 119 | mod rustc_link_search; 120 | /// `cargo:warning=$message` 121 | mod warning; 122 | 123 | #[cfg(test)] 124 | fn capture_output(f: F) -> String 125 | where 126 | F: FnOnce(&mut String), 127 | { 128 | let mut output = String::new(); 129 | f(&mut output); 130 | output 131 | } 132 | -------------------------------------------------------------------------------- /src/pair.rs: -------------------------------------------------------------------------------- 1 | /// Emits a `$key`/`$value` pair to Cargo based on [build script outputs]. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:$key=$value"); 7 | /// ``` 8 | /// 9 | /// This is the base macro upon which the other macros are built. 10 | /// 11 | /// # Examples 12 | /// 13 | /// This can be used to emit arbitrary user-defined metadata. 14 | /// 15 | /// ``` 16 | /// cargo_emit::pair!("root", "/path/to/root"); 17 | /// ``` 18 | /// 19 | /// or, in case you want it to emit to a custom stream: 20 | /// 21 | /// ``` 22 | /// let mut stdout = std::io::stdout(); 23 | /// cargo_emit::pair!( 24 | /// to: stdout, 25 | /// "root", "/path/to/root" 26 | /// ); 27 | /// ``` 28 | /// 29 | /// The `$key` and `$value` parameters get concatenated into a single formatting 30 | /// string. Formatting runtime values can be done by passing subsequent values. 31 | /// 32 | /// ``` 33 | /// let name = "foo"; 34 | /// cargo_emit::pair!("{lib}dir", "/path/to/{lib}", lib = name); 35 | /// ``` 36 | /// 37 | /// [build script outputs]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script 38 | #[macro_export] 39 | macro_rules! pair { 40 | (to: $stream:expr, $key:expr, $value:expr $(, $($args:tt)*)?) => {{ 41 | #[allow(unused_imports)] 42 | use std::{fmt::Write as _, io::Write as _}; 43 | 44 | #[allow(clippy::explicit_write)] 45 | writeln!($stream, concat!("cargo:", $key, "=", $value) $(, $($args)*)?).unwrap() 46 | }}; 47 | ($key:expr, $value:expr $(, $($args:tt)*)?) => { 48 | $crate::pair!(to: std::io::stdout(), $key, $value $(, $($args)*)?); 49 | }; 50 | } 51 | 52 | #[cfg(test)] 53 | mod tests { 54 | #[test] 55 | fn single_literal() { 56 | insta::assert_display_snapshot!( 57 | crate::capture_output(|output| { 58 | crate::pair!( 59 | to: output, 60 | "KEY", "VALUE" 61 | ); 62 | }), 63 | @"cargo:KEY=VALUE\n" 64 | ); 65 | } 66 | 67 | #[test] 68 | fn single_with_key_formatted_by_index() { 69 | insta::assert_display_snapshot!( 70 | crate::capture_output(|output| { 71 | crate::pair!( 72 | to: output, 73 | "{}", "VALUE", "KEY" 74 | ); 75 | }), 76 | @"cargo:KEY=VALUE\n" 77 | ); 78 | } 79 | 80 | #[test] 81 | fn single_with_key_formatted_by_name() { 82 | insta::assert_display_snapshot!( 83 | crate::capture_output(|output| { 84 | crate::pair!( 85 | to: output, 86 | "{key}", "VALUE", key = "KEY" 87 | ); 88 | }), 89 | @"cargo:KEY=VALUE\n" 90 | ); 91 | } 92 | 93 | #[test] 94 | fn single_with_value_formatted_by_index() { 95 | insta::assert_display_snapshot!( 96 | crate::capture_output(|output| { 97 | crate::pair!( 98 | to: output, 99 | "KEY", "{}", "VALUE" 100 | ); 101 | }), 102 | @"cargo:KEY=VALUE\n" 103 | ); 104 | } 105 | 106 | #[test] 107 | fn single_with_value_formatted_by_name() { 108 | insta::assert_display_snapshot!( 109 | crate::capture_output(|output| { 110 | crate::pair!( 111 | to: output, 112 | "KEY", "{value}", value = "VALUE" 113 | ); 114 | }), 115 | @"cargo:KEY=VALUE\n" 116 | ); 117 | } 118 | 119 | #[test] 120 | fn single_with_key_and_value_formatted_by_index() { 121 | insta::assert_display_snapshot!( 122 | crate::capture_output(|output| { 123 | crate::pair!( 124 | to: output, 125 | "{}", "{}", "KEY", "VALUE" 126 | ); 127 | }), 128 | @"cargo:KEY=VALUE\n" 129 | ); 130 | } 131 | 132 | #[test] 133 | fn single_with_key_and_value_formatted_by_name() { 134 | insta::assert_display_snapshot!( 135 | crate::capture_output(|output| { 136 | crate::pair!( 137 | to: output, 138 | "{key}", "{value}", key = "KEY", value = "VALUE" 139 | ); 140 | }), 141 | @"cargo:KEY=VALUE\n" 142 | ); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/rerun_if_changed.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to run again if the file or directory at `$path` changes. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rerun-if-changed=$path"); 7 | /// ``` 8 | /// 9 | /// `$path` is a path to a file or directory which indicates that the build 10 | /// script should be re-run if it changes (detected by a more-recent 11 | /// last-modified timestamp on the file). Normally build scripts are re-run if 12 | /// any file inside the crate root changes, but this can be used to scope 13 | /// changes to just a small set of files. (If this path points to a directory 14 | /// the entire directory will not be traversed for changes -- only changes to 15 | /// the timestamp of the directory itself (which corresponds to some types of 16 | /// changes within the directory, depending on platform) will trigger a rebuild. 17 | /// To request a re-run on any changes within an entire directory, print a line 18 | /// for the directory and another line for everything inside it, recursively.) 19 | /// Note that if the build script itself (or one of its dependencies) changes, 20 | /// then it's rebuilt and rerun unconditionally, so 21 | /// `rerun_if_changed!("build.rs")` is almost always redundant (unless you want 22 | /// to ignore changes in all other files except for `build.rs`). 23 | /// 24 | /// # Examples 25 | /// 26 | /// This is useful for tracking build-dependent files that Cargo does not 27 | /// already know. 28 | /// 29 | /// ``` 30 | /// cargo_emit::rerun_if_changed!( 31 | /// "/path/to/resource1", 32 | /// "/path/to/resource2", 33 | /// ); 34 | /// ``` 35 | /// 36 | /// or, in case you want it to emit to a custom stream: 37 | /// 38 | /// ``` 39 | /// let mut stdout = std::io::stdout(); 40 | /// // .. 41 | /// cargo_emit::rerun_if_changed!( 42 | /// to: stdout, 43 | /// "/path/to/resource1", 44 | /// "/path/to/resource2", 45 | /// ); 46 | /// ``` 47 | #[macro_export] 48 | macro_rules! rerun_if_changed { 49 | (to: $stream:expr, $($path:expr),+ $(,)?) => { 50 | $($crate::pair!(to: $stream, "rerun-if-changed", "{}", $path);)+ 51 | }; 52 | ($($path:expr),+ $(,)?) => { 53 | $crate::rerun_if_changed!(to: std::io::stdout(), $($path),+); 54 | }; 55 | } 56 | 57 | #[cfg(test)] 58 | mod tests { 59 | use std::path::PathBuf; 60 | 61 | #[test] 62 | fn single_literal() { 63 | insta::assert_display_snapshot!( 64 | crate::capture_output(|output| { 65 | crate::rerun_if_changed!( 66 | to: output, 67 | "/path/to/resource" 68 | ); 69 | }), 70 | @"cargo:rerun-if-changed=/path/to/resource\n" 71 | ); 72 | } 73 | 74 | #[test] 75 | fn single_expression() { 76 | insta::assert_display_snapshot!( 77 | crate::capture_output(|output| { 78 | crate::rerun_if_changed!( 79 | to: output, 80 | PathBuf::from("/path/to/resource").display() 81 | ); 82 | }), 83 | @"cargo:rerun-if-changed=/path/to/resource\n" 84 | ); 85 | } 86 | 87 | #[test] 88 | fn multiple_literals() { 89 | insta::assert_display_snapshot!( 90 | crate::capture_output(|output| { 91 | crate::rerun_if_changed!( 92 | to: output, 93 | "/path/to/resource1", 94 | "/path/to/resource2", 95 | "/path/to/resource3", 96 | "/path/to/resource4", 97 | ); 98 | }), 99 | @"cargo:rerun-if-changed=/path/to/resource1\n\ 100 | cargo:rerun-if-changed=/path/to/resource2\n\ 101 | cargo:rerun-if-changed=/path/to/resource3\n\ 102 | cargo:rerun-if-changed=/path/to/resource4\n" 103 | ); 104 | } 105 | 106 | #[test] 107 | fn multiple_expressions() { 108 | insta::assert_display_snapshot!( 109 | crate::capture_output(|output| { 110 | crate::rerun_if_changed!( 111 | to: output, 112 | PathBuf::from("/path/to/resource1").display(), 113 | PathBuf::from("/path/to/resource2").display(), 114 | PathBuf::from("/path/to/resource3").display(), 115 | PathBuf::from("/path/to/resource4").display(), 116 | ); 117 | }), 118 | @"cargo:rerun-if-changed=/path/to/resource1\n\ 119 | cargo:rerun-if-changed=/path/to/resource2\n\ 120 | cargo:rerun-if-changed=/path/to/resource3\n\ 121 | cargo:rerun-if-changed=/path/to/resource4\n" 122 | ); 123 | } 124 | 125 | #[test] 126 | fn multiple_mixed() { 127 | insta::assert_display_snapshot!( 128 | crate::capture_output(|output| { 129 | crate::rerun_if_changed!( 130 | to: output, 131 | "/path/to/resource1", 132 | PathBuf::from("/path/to/resource2").display(), 133 | "/path/to/resource3", 134 | PathBuf::from("/path/to/resource4").display(), 135 | ); 136 | }), 137 | @"cargo:rerun-if-changed=/path/to/resource1\n\ 138 | cargo:rerun-if-changed=/path/to/resource2\n\ 139 | cargo:rerun-if-changed=/path/to/resource3\n\ 140 | cargo:rerun-if-changed=/path/to/resource4\n" 141 | ); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/rerun_if_env_changed.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to run again if the file or directory at `$key` changes. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rerun-if-env-changed=$key"); 7 | /// ``` 8 | /// 9 | /// `$key` is the name of an environment variable which indicates that if the 10 | /// environment variable's value changes the build script should be rerun. This 11 | /// basically behaves the same as [`rerun_if_changed!`] except that it works 12 | /// with environment variables instead. Note that the environment variables here 13 | /// are intended for global environment variables like `CC` and such, it's not 14 | /// necessary to use this for env vars like `TARGET` that Cargo sets. Also note 15 | /// that if [`rerun_if_env_changed!`] is used then Cargo will only rerun the 16 | /// build script if those environment variables change or if files printed out 17 | /// by [`rerun_if_changed!`] change. 18 | /// 19 | /// # Examples 20 | /// 21 | /// This is useful for tracking build-dependent files that Cargo does not 22 | /// already know. 23 | /// 24 | /// ``` 25 | /// cargo_emit::rerun_if_env_changed!("MY_DEPENDENCY", "PATH"); 26 | /// ``` 27 | /// 28 | /// or, in case you want it to emit to a custom stream: 29 | /// 30 | /// ``` 31 | /// let mut stdout = std::io::stdout(); 32 | /// cargo_emit::rerun_if_env_changed!( 33 | /// to: stdout, 34 | /// "MY_DEPENDENCY", "PATH" 35 | /// ); 36 | /// ``` 37 | /// 38 | /// [`rerun_if_changed!`]: macro.rerun_if_changed.html 39 | /// [`rerun_if_env_changed!`]: macro.rerun_if_env_changed.html 40 | #[macro_export] 41 | macro_rules! rerun_if_env_changed { 42 | (to: $stream:expr, $($key:expr),+ $(,)?) => { 43 | $($crate::pair!(to: $stream, "rerun-if-env-changed", "{}", $key);)+ 44 | }; 45 | ($($key:expr),+ $(,)?) => { 46 | $crate::rerun_if_env_changed!(to: std::io::stdout(), "{}", $($key),+); 47 | }; 48 | } 49 | 50 | #[cfg(test)] 51 | mod tests { 52 | #[test] 53 | fn single() { 54 | insta::assert_display_snapshot!( 55 | crate::capture_output(|output| { 56 | crate::rerun_if_env_changed!( 57 | to: output, 58 | "KEY" 59 | ); 60 | }), 61 | @"cargo:rerun-if-env-changed=KEY\n" 62 | ); 63 | } 64 | 65 | #[test] 66 | fn multiple() { 67 | insta::assert_display_snapshot!( 68 | crate::capture_output(|output| { 69 | crate::rerun_if_env_changed!( 70 | to: output, 71 | "KEY1", 72 | "KEY2", 73 | "KEY3", 74 | ); 75 | }), 76 | @"cargo:rerun-if-env-changed=KEY1\n\ 77 | cargo:rerun-if-env-changed=KEY2\n\ 78 | cargo:rerun-if-env-changed=KEY3\n" 79 | ); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/rustc_cdylib_link_arg.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass `-C link-arg=$flag` to the compiler. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rustc-cdylib-link-arg=$flag"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Runtime arguments can be passed directly. 12 | /// 13 | /// ``` 14 | /// let flag1 = // ... 15 | /// # ""; 16 | /// let flag2 = // ... 17 | /// # ""; 18 | /// cargo_emit::rustc_cdylib_link_arg!(flag1, flag2); 19 | /// ``` 20 | /// 21 | /// or, in case you want it to emit to a custom stream: 22 | /// 23 | /// ``` 24 | /// let flag1 = // ... 25 | /// # ""; 26 | /// let flag2 = // ... 27 | /// # ""; 28 | /// let mut stdout = std::io::stdout(); 29 | /// cargo_emit::rustc_cdylib_link_arg!( 30 | /// to: stdout, 31 | /// flag1, flag2 32 | /// ); 33 | /// ``` 34 | #[macro_export] 35 | macro_rules! rustc_cdylib_link_arg { 36 | (to: $stream:expr, $($flag:expr),+ $(,)?) => { 37 | $($crate::pair!(to: $stream, "rustc-cdylib-link-arg", "{}", $flag);)+ 38 | }; 39 | ($($flag:expr),+ $(,)?) => { 40 | $crate::rustc_cdylib_link_arg!(to: std::io::stdout(), $($flag),+); 41 | }; 42 | } 43 | 44 | #[cfg(test)] 45 | mod tests { 46 | #[test] 47 | fn single() { 48 | insta::assert_display_snapshot!( 49 | crate::capture_output(|output| { 50 | crate::rustc_cdylib_link_arg!( 51 | to: output, 52 | "ARG" 53 | ); 54 | }), 55 | @"cargo:rustc-cdylib-link-arg=ARG\n" 56 | ); 57 | } 58 | 59 | #[test] 60 | fn multiple() { 61 | insta::assert_display_snapshot!( 62 | crate::capture_output(|output| { 63 | crate::rustc_cdylib_link_arg!( 64 | to: output, 65 | "ARG1", 66 | "ARG2", 67 | ); 68 | }), 69 | @"cargo:rustc-cdylib-link-arg=ARG1\n\ 70 | cargo:rustc-cdylib-link-arg=ARG2\n" 71 | ); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/rustc_cfg.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to enable a `$feature`. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rustc-cfg=$feature"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Useful for conditionally enabling certain code to run. 12 | /// 13 | /// ``` 14 | /// # struct Cargo; 15 | /// # impl Cargo { 16 | /// # fn can_bench(&self) -> bool { true } 17 | /// # } 18 | /// # let cargo = Cargo; 19 | /// if cargo.can_bench() { 20 | /// cargo_emit::rustc_cfg!("bench"); 21 | /// } 22 | /// ``` 23 | /// 24 | /// or, in case you want it to emit to a custom stream: 25 | /// 26 | /// ``` 27 | /// # struct Cargo; 28 | /// # impl Cargo { 29 | /// # fn can_bench(&self) -> bool { true } 30 | /// # } 31 | /// # let cargo = Cargo; 32 | /// let mut stdout = std::io::stdout(); 33 | /// if cargo.can_bench() { 34 | /// cargo_emit::rustc_cfg!( 35 | /// to: stdout, 36 | /// "bench" 37 | /// ); 38 | /// } 39 | /// ``` 40 | /// 41 | /// Then outside of `build.rs`: 42 | /// 43 | /// ``` 44 | /// #[cfg(bench)] 45 | /// mod benches { 46 | /// // ... 47 | /// } 48 | /// ``` 49 | #[macro_export] 50 | macro_rules! rustc_cfg { 51 | (to: $stream:expr, $feature:expr $(, $($args:tt)*)?) => { 52 | $crate::pair!(to: $stream, "rustc-cfg", $feature $(, $($args)+)?); 53 | }; 54 | ($feature:expr $(, $($args:tt)*)?) => { 55 | $crate::rustc_cfg!(to: std::io::stdout(), $feature $(, $($args)+)?); 56 | }; 57 | } 58 | 59 | #[cfg(test)] 60 | mod tests { 61 | #[test] 62 | fn literal() { 63 | insta::assert_display_snapshot!( 64 | crate::capture_output(|output| { 65 | crate::rustc_cfg!( 66 | to: output, 67 | "CFG" 68 | ); 69 | }), 70 | @"cargo:rustc-cfg=CFG\n" 71 | ); 72 | } 73 | 74 | #[test] 75 | fn formatted() { 76 | insta::assert_display_snapshot!( 77 | crate::capture_output(|output| { 78 | crate::rustc_cfg!( 79 | to: output, 80 | "{}", "CFG" 81 | ); 82 | }), 83 | @"cargo:rustc-cfg=CFG\n" 84 | ); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/rustc_env.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to assign `$key` for the environment variable for `$key`. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rustc-env=$key=$value"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Useful for injecting environment variables during the build. 12 | /// 13 | /// The `$key` and `$value` parameters get concatenated into a single formatting 14 | /// string. Formatting runtime values can be done by passing subsequent values. 15 | /// 16 | /// ``` 17 | /// let git_rev_hash = // 18 | /// # "0000111122223333444455556666777788889999"; 19 | /// cargo_emit::rustc_env!("MY_HASH", "{}", git_rev_hash); 20 | /// ``` 21 | /// 22 | /// or, in case you want it to emit to a custom stream: 23 | /// 24 | /// ``` 25 | /// let mut stdout = std::io::stdout(); 26 | /// let git_rev_hash = // ... 27 | /// # "0000111122223333444455556666777788889999"; 28 | /// cargo_emit::rustc_env!( 29 | /// to: stdout, 30 | /// "MY_HASH", "{}", git_rev_hash 31 | /// ); 32 | /// ``` 33 | #[macro_export] 34 | macro_rules! rustc_env { 35 | (to: $stream:expr, $key:expr, $value:expr $(, $($args:tt)*)?) => { 36 | $crate::pair!(to: $stream, "rustc-env", concat!($key, "=", $value) $(, $($args)+)?); 37 | }; 38 | ($key:expr, $value:expr $(, $($args:tt)*)?) => { 39 | $crate::rustc_env!(to: std::io::stdout(), $key, $value $(, $($args)+)?); 40 | }; 41 | } 42 | 43 | #[cfg(test)] 44 | mod tests { 45 | #[test] 46 | fn single_literal() { 47 | insta::assert_display_snapshot!( 48 | crate::capture_output(|output| { 49 | crate::rustc_env!( 50 | to: output, 51 | "KEY", "VALUE" 52 | ); 53 | }), 54 | @"cargo:rustc-env=KEY=VALUE\n" 55 | ); 56 | } 57 | 58 | #[test] 59 | fn single_with_key_formatted_by_index() { 60 | insta::assert_display_snapshot!( 61 | crate::capture_output(|output| { 62 | crate::rustc_env!( 63 | to: output, 64 | "{}", "VALUE", "KEY" 65 | ); 66 | }), 67 | @"cargo:rustc-env=KEY=VALUE\n" 68 | ); 69 | } 70 | 71 | #[test] 72 | fn single_with_key_formatted_by_name() { 73 | insta::assert_display_snapshot!( 74 | crate::capture_output(|output| { 75 | crate::rustc_env!( 76 | to: output, 77 | "{key}", "VALUE", key = "KEY" 78 | ); 79 | }), 80 | @"cargo:rustc-env=KEY=VALUE\n" 81 | ); 82 | } 83 | 84 | #[test] 85 | fn single_with_value_formatted_by_index() { 86 | insta::assert_display_snapshot!( 87 | crate::capture_output(|output| { 88 | crate::rustc_env!( 89 | to: output, 90 | "KEY", "{}", "VALUE" 91 | ); 92 | }), 93 | @"cargo:rustc-env=KEY=VALUE\n" 94 | ); 95 | } 96 | 97 | #[test] 98 | fn single_with_value_formatted_by_name() { 99 | insta::assert_display_snapshot!( 100 | crate::capture_output(|output| { 101 | crate::rustc_env!( 102 | to: output, 103 | "KEY", "{value}", value = "VALUE" 104 | ); 105 | }), 106 | @"cargo:rustc-env=KEY=VALUE\n" 107 | ); 108 | } 109 | 110 | #[test] 111 | fn single_with_key_and_value_formatted_by_index() { 112 | insta::assert_display_snapshot!( 113 | crate::capture_output(|output| { 114 | crate::rustc_env!( 115 | to: output, 116 | "{}", "{}", "KEY", "VALUE" 117 | ); 118 | }), 119 | @"cargo:rustc-env=KEY=VALUE\n" 120 | ); 121 | } 122 | 123 | #[test] 124 | fn single_with_key_and_value_formatted_by_name() { 125 | insta::assert_display_snapshot!( 126 | crate::capture_output(|output| { 127 | crate::rustc_env!( 128 | to: output, 129 | "{key}", "{value}", key = "KEY", value = "VALUE" 130 | ); 131 | }), 132 | @"cargo:rustc-env=KEY=VALUE\n" 133 | ); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/rustc_flags.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass `$flags` to the compiler. 2 | /// 3 | /// As of this writing, only `-l` and `-L` flags are supported. 4 | /// 5 | /// This is equivalent to: 6 | /// 7 | /// ``` 8 | /// println!("cargo:rustc-flags=$flags"); 9 | /// ``` 10 | /// 11 | /// # Examples 12 | /// 13 | /// The `$flags` get concatenated into a single formatting 14 | /// string. Formatting runtime values can be done by passing subsequent values. 15 | /// 16 | /// ``` 17 | /// cargo_emit::rustc_flags!("-l pthread"); 18 | /// ``` 19 | /// 20 | /// or, in case you want it to emit to a custom stream: 21 | /// 22 | /// ``` 23 | /// let mut stdout = std::io::stdout(); 24 | /// cargo_emit::rustc_flags!( 25 | /// to: stdout, 26 | /// "-l pthread" 27 | /// ); 28 | /// ``` 29 | #[macro_export] 30 | macro_rules! rustc_flags { 31 | (to: $stream:expr, $($flags:expr),+ $(,)?) => { 32 | $($crate::pair!(to: $stream, "rustc-flags", "{}", $flags);)+ 33 | }; 34 | ($($flags:expr),+ $(,)?) => { 35 | $crate::rustc_flags!(to: std::io::stdout(), $($flags),+); 36 | }; 37 | } 38 | 39 | #[cfg(test)] 40 | mod tests { 41 | #[test] 42 | fn single() { 43 | insta::assert_display_snapshot!( 44 | crate::capture_output(|output| { 45 | crate::rustc_flags!( 46 | to: output, 47 | "FLAG" 48 | ); 49 | }), 50 | @"cargo:rustc-flags=FLAG\n" 51 | ); 52 | } 53 | 54 | #[test] 55 | fn multiple() { 56 | insta::assert_display_snapshot!( 57 | crate::capture_output(|output| { 58 | crate::rustc_flags!( 59 | to: output, 60 | "FLAG1", 61 | "FLAG2", 62 | ); 63 | }), 64 | @"cargo:rustc-flags=FLAG1\n\ 65 | cargo:rustc-flags=FLAG2\n" 66 | ); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/rustc_link_arg.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass the -C link-arg=FLAG option to the compiler, 2 | /// but only when building supported targets (benchmarks, binaries, cdylib crates, examples, and tests). 3 | /// Its usage is highly platform specific. 4 | /// It is useful to set the shared library version or linker script. 5 | /// 6 | /// This is equivalent to: 7 | /// 8 | /// ``` 9 | /// println!("cargo:rustc-link-arg=$flag"); 10 | /// ``` 11 | /// 12 | /// # Examples 13 | /// 14 | /// ``` 15 | /// let flag1 = // ... 16 | /// # ""; 17 | /// let flag2 = // ... 18 | /// # ""; 19 | /// cargo_emit::rustc_link_arg!( 20 | /// flag1, flag2 21 | /// ); 22 | /// ``` 23 | /// 24 | /// or, in case you want it to emit to a custom stream: 25 | /// 26 | /// ``` 27 | /// let flag1 = // ... 28 | /// # ""; 29 | /// let flag2 = // ... 30 | /// # ""; 31 | /// let mut stdout = std::io::stdout(); 32 | /// cargo_emit::rustc_link_arg!( 33 | /// to: stdout, 34 | /// flag1, flag2 35 | /// ); 36 | /// ``` 37 | #[macro_export] 38 | macro_rules! rustc_link_arg { 39 | (to: $stream:expr, $($flag:expr),+ $(,)?) => { 40 | $($crate::pair!(to: $stream, "rustc-link-arg", "{}", $flag);)+ 41 | }; 42 | ($($flag:expr),+ $(,)?) => { 43 | $crate::rustc_link_arg!(to: std::io::stdout(), $($flag),+); 44 | }; 45 | } 46 | 47 | #[cfg(test)] 48 | mod tests { 49 | #[test] 50 | fn single() { 51 | insta::assert_display_snapshot!( 52 | crate::capture_output(|output| { 53 | crate::rustc_link_arg!( 54 | to: output, 55 | "ARG" 56 | ); 57 | }), 58 | @"cargo:rustc-link-arg=ARG 59 | " 60 | ); 61 | } 62 | 63 | #[test] 64 | fn multiple() { 65 | insta::assert_display_snapshot!( 66 | crate::capture_output(|output| { 67 | crate::rustc_link_arg!( 68 | to: output, 69 | "ARG1", 70 | "ARG2" 71 | ); 72 | }), 73 | @r###" 74 | cargo:rustc-link-arg=ARG1 75 | cargo:rustc-link-arg=ARG2 76 | "### 77 | ); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/rustc_link_arg_bin.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass the `-C link-arg=$flag` option to the compiler, 2 | /// but only when building the binary target with name `$bin`. 3 | /// Its usage is highly platform specific. 4 | /// It is useful to set a linker script or other linker options. 5 | /// 6 | /// This is equivalent to: 7 | /// 8 | /// ``` 9 | /// println!("cargo:rustc-link-arg-bin=$bin=$flag"); 10 | /// ``` 11 | /// 12 | /// # Examples 13 | /// 14 | /// ``` 15 | /// cargo_emit::rustc_link_arg_bin!( 16 | /// "hello_world" => "-Wall" 17 | /// ); 18 | /// ``` 19 | /// 20 | /// or, in case you want it to emit to a custom stream: 21 | /// 22 | /// ``` 23 | /// let mut stdout = std::io::stdout(); 24 | /// cargo_emit::rustc_link_arg_bin!( 25 | /// to: stdout, 26 | /// "hello_world" => "-Wall" 27 | /// ); 28 | /// ``` 29 | #[macro_export] 30 | macro_rules! rustc_link_arg_bin { 31 | (to: $stream:expr, $bin:expr => $flags:expr $(,)?) => { 32 | $crate::pair!(to: $stream, "rustc-link-arg-bin", "{}={}", $bin, $flags); 33 | }; 34 | (to: $stream:expr, $($bin:expr=> $flags:expr),+ $(,)?) => { { 35 | $($crate::rustc_link_arg_bin!(to: $stream, $bin => $flags);)+ 36 | } }; 37 | ($bin:expr => $flags:expr $(,)?) => { 38 | $crate::rustc_link_arg_bin!(to: std::io::stdout(), $bin => $flags); 39 | }; 40 | ($($bin:expr=> $flags:expr),+ $(,)?) => { { 41 | $crate::rustc_link_arg_bin!(to: std::io::stdout(), $($bin=> $flags),+); 42 | } }; 43 | } 44 | 45 | #[cfg(test)] 46 | mod tests { 47 | #[test] 48 | fn single() { 49 | insta::assert_display_snapshot!( 50 | crate::capture_output(|output| { 51 | crate::rustc_link_arg_bin!( 52 | to: output, 53 | "BIN" => "FLAGS" 54 | ); 55 | }), 56 | @"cargo:rustc-link-arg-bin=BIN=FLAGS 57 | " 58 | ); 59 | } 60 | 61 | #[test] 62 | fn multiple() { 63 | insta::assert_display_snapshot!( 64 | crate::capture_output(|output| { 65 | crate::rustc_link_arg_bin!( 66 | to: output, 67 | "BIN1" => "FLAGS1", 68 | "BIN2" => "FLAGS2", 69 | ); 70 | }), 71 | @r###" 72 | cargo:rustc-link-arg-bin=BIN1=FLAGS1 73 | cargo:rustc-link-arg-bin=BIN2=FLAGS2 74 | "### 75 | ); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/rustc_link_arg_bins.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass the `-C link-arg=$flag` option to the compiler, 2 | /// but only when building a binary target. 3 | /// Its usage is highly platform specific. 4 | /// It is useful to set a linker script or other linker options. 5 | /// 6 | /// This is equivalent to: 7 | /// 8 | /// ``` 9 | /// println!("cargo:rustc-link-arg-bins=$flag"); 10 | /// ``` 11 | /// 12 | /// # Examples 13 | /// 14 | /// ``` 15 | /// let flag1 = // ... 16 | /// # ""; 17 | /// let flag2 = // ... 18 | /// # ""; 19 | /// cargo_emit::rustc_link_arg_bins!( 20 | /// flag1, flag2 21 | /// ); 22 | /// ``` 23 | /// 24 | /// or, in case you want it to emit to a custom stream: 25 | /// 26 | /// ``` 27 | /// let flag1 = // ... 28 | /// # ""; 29 | /// let flag2 = // ... 30 | /// # ""; 31 | /// let mut stdout = std::io::stdout(); 32 | /// cargo_emit::rustc_link_arg_bins!( 33 | /// to: stdout, 34 | /// flag1, flag2 35 | /// ); 36 | /// ``` 37 | #[macro_export] 38 | macro_rules! rustc_link_arg_bins { 39 | (to: $stream:expr, $($flag:expr),+ $(,)?) => { 40 | $($crate::pair!(to: $stream, "rustc-link-arg-bins", "{}", $flag);)+ 41 | }; 42 | ($($flag:expr),+ $(,)?) => { 43 | $crate::rustc_link_arg_bins!(to: std::io::stdout(), $($flag),+); 44 | }; 45 | } 46 | 47 | #[cfg(test)] 48 | mod tests { 49 | #[test] 50 | fn single() { 51 | insta::assert_display_snapshot!( 52 | crate::capture_output(|output| { 53 | crate::rustc_link_arg_bins!( 54 | to: output, 55 | "ARG" 56 | ); 57 | }), 58 | @"cargo:rustc-link-arg-bins=ARG 59 | " 60 | ); 61 | } 62 | 63 | #[test] 64 | fn multiple() { 65 | insta::assert_display_snapshot!( 66 | crate::capture_output(|output| { 67 | crate::rustc_link_arg_bins!( 68 | to: output, 69 | "ARG1", 70 | "ARG2" 71 | ); 72 | }), 73 | @r###" 74 | cargo:rustc-link-arg-bins=ARG1 75 | cargo:rustc-link-arg-bins=ARG2 76 | "### 77 | ); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/rustc_link_lib.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass `$lib` to the compiler as a `-l` flag. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rustc-link-lib=[$kind=]$name"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Useful for telling the linker what libraries should be linked. 12 | /// 13 | /// ``` 14 | /// cargo_emit::rustc_link_lib!( 15 | /// "ssl", // same as `=> "dylib"` 16 | /// "ruby" => "static", 17 | /// "CoreFoundation" => "framework", 18 | /// ); 19 | /// ``` 20 | /// 21 | /// or, in case you want it to emit to a custom stream: 22 | /// 23 | /// ``` 24 | /// let mut stdout = std::io::stdout(); 25 | /// cargo_emit::rustc_link_lib!( 26 | /// to: stdout, 27 | /// "ssl", // same as `=> "dylib"` 28 | /// "ruby" => "static", 29 | /// "CoreFoundation" => "framework", 30 | /// ); 31 | /// ``` 32 | #[macro_export] 33 | macro_rules! rustc_link_lib { 34 | (to: $stream:expr, $name:expr $(,)?) => { 35 | $crate::pair!(to: $stream, "rustc-link-lib", "{}", $name); 36 | }; 37 | (to: $stream:expr, $name:expr => $kind:expr $(,)?) => { 38 | $crate::pair!(to: $stream, "rustc-link-lib", "{}={}", $kind, $name); 39 | }; 40 | (to: $stream:expr, $($name:expr $(=> $kind:expr)?),+ $(,)?) => { { 41 | $($crate::rustc_link_lib!(to: $stream, $name $(=> $kind)?);)+ 42 | } }; 43 | ($name:expr $(,)?) => { 44 | $crate::rustc_link_lib!(to: std::io::stdout(), $name); 45 | }; 46 | ($name:expr => $kind:expr $(,)?) => { 47 | $crate::rustc_link_lib!(to: std::io::stdout(), $name => $kind); 48 | }; 49 | ($($name:expr $(=> $kind:expr)?),+ $(,)?) => { { 50 | $crate::rustc_link_lib!(to: std::io::stdout(), $($name $(=> $kind)?),+); 51 | } }; 52 | } 53 | 54 | #[cfg(test)] 55 | mod tests { 56 | #[test] 57 | fn single_name_literal() { 58 | insta::assert_display_snapshot!( 59 | crate::capture_output(|output| { 60 | crate::rustc_link_lib!( 61 | to: output, 62 | "NAME" 63 | ); 64 | }), 65 | @"cargo:rustc-link-lib=NAME\n" 66 | ); 67 | } 68 | 69 | #[test] 70 | fn single_name_expression() { 71 | insta::assert_display_snapshot!( 72 | crate::capture_output(|output| { 73 | let name = "NAME"; 74 | crate::rustc_link_lib!( 75 | to: output, 76 | name 77 | ); 78 | }), 79 | @"cargo:rustc-link-lib=NAME\n" 80 | ); 81 | } 82 | 83 | #[test] 84 | fn single_name_literal_with_kind() { 85 | insta::assert_display_snapshot!( 86 | crate::capture_output(|output| { 87 | crate::rustc_link_lib!( 88 | to: output, 89 | "NAME" => "KIND" 90 | ); 91 | }), 92 | @"cargo:rustc-link-lib=KIND=NAME\n" 93 | ); 94 | } 95 | 96 | #[test] 97 | fn single_name_expression_with_kind() { 98 | insta::assert_display_snapshot!( 99 | crate::capture_output(|output| { 100 | let name = "NAME"; 101 | let kind = "KIND"; 102 | crate::rustc_link_lib!( 103 | to: output, 104 | name => kind 105 | ); 106 | }), 107 | @"cargo:rustc-link-lib=KIND=NAME\n" 108 | ); 109 | } 110 | 111 | #[test] 112 | fn multiple_name_expression_with_kind() { 113 | insta::assert_display_snapshot!( 114 | crate::capture_output(|output| { 115 | let name2 = "NAME2"; 116 | let kind2 = "KIND2"; 117 | crate::rustc_link_lib!( 118 | to: output, 119 | "NAME1" => "KIND1", 120 | name2 => kind2, 121 | ); 122 | }), 123 | @"cargo:rustc-link-lib=KIND1=NAME1\n\ 124 | cargo:rustc-link-lib=KIND2=NAME2\n" 125 | ); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/rustc_link_search.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to pass `$path` to the compiler as a `-L` flag. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:rustc-link-search=[$kind=]$path"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Useful for telling the linker where a path can be found. 12 | /// 13 | /// ``` 14 | /// cargo_emit::rustc_link_search!( 15 | /// "path/to/ssl/lib/", // same as `=> "all"` 16 | /// "path/to/ruby/lib/" => "native", 17 | /// ); 18 | /// ``` 19 | /// 20 | /// or, in case you want it to emit to a custom stream: 21 | /// 22 | /// ``` 23 | /// let mut stdout = std::io::stdout(); 24 | /// cargo_emit::rustc_link_search!( 25 | /// to: stdout, 26 | /// "path/to/ssl/lib/", // same as `=> "all"` 27 | /// "path/to/ruby/lib/" => "native", 28 | /// ); 29 | /// ``` 30 | #[macro_export] 31 | macro_rules! rustc_link_search { 32 | (to: $stream:expr, $path:expr $(,)?) => { 33 | $crate::pair!(to: $stream, "rustc-link-search", "{}", $path); 34 | }; 35 | (to: $stream:expr, $path:expr => $kind:expr $(,)?) => { 36 | $crate::pair!(to: $stream, "rustc-link-search", "{}={}", $kind, $path); 37 | }; 38 | (to: $stream:expr, $($path:expr $(=> $kind:expr)?),+ $(,)?) => { { 39 | $($crate::rustc_link_search!(to: $stream, $path $(=> $kind)?);)+ 40 | } }; 41 | ($path:expr $(,)?) => { 42 | $crate::rustc_link_search!(to: std::io::stdout(), $path); 43 | }; 44 | ($path:expr => $kind:expr $(,)?) => { 45 | $crate::rustc_link_search!(to: std::io::stdout(), $path => $kind); 46 | }; 47 | ($($path:expr $(=> $kind:expr)?),+ $(,)?) => { { 48 | $crate::rustc_link_search!(to: std::io::stdout(), $($path $(=> $kind)?),+); 49 | } }; 50 | } 51 | 52 | #[cfg(test)] 53 | mod tests { 54 | #[test] 55 | fn single_name_literal() { 56 | insta::assert_display_snapshot!( 57 | crate::capture_output(|output| { 58 | crate::rustc_link_search!( 59 | to: output, 60 | "PATH" 61 | ); 62 | }), 63 | @"cargo:rustc-link-search=PATH\n" 64 | ); 65 | } 66 | 67 | #[test] 68 | fn single_name_expression() { 69 | insta::assert_display_snapshot!( 70 | crate::capture_output(|output| { 71 | let path = "PATH"; 72 | crate::rustc_link_search!( 73 | to: output, 74 | path 75 | ); 76 | }), 77 | @"cargo:rustc-link-search=PATH\n" 78 | ); 79 | } 80 | 81 | #[test] 82 | fn single_name_literal_with_kind() { 83 | insta::assert_display_snapshot!( 84 | crate::capture_output(|output| { 85 | crate::rustc_link_search!( 86 | to: output, 87 | "PATH" => "KIND" 88 | ); 89 | }), 90 | @"cargo:rustc-link-search=KIND=PATH\n" 91 | ); 92 | } 93 | 94 | #[test] 95 | fn single_name_expression_with_kind() { 96 | insta::assert_display_snapshot!( 97 | crate::capture_output(|output| { 98 | let path = "PATH"; 99 | let kind = "KIND"; 100 | crate::rustc_link_search!( 101 | to: output, 102 | path => kind 103 | ); 104 | }), 105 | @"cargo:rustc-link-search=KIND=PATH\n" 106 | ); 107 | } 108 | 109 | #[test] 110 | fn multiple_name_expression_with_kind() { 111 | insta::assert_display_snapshot!( 112 | crate::capture_output(|output| { 113 | let path2 = "PATH2"; 114 | let kind2 = "KIND2"; 115 | crate::rustc_link_search!( 116 | to: output, 117 | "PATH1" => "KIND1", 118 | path2 => kind2, 119 | ); 120 | }), 121 | @"cargo:rustc-link-search=KIND1=PATH1\n\ 122 | cargo:rustc-link-search=KIND2=PATH2\n" 123 | ); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/warning.rs: -------------------------------------------------------------------------------- 1 | /// Tells Cargo to print the formatted `warning` message. 2 | /// 3 | /// This is equivalent to: 4 | /// 5 | /// ``` 6 | /// println!("cargo:warning=$args"); 7 | /// ``` 8 | /// 9 | /// # Examples 10 | /// 11 | /// Useful for showing when something expected (but not critical) has failed. 12 | /// 13 | /// ``` 14 | /// match std::env::current_dir() { 15 | /// Ok(dir) => { /* ... */ } 16 | /// Err(error) => cargo_emit::warning!( 17 | /// "Something suspicious is happening: {}", 18 | /// error, 19 | /// ), 20 | /// } 21 | /// ``` 22 | /// 23 | /// or, in case you want it to emit to a custom stream: 24 | /// 25 | /// ``` 26 | /// let mut stdout = std::io::stdout(); 27 | /// match std::env::current_dir() { 28 | /// Ok(dir) => { /* ... */ } 29 | /// Err(error) => cargo_emit::warning!( 30 | /// to: stdout, 31 | /// "Something suspicious is happening: {}", 32 | /// error, 33 | /// ), 34 | /// } 35 | /// ``` 36 | /// 37 | /// Assuming you're building `my-crate`, you will see: 38 | /// 39 | /// ```sh 40 | /// $ cargo build 41 | /// Compiling my-crate v0.1.0 (/path/to/my-crate) 42 | /// warning: Something suspicious is happening: ... 43 | /// ``` 44 | #[macro_export] 45 | macro_rules! warning { 46 | (to: $stream:expr, $($args:tt)+) => { 47 | $crate::pair!(to: $stream, "warning", $($args)+) 48 | }; 49 | ($($args:tt)+) => { 50 | $crate::warning!(to: std::io::stdout(), $($args)+) 51 | }; 52 | } 53 | 54 | #[cfg(test)] 55 | mod tests { 56 | #[test] 57 | fn single_literal() { 58 | insta::assert_display_snapshot!( 59 | crate::capture_output(|output| { 60 | crate::warning!( 61 | to: output, 62 | "WARNING" 63 | ); 64 | }), 65 | @"cargo:warning=WARNING\n" 66 | ); 67 | } 68 | 69 | #[test] 70 | fn single_formatted_by_index() { 71 | // Formatted argument: 72 | insta::assert_display_snapshot!( 73 | crate::capture_output(|output| { 74 | crate::warning!( 75 | to: output, 76 | "{}", "WARNING" 77 | ); 78 | }), 79 | @"cargo:warning=WARNING\n" 80 | ); 81 | } 82 | 83 | #[test] 84 | fn single_formatted_by_key() { 85 | // Formatted argument: 86 | insta::assert_display_snapshot!( 87 | crate::capture_output(|output| { 88 | crate::warning!( 89 | to: output, 90 | "{warning}", warning = "WARNING" 91 | ); 92 | }), 93 | @"cargo:warning=WARNING\n" 94 | ); 95 | } 96 | } 97 | --------------------------------------------------------------------------------