""" def print_box(msg): lines = msg.split('\n') size = max(len(l) + 1 for l in lines) print('-' * (size + 2)) for l in lines: print('|{}{}|'.format(l, ' ' * (size - len(l)))) print('-' * (size + 2)) if __name__ == '__main__': # Parse the command line and check the arguments # before we proceed with building deps and setup dist = Distribution() dist.script_name = sys.argv[0] dist.script_args = sys.argv[1:] try: ok = dist.parse_command_line() except DistutilsArgError as msg: raise SystemExit( core.gen_usage(dist.script_name) + "\nerror: %s" % msg) if not ok: sys.exit() if RUN_BUILD_DEPS: build_deps() setup( name=package_name, version=version,
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'])
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') # issue #5977 : distutils build_ext.get_outputs # returns wrong result with --inplace 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) # inplace = 0, cmd.package = 'bar' build_py = cmd.get_finalized_command('build_py') build_py.package_dir = {'': 'bar'} path = cmd.get_ext_fullpath('foo') # checking that the last directory is the build_dir path = os.path.split(path)[0] self.assertEqual(path, cmd.build_lib) # inplace = 1, cmd.package = 'bar' 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) # checking that the last directory is bar path = os.path.split(path)[0] lastdir = os.path.split(path)[-1] self.assertEqual(lastdir, 'bar')
def test_finalize_options(self): # Make sure Python's include directories (for Python.h, pyconfig.h, # etc.) are in the include search path. 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) # make sure cmd.libraries is turned into a list # if it's a string cmd = build_ext(dist) cmd.libraries = 'my_lib, other_lib lastlib' cmd.finalize_options() self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib']) # make sure cmd.library_dirs is turned into a list # if it's a string 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) # make sure rpath is turned into a list # if it's a string cmd = build_ext(dist) cmd.rpath = 'one%stwo' % os.pathsep cmd.finalize_options() self.assertEqual(cmd.rpath, ['one', 'two']) # XXX more tests to perform for win32 # make sure define is turned into 2-tuples # strings if they are ','-separated strings cmd = build_ext(dist) cmd.define = 'one,two' cmd.finalize_options() self.assertEqual(cmd.define, [('one', '1'), ('two', '1')]) # make sure undef is turned into a list of # strings if they are ','-separated strings cmd = build_ext(dist) cmd.undef = 'one,two' cmd.finalize_options() self.assertEqual(cmd.undef, ['one', 'two']) # make sure swig_opts is turned into a list 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'])
def get_command_class(name): # Returns the right class for either distutils or setuptools return Distribution({}).get_command_class(name)
def run(self): cmd = self.reinitialize_command("build_ext") cmd.inplace = True cmd.force = self.force cmd.ensure_finalized() cmd.run() gidatadir = pkg_config_parse("--variable=gidatadir", "gobject-introspection-1.0")[0] g_ir_scanner = pkg_config_parse("--variable=g_ir_scanner", "gobject-introspection-1.0")[0] g_ir_compiler = pkg_config_parse("--variable=g_ir_compiler", "gobject-introspection-1.0")[0] script_dir = get_script_dir() gi_dir = os.path.join(script_dir, "gi") tests_dir = os.path.join(script_dir, "tests") gi_tests_dir = os.path.join(gidatadir, "tests") schema_xml = os.path.join(tests_dir, "org.gnome.test.gschema.xml") schema_bin = os.path.join(tests_dir, "gschemas.compiled") if self._newer_group([schema_xml], schema_bin): subprocess.check_call([ "glib-compile-schemas", "--targetdir=%s" % tests_dir, "--schema-file=%s" % schema_xml, ]) compiler = new_compiler() customize_compiler(compiler) if os.name == "nt": compiler.shared_lib_extension = ".dll" elif sys.platform == "darwin": compiler.shared_lib_extension = ".dylib" if "-bundle" in compiler.linker_so: compiler.linker_so = list(compiler.linker_so) i = compiler.linker_so.index("-bundle") compiler.linker_so[i] = "-dynamiclib" else: compiler.shared_lib_extension = ".so" if compiler.compiler_type == "msvc": g_ir_scanner_cmd = [sys.executable, g_ir_scanner] else: g_ir_scanner_cmd = [g_ir_scanner] def build_ext(ext): libname = compiler.shared_object_filename(ext.name) ext_paths = [os.path.join(tests_dir, libname)] if os.name == "nt": if compiler.compiler_type == "msvc": # MSVC: Get rid of the 'lib' prefix and the .dll # suffix from libname, and append .lib so # that we get the right .lib filename to # pass to g-ir-scanner with --library implibname = libname[3:libname.rfind(".dll")] + '.lib' else: implibname = libname + ".a" ext_paths.append(os.path.join(tests_dir, implibname)) if self._newer_group(ext.sources + ext.depends, *ext_paths): # MSVC: We need to define _GI_EXTERN explcitly so that # symbols get exported properly if compiler.compiler_type == "msvc": extra_defines = [('_GI_EXTERN', '__declspec(dllexport)extern')] else: extra_defines = [] objects = compiler.compile(ext.sources, output_dir=self.build_temp, include_dirs=ext.include_dirs, macros=ext.define_macros + extra_defines) if os.name == "nt": if compiler.compiler_type == "msvc": postargs = [ "-implib:%s" % os.path.join(tests_dir, implibname) ] else: postargs = [ "-Wl,--out-implib=%s" % os.path.join(tests_dir, implibname) ] else: postargs = [] compiler.link_shared_object(objects, compiler.shared_object_filename( ext.name), output_dir=tests_dir, libraries=ext.libraries, library_dirs=ext.library_dirs, extra_postargs=postargs) return ext_paths ext = Extension( name='libgimarshallingtests', sources=[ os.path.join(gi_tests_dir, "gimarshallingtests.c"), os.path.join(tests_dir, "gimarshallingtestsextra.c"), ], include_dirs=[ gi_tests_dir, tests_dir, ], depends=[ os.path.join(gi_tests_dir, "gimarshallingtests.h"), os.path.join(tests_dir, "gimarshallingtestsextra.h"), ], ) add_ext_pkg_config_dep(ext, compiler.compiler_type, "glib-2.0") add_ext_pkg_config_dep(ext, compiler.compiler_type, "gio-2.0") ext_paths = build_ext(ext) # We want to always use POSIX-style paths for g-ir-compiler # because it expects the input .gir file and .typelib file to use # POSIX-style paths, otherwise it fails gir_path = posixpath.join(tests_dir, "GIMarshallingTests-1.0.gir") typelib_path = posixpath.join(tests_dir, "GIMarshallingTests-1.0.typelib") gimarshal_g_ir_scanner_cmd = g_ir_scanner_cmd + [ "--no-libtool", "--include=Gio-2.0", "--namespace=GIMarshallingTests", "--nsversion=1.0", "--symbol-prefix=gi_marshalling_tests", "--warn-all", "--warn-error", "--library-path=%s" % tests_dir, "--library=gimarshallingtests", "--pkg=glib-2.0", "--pkg=gio-2.0", "--cflags-begin", "-I%s" % gi_tests_dir, "--cflags-end", "--output=%s" % gir_path, ] if self._newer_group(ext_paths, gir_path): subprocess.check_call(gimarshal_g_ir_scanner_cmd + ext.sources + ext.depends) if self._newer_group([gir_path], typelib_path): subprocess.check_call([ g_ir_compiler, gir_path, "--output=%s" % typelib_path, ]) regress_macros = [] if not WITH_CAIRO: regress_macros.append(("_GI_DISABLE_CAIRO", "1")) ext = Extension( name='libregress', sources=[ os.path.join(gi_tests_dir, "regress.c"), os.path.join(tests_dir, "regressextra.c"), ], include_dirs=[ gi_tests_dir, ], depends=[ os.path.join(gi_tests_dir, "regress.h"), os.path.join(tests_dir, "regressextra.h"), ], define_macros=regress_macros, ) add_ext_pkg_config_dep(ext, compiler.compiler_type, "glib-2.0") add_ext_pkg_config_dep(ext, compiler.compiler_type, "gio-2.0") if WITH_CAIRO: add_ext_pkg_config_dep(ext, compiler.compiler_type, "cairo") add_ext_pkg_config_dep(ext, compiler.compiler_type, "cairo-gobject") ext_paths = build_ext(ext) # We want to always use POSIX-style paths for g-ir-compiler # because it expects the input .gir file and .typelib file to use # POSIX-style paths, otherwise it fails gir_path = posixpath.join(tests_dir, "Regress-1.0.gir") typelib_path = posixpath.join(tests_dir, "Regress-1.0.typelib") regress_g_ir_scanner_cmd = g_ir_scanner_cmd + [ "--no-libtool", "--include=Gio-2.0", "--namespace=Regress", "--nsversion=1.0", "--warn-all", "--warn-error", "--library-path=%s" % tests_dir, "--library=regress", "--pkg=glib-2.0", "--pkg=gio-2.0" ] if self._newer_group(ext_paths, gir_path): if WITH_CAIRO: regress_g_ir_scanner_cmd += ["--include=cairo-1.0"] # MSVC: We don't normally have the pkg-config files for # cairo and cairo-gobject, so use --extra-library # instead of --pkg to pass those to the linker, so that # g-ir-scanner won't fail due to linker errors if compiler.compiler_type == "msvc": regress_g_ir_scanner_cmd += [ "--extra-library=cairo", "--extra-library=cairo-gobject" ] else: regress_g_ir_scanner_cmd += [ "--pkg=cairo", "--pkg=cairo-gobject" ] else: regress_g_ir_scanner_cmd += ["-D_GI_DISABLE_CAIRO"] regress_g_ir_scanner_cmd += ["--output=%s" % gir_path] subprocess.check_call(regress_g_ir_scanner_cmd + ext.sources + ext.depends) if self._newer_group([gir_path], typelib_path): subprocess.check_call([ g_ir_compiler, gir_path, "--output=%s" % typelib_path, ]) ext = Extension( name='tests.testhelper', sources=[ os.path.join(tests_dir, "testhelpermodule.c"), os.path.join(tests_dir, "test-floating.c"), os.path.join(tests_dir, "test-thread.c"), os.path.join(tests_dir, "test-unknown.c"), ], include_dirs=[ gi_dir, tests_dir, ], depends=list_headers(gi_dir) + list_headers(tests_dir), define_macros=[("PY_SSIZE_T_CLEAN", None)], ) add_ext_pkg_config_dep(ext, compiler.compiler_type, "glib-2.0") add_ext_pkg_config_dep(ext, compiler.compiler_type, "gio-2.0") add_ext_compiler_flags(ext, compiler) dist = Distribution({"ext_modules": [ext]}) build_cmd = dist.get_command_obj("build") build_cmd.build_base = os.path.join(self.build_base, "pygobject_tests") build_cmd.ensure_finalized() cmd = dist.get_command_obj("build_ext") cmd.inplace = True cmd.force = self.force cmd.ensure_finalized() cmd.run()
def get_command_class(name): # in case pip loads with setuptools this returns the extended commands return Distribution({}).get_command_class(name)
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 = self.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")