1# Copyright (C) 2009 Kevin Ollivier  All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7#    notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9#    notice, this list of conditions and the following disclaimer in the
10#    documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
13# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
16# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23#
24# Library for functions to determine wx settings based on configuration
25
26import os
27import re
28
29import Options
30
31def parse_build_cfg(filename):
32    cfg_file = open(filename, 'r')
33    cfg = {}
34    for cfg_line in cfg_file.readlines():
35        key = None
36        value = None
37        parts = cfg_line.split('=')
38        if len(parts) >= 1:
39            key = parts[0].strip()
40
41        if len(parts) >= 2:
42            value = parts[1].strip()
43            if value.isdigit():
44                value = int(value)
45
46        if key:
47            cfg[key] = value
48
49    return cfg
50
51def get_wx_version(wx_root):
52    versionText = open(os.path.join(wx_root, "include", "wx", "version.h"), "r").read()
53
54    majorVersion = re.search("#define\swxMAJOR_VERSION\s+(\d+)", versionText).group(1)
55    minorVersion = re.search("#define\swxMINOR_VERSION\s+(\d+)", versionText).group(1)
56    releaseVersion = re.search("#define\swxRELEASE_NUMBER\s+(\d+)", versionText).group(1)
57
58    release = [majorVersion, minorVersion]
59    if int(minorVersion) % 2 == 1:
60        release.append(releaseVersion)
61    return release
62
63def get_wxmsw_settings(wx_root, shared = False, unicode = False, debug = False, wxPython=False):
64    if not os.path.exists(wx_root):
65        print "Directory %s does not exist." % wx_root
66        sys.exit(1)
67
68    defines = ['__WXMSW__']
69    includes = [os.path.join(wx_root, 'include')]
70    cxxflags = []
71    libs = []
72    libpaths = []
73
74    libdir = os.path.join(wx_root, 'lib')
75    ext = ''
76    postfix = 'vc'
77
78    version_str_nodot = ''.join(get_wx_version(wx_root)[0:2])
79
80    if shared:
81        defines.append('WXUSINGDLL')
82        libdir = os.path.join(libdir, Options.options.wx_compiler_prefix + '_dll')
83    else:
84        libdir = os.path.join(libdir, Options.options.wx_compiler_prefix + '_lib')
85
86    if unicode:
87        defines.append('_UNICODE')
88        ext += 'u'
89
90    depext = ''
91    if wxPython and not version_str_nodot.startswith('29'):
92        ext += 'h'
93        depext += 'h'
94    elif debug:
95        ext += 'd'
96        depext += 'd'
97
98    configdir = os.path.join(libdir, 'msw' + ext)
99
100    monolithic = False
101    cfg_file = os.path.join(configdir, 'build.cfg')
102    if os.path.exists(cfg_file):
103        cfg = parse_build_cfg(cfg_file)
104        if "MONOLITHIC" in cfg:
105            monolithic = cfg["MONOLITHIC"]
106    libpaths.append(libdir)
107    includes.append(configdir)
108
109    def get_wxlib_name(name):
110        if name == 'base':
111            return 'wxbase%s%s' % (version_str_nodot, ext)
112
113        return "wxmsw%s%s_%s" % (version_str_nodot, ext, name)
114
115    libs.extend(['wxzlib' + depext, 'wxjpeg' + depext, 'wxpng' + depext, 'wxexpat' + depext])
116    if monolithic:
117        libs.extend(["wxmsw%s%s" % (version_str_nodot, ext)])
118    else:
119        libs.extend([get_wxlib_name('base'), get_wxlib_name('core')])
120
121    if wxPython or debug:
122        defines.append('__WXDEBUG__')
123
124    return (defines, includes, libs, libpaths)
125