Skip to content

Commit

Permalink
Add '/GF' and '/Gy' flags to win32 builds only when the compiler supp…
Browse files Browse the repository at this point in the history
…orts them (i.e. is msvc)
  • Loading branch information
MyreMylar committed May 19, 2020
1 parent 16a8d3e commit 4d14f5e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions setup.py
Expand Up @@ -557,6 +557,31 @@ def dependencies(roots):
else:
pygame_data_files.append(f)

def has_flag(compiler, flagname):
"""
Adapted from here: https://github.com/pybind/python_example/blob/master/setup.py#L37
"""
from distutils.errors import CompileError
import tempfile
import os
with tempfile.NamedTemporaryFile('w', suffix='.cpp', delete=False) as f:
f.write('int main (int argc, char **argv) { return 0; }')
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
pass
return True

# filter flags, returns list of accepted flags
def flag_filter(compiler, *flags):
return [flag for flag in flags if has_flag(compiler, flag)]

class WinBuildExt(build_ext):
"""This build_ext sets necessary environment variables for MinGW"""

Expand All @@ -568,6 +593,14 @@ class WinBuildExt(build_ext):
__sdl_lib_dir = e.library_dirs[0].replace('/', os.sep)
break

def build_extensions(self):
# Add supported optimisations flags to reduce code size with MSVC
opts = flag_filter(self.compiler, "/GF", "/Gy")
for extension in extensions:
extension.extra_compile_args += opts

build_ext.build_extensions(self)

cmdclass['build_ext'] = WinBuildExt

# Add the precompiled smooth scale MMX functions to transform.
Expand Down

0 comments on commit 4d14f5e

Please sign in to comment.