summaryrefslogtreecommitdiff
path: root/site_scons/aptitude_configure_checks.py
blob: a5327f5150f4cb037427b10e2d02d6727dc667e7 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Copyright (C) 2010 Daniel Burrows
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

from SCons.Script import AddOption, GetOption
import SCons.Script

# Custom configure checks for aptitude and the code to register and
# configure them.

custom_tests = {}

def Configure(env):
    """Creates an aptitude-flavored configuration object targeting
the given environment."""

    result = SCons.Script.Configure(env, custom_tests, config_h = 'config.h')
    result.Define('PACKAGE', '"%s"' % env['PACKAGE'])
    result.Define('VERSION', '"%s"' % env['VERSION'])
    # Need to inform the source code that we have a config.h file:
    env.MergeFlags(env.ParseFlags('-DHAVE_CONFIG_H'))
    return result

def RegisterCheck(f, name = None):
    """Decorates a custom configure check by registering it in the
    global dictionary of checks under its name."""

    if name is None:
        name = f.__name__
    custom_tests[name] = f
    return f


def ConfigureCheck(message, register = True, name = None):
    """Decorates a custom configure function by modifying its context
as specified in kwargs before running it.  If the test succeeds
by returning a true value, the environment is preserved; if the test
fails, the environment is restored.

The "tries" keyword argument can be used to specify an explicit series
of checks.  The advantage of doing this versus simply invoking the
check multiple times is that you get better messages: instead of

Checking for ncursesw... no
Checking for ncursesw... yes

you get:

Checking for ncursesw...
  In /usr/include... no
  In /usr/include/ncursesw... yes

Each entry of "tries" has the form (msg, vars).  For instance, in the
above example. "msg" would be "In /usr/include" or "In /usr/include/ncursesw".
If "tries" is not present, one run is performed with the values in kwargs.

This decorator also adds the test to the custom_tests dictionary."""
    def decorator(f, name = name):
        def check(context, tries = None, *args, **kwargs):
            context.Message('%s...' % message)

            if tries is None:
                show_msg = False
                tries = [("", {})]
            else:
                if len(tries) == 0:
                    raise Exception('Configure checks must have at least one test.')
                context.Result('')
                show_msg = True

            for msg, bindings in tries:
                if show_msg:
                    context.Message('  %s...' % msg)

                env2 = context.env.Clone()
                context.env.Append(**kwargs)
                context.env.Append(**bindings)
                result = f(context, *args)

                context.Result(bool(result))

                if not result:
                    # TODO: this might not work if variables were changed
                    # that weren't in the original environment.  What to
                    # do then?
                    context.env.Replace(**env2.Dictionary())
                else:
                    return result

            return result

        if name is None:
            name = f.__name__
        if register:
            if name in custom_tests:
                raise Exception('Duplicate function name \"%s\".' % f)
            else:
                custom_tests[name] = check

        check.__name__ = name
        return check

    return decorator

def TryInclude(d):
    """Generate a single entry in "tries" that outputs an appropriate message."""

    return ("In %s" % d, { 'CPPPATH' : [ d ] })

@RegisterCheck
def CheckForExecutable(context, filename, var, help = None):
    """Look for the given filename in $PATH.  If var is set in the
environment, use its value; otherwise find the program and set var to
its absolute path."""
    context.Message("Checking for %s..." % filename)

    AddOption('--with-%s' % filename,
              dest = 'check_for_%s' % filename,
              nargs = 1,
              type = 'string', action = 'store',
              metavar = 'DIR',
              default = context.env.get(var),
              help = help or 'set the path to %s' % filename)
    location = GetOption('check_for_%s' % filename)
    if location is None:
        location = context.env.WhereIs(filename)

    if location is None:
        context.Result('no')
        return False
    else:
        context.env[var] = location
        context.Result(location)
        return True

