File: configure.py

package info (click to toggle)
sip4-qt3 4.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,528 kB
  • ctags: 2,380
  • sloc: ansic: 11,574; yacc: 2,356; python: 1,489; cpp: 559; lex: 468; makefile: 80
file content (871 lines) | stat: -rw-r--r-- 25,512 bytes parent folder | download
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# This script handles the SIP configuration and generates the Makefiles.
#
# Copyright (c) 2004
# 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
# 
# This file is part of SIP.
# 
# This copy of SIP is licensed for use under the terms of the SIP License
# Agreement.  See the file LICENSE for more details.
# 
# SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


import sys
import os
import string
import glob
import getopt

import siputils


# Initialise the globals.
sip_version = 0x040101
sip_version_str = "4.1.1"
qt_version = 0
qt_edition = ""
qt_winconfig = ""
qt_threaded = 0
qt_dir = None
qt_incdir = None
qt_libdir = None
py_version = sys.hexversion >> 8
plat_py_site_dir = None
plat_py_inc_dir = None
plat_py_lib_dir = None
plat_sip_dir = None
plat_bin_dir = None
platform_specs = []
default_platform = None

# Command line options.
opt_platform = None
opt_qtlib = None
opt_sipbindir = None
opt_sipmoddir = None
opt_sipincdir = None
opt_sipsipdir = None
opt_static = 0
opt_debug = 0

# The names of build macros extracted from the platform specific configuration
# files.
build_macros = [
    "DEFINES",
    "CC",
    "CFLAGS",
    "CFLAGS_RELEASE", "CFLAGS_DEBUG",
    "CFLAGS_CONSOLE", "CFLAGS_SHLIB", "CFLAGS_THREAD",
    "CFLAGS_MT", "CFLAGS_MT_DBG", "CFLAGS_MT_DLL", "CFLAGS_MT_DLLDBG",
    "CFLAGS_EXCEPTIONS_ON", "CFLAGS_EXCEPTIONS_OFF",
    "CFLAGS_RTTI_ON", "CFLAGS_RTTI_OFF",
    "CFLAGS_STL_ON", "CFLAGS_STL_OFF",
    "CFLAGS_WARN_ON", "CFLAGS_WARN_OFF",
    "CHK_DIR_EXISTS", "COPY",
    "CXX",
    "CXXFLAGS",
    "CXXFLAGS_RELEASE", "CXXFLAGS_DEBUG",
    "CXXFLAGS_CONSOLE", "CXXFLAGS_SHLIB", "CXXFLAGS_THREAD",
    "CXXFLAGS_MT", "CXXFLAGS_MT_DBG", "CXXFLAGS_MT_DLL", "CXXFLAGS_MT_DLLDBG",
    "CXXFLAGS_EXCEPTIONS_ON", "CXXFLAGS_EXCEPTIONS_OFF",
    "CXXFLAGS_RTTI_ON", "CXXFLAGS_RTTI_OFF",
    "CXXFLAGS_STL_ON", "CXXFLAGS_STL_OFF",
    "CXXFLAGS_WARN_ON", "CXXFLAGS_WARN_OFF",
    "DEL_FILE",
    "EXTENSION_SHLIB",
    "INCDIR", "INCDIR_X11", "INCDIR_QT", "INCDIR_OPENGL",
    "LINK", "LINK_SHLIB", "AIX_SHLIB", "LINK_SHLIB_CMD",
    "LFLAGS", "LFLAGS_CONSOLE", "LFLAGS_CONSOLE_DLL", "LFLAGS_DEBUG",
    "LFLAGS_PLUGIN", "LFLAGS_RELEASE", "LFLAGS_SHLIB", "LFLAGS_SONAME",
    "LFLAGS_THREAD", "LFLAGS_WINDOWS", "LFLAGS_WINDOWS_DLL", "LFLAGS_OPENGL",
    "LIBDIR", "LIBDIR_X11", "LIBDIR_QT", "LIBDIR_OPENGL",
    "LIBS", "LIBS_CONSOLE", "LIBS_OPENGL", "LIBS_OPENGL", "LIBS_RT",
    "LIBS_RTMT", "LIBS_THREAD", "LIBS_WINDOWS", "LIBS_X11",
    "MAKEFILE_GENERATOR",
    "MKDIR", "MOC",
    "RPATH",
    "AR", "RANLIB", "LIB"
]


