def test_optional_extension(self):

        # this extension will fail, but let's ignore this failure
        # with the optional argument.
        modules = [Extension("foo", ["xxx"], optional=False)]
        dist = Distribution({"name": "xx", "ext_modules": modules})
        cmd = build_ext(dist)
        cmd.ensure_finalized()
        self.assertRaises((UnknownFileError, CompileError), cmd.run)  # should raise an error

        modules = [Extension("foo", ["xxx"], optional=True)]
        dist = Distribution({"name": "xx", "ext_modules": modules})
        cmd = build_ext(dist)
        cmd.ensure_finalized()
        cmd.run()  # should pass
Esempio n. 2
0
 def test_ext_fullpath(self):
     ext = sysconfig.get_config_vars()['SO']
     dist = Distribution()
     cmd = build_ext(dist)
     cmd.inplace = 1
     cmd.distribution.package_dir = {'': 'src'}
     cmd.distribution.packages = ['lxml', 'lxml.html']
     curdir = os.getcwd()
     wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
     path = cmd.get_ext_fullpath('lxml.etree')
     self.assertEqual(wanted, path)
     cmd.inplace = 0
     cmd.build_lib = os.path.join(curdir, 'tmpdir')
     wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext)
     path = cmd.get_ext_fullpath('lxml.etree')
     self.assertEqual(wanted, path)
     build_py = cmd.get_finalized_command('build_py')
     build_py.package_dir = {}
     cmd.distribution.packages = ['twisted', 'twisted.runner.portmap']
     path = cmd.get_ext_fullpath('twisted.runner.portmap')
     wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner', 'portmap' + ext)
     self.assertEqual(wanted, path)
     cmd.inplace = 1
     path = cmd.get_ext_fullpath('twisted.runner.portmap')
     wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
     self.assertEqual(wanted, path)
Esempio n. 3
0
 def __init__(self):
     self.debug = False
     self._compiler = new_compiler()
     customize_compiler(self._compiler)
     self._build_ext = build_ext(Distribution())
     self._build_ext.finalize_options()
     self._py_lib_dirs = self._build_ext.library_dirs
Esempio n. 4
0
    def test_user_site(self):
        import site
        dist = Distribution({'name': 'xx'})
        cmd = build_ext(dist)

        # making sure the user option is there
        options = [name for name, short, lable in
                   cmd.user_options]
        self.assertIn('user', options)

        # setting a value
        cmd.user = 1

        # setting user based lib and include
        lib = os.path.join(site.USER_BASE, 'lib')
        incl = os.path.join(site.USER_BASE, 'include')
        os.mkdir(lib)
        os.mkdir(incl)

        # let's run finalize
        cmd.ensure_finalized()

        # see if include_dirs and library_dirs
        # were set
        self.assertIn(lib, cmd.library_dirs)
        self.assertIn(lib, cmd.rpath)
        self.assertIn(incl, cmd.include_dirs)
Esempio n. 5
0
    def test_record_extensions(self):
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(ext_modules=[Extension("xx", ["xxmodule.c"])])
        os.chdir(project_dir)
        support.copy_xxmodule_c(project_dir)

        buildextcmd = build_ext(dist)
        support.fixup_build_ext(buildextcmd)
        buildextcmd.ensure_finalized()

        cmd = install(dist)
        dist.command_obj["install"] = cmd
        dist.command_obj["build_ext"] = buildextcmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, "filelist")
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [_make_ext_name("xx"), "UNKNOWN-0.0.0-py%s.%s.egg-info" % sys.version_info[:2]]
        self.assertEqual(found, expected)
    def test_user_site(self):
        # site.USER_SITE was introduced in 2.6
        if sys.version < '2.6':
            return

        import site
        dist = Distribution({'name': 'xx'})
        cmd = build_ext(dist)

        # making sure the user option is there
        options = [name for name, short, lable in
                   cmd.user_options]
        self.assertTrue('user' in options)

        # setting a value
        cmd.user = 1

        # setting user based lib and include
        lib = os.path.join(site.USER_BASE, 'lib')
        incl = os.path.join(site.USER_BASE, 'include')
        os.mkdir(lib)
        os.mkdir(incl)

        # let's run finalize
        cmd.ensure_finalized()

        # see if include_dirs and library_dirs
        # were set
        self.assertTrue(lib in cmd.library_dirs)
        self.assertTrue(lib in cmd.rpath)
        self.assertTrue(incl in cmd.include_dirs)
Esempio n. 7
0
 def __init__(self, debug=False):
     self._compiler = new_compiler()
     log.set_threshold(log.DEBUG if debug else log.INFO)
     customize_compiler(self._compiler)
     self._build_ext = build_ext(Distribution())
     self._build_ext.finalize_options()
     self._py_lib_dirs = self._build_ext.library_dirs
Esempio n. 8
0
def main():
  """Command line utility to retrieve compilation options for python modules'
  """
  parser = argparse.ArgumentParser(
      description='Retrieves compilation options for python modules.')
  parser.add_argument('--libraries', help='Returns libraries',
                      action='store_true')
  parser.add_argument('--includes', help='Returns includes',
                      action='store_true')
  parser.add_argument('--library_dirs', help='Returns library_dirs',
                      action='store_true')
  opts = parser.parse_args()

  ext = Extension('Dummy', [])
  b = build_ext.build_ext(Distribution())
  b.initialize_options()
  b.finalize_options()
  result = []
  if opts.libraries:
    libraries = b.get_libraries(ext)
    if sys.platform == 'darwin':
      libraries.append('python%s' % sys.version[:3])
    result.extend(libraries)
  if opts.includes:
    result = result  + b.include_dirs
  if opts.library_dirs:
    if sys.platform == 'darwin':
      result.append('%s/lib' % sysconfig.get_config_vars('prefix')[0])

  for x in result:
    print x
