Esempio n. 1
0
def _standard_libs_libdirs_incdirs_aliases():
    r"""
    Return the list of libraries and library directories.

    EXAMPLES::

        sage: from sage.misc.cython import _standard_libs_libdirs_incdirs_aliases
        sage: _standard_libs_libdirs_incdirs_aliases()
        (['mpfr', 'gmp', 'gmpxx', 'pari', ...],
         [...],
         [...],
         {...})
    """
    aliases = cython_aliases()
    standard_libs = [
        'mpfr', 'gmp', 'gmpxx', 'pari', 'm',
        'ec', 'gsl',
    ] + aliases["CBLAS_LIBRARIES"] + [
        'ntl']
    standard_libdirs = []
    if SAGE_LOCAL:
        standard_libdirs.append(os.path.join(SAGE_LOCAL, "lib"))
    standard_libdirs.extend(aliases["CBLAS_LIBDIR"] + aliases["NTL_LIBDIR"])
    standard_incdirs = sage_include_directories() + aliases["CBLAS_INCDIR"] + aliases["NTL_INCDIR"]
    return standard_libs, standard_libdirs, standard_incdirs, aliases
Esempio n. 2
0
    def run(self):
        """
        Call ``cythonize()`` to replace the ``ext_modules`` with the
        extensions containing Cython-generated C code.
        """
        from sage.env import (cython_aliases, sage_include_directories)
        # Set variables used in self.create_extension
        from ..library_order import library_order
        self.library_order = library_order
        # Search for dependencies in the source tree and add to the list of include directories
        self.sage_include_dirs = sage_include_directories(use_sources=True)

        from Cython.Build import cythonize
        import Cython.Compiler.Options

        Cython.Compiler.Options.embed_pos_in_docstring = True

        log.info("Updating Cython code....")
        t = time.time()
        extensions = cythonize(
            self.extensions,
            nthreads=self.parallel,
            build_dir=self.build_dir,
            force=self.force,
            aliases=cython_aliases(),
            compiler_directives=self.cython_directives,
            compile_time_env=self.compile_time_env,
            create_extension=self.create_extension,
            # Debugging
            gdb_debug=self.debug,
            output_dir=os.path.join(self.build_lib, "sage"),
            # Disable Cython caching, which is currently too broken to
            # use reliably: http://trac.sagemath.org/ticket/17851
            cache=False,
        )

        # Filter out extensions with skip_build=True
        extensions = [
            ext for ext in extensions if not getattr(ext, "skip_build", False)
        ]

        # We use [:] to change the list in-place because the same list
        # object is pointed to from different places.
        self.extensions[:] = extensions

        log.info("Finished Cythonizing, time: %.2f seconds." %
                 (time.time() - t))

        with open(self._version_file, 'w') as f:
            f.write(self._version_stamp)

        # Finally, copy relevant cythonized files from build/cythonized
        # tree into the build-lib tree
        for (dst_dir, src_files) in self.get_cythonized_package_files():
            dst = os.path.join(self.build_lib, dst_dir)
            self.mkpath(dst)
            for src in src_files:
                self.copy_file(src, dst, preserve_mode=False)
