boytacean 0.9.18__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. boytacean-0.9.18/Cargo.toml +51 -0
  2. boytacean-0.9.18/LICENSE +201 -0
  3. boytacean-0.9.18/MANIFEST.in +2 -0
  4. boytacean-0.9.18/PKG-INFO +22 -0
  5. boytacean-0.9.18/README.md +131 -0
  6. boytacean-0.9.18/build.rs +215 -0
  7. boytacean-0.9.18/setup.cfg +7 -0
  8. boytacean-0.9.18/setup.py +65 -0
  9. boytacean-0.9.18/src/apu.rs +1149 -0
  10. boytacean-0.9.18/src/cheats/genie.rs +283 -0
  11. boytacean-0.9.18/src/cheats/mod.rs +2 -0
  12. boytacean-0.9.18/src/cheats/shark.rs +219 -0
  13. boytacean-0.9.18/src/consts.rs +13 -0
  14. boytacean-0.9.18/src/cpu.rs +753 -0
  15. boytacean-0.9.18/src/data.rs +175 -0
  16. boytacean-0.9.18/src/devices/buffer.rs +62 -0
  17. boytacean-0.9.18/src/devices/mod.rs +3 -0
  18. boytacean-0.9.18/src/devices/printer.rs +329 -0
  19. boytacean-0.9.18/src/devices/stdout.rs +63 -0
  20. boytacean-0.9.18/src/dma.rs +238 -0
  21. boytacean-0.9.18/src/error.rs +26 -0
  22. boytacean-0.9.18/src/gb.rs +1359 -0
  23. boytacean-0.9.18/src/gen/_build.rs +18 -0
  24. boytacean-0.9.18/src/gen/build.rs +18 -0
  25. boytacean-0.9.18/src/gen/mock.rs +21 -0
  26. boytacean-0.9.18/src/gen/mod.rs +24 -0
  27. boytacean-0.9.18/src/info.rs +49 -0
  28. boytacean-0.9.18/src/inst.rs +3368 -0
  29. boytacean-0.9.18/src/lib.rs +27 -0
  30. boytacean-0.9.18/src/macros.rs +49 -0
  31. boytacean-0.9.18/src/mmu.rs +663 -0
  32. boytacean-0.9.18/src/pad.rs +184 -0
  33. boytacean-0.9.18/src/ppu.rs +2145 -0
  34. boytacean-0.9.18/src/py.rs +207 -0
  35. boytacean-0.9.18/src/python/boytacean/__init__.py +19 -0
  36. boytacean-0.9.18/src/python/boytacean/boytacean.pyi +51 -0
  37. boytacean-0.9.18/src/python/boytacean/gb.py +303 -0
  38. boytacean-0.9.18/src/python/boytacean/graphics.py +98 -0
  39. boytacean-0.9.18/src/python/boytacean/palettes.py +17 -0
  40. boytacean-0.9.18/src/python/boytacean/pyboy.py +299 -0
  41. boytacean-0.9.18/src/python/boytacean/video.py +133 -0
  42. boytacean-0.9.18/src/python/boytacean.egg-info/PKG-INFO +22 -0
  43. boytacean-0.9.18/src/python/boytacean.egg-info/SOURCES.txt +51 -0
  44. boytacean-0.9.18/src/python/boytacean.egg-info/dependency_links.txt +1 -0
  45. boytacean-0.9.18/src/python/boytacean.egg-info/not-zip-safe +1 -0
  46. boytacean-0.9.18/src/python/boytacean.egg-info/top_level.txt +1 -0
  47. boytacean-0.9.18/src/rom.rs +1115 -0
  48. boytacean-0.9.18/src/serial.rs +255 -0
  49. boytacean-0.9.18/src/state.rs +1648 -0
  50. boytacean-0.9.18/src/test.rs +125 -0
  51. boytacean-0.9.18/src/timer.rs +165 -0
  52. boytacean-0.9.18/src/util.rs +186 -0
