11import os
2+ import platform
23import shutil
34
45from setuptools import setup
@@ -22,6 +23,65 @@ def build_extension(self, ext):
2223 shutil .copyfile (frida_extension , target )
2324
2425
26+ class FridaMissingDevkitBuildExt (build_ext ):
27+ def build_extension (self , ext ):
28+ raise RuntimeError (
29+ "Need frida-core devkit to build from source.\n "
30+ "Download one from https://github.com/frida/frida/releases, "
31+ "extract it to a directory,\n "
32+ "and then add an environment variable named FRIDA_CORE_DEVKIT "
33+ "pointing at the directory."
34+ )
35+
36+
37+ include_dirs = []
38+ library_dirs = []
39+ libraries = []
40+ extra_link_args = []
41+
42+ cmdclass = {}
43+ if frida_extension is not None :
44+ cmdclass ["build_ext" ] = FridaPrebuiltExt
45+ else :
46+ devkit_dir = os .environ .get ("FRIDA_CORE_DEVKIT" , None )
47+ if devkit_dir is not None :
48+ include_dirs += [devkit_dir ]
49+ library_dirs += [devkit_dir ]
50+ libraries += ["frida-core" ]
51+
52+ system = platform .system ()
53+ if system == "Windows" :
54+ pass
55+ elif system == "Darwin" :
56+ extra_link_args += [
57+ "-Wl,-exported_symbol,_PyInit__frida" ,
58+ "-Wl,-dead_strip" ,
59+ ]
60+ else :
61+ version_script = os .path .join (package_dir , "src" , "_frida.version" )
62+ if not os .path .exists (version_script ):
63+ with open (version_script , "w" , encoding = "utf-8" ) as f :
64+ f .write (
65+ "\n " .join (
66+ [
67+ "{" ,
68+ " global:" ,
69+ " PyInit__frida;" ,
70+ "" ,
71+ " local:" ,
72+ " *;" ,
73+ "};" ,
74+ ]
75+ )
76+ )
77+ extra_link_args += [
78+ f"-Wl,--version-script,{ version_script } " ,
79+ "-Wl,--gc-sections" ,
80+ ]
81+ else :
82+ cmdclass ["build_ext" ] = FridaMissingDevkitBuildExt
83+
84+
2585if __name__ == "__main__" :
2686 setup (
2787 name = "frida" ,
@@ -60,7 +120,17 @@ def build_extension(self, ext):
60120 ],
61121 packages = ["frida" , "_frida" ],
62122 package_data = {"frida" : ["py.typed" ], "_frida" : ["py.typed" , "__init__.pyi" ]},
63- ext_modules = [Extension ("_frida" , [], py_limited_api = True )],
64- cmdclass = {"build_ext" : FridaPrebuiltExt },
123+ ext_modules = [
124+ Extension (
125+ name = "_frida" ,
126+ sources = ["src/_frida.c" ],
127+ include_dirs = include_dirs ,
128+ library_dirs = library_dirs ,
129+ libraries = libraries ,
130+ extra_link_args = extra_link_args ,
131+ py_limited_api = True ,
132+ )
133+ ],
134+ cmdclass = cmdclass ,
65135 zip_safe = False ,
66136 )
0 commit comments