| 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 std::env; |
| 19 | use std::fs::File; |
| 20 | use std::io::Write; |
| 21 | use std::path::*; |
| 22 | |
| 23 | fn main() { |
| 24 | // Create and open a file in the output directory to contain our generated rust code |
| 25 | let dest = env::var("OUT_DIR").unwrap(); |
| 26 | let mut file = File::create(&Path::new(&dest).join("webgl_exts.rs")).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 = env::current_dir().unwrap().join("api_webgl/extensions"); |
| 33 | |
| 34 | // Generate a slice literal, looking like this: |
| 35 | // `&[&*include_bytes!(..), &*include_bytes!(..), ..]` |
| 36 | writeln!(file, "&[").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_dir().unwrap() { |
| 42 | let entry = entry.unwrap(); |
| 43 | let path = entry.path(); |
| 44 | let ext_name = path.file_name().unwrap().to_str().unwrap(); |
| 45 | |
| 46 | // Each item which is a directory, and is not named `template`, may be |
| 47 | // an extension. |
| 48 | if path.is_dir() && ext_name != "template" { |
| 49 | // If the directory contains a file named `extension.xml`, then this |
| 50 | // really is an extension. |
| 51 | let ext_path = path.join("extension.xml"); |
| 52 | if ext_path.is_file() { |
| 53 | // Include the XML file, making sure to use an absolute path. |
| 54 | writeln!(file, "&*include_bytes!({:?}),", ext_path.to_str().unwrap()).unwrap(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Close the slice |
| 60 | writeln!(file, "]").unwrap(); |
| 61 | } |