Esempio n. 3
0
def cython(filename, verbose=0, compile_message=False,
           use_cache=False, create_local_c_file=False, annotate=True, sage_namespace=True,
           create_local_so_file=False):
    r"""
    Compile a Cython file. This converts a Cython file to a C (or C++ file),
    and then compiles that. The .c file and the .so file are
    created in a temporary directory.

    INPUT:

    - ``filename`` -- the name of the file to be compiled. Should end with
      'pyx'.

    - ``verbose`` (integer, default 0) -- level of verbosity. A negative
      value ensures complete silence.

    - ``compile_message`` (bool, default False) -- if True, print
      ``'Compiling <filename>...'`` to the standard error.

    - ``use_cache`` (bool, default False) -- if True, check the
      temporary build directory to see if there is already a
      corresponding .so file. If so, and if the .so file is newer than the
      Cython file, don't recompile, just reuse the .so file.

    - ``create_local_c_file`` (bool, default False) -- if True, save a
      copy of the ``.c`` or ``.cpp`` file in the current directory.

    - ``annotate`` (bool, default True) -- if True, create an html file which
      annotates the conversion from .pyx to .c. By default this is only created
      in the temporary directory, but if ``create_local_c_file`` is also True,
      then save a copy of the .html file in the current directory.

    - ``sage_namespace`` (bool, default True) -- if True, import
      ``sage.all``.

    - ``create_local_so_file`` (bool, default False) -- if True, save a
      copy of the compiled .so file in the current directory.

    OUTPUT: a tuple ``(name, dir)`` where ``name`` is the name
    of the compiled module and ``dir`` is the directory containing
    the generated files.

    TESTS:

    Before :trac:`12975`, it would have been needed to write ``#clang c++``,
    but upper case ``C++`` has resulted in an error.
    Using pkgconfig to find the libraries, headers and macros. This is a
    work around while waiting for :trac:`22461` which will offer a better
    solution::

        sage: code = [
        ....: "#clang C++",
        ....: "from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomial_libsingular",
        ....: "from sage.libs.singular.polynomial cimport singular_polynomial_pow",
        ....: "def test(MPolynomial_libsingular p):",
        ....: "    singular_polynomial_pow(&p._poly, p._poly, 2, p._parent_ring)"]
        sage: cython(os.linesep.join(code))

    The function ``test`` now manipulates internal C data of polynomials,
    squaring them::

        sage: P.<x,y>=QQ[]
        sage: test(x)
        sage: x
        x^2

    Check that compiling C++ code works::

        sage: cython("# distutils: language = c++\n"+
        ....:        "from libcpp.vector cimport vector\n"
        ....:        "cdef vector[int] * v = new vector[int](4)\n")

    Check that compiling C++ code works when creating a local C file,
    first moving to a tempdir to avoid clutter.  Before :trac:`22113`,
    the create_local_c_file argument was not tested for C++ code::

        sage: import sage.misc.cython
        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: with open("test.pyx", 'w') as f:
        ....:     _ = f.write("# distutils: language = c++\n"
        ....:       "from libcpp.vector cimport vector\n"
        ....:       "cdef vector[int] * v = new vector[int](4)\n")
        sage: output = sage.misc.cython.cython("test.pyx", create_local_c_file=True)

    Accessing a ``.pxd`` file from the current directory works::

        sage: import sage.misc.cython
        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: with open("helper.pxd", 'w') as f:
        ....:     f.write("cdef inline int the_answer(): return 42")
        sage: cython('''
        ....: from helper cimport the_answer
        ....: print(the_answer())
        ....: ''')
        42

    Warning and error messages generated by Cython are properly
    handled. Warnings are only shown if verbose >= 0::

        sage: code = '''
        ....: def test_unreachable():
        ....:     raise Exception
        ....:     return 42
        ....: '''
        sage: cython(code, verbose=-1)
        sage: cython(code, verbose=0)
        warning: ...:4:4: Unreachable code

        sage: cython("foo = bar\n")
        Traceback (most recent call last):
        ...
        RuntimeError: Error compiling Cython file:
        ------------------------------------------------------------
        ...
        foo = bar
             ^
        ------------------------------------------------------------
        <BLANKLINE>
        ...:1:6: undeclared name not builtin: bar

        sage: cython("cdef extern from 'no_such_header_file': pass")
        Traceback (most recent call last):
        ...
        RuntimeError: ...

    Sage used to automatically include various ``.pxi`` files. Since
    :trac:`22805`, we no longer do this. But we make sure to give a
    useful message in case the ``.pxi`` files were needed::

        sage: cython("sig_malloc(0)\n")
        Traceback (most recent call last):
        ...
        RuntimeError: Error compiling Cython file:
        ------------------------------------------------------------
        ...
        sig_malloc(0)
        ^
        ------------------------------------------------------------
        <BLANKLINE>
        ...:1:0: undeclared name not builtin: sig_malloc
        <BLANKLINE>
        NOTE: Sage no longer automatically includes the deprecated files
        "cdefs.pxi", "signals.pxi" and "stdsage.pxi" in Cython files.
        You can fix your code by adding "from cysignals.memory cimport sig_malloc".
    """
    if not filename.endswith('pyx'):
        print("Warning: file (={}) should have extension .pyx".format(filename), file=sys.stderr)

    # base is the name of the .so module that we create. If we are
    # creating a local shared object file, we use a more natural
    # naming convention. If we are not creating a local shared object
    # file, the main constraint is that it is unique and determined by
    # the file that we're running Cython on, so that in some cases we
    # can cache the result (e.g., recompiling the same pyx file during
    # the same session).
    if create_local_so_file:
        base, ext = os.path.splitext(os.path.basename(filename))
    else:
        base = os.path.abspath(filename)
    base = sanitize(base)

    # This is the *temporary* directory where we store the pyx file.
    # This is deleted when Sage exits, which means pyx files must be
    # rebuilt every time Sage is restarted at present.
    target_dir = os.path.join(SPYX_TMP, base)

    # Build directory for Cython/distutils
    build_dir = os.path.join(target_dir, "build")

    if os.path.exists(target_dir):
        # There is already a module here. Maybe we do not have to rebuild?
        # Find the name.
        if use_cache:
            from sage.misc.sageinspect import loadable_module_extension
            prev_so = [F for F in os.listdir(target_dir) if F.endswith(loadable_module_extension())]
            if len(prev_so) > 0:
                prev_so = prev_so[0]     # should have length 1 because of deletes below
                if os.path.getmtime(filename) <= os.path.getmtime('%s/%s'%(target_dir, prev_so)):
                    # We do not have to rebuild.
                    return prev_so[:-len(loadable_module_extension())], target_dir

        # Delete all ordinary files in target_dir
        for F in os.listdir(target_dir):
            G = os.path.join(target_dir, F)
            if os.path.isdir(G):
                continue
            try:
                os.unlink(G)
            except OSError:
                pass
    else:
        sage_makedirs(target_dir)

    if create_local_so_file:
        name = base
    else:
        global sequence_number
        if base not in sequence_number:
            sequence_number[base] = 0
        name = '%s_%s'%(base, sequence_number[base])

        # increment the sequence number so will use a different one next time.
        sequence_number[base] += 1

    if compile_message:
        print("Compiling {}...".format(filename), file=sys.stderr)
        sys.stderr.flush()

    with open(filename) as f:
        (preparsed, libs, includes, language, additional_source_files,
         extra_args, libdirs) = _pyx_preparse(f.read())

    # New filename with preparsed code.
    # NOTE: if we ever stop preparsing, we should still copy the
    # original file to the target directory.
    pyxfile = os.path.join(target_dir, name + ".pyx")
    with open(pyxfile, 'w') as f:
        f.write(preparsed)

    extra_sources = []
    for fname in additional_source_files:
        fname = fname.replace("$SAGE_SRC", SAGE_SRC)
        fname = fname.replace("$SAGE_LOCAL", SAGE_LOCAL)
        extra_sources.append(fname)

    # Add current working directory to includes. This is needed because
    # we cythonize from a different directory. See Trac #24764.
    includes.insert(0, os.getcwd())

    # Now do the actual build, directly calling Cython and distutils
    from Cython.Build import cythonize
    from Cython.Compiler.Errors import CompileError
    import Cython.Compiler.Options
    from distutils.dist import Distribution
    from distutils.core import Extension
    from distutils.log import set_verbosity
    set_verbosity(verbose)

    Cython.Compiler.Options.annotate = annotate
    Cython.Compiler.Options.embed_pos_in_docstring = True
    Cython.Compiler.Options.pre_import = "sage.all" if sage_namespace else None

    ext = Extension(name,
                    sources=[pyxfile] + extra_sources,
                    libraries=libs,
                    library_dirs=[os.path.join(SAGE_LOCAL, "lib")] + libdirs,
                    extra_compile_args=extra_args,
                    language=language)

    try:
        # Change directories to target_dir so that Cython produces the correct
        # relative path; https://trac.sagemath.org/ticket/24097
        with restore_cwd(target_dir):
            try:
                ext, = cythonize([ext],
                        aliases=cython_aliases(),
                        include_path=includes,
                        quiet=(verbose <= 0),
                        errors_to_stderr=False,
                        use_listing_file=True)
            finally:
                # Read the "listing file" which is the file containing
                # warning and error messages generated by Cython.
                try:
                    with open(name + ".lis") as f:
                        cython_messages = f.read()
                except IOError:
                    cython_messages = "Error compiling Cython file"
    except CompileError:
        # Check for names in old_pxi_names
        for pxd, names in old_pxi_names.items():
            for name in names:
                if re.search(r"\b{}\b".format(name), cython_messages):
                    cython_messages += dedent(
                        """
                        NOTE: Sage no longer automatically includes the deprecated files
                        "cdefs.pxi", "signals.pxi" and "stdsage.pxi" in Cython files.
                        You can fix your code by adding "from {} cimport {}".
                        """.format(pxd, name))
        raise RuntimeError(cython_messages.strip())

    if verbose >= 0:
        sys.stderr.write(cython_messages)
        sys.stderr.flush()

    if create_local_c_file:
        shutil.copy(os.path.join(target_dir, ext.sources[0]),
                    os.curdir)
        if annotate:
            shutil.copy(os.path.join(target_dir, name + ".html"),
                        os.curdir)

    # This emulates running "setup.py build" with the correct options
    dist = Distribution()
    dist.ext_modules = [ext]
    dist.include_dirs = includes
    buildcmd = dist.get_command_obj("build")
    buildcmd.build_base = build_dir
    buildcmd.build_lib = target_dir

    try:
        # Capture errors from distutils and its child processes
        with open(os.path.join(target_dir, name + ".err"), 'w+') as errfile:
            try:
                # Redirect stderr to errfile.  We use the file descriptor
                # number "2" instead of "sys.stderr" because we really
                # want to redirect the messages from GCC. These are sent
                # to the actual stderr, regardless of what sys.stderr is.
                sys.stderr.flush()
                with redirection(2, errfile, close=False):
                    dist.run_command("build")
            finally:
                errfile.seek(0)
                distutils_messages = errfile.read()
    except Exception as msg:
        msg = str(msg) + "\n" + distutils_messages
        raise RuntimeError(msg.strip())

    if verbose >= 0:
        sys.stderr.write(distutils_messages)
        sys.stderr.flush()

    if create_local_so_file:
        # Copy module to current directory
        from sage.misc.sageinspect import loadable_module_extension
        shutil.copy(os.path.join(target_dir, name + loadable_module_extension()),
                    os.curdir)

    return name, target_dir