def usage(rcode = 2):
    """Display a usage message and exit.

    rcode is the return code passed back to the calling process.
    """
    print "Usage:"
    print "    python configure.py [-h] [-b dir] [-d dir] [-e dir] [-k] [-l Qt-library] [-p platform] [-u] [-v dir] [-x] option=value option+=value ..."
    print "where:"
    print "    -h             display this help message"
    print "    -b dir         where the SIP code generator will be installed [default %s]" % opt_sipbindir
    print "    -d dir         where the SIP module will be installed [default %s]" % opt_sipmoddir
    print "    -e dir         where the SIP header file will be installed [default %s]" % opt_sipincdir
    print "    -k             build the SIP module as a static library"
    print "    -l Qt-library  explicitly specify the type of Qt library, either qt, qt-mt, qte, qte-mt or qtmt"
    print "    -p platform    the platform/compiler configuration [default the Qt configuration if enabled or %s]" % default_platform
    print "    -u             build with debugging symbols"
    print "    -v dir         where .sip files are normally installed [default %s]" % opt_sipsipdir
    print "    -x             Qt support is disabled"

    # Display the different platform/compilers.
    print
    print "The following platform/compiler configurations are supported:"
    print

    platform_specs.sort()

    ss = ""
    sep = ""
    for s in platform_specs:
        ss = ss + sep + s
        sep = ", "

    print siputils.format(ss, 2)

    # Display the different build macros.
    print
    print "The following options may be used to adjust the compiler configuration:"
    print

    build_macros.sort()

    ss = ""
    sep = ""
    for s in build_macros:
        ss = ss + sep + s
        sep = " "

    print siputils.format(ss, 2)

    sys.exit(rcode)


def set_defaults():
    """Set up the defaults for values that can be set on the command line.
    """
    global default_platform, qt_dir
    global opt_sipbindir, opt_sipmoddir, opt_sipincdir, opt_sipsipdir

    # Set the platform specific default specification.
    platdefaults = {
        "aix":          "aix-xlc",
        "bsd":          "bsdi-g++",
        "cygwin":       "cygwin-g++",
        "darwin":       "macx-g++",
        "dgux":         "dgux-g++",
        "freebsd":      "freebsd-g++",
        "gnu":          "hurd-g++",
        "hp-ux":        "hpux-acc",
        "irix":         "irix-cc",
        "linux":        "linux-g++",
        "lynxos":       "lynxos-g++",
        "netbsd":       "netbsd-g++",
        "openbsd":      "openbsd-g++",
        "openunix":     "unixware-cc",
        "osf1":         "tru64-cxx",
        "qnx":          "qnx-g++",
        "reliantunix":  "reliant-cds",
        "sco_sv":       "sco-cc",
        "sinix":        "reliant-cds",
        "sunos5":       "solaris-cc",
        "ultrix":       "ultrix-g++",
        "unix_sv":      "unixware-g++",
        "unixware":     "unixware-cc",
        "win32":        "win32-msvc"
    }

    default_platform = "none"

    for pd in platdefaults.keys():
        if sys.platform[:len(pd)] == pd:
            default_platform = platdefaults[pd]
            break

    # If the environment variable is set assume that Qt is installed and
    # support for it enabled.
    try:
        qt_dir = os.environ["QTDIR"]
    except KeyError:
        pass

    opt_sipbindir = plat_bin_dir
    opt_sipmoddir = plat_py_site_dir
    opt_sipincdir = plat_py_inc_dir
    opt_sipsipdir = plat_sip_dir


