Merge pull request #206291 from jtojnar/mk-gsettings-patch

makeHardcodeGsettingsPatch: Support other constructors
This commit is contained in:
Jan Tojnar 2022-12-20 22:13:25 +01:00 committed by GitHub
commit 3c5fffed82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 416 additions and 123 deletions

1
.github/CODEOWNERS vendored
View file

@ -270,6 +270,7 @@ pkgs/development/python-modules/buildcatrust/ @ajs124 @lukegb @mweinelt
# GNOME
/pkgs/desktops/gnome @jtojnar
/pkgs/desktops/gnome/extensions @piegamesde @jtojnar
/pkgs/build-support/make-hardcode-gsettings-patch @jtojnar
# Cinnamon
/pkgs/desktops/cinnamon @mkg20001

View file

@ -17,6 +17,7 @@
, substituteAll
, _experimental-update-script-combinators
, glib
, makeHardcodeGsettingsPatch
}:
stdenv.mkDerivation rec {
@ -64,9 +65,9 @@ stdenv.mkDerivation rec {
];
passthru = {
hardcodeGsettingsPatch = glib.mkHardcodeGsettingsPatch {
hardcodeGsettingsPatch = makeHardcodeGsettingsPatch {
inherit src;
glib-schema-to-var = {
schemaIdToVariableMapping = {
"org.gnome.evolution.mail" = "evo";
"org.gnome.evolution.calendar" = "evo";
};

View file

@ -0,0 +1,60 @@
{
runCommand,
git,
coccinelle,
python3,
}:
/*
Can be used as part of an update script to automatically create a patch
hardcoding the path of all GSettings schemas in C code.
For example:
passthru = {
hardcodeGsettingsPatch = makeHardcodeGsettingsPatch {
inherit src;
schemaIdToVariableMapping = {
...
};
};
updateScript =
let
updateSource = ...;
updatePatch = _experimental-update-script-combinators.copyAttrOutputToFile "evolution-ews.hardcodeGsettingsPatch" ./hardcode-gsettings.patch;
in
_experimental-update-script-combinators.sequence [
updateSource
updatePatch
];
};
}
takes as input a mapping from schema path to variable name.
For example `{ "org.gnome.evolution" = "EVOLUTION_SCHEMA_PATH"; }`
hardcodes looking for `org.gnome.evolution` into `@EVOLUTION_SCHEMA_PATH@`.
All schemas must be listed.
*/
{
src,
schemaIdToVariableMapping,
}:
runCommand
"hardcode-gsettings.patch"
{
inherit src;
nativeBuildInputs = [
git
coccinelle
python3 # For patch script
];
}
''
unpackPhase
cd "''${sourceRoot:-.}"
set -x
cp ${builtins.toFile "glib-schema-to-var.json" (builtins.toJSON schemaIdToVariableMapping)} ./glib-schema-to-var.json
git init
git add -A
spatch --sp-file "${./hardcode-gsettings.cocci}" --dir . --in-place
git diff > "$out"
''

View file

@ -0,0 +1,142 @@
/**
* Since Nix does not have a standard location like /usr/share,
* where GSettings system could look for schemas, we need to point the software to a correct location somehow.
* For executables, we handle this using wrappers but this is not an option for libraries like e-d-s.
* Instead, we hardcode the schema path when creating the settings.
* A schema path (ie org.gnome.evolution) can be replaced by @EVOLUTION_SCHEMA_ID@
* which is then replaced at build time by substituteAll.
* The mapping is provided in a json file ./glib-schema-to-var.json
*/
@initialize:python@
@@
import json
cpp_constants = {}
def register_cpp_constant(const_name, val):
cpp_constants[const_name] = val.strip()
def resolve_cpp_constant(const_name):
return cpp_constants.get(const_name, const_name)
with open("./glib-schema-to-var.json") as mapping_file:
schema_to_var = json.load(mapping_file);
def get_schema_directory(schema_id):
# Sometimes the schema id is referenced using C preprocessor #define constant in the same file
# lets try to resolve it first.
schema_id = resolve_cpp_constant(schema_id.strip()).strip('"')
if schema_id in schema_to_var:
return f'"@{schema_to_var[schema_id]}@"'
raise Exception(f"Unknown schema path {schema_id!r}, please add it to ./glib-schema-to-var.json")
@find_cpp_constants@
identifier const_name;
expression val;
@@
#define const_name val
@script:python record_cpp_constants depends on find_cpp_constants@
const_name << find_cpp_constants.const_name;
val << find_cpp_constants.val;
@@
register_cpp_constant(const_name, val)
@depends on ever record_cpp_constants || never record_cpp_constants@
// We want to run after #define constants have been collected but even if there are no #defines.
expression SCHEMA_ID;
expression settings;
// Coccinelle does not like autocleanup macros in + sections,
// lets use fresh id with concatenation to produce the code as a string.
fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source";
fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema";
fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_ID) { get_schema_directory(SCHEMA_ID) };
@@
-settings = g_settings_new(SCHEMA_ID);
+{
+ schema_source_decl;
+ schema_decl;
+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY,
+ g_settings_schema_source_get_default(),
+ TRUE,
+ NULL);
+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_ID, FALSE);
+ settings = g_settings_new_full(schema, NULL, NULL);
+}
@depends on ever record_cpp_constants || never record_cpp_constants@
// We want to run after #define constants have been collected but even if there are no #defines.
expression SCHEMA_ID;
expression settings;
expression BACKEND;
// Coccinelle does not like autocleanup macros in + sections,
// lets use fresh id with concatenation to produce the code as a string.
fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source";
fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema";
fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_ID) { get_schema_directory(SCHEMA_ID) };
@@
-settings = g_settings_new_with_backend(SCHEMA_ID, BACKEND);
+{
+ schema_source_decl;
+ schema_decl;
+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY,
+ g_settings_schema_source_get_default(),
+ TRUE,
+ NULL);
+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_ID, FALSE);
+ settings = g_settings_new_full(schema, BACKEND, NULL);
+}
@depends on ever record_cpp_constants || never record_cpp_constants@
// We want to run after #define constants have been collected but even if there are no #defines.
expression SCHEMA_ID;
expression settings;
expression BACKEND;
expression PATH;
// Coccinelle does not like autocleanup macros in + sections,
// lets use fresh id with concatenation to produce the code as a string.
fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source";
fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema";
fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_ID) { get_schema_directory(SCHEMA_ID) };
@@
-settings = g_settings_new_with_backend_and_path(SCHEMA_ID, BACKEND, PATH);
+{
+ schema_source_decl;
+ schema_decl;
+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY,
+ g_settings_schema_source_get_default(),
+ TRUE,
+ NULL);
+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_ID, FALSE);
+ settings = g_settings_new_full(schema, BACKEND, PATH);
+}
@depends on ever record_cpp_constants || never record_cpp_constants@
// We want to run after #define constants have been collected but even if there are no #defines.
expression SCHEMA_ID;
expression settings;
expression PATH;
// Coccinelle does not like autocleanup macros in + sections,
// lets use fresh id with concatenation to produce the code as a string.
fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source";
fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema";
fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_ID) { get_schema_directory(SCHEMA_ID) };
@@
-settings = g_settings_new_with_path(SCHEMA_ID, PATH);
+{
+ schema_source_decl;
+ schema_decl;
+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY,
+ g_settings_schema_source_get_default(),
+ TRUE,
+ NULL);
+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_ID, FALSE);
+ settings = g_settings_new_full(schema, NULL, PATH);
+}