def MakeCheckForExecutable(name):
    """Register a configure method named CheckForName that checks for
the given executable and sets NAME to its location."""
    # This won't cover every case, but it will cut down on code
    # duplication in a lot of them.

    method_name = 'CheckFor%s' % name.capitalize()
    var_name = name.upper()

    def check(context):
        """Look for %s in $PATH and set $%s to its location.""" % (name, var_name)

        return CheckForExecutable(context, name, var_name)
    RegisterCheck(check, name = method_name)
    return check

@ConfigureCheck("Checking for apt")
def CheckForApt(context):
    """Look for apt in the given directory."""

    context.env.Append(LIBS = [ 'apt-pkg' ])

    return context.TryLink('''
#include <apt-pkg/init.h>

int main(int argc, char **argv)
{
  pkgInitSystem(*_config, _system);
  return 0;
}''', context.env['CXXFILESUFFIX'])

@ConfigureCheck("Checking for libncursesw")
def CheckForNCursesW(context):
    """Look for NCursesW in the system header directory."""

    context.env.Append(LIBS = [ 'ncursesw' ])

    return context.TryLink('''
#include <ncurses.h>

int main(int argc, char **argv)
{
  wget_wch(0, 0);
  return 0;
}''', context.env['CXXFILESUFFIX'])

@ConfigureCheck("Checking for libpthread")
def CheckForPThread(context):
    """Look for POSIX thread support."""

    context.env.Append(LIBS = [ 'pthread' ])

    return context.TryLink('''
#include <pthread.h>

int main(int argc, char **argv)
{
  pthread_mutex_t mutex;
  pthread_mutexattr_t mutexattr;

  pthread_mutexattr_init(&mutexattr);
  pthread_mutex_init(&mutex, &mutexattr);
  return 0;
}''', context.env['CXXFILESUFFIX'])

@ConfigureCheck("Checking for Boost.IOStreams")
def CheckForBoostIOStreams(context):
    """Look for Boost.IOStreams."""

    context.env.Append(LIBS = [ 'boost_iostreams' ])

    return context.TryLink('''
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>

int main(int argc, char **argv)
{
  boost::iostreams::file_sink devnull("/dev/null");
  boost::iostreams::filtering_ostream compressed_devnull(boost::iostreams::zlib_compressor(9) | devnull);
}''', context.env['CXXFILESUFFIX'])

@ConfigureCheck("Checking for Boost.Test")
def CheckForBoostTest(context):
    """Look for Boost.Test."""

    context.env.Append(LIBS = [ 'boost_unit_test_framework' ],
                       CPPDEFINES = [ 'BOOST_TEST_DYN_LINK',
                                      'BOOST_TEST_NO_MAIN' ])

    return context.TryLink('''
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(dummy)
{
}

bool init_unit_test()
{
  return true;
}

int main(int argc, char **argv)
{
  return boost::unit_test::unit_test_main(init_unit_test, argc, argv);
}''', context.env['CXXFILESUFFIX'])

@ConfigureCheck("Checking for CPPUnit")
def CheckForCPPUnit(context):
    """Look for CPPUnit."""

    context.env.Append(LIBS = 'cppunit')

    return context.TryLink('''
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

class FooTest : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(FooTest);

  CPPUNIT_TEST(testDummy);

  CPPUNIT_TEST_SUITE_END();

public:
  void testDummy()
  {
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(FooTest);

int main(int argc, char **argv)
{
  CppUnit::TextTestRunner runner;
  CppUnit::TestFactoryRegistry &registry =
    CppUnit::TestFactoryRegistry::getRegistry();

  runner.addTest(registry.makeTest());

  bool wasSuccessful = runner.run("", false);

  return wasSuccessful ? 0 : -255;
}''', context.env['CXXFILESUFFIX'])

@RegisterCheck
def PkgConfig(context, *pkgs):
    context.Message('Checking for %s' % pkgs)

    flags = context.env.GetPkgConfigFlags(*pkgs)

    if flags is not None:
        context.env.MergeFlags(flags)
        result = True
    else:
        result = False

    context.Result(result)
    return result