Esempio n. 4
0
def cython(filename,
           verbose=0,
           compile_message=False,
           use_cache=False,
           create_local_c_file=False,
           annotate=True,
           sage_namespace=True,
           create_local_so_file=False):
    r"""
    Compile a Cython file. This converts a Cython file to a C (or C++ file),
    and then compiles that. The .c file and the .so file are
    created in a temporary directory.

    INPUT:

    - ``filename`` -- the name of the file to be compiled. Should end with
      'pyx'.

    - ``verbose`` (integer, default 0) -- level of verbosity. A negative
      value ensures complete silence.

    - ``compile_message`` (bool, default False) -- if True, print
      ``'Compiling <filename>...'`` to the standard error.

    - ``use_cache`` (bool, default False) -- if True, check the
      temporary build directory to see if there is already a
      corresponding .so file. If so, and if the .so file is newer than the
      Cython file, don't recompile, just reuse the .so file.

    - ``create_local_c_file`` (bool, default False) -- if True, save a
      copy of the ``.c`` or ``.cpp`` file in the current directory.

    - ``annotate`` (bool, default True) -- if True, create an html file which
      annotates the conversion from .pyx to .c. By default this is only created
      in the temporary directory, but if ``create_local_c_file`` is also True,
      then save a copy of the .html file in the current directory.

    - ``sage_namespace`` (bool, default True) -- if True, import
      ``sage.all``.

    - ``create_local_so_file`` (bool, default False) -- if True, save a
      copy of the compiled .so file in the current directory.

    OUTPUT: a tuple ``(name, dir)`` where ``name`` is the name
    of the compiled module and ``dir`` is the directory containing
    the generated files.

    TESTS:

    Before :trac:`12975`, it would have been needed to write ``#clang c++``,
    but upper case ``C++`` has resulted in an error.
    Using pkgconfig to find the libraries, headers and macros. This is a
    work around while waiting for :trac:`22461` which will offer a better
    solution::

        sage: code = [
        ....: "#clang C++",
        ....: "from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomial_libsingular",
        ....: "from sage.libs.singular.polynomial cimport singular_polynomial_pow",
        ....: "def test(MPolynomial_libsingular p):",
        ....: "    singular_polynomial_pow(&p._poly, p._poly, 2, p._parent_ring)"]
        sage: cython(os.linesep.join(code))

    The function ``test`` now manipulates internal C data of polynomials,
    squaring them::

        sage: P.<x,y>=QQ[]
        sage: test(x)
        sage: x
        x^2

    Check that compiling C++ code works::

        sage: cython("# distutils: language = c++\n"+
        ....:        "from libcpp.vector cimport vector\n"
        ....:        "cdef vector[int] * v = new vector[int](4)\n")

    Check that compiling C++ code works when creating a local C file,
    first moving to a tempdir to avoid clutter.  Before :trac:`22113`,
    the create_local_c_file argument was not tested for C++ code::

        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: with open("test.pyx", 'w') as f:
        ....:     _ = f.write("# distutils: language = c++\n"
        ....:       "from libcpp.vector cimport vector\n"
        ....:       "cdef vector[int] * v = new vector[int](4)\n")
        sage: output = sage.misc.cython.cython("test.pyx", create_local_c_file=True)

    Accessing a ``.pxd`` file from the current directory works::

        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: with open("helper.pxd", 'w') as f:
        ....:     _ = f.write("cdef inline int the_answer(): return 42")
        sage: cython('''
        ....: from helper cimport the_answer
        ....: print(the_answer())
        ....: ''')
        42

    Warning and error messages generated by Cython are properly
    handled. Warnings are only shown if verbose >= 0::

        sage: code = '''
        ....: def test_unreachable():
        ....:     raise Exception
        ....:     return 42
        ....: '''
        sage: cython(code, verbose=-1)
        sage: cython(code, verbose=0)
        warning: ...:4:4: Unreachable code

        sage: cython("foo = bar\n")
        Traceback (most recent call last):
        ...
        RuntimeError: Error compiling Cython file:
        ------------------------------------------------------------
        ...
        foo = bar
             ^
        ------------------------------------------------------------
        <BLANKLINE>
        ...:1:6: undeclared name not builtin: bar

        sage: cython("cdef extern from 'no_such_header_file': pass")
        Traceback (most recent call last):
        ...
        RuntimeError: ...
    """
    if not filename.endswith('pyx'):
        print(
            "Warning: file (={}) should have extension .pyx".format(filename),
            file=sys.stderr)

    # base is the name of the .so module that we create. If we are
    # creating a local shared object file, we use a more natural
    # naming convention. If we are not creating a local shared object
    # file, the main constraint is that it is unique and determined by
    # the file that we're running Cython on, so that in some cases we
    # can cache the result (e.g., recompiling the same pyx file during
    # the same session).
    if create_local_so_file:
        base, ext = os.path.splitext(os.path.basename(filename))
    else:
        base = os.path.abspath(filename)
    base = sanitize(base)

    # This is the *temporary* directory where we store the pyx file.
    # This is deleted when Sage exits, which means pyx files must be
    # rebuilt every time Sage is restarted at present.
    target_dir = os.path.join(SPYX_TMP, base)

    # Build directory for Cython/distutils
    build_dir = os.path.join(target_dir, "build")

    if os.path.exists(target_dir):
        # There is already a module here. Maybe we do not have to rebuild?
        # Find the name.
        if use_cache:
            from sage.misc.sageinspect import loadable_module_extension
            prev_so = [
                F for F in os.listdir(target_dir)
                if F.endswith(loadable_module_extension())
            ]
            if len(prev_so) > 0:
                prev_so = prev_so[
                    0]  # should have length 1 because of deletes below
                if os.path.getmtime(filename) <= os.path.getmtime(
                        '%s/%s' % (target_dir, prev_so)):
                    # We do not have to rebuild.
                    return prev_so[:-len(loadable_module_extension()
                                         )], target_dir

        # Delete all ordinary files in target_dir
        for F in os.listdir(target_dir):
            G = os.path.join(target_dir, F)
            if os.path.isdir(G):
                continue
            try:
                os.unlink(G)
            except OSError:
                pass
    else:
        sage_makedirs(target_dir)

    if create_local_so_file:
        name = base
    else:
        global sequence_number
        if base not in sequence_number:
            sequence_number[base] = 0
        name = '%s_%s' % (base, sequence_number[base])

        # increment the sequence number so will use a different one next time.
        sequence_number[base] += 1

    if compile_message:
        sys.stderr.write("Compiling {}...\n".format(filename))
        sys.stderr.flush()

    # Copy original file to the target directory.
    pyxfile = os.path.join(target_dir, name + ".pyx")
    shutil.copy(filename, pyxfile)

    # Add current working directory to includes. This is needed because
    # we cythonize from a different directory. See Trac #24764.
    includes = [os.getcwd()] + sage_include_directories()

    # Now do the actual build, directly calling Cython and distutils
    from Cython.Build import cythonize
    from Cython.Compiler.Errors import CompileError
    import Cython.Compiler.Options
    from distutils.dist import Distribution
    from distutils.core import Extension
    from distutils.log import set_verbosity
    set_verbosity(verbose)

    Cython.Compiler.Options.annotate = annotate
    Cython.Compiler.Options.embed_pos_in_docstring = True
    Cython.Compiler.Options.pre_import = "sage.all" if sage_namespace else None

    ext = Extension(
        name,
        sources=[pyxfile],
        extra_compile_args=["-w"],  # no warnings
        libraries=standard_libs,
        library_dirs=standard_libdirs)

    directives = dict(language_level=sys.version_info[0])

    try:
        # Change directories to target_dir so that Cython produces the correct
        # relative path; https://trac.sagemath.org/ticket/24097
        with restore_cwd(target_dir):
            try:
                ext, = cythonize([ext],
                                 aliases=cython_aliases(),
                                 include_path=includes,
                                 compiler_directives=directives,
                                 quiet=(verbose <= 0),
                                 errors_to_stderr=False,
                                 use_listing_file=True)
            finally:
                # Read the "listing file" which is the file containing
                # warning and error messages generated by Cython.
                try:
                    with open(name + ".lis") as f:
                        cython_messages = f.read()
                except IOError:
                    cython_messages = "Error compiling Cython file"
    except CompileError:
        raise RuntimeError(cython_messages.strip())

    if verbose >= 0:
        sys.stderr.write(cython_messages)
        sys.stderr.flush()

    if create_local_c_file:
        shutil.copy(os.path.join(target_dir, ext.sources[0]), os.curdir)
        if annotate:
            shutil.copy(os.path.join(target_dir, name + ".html"), os.curdir)

    # This emulates running "setup.py build" with the correct options
    dist = Distribution()
    dist.ext_modules = [ext]
    dist.include_dirs = includes
    buildcmd = dist.get_command_obj("build")
    buildcmd.build_base = build_dir
    buildcmd.build_lib = target_dir

    try:
        # Capture errors from distutils and its child processes
        with open(os.path.join(target_dir, name + ".err"), 'w+') as errfile:
            try:
                # Redirect stderr to errfile.  We use the file descriptor
                # number "2" instead of "sys.stderr" because we really
                # want to redirect the messages from GCC. These are sent
                # to the actual stderr, regardless of what sys.stderr is.
                sys.stderr.flush()
                with redirection(2, errfile, close=False):
                    dist.run_command("build")
            finally:
                errfile.seek(0)
                distutils_messages = errfile.read()
    except Exception as msg:
        msg = str(msg) + "\n" + distutils_messages
        raise RuntimeError(msg.strip())

    if verbose >= 0:
        sys.stderr.write(distutils_messages)
        sys.stderr.flush()

    if create_local_so_file:
        # Copy module to current directory
        from sage.misc.sageinspect import loadable_module_extension
        shutil.copy(
            os.path.join(target_dir, name + loadable_module_extension()),
            os.curdir)

    return name, target_dir