View file

@ -45,6 +45,7 @@
, boost
, protobuf
, libiconv
, makeHardcodeGsettingsPatch
}:
stdenv.mkDerivation rec {
@ -150,8 +151,8 @@ stdenv.mkDerivation rec {
'';
passthru = {
hardcodeGsettingsPatch = glib.mkHardcodeGsettingsPatch {
glib-schema-to-var = {
hardcodeGsettingsPatch = makeHardcodeGsettingsPatch {
schemaIdToVariableMapping = {
"org.gnome.Evolution.DefaultSources" = "EDS";
"org.gnome.evolution.shell.network-config" = "EDS";
"org.gnome.evolution-data-server.addressbook" = "EDS";

View file

@ -18,8 +18,7 @@
, coreutils, dbus, libxml2, tzdata
, desktop-file-utils, shared-mime-info
, darwin
# update script
, runCommand, git, coccinelle
, makeHardcodeGsettingsPatch
}:
assert stdenv.isLinux -> util-linuxMinimal != null;
@ -271,55 +270,18 @@ stdenv.mkDerivation (finalAttrs: {
packageName = "glib";
versionPolicy = "odd-unstable";
};
/*
can be used as part of an update script to automatically create a patch
hardcoding the path of all gsettings schemas in C code.
For example:
passthru = {
hardcodeGsettingsPatch = glib.mkHardcodeGsettingsPatch {
inherit src;
glib-schema-to-var = {
...
};
};
updateScript =
let
updateSource = ...;
patch = _experimental-update-script-combinators.copyAttrOutputToFile "evolution-ews.hardcodeGsettingsPatch" ./hardcode-gsettings.patch;
in
_experimental-update-script-combinators.sequence [
updateSource
patch
];
};
}
takes as input a mapping from schema path to variable name.
For example `{ "org.gnome.evolution" = "EVOLUTION_SCHEMA_PATH"; }`
hardcodes looking for `org.gnome.evolution` into `@EVOLUTION_SCHEMA_PATH@`.
All schemas must be listed.
*/
mkHardcodeGsettingsPatch = { src, glib-schema-to-var }:
runCommand
"hardcode-gsettings.patch"
{
mkHardcodeGsettingsPatch =
{
src,
glib-schema-to-var,
}:
builtins.trace
"glib.mkHardcodeGsettingsPatch is deprecated, please use makeHardcodeGsettingsPatch instead"
(makeHardcodeGsettingsPatch {
inherit src;
nativeBuildInputs = [
git
coccinelle
python3 # For patch script
];
}
''
unpackPhase
cd "''${sourceRoot:-.}"
set -x
cp ${builtins.toFile "glib-schema-to-var.json" (builtins.toJSON glib-schema-to-var)} ./glib-schema-to-var.json
git init
git add -A
spatch --sp-file "${./hardcode-gsettings.cocci}" --dir . --in-place
git diff > "$out"
'';
schemaIdToVariableMapping = glib-schema-to-var;
});
};
meta = with lib; {

View file

@ -1,70 +0,0 @@
/**
* Since Nix does not have a standard location like /usr/share,
* where GSettings system could look for schemas, we need to point the software to a correct location somehow.
* For executables, we handle this using wrappers but this is not an option for libraries like e-d-s.
* Instead, we hardcode the schema path when creating the settings.
* A schema path (ie org.gnome.evolution) can be replaced by @EVOLUTION_SCHEMA_PATH@
* which is then replaced at build time by substituteAll.
* The mapping is provided in a json file ./glib-schema-to-var.json
*/
@initialize:python@
@@
import json
cpp_constants = {}
def register_cpp_constant(const_name, val):
cpp_constants[const_name] = val.strip()
def resolve_cpp_constant(const_name):
return cpp_constants.get(const_name, const_name)
with open("./glib-schema-to-var.json") as mapping_file:
schema_to_var = json.load(mapping_file);
def get_schema_directory(schema_path):
# Sometimes the schema id is referenced using C preprocessor #define constant in the same file
# lets try to resolve it first.
schema_path = resolve_cpp_constant(schema_path.strip()).strip('"')
if schema_path in schema_to_var:
return f'"@{schema_to_var[schema_path]}@"'
raise Exception(f"Unknown schema path {schema_path!r}, please add it to ./glib-schema-to-var.json")
@find_cpp_constants@
identifier const_name;
expression val;
@@
#define const_name val
@script:python record_cpp_constants depends on find_cpp_constants@
const_name << find_cpp_constants.const_name;
val << find_cpp_constants.val;
@@
register_cpp_constant(const_name, val)
@depends on ever record_cpp_constants || never record_cpp_constants@
// We want to run after #define constants have been collected but even if there are no #defines.
expression SCHEMA_PATH;
expression settings;
// Coccinelle does not like autocleanup macros in + sections,
// lets use fresh id with concatenation to produce the code as a string.
fresh identifier schema_source_decl = "g_autoptr(GSettingsSchemaSource) " ## "schema_source";
fresh identifier schema_decl = "g_autoptr(GSettingsSchema) " ## "schema";
fresh identifier SCHEMA_DIRECTORY = script:python(SCHEMA_PATH) { get_schema_directory(SCHEMA_PATH) };
@@
-settings = g_settings_new(SCHEMA_PATH);
+{
+ schema_source_decl;
+ schema_decl;
+ schema_source = g_settings_schema_source_new_from_directory(SCHEMA_DIRECTORY,
+ g_settings_schema_source_get_default(),
+ TRUE,
+ NULL);
+ schema = g_settings_schema_source_lookup(schema_source, SCHEMA_PATH, FALSE);
+ settings = g_settings_new_full(schema, NULL, NULL);
+}

View file

@ -81,6 +81,8 @@ with pkgs;
coq = callPackage ./coq {};
makeHardcodeGsettingsPatch = callPackage ./make-hardcode-gsettings-patch { };
makeWrapper = callPackage ./make-wrapper { };
makeBinaryWrapper = callPackage ./make-binary-wrapper {
makeBinaryWrapper = pkgs.makeBinaryWrapper.override {

View file

@ -0,0 +1,58 @@
{ runCommandLocal
, git
, clang-tools
, makeHardcodeGsettingsPatch
}:
let
mkTest =
{
name,
expected,
src,
schemaIdToVariableMapping,
}:
let
patch = makeHardcodeGsettingsPatch {
inherit src schemaIdToVariableMapping;
};
in
runCommandLocal
"makeHardcodeGsettingsPatch-tests-${name}"
{
nativeBuildInputs = [
git
clang-tools
];
}
''
cp -r --no-preserve=all "${src}" src
cp -r --no-preserve=all "${expected}" src-expected
pushd src
patch < "${patch}"
popd
find src -name '*.c' -print0 | while read -d $'\0' sourceFile; do
sourceFile=''${sourceFile#src/}
clang-format -style='{BasedOnStyle: InheritParentConfig, ColumnLimit: 240}' -i "src/$sourceFile" "src-expected/$sourceFile"
git diff --no-index "src/$sourceFile" "src-expected/$sourceFile" | cat
done
touch "$out"
'';
in
{
basic = mkTest {
name = "basic";
src = ./fixtures/example-project;
schemaIdToVariableMapping = {
"org.gnome.evolution-data-server.addressbook" = "EDS";
"org.gnome.evolution.calendar" = "EVO";
"org.gnome.seahorse.nautilus.window" = "SEANAUT";
};
expected = ./fixtures/example-project-patched;
};
}

View file

@ -0,0 +1,85 @@
#include <gio/gio.h>
#include <glib-object.h>
void schema_id_literal() {
GSettings *settings;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@EDS@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, "org.gnome.evolution-data-server.addressbook", FALSE);
settings = g_settings_new_full(schema, NULL, NULL);
}
g_object_unref(settings);
}
#define SELF_UID_PATH_ID "org.gnome.evolution-data-server.addressbook"
int schema_id_from_constant() {
GSettings *settings;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@EDS@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, SELF_UID_PATH_ID, FALSE);
settings = g_settings_new_full(schema, NULL, NULL);
}
g_object_unref(settings);
}
void schema_id_autoptr() {
g_autoptr(GSettings) settings = NULL;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@EVO@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, "org.gnome.evolution.calendar", FALSE);
settings = g_settings_new_full(schema, NULL, NULL);
}
}
void schema_id_with_backend() {
GSettings *settings;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@EDS@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, "org.gnome.evolution-data-server.addressbook", FALSE);
settings = g_settings_new_full(schema, g_settings_backend_get_default(), NULL);
}
g_object_unref(settings);
}
void schema_id_with_backend_and_path() {
GSettings *settings;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@SEANAUT@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, "org.gnome.seahorse.nautilus.window", FALSE);
settings = g_settings_new_full(schema, g_settings_backend_get_default(), "/org/gnome/seahorse/nautilus/windows/123/");
}
g_object_unref(settings);
}
void schema_id_with_path() {
GSettings *settings;
{
g_autoptr(GSettingsSchemaSource) schema_source;
g_autoptr(GSettingsSchema) schema;
schema_source = g_settings_schema_source_new_from_directory("@SEANAUT@", g_settings_schema_source_get_default(), TRUE, NULL);
schema = g_settings_schema_source_lookup(schema_source, "org.gnome.seahorse.nautilus.window", FALSE);
settings = g_settings_new_full(schema, NULL, "/org/gnome/seahorse/nautilus/windows/123/");
}
g_object_unref(settings);
}
int main() {
schema_id_literal();
schema_id_from_constant();
schema_id_autoptr();
schema_id_with_backend();
schema_id_with_backend_and_path();
schema_id_with_path();
return 0;
}