def inform_user():
    """Tell the user the option values that are going to be used.
    """
    siputils.inform("The SIP code generator will be installed in %s." % opt_sipbindir)
    siputils.inform("The SIP module will be installed in %s." % opt_sipmoddir)
    siputils.inform("The SIP header file will be installed in %s." % opt_sipincdir)
    siputils.inform("The default directory to install .sip files in is %s." % opt_sipsipdir)

    if qt_version:
        if qt_edition:
            edstr = qt_edition + " edition "
        else:
            edstr = ""

        siputils.inform("Qt v%s %sis being used." % (siputils.version_to_string(qt_version), edstr))
        siputils.inform("The Qt header files are in %s." % qt_incdir)
        siputils.inform("The %s Qt library is in %s." % (opt_qtlib, qt_libdir))
    else:
        siputils.inform("Qt support is disabled.")

    if opt_platform:
        siputils.inform("The platform/compiler configuration is %s." % opt_platform)
    else:
        siputils.inform("Qt's default platform/compiler configuration will be used.")


def set_platform_directories():
    """Initialise the global variables relating to platform specific
    directories.
    """
    global plat_py_site_dir, plat_py_inc_dir, plat_bin_dir, plat_sip_dir
    global plat_py_lib_dir

    if sys.platform == "win32":
        plat_py_site_dir = sys.prefix + "\\Lib"
        if py_version >= 0x020200:
            plat_py_site_dir = plat_py_site_dir + "\\site-packages"

        plat_py_inc_dir = sys.prefix + "\\include"
        plat_py_lib_dir = sys.prefix + "\\libs"
        plat_bin_dir = sys.exec_prefix
        plat_sip_dir = sys.prefix + "\\sip"
    else:
        vers = "%d.%d" % ((py_version >> 16) & 0xff, (py_version >> 8) & 0xff)

        plat_py_site_dir = sys.prefix + "/lib/python" + vers
        if py_version >= 0x020000:
            plat_py_site_dir = plat_py_site_dir + "/site-packages"

        plat_py_inc_dir = sys.prefix + "/include/python" + vers
        plat_py_lib_dir = sys.prefix + "/lib/python" + vers + "/config"
        plat_bin_dir = sys.exec_prefix + "/bin"
        plat_sip_dir = sys.prefix + "/share/sip"


def create_config(module, template, macros):
    """Create the SIP configuration module so that it can be imported by build
    scripts.

    module is the module file name.
    template is the template file name.
    macros is the dictionary of build macros.
    """
    siputils.inform("Creating %s..." % module)

    content = {
        "sip_version":      sip_version,
        "sip_version_str":  sip_version_str,
        "sip_bin":          os.path.join(opt_sipbindir, "sip"),
        "sip_inc_dir":      opt_sipincdir,
        "sip_mod_dir":      opt_sipmoddir,
        "default_bin_dir":  plat_bin_dir,
        "default_mod_dir":  plat_py_site_dir,
        "default_sip_dir":  opt_sipsipdir,
        "py_version":       py_version,
        "py_inc_dir":       plat_py_inc_dir,
        "py_lib_dir":       plat_py_lib_dir,
        "qt_version":       qt_version
    }

    if qt_version:
        content["qt_edition"] = qt_edition
        content["qt_winconfig"] = qt_winconfig
        content["qt_threaded"] = qt_threaded
        content["qt_dir"] = qt_dir
        content["qt_inc_dir"] = qt_incdir
        content["qt_lib"] = opt_qtlib
        content["qt_lib_dir"] = qt_libdir

    # Write the (nicely formatted) dictionary.
    content = siputils.create_content(content) + "_default_macros = {\n"

    names = macros.keys()
    names.sort()

    width = 0
    for c in names:
        clen = len(c)
        if width < clen:
            width = clen

    for c in names:
        if c == names[-1]:
            sep = ""
        else:
            sep = ","

        k = "'" + c + "':"
        content = content + "    %-*s  '%s'%s\n" % (1 + width + 2, k, string.replace(macros[c], "\\", "\\\\"), sep)

    content = content + "}\n"

    siputils.create_config_module(module, template, content)


