summaryrefslogtreecommitdiff
path: root/codegen/mod.rs
blob: 42dc9dcf0e2c2e6b5077627087268e0dced758df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::error::Error;
use std::fs::File;
use std::io::Write;
use std::path::Path;

use opengl_registry::api_interface_definition::FeatureKind;
use opengl_registry::Registry;
use quote::quote;

use crate::codegen::command::functions::create_command_functions;
use crate::codegen::command::globals::create_command_globals;
use crate::codegen::load_with::create_load_with_function;

pub mod bindings;

mod command;
mod formatting;
mod load_with;

pub fn generate_using_registry(dest_file: &Path) -> Result<(), Box<dyn Error>>
{
    let registry = Registry::retrieve()?;

    let removed_commands = registry
        .api_interface_definitions()
        .iter()
        .flat_map(|api_interface_def| {
            api_interface_def.removals().iter().flat_map(|removal| {
                removal.features().iter().filter_map(|feature| {
                    if feature.kind() != FeatureKind::Command {
                        return None;
                    }

                    Some(feature.name())
                })
            })
        })
        .collect::<Vec<_>>();

    let filtered_commands = registry
        .commands()
        .iter()
        .filter(|command| !removed_commands.contains(&command.prototype().name()))
        .cloned()
        .collect::<Vec<_>>();

    let command_globals = create_command_globals(&filtered_commands);
    let command_functions = create_command_functions(&filtered_commands)?;
    let load_with_function = create_load_with_function(&filtered_commands);

    let tokens = quote! {
        #(#command_functions)*

        #load_with_function

        mod functions
        {
            #(#command_globals)*

            extern "C" fn function_not_loaded()
            {
                panic!("OpenGL function not loaded");
            }
        }
    };

    let mut file = File::create(dest_file)?;

    file.write_all(tokens.to_string().as_bytes())?;

    Ok(())
}