Esempio n. 9
0
    def _try_compile_deployment_target(self, operator, target):
        orig_environ = os.environ
        os.environ = orig_environ.copy()
        self.addCleanup(setattr, os, 'environ', orig_environ)
        if target is None:
            if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
                del os.environ['MACOSX_DEPLOYMENT_TARGET']
        else:
            os.environ['MACOSX_DEPLOYMENT_TARGET'] = target
        deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')
        with open(deptarget_c, 'w') as fp:
            fp.write(textwrap.dedent('                #include <AvailabilityMacros.h>\n\n                int dummy;\n\n                #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED\n                #else\n                #error "Unexpected target"\n                #endif\n\n            ' % operator))
        target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
        target = tuple(map(int, target.split('.')))
        target = '%02d%01d0' % target
        deptarget_ext = Extension('deptarget', [deptarget_c], extra_compile_args=['-DTARGET=%s' % (target,)])
        dist = Distribution({'name': 'deptarget',
         'ext_modules': [deptarget_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir
        try:
            cmd.ensure_finalized()
            cmd.run()
        except CompileError:
            self.fail('Wrong deployment target during compilation')

        return
Esempio n. 10
0
 def test_compiler_option(self):
     dist = Distribution()
     cmd = build_ext(dist)
     cmd.compiler = 'unix'
     cmd.ensure_finalized()
     cmd.run()
     self.assertEqual(cmd.compiler, 'unix')
Esempio n. 11
0
    def test_ext_fullpath(self):
        ext = sysconfig.get_config_vars()["SO"]
        dist = Distribution()
        cmd = build_ext(dist)
        cmd.inplace = 1
        cmd.distribution.package_dir = {"": "src"}
        cmd.distribution.packages = ["lxml", "lxml.html"]
        curdir = os.getcwd()
        wanted = os.path.join(curdir, "src", "lxml", "etree" + ext)
        path = cmd.get_ext_fullpath("lxml.etree")
        self.assertEquals(wanted, path)

        # building lxml.etree not inplace
        cmd.inplace = 0
        cmd.build_lib = os.path.join(curdir, "tmpdir")
        wanted = os.path.join(curdir, "tmpdir", "lxml", "etree" + ext)
        path = cmd.get_ext_fullpath("lxml.etree")
        self.assertEquals(wanted, path)

        # building twisted.runner.portmap not inplace
        build_py = cmd.get_finalized_command("build_py")
        build_py.package_dir = {}
        cmd.distribution.packages = ["twisted", "twisted.runner.portmap"]
        path = cmd.get_ext_fullpath("twisted.runner.portmap")
        wanted = os.path.join(curdir, "tmpdir", "twisted", "runner", "portmap" + ext)
        self.assertEquals(wanted, path)

        # building twisted.runner.portmap inplace
        cmd.inplace = 1
        path = cmd.get_ext_fullpath("twisted.runner.portmap")
        wanted = os.path.join(curdir, "twisted", "runner", "portmap" + ext)
        self.assertEquals(wanted, path)
Esempio n. 12
0
    def test_record_extensions(self):
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(ext_modules=[
            Extension('xx', ['xxmodule.c'])])
        self.addCleanup(os.chdir, os.getcwd())
        os.chdir(project_dir)
        support.copy_xxmodule_c(project_dir)

        buildextcmd = build_ext(dist)
        support.fixup_build_ext(buildextcmd)
        buildextcmd.ensure_finalized()

        cmd = install(dist)
        dist.command_obj['install'] = cmd
        dist.command_obj['build_ext'] = buildextcmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'RECORD')
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [_make_ext_name('xx'),
                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
        self.assertEqual(found, expected)
Esempio n. 13
0
    def test_build_ext(self):
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        if os.name == "nt":
            # On Windows, we must build a debug version iff running
            # a debug build of Python
            cmd.debug = sys.executable.endswith("_d.exe")
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not test_support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            cmd.run()
        finally:
            sys.stdout = old_stdout

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assert_(hasattr(xx, attr))

        self.assertEquals(xx.foo(2, 5), 7)
        self.assertEquals(xx.foo(13,15), 28)
        self.assertEquals(xx.new().demo(), None)
        doc = 'This is a template module just for instruction.'
        self.assertEquals(xx.__doc__, doc)
        self.assert_(isinstance(xx.Null(), xx.Null))
        self.assert_(isinstance(xx.Str(), xx.Str))
Esempio n. 14
0
 def test_get_source_files(self):
     modules = [Extension('foo', ['xxx'])]
     dist = Distribution({'name': 'xx',
      'ext_modules': modules})
     cmd = build_ext(dist)
     cmd.ensure_finalized()
     self.assertEqual(cmd.get_source_files(), ['xxx'])
Esempio n. 15
0
 def test_check_extensions_list(self):
     dist = Distribution()
     cmd = build_ext(dist)
     cmd.finalize_options()
     self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, 'foo')
     exts = [('bar', 'foo', 'bar'), 'foo']
     self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
     exts = [('foo-bar', '')]
     self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
     exts = [('foo.bar', '')]
     self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
     exts = [('foo.bar', {'sources': [''],
        'libraries': 'foo',
        'some': 'bar'})]
     cmd.check_extensions_list(exts)
     ext = exts[0]
     self.assertIsInstance(ext, Extension)
     self.assertEqual(ext.libraries, 'foo')
     self.assertFalse(hasattr(ext, 'some'))
     exts = [('foo.bar', {'sources': [''],
        'libraries': 'foo',
        'some': 'bar',
        'macros': [('1', '2', '3'), 'foo']})]
     self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
     exts[0][1]['macros'] = [('1', '2'), ('3',)]
     cmd.check_extensions_list(exts)
     self.assertEqual(exts[0].undef_macros, ['3'])
     self.assertEqual(exts[0].define_macros, [('1', '2')])
Esempio n. 16
0
    def test_record_extensions(self):
        cmd = test_support.missing_compiler_executable()
        if cmd is not None:
            self.skipTest('The %r command is not found' % cmd)
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(ext_modules=[
            Extension('xx', ['xxmodule.c'])])
        os.chdir(project_dir)
        support.copy_xxmodule_c(project_dir)

        buildextcmd = build_ext(dist)
        support.fixup_build_ext(buildextcmd)
        buildextcmd.ensure_finalized()

        cmd = install(dist)
        dist.command_obj['install'] = cmd
        dist.command_obj['build_ext'] = buildextcmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [_make_ext_name('xx'),
                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
        self.assertEqual(found, expected)
Esempio n. 17
0
def prepare_sample_zip(file):
    """Create a zipfile which contains the `sample` package.

    On completion, the contents of `file` will be::

        sample/
        sample/one.py
        sample/__init__.<SO_EXT>
        sample/two.<SO_EXT>

    The extension modules are compiled in a temporary directory.
    """

    src = os.path.join(os.path.dirname(__file__), 'sample')

    zf = zipfile.ZipFile(file, mode='w')

    # Is there an easier way to make an empty directory in the zipfile???
    with tempfile.TemporaryDirectory() as td:
        zf.write(td, 'sample')

    zf.write(os.path.join(src, 'one.py'),
             os.path.join('sample', 'one.py'))

    with tempfile.TemporaryDirectory() as td:
        ## Build the extension modules.
        ## This is more or less the same as running::
        ##   python setup.py build_ext --force
        ## for the following `setup.py` script::
        #
        # from distutils.core import setup
        # from distutils.extension import Extension
        #
        # setup(
        #     packages = ['test_pydzipimport'],
        #     ext_modules = [
        #         Extension("sample.__init__", ["sample/__init__.c"]),
        #         Extension("sample.two", ["sample/two.c"]),
        #         ],
        #     )

        b = build_ext(Distribution())
        b.force = True
        b.finalize_options()
        b.extensions = [
            Extension('sample.__init__', [os.path.join(src, '__init__.c')]),
            Extension('sample.two', [os.path.join(src, 'two.c')]),
            ]
        b.build_temp = td
        b.build_lib = td
        b.run()

        zf.write(b.get_ext_fullpath('sample.__init__'),
                 os.path.join('sample', '__init__' + SO_EXT))
        zf.write(b.get_ext_fullpath('sample.two'),
                 os.path.join('sample', 'two' + SO_EXT))

    zf.close()
Esempio n. 18
0
    def _try_compile_deployment_target(self, operator, target):
        orig_environ = os.environ
        os.environ = orig_environ.copy()
        self.addCleanup(setattr, os, 'environ', orig_environ)

        if target is None:
            if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
                del os.environ['MACOSX_DEPLOYMENT_TARGET']
        else:
            os.environ['MACOSX_DEPLOYMENT_TARGET'] = target

        deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')

        with open(deptarget_c, 'w') as fp:
            fp.write(textwrap.dedent('''\
                #include <AvailabilityMacros.h>

                int dummy;

                #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED
                #else
                #error "Unexpected target"
                #endif

            ''' % operator))

        # get the deployment target that the interpreter was built with
        target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
        target = tuple(map(int, target.split('.')))
        target = '%02d%01d0' % target
        deptarget_ext = Extension(
            'deptarget',
            [deptarget_c],
            extra_compile_args=['-DTARGET=%s'%(target,)],
        )
        dist = Distribution({
            'name': 'deptarget',
            'ext_modules': [deptarget_ext]
        })
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        try:
            old_stdout = sys.stdout
            if not support.verbose:
                # silence compiler output
                sys.stdout = StringIO()
            try:
                cmd.ensure_finalized()
                cmd.run()
            finally:
                sys.stdout = old_stdout

        except CompileError:
            self.fail("Wrong deployment target during compilation")
Esempio n. 19
0
 def test_build_ext_path_cross_platform(self):
     dist = Distribution({'name': 'UpdateManager'})
     cmd = build_ext(dist)
     cmd.ensure_finalized()
     ext = sysconfig.get_config_var('SO')
     ext_name = 'UpdateManager/fdsend'
     ext_path = cmd.get_ext_fullpath(ext_name)
     wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
     self.assertEqual(ext_path, wanted)
Esempio n. 20
0
def _get_ext_libraries(compiler):
    binst = build_ext.build_ext(dist.Distribution())
    binst.compiler = compiler
    binst.initialize_options()
    binst.finalize_options()
    class _FakeExt(object):
        def __init__(self):
            self.libraries = []
    return binst.get_libraries(_FakeExt())
Esempio n. 21
0
    def _try_compile_deployment_target(self, operator, target):
        orig_environ = os.environ
        os.environ = orig_environ.copy()
        self.addCleanup(setattr, os, 'environ', orig_environ)

        if target is None:
            if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
                del os.environ['MACOSX_DEPLOYMENT_TARGET']
        else:
            os.environ['MACOSX_DEPLOYMENT_TARGET'] = target

        deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')

        with open(deptarget_c, 'w') as fp:
            fp.write(textwrap.dedent('''\
                #include <AvailabilityMacros.h>

                int dummy;

                #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED
                #else
                #error "Unexpected target"
                #endif

            ''' % operator))

        # get the deployment target that the interpreter was built with
        target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
        target = tuple(map(int, target.split('.')[0:2]))
        # format the target value as defined in the Apple
        # Availability Macros.  We can't use the macro names since
        # at least one value we test with will not exist yet.
        if target[1] < 10:
            # for 10.1 through 10.9.x -> "10n0"
            target = '%02d%01d0' % target
        else:
            # for 10.10 and beyond -> "10nn00"
            target = '%02d%02d00' % target
        deptarget_ext = Extension(
            'deptarget',
            [deptarget_c],
            extra_compile_args=['-DTARGET=%s'%(target,)],
        )
        dist = Distribution({
            'name': 'deptarget',
            'ext_modules': [deptarget_ext]
        })
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        try:
            cmd.ensure_finalized()
            cmd.run()
        except CompileError:
            self.fail("Wrong deployment target during compilation")
Esempio n. 22
0
 def test_build_ext_path_with_os_sep(self):
     dist = Distribution({'name': 'UpdateManager'})
     cmd = build_ext(dist)
     cmd.ensure_finalized()
     ext = sysconfig.get_config_var("SO")
     ext_name = os.path.join('UpdateManager', 'fdsend')
     ext_path = cmd.get_ext_fullpath(ext_name)
     wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
     self.assertEquals(ext_path, wanted)
Esempio n. 23
0
def _get_build_extension():
    dist = Distribution()
    # Ensure the build respects distutils configuration by parsing
    # the configuration files
    config_files = dist.find_config_files()
    dist.parse_config_files(config_files)
    build_extension = build_ext(dist)
    build_extension.finalize_options()
    return build_extension
Esempio n. 24
0
 def test_compiler_option(self):
     # cmd.compiler is an option and
     # should not be overriden by a compiler instance
     # when the command is run
     dist = Distribution()
     cmd = build_ext(dist)
     cmd.compiler = 'unix'
     cmd.ensure_finalized()
     cmd.run()
     self.assertEqual(cmd.compiler, 'unix')
Esempio n. 25
0
 def _get_build_extension(self):
     dist = Distribution()
     config_files = dist.find_config_files()
     try:
         config_files.remove('setup.cfg')
     except ValueError:
         pass
     dist.parse_config_files(config_files)
     build_extension = build_ext(dist)
     build_extension.finalize_options()
     return build_extension
Esempio n. 26
0
 def __init__(self):
     # Need to import it here since setuptools may monkeypatch it
     from distutils.dist import Distribution
     self._verbose = False
     self._compiler = new_compiler()
     customize_compiler(self._compiler)
     self._build_ext = build_ext(Distribution())
     self._build_ext.finalize_options()
     self._py_lib_dirs = self._build_ext.library_dirs
     self._py_include_dirs = self._build_ext.include_dirs
     self._math_info = np_misc.get_info('npymath')
Esempio n. 27
0
 def _get_build_extension(self):
     self._clear_distutils_mkpath_cache()
     dist = Distribution()
     config_files = dist.find_config_files()
     try:
         config_files.remove('setup.cfg')
     except ValueError:
         pass
     dist.parse_config_files(config_files)
     build_extension = build_ext(dist)
     build_extension.finalize_options()
     return build_extension
Esempio n. 28
0
def pre_build_check():
    """
    Try to verify build tools
    """
    if os.environ.get('CASS_DRIVER_NO_PRE_BUILD_CHECK'):
        return True

    try:
        from distutils.ccompiler import new_compiler
        from distutils.sysconfig import customize_compiler
        from distutils.dist import Distribution

        # base build_ext just to emulate compiler option setup
        be = build_ext(Distribution())
        be.initialize_options()
        be.finalize_options()

        # First, make sure we have a Python include directory
        have_python_include = any(os.path.isfile(os.path.join(p, 'Python.h')) for p in be.include_dirs)
        if not have_python_include:
            sys.stderr.write("Did not find 'Python.h' in %s.\n" % (be.include_dirs,))
            return False

        compiler = new_compiler(compiler=be.compiler)
        customize_compiler(compiler)

        try:
            # We must be able to initialize the compiler if it has that method
            if hasattr(compiler, "initialize"):
                compiler.initialize()
        except:
            return False

        executables = []
        if compiler.compiler_type in ('unix', 'cygwin'):
            executables = [compiler.executables[exe][0] for exe in ('compiler_so', 'linker_so')]
        elif compiler.compiler_type == 'nt':
            executables = [getattr(compiler, exe) for exe in ('cc', 'linker')]

        if executables:
            from distutils.spawn import find_executable
            for exe in executables:
                if not find_executable(exe):
                    sys.stderr.write("Failed to find %s for compiler type %s.\n" % (exe, compiler.compiler_type))
                    return False

    except Exception as exc:
        sys.stderr.write('%s\n' % str(exc))
        sys.stderr.write("Failed pre-build check. Attempting anyway.\n")

    # if we are unable to positively id the compiler type, or one of these assumptions fails,
    # just proceed as we would have without the check
    return True
Esempio n. 29
0
 def test_build_ext_path_cross_platform(self):
     if sys.platform != 'win32':
         return
     dist = Distribution({'name': 'UpdateManager'})
     cmd = build_ext(dist)
     cmd.ensure_finalized()
     ext = sysconfig.get_config_var("SO")
     # this needs to work even under win32
     ext_name = 'UpdateManager/fdsend'
     ext_path = cmd.get_ext_fullpath(ext_name)
     wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
     self.assertEqual(ext_path, wanted)
Esempio n. 30
0
    def test_get_outputs(self):
        tmp_dir = self.mkdtemp()
        c_file = os.path.join(tmp_dir, 'foo.c')
        self.write_file(c_file, 'void initfoo(void) {};\n')
        ext = Extension('foo', [c_file])
        dist = Distribution({'name': 'xx',
         'ext_modules': [ext]})
        cmd = build_ext(dist)
        support.fixup_build_ext(cmd)
        cmd.ensure_finalized()
        self.assertEqual(len(cmd.get_outputs()), 1)
        cmd.build_lib = os.path.join(self.tmp_dir, 'build')
        cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
        other_tmp_dir = os.path.realpath(self.mkdtemp())
        old_wd = os.getcwd()
        os.chdir(other_tmp_dir)
        try:
            cmd.inplace = 1
            cmd.run()
            so_file = cmd.get_outputs()[0]
        finally:
            os.chdir(old_wd)

        self.assertTrue(os.path.exists(so_file))
        self.assertEqual(os.path.splitext(so_file)[-1], sysconfig.get_config_var('SO'))
        so_dir = os.path.dirname(so_file)
        self.assertEqual(so_dir, other_tmp_dir)
        cmd.compiler = None
        cmd.inplace = 0
        cmd.run()
        so_file = cmd.get_outputs()[0]
        self.assertTrue(os.path.exists(so_file))
        self.assertEqual(os.path.splitext(so_file)[-1], sysconfig.get_config_var('SO'))
        so_dir = os.path.dirname(so_file)
        self.assertEqual(so_dir, cmd.build_lib)
        build_py = cmd.get_finalized_command('build_py')
        build_py.package_dir = {'': 'bar'}
        path = cmd.get_ext_fullpath('foo')
        path = os.path.split(path)[0]
        self.assertEqual(path, cmd.build_lib)
        cmd.inplace = 1
        other_tmp_dir = os.path.realpath(self.mkdtemp())
        old_wd = os.getcwd()
        os.chdir(other_tmp_dir)
        try:
            path = cmd.get_ext_fullpath('foo')
        finally:
            os.chdir(old_wd)

        path = os.path.split(path)[0]
        lastdir = os.path.split(path)[-1]
        self.assertEqual(lastdir, 'bar')
        return
Esempio n. 31
0
 def __init__(self):
     # Need to import it here since setuptools may monkeypatch it
     from distutils.dist import Distribution
     self._verbose = False
     self._cleanup()
     self._compiler = new_compiler()
     customize_compiler(self._compiler)
     self._build_ext = build_ext(Distribution())
     self._build_ext.finalize_options()
     self._py_lib_dirs = self._build_ext.library_dirs
     self._py_include_dirs = self._build_ext.include_dirs
     self._math_info = np_misc.get_info('npymath')
Esempio n. 32
0
    def test_get_outputs(self):
        tmp_dir = self.mkdtemp()
        c_file = os.path.join(tmp_dir, 'foo.c')
        self.write_file(c_file, 'void initfoo(void) {};\n')
        ext = Extension('foo', [c_file])
        dist = Distribution({'name': 'xx',
         'ext_modules': [ext]})
        cmd = build_ext(dist)
        support.fixup_build_ext(cmd)
        cmd.ensure_finalized()
        self.assertEqual(len(cmd.get_outputs()), 1)
        cmd.build_lib = os.path.join(self.tmp_dir, 'build')
        cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
        other_tmp_dir = os.path.realpath(self.mkdtemp())
        old_wd = os.getcwd()
        os.chdir(other_tmp_dir)
        try:
            cmd.inplace = 1
            cmd.run()
            so_file = cmd.get_outputs()[0]
        finally:
            os.chdir(old_wd)

        self.assertTrue(os.path.exists(so_file))
        self.assertEqual(os.path.splitext(so_file)[-1], sysconfig.get_config_var('SO'))
        so_dir = os.path.dirname(so_file)
        self.assertEqual(so_dir, other_tmp_dir)
        cmd.compiler = None
        cmd.inplace = 0
        cmd.run()
        so_file = cmd.get_outputs()[0]
        self.assertTrue(os.path.exists(so_file))
        self.assertEqual(os.path.splitext(so_file)[-1], sysconfig.get_config_var('SO'))
        so_dir = os.path.dirname(so_file)
        self.assertEqual(so_dir, cmd.build_lib)
        build_py = cmd.get_finalized_command('build_py')
        build_py.package_dir = {'': 'bar'}
        path = cmd.get_ext_fullpath('foo')
        path = os.path.split(path)[0]
        self.assertEqual(path, cmd.build_lib)
        cmd.inplace = 1
        other_tmp_dir = os.path.realpath(self.mkdtemp())
        old_wd = os.getcwd()
        os.chdir(other_tmp_dir)
        try:
            path = cmd.get_ext_fullpath('foo')
        finally:
            os.chdir(old_wd)

        path = os.path.split(path)[0]
        lastdir = os.path.split(path)[-1]
        self.assertEqual(lastdir, 'bar')
        return
Esempio n. 33
0
def pre_build_check():
    """
    Try to verify build tools
    """
    if os.environ.get('CASS_DRIVER_NO_PRE_BUILD_CHECK'):
        return True

    try:
        from distutils.ccompiler import new_compiler
        from distutils.sysconfig import customize_compiler
        from distutils.dist import Distribution

        # base build_ext just to emulate compiler option setup
        be = build_ext(Distribution())
        be.initialize_options()
        be.finalize_options()

        # First, make sure we have a Python include directory
        have_python_include = any(os.path.isfile(os.path.join(p, 'Python.h')) for p in be.include_dirs)
        if not have_python_include:
            sys.stderr.write("Did not find 'Python.h' in %s.\n" % (be.include_dirs,))
            return False

        compiler = new_compiler(compiler=be.compiler)
        customize_compiler(compiler)

        try:
            # We must be able to initialize the compiler if it has that method
            if hasattr(compiler, "initialize"):
                compiler.initialize()
        except:
            return False

        executables = []
        if compiler.compiler_type in ('unix', 'cygwin'):
            executables = [compiler.executables[exe][0] for exe in ('compiler_so', 'linker_so')]
        elif compiler.compiler_type == 'nt':
            executables = [getattr(compiler, exe) for exe in ('cc', 'linker')]

        if executables:
            from distutils.spawn import find_executable
            for exe in executables:
                if not find_executable(exe):
                    sys.stderr.write("Failed to find %s for compiler type %s.\n" % (exe, compiler.compiler_type))
                    return False

    except Exception as exc:
        sys.stderr.write('%s\n' % str(exc))
        sys.stderr.write("Failed pre-build check. Attempting anyway.\n")

    # if we are unable to positively id the compiler type, or one of these assumptions fails,
    # just proceed as we would have without the check
    return True
Esempio n. 34
0
 def _get_build_extension(self):
     self._clear_distutils_mkpath_cache()
     dist = Distribution()
     config_files = dist.find_config_files()
     try:
         config_files.remove("setup.cfg")
     except ValueError:
         pass
     dist.parse_config_files(config_files)
     build_extension = build_ext(dist)
     build_extension.finalize_options()
     return build_extension
Esempio n. 35
0
 def test_build_ext_path_cross_platform(self):
     if sys.platform != 'win32':
         return
     dist = Distribution({'name': 'UpdateManager'})
     cmd = build_ext(dist)
     cmd.ensure_finalized()
     ext = sysconfig.get_config_var("SO")
     # this needs to work even under win32
     ext_name = 'UpdateManager/fdsend'
     ext_path = cmd.get_ext_fullpath(ext_name)
     wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
     self.assertEquals(ext_path, wanted)
Esempio n. 36
0
def compile_c_extension(
    generated_source_path: str,
    build_dir: Optional[str] = None,
    verbose: bool = False,
    keep_asserts: bool = True,
) -> str:
    """Compile the generated source for a parser generator into an extension module.

    The extension module will be generated in the same directory as the provided path
    for the generated source, with the same basename (in addition to extension module
    metadata). For example, for the source mydir/parser.c the generated extension
    in a darwin system with python 3.8 will be mydir/parser.cpython-38-darwin.so.

    If *build_dir* is provided, that path will be used as the temporary build directory
    of distutils (this is useful in case you want to use a temporary directory).
    """
    if verbose:
        distutils.log.set_verbosity(distutils.log.DEBUG)

    source_file_path = pathlib.Path(generated_source_path)
    extension_name = source_file_path.stem
    extra_compile_args = []
    if keep_asserts:
        extra_compile_args.append("-UNDEBUG")
    extension = [
        Extension(
            extension_name,
            sources=[
                str(MOD_DIR.parent / "pegen.c"),
                str(MOD_DIR.parent / "parse_string.c"),
                generated_source_path,
            ],
            include_dirs=[str(MOD_DIR.parent)],
            extra_compile_args=extra_compile_args,
        )
    ]
    dist = Distribution({"name": extension_name, "ext_modules": extension})
    cmd = build_ext(dist)
    cmd.inplace = True
    if build_dir:
        cmd.build_temp = build_dir
    cmd.ensure_finalized()
    cmd.run()

    extension_path = source_file_path.parent / cmd.get_ext_filename(extension_name)
    shutil.move(cmd.get_ext_fullpath(extension_name), extension_path)

    cmd = clean(dist)
    cmd.finalize_options()
    cmd.run()

    return extension_path
Esempio n. 37
0
 def test_build_ext_inplace(self):
     etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
     etree_ext = Extension('lxml.etree', [etree_c])
     dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
     cmd = build_ext(dist)
     cmd.inplace = 1
     cmd.distribution.package_dir = {'': 'src'}
     cmd.distribution.packages = ['lxml', 'lxml.html']
     curdir = os.getcwd()
     ext = sysconfig.get_config_var("SO")
     wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
     path = cmd.get_ext_fullpath('lxml.etree')
     self.assertEquals(wanted, path)
Esempio n. 38
0
    def _try_compile_deployment_target(self, operator, target):
        orig_environ = os.environ
        os.environ = orig_environ.copy()
        self.addCleanup(setattr, os, 'environ', orig_environ)

        if target is None:
            if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
                del os.environ['MACOSX_DEPLOYMENT_TARGET']
        else:
            os.environ['MACOSX_DEPLOYMENT_TARGET'] = target

        deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')

        with open(deptarget_c, 'w') as fp:
            fp.write(
                textwrap.dedent('''\
                #include <AvailabilityMacros.h>

                int dummy;

                #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED
                #else
                #error "Unexpected target"
                #endif

            ''' % operator))

        # get the deployment target that the interpreter was built with
        target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
        target = tuple(map(int, target.split('.')))
        target = '%02d%01d0' % target
        deptarget_ext = Extension(
            'deptarget',
            [deptarget_c],
            extra_compile_args=['-DTARGET=%s' % (target, )],
        )
        dist = Distribution({
            'name': 'deptarget',
            'ext_modules': [deptarget_ext]
        })
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        try:
            cmd.ensure_finalized()
            cmd.run()
        except CompileError:
            self.fail("Wrong deployment target during compilation")
Esempio n. 39
0
def _get_build_extension():
    from distutils.command.build_ext import build_ext
    from distutils.core import Distribution

    # Modified from Cython/Build/Inline.py, Apache License Version 2.0
    dist = Distribution()
    # Ensure the build respects distutils configuration by parsing
    # the configuration files
    config_files = dist.find_config_files()
    dist.parse_config_files(config_files)
    build_extension = build_ext(dist)
    # build_extension.verbose = True
    build_extension.finalize_options()
    return build_extension
Esempio n. 40
0
def build():
    cython_sources = []
    cython_sources.extend(Path("aiotone").glob("*.pyx"))

    cpu_arch = platform.machine().lower()
    if "arm" in cpu_arch:
        compile_args = compile_args_arm
    elif "x86" in cpu_arch or "amd64" in cpu_arch:
        compile_args = compile_args_x86
    else:
        print(f"warning: unknown machine arch {cpu_arch}; assuming Intel")
        compile_args = compile_args_x86

    extensions = [
        Extension(
            "aiotone." + path.with_suffix("").name,
            [str(path)],
            extra_compile_args=compile_args,
            extra_link_args=link_args,
            include_dirs=include_dirs,
            libraries=libraries,
        ) for path in cython_sources
    ]
    ext_modules = cythonize(
        extensions,
        include_path=include_dirs,
        compiler_directives={
            "binding": True,
            "language_level": 3
        },
    )

    distribution = Distribution({
        "name": "extended",
        "ext_modules": ext_modules
    })
    distribution.package_dir = "extended"

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built extensions back to the project
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 41
0
 def test_user_site(self):
     import site
     dist = Distribution({'name': 'xx'})
     cmd = build_ext(dist)
     options = [ name for name, short, label in cmd.user_options ]
     self.assertIn('user', options)
     cmd.user = 1
     lib = os.path.join(site.USER_BASE, 'lib')
     incl = os.path.join(site.USER_BASE, 'include')
     os.mkdir(lib)
     os.mkdir(incl)
     cmd.ensure_finalized()
     self.assertIn(lib, cmd.library_dirs)
     self.assertIn(lib, cmd.rpath)
     self.assertIn(incl, cmd.include_dirs)
Esempio n. 42
0
def findswig(pathstring):
    list = []
    from distutils.command import build_ext
    from distutils.dist import Distribution
    d  =Distribution()
    a = build_ext.build_ext(d)
    yourswig = a.find_swig()
    
    for a in pathstring:
        if  os.path.exists('%s/%s' %(a,yourswig)):
            list.append('%s/%s' %(a,yourswig))  
             
    if list == []:
        print "YOU DON'T HAVE SWIG"
        sys.exit()
Esempio n. 43
0
def build(*args, **setup_kwargs):

    distribution = Distribution({"name": "extended", "ext_modules": ext_modules})

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built extensions back to the project
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)  # type: ignore
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 44
0
def main():
    distutils.log.set_verbosity(1)

    ext_modules = discover_extensions()
    dist = Distribution(dict(ext_modules=ext_modules))

    shutil.rmtree('build')

    cmd = build_ext(dist)
    cmd.finalize_options()
    cmd.run()

    for ext_module in ext_modules:
        shutil.copy(cmd.get_ext_fullpath(ext_module.name),
                    ext_module.origin_dir)
Esempio n. 45
0
def build() -> None:
    distribution = Distribution({"name": "extended", "ext_modules": extensions})
    distribution.package_dir = "extended"

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built extensions back to the project
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 46
0
    def test_check_extensions_list(self):
        dist = Distribution()
        cmd = build_ext(dist)
        cmd.finalize_options()

        #'extensions' option must be a list of Extension instances
        self.assertRaises(DistutilsSetupError,
                          cmd.check_extensions_list, 'foo')

        # each element of 'ext_modules' option must be an
        # Extension instance or 2-tuple
        exts = [('bar', 'foo', 'bar'), 'foo']
        self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)

        # first element of each tuple in 'ext_modules'
        # must be the extension name (a string) and match
        # a python dotted-separated name
        exts = [('foo-bar', '')]
        self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)

        # second element of each tuple in 'ext_modules'
        # must be a ary (build info)
        exts = [('foo.bar', '')]
        self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)

        # ok this one should pass
        exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
                             'some': 'bar'})]
        cmd.check_extensions_list(exts)
        ext = exts[0]
        self.assertTrue(isinstance(ext, Extension))

        # check_extensions_list adds in ext the values passed
        # when they are in ('include_dirs', 'library_dirs', 'libraries'
        # 'extra_objects', 'extra_compile_args', 'extra_link_args')
        self.assertEqual(ext.libraries, 'foo')
        self.assertTrue(not hasattr(ext, 'some'))

        # 'macros' element of build info dict must be 1- or 2-tuple
        exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
                'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})]
        self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)

        exts[0][1]['macros'] = [('1', '2'), ('3',)]
        cmd.check_extensions_list(exts)
        self.assertEqual(exts[0].undef_macros, ['3'])
        self.assertEqual(exts[0].define_macros, [('1', '2')])