def check_qt_installation(macros):
    """Check the Qt installation and get the version number and edition.

    macros is the dictionary of macros.
    """
    # Get the Makefile generator.
    generator = macros["MAKEFILE_GENERATOR"]

    if not generator:
        generator = "UNIX"
    elif generator == "DSP":
        # tmake used a different generator name.
        generator = "MSVC"

    # Make sure the correct value gets written.
    macros["MAKEFILE_GENERATOR"] = generator

    # Set the Qt include and lib directories.
    global qt_incdir, qt_libdir

    qt_incdir = macros["INCDIR_QT"]

    if not qt_incdir:
        qt_incdir = os.path.join(qt_dir, "include")
        macros["INCDIR_QT"] = qt_incdir

    qt_libdir = macros["LIBDIR_QT"]

    if not qt_libdir:
        qt_libdir = os.path.join(qt_dir, "lib")
        macros["LIBDIR_QT"] = qt_libdir

    # Check the Qt header files have been installed.
    qglobal = os.path.join(qt_incdir, "qglobal.h")

    if not os.access(qglobal, os.F_OK):
        siputils.error("qglobal.h could not be found in %s." % qt_incdir)

    # Get the Qt version number.
    global qt_version

    qt_version, ignore = siputils.read_version(qglobal, "Qt", "QT_VERSION")

    # Early versions of Qt for the Mac didn't include everything.  Rather than
    # maintain these in the future we just mandate a later version.
    if sys.platform == "darwin" and qt_version < 0x030100:
        siputils.error("PyQt for MacOS/X requires Qt v3.1.0 or later.")

    # Try and work out which edition it is.
    global qt_edition

    if qt_version >= 0x030000:
        qconfig = os.path.join(qt_incdir, "qconfig.h")

        if not os.access(qconfig,os.F_OK):
            siputils.error("qconfig.h could not be found in %s." % qt_incdir)

        f = open(qconfig)
        l = f.readline()

        while l:
            wl = string.split(l)
            if len(wl) == 3 and wl[0] == "#define" and wl[1] == "QT_PRODUCT_LICENSE":
                    qt_edition = wl[2][4:-1]
                    break

            l = f.readline()

        f.close()

        if not qt_edition:
            siputils.error("The Qt edition could not be determined by parsing %s." % qconfig)
    elif qt_version == 0x020300 and sys.platform == "win32":
        # See if we have the Qt v2 non-commercial version.
        if os.access(os.path.join(qt_libdir, "qt-mt230nc.lib"), os.F_OK):
            qt_edition = "non-commercial"

    # See which version of the Qt library can be found.  (We can't trust the
    # configuration files.)
    global opt_qtlib

    if opt_qtlib:
        if not is_qt_library(generator, opt_qtlib):
            siputils.error("The %s Qt library could not be found in %s." % (opt_qtlib, qt_libdir))
    else:
        stlib = is_qt_library(generator, "qt")
        mtlib = is_qt_library(generator, "qt-mt")
        edlib = is_qt_library(generator, "qt-mtedu")
        evlib = is_qt_library(generator, "qt-mteval")
        emlib = is_qt_library(generator, "qte")
        etlib = is_qt_library(generator, "qte-mt")

        # Borland likes to be a little different.
        bmtlib = is_qt_library(generator, "qtmt")
        bedlib = is_qt_library(generator, "qtmtedu")
        bevlib = is_qt_library(generator, "qtmteval")

        names = []

        if stlib:
            opt_qtlib = "qt"
            names.append(opt_qtlib)

        if mtlib:
            opt_qtlib = "qt-mt"
            names.append(opt_qtlib)

        if edlib:
            opt_qtlib = "qt-mtedu"
            names.append(opt_qtlib)

        if evlib:
            opt_qtlib = "qt-mteval"
            names.append(opt_qtlib)

        if emlib:
            opt_qtlib = "qte"
            names.append(opt_qtlib)

        if etlib:
            opt_qtlib = "qte-mt"
            names.append(opt_qtlib)

        if bmtlib:
            opt_qtlib = "qtmt"
            names.append(opt_qtlib)

        if bedlib:
            opt_qtlib = "qtmtedu"
            names.append(opt_qtlib)

        if bevlib:
            opt_qtlib = "qtmteval"
            names.append(opt_qtlib)

        if not names:
            siputils.error("No Qt libraries could be found in %s." % qt_libdir)

        if len(names) > 1:
            siputils.error("These Qt libraries were found: %s. Use the -l argument to explicitly specify which you want to use." % string.join(names))

    # See if Qt has thread support.
    if opt_qtlib in ("qt-mt", "qt-mtedu", "qt-mteval", "qte-mt", "qtmt", "qtmtedu", "qtmteval"):
        global qt_threaded
        qt_threaded = 1

    # Try and work out how Qt was built on Windows.
    if sys.platform == "win32":
        global qt_winconfig

        try:
            f = open(os.path.join(qt_dir, ".qtwinconfig"), "r")
        except IOError:
            f = None

        if f:
            cfg = f.readline()
            f.close()

            val = string.find(cfg, "=")

            if val >= 0:
                qt_winconfig = string.strip(cfg[val + 1:])
        else:
            # Assume it was built as a DLL.
            qt_winconfig = "shared"


