-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
195 lines (177 loc) · 7.45 KB
/
setup.py
File metadata and controls
195 lines (177 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from setuptools import setup, Extension, find_packages
from Cython.Build import cythonize
import numpy as np
import platform
import os
import glob
import shutil
import subprocess
from distutils.command.build_ext import build_ext
# ===== DETECCIÓN INTELIGENTE DE CPU =====
def is_modern_cpu():
"""
Detecta si la CPU soporta instrucciones AVX2 (moderno)
Retorna True para: Intel Haswell (4th gen)+, AMD Ryzen+
Retorna False para: CPUs más antiguas
"""
try:
cpu_name = ""
if platform.system() == "Windows":
# Windows: WMIC
output = subprocess.check_output("wmic cpu get name", shell=True).decode()
cpu_name = output.lower().split('\n')[1].strip()
elif platform.system() == "Linux":
# Linux: /proc/cpuinfo
with open("/proc/cpuinfo", "r") as f:
for line in f:
if line.startswith("model name"):
cpu_name = line.lower().split(":")[1].strip()
break
elif platform.system() == "Darwin":
# macOS: sysctl
output = subprocess.check_output(["sysctl", "-n", "machdep.cpu.brand_string"]).decode()
cpu_name = output.lower().strip()
# Lista de CPUs modernas (con AVX2)
modern_keywords = [
"haswell", "broadwell", "skylake", "kabylake", "coffeelake",
"comet lake", "ice lake", "tiger lake", "alder lake", "raptor lake",
"ryzen", "zen", "epyc", "threadripper"
]
# Verificar si es moderno
is_modern = any(keyword in cpu_name for keyword in modern_keywords)
# Fallback: Intentar detectar AVX2 via CPUID (más preciso)
if not is_modern and platform.system() == "Windows":
try:
import cpuinfo
flags = cpuinfo.get_cpu_info().get('flags', [])
is_modern = 'avx2' in flags
except:
pass
print(f"🔍 CPU detectada: {cpu_name}")
print(f" {'✅ Moderna (AVX2 habilitado)' if is_modern else '⚠️ Antigua (modo de compatibilidad)'}")
return is_modern
except Exception as e:
print(f"⚠️ No se pudo detectar CPU: {e}")
print(" Usando modo de compatibilidad por seguridad")
return False
IS_WINDOWS = platform.system() == "Windows"
IS_MODERN_CPU = is_modern_cpu()
# ===== FLAGS SEGÚN CPU =====
if IS_WINDOWS:
# Windows: MSVC
if IS_MODERN_CPU:
# 🔥 MÁXIMO RENDIMIENTO
COMPILER_FLAGS = [
"/O2", # Máxima velocidad
"/fp:fast", # Fast math
"/arch:AVX2", # SIMD AVX2 (crítico para PPU/APU)
"/GL", # Whole Program Optimization
"/Oi", # Funciones intrínsecas
"/Oy", # Omitir frame pointers
"/W3", # Warning level 3
"/wd4244", # Silenciar conversiones seguras
]
LINK_FLAGS = [
"/LTCG", # Link Time Code Generation
"/OPT:REF", # Eliminar código muerto
"/OPT:ICF", # Fusionar funciones idénticas
]
else:
# ⚠️ COMPATIBILIDAD (CPU antigua, sin AVX2)
COMPILER_FLAGS = [
"/O2", # Máxima velocidad
"/fp:fast", # Fast math (seguro en CPUs antiguas)
"/W3", # Warning level 3
"/wd4244", # Silenciar conversiones
]
LINK_FLAGS = [] # Sin LTCG para compatibilidad
else:
# Linux/macOS: GCC/Clang
if IS_MODERN_CPU:
# 🔥 MÁXIMO RENDIMIENTO
COMPILER_FLAGS = [
"-O3", # Máxima optimización
"-ffast-math", # Fast math
"-march=native", # Optimizar para CPU EXACTA
"-mtune=native", # Tune para CPU EXACTA
"-flto", # Link Time Optimization
"-funroll-loops", # Unroll loops
"-fomit-frame-pointer", # Reducir overhead
]
LINK_FLAGS = ["-flto", "-O3"]
else:
# ⚠️ COMPATIBILIDAD
COMPILER_FLAGS = [
"-O2", # Optimización segura
"-ffast-math", # Fast math
"-march=x86-64", # x64 genérico (sin instrucciones modernas)
"-mtune=generic", # Tune genérico
]
LINK_FLAGS = ["-O2"]
# ===== LIMPIEZA POST-BUILD =====
KEEP_CYTHON_FILES = os.environ.get('KEEP_CYTHON_FILES', '').lower() in ('1', 'true', 'yes')
class PostBuildClean(build_ext):
def run(self):
super().run()
if KEEP_CYTHON_FILES:
return
print("\n🧹 Limpiando archivos intermedios...")
cleaned = 0
for pattern in ["pydmg/*.c", "pydmg/*.cpp", "pydmg/*.html"]:
for filepath in glob.glob(pattern):
try:
os.remove(filepath)
cleaned += 1
except:
pass
if os.path.exists("build"):
shutil.rmtree("build", ignore_errors=True)
cleaned += 1
print(f" ✅ {cleaned} elementos eliminados")
# ===== EXTENSIONES =====
extensions = [
Extension("pydmg.cpu", ["pydmg/cpu.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
Extension("pydmg.ppu", ["pydmg/ppu.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
Extension("pydmg.timer", ["pydmg/timer.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
Extension("pydmg.apu", ["pydmg/apu.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
Extension("pydmg.mmu", ["pydmg/mmu.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
Extension("pydmg.gameboy", ["pydmg/gameboy.pyx"], include_dirs=[np.get_include()],
extra_compile_args=COMPILER_FLAGS, extra_link_args=LINK_FLAGS,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]),
]
# ===== SETUP FINAL =====
setup(
name="pydmg",
version="1.0.0",
description="Game Boy Emulator Core (Auto-optimizado)",
packages=find_packages(),
ext_modules=cythonize(
extensions,
compiler_directives={
'language_level': 3,
'boundscheck': False,
'wraparound': False,
'cdivision': True,
'initializedcheck': False,
'profile': False,
'always_allow_keywords': False, # 🔥 +3% rendimiento
},
annotate=False, # Desactivar para builds release
),
cmdclass={'build_ext': PostBuildClean},
install_requires=[
"numpy>=1.19.0",
"PySDL2>=0.9.14",
],
python_requires=">=3.7",
)