Esempio n. 47
0
    def test_build_ext(self):
        global ALREADY_TESTED
        support.copy_xxmodule_c(self.tmp_dir)
        self.xx_created = True
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        support.fixup_build_ext(cmd)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not test_support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            #Broken after issue 7712(r78136) : add a temp_cwd context manager to test_support ...
            #Without current working dir: "...cannot find -lpython27"
            #NOTE: [py3k svn r85559] First (uncontroversial) part of issue 9807, barry.warsaw, 2010-10-16 :
            #  new _fixup_command is bogus, so we will use own work-around
            cmd.library_dirs.insert(0, test_support.SAVEDCWD)
            cmd.run()
        finally:
            sys.stdout = old_stdout

        if ALREADY_TESTED:
            return
        else:
            ALREADY_TESTED = True

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assertTrue(hasattr(xx, attr))

        self.assertEqual(xx.foo(2, 5), 7)
        self.assertEqual(xx.foo(13, 15), 28)
        self.assertEqual(xx.new().demo(), None)
        if test_support.HAVE_DOCSTRINGS:
            doc = 'This is a template module just for instruction.'
            self.assertEqual(xx.__doc__, doc)
        self.assertTrue(isinstance(xx.Null(), xx.Null))
        self.assertTrue(isinstance(xx.Str(), xx.Str))
