comparison setup.py @ 619:39874a722b76

Use two passes for the module search, and clean it up so that pure Python modules don’t get compiled by default.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 Mar 2015 21:26:00 +0200
parents 725bd24235a2
children 398860bc3b7b
comparison
equal deleted inserted replaced
618:0ea591b0b29e 619:39874a722b76
17 17
18 COMMAND = 'pkg-config' 18 COMMAND = 'pkg-config'
19 SDL_LIBRARIES = ['sdl2', 'SDL2_image', 'SDL2_mixer', 'SDL2_ttf'] 19 SDL_LIBRARIES = ['sdl2', 'SDL2_image', 'SDL2_mixer', 'SDL2_ttf']
20 GL_LIBRARIES = ['epoxy'] 20 GL_LIBRARIES = ['epoxy']
21 21
22 packages = []
23 extension_names = []
24 extensions = []
25
26 debug = False # True to generate HTML annotations and display infered types. 22 debug = False # True to generate HTML annotations and display infered types.
27 anmviewer = False # It’s currently broken anyway. 23 anmviewer = False # It’s currently broken anyway.
28 nthreads = 4 # How many processes to use for Cython compilation. 24 nthreads = 4 # How many processes to use for Cython compilation.
25 compile_everything = False # Maybe improve running time a bit by wasting a lot
26 # of CPU time during compilation, and disk space.
29 27
30 28
31 # Hack to move us to the correct build directory. 29 # Hack to move us to the correct build directory.
32 os.chdir(os.path.join(os.getcwd(), os.path.dirname(__file__))) 30 os.chdir(os.path.join(os.getcwd(), os.path.dirname(__file__)))
33 31
93 if use_opengl: 91 if use_opengl:
94 opengl_args = {'extra_compile_args': get_arguments('--cflags', GL_LIBRARIES + SDL_LIBRARIES), 92 opengl_args = {'extra_compile_args': get_arguments('--cflags', GL_LIBRARIES + SDL_LIBRARIES),
95 'extra_link_args': get_arguments('--libs', GL_LIBRARIES + SDL_LIBRARIES)} 93 'extra_link_args': get_arguments('--libs', GL_LIBRARIES + SDL_LIBRARIES)}
96 94
97 95
98 for directory, _, files in os.walk('pytouhou'): 96 def get_module_hierarchy(directory):
99 if directory.endswith('/__pycache__'): 97 packages = {}
100 continue 98 allowed_extensions = ('.py', '.pyx', '.pxd')
101 package = directory.replace(os.path.sep, '.') 99 pycache = os.path.sep + '__pycache__'
102 if not use_opengl and package in ('pytouhou.ui.opengl', 'pytouhou.ui.opengl.shaders'): 100 for directory, _, files in os.walk(directory):
103 continue 101 if directory.endswith(pycache):
104 packages.append(package) 102 continue
105 if package not in ('pytouhou.formats', 'pytouhou.game', 'pytouhou.lib', 103 package_name = directory.replace(os.path.sep, '.')
106 'pytouhou.utils', 'pytouhou.ui', 'pytouhou.ui.opengl', 104 package = packages.setdefault(package_name, {})
107 'pytouhou.ui.opengl.shaders', 'pytouhou.ui.sdl'): 105 for filename in files:
108 continue 106 module_name, file_ext = os.path.splitext(filename)
109 if package in ('pytouhou.ui', 'pytouhou.ui.sdl'): 107 if file_ext not in allowed_extensions:
110 package_args = sdl_args
111 elif package == 'pytouhou.ui.opengl':
112 package_args = opengl_args
113 else:
114 package_args = {}
115 for filename in files:
116 if (filename.endswith('.pyx') or filename.endswith('.py') and
117 not filename == '__init__.py'):
118 extension_name = '%s.%s' % (package, os.path.splitext(filename)[0])
119 extension_names.append(extension_name)
120 if extension_name == 'pytouhou.lib.sdl':
121 compile_args = sdl_args
122 elif extension_name == 'pytouhou.ui.anmrenderer' and not anmviewer:
123 extension_names.pop()
124 continue 108 continue
125 elif package == 'pytouhou.formats' and extension_name != 'pytouhou.formats.animation': 109 package.setdefault(module_name, []).append(file_ext)
126 continue 110 for module_name, extensions in list(package.items()):
111 if '.pyx' not in extensions and '.py' not in extensions:
112 del package[module_name]
113 return packages
114
115
116 def extract_module_types(packages):
117 py_modules = []
118 ext_modules = []
119 for package_name, package in packages.items():
120 if package_name in ('pytouhou.ui', 'pytouhou.ui.sdl'):
121 package_args = sdl_args
122 elif package_name == 'pytouhou.ui.opengl':
123 package_args = opengl_args
124 else:
125 package_args = {}
126 for module_name, extensions in package.items():
127 fully_qualified_name = '%s.%s' % (package_name, module_name)
128 if '.pyx' in extensions or '.pxd' in extensions or compile_everything:
129 if fully_qualified_name == 'pytouhou.lib.sdl':
130 compile_args = sdl_args
131 else:
132 compile_args = package_args
133 ext = 'pyx' if '.pyx' in extensions else 'py'
134 source = '%s.%s' % (fully_qualified_name.replace('.', os.path.sep), ext)
135 ext_modules.append(Extension(fully_qualified_name,
136 [source],
137 **compile_args))
127 else: 138 else:
128 compile_args = package_args 139 py_modules.append(fully_qualified_name)
129 extensions.append(Extension(extension_name, 140 return py_modules, ext_modules
130 [os.path.join(directory, filename)], 141
131 **compile_args)) 142
143 packages = get_module_hierarchy('pytouhou')
144
145 if not use_opengl:
146 del packages['pytouhou.ui.opengl']
147 del packages['pytouhou.ui.opengl.shaders']
148
149 if not anmviewer:
150 del packages['pytouhou.ui']['anmrenderer']
151
152 py_modules, ext_modules = extract_module_types(packages)
132 153
133 154
134 # OS-specific setuptools options. 155 # OS-specific setuptools options.
135 try: 156 try:
136 from cx_Freeze import setup, Executable 157 from cx_Freeze import setup, Executable
154 version='0.1', 175 version='0.1',
155 author='Thibaut Girka', 176 author='Thibaut Girka',
156 author_email='thib@sitedethib.com', 177 author_email='thib@sitedethib.com',
157 url='http://pytouhou.linkmauve.fr/', 178 url='http://pytouhou.linkmauve.fr/',
158 license='GPLv3', 179 license='GPLv3',
159 packages=packages, 180 py_modules=py_modules,
160 ext_modules=cythonize(extensions, nthreads=nthreads, annotate=debug, 181 ext_modules=cythonize(ext_modules, nthreads=nthreads, annotate=debug,
161 language_level=3, 182 language_level=3,
162 compiler_directives={'infer_types': True, 183 compiler_directives={'infer_types': True,
163 'infer_types.verbose': debug, 184 'infer_types.verbose': debug,
164 'profile': debug}, 185 'profile': debug},
165 compile_time_env={'MAX_TEXTURES': 128, 186 compile_time_env={'MAX_TEXTURES': 128,