def is_qt_library(generator, lib):
    """See if a particular Qt library is installed.

    generator is the name of the Makefile generator.
    lib is the name of the library.
    """
    if generator in ("MSVC", "MSVC.NET", "BMAKE"):
        lpatts = [lib + "[0-9]*.lib", lib + ".lib"]
    else:
        lpatts = ["lib" + lib + ".*"]

    for lpatt in lpatts:
        lmatch = glob.glob(os.path.join(qt_libdir, lpatt))

        if lmatch:
            return lmatch

    return []


def get_build_macros(overrides):
    """Return a dictionary of build macros extracted from the user's Qt
    configuration, or what we bundle with SIP.

    overrides is the dictionary of modified build macros.
    """
    macro_prefix = "QMAKE_"

    # Get the name of the file we want to read the macros from.
    if opt_platform:
        fname = os.path.join("specs", opt_platform)
    elif "QMAKESPEC" in os.environ.keys():
        fname = os.path.join(qt_dir, "mkspecs", os.environ["QMAKESPEC"], "qmake.conf")
    else:
        qname = os.path.join(qt_dir, "mkspecs", "default", "qmake.conf")

        if os.access(qname, os.F_OK):
            fname = qname
        else:
            try:
                tmakepath = os.environ["TMAKEPATH"]
            except KeyError:
                siputils.error("This version of Qt seems to use tmake but the TMAKEPATH environment is not set.")

            tname = os.path.join(tmakepath, "tmake.conf")

            if not os.access(tname, os.F_OK):
                siputils.error("Unable to find either %s for qmake or %s for tmake." % (qname, tname))

            fname = tname
            macro_prefix = "TMAKE_"

    try:
        f = open(fname, "r")
    except IOError, detail:
        siputils.error("Unable to open %s: %s" % (fname, detail))

    # Get everything into a dictionary.
    raw = {
        "LITERAL_WHITESPACE":   " ",
        "LITERAL_DOLLAR":       "$",
        "LITERAL_HASH":         "#"
    }

    line = f.readline()
    while line:
        # Handle line continuations.
        while len(line) > 1 and line[-2] == "\\":
            line = line[:-2]

            next = f.readline()

            if next:
                line = line + next
            else:
                break

        line = string.strip(line)

        # Ignore comments.
        if line and line[0] != "#":
            assstart = string.find(line, "+")
            if assstart > 0 and line[assstart + 1] == '=':
                assend = assstart + 1
            else:
                assstart = string.find(line, "=")
                assend = assstart

            if assstart > 0:
                lhs = string.strip(line[:assstart])
                rhs = string.strip(line[assend + 1:])

                raw[lhs] = rhs

        line = f.readline()

    # Go through the raw dictionary extracting the macros we need and resolving
    # any macro expansions.  First of all, make sure every macro has a value.
    refined = {}

    for m in build_macros:
        refined[m] = ""

    for lhs in raw.keys():
        # Strip any prefix.
        if string.find(lhs, macro_prefix) == 0:
            reflhs = lhs[len(macro_prefix):]
        else:
            reflhs = lhs

        # See if we are interested in this one.
        if reflhs not in build_macros:
            continue

        rhs = raw[lhs]

        # Resolve any references.
        mstart = string.find(rhs, "$$")

        while mstart >= 0:
            rstart = mstart + 2
            if rstart < len(rhs) and rhs[rstart] == "{":
                rstart = rstart + 1
                term = "}"
            else:
                term = string.whitespace

            mend = rstart
            while mend < len(rhs) and rhs[mend] not in term:
                mend = mend + 1

            lhs = rhs[rstart:mend]

            if term == "}":
                mend = mend + 1

            try:
                rhs = rhs[:mstart] + raw[lhs] + rhs[mend:]
            except KeyError:
                siputils.error("%s: macro '%s' is not defined." % (fname, lhs))

            mstart = string.find(rhs, "$$")

        # Expand any POSIX style environment variables.
        estart = string.find(rhs, "$(")

        while estart >= 0:
            eend = string.find(rhs[estart + 2:], ")")

            if eend < 0:
                break

            eend = estart + 2 + eend

            name = rhs[estart + 2:eend]

            try:
                env = os.environ[name]
            except KeyError:
                env = ""

            rhs = rhs[:estart] + env + rhs[eend + 1:]

            estart = string.find(rhs, "$(")

        # Expand any Windows style environment variables.
        estart = string.find(rhs, "%")

        while estart >= 0:
            eend = string.find(rhs[estart + 1:], "%")

            if eend < 0:
                break

            eend = estart + 1 + eend

            name = rhs[estart + 1:eend]

            try:
                env = os.environ[name]
            except KeyError:
                env = ""

            rhs = rhs[:estart] + env + rhs[eend + 1:]

            estart = string.find(rhs, "%")

        refined[reflhs] = rhs

    # Handle the user overrides.
    for lhs in overrides.keys():
        rhs = refined[lhs]
        oride = overrides[lhs]

        if string.find(oride, "+") == 0:
            if rhs:
                rhs = rhs + " " + oride[1:]
            else:
                rhs = oride[1:]
        else:
            rhs = oride

        refined[lhs] = rhs

    return refined