Esempio n. 48
0
    def so_ext(self):
        """The extension suffix for compiled modules."""
        try:
            return self._so_ext
        except AttributeError:

            dist = Distribution()
            config_files = dist.find_config_files()
            try:
                config_files.remove('setup.cfg')
            except ValueError:
                pass
            dist.parse_config_files(config_files)
            build_extension = build_ext(dist)
            build_extension.finalize_options()
            self._so_ext = build_extension.get_ext_filename('')
            return self._so_ext
Esempio n. 49
0
def build():
    extensions = [
        Extension(
            "cassiopeia.preprocess.collapse_cython",
            ["cassiopeia/preprocess/collapse_cython.pyx"],
        ),
        Extension(
            "cassiopeia.solver.ilp_solver_utilities",
            ["cassiopeia/solver/ilp_solver_utilities.pyx"],
            include_dirs=[numpy.get_include()],
        ),
        Extension(
            "cassiopeia.tools.branch_length_estimator._iid_exponential_bayesian",
            sources=[
                "cassiopeia/tools/branch_length_estimator/_iid_exponential_bayesian.pyx",
                "cassiopeia/tools/branch_length_estimator/_iid_exponential_bayesian_cpp.cpp",
            ],
            extra_compile_args=[
                "-std=c++17",
                "-Wall",
                "-Wextra",
                "-pedantic",
                "-O3",
            ],
            language="c++",
        ),
    ]
    ext_modules = cythonize(
        extensions,
        compiler_directives={"language_level": 3},
    )

    distribution = Distribution({"name": "extended", "ext_modules": ext_modules})
    distribution.package_dir = "extended"

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built extensions back to the project
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 50
0
def build():
    extensions = [
        Extension('ffm2', ['fastfm2/core/ffm2.pyx'],
                  libraries=['fastfm', 'solvers'],
                  library_dirs=[ffm2_library_dir, ffm2_library_solvers_dir],
                  include_dirs=[
                      'fastfm2/core', ffm2_include_dir,
                      ffm2_solvers_include_dir,
                      numpy.get_include()
                  ],
                  extra_compile_args=['-std=c++11', '-Wall'],
                  extra_link_args=['-std=c++11', '-mstackrealign'],
                  language="c++")
    ]
    ext_modules = cythonize(
        extensions,
        compile_time_env=dict(EXTERNAL_RELEASE=True),
        compiler_directives={
            'binding': True,
            'language_level': 3
        },
    )

    distribution = Distribution({
        'name': 'fastfm2',
        'ext_modules': ext_modules,
        'package_data': {
            'fastfm2': ['fastfm2/core/*.pxd']
        },
        'version': get_version_from_pyproject(),
    })

    distribution.package_dir = 'fastfm2'

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built lib to project root
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 51
0
def main():
    """Command line utility to retrieve compilation options for python modules'
  """
    parser = argparse.ArgumentParser(
        description='Retrieves compilation options for python modules.')
    parser.add_argument('--gn',
                        help='Returns all values in a format suitable for gn',
                        action='store_true')
    parser.add_argument('--libraries',
                        help='Returns libraries',
                        action='store_true')
    parser.add_argument('--includes',
                        help='Returns includes',
                        action='store_true')
    parser.add_argument('--library_dirs',
                        help='Returns library_dirs',
                        action='store_true')
    opts = parser.parse_args()

    ext = Extension('Dummy', [])
    b = build_ext.build_ext(Distribution())
    b.initialize_options()
    b.finalize_options()
    result = []
    if opts.libraries:
        libraries = b.get_libraries(ext)
        if sys.platform == 'darwin':
            libraries.append('python%s' % sys.version[:3])
        if not opts.gn and sys.platform in ['darwin', 'linux2']:
            # In case of GYP output for darwin and linux prefix all
            # libraries (if there are any) so the result can be used as a
            # compiler argument. GN handles platform-appropriate prefixing itself.
            libraries = ['-l%s' % library for library in libraries]
        result.extend(libraries)
    if opts.includes:
        result = result + b.include_dirs
    if opts.library_dirs:
        if sys.platform == 'darwin':
            result.append('%s/lib' % sysconfig.get_config_vars('prefix')[0])

    if opts.gn:
        for x in result:
            print x
    else:
        print ''.join(['"%s"' % x for x in result])
