at 23.05-pre 70 lines 2.8 kB view raw
1/** 2 * Since Nix does not have a standard location like /usr/share, 3 * where GSettings system could look for schemas, we need to point the software to a correct location somehow. 4 * For executables, we handle this using wrappers but this is not an option for libraries like e-d-s. 5 * Instead, we hardcode the schema path when creating the settings. 6 * A schema path (ie org.gnome.evolution) can be replaced by @EVOLUTION_SCHEMA_PATH@ 7 * which is then replaced at build time by substituteAll. 8 * The mapping is provided in a json file ./glib-schema-to-var.json 9 */ 10 11@initialize:python@ 12@@ 13import json 14 15cpp_constants = {} 16 17def register_cpp_constant(const_name, val): 18 cpp_constants[const_name] = val.strip() 19 20def resolve_cpp_constant(const_name): 21 return cpp_constants.get(const_name, const_name) 22 23with open("./glib-schema-to-var.json") as mapping_file: 24 schema_to_var = json.load(mapping_file); 25 26def get_schema_directory(schema_path): 27 # Sometimes the schema id is referenced using C preprocessor #define constant in the same file 28 # let’s try to resolve it first. 29 schema_path = resolve_cpp_constant(schema_path.strip()).strip('"') 30 if schema_path in schema_to_var: 31 return f'"@{schema_to_var[schema_path]}@"' 32 raise Exception(f"Unknown schema path {schema_path!r}, please add it to ./glib-schema-to-var.json") 33 34 35@find_cpp_constants@ 36identifier const_name; 37expression val; 38@@ 39 40#define const_name val 41 42@script:python record_cpp_constants depends on find_cpp_constants@ 43const_name << find_cpp_constants.const_name; 44val << find_cpp_constants.val; 45@@ 46 47register_cpp_constant(const_name, val) 48 49 50@depends on ever record_cpp_constants || never record_cpp_constants@ 51// We want to run after #define constants have been collected but even if there are no #defines. 52expression SCHEMA_PATH; 53expression settings; 54// Coccinelle does not like autocleanup macros in + sections, 55// let’s use fresh id with concatenation to produce the code as a string. 56fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source"; 57fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema"; 58fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_PATH) { get_schema_directory(SCHEMA_PATH) }; 59@@ 60-settings = g_settings_new(SCHEMA_PATH); 61+{ 62+ schema_source_decl; 63+ schema_decl; 64+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY, 65+ g_settings_schema_source_get_default(), 66+ TRUE, 67+ NULL); 68+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_PATH, FALSE); 69+ settings = g_settings_new_full(schema, NULL, NULL); 70+}