Esempio n. 5
0
    m4ri_extra_compile_args.remove("-pedantic")
except ValueError:
    pass

# END copied from module_list.py (but #29706 removes the original).

# This list defines the *order* of linking libraries. A library should
# be put *before* any library it links to. Cython allows
# defining libraries using "# distutils: libraries = LIB". However, if
# there are multiple libraries, the order is undefined so we need to
# manually reorder the libraries according to this list. The order is
# important in particular for Cygwin. Any libraries which are not
# listed here will be added at the end of the list (without changing
# their relative order).
from sage.env import cython_aliases
aliases = cython_aliases()

arb_dylib_name = aliases["ARB_LIBRARY"]
library_order_list = aliases["SINGULAR_LIBRARIES"] + [
    "giac", "intl", "curl", "ec", "ecm"
] + aliases["LINBOX_LIBRARIES"] + aliases["FFLASFFPACK_LIBRARIES"] + aliases[
    "GSL_LIBRARIES"] + [
        "pari",
        "flint",
        "ratpoints",
        "ecl",
        "glpk",
        "ppl",
        arb_dylib_name,
        "mpfi",
        "mpfr",
Esempio n. 6
0
### Library order
#########################################################

# This list defines the *order* of linking libraries. A library should
# be put *before* any library it links to. Cython allows
# defining libraries using "# distutils: libraries = LIB". However, if
# there are multiple libraries, the order is undefined so we need to
# manually reorder the libraries according to this list. The order is
# important in particular for Cygwin. Any libraries which are not
# listed here will be added at the end of the list (without changing
# their relative order).
from sage.env import cython_aliases, default_required_modules, default_optional_modules

modules = default_required_modules + default_optional_modules

aliases = cython_aliases(required_modules=(), optional_modules=modules)

if "ARB_LIBRARY" in aliases:
    arb_dylib_names = [aliases["ARB_LIBRARY"]]
else:
    arb_dylib_names = []

library_order_list = aliases.get("SINGULAR_LIBRARIES", []) + [
    "giac", "intl", "curl", "ec", "ecm"
] + aliases.get("LINBOX_LIBRARIES", []) + aliases.get(
    "FFLASFFPACK_LIBRARIES", []) + aliases.get("GSL_LIBRARIES", []) + [
        "pari",
        "flint",
        "ratpoints",
        "ecl",
        "glpk",
Esempio n. 7
0
                  include_dirs=["local/include"] + sage_include_directories(),
                  library_dirs=["local/lib"],
                  libraries=[
                      "fgb", "fgbexp", "gb", "gbexp", "minpoly", "minpolyvgf",
                      "gmp", "m"
                  ],
                  extra_compile_args=["-std=c++11", "-fopenmp"],
                  extra_link_args=["-fopenmp"],
                  define_macros=[("LIBMODE", libmode),
                                 ("FGB_MAC", 1 if UNAME == 'Darwin' else 0),
                                 ("FGB_MAC_MAX_INPUT", 100000)],
                  sources=[os.path.join(SRC, name + ".pyx")])
    ],
              compile_time_env=dict(PY_LIBMODE=libmode,
                                    PY_FGB_MAC=(UNAME == 'Darwin')),
              compiler_directives=dict(language_level=2),
              annotate=False,
              aliases=cython_aliases())[0] for (name, libmode) in PYX_FILES
]

