Create a gnome module and add support for resource compiling.

pull/39/merge
Jussi Pakkanen 10 years ago
parent 52b69b8939
commit 519f159dcc
  1. 43
      interpreter.py
  2. 37
      modules/gnome.py
  3. 2
      modules/modtest.py
  4. 1
      test cases/frameworks/10 gresource/data/res1.txt
  5. 27
      test cases/frameworks/10 gresource/main.c
  6. 12
      test cases/frameworks/10 gresource/meson.build
  7. 6
      test cases/frameworks/10 gresource/myresource.gresource.xml

@ -411,8 +411,8 @@ class JarHolder(BuildTargetHolder):
super().__init__(build.Jar, name, subdir, is_cross, sources, objects, environment, kwargs) super().__init__(build.Jar, name, subdir, is_cross, sources, objects, environment, kwargs)
class CustomTargetHolder(InterpreterObject): class CustomTargetHolder(InterpreterObject):
def __init__(self, name, subdir, kwargs): def __init__(self, object_to_hold):
self.held_object = build.CustomTarget(name, subdir, kwargs) self.held_object = object_to_hold
def is_cross(self): def is_cross(self):
return self.held_object.is_cross() return self.held_object.is_cross()
@ -593,18 +593,27 @@ class CompilerHolder(InterpreterObject):
mlog.log('Has header "%s":' % string, h) mlog.log('Has header "%s":' % string, h)
return haz return haz
class ModuleState:
pass
class ModuleHolder(InterpreterObject): class ModuleHolder(InterpreterObject):
def __init__(self, modname): def __init__(self, modname, interpreter):
InterpreterObject.__init__(self) InterpreterObject.__init__(self)
self.modname = modname self.modname = modname
self.m = importlib.import_module('modules.' + modname) self.m = importlib.import_module('modules.' + modname)
self.interpreter = interpreter
def method_call(self, method_name, args, kwargs): def method_call(self, method_name, args, kwargs):
try: try:
fn = getattr(self.m, method_name) fn = getattr(self.m, method_name)
except AttributeError: except AttributeError:
raise InvalidArguments('Module %s does not have method %s.' % (self.modname, method_name)) raise InvalidArguments('Module %s does not have method %s.' % (self.modname, method_name))
fn(args, kwargs) state = ModuleState()
state.build_to_src = os.path.relpath(self.interpreter.environment.get_source_dir(),
self.interpreter.environment.get_build_dir())
state.subdir = self.interpreter.subdir
value = fn(state, args, kwargs)
return self.interpreter.module_method_callback(value)
class MesonMain(InterpreterObject): class MesonMain(InterpreterObject):
def __init__(self, build, interpreter): def __init__(self, build, interpreter):
@ -759,6 +768,28 @@ class Interpreter():
'import' : self.func_import, 'import' : self.func_import,
} }
def module_method_callback(self, invalues):
unwrap_single = False
if invalues is None:
return
if not isinstance(invalues, list):
unwrap_single = True
invalues = [invalues]
outvalues = []
for v in invalues:
if isinstance(v, build.CustomTarget):
if v.name in self.build.targets:
raise InterpreterException('Tried to create target %s which already exists.' % v.name)
self.build.targets[v.name] = v
outvalues.append(CustomTargetHolder(v))
elif isinstance(v, int) or isinstance(v, str):
outvalues.append(v)
else:
raise InterpreterException('Module returned a value of unknown type.')
if len(outvalues) == 1 and unwrap_single:
return outvalues[0]
return outvalues
def get_build_def_files(self): def get_build_def_files(self):
return self.build_def_files return self.build_def_files
@ -821,7 +852,7 @@ class Interpreter():
if not isinstance(modname, str): if not isinstance(modname, str):
raise InvalidCode('Argument to import was not a string') raise InvalidCode('Argument to import was not a string')
if not modname in self.modules: if not modname in self.modules:
self.modules[modname] = ModuleHolder(modname) self.modules[modname] = ModuleHolder(modname, self)
return self.modules[modname] return self.modules[modname]
def set_variable(self, varname, variable): def set_variable(self, varname, variable):
@ -1200,7 +1231,7 @@ class Interpreter():
% name) % name)
if name in self.build.targets: if name in self.build.targets:
raise InvalidCode('Tried to create target "%s", but a target of that name already exists.' % name) raise InvalidCode('Tried to create target "%s", but a target of that name already exists.' % name)
tg = CustomTargetHolder(name, self.subdir, kwargs) tg = CustomTargetHolder(build.CustomTarget(name, self.subdir, kwargs))
self.build.targets[name] = tg.held_object self.build.targets[name] = tg.held_object
return tg return tg

@ -0,0 +1,37 @@
# Copyright 2012-2015 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''This module provides helper functions for Gnome/GLib related
functionality such as gobject-introspection and gresources.'''
import build
import os
def compile_resources(state, args, kwargs):
cmd = ['glib-compile-resources', '@INPUT@', '--generate']
if 'source_dir' in kwargs:
d = os.path.join(state.build_to_src, kwargs.pop('source_dir'))
cmd += ['--sourcedir', d]
if 'c_name' in kwargs:
cmd += ['--c-name', kwargs.pop('c_name')]
cmd += ['--target', '@OUTPUT@']
kwargs['command'] = cmd
output_c = args[0] + '.c'
output_h = args[0] + '.h'
kwargs['input'] = args[1]
kwargs['output'] = output_c
target_c = build.CustomTarget(args[0]+'_c', state.subdir, kwargs)
kwargs['output'] = output_h
target_h = build.CustomTarget(args[0] + '_h', state.subdir, kwargs)
return [target_c, target_h]

@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
def print_hello(args, kwargs): def print_hello(state, args, kwargs):
print('Hello from a Meson module') print('Hello from a Meson module')

@ -0,0 +1,27 @@
#include<stdio.h>
#include<string.h>
#include<gio/gio.h>
#include"myresources.h"
#define EXPECTED "This is a resource.\n"
int main(int argc, char **argv) {
GResource *res = myres_get_resource();
GError *err = NULL;
GBytes *data = g_resources_lookup_data("/com/example/myprog/res1.txt",
G_RESOURCE_LOOKUP_FLAGS_NONE, &err);
if(data == NULL) {
fprintf(stderr, "Data lookup failed: %s\n", err->message);
return 1;
}
if(strcmp(g_bytes_get_data(data, NULL), EXPECTED) != 0) {
fprintf(stderr, "Resource contents are wrong:\n %s\n",
(const char*)g_bytes_get_data(data, NULL));
return 1;
}
fprintf(stderr, "All ok.\n");
g_bytes_unref(data);
g_resource_unref(res);
return 0;
}

@ -0,0 +1,12 @@
project('glib compile resource', 'c')
gnome = import('gnome')
gio = dependency('gio-2.0')
myres = gnome.compile_resources('myresources', 'myresource.gresource.xml',
source_dir : 'data',
c_name : 'myres')
exe = executable('resprog', 'main.c', myres,
dependencies : gio)
test('resource test', exe)

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/example/myprog">
<file>res1.txt</file>
</gresource>
</gresources>
Loading…
Cancel
Save