Esempio n. 52
0
    def test_build_ext(self):
        global ALREADY_TESTED
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        if not os.path.exists(xx_c):
            return
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        self._fixup_command(cmd)
        if os.name == "nt":
            # On Windows, we must build a debug version iff running
            # a debug build of Python
            cmd.debug = sys.executable.endswith("_d.exe")
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            cmd.run()
        finally:
            sys.stdout = old_stdout

        if ALREADY_TESTED:
            return
        else:
            ALREADY_TESTED = True

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assertTrue(hasattr(xx, attr))

        self.assertEqual(xx.foo(2, 5), 7)
        self.assertEqual(xx.foo(13,15), 28)
        self.assertEqual(xx.new().demo(), None)
        doc = 'This is a template module just for instruction.'
        self.assertEqual(xx.__doc__, doc)
        self.assertTrue(isinstance(xx.Null(), xx.Null))
        self.assertTrue(isinstance(xx.Str(), xx.Str))
Esempio n. 53
0
    def test_solaris_enable_shared(self):
        dist = Distribution({'name': 'xx'})
        cmd = build_ext(dist)
        old = sys.platform
        sys.platform = 'sunos'
        from distutils.sysconfig import _config_vars
        old_var = _config_vars.get('Py_ENABLE_SHARED')
        _config_vars['Py_ENABLE_SHARED'] = 1
        try:
            cmd.ensure_finalized()
        finally:
            sys.platform = old
            if old_var is None:
                del _config_vars['Py_ENABLE_SHARED']
            else:
                _config_vars['Py_ENABLE_SHARED'] = old_var

        self.assertGreater(len(cmd.library_dirs), 0)
        return