@@ -0,0 +1,51 @@
1
+ [package]
2
+ name = "boytacean"
3
+ description = "A Game Boy emulator that is written in Rust."
4
+ version = "0.9.18"
5
+ authors = ["João Magalhães <joamag@gmail.com>"]
6
+ license = "Apache-2.0"
7
+ repository = "https://github.com/joamag/boytacean"
8
+ keywords = ["gameboy", "emulator", "rust"]
9
+ edition = "2018"
10
+ exclude = ["/frontends", "/res/roms", "/res/screens", "/res/videos"]
11
+ build = "build.rs"
12
+ readme = "README.md"
13
+
14
+ [lib]
15
+ crate-type = ["cdylib", "rlib"]
16
+
17
+ [features]
18
+ wasm = ["wasm-bindgen", "js-sys"]
19
+ python = ["pyo3"]
20
+ debug = []
21
+ pedantic = []
22
+ cpulog = []
23
+ gen-mock = []
24
+
25
+ [dependencies]
26
+ wasm-bindgen = { version = "0.2", optional = true }
27
+ js-sys = { version = "0.3", optional = true }
28
+ pyo3 = { version = "0.20", optional = true }
29
+
30
+ [build-dependencies]
31
+ chrono = "0.4"
32
+ regex = "1"
33
+ built = "0.5"
34
+
35
+ [profile.release]
36
+ debug = false
37
+ lto = true
38
+ opt-level = 3
39
+ codegen-units = 1
40
+
41
+ [profile.release-safe]
42
+ inherits = "release"
43
+ debug = false
44
+ lto = true
45
+ opt-level = 3
46
+
47
+ [workspace]
48
+ members = [".", "frontends/libretro", "frontends/sdl"]
49
+
50
+ [package.metadata.docs.rs]
51
+ features = ["wasm", "gen-mock"]
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2022 João Magalhães
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ include build.rs Cargo.toml LICENSE
2
+ recursive-include src *.rs
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.1
2
+ Name: boytacean
3
+ Version: 0.9.18
4
+ Summary: A Game Boy emulator that is written in Rust
5
+ Home-page: https://boytacean.joao.me
6
+ Author: João Magalhães
7
+ Author-email: joamag@gmail.com
8
+ License: Apache License, Version 2.0
9
+ Keywords: gameboy emulator rust
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Topic :: Utilities
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python
15
+ Classifier: Programming Language :: Python :: 3.5
16
+ Classifier: Programming Language :: Python :: 3.6
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ License-File: LICENSE
@@ -0,0 +1,131 @@
1
+ # [Boytacean](https://boytacean.joao.me)
2
+
3
+ A Game Boy emulator that is written in Rust 🦀.
4
+
5
+ <a href="https://boytacean.joao.me" target="_blank"><img src="https://github.com/joamag/boytacean/raw/master/res/videos/002-mario.low.gif" width="360" /></a>
6
+
7
+ **This emulator has been written for educational purposes and shouldn't be taken too seriously.** But yeahh it plays most Game Boy games, which is cool... 🕹️
8
+
9
+ ## Features
10
+
11
+ * Game Boy (DMG) and Game Boy Color (CGB) emulation
12
+ * Simple navigable source-code
13
+ * Web, [SDL](https://www.libsdl.org) and [Libretro](https://www.libretro.com) front-ends
14
+ * Audio, with a pretty accurate APU
15
+ * Serial Data Transfer ([Link Cable](https://en.wikipedia.org/wiki/Game_Link_Cable)) support
16
+ * Game Boy Printer emulation
17
+ * Support for multiple MBCs: MBC1, MBC3, and MBC5
18
+ * Save state support using the [BESS Specification](https://github.com/LIJI32/SameBoy/blob/master/BESS.md) for cross-compatibility with other emulators
19
+ * Cheat support using [Game Genie](https://en.wikipedia.org/wiki/Game_Genie) and [GameShark](https://pt.wikipedia.org/wiki/GameShark) codes
20
+ * Variable CPU clock speed
21
+ * Accurate PPU - passes [dmg-acid2](https://github.com/mattcurrie/dmg-acid2) and [cgb-acid2](https://github.com/mattcurrie/cgb-acid2) tests
22
+
23
+ For the Web front-end...
24
+
25
+ * Mobile first experience
26
+ * Transparent RAM saving using [Web Storage API](https://developer.mozilla.org/docs/Web/API/Window/localStorage)
27
+ * GamePad support using [Web Gamepad API](https://developer.mozilla.org/docs/Web/API/Gamepad_API)
28
+ * Unobstructive and effective on-screen GamePad
29
+ * Rumble emulation and haptic feedback
30
+ * Cool bespoke display palettes built by [TheWolfBunny64](https://www.deviantart.com/thewolfbunny).
31
+ * Fullscreen browser mode
32
+ * Debug mode - VRAM and registers
33
+
34
+ What's still missing...
35
+
36
+ * Low-level cycle accurate system emulation
37
+ * [Game Boy Camera](https://en.wikipedia.org/wiki/Game_Boy_Camera) emulation
38
+ * NetPlay gaming support
39
+
40
+ ## Deployments
41
+
42
+ | Provider | Stable | URL |
43
+ | ---------- | ------- | ---------------------------------------------------------------- |
44
+ | Cloudflare | `True` | [boytacean.joao.me](https://boytacean.joao.me) |
45
+ | Cloudflare | `True` | [boytacean.pages.dev](https://boytacean.pages.dev) |
46
+ | Cloudflare | `True` | [prod.boytacean.pages.dev](https://prod.boytacean.pages.dev) |
47
+ | Cloudflare | `True` | [stable.boytacean.pages.dev](https://stable.boytacean.pages.dev) |
48
+ | Cloudflare | `False` | [master.boytacean.pages.dev](https://master.boytacean.pages.dev) |
49
+
50
+ ## Build
51
+
52
+ ### Library
53
+
54
+ ```bash
55
+ cargo build
56
+ ```
57
+
58
+ ### Python Library
59
+
60
+ ```bash
61
+ pip install .
62
+ ```
63
+
64
+ or
65
+
66
+ ```bash
67
+ python setup.py install
68
+ ```
69
+
70
+ ### WASM for Node.js
71
+
72
+ ```bash
73
+ cargo install wasm-pack
74
+ wasm-pack build --release --target=nodejs -- --features wasm
75
+ ```
76
+
77
+ ### WASM for Web
78
+
79
+ ```bash
80
+ cargo install wasm-pack
81
+ wasm-pack build --release --target=web --out-dir=frontends/web/lib -- --features wasm
82
+ cd frontends/web
83
+ npm install && npm run build
84
+ cd dist && python3 -m http.server
85
+ ```
86
+
87
+ ## Web front-end
88
+
89
+ The Web front-end makes use of [EmuKit](https://github.com/joamag/emukit) which is a UI toolkit that provides the required infrastructure for the creation
90
+ of interfaces for emulation in a Web context.
91
+
92
+ ### Configuration
93
+
94
+ You can use some GET parameters to control the initial behavior of the emulator.
95
+
96
+ | Parameter | Type | Description |
97
+ | ------------ | ------- | ---------------------------------------------------------------------------------------------- |
98
+ | `rom_url` | String | The URL from which the initial ROM is going to be loaded, should support CORS. |
99
+ | `url` | String | The same as `rom_url`. |
100
+ | `fullscreen` | Boolean | If the emulator should start in fullscreen mode. |
101
+ | `fs` | Boolean | The same as `fullscreen`. |
102
+ | `debug` | Boolean | If the "debugger" should start visible. |
103
+ | `keyboard` | Boolean | If the on-screen keyboard should start visible. |
104
+ | `palette` | String | The name of the palette to be set at startup( eg: `christmas`, `hogwards`, `mariobros`, etc.). |
105
+
106
+ ## Palettes
107
+
108
+ The palettes offered in the web version were provided by [TheWolfBunny64](https://www.deviantart.com/thewolfbunny).
109
+
110
+ ## Inspiration
111
+
112
+ To get some information about the resources that inspired me through the emulation creation journey check [Inspiration](doc/inspiration.md).
113
+
114
+ ## Contributing
115
+
116
+ Love Gameboy emulation? Want to contribute to Boytacean? That's awesome! Your contributions are what make the open-source community such an amazing place to learn, inspire, and create.
117
+
118
+ To get started, please take a look at our [Contributing Guidelines](CONTRIBUTING.md). Whether you're fixing bugs, adding new features, or helping with documentation, every contribution is valuable and greatly appreciated!
119
+
120
+ Don't hesitate to reach out if you have any questions. Join us in making Boytacean the best Gameboy emulator out there!
121
+
122
+ ## License
123
+
124
+ Boytacean is currently licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/).
125
+
126
+ ## Build Automation
127
+
128
+ [![Build Status](https://github.com/joamag/boytacean/workflows/Main%20Workflow/badge.svg)](https://github.com/joamag/boytacean/actions)
129
+ [![crates Status](https://img.shields.io/crates/v/boytacean)](https://crates.io/crates/boytacean)
130
+ [![npm Status](https://img.shields.io/npm/v/boytacean.svg)](https://www.npmjs.com/package/boytacean)
131
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://www.apache.org/licenses/)
@@ -0,0 +1,215 @@
1
+ #![allow(clippy::uninlined_format_args)]
2
+
3
+ //! Build script (https://doc.rust-lang.org/cargo/reference/build-scripts.html)
4
+ //! This script is executed as the first step in the compilation process.
5
+ //! Here we export metadata constants to a `constants/generated.rs` file which is then
6
+ //! imported and used by the remaining crate.
7
+ //!
8
+ //! # Examples
9
+ //!
10
+ //! In C you can use the preprocessor macro `__DATE__` to save the compilation date like:
11
+ //!
12
+ //! ```c
13
+ //! #define COMPILATION_DATE __DATE__
14
+ //! ```
15
+ //!
16
+ //! Rust does not have such preprocessor macros, so we use this script and do:
17
+ //!
18
+ //! ```rust
19
+ //! let now_utc = chrono::Utc::now();
20
+ //! write_str_constant(
21
+ //! &mut file,
22
+ //! "COMPILATION_DATE",
23
+ //! &format!("{}", now_utc.format("%b %d %Y")),
24
+ //! );
25
+ //! ```
26
+
27
+ use built::{write_built_file_with_opts, Options};
28
+ use chrono::Utc;
29
+ use regex::Regex;
30
+ use std::fs::{File, OpenOptions};
31
+ use std::io::Write;
32
+ use std::path::Path;
33
+ use std::process::Command;
34
+ use std::{env, str};
35
+
36
+ const BUILD_OUT_FILE: &str = "build.rs";
37
+ const GEN_DIR: &str = "./src/gen";
38
+
39
+ fn main() {
40
+ // in case we're running under docs.rs then we must return the control
41
+ // flow immediately as it's not possible to generate files under the
42
+ // expected read only file system present in `docs.rs` build environment
43
+ if std::env::var("DOCS_RS").is_ok() {
44
+ return;
45
+ }
46
+
47
+ // opens the target destination file panicking with a proper message in
48
+ // case it was not possible to open it (eg: directory inexistent)
49
+ let dest_path = Path::new(GEN_DIR).join(Path::new(BUILD_OUT_FILE));
50
+ let mut file = OpenOptions::new()
51
+ .truncate(true)
52
+ .write(true)
53
+ .create(true)
54
+ .open(dest_path)
55
+ .unwrap_or_else(|_| panic!("Can't open '{}'", BUILD_OUT_FILE));
56
+
57
+ writeln!(file, "//! Global constants, such as compiler version used, features, platform information and others.\n").unwrap();
58
+ writeln!(file, "// @generated\n").unwrap();
59
+
60
+ let now_utc = Utc::now();
61
+ write_str_constant(
62
+ &mut file,
63
+ "COMPILATION_DATE",
64
+ &format!("{}", now_utc.format("%b %d %Y")),
65
+ );
66
+ write_str_constant(
67
+ &mut file,
68
+ "COMPILATION_TIME",
69
+ &format!("{}", now_utc.format("%H:%M:%S")),
70
+ );
71
+
72
+ write_str_constant(
73
+ &mut file,
74
+ "NAME",
75
+ option_env!("CARGO_PKG_NAME").unwrap_or("UNKNOWN"),
76
+ );
77
+
78
+ write_str_constant(
79
+ &mut file,
80
+ "VERSION",
81
+ option_env!("CARGO_PKG_VERSION").unwrap_or("UNKNOWN"),
82
+ );
83
+
84
+ write_str_constant(&mut file, "COMPILER", "rustc");
85
+
86
+ let compiler_version = Command::new("rustc")
87
+ .arg("--version")
88
+ .output()
89
+ .ok()
90
+ .and_then(|output| String::from_utf8(output.stdout).ok())
91
+ .unwrap_or_else(|| "UNKNOWN".to_string());
92
+ let re = Regex::new("rustc ([\\d.\\d.\\d]*)").unwrap();
93
+ let compiler_version = re
94
+ .captures(&compiler_version)
95
+ .unwrap()
96
+ .get(1)
97
+ .unwrap()
98
+ .as_str();
99
+ write_str_constant(&mut file, "COMPILER_VERSION", compiler_version);
100
+
101
+ write_str_constant(
102
+ &mut file,
103
+ "HOST",
104
+ &env::var("HOST").unwrap_or_else(|_| String::from("UNKNOWN")),
105
+ );
106
+
107
+ write_str_constant(
108
+ &mut file,
109
+ "TARGET",
110
+ &env::var("TARGET").unwrap_or_else(|_| String::from("UNKNOWN")),
111
+ );
112
+
113
+ write_str_constant(
114
+ &mut file,
115
+ "PROFILE",
116
+ &env::var("PROFILE").unwrap_or_else(|_| String::from("UNKNOWN")),
117
+ );
118
+
119
+ write_str_constant(
120
+ &mut file,
121
+ "OPT_LEVEL",
122
+ &env::var("OPT_LEVEL").unwrap_or_else(|_| String::from("UNKNOWN")),
123
+ );
124
+
125
+ write_str_constant(
126
+ &mut file,
127
+ "MAKEFLAGS",
128
+ option_env!("CARGO_MAKEFLAGS").unwrap_or("UNKNOWN"),
129
+ );
130
+
131
+ let mut features = vec!["cpu"];
132
+ if cfg!(feature = "wasm") {
133
+ features.push("wasm")
134
+ }
135
+ if cfg!(feature = "debug") {
136
+ features.push("debug")
137
+ }
138
+ if cfg!(feature = "pedantic") {
139
+ features.push("pedantic")
140
+ }
141
+ if cfg!(feature = "cpulog") {
142
+ features.push("cpulog")
143
+ }
144
+
145
+ write_vec_constant(&mut file, "FEATURES_SEQ", features);
146
+
147
+ write_str_constant(
148
+ &mut file,
149
+ "PLATFORM_CPU_BITS",
150
+ &(std::mem::size_of::<usize>() * 8).to_string(),
151
+ );
152
+
153
+ write_constant(
154
+ &mut file,
155
+ "PLATFORM_CPU_BITS_INT",
156
+ std::mem::size_of::<usize>() * 8,
157
+ );
158
+
159
+ let mut options = Options::default();
160
+ options.set_cfg(false);
161
+ options.set_ci(false);
162
+ options.set_compiler(false);
163
+ options.set_env(false);
164
+ options.set_dependencies(true);
165
+ options.set_features(true);
166
+
167
+ let manifest_path = env::var("CARGO_MANIFEST_DIR").unwrap();
168
+ let built_path = Path::new(GEN_DIR).join(Path::new("_build.rs"));
169
+
170
+ write_built_file_with_opts(&options, manifest_path.as_ref(), &built_path).unwrap();
171
+ }
172
+
173
+ fn write_constant<T>(file: &mut File, key: &str, val: T)
174
+ where
175
+ T: std::fmt::Display,
176
+ {
177
+ writeln!(
178
+ file,
179
+ "pub const {}: {} = {};",
180
+ key,
181
+ std::any::type_name::<T>(),
182
+ val
183
+ )
184
+ .unwrap_or_else(|_| panic!("Failed to write '{}' to 'build_constants.rs'", key));
185
+ }
186
+
187
+ fn write_str_constant(file: &mut File, key: &str, val: &str) {
188
+ writeln!(file, "pub const {}: &str = \"{}\";", key, val)
189
+ .unwrap_or_else(|_| panic!("Failed to write '{}' to 'build_constants.rs'", key));
190
+ }
191
+
192
+ fn write_vec_constant<T>(file: &mut File, key: &str, vec: Vec<T>)
193
+ where
194
+ T: std::fmt::Display,
195
+ {
196
+ let mut list_str = String::new();
197
+ let mut is_first = true;
198
+ for value in &vec {
199
+ if is_first {
200
+ is_first = false;
201
+ } else {
202
+ list_str.push_str(", ");
203
+ }
204
+ list_str.push_str(format!("\"{}\"", value).as_str());
205
+ }
206
+ writeln!(
207
+ file,
208
+ "pub const {}: [{}; {}] = [{}];",
209
+ key,
210
+ std::any::type_name::<T>(),
211
+ vec.len(),
212
+ list_str
213
+ )
214
+ .unwrap_or_else(|_| panic!("Failed to write '{}' to 'build_constants.rs'", key));
215
+ }
@@ -0,0 +1,7 @@
1
+ [bdist_wheel]
2
+ universal = 1
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+