@@ -58,9 +58,9 @@ def create_ffibuilder(**kwargs):
58
58
59
59
return ffibuilder
60
60
61
- def find_ffibuilder ():
61
+ def find_rootdir ():
62
62
# Figure out where the libpathrs source dir is.
63
- ROOT_DIR = None
63
+ root_dir = None
64
64
candidate = os .path .dirname (sys .path [0 ] or os .getcwd ())
65
65
while candidate != "/" :
66
66
try :
@@ -69,36 +69,65 @@ def find_ffibuilder():
69
69
with open (candidate_toml , "r" ) as f :
70
70
content = f .read ()
71
71
if re .findall (r'^name = "pathrs"$' , content , re .MULTILINE ):
72
- ROOT_DIR = candidate
72
+ root_dir = candidate
73
73
break
74
74
except :
75
75
pass
76
76
candidate = os .path .dirname (candidate )
77
77
78
- # TODO: Support using the system paths.
79
- if not ROOT_DIR :
80
- raise RuntimeError ("Could not find pathrs source-dir root." )
78
+ if not root_dir :
79
+ raise FileNotFoundError ("Could not find pathrs source-dir root." )
80
+
81
+ return root_dir
82
+
83
+ def srcdir_ffibuilder (root_dir = None ):
84
+ """
85
+ Build the CFFI bindings using the provided root_dir as the root of a
86
+ pathrs source tree which has compiled cdylibs ready in target/*.
87
+ """
88
+
89
+ if root_dir is None :
90
+ root_dir = find_rootdir ()
81
91
82
92
# Figure out which libs are usable.
83
- lib_paths = []
84
- for mode in [ "debug" , "release" ]:
85
- so_path = os . path . join ( ROOT_DIR , "target/%s/libpathrs.so" % ( mode ,) )
86
- if os . path . exists ( so_path ):
87
- lib_paths . append (so_path )
88
- lib_paths = sorted (lib_paths , key = lambda path : - os .path .getmtime (path ))
89
- lib_paths = [os .path .dirname (path ) for path in lib_paths ]
93
+ library_dirs = (
94
+ os . path . join ( root_dir , "target/%s/libpathrs.so" % ( mode ,))
95
+ for mode in ( "debug" , "release" )
96
+ )
97
+ library_dirs = ( so_path for so_path in library_dirs if os . path . exists (so_path ) )
98
+ library_dirs = sorted (library_dirs , key = lambda path : - os .path .getmtime (path ))
99
+ library_dirs = [os .path .dirname (path ) for path in library_dirs ]
90
100
91
101
# Compile the libpathrs module.
92
- return create_ffibuilder (include_dirs = [os .path .join (ROOT_DIR , "include" )],
93
- library_dirs = lib_paths )
102
+ return create_ffibuilder (include_dirs = [os .path .join (root_dir , "include" )],
103
+ library_dirs = library_dirs )
94
104
95
- if __name__ == "__main__" :
96
- # Compile the cffi module if running outside of setuptools.
97
- ffibuilder = find_ffibuilder ()
98
- ffibuilder .compile (verbose = True )
99
- else :
100
- # Use the system libraries if running inside setuptools.
101
- ffibuilder = create_ffibuilder (include_dirs = [
105
+ def system_ffibuilder ():
106
+ """
107
+ Build the CFFI bindings using the installed libpathrs system libraries.
108
+ """
109
+
110
+ return create_ffibuilder (include_dirs = [
102
111
"/usr/include" ,
103
112
"/usr/local/include"
104
113
])
114
+
115
+ if __name__ == "__main__" :
116
+ try :
117
+ # Search for the compiled libraries to link to from our libpathrs
118
+ # source if running outside of setuptools as a regular program.
119
+ ffibuilder = find_ffibuilder ()
120
+ except FileNotFoundError :
121
+ # If we couldn't find a valid library in the source dir, just fallback
122
+ # to using the system libraries.
123
+ ffibuilder = system_ffibuilder ()
124
+ ffibuilder .compile (verbose = True )
125
+ elif os .environ .get ("PATHRS_SRC_ROOT" , "" ) != "" :
126
+ # If we're running in setup tools, we can't easily find the source dir.
127
+ # However, distributions can set PATHRS_SRC_ROOT to the path of the
128
+ # libpathrs source directory to make it easier to build the python modules
129
+ # in the same %build script as the main library.
130
+ ffibuilder = srcdir_ffibuilder (root_dir = os .environ .get ("PATHRS_SRC_ROOT" ))
131
+ else :
132
+ # Use the system libraries if running inside standard setuptools.
133
+ ffibuilder = system_ffibuilder ()
0 commit comments