<commit_before>from fnmatch import filter as fnfilter
from sys import platform, argv
from os.path import join, dirname, realpath, sep, exists
from os import walk, environ
from distutils.core import setup
from distutils.extension import Extension

try:
    # check for cython
    from Cython.Distutils import build_ext
    have_cython = True
except ImportError:
    print '\nCython is missing, its required for compiling kivy !\n\n'
    have_cython = False
    from distutils.command.build_ext import build_ext

# extract version (simulate doc generation, kivy will be not imported)
environ['KIVY_DOC_INCLUDE'] = '1'
import kivy

# extra build commands go in the cmdclass dict {'command-name': CommandClass}
# see tools.packaging.{platform}.build.py for custom build commands for
# portable packages.  also e.g. we use build_ext command from cython if its
# installed for c extensions.
cmdclass = {}

try:
    # add build rules for portable packages to cmdclass
    if platform == 'win32':
        from kivy.tools.packaging.win32.build import WindowsPortableBuild
        cmdclass['build_portable'] = WindowsPortableBuild
    elif platform == 'darwin':
        from kivy.tools.packaging.osx.build import OSXPortableBuild
        cmdclass['build_portable'] = OSXPortableBuild
except ImportError:
    print 'User distribution detected, avoid portable command.'

from kivy.tools.packaging.factory import FactoryBuild
cmdclass['build_factory'] = FactoryBuild

#
# Detect options
#
c_options = {
    'use_opengl_es2': True,
    'use_opengl_debug': False,
    'use_glew': False,
    'use_sdl': False,
    'use_ios': False,
    'use_mesagl': False}

# now check if environ is changing the default values
for key in c_options.keys():
    ukey = key.upper()
    if ukey in environ:
        value = bool(int(environ[ukey]))
        print 'Environ change %s -> %s' % (key, value)
        c_options[key] = value

# check if we are in a kivy-ios build
kivy_ios_root = environ.get('KIVYIOSROOT', None)
if kivy_ios_root is not None:
    print 'Kivy-IOS project environment detect, use it.'
    print 'Kivy-IOS project located at %r' % kivy_ios_root
    print 'Activate SDL compilation.'
    c_options['use_ios'] = True
    c_options['use_sdl'] = True

# Detect which opengl version headers to use
if platform == 'win32':
    print 'Windows platform detected, force GLEW usage.'
    c_options['use_glew'] = True
elif platform == 'darwin':
    # macosx is using their own gl.h
    pass
else:
    # searching GLES headers
    default_header_dirs = ['/usr/include', '/usr/local/include']
    found = False
    for hdir in default_header_dirs:
        filename = join(hdir, 'GLES2', 'gl2.h')
        if exists(filename):
            found = True
            print 'Found GLES 2.0 headers at', filename
            break
    if not found:
        print 'WARNING: GLES 2.0 headers are not found'
        print 'Fallback to Desktop opengl headers.'
        c_options['use_opengl_es2'] = False


class KivyBuildExt(build_ext):

    def build_extensions(self):
        print 'Generate config.h'
        config_h = join(dirname(__file__), 'kivy', 'graphics', 'config.h')
        with open(config_h, 'w') as fd:
            fd.write('// Autogenerated file for Kivy C configuration\n')
            for k, v in c_options.iteritems():
                fd.write('#define __%s %d\n' % (k.upper(), int(v)))

        print 'Generate config.pxi'
        config_pxi = join(dirname(__file__), 'kivy', 'graphics', 'config.pxi')
        with open(config_pxi, 'w') as fd:
            fd.write('# Autogenerated file for Kivy Cython configuration\n')
            for k, v in c_options.iteritems():
                fd.write('DEF %s = %d\n' % (k.upper(), int(v)))

        build_ext.build_extensions(self)

cmdclass['build_ext'] = KivyBuildExt

# extension modules
ext_modules = []

# list all files to compile
pyx_files = []
pxd_files = []
kivy_libs_dir = realpath(join(kivy.kivy_base_dir, 'libs'))
for root, dirnames, filenames in walk(join(dirname(__file__), 'kivy')):
    # ignore lib directory
    if realpath(root).startswith(kivy_libs_dir):
        continue
    for filename in fnfilter(filenames, '*.pxd'):
        pxd_files.append(join(root, filename))
    for filename in fnfilter(filenames, '*.pyx'):
        pyx_files.append(join(root, filename))

