1"""Config on Darwin w/ frameworks"""
2
3import os
4from distutils.sysconfig import get_python_inc
5
6
7try:
8    from config_unix import DependencyProg
9except ImportError:
10    from buildconfig.config_unix import DependencyProg
11
12
13try:
14    basestring_ = basestring
15except NameError:
16    #python 3.
17    basestring_ = str
18
19class Dependency:
20    libext = '.dylib'
21    def __init__(self, name, checkhead, checklib, libs):
22        self.name = name
23        self.inc_dir = None
24        self.lib_dir = None
25        self.libs = libs
26        self.found = 0
27        self.checklib = checklib
28        if self.checklib:
29            self.checklib += self.libext
30        self.checkhead = checkhead
31        self.cflags = ''
32
33    def configure(self, incdirs, libdirs):
34        incnames = self.checkhead
35        libnames = self.checklib, self.name.lower()
36        for dir in incdirs:
37            if isinstance(incnames, basestring_):
38                incnames = [incnames]
39
40            for incname in incnames:
41                path = os.path.join(dir, incname)
42                if os.path.isfile(path):
43                    self.inc_dir = os.path.dirname(path)
44                    break
45
46        for dir in libdirs:
47            for name in libnames:
48                path = os.path.join(dir, name)
49                if os.path.isfile(path):
50                    self.lib_dir = dir
51                    break
52        if self.inc_dir and (self.lib_dir or not self.checklib):
53            print (self.name + '        '[len(self.name):] + ': found')
54            self.found = 1
55        else:
56            print (self.name + '        '[len(self.name):] + ': not found')
57
58class FrameworkDependency(Dependency):
59    def configure(self, incdirs, libdirs):
60        BASE_DIRS = '/', os.path.expanduser('~/'), '/System/'
61        for n in BASE_DIRS:
62            n += 'Library/Frameworks/'
63            fmwk = n + self.libs + '.framework/Versions/Current/'
64            if os.path.isdir(fmwk):
65                print ('Framework ' + self.libs + ' found')
66                self.found = 1
67                self.inc_dir = fmwk + 'Headers'
68                self.cflags = (
69                    '-Xlinker "-framework" -Xlinker "' + self.libs + '"' +
70                    ' -Xlinker "-F' + n + '"')
71                self.origlib = self.libs
72                self.libs = ''
73                return
74        print ('Framework ' + self.libs + ' not found')
75
76
77class DependencyPython:
78    def __init__(self, name, module, header):
79        self.name = name
80        self.lib_dir = ''
81        self.inc_dir = ''
82        self.libs = []
83        self.cflags = ''
84        self.found = 0
85        self.ver = '0'
86        self.module = module
87        self.header = header
88
89    def configure(self, incdirs, libdirs):
90        self.found = 1
91        if self.module:
92            try:
93                self.ver = __import__(self.module).__version__
94            except ImportError:
95                self.found = 0
96        if self.found and self.header:
97            fullpath = os.path.join(get_python_inc(0), self.header)
98            if not os.path.isfile(fullpath):
99                self.found = 0
100            else:
101                self.inc_dir = os.path.split(fullpath)[0]
102        if self.found:
103            print (self.name + '        '[len(self.name):] + ': found', self.ver)
104        else:
105            print (self.name + '        '[len(self.name):] + ': not found')
106
107def find_freetype():
108    """ modern freetype uses pkg-config. However, some older systems don't have that.
109    """
110    pkg_config = DependencyProg(
111        'FREETYPE', 'FREETYPE_CONFIG', 'pkg-config freetype2', '2.0',
112        ['freetype2'], '--modversion'
113    )
114    if pkg_config.found:
115        return pkg_config
116
117    #DependencyProg('FREETYPE', 'FREETYPE_CONFIG', '/usr/X11R6/bin/freetype-config', '2.0',
118    freetype_config = DependencyProg(
119        'FREETYPE', 'FREETYPE_CONFIG', 'freetype-config', '2.0', ['freetype'], '--ftversion'
120    )
121    if freetype_config.found:
122        return freetype_config
123    return pkg_config
124
125
126
127
128
129def main():
130
131    DEPS = [
132        [DependencyProg('SDL', 'SDL_CONFIG', 'sdl2-config', '2.0', ['sdl'])],
133        [Dependency('FONT', ['SDL_ttf.h', 'SDL2/SDL_ttf.h'], 'libSDL2_ttf', ['SDL2_ttf'])],
134        [Dependency('IMAGE', ['SDL_image.h', 'SDL2/SDL_image.h'], 'libSDL2_image', ['SDL2_image'])],
135        [Dependency('MIXER', ['SDL_mixer.h', 'SDL2/SDL_mixer.h'], 'libSDL2_mixer', ['SDL2_mixer'])],
136    ]
137
138    DEPS.extend([
139        Dependency('PNG', 'png.h', 'libpng', ['png']),
140        Dependency('JPEG', 'jpeglib.h', 'libjpeg', ['jpeg']),
141        Dependency('PORTMIDI', 'portmidi.h', 'libportmidi', ['portmidi']),
142        Dependency('PORTTIME', 'porttime.h', '', []),
143        find_freetype(),
144        # Scrap is included in sdlmain_osx, there is nothing to look at.
145        # Dependency('SCRAP', '','',[]),
146    ])
147
148    print ('Hunting dependencies...')
149    incdirs = ['/usr/local/include', '/opt/homebrew/include']
150    incdirs.extend(['/usr/local/include/SDL2', '/opt/homebrew/include/SDL2', '/opt/local/include/SDL2'])
151
152    incdirs.extend([
153       #'/usr/X11/include',
154       '/opt/local/include',
155       '/opt/local/include/freetype2/freetype']
156    )
157    #libdirs = ['/usr/local/lib', '/usr/X11/lib', '/opt/local/lib']
158    libdirs = ['/usr/local/lib', '/opt/local/lib', '/opt/homebrew/lib']
159
160    for d in DEPS:
161        if isinstance(d, (list, tuple)):
162            for deptype in d:
163                deptype.configure(incdirs, libdirs)
164        else:
165            d.configure(incdirs, libdirs)
166
167    for d in DEPS:
168        if type(d)==list:
169            found = False
170            for deptype in d:
171                if deptype.found:
172                    found = True
173                    DEPS[DEPS.index(d)] = deptype
174                    break
175            if not found:
176                DEPS[DEPS.index(d)] = d[0]
177
178    DEPS[0].cflags = '-Ddarwin '+ DEPS[0].cflags
179    return DEPS
180
181
182if __name__ == '__main__':
183    print ("""This is the configuration subscript for OSX Darwin.
184             Please run "config.py" for full configuration.""")
185