View file

@ -0,0 +1,49 @@
#include <gio/gio.h>
#include <glib-object.h>
void schema_id_literal() {
GSettings *settings;
settings = g_settings_new("org.gnome.evolution-data-server.addressbook");
g_object_unref(settings);
}
#define SELF_UID_PATH_ID "org.gnome.evolution-data-server.addressbook"
int schema_id_from_constant() {
GSettings *settings;
settings = g_settings_new(SELF_UID_PATH_ID);
g_object_unref(settings);
}
void schema_id_autoptr() {
g_autoptr(GSettings) settings = NULL;
settings = g_settings_new("org.gnome.evolution.calendar");
}
void schema_id_with_backend() {
GSettings *settings;
settings = g_settings_new_with_backend("org.gnome.evolution-data-server.addressbook", g_settings_backend_get_default());
g_object_unref(settings);
}
void schema_id_with_backend_and_path() {
GSettings *settings;
settings = g_settings_new_with_backend_and_path("org.gnome.seahorse.nautilus.window", g_settings_backend_get_default(), "/org/gnome/seahorse/nautilus/windows/123/");
g_object_unref(settings);
}
void schema_id_with_path() {
GSettings *settings;
settings = g_settings_new_with_path("org.gnome.seahorse.nautilus.window", "/org/gnome/seahorse/nautilus/windows/123/");
g_object_unref(settings);
}
int main() {
schema_id_literal();
schema_id_from_constant();
schema_id_autoptr();
schema_id_with_backend();
schema_id_with_backend_and_path();
schema_id_with_path();
return 0;
}

View file

@ -1079,6 +1079,8 @@ with pkgs;
{ deps = [ lcov enableGCOVInstrumentation ]; }
../build-support/setup-hooks/make-coverage-analysis-report.sh;
makeHardcodeGsettingsPatch = callPackage ../build-support/make-hardcode-gsettings-patch { };
# intended to be used like nix-build -E 'with import <nixpkgs> {}; enableDebugging fooPackage'
enableDebugging = pkg: pkg.override { stdenv = stdenvAdapters.keepDebugInfo pkg.stdenv; };