theano/gof/cmodule.py
def compile_str(module_name, src_code, location=None,
                    include_dirs=None, lib_dirs=None, libs=None,
                    preargs=None, py_module=True, hide_symbols=True):
        """
        Parameters
        ----------
        module_name : str
            This has been embedded in the src_code.
        src_code
            A complete c or c++ source listing for the module.
        location
            A pre-existing filesystem directory where the cpp file and .so will
            be written.
        include_dirs
            A list of include directory names (each gets prefixed with -I).
        lib_dirs
            A list of library search path directory names (each gets prefixed
            with -L).
        libs
            A list of libraries to link with (each gets prefixed with -l).
        preargs
            A list of extra compiler arguments.
        py_module
            If False, compile to a shared library, but do not import it as a
            Python module.
        hide_symbols
            If True (the default) all symbols will be hidden from the library
            symbol table (which means that other objects can't use them).

        Returns
        -------
        object
            Dynamically-imported python module of the compiled code (unless
            py_module is False, in that case returns None).

        """
        # TODO: Do not do the dlimport in this function

        if not theano.config.cxx:
            raise MissingGXX("g++ not available! We can't compile c code.")

        if include_dirs is None:
            include_dirs = []
        if lib_dirs is None:
            lib_dirs = []
        if libs is None:
            libs = []
        if preargs is None:
            preargs = []

        # Remove empty string directory
        include_dirs = [d for d in include_dirs if d]
        lib_dirs = [d for d in lib_dirs if d]

        include_dirs = include_dirs + std_include_dirs()
        libs = libs + std_libs()
        lib_dirs = lib_dirs + std_lib_dirs()

        cppfilename = os.path.join(location, 'mod.cpp')
        with open(cppfilename, 'w') as cppfile:

            _logger.debug('Writing module C++ code to %s', cppfilename)

            cppfile.write(src_code)
            # Avoid gcc warning "no newline at end of file".
            if not src_code.endswith('\n'):
                cppfile.write('\n')

        if platform.python_implementation() == 'PyPy':
            suffix = '.' + get_lib_extension()

            dist_suffix = distutils.sysconfig.get_config_var("SO")
            if dist_suffix is not None and dist_suffix != '':
                suffix = dist_suffix

            filepath = '%s%s' % (module_name, suffix)
        else:
            filepath = '%s.%s' % (module_name, get_lib_extension())

        lib_filename = os.path.join(location, filepath)

        _logger.debug('Generating shared lib %s', lib_filename)
        cmd = [theano.config.cxx, get_gcc_shared_library_arg(), '-g']

        if config.cmodule.remove_gxx_opt:
            cmd.extend(p for p in preargs if not p.startswith('-O'))
        else:
            cmd.extend(preargs)
        # to support path that includes spaces, we need to wrap it with double quotes on Windows
        path_wrapper = "\"" if os.name == 'nt' else ""
        cmd.extend(['-I%s%s%s' % (path_wrapper, idir, path_wrapper) for idir in include_dirs])
        cmd.extend(['-L%s%s%s' % (path_wrapper, ldir, path_wrapper) for ldir in lib_dirs])
        if hide_symbols and sys.platform != 'win32':
            # This has been available since gcc 4.0 so we suppose it
            # is always available. We pass it here since it
            # significantly reduces the size of the symbol table for
            # the objects we want to share. This in turns leads to
            # improved loading times on most platforms (win32 is
            # different, as usual).
            cmd.append('-fvisibility=hidden')
        cmd.extend(['-o', '%s%s%s' % (path_wrapper, lib_filename, path_wrapper)])
        cmd.append('%s%s%s' % (path_wrapper, cppfilename, path_wrapper))
        cmd.extend(['-l%s' % l for l in libs])
        # print >> sys.stderr, 'COMPILING W CMD', cmd
        _logger.debug('Running cmd: %s', ' '.join(cmd))

        def print_command_line_error():
            # Print command line when a problem occurred.
            print(("Problem occurred during compilation with the "
                   "command line below:"), file=sys.stderr)
            print(' '.join(cmd), file=sys.stderr)

        try:
            p_out = output_subprocess_Popen(cmd)
            compile_stderr = decode(p_out[1])
        except Exception:
            # An exception can occur e.g. if `g++` is not found.
            print_command_line_error()
            raise

        status = p_out[2]

        if status:
            tf = tempfile.NamedTemporaryFile(
                mode='w',
                prefix='theano_compilation_error_',
                delete=False
            )
            # gcc put its messages to stderr, so we add ours now
            tf.write('===============================\n')
            for i, l in enumerate(src_code.split('\n')):
                tf.write('%05i\t%s\n' % (i + 1, l))
            tf.write('===============================\n')
            tf.write("Problem occurred during compilation with the "
                     "command line below:\n")
            tf.write(' '.join(cmd))
            # Print errors just below the command line.
            tf.write(compile_stderr)
            tf.close()
            print('\nYou can find the C code in this temporary file: ' + tf.name)
            not_found_libraries = re.findall('-l["."-_a-zA-Z0-9]*', compile_stderr)
            for nf_lib in not_found_libraries:
                print('library ' + nf_lib[2:] + ' is not found.')
                if re.search('-lPYTHON["."0-9]*', nf_lib, re.IGNORECASE):
                    py_string = re.search('-lpython["."0-9]*', nf_lib, re.IGNORECASE).group()[8:]
                    if py_string != '':
                        print(
                            'Check if package python-dev ' + py_string + ' or python-devel ' + py_string + ' is installed.'
                        )
                    else:
                        print(
                            'Check if package python-dev or python-devel is installed.'
                        )

            # We replace '\n' by '. ' in the error message because when Python
            # prints the exception, having '\n' in the text makes it more
            # difficult to read.
            raise Exception('Compilation failed (return status=%s): %s' %
                            (status, compile_stderr.replace('\n', '. ')))
        elif config.cmodule.compilation_warning and compile_stderr:
            # Print errors just below the command line.
            print(compile_stderr)

        if py_module:
            # touch the __init__ file
            open(os.path.join(location, "__init__.py"), 'w').close()
            assert os.path.isfile(lib_filename)
            return dlimport(lib_filename)

        Parameters
        ----------
        module_name : str
            This has been embedded in the src_code.
        src_code
            A complete c or c++ source listing for the module.
        location
            A pre-existing filesystem directory where the cpp file and .so will
            be written.
        include_dirs
            A list of include directory names (each gets prefixed with -I).
        lib_dirs
            A list of library search path directory names (each gets prefixed
            with -L).
        libs
            A list of libraries to link with (each gets prefixed with -l).
        preargs
            A list of extra compiler arguments.
        py_module
            If False, compile to a shared library, but do not import it as a
            Python module.
        hide_symbols
            If True (the default) all symbols will be hidden from the library
            symbol table (which means that other objects can't use them).

        Returns
        -------
        object
            Dynamically-imported python module of the compiled code (unless
            py_module is False, in that case returns None).

        
Parameters
module_name : str
This has been embedded in the src_code.
src_code
A complete c or c++ source listing for the module.
location
A pre-existing filesystem directory where the cpp file and .so will
be written.
include_dirs
A list of include directory names (each gets prefixed with -I).
lib_dirs
A list of library search path directory names (each gets prefixed
with -L).
libs
A list of libraries to link with (each gets prefixed with -l).
preargs
A list of extra compiler arguments.
py_module
If False, compile to a shared library, but do not import it as a
Python module.
hide_symbols
If True (the default) all symbols will be hidden from the library
symbol table (which means that other objects can't use them).

Returns

object
Dynamically-imported python module of the compiled code (unless
py_module is False, in that case returns None).
Parameters module_name : str This has been embedded in the src_code . src_code A complete c or c ++ source listing for the module . location A pre - existing filesystem directory where the cpp file and . so will be written . include_dirs A list of include directory names ( each gets prefixed with - I ) . lib_dirs A list of library search path directory names ( each gets prefixed with - L ) . libs A list of libraries to link with ( each gets prefixed with - l ) . preargs A list of extra compiler arguments . py_module If False compile to a shared library but do not import it as a Python module . hide_symbols If True ( the default ) all symbols will be hidden from the library symbol table ( which means that other objects can ' t use them ) . Returns object Dynamically - imported python module of the compiled code ( unless py_module is False in that case returns None ) .
def compile_str(module_name, src_code, location=None,
                    include_dirs=None, lib_dirs=None, libs=None,
                    preargs=None, py_module=True, hide_symbols=True):
        if not theano.config.cxx:
            raise MissingGXX("g++ not available! We can't compile c code.")
        if include_dirs is None:
            include_dirs = []
        if lib_dirs is None:
            lib_dirs = []
        if libs is None:
            libs = []
        if preargs is None:
            preargs = []
        include_dirs = [d for d in include_dirs if d]
        lib_dirs = [d for d in lib_dirs if d]
        include_dirs = include_dirs + std_include_dirs()
        libs = libs + std_libs()
        lib_dirs = lib_dirs + std_lib_dirs()
        cppfilename = os.path.join(location, 'mod.cpp')
        with open(cppfilename, 'w') as cppfile:
            _logger.debug('Writing module C++ code to %s', cppfilename)
            cppfile.write(src_code)
            if not src_code.endswith('\n'):
                cppfile.write('\n')
        if platform.python_implementation() == 'PyPy':
            suffix = '.' + get_lib_extension()
            dist_suffix = distutils.sysconfig.get_config_var("SO")
            if dist_suffix is not None and dist_suffix != '':
                suffix = dist_suffix
            filepath = '%s%s' % (module_name, suffix)
        else:
            filepath = '%s.%s' % (module_name, get_lib_extension())
        lib_filename = os.path.join(location, filepath)
        _logger.debug('Generating shared lib %s', lib_filename)
        cmd = [theano.config.cxx, get_gcc_shared_library_arg(), '-g']
        if config.cmodule.remove_gxx_opt:
            cmd.extend(p for p in preargs if not p.startswith('-O'))
        else:
            cmd.extend(preargs)
        path_wrapper = "\"" if os.name == 'nt' else ""
        cmd.extend(['-I%s%s%s' % (path_wrapper, idir, path_wrapper) for idir in include_dirs])
        cmd.extend(['-L%s%s%s' % (path_wrapper, ldir, path_wrapper) for ldir in lib_dirs])
        if hide_symbols and sys.platform != 'win32':
            cmd.append('-fvisibility=hidden')
        cmd.extend(['-o', '%s%s%s' % (path_wrapper, lib_filename, path_wrapper)])
        cmd.append('%s%s%s' % (path_wrapper, cppfilename, path_wrapper))
        cmd.extend(['-l%s' % l for l in libs])
        _logger.debug('Running cmd: %s', ' '.join(cmd))
        def print_command_line_error():
            print(("Problem occurred during compilation with the "
                   "command line below:"), file=sys.stderr)
            print(' '.join(cmd), file=sys.stderr)
        try:
            p_out = output_subprocess_Popen(cmd)
            compile_stderr = decode(p_out[1])
        except Exception:
            print_command_line_error()
            raise
        status = p_out[2]
        if status:
            tf = tempfile.NamedTemporaryFile(
                mode='w',
                prefix='theano_compilation_error_',
                delete=False
            )
            tf.write('===============================\n')
            for i, l in enumerate(src_code.split('\n')):
                tf.write('%05i\t%s\n' % (i + 1, l))
            tf.write('===============================\n')
            tf.write("Problem occurred during compilation with the "
                     "command line below:\n")
            tf.write(' '.join(cmd))
            tf.write(compile_stderr)
            tf.close()
            print('\nYou can find the C code in this temporary file: ' + tf.name)
            not_found_libraries = re.findall('-l["."-_a-zA-Z0-9]*', compile_stderr)
            for nf_lib in not_found_libraries:
                print('library ' + nf_lib[2:] + ' is not found.')
                if re.search('-lPYTHON["."0-9]*', nf_lib, re.IGNORECASE):
                    py_string = re.search('-lpython["."0-9]*', nf_lib, re.IGNORECASE).group()[8:]
                    if py_string != '':
                        print(
                            'Check if package python-dev ' + py_string + ' or python-devel ' + py_string + ' is installed.'
                        )
                    else:
                        print(
                            'Check if package python-dev or python-devel is installed.'
                        )
            raise Exception('Compilation failed (return status=%s): %s' %
                            (status, compile_stderr.replace('\n', '. ')))
        elif config.cmodule.compilation_warning and compile_stderr:
            print(compile_stderr)
        if py_module:
            open(os.path.join(location, "__init__.py"), 'w').close()
            assert os.path.isfile(lib_filename)
            return dlimport(lib_filename)
def compile_str ( module_name , src_code , location = None , include_dirs = None , lib_dirs = None , libs = None , preargs = None , py_module = True , hide_symbols = True ) : if not theano . config . cxx : raise MissingGXX ( "g++ not available! We can't compile c code." ) if include_dirs is None : include_dirs = [ ] if lib_dirs is None : lib_dirs = [ ] if libs is None : libs = [ ] if preargs is None : preargs = [ ] include_dirs = [ d for d in include_dirs if d ] lib_dirs = [ d for d in lib_dirs if d ] include_dirs = include_dirs + std_include_dirs ( ) libs = libs + std_libs ( ) lib_dirs = lib_dirs + std_lib_dirs ( ) cppfilename = os . path . join ( location , 'mod.cpp' ) with open ( cppfilename , 'w' ) as cppfile : _logger . debug ( 'Writing module C++ code to %s' , cppfilename ) cppfile . write ( src_code ) if not src_code . endswith ( '\n' ) : cppfile . write ( '\n' ) if platform . python_implementation ( ) == 'PyPy' : suffix = '.' + get_lib_extension ( ) dist_suffix = distutils . sysconfig . get_config_var ( "SO" ) if dist_suffix is not None and dist_suffix != '' : suffix = dist_suffix filepath = '%s%s' % ( module_name , suffix ) else : filepath = '%s.%s' % ( module_name , get_lib_extension ( ) ) lib_filename = os . path . join ( location , filepath ) _logger . debug ( 'Generating shared lib %s' , lib_filename ) cmd = [ theano . config . cxx , get_gcc_shared_library_arg ( ) , '-g' ] if config . cmodule . remove_gxx_opt : cmd . extend ( p for p in preargs if not p . startswith ( '-O' ) ) else : cmd . extend ( preargs ) path_wrapper = "\"" if os . name == 'nt' else "" cmd . extend ( [ '-I%s%s%s' % ( path_wrapper , idir , path_wrapper ) for idir in include_dirs ] ) cmd . extend ( [ '-L%s%s%s' % ( path_wrapper , ldir , path_wrapper ) for ldir in lib_dirs ] ) if hide_symbols and sys . platform != 'win32' : cmd . append ( '-fvisibility=hidden' ) cmd . extend ( [ '-o' , '%s%s%s' % ( path_wrapper , lib_filename , path_wrapper ) ] ) cmd . append ( '%s%s%s' % ( path_wrapper , cppfilename , path_wrapper ) ) cmd . extend ( [ '-l%s' % l for l in libs ] ) _logger . debug ( 'Running cmd: %s' , ' ' . join ( cmd ) ) def print_command_line_error ( ) : print ( ( "Problem occurred during compilation with the " "command line below:" ) , file = sys . stderr ) print ( ' ' . join ( cmd ) , file = sys . stderr ) try : p_out = output_subprocess_Popen ( cmd ) compile_stderr = decode ( p_out [ 1 ] ) except Exception : print_command_line_error ( ) raise status = p_out [ 2 ] if status : tf = tempfile . NamedTemporaryFile ( mode = 'w' , prefix = 'theano_compilation_error_' , delete = False ) tf . write ( '===============================\n' ) for i , l in enumerate ( src_code . split ( '\n' ) ) : tf . write ( '%05i\t%s\n' % ( i + 1 , l ) ) tf . write ( '===============================\n' ) tf . write ( "Problem occurred during compilation with the " "command line below:\n" ) tf . write ( ' ' . join ( cmd ) ) tf . write ( compile_stderr ) tf . close ( ) print ( '\nYou can find the C code in this temporary file: ' + tf . name ) not_found_libraries = re . findall ( '-l["."-_a-zA-Z0-9]*' , compile_stderr ) for nf_lib in not_found_libraries : print ( 'library ' + nf_lib [ 2 : ] + ' is not found.' ) if re . search ( '-lPYTHON["."0-9]*' , nf_lib , re . IGNORECASE ) : py_string = re . search ( '-lpython["."0-9]*' , nf_lib , re . IGNORECASE ) . group ( ) [ 8 : ] if py_string != '' : print ( 'Check if package python-dev ' + py_string + ' or python-devel ' + py_string + ' is installed.' ) else : print ( 'Check if package python-dev or python-devel is installed.' ) raise Exception ( 'Compilation failed (return status=%s): %s' % ( status , compile_stderr . replace ( '\n' , '. ' ) ) ) elif config . cmodule . compilation_warning and compile_stderr : print ( compile_stderr ) if py_module : open ( os . path . join ( location , "__init__.py" ) , 'w' ) . close ( ) assert os . path . isfile ( lib_filename ) return dlimport ( lib_filename )
Parameters
module_name : str
This has been embedded in the src_code.
Parameters module_name : str This has been embedded in the src_code .
"""
        Parameters
        ----------
        module_name : str
            This has been embedded in the src_code.
        src_code
            A complete c or c++ source listing for the module.
        location
            A pre-existing filesystem directory where the cpp file and .so will
            be written.
        include_dirs
            A list of include directory names (each gets prefixed with -I).
        lib_dirs
            A list of library search path directory names (each gets prefixed
            with -L).
        libs
            A list of libraries to link with (each gets prefixed with -l).
        preargs
            A list of extra compiler arguments.
        py_module
            If False, compile to a shared library, but do not import it as a
            Python module.
        hide_symbols
            If True (the default) all symbols will be hidden from the library
            symbol table (which means that other objects can't use them).

        Returns
        -------
        object
            Dynamically-imported python module of the compiled code (unless
            py_module is False, in that case returns None).

        """ # TODO: Do not do the dlimport in this function # Remove empty string directory # Avoid gcc warning "no newline at end of file". # to support path that includes spaces, we need to wrap it with double quotes on Windows # This has been available since gcc 4.0 so we suppose it # is always available. We pass it here since it # significantly reduces the size of the symbol table for # the objects we want to share. This in turns leads to # improved loading times on most platforms (win32 is # different, as usual). # print >> sys.stderr, 'COMPILING W CMD', cmd # Print command line when a problem occurred. # An exception can occur e.g. if `g++` is not found. # gcc put its messages to stderr, so we add ours now # Print errors just below the command line. # We replace '\n' by '. ' in the error message because when Python # prints the exception, having '\n' in the text makes it more # difficult to read. # Print errors just below the command line. # touch the __init__ file
module_name
src_code
location
include_dirs
lib_dirs
libs
preargs
py_module
hide_symbols