@ConfigureCheck('Checking for GTK+ libraries')
def CheckGTK(context):
    '''If the necessary libraries for GTK+ support appear to be
available, add the flags necessary to activate them to GTKFLAGS and
set the variable HAVE_GTK to 1.  Also arrange for -DHAVE_GTK to be
part of GTKFLAGS.'''

    SCons.Script.AddOption('--disable-gtk',
                           dest = 'disable_gtk',
                           default = False,
                           action = 'store_true')

    if SCons.Script.GetOption('disable_gtk'):
        context.Result(False)
        print 'Disabling the GTK+ frontend at your request (--disable-gtk).'
        return False

    flags = context.env.GetPkgConfigFlags(
        'glibmm-2.4',
        'gthread-2.0',
        'gtkmm-2.4',
        'libglademm-2.4',
        )

    if flags is not None:
        context.env.Replace(GTKFLAGS = '%s -DHAVE_GTK=1' % flags, HAVE_GTK = 1)
        result = True
    else:
        result = False

    context.Result(result)
    return result

MakeCheckForExecutable("xsltproc")

@ConfigureCheck("Checking for po4a")
def CheckForPo4A(context):
    """Look for po4a in $PATH and set $PO4A accordingly."""

    has_po4a = CheckForExecutable(context, 'po4a', 'PO4A')
    has_po4a_translate = CheckForExecutable(context, 'po4a-translate', 'PO4A_TRANSLATE')
    return has_po4a and has_po4a_translate

# The old Makefile defines both MSGFMT and GMSGFMT.  It's unclear what
# the distinction is supposed to be; they get defined to the same
# thing.
MakeCheckForExecutable("msgfmt")
MakeCheckForExecutable("xgettext")

# TODO: the Makefile stored actual commands, MSGMERGE="msgmerge
# --previous" and MSGMERGE_UPDATE="msgmerge --previous --update".
# Presumably there should be configure checks for platforms where
# msgmerge behaves differently.
MakeCheckForExecutable("msgmerge")
MakeCheckForExecutable("msginit")
MakeCheckForExecutable("msgconv")
MakeCheckForExecutable("msgfilter")

@ConfigureCheck("Checking for libintl in libc")
def CheckForLibintlInLibc(context):
    """Check whether libintl is already included in libc."""

    if context.TryLink('''
#include <libintl.h>
#include <stdio.h> // For printf.

int main(int argc, char **argv)
{
  const char * const foo = gettext("Foo");
  printf("%s\\n", foo);
  return 0;
}''', context.env['CXXFILESUFFIX']):
        context.Result('yes')
        return True
    else:
        context.Result('no')
        return False

@ConfigureCheck('Checking for setlocale in libintl.h')
def CheckSetLocale(context):
    if context.TryLink('''
#include <locale.h>

int main(int argc, char **argv)
{
  setlocale(0, 0);
}''', context.env['CXXFILESUFFIX']):
        context.Result('yes')
        return True
    else:
        context.Result('no')
        return False

@ConfigureCheck('Checking whether apt supports ddtp')
def CheckDDTP(context):
    if context.TryLink('''
#include <apt-pkg/pkgcache.h>

int main(int argc, char **argv)
{
  pkgCache::DescIterator d;
}''', context.env['CXXFILESUFFIX']):
        context.Result('yes')
        return True
    else:
        context.Result('no')
        return False


def FindGettextTools(configure):
    """Look for utilities needed to process pofiles."""

    result = True

    result = configure.CheckForMsgfmt() and result
    result = configure.CheckForXgettext() and result
    result = configure.CheckForMsgmerge() and result
    result = configure.CheckForMsginit() and result
    result = configure.CheckForMsgconv() and result
    result = configure.CheckForMsgfilter() and result

    configure.env['HAVE_GETTEXT_UTILS'] = result
    return result

def FindGettext(configure):
    """Look for gettext-related functions."""

    result = True

    result = configure.CheckForLibintlInLibc() and result
    result = configure.CheckHeader('libintl.h') and result
    result = configure.CheckHeader('locale.h') and result

    if configure.CheckSetLocale():
        configure.Define("HAVE_SETLOCALE")
    else:
        result = False

    if result:
        configure.Define("ENABLE_NLS", 1)

    return result