| 1 | // Copyright 2015 Brendan Zabarauskas and the gl-rs developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // This build script is used to discover all of the accepted extensions |
| 16 | // described in the Khronos WebGL repository, and construct a static slice |
| 17 | // containing the XML specifications for all of them. |
| 18 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 19 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 20 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 21 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::*; |
| 22 | |
| 23 | fn mainrust-analyzer cargo khronos_api 3.1.0 main().() { |
| 24 | // Create and open a file in the output directory to contain our generated rust code |
| 25 | let dest = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("OUT_DIR").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 26 | let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&dest).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("webgl_exts.rs")).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 27 | |
| 28 | // Find the absolute path to the folder containing the WebGL extensions. |
| 29 | // The absolute path is needed, because we don't know where the output |
| 30 | // directory will be, and `include_bytes!(..)` resolves paths relative to the |
| 31 | // containing file. |
| 32 | let root = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::current_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/current_dir().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("api_webgl/extensions"); |
| 33 | |
| 34 | // Generate a slice literal, looking like this: |
| 35 | // `&[&*include_bytes!(..), &*include_bytes!(..), ..]` |
| 36 | writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(file, "&[").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 37 | |
| 38 | // The slice will have one entry for each WebGL extension. To find the |
| 39 | // extensions we mirror the behaviour of the `api_webgl/extensions/find-exts` |
| 40 | // shell script. |
| 41 | for entry in root.read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]read_dir().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() { |
| 42 | let entry = entry.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 43 | let path = entry.pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().(); |
| 44 | let ext_name = path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 45 | |
| 46 | // Each item which is a directory, and is not named `template`, may be |
| 47 | // an extension. |
| 48 | if path.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_dir().() && ext_name rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."template" { |
| 49 | // If the directory contains a file named `extension.xml`, then this |
| 50 | // really is an extension. |
| 51 | let ext_path = path.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("extension.xml"); |
| 52 | if ext_path.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() { |
| 53 | // Include the XML file, making sure to use an absolute path. |
| 54 | writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(file, "&*include_bytes!({:?}),", ext_path.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Close the slice |
| 60 | writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(file, "]").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 61 | } |