Esempio n. 54
0
 def test_finalize_options(self):
     modules = [Extension('foo', ['xxx'])]
     dist = Distribution({'name': 'xx',
      'ext_modules': modules})
     cmd = build_ext(dist)
     cmd.finalize_options()
     py_include = sysconfig.get_python_inc()
     self.assertIn(py_include, cmd.include_dirs)
     plat_py_include = sysconfig.get_python_inc(plat_specific=1)
     self.assertIn(plat_py_include, cmd.include_dirs)
     cmd = build_ext(dist)
     cmd.libraries = 'my_lib, other_lib lastlib'
     cmd.finalize_options()
     self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib'])
     cmd = build_ext(dist)
     cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep
     cmd.finalize_options()
     self.assertIn('my_lib_dir', cmd.library_dirs)
     self.assertIn('other_lib_dir', cmd.library_dirs)
     cmd = build_ext(dist)
     cmd.rpath = 'one%stwo' % os.pathsep
     cmd.finalize_options()
     self.assertEqual(cmd.rpath, ['one', 'two'])
     cmd = build_ext(dist)
     cmd.define = 'one,two'
     cmd.finalize_options()
     self.assertEqual(cmd.define, [('one', '1'), ('two', '1')])
     cmd = build_ext(dist)
     cmd.undef = 'one,two'
     cmd.finalize_options()
     self.assertEqual(cmd.undef, ['one', 'two'])
     cmd = build_ext(dist)
     cmd.swig_opts = None
     cmd.finalize_options()
     self.assertEqual(cmd.swig_opts, [])
     cmd = build_ext(dist)
     cmd.swig_opts = '1 2'
     cmd.finalize_options()
     self.assertEqual(cmd.swig_opts, ['1', '2'])
     return
