Test for existence of a function within a tuple of libraries. This is used as a smoke test to check whether a certain library is available. As a test, this creates a simple C program that calls the specified function, and then distutils is used to compile that progr
(self, funcname, libraries, library_dirs=None, verbose=False)
| 306 | return {'cxx': self.strip_empty_entries(self.cxx_args())} |
| 307 | |
| 308 | def has_function(self, funcname, libraries, library_dirs=None, verbose=False): |
| 309 | ''' |
| 310 | Test for existence of a function within a tuple of libraries. |
| 311 | |
| 312 | This is used as a smoke test to check whether a certain library is available. |
| 313 | As a test, this creates a simple C program that calls the specified function, |
| 314 | and then distutils is used to compile that program and link it with the specified libraries. |
| 315 | Returns True if both the compile and link are successful, False otherwise. |
| 316 | ''' |
| 317 | # Imported here because distutils pulls in setuptools, which slows down import deepspeed. |
| 318 | import distutils.ccompiler |
| 319 | import distutils.log |
| 320 | import distutils.sysconfig |
| 321 | |
| 322 | tempdir = None # we create a temporary directory to hold various files |
| 323 | filestderr = None # handle to open file to which we redirect stderr |
| 324 | oldstderr = None # file descriptor for stderr |
| 325 | try: |
| 326 | # Echo compile and link commands that are used. |
| 327 | if verbose: |
| 328 | distutils.log.set_verbosity(1) |
| 329 | |
| 330 | # Create a compiler object. |
| 331 | compiler = distutils.ccompiler.new_compiler(verbose=verbose) |
| 332 | |
| 333 | # Configure compiler and linker to build according to Python install. |
| 334 | distutils.sysconfig.customize_compiler(compiler) |
| 335 | |
| 336 | # Create a temporary directory to hold test files. |
| 337 | tempdir = tempfile.mkdtemp() |
| 338 | |
| 339 | # Define a simple C program that calls the function in question |
| 340 | prog = "void %s(void); int main(int argc, char** argv) { %s(); return 0; }" % (funcname, funcname) |
| 341 | |
| 342 | # Write the test program to a file. |
| 343 | filename = os.path.join(tempdir, 'test.c') |
| 344 | with open(filename, 'w') as f: |
| 345 | f.write(prog) |
| 346 | |
| 347 | # Redirect stderr file descriptor to a file to silence compile/link warnings. |
| 348 | if not verbose: |
| 349 | filestderr = open(os.path.join(tempdir, 'stderr.txt'), 'w') |
| 350 | oldstderr = os.dup(sys.stderr.fileno()) |
| 351 | os.dup2(filestderr.fileno(), sys.stderr.fileno()) |
| 352 | |
| 353 | # Workaround for behavior in distutils.ccompiler.CCompiler.object_filenames() |
| 354 | # Otherwise, a local directory will be used instead of tempdir |
| 355 | drive, driveless_filename = os.path.splitdrive(filename) |
| 356 | root_dir = driveless_filename[0] if os.path.isabs(driveless_filename) else '' |
| 357 | output_dir = os.path.join(drive, root_dir) |
| 358 | |
| 359 | # Attempt to compile the C program into an object file. |
| 360 | cflags = shlex.split(os.environ.get('CFLAGS', "")) |
| 361 | objs = compiler.compile([filename], output_dir=output_dir, extra_preargs=self.strip_empty_entries(cflags)) |
| 362 | |
| 363 | # Attempt to link the object file into an executable. |
| 364 | # Be sure to tack on any libraries that have been specified. |
| 365 | ldflags = shlex.split(os.environ.get('LDFLAGS', "")) |
no test coverage detected