setuptools.setup(cmdclass={
    'build_libfgb': BuildLibfgbCommand,
    'build_ext': BuildExtCommand,
    'test': TestCommand,
    'clean': CleanCommand,
},
                 name="fgb_sage",
                 version=VERSION,
                 packages=["fgb_sage"],
                 ext_modules=ext_modules)
Esempio n. 8
0
File: cython.py Progetto: yarv/sage
def cython(filename,
           verbose=0,
           compile_message=False,
           use_cache=False,
           create_local_c_file=False,
           annotate=True,
           sage_namespace=True,
           create_local_so_file=False):
    r"""
    Compile a Cython file. This converts a Cython file to a C (or C++ file),
    and then compiles that. The .c file and the .so file are
    created in a temporary directory.

    INPUT:

    - ``filename`` -- the name of the file to be compiled. Should end with
      'pyx'.

    - ``verbose`` (integer, default 0) -- level of verbosity.

    - ``compile_message`` (bool, default False) -- if True, print
      ``'Compiling <filename>...'`` to the standard error.

    - ``use_cache`` (bool, default False) -- if True, check the
      temporary build directory to see if there is already a
      corresponding .so file. If so, and if the .so file is newer than the
      Cython file, don't recompile, just reuse the .so file.

    - ``create_local_c_file`` (bool, default False) -- if True, save a
      copy of the ``.c`` or ``.cpp`` file in the current directory.

    - ``annotate`` (bool, default True) -- if True, create an html file which
      annotates the conversion from .pyx to .c. By default this is only created
      in the temporary directory, but if ``create_local_c_file`` is also True,
      then save a copy of the .html file in the current directory.

    - ``sage_namespace`` (bool, default True) -- if True, import
      ``sage.all``.

    - ``create_local_so_file`` (bool, default False) -- if True, save a
      copy of the compiled .so file in the current directory.

    TESTS:

    Before :trac:`12975`, it would have been needed to write ``#clang c++``,
    but upper case ``C++`` has resulted in an error.
    Using pkgconfig to find the libraries, headers and macros. This is a
    work around while waiting for :trac:`22461` which will offer a better
    solution::

        sage: code = [
        ....: "#clang C++",
        ....: "from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomial_libsingular",
        ....: "from sage.libs.singular.polynomial cimport singular_polynomial_pow",
        ....: "def test(MPolynomial_libsingular p):",
        ....: "    singular_polynomial_pow(&p._poly, p._poly, 2, p._parent_ring)"]
        sage: cython(os.linesep.join(code))

    The function ``test`` now manipulates internal C data of polynomials,
    squaring them::

        sage: P.<x,y>=QQ[]
        sage: test(x)
        sage: x
        x^2

    Check that compiling C++ code works::

        sage: cython("# distutils: language = c++\n"+
        ....:        "from libcpp.vector cimport vector\n"
        ....:        "cdef vector[int] * v = new vector[int](4)\n")

    Check that compiling C++ code works when creating a local C file,
    first moving to a tempdir to avoid clutter.  Before :trac:`22113`,
    the create_local_c_file argument was not tested for C++ code::

        sage: import sage.misc.cython
        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: with open("test.pyx", 'w') as f:
        ....:     _ = f.write("# distutils: language = c++\n"
        ....:       "from libcpp.vector cimport vector\n"
        ....:       "cdef vector[int] * v = new vector[int](4)\n")
        sage: output = sage.misc.cython.cython("test.pyx", create_local_c_file=True)

    Sage used to automatically include various ``.pxi`` files. Since
    :trac:`22805`, we no longer do this. But we make sure to give a
    useful message in case the ``.pxi`` files were needed::

        sage: cython("sig_malloc(0)")
        Traceback (most recent call last):
        ...
        RuntimeError: Error converting ... to C
        NOTE: Sage no longer automatically includes the deprecated files
        "cdefs.pxi", "signals.pxi" and "stdsage.pxi" in Cython files.
        You can fix your code by adding "from cysignals.memory cimport sig_malloc".
    """
    if not filename.endswith('pyx'):
        print(
            "Warning: file (={}) should have extension .pyx".format(filename),
            file=sys.stderr)

    # base is the name of the .so module that we create. If we are
    # creating a local shared object file, we use a more natural
    # naming convention. If we are not creating a local shared object
    # file, the main constraint is that it is unique and determined by
    # the file that we're running Cython on, so that in some cases we
    # can cache the result (e.g., recompiling the same pyx file during
    # the same session).
    if create_local_so_file:
        base, ext = os.path.splitext(os.path.basename(filename))
        base = sanitize(base)
    else:
        base = sanitize(os.path.abspath(filename))

    # This is the *temporary* directory where we store the pyx file.
    # This is deleted when Sage exits, which means pyx files must be
    # rebuilt every time Sage is restarted at present.
    target_dir = os.path.join(SPYX_TMP, base)

    # Build directory for Cython/distutils
    build_dir = os.path.join(target_dir, "build")

    if os.path.exists(target_dir):
        # There is already a module here. Maybe we do not have to rebuild?
        # Find the name.
        if use_cache:
            from sage.misc.sageinspect import loadable_module_extension
            prev_so = [
                F for F in os.listdir(target_dir)
                if F.endswith(loadable_module_extension())
            ]
            if len(prev_so) > 0:
                prev_so = prev_so[
                    0]  # should have length 1 because of deletes below
                if os.path.getmtime(filename) <= os.path.getmtime(
                        '%s/%s' % (target_dir, prev_so)):
                    # We do not have to rebuild.
                    return prev_so[:-len(loadable_module_extension()
                                         )], target_dir

        # Delete all ordinary files in target_dir
        for F in os.listdir(target_dir):
            G = os.path.join(target_dir, F)
            if os.path.isdir(G):
                continue
            try:
                os.unlink(G)
            except OSError:
                pass
    else:
        sage_makedirs(target_dir)

    if create_local_so_file:
        name = base
    else:
        global sequence_number
        if base not in sequence_number:
            sequence_number[base] = 0
        name = '%s_%s' % (base, sequence_number[base])

        # increment the sequence number so will use a different one next time.
        sequence_number[base] += 1

    if compile_message:
        print("Compiling {}...".format(filename), file=sys.stderr)

    with open(filename) as f:
        (preparsed, libs, includes, language, additional_source_files,
         extra_args, libdirs) = _pyx_preparse(f.read())

    # New filename with preparsed code.
    # NOTE: if we ever stop preparsing, we should still copy the
    # original file to the target directory.
    pyxfile = os.path.join(target_dir, name + ".pyx")
    with open(pyxfile, 'w') as f:
        f.write(preparsed)

    extra_sources = []
    for fname in additional_source_files:
        fname = fname.replace("$SAGE_SRC", SAGE_SRC)
        fname = fname.replace("$SAGE_LOCAL", SAGE_LOCAL)
        extra_sources.append(fname)

    # Now do the actual build, directly calling Cython and distutils
    from Cython.Build import cythonize
    from Cython.Compiler.Errors import CompileError
    import Cython.Compiler.Options
    from distutils.dist import Distribution
    from distutils.core import Extension
    from distutils.log import set_verbosity
    set_verbosity(verbose)

    Cython.Compiler.Options.annotate = annotate
    Cython.Compiler.Options.embed_pos_in_docstring = True
    Cython.Compiler.Options.pre_import = "sage.all" if sage_namespace else None

    ext = Extension(name,
                    sources=[pyxfile] + extra_sources,
                    libraries=libs,
                    library_dirs=[os.path.join(SAGE_LOCAL, "lib")] + libdirs,
                    extra_compile_args=extra_args,
                    language=language)

    orig_cwd = os.getcwd()
    try:
        # Change directories to target_dir so that Cython produces the correct
        # relative path; https://trac.sagemath.org/ticket/24097
        os.chdir(target_dir)
        ext, = cythonize([ext],
                         aliases=cython_aliases(),
                         include_path=includes,
                         quiet=not verbose)
    except CompileError:
        # Check for names in old_pxi_names
        note = ''
        for pxd, names in old_pxi_names.items():
            for name in names:
                if re.search(r"\b{}\b".format(name), preparsed):
                    note += dedent("""
                        NOTE: Sage no longer automatically includes the deprecated files
                        "cdefs.pxi", "signals.pxi" and "stdsage.pxi" in Cython files.
                        You can fix your code by adding "from {} cimport {}".
                        """.format(pxd, name))
        raise RuntimeError("Error converting {} to C".format(filename) + note)
    finally:
        os.chdir(orig_cwd)

    if create_local_c_file:
        shutil.copy(os.path.join(target_dir, ext.sources[0]), os.curdir)
        if annotate:
            shutil.copy(os.path.join(target_dir, name + ".html"), os.curdir)

    # This emulates running "setup.py build" with the correct options
    dist = Distribution()
    dist.ext_modules = [ext]
    dist.include_dirs = includes
    buildcmd = dist.get_command_obj("build")
    buildcmd.build_base = build_dir
    buildcmd.build_lib = target_dir
    dist.run_command("build")

    if create_local_so_file:
        # Copy module to current directory
        from sage.misc.sageinspect import loadable_module_extension
        shutil.copy(
            os.path.join(target_dir, name + loadable_module_extension()),
            os.curdir)

    return name, target_dir