if not have_cython:
    pyx_files = pyx_files = ['%s.c' % x[:-4] for x in pyx_files]
    pxd_files = []

# add cython core extension modules if cython is available

if True:
    libraries = ['m']
    include_dirs = []
    extra_link_args = []
    if platform == 'win32':
        libraries.append('opengl32')
    elif platform == 'darwin':
        # On OSX, it's not -lGL, but -framework OpenGL...
        extra_link_args = ['-framework', 'OpenGLES']
    elif platform.startswith('freebsd'):
        include_dirs += ['/usr/local/include']
        extra_link_args += ['-L', '/usr/local/lib']
    else:
        libraries.append('GLESv2')

    if c_options['use_glew']:
        if platform == 'win32':
            libraries.append('glew32')
        else:
            libraries.append('GLEW')

    def get_modulename_from_file(filename):
        pyx = '.'.join(filename.split('.')[:-1])
        pyxl = pyx.split(sep)
        while pyxl[0] != 'kivy':
            pyxl.pop(0)
        if pyxl[1] == 'kivy':
            pyxl.pop(0)
        return '.'.join(pyxl)

    OrigExtension = Extension

    def Extension(*args, **kwargs):
        # Small hack to only compile for x86_64 on OSX.
        # Is there a better way to do this?
        #if platform == 'darwin':
        #    extra_args = ['-arch', 'x86_64']
        #    kwargs['extra_compile_args'] = extra_args + \
        #        kwargs.get('extra_compile_args', [])
        #    kwargs['extra_link_args'] = extra_args + \
        #        kwargs.get('extra_link_args', [])
        return OrigExtension(*args, **kwargs)

    if c_options['use_sdl']:
        sdl_libraries = ['SDL', 'SDL_ttf', 'freetype', 'z', 'bz2', 'm']
        sdl_includes = []
        sdl_extra_link_args = []
        sdl_extra_compile_args = []
        if platform == 'darwin':
            # Paths as per homebrew (modified formula to use hg checkout)
            if c_options['use_ios']:
                '''
                sdl_extra_link_args += [
                        '-march=armv7', '-mcpu=arm1176jzf-s',
                        '-mcpu=cortex-a8']
                sdl_extra_compile_args += [
                        '-march=armv7', '-mcpu=arm1176jzf-s',
                        '-mcpu=cortex-a8']
                '''
                sdl_includes += [join(kivy_ios_root, 'build', 'include')]
                sdl_includes += [join(kivy_ios_root, 'build', 'include', 'SDL')]
                sdl_includes += [join(kivy_ios_root, 'build', 'include', 'freetype')]
                sdl_extra_link_args += ['-L', join(kivy_ios_root, 'build', 'lib')]
            sdl_extra_link_args += ['-framework', 'Foundation']
            sdl_extra_link_args += ['-framework', 'UIKit']
            sdl_extra_link_args += ['-framework', 'AudioToolbox']
            sdl_extra_link_args += ['-framework', 'CoreGraphics']
            sdl_extra_link_args += ['-framework', 'QuartzCore']
            sdl_extra_link_args += ['-framework', 'ImageIO']
        else:
            sdl_includes = ['/usr/local/include/SDL']
            sdl_extra_link_args += ['-L/usr/local/lib/']

    pxd_core = [x for x in pxd_files if not 'graphics' in x]
    pxd_graphics = [x for x in pxd_files if 'graphics' in x]

    for pyx in pyx_files:
        module_name = get_modulename_from_file(pyx)
        ext_files = [pyx]
        ext_libraries = libraries[:]
        ext_include_dirs = include_dirs[:]
        ext_extra_link_args = extra_link_args[:]
        ext_extra_compile_args = []

        if c_options['use_ios']:
            isysroot = ['-isysroot', '/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk']
            ext_include_dirs += ['/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/']
            ext_extra_compile_args += isysroot<commit_msg>setup: add a fix for duplicate symbol name/implementation at the loading of text_sdlttf/sdl libraries. Because the ios loader already link against libSDL.a, we should not include the .a on our python extension. But this will lead to unknown symbol. Since we know what we are doing, remove SDL from libraries, and add -undefined dynamic_lookup to allow unknown symbol (care, this can lead to a crash if we are using real unknown symbol...)<commit_after>from fnmatch import filter as fnfilter
from sys import platform, argv
from os.path import join, dirname, realpath, sep, exists
from os import walk, environ
from distutils.core import setup
from distutils.extension import Extension

