diff --git a/setup.py b/setup.py index e952e8c..9e24942 100644 --- a/setup.py +++ b/setup.py @@ -12,75 +12,15 @@ from setuptools.extension import Extension SOURCE_ROOT = Path(__file__).resolve().parent +FRIDA_EXTENSION = os.environ.get("FRIDA_EXTENSION", None) -def detect_version() -> str: - pkg_info = SOURCE_ROOT / "PKG-INFO" - in_source_package = pkg_info.exists() - if in_source_package: - version_line = [line for line in pkg_info.read_text(encoding="utf-8").split("\n") - if line.startswith("Version: ")][0].strip() - version = version_line[9:] - else: - releng_location = next(enumerate_releng_locations(), None) - if releng_location is not None: - sys.path.insert(0, str(releng_location.parent)) - from releng.frida_version import detect - version = detect(SOURCE_ROOT).name.replace("-dev.", ".dev") - else: - version = "0.0.0" - return version - - -def enumerate_releng_locations() -> Iterator[Path]: - val = os.environ.get("MESON_SOURCE_ROOT") - if val is not None: - parent_releng = Path(val) / "releng" - if releng_location_exists(parent_releng): - yield parent_releng - - local_releng = SOURCE_ROOT / "releng" - if releng_location_exists(local_releng): - yield local_releng - - -def releng_location_exists(location: Path) -> bool: - return (location / "frida_version.py").exists() - - -class FridaPrebuiltExt(build_ext): - def build_extension(self, ext): - target = self.get_ext_fullpath(ext.name) - Path(target).parent.mkdir(parents=True, exist_ok=True) - shutil.copy(frida_extension, target) - - -class FridaDemandBuiltExt(build_ext): - def build_extension(self, ext): - make = SOURCE_ROOT / "make.bat" if platform.system() == "Windows" else "make" - subprocess.run([make], check=True) - - outputs = [entry for entry in (SOURCE_ROOT / "build" / "src").glob("_frida.*") if entry.is_file()] - assert len(outputs) == 1 - target = self.get_ext_fullpath(ext.name) - Path(target).parent.mkdir(parents=True, exist_ok=True) - shutil.copy(outputs[0], target) - - -frida_version = detect_version() -long_description = (SOURCE_ROOT / "README.md").read_text(encoding="utf-8") -frida_extension = os.environ.get("FRIDA_EXTENSION", None) - -cmdclass = {} -cmdclass["build_ext"] = FridaPrebuiltExt if frida_extension is not None else FridaDemandBuiltExt - - -if __name__ == "__main__": +def main(): setup( name="frida", - version=frida_version, + version=detect_version(), description="Dynamic instrumentation toolkit for developers, reverse-engineers, and security researchers", - long_description=long_description, + long_description=compute_long_description(), long_description_content_type="text/markdown", author="Frida Developers", author_email="oleavr@frida.re", @@ -120,6 +60,67 @@ if __name__ == "__main__": py_limited_api=True, ) ], - cmdclass=cmdclass, + cmdclass={"build_ext": FridaPrebuiltExt if FRIDA_EXTENSION is not None else FridaDemandBuiltExt}, zip_safe=False, ) + + +def detect_version() -> str: + pkg_info = SOURCE_ROOT / "PKG-INFO" + in_source_package = pkg_info.exists() + if in_source_package: + version_line = [line for line in pkg_info.read_text(encoding="utf-8").split("\n") + if line.startswith("Version: ")][0].strip() + version = version_line[9:] + else: + releng_location = next(enumerate_releng_locations(), None) + if releng_location is not None: + sys.path.insert(0, str(releng_location.parent)) + from releng.frida_version import detect + version = detect(SOURCE_ROOT).name.replace("-dev.", ".dev") + else: + version = "0.0.0" + return version + + +def compute_long_description() -> str: + return (SOURCE_ROOT / "README.md").read_text(encoding="utf-8") + + +def enumerate_releng_locations() -> Iterator[Path]: + val = os.environ.get("MESON_SOURCE_ROOT") + if val is not None: + parent_releng = Path(val) / "releng" + if releng_location_exists(parent_releng): + yield parent_releng + + local_releng = SOURCE_ROOT / "releng" + if releng_location_exists(local_releng): + yield local_releng + + +def releng_location_exists(location: Path) -> bool: + return (location / "frida_version.py").exists() + + +class FridaPrebuiltExt(build_ext): + def build_extension(self, ext): + target = self.get_ext_fullpath(ext.name) + Path(target).parent.mkdir(parents=True, exist_ok=True) + shutil.copy(FRIDA_EXTENSION, target) + + +class FridaDemandBuiltExt(build_ext): + def build_extension(self, ext): + make = SOURCE_ROOT / "make.bat" if platform.system() == "Windows" else "make" + subprocess.run([make], check=True) + + outputs = [entry for entry in (SOURCE_ROOT / "build" / "src").glob("_frida.*") if entry.is_file()] + assert len(outputs) == 1 + target = self.get_ext_fullpath(ext.name) + Path(target).parent.mkdir(parents=True, exist_ok=True) + shutil.copy(outputs[0], target) + + +if __name__ == "__main__": + main()