Esempio n. 55
0
def build():
    cython_sources = []
    cython_sources.extend(Path("aiotone").glob("*.pyx"))

    extensions = [
        Extension(
            "aiotone." + path.with_suffix("").name,
            [str(path)],
            extra_compile_args=compile_args,
            extra_link_args=link_args,
            include_dirs=include_dirs,
            libraries=libraries,
        ) for path in cython_sources
    ]
    ext_modules = cythonize(
        extensions,
        include_path=include_dirs,
        compiler_directives={
            "binding": True,
            "language_level": 3
        },
    )

    distribution = Distribution({
        "name": "extended",
        "ext_modules": ext_modules
    })
    distribution.package_dir = "extended"

    cmd = build_ext(distribution)
    cmd.ensure_finalized()
    cmd.run()

    # Copy built extensions back to the project
    for output in cmd.get_outputs():
        relative_extension = os.path.relpath(output, cmd.build_lib)
        shutil.copyfile(output, relative_extension)
        if relative_extension.startswith("aiotone/"):
            shutil.copyfile(output, relative_extension[len("aiotone/"):])
        mode = os.stat(relative_extension).st_mode
        mode |= (mode & 0o444) >> 2
        os.chmod(relative_extension, mode)
Esempio n. 56
0
    def test_solaris_enable_shared(self):
        dist = Distribution({'name': 'xx'})
        cmd = build_ext(dist)
        old = sys.platform

        sys.platform = 'sunos'  # fooling finalize_options
        from distutils.sysconfig import _config_vars
        old_var = _config_vars.get('Py_ENABLE_SHARED')
        _config_vars['Py_ENABLE_SHARED'] = 1
        try:
            cmd.ensure_finalized()
        finally:
            sys.platform = old
            if old_var is None:
                del _config_vars['Py_ENABLE_SHARED']
            else:
                _config_vars['Py_ENABLE_SHARED'] = old_var

        # make sure we get some library dirs under solaris
        self.assertTrue(len(cmd.library_dirs) > 0)