try:
    # check for cython
    from Cython.Distutils import build_ext
    have_cython = True
except ImportError:
    print '\nCython is missing, its required for compiling kivy !\n\n'
    have_cython = False
    from distutils.command.build_ext import build_ext

# extract version (simulate doc generation, kivy will be not imported)
environ['KIVY_DOC_INCLUDE'] = '1'
import kivy

# extra build commands go in the cmdclass dict {'command-name': CommandClass}
# see tools.packaging.{platform}.build.py for custom build commands for
# portable packages.  also e.g. we use build_ext command from cython if its
# installed for c extensions.
cmdclass = {}

try:
    # add build rules for portable packages to cmdclass
    if platform == 'win32':
        from kivy.tools.packaging.win32.build import WindowsPortableBuild
        cmdclass['build_portable'] = WindowsPortableBuild
    elif platform == 'darwin':
        from kivy.tools.packaging.osx.build import OSXPortableBuild
        cmdclass['build_portable'] = OSXPortableBuild
except ImportError:
    print 'User distribution detected, avoid portable command.'

from kivy.tools.packaging.factory import FactoryBuild
cmdclass['build_factory'] = FactoryBuild

#
# Detect options
#
c_options = {
    'use_opengl_es2': True,
    'use_opengl_debug': False,
    'use_glew': False,
    'use_sdl': False,
    'use_ios': False,
    'use_mesagl': False}

# now check if environ is changing the default values
for key in c_options.keys():
    ukey = key.upper()
    if ukey in environ:
        value = bool(int(environ[ukey]))
        print 'Environ change %s -> %s' % (key, value)
        c_options[key] = value

# check if we are in a kivy-ios build
kivy_ios_root = environ.get('KIVYIOSROOT', None)
if kivy_ios_root is not None:
    print 'Kivy-IOS project environment detect, use it.'
    print 'Kivy-IOS project located at %r' % kivy_ios_root
    print 'Activate SDL compilation.'
    c_options['use_ios'] = True
    c_options['use_sdl'] = True

# Detect which opengl version headers to use
if platform == 'win32':
    print 'Windows platform detected, force GLEW usage.'
    c_options['use_glew'] = True
elif platform == 'darwin':
    # macosx is using their own gl.h
    pass
else:
    # searching GLES headers
    default_header_dirs = ['/usr/include', '/usr/local/include']
    found = False
    for hdir in default_header_dirs:
        filename = join(hdir, 'GLES2', 'gl2.h')
        if exists(filename):
            found = True
            print 'Found GLES 2.0 headers at', filename
            break
    if not found:
        print 'WARNING: GLES 2.0 headers are not found'
        print 'Fallback to Desktop opengl headers.'
        c_options['use_opengl_es2'] = False


class KivyBuildExt(build_ext):

    def build_extensions(self):
        print 'Generate config.h'
        config_h = join(dirname(__file__), 'kivy', 'graphics', 'config.h')
        with open(config_h, 'w') as fd:
            fd.write('// Autogenerated file for Kivy C configuration\n')
            for k, v in c_options.iteritems():
                fd.write('#define __%s %d\n' % (k.upper(), int(v)))

        print 'Generate config.pxi'
        config_pxi = join(dirname(__file__), 'kivy', 'graphics', 'config.pxi')
        with open(config_pxi, 'w') as fd:
            fd.write('# Autogenerated file for Kivy Cython configuration\n')
            for k, v in c_options.iteritems():
                fd.write('DEF %s = %d\n' % (k.upper(), int(v)))

        build_ext.build_extensions(self)

cmdclass['build_ext'] = KivyBuildExt

# extension modules
ext_modules = []

# list all files to compile
pyx_files = []
pxd_files = []
kivy_libs_dir = realpath(join(kivy.kivy_base_dir, 'libs'))
for root, dirnames, filenames in walk(join(dirname(__file__), 'kivy')):
    # ignore lib directory
    if realpath(root).startswith(kivy_libs_dir):
        continue
    for filename in fnfilter(filenames, '*.pxd'):
        pxd_files.append(join(root, filename))
    for filename in fnfilter(filenames, '*.pyx'):
        pyx_files.append(join(root, filename))

if not have_cython:
    pyx_files = pyx_files = ['%s.c' % x[:-4] for x in pyx_files]
    pxd_files = []

# add cython core extension modules if cython is available