def create_makefiles():
    """Create the Makefiles.
    """
    # Bootstrap.
    import sipconfig

    cfg = sipconfig.Configuration()

    sipconfig.inform("Creating top level Makefile...")

    sipconfig.ParentMakefile(
        configuration=cfg,
        subdirs=["sipgen", "siplib"],
        installs=("sipconfig.py", cfg.sip_mod_dir)
    ).generate()

    sipconfig.inform("Creating sip code generator Makefile...")

    sipconfig.ProgramMakefile(
        configuration=cfg,
        build_file="sipgen.sbf",
        dir="sipgen",
        install_dir=os.path.dirname(cfg.sip_bin),
        console=1,
        warnings=0
    ).generate()

    sipconfig.inform("Creating sip module Makefile...")

    hfiles = ["sip.h"]

    if cfg.sip_version < 0x040000:
        hfiles.append("sipQt.h")

    if cfg.sip_version >= 0x040000 or cfg.qt_version:
        sbf = "siplib.sbf"
    else:
        sbf = "siplib-noqt.sbf"

    makefile = sipconfig.ModuleMakefile(
        configuration=cfg,
        build_file=sbf,
        dir="siplib",
        install_dir=cfg.sip_mod_dir,
        installs=(hfiles, cfg.sip_inc_dir),
        qt=cfg.qt_version,
        warnings=0,
        static=opt_static,
        debug=opt_debug
    )

    if cfg.qt_version:
        makefile.extra_defines.append("SIP_QT_SUPPORT")

    if cfg.sip_version < 0x040000 and sys.platform == "win32":
        if opt_static:
            libdef = "SIP_MAKE_STATIC"
        else:
            libdef = "SIP_MAKE_DLL"

        makefile.extra_defines.append(libdef)

    makefile.generate()