Esempio n. 57
0
def _get_build_extension(extension, lib_dir):
    dist = Distribution()
    config_files = dist.find_config_files()

    # TODO: Is this check really useful?
    try:
        config_files.remove('setup.cfg')
    except ValueError:
        pass

    dist.parse_config_files(config_files)

    build_extension_ = build_ext(dist)
    build_extension_.finalize_options()

    build_extension_.build_temp = lib_dir
    build_extension_.build_lib = lib_dir
    build_extension_.extensions = [extension]

    return build_extension_
Esempio n. 58
0
    def _try_compile_deployment_target(self):
        deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')

        with open(deptarget_c, 'w') as fp:
            fp.write(
                textwrap.dedent('''\
                #include <AvailabilityMacros.h>

                int dummy;

                #if TARGET != MAC_OS_X_VERSION_MIN_REQUIRED
                #error "Unexpected target"
               #endif

            '''))

        target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
        target = tuple(map(int, target.split('.')))
        target = '%02d%01d0' % target

        deptarget_ext = Extension(
            'deptarget',
            [deptarget_c],
            extra_compile_args=['-DTARGET=%s' % (target, )],
        )
        dist = Distribution({
            'name': 'deptarget',
            'ext_modules': [deptarget_ext]
        })
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        try:
            old_stdout = sys.stdout
            cmd.ensure_finalized()
            cmd.run()

        except CompileError:
            self.fail("Wrong deployment target during compilation")
Esempio n. 59
0
    def test_build_ext(self):
        global ALREADY_TESTED
        support.copy_xxmodule_c(self.tmp_dir)
        self.xx_created = True
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        support.fixup_build_ext(cmd)
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not test_support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            cmd.run()
        finally:
            sys.stdout = old_stdout

        if ALREADY_TESTED:
            self.skipTest('Already tested in %s' % ALREADY_TESTED)
        else:
            ALREADY_TESTED = type(self).__name__

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assertTrue(hasattr(xx, attr))

        self.assertEqual(xx.foo(2, 5), 7)
        self.assertEqual(xx.foo(13, 15), 28)
        self.assertEqual(xx.new().demo(), None)
        if test_support.HAVE_DOCSTRINGS:
            doc = 'This is a template module just for instruction.'
            self.assertEqual(xx.__doc__, doc)
        self.assertIsInstance(xx.Null(), xx.Null)
        self.assertIsInstance(xx.Str(), xx.Str)
Esempio n. 60
0
def build_test_extensions():
    """Because distutils sucks, it just copies the entire contents of the build
    results dir (e.g. build/lib.linux-i686-2.6) during installation. That means
    that we can't put any files there that we don't want to distribute. </3.

    To deal with that, this code will compile the test extension and place the
    object files in the normal temp directory using the same logic as distutils,
    but linked shared library will go directly into the tests directory.
    """
    build_temp_dir = os.path.join(
        'build', 'temp.%s-%s' % (util.get_platform(), sys.version[0:3]))
    compiler = new_compiler()
    distribution = dist.Distribution()
    build_ext_cmd = build_ext.build_ext(distribution)
    build_ext_cmd.finalize_options()
    compiler.set_library_dirs(build_ext_cmd.library_dirs)
    sysconfig.customize_compiler(compiler)
    def build_and_copy(extension):
        """compile sources, link shared library, and copy it into CWD"""
        objects = compiler.compile(
            extension.sources,
            output_dir=build_temp_dir,
            include_dirs=extension.include_dirs,
            debug=False,
            depends=extension.depends)
        output_file = os.path.join(
            build_temp_dir, build_ext_cmd.get_ext_filename(extension.name))
        compiler.link_shared_object(
            objects,
            output_file,
            libraries=build_ext_cmd.get_libraries(extension),
            library_dirs=extension.library_dirs,
            runtime_library_dirs=extension.runtime_library_dirs,
            export_symbols=build_ext_cmd.get_export_symbols(extension),
            debug=False,
            build_temp=build_temp_dir,
            target_lang=compiler.detect_language(extension.sources))
        file_util.copy_file(output_file, os.path.curdir)
    for extension in TEST_EXTENSIONS:
        build_and_copy(extension)