if True:
    libraries = ['m']
    include_dirs = []
    extra_link_args = []
    if platform == 'win32':
        libraries.append('opengl32')
    elif platform == 'darwin':
        # On OSX, it's not -lGL, but -framework OpenGL...
        extra_link_args = ['-framework', 'OpenGLES']
    elif platform.startswith('freebsd'):
        include_dirs += ['/usr/local/include']
        extra_link_args += ['-L', '/usr/local/lib']
    else:
        libraries.append('GLESv2')

    if c_options['use_glew']:
        if platform == 'win32':
            libraries.append('glew32')
        else:
            libraries.append('GLEW')

    def get_modulename_from_file(filename):
        pyx = '.'.join(filename.split('.')[:-1])
        pyxl = pyx.split(sep)
        while pyxl[0] != 'kivy':
            pyxl.pop(0)
        if pyxl[1] == 'kivy':
            pyxl.pop(0)
        return '.'.join(pyxl)

    OrigExtension = Extension

    def Extension(*args, **kwargs):
        # Small hack to only compile for x86_64 on OSX.
        # Is there a better way to do this?
        #if platform == 'darwin':
        #    extra_args = ['-arch', 'x86_64']
        #    kwargs['extra_compile_args'] = extra_args + \
        #        kwargs.get('extra_compile_args', [])
        #    kwargs['extra_link_args'] = extra_args + \
        #        kwargs.get('extra_link_args', [])
        return OrigExtension(*args, **kwargs)

    if c_options['use_sdl']:
        sdl_libraries = ['SDL', 'SDL_ttf', 'freetype', 'z', 'bz2', 'm']
        sdl_includes = []
        sdl_extra_link_args = []
        sdl_extra_compile_args = []
        if platform == 'darwin':
            # Paths as per homebrew (modified formula to use hg checkout)
            if c_options['use_ios']:
                '''
                sdl_extra_link_args += [
                        '-march=armv7', '-mcpu=arm1176jzf-s',
                        '-mcpu=cortex-a8']
                sdl_extra_compile_args += [
                        '-march=armv7', '-mcpu=arm1176jzf-s',
                        '-mcpu=cortex-a8']
                '''
                # Note: on IOS, SDL is already loaded by the launcher/main.m
                # So if we add it here, it will just complain about duplicate
                # symbol, cause libSDL.a would be included in main.m binary +
                # text_sdlttf.so
                # At the result, we are linking without SDL explicitly, and add
                # -undefined dynamic_lookup
                # (/tito)
                sdl_libraries = ['SDL_ttf', 'freetype', 'bz2']
                sdl_includes += [join(kivy_ios_root, 'build', 'include')]
                sdl_includes += [join(kivy_ios_root, 'build', 'include', 'SDL')]
                sdl_includes += [join(kivy_ios_root, 'build', 'include', 'freetype')]
                sdl_extra_link_args += ['-L', join(kivy_ios_root, 'build', 'lib')]
                sdl_extra_link_args += ['-undefined', 'dynamic_lookup']
            sdl_extra_link_args += ['-framework', 'Foundation']
            sdl_extra_link_args += ['-framework', 'UIKit']
            sdl_extra_link_args += ['-framework', 'AudioToolbox']
            sdl_extra_link_args += ['-framework', 'CoreGraphics']
            sdl_extra_link_args += ['-framework', 'QuartzCore']
            sdl_extra_link_args += ['-framework', 'ImageIO']
        else:
            sdl_includes = ['/usr/local/include/SDL']
            sdl_extra_link_args += ['-L/usr/local/lib/']

    pxd_core = [x for x in pxd_files if not 'graphics' in x]
    pxd_graphics = [x for x in pxd_files if 'graphics' in x]

    for pyx in pyx_files:
        module_name = get_modulename_from_file(pyx)
        ext_files = [pyx]
        ext_libraries = libraries[:]
        ext_include_dirs = include_dirs[:]
        ext_extra_link_args = extra_link_args[:]
        ext_extra_compile_args = []

        if c_options['use_ios']:
            isysroot = ['-isysroot', '/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk']
            ext_include_dirs += ['/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/']
            ext_extra_compile_args += isysroot
{'fraction_non_alphanumeric': 0.1199347927340475, 'fraction_numerical': 0.006462505822077317, 'mean_word_length': 2.6269003378378377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7668524', 'n_tokens_mistral': 5656, 'n_tokens_neox': 5457, 'n_words': 1431}