1#!/usr/bin/env python
2# encoding: utf-8
3# Thomas Nagy, 2006-2010 (ita)
4
5"""
6Support for the KDE4 libraries and msgfmt
7"""
8
9import os, re
10from waflib import Task, Utils
11from waflib.TaskGen import feature
12
13@feature('msgfmt')
14def apply_msgfmt(self):
15	"""
16	Process all languages to create .mo files and to install them::
17
18		def build(bld):
19			bld(features='msgfmt', langs='es de fr', appname='myapp', install_path='${KDE4_LOCALE_INSTALL_DIR}')
20	"""
21	for lang in self.to_list(self.langs):
22		node = self.path.find_resource(lang+'.po')
23		task = self.create_task('msgfmt', node, node.change_ext('.mo'))
24
25		langname = lang.split('/')
26		langname = langname[-1]
27
28		inst = getattr(self, 'install_path', '${KDE4_LOCALE_INSTALL_DIR}')
29
30		self.add_install_as(
31			inst_to = inst + os.sep + langname + os.sep + 'LC_MESSAGES' + os.sep + getattr(self, 'appname', 'set_your_appname') + '.mo',
32			inst_from = task.outputs[0],
33			chmod = getattr(self, 'chmod', Utils.O644))
34
35class msgfmt(Task.Task):
36	"""
37	Transform .po files into .mo files
38	"""
39	color   = 'BLUE'
40	run_str = '${MSGFMT} ${SRC} -o ${TGT}'
41
42def configure(self):
43	"""
44	Detect kde4-config and set various variables for the *use* system::
45
46		def options(opt):
47			opt.load('compiler_cxx kde4')
48		def configure(conf):
49			conf.load('compiler_cxx kde4')
50		def build(bld):
51			bld.program(source='main.c', target='app', use='KDECORE KIO KHTML')
52	"""
53	kdeconfig = self.find_program('kde4-config')
54	prefix = self.cmd_and_log(kdeconfig + ['--prefix']).strip()
55	fname = '%s/share/apps/cmake/modules/KDELibsDependencies.cmake' % prefix
56	try:
57		os.stat(fname)
58	except OSError:
59		fname = '%s/share/kde4/apps/cmake/modules/KDELibsDependencies.cmake' % prefix
60		try:
61			os.stat(fname)
62		except OSError:
63			self.fatal('could not open %s' % fname)
64
65	try:
66		txt = Utils.readf(fname)
67	except EnvironmentError:
68		self.fatal('could not read %s' % fname)
69
70	txt = txt.replace('\\\n', '\n')
71	fu = re.compile('#(.*)\n')
72	txt = fu.sub('', txt)
73
74	setregexp = re.compile(r'([sS][eE][tT]\s*\()\s*([^\s]+)\s+\"([^"]+)\"\)')
75	found = setregexp.findall(txt)
76
77	for (_, key, val) in found:
78		#print key, val
79		self.env[key] = val
80
81	# well well, i could just write an interpreter for cmake files
82	self.env['LIB_KDECORE']= ['kdecore']
83	self.env['LIB_KDEUI']  = ['kdeui']
84	self.env['LIB_KIO']    = ['kio']
85	self.env['LIB_KHTML']  = ['khtml']
86	self.env['LIB_KPARTS'] = ['kparts']
87
88	self.env['LIBPATH_KDECORE']  = [os.path.join(self.env.KDE4_LIB_INSTALL_DIR, 'kde4', 'devel'), self.env.KDE4_LIB_INSTALL_DIR]
89	self.env['INCLUDES_KDECORE'] = [self.env['KDE4_INCLUDE_INSTALL_DIR']]
90	self.env.append_value('INCLUDES_KDECORE', [self.env['KDE4_INCLUDE_INSTALL_DIR']+ os.sep + 'KDE'])
91
92	self.find_program('msgfmt', var='MSGFMT')
93
94