def main(argv):
    """Create the configuration module module.

    argv is the list of command line arguments.
    """
    # Basic initialisation.
    set_platform_directories()

    # Build up the list of valid specs.
    for s in os.listdir("specs"):
        platform_specs.append(s)

    # Handle the command line.
    set_defaults()

    try:
        optlist, args = getopt.getopt(argv[1:], "hb:d:e:kl:p:uv:x")
    except getopt.GetoptError:
        usage()

    global qt_dir, opt_sipbindir, opt_sipmoddir, opt_sipincdir, opt_sipsipdir
    global opt_platform, opt_qtlib, opt_static, opt_debug

    for opt, arg in optlist:
        if opt == "-h":
            usage(0)
        elif opt == "-b":
            opt_sipbindir = arg
        elif opt == "-d":
            opt_sipmoddir = arg
        elif opt == "-e":
            opt_sipincdir = arg
        elif opt == "-k":
            opt_static = 1
        elif opt == "-l":
            if arg in ("qt", "qt-mt", "qt-mtedu", "qt-mteval", "qte", "qte-mt", "qtmt", "qtmtedu"):
                opt_qtlib = arg
            else:
                usage()
        elif opt == "-p":
            if arg not in platform_specs:
                usage()
            
            opt_platform = arg
        elif opt == "-q":
            opt_qtdir = arg
        elif opt == "-u":
            opt_debug = 1
        elif opt == "-v":
            opt_sipsipdir = arg
        elif opt == "-x":
            qt_dir = None

    if qt_dir is None and opt_platform is None:
        opt_platform = default_platform

    # Allow the user to modify the build macros.
    overrides = {}

    for arg in args:
        prefix = ""
        name_end = string.find(arg, "+=")

        if name_end >= 0:
            prefix = "+"
            val_start = name_end + 2
        else:
            name_end = string.find(arg, "=")

            if name_end >= 0:
                val_start = name_end + 1
            else:
                usage()

        name = arg[:name_end]

        if name not in build_macros:
            usage()

        overrides[name] = prefix + arg[val_start:]

    # Get the platform specific macros for building.
    macros = get_build_macros(overrides)

    siputils.inform("This is SIP %s for Python %s on %s." % (sip_version_str, string.split(sys.version)[0], sys.platform))

    if sip_version >= 0x040000 and py_version < 0x020300:
        siputils.error("This version of SIP requires Python v2.3 or later.")

    # Check the Qt installation.
    if qt_dir:
        check_qt_installation(macros)

    # Tell the user what's been found.
    inform_user()

    # Install the configuration module.
    create_config("sipconfig.py", "siputils.py", macros)

    # Create the Makefiles.
    create_makefiles()


###############################################################################
# The script starts here.
###############################################################################

if __name__ == "__main__":
    try:
        main(sys.argv)
    except SystemExit:
        raise
    except:
        print \
"""An internal error occured.  Please report all the output from the program,
including the following traceback, to support@riverbankcomputing.co.uk.
"""
        raise