Exemplo n.º 1
0
def ld_flags():
    r"""Get the linker flags necessary for calling functions/classes from
    the interface library in a C or C++ program.

    Returns:
        list: The necessary library linking flags.

    """
    return ' '.join(GCCModelDriver.get_flags()[1])
Exemplo n.º 2
0
def cc_flags():
    r"""Get the compiler flags necessary for including the interface
    library in a C or C++ program.

    Returns:
        list: The necessary compiler flags and preprocessor definitions.

    """
    return ' '.join(GCCModelDriver.get_flags()[0])
Exemplo n.º 3
0
def setup_environ(compile_flags=[], linker_flags=[]):
    r"""Set environment variables CISCCFLAGS and CISLDFLAGS.

    Args:
        compile_flags (list, optional): Additional compile flags that
            should be set. Defaults to [].
        linker_flags (list, optional): Additional linker flags that
            should be set. Defaults to [].

    """
    _compile_flags, _linker_flags = GCCModelDriver.get_flags()
    os.environ['CISCCFLAGS'] = ' '.join(compile_flags + _compile_flags)
    os.environ['CISLDFLAGS'] = ' '.join(linker_flags + _linker_flags)
Exemplo n.º 4
0
def ciscc():
    r"""Compile C/C++ program."""
    # prog = sys.argv[0].split(os.path.sep)[-1]
    src = sys.argv[1:]
    out = GCCModelDriver.do_compile(src)
    print("executable: %s" % out)
Exemplo n.º 5
0
def create_include(fname, target, compile_flags=None, linker_flags=None):
    r"""Create CMakeList include file with necessary includes,
    definitions, and linker flags.

    Args:
        fname (str): File where the include file should be saved.
        target (str): Target that links should be added to.
        compile_flags (list, optional): Additional compile flags that
            should be set. Defaults to [].
        linker_flags (list, optional): Additional linker flags that
            should be set. Defaults to [].

    """
    if compile_flags is None:
        compile_flags = []
    if linker_flags is None:
        linker_flags = []
    _compile_flags, _linker_flags = GCCModelDriver.get_flags(for_cmake=True)
    if platform._is_win:  # pragma: windows
        assert(os.path.isfile(_regex_win32_lib))
        _linker_flags.append(_regex_win32_lib)
    compile_flags += _compile_flags
    linker_flags += _linker_flags
    lines = []
    var_count = 0
    for x in compile_flags:
        if x.startswith('-D'):
            lines.append('ADD_DEFINITIONS(%s)' % x)
        elif x.startswith('-I'):
            xdir = x.split('-I', 1)[-1]
            if platform._is_win:  # pragma: windows
                xdir = xdir.replace('\\', re.escape('\\'))
            lines.append('INCLUDE_DIRECTORIES(%s)' % xdir)
        elif x.startswith('-') or x.startswith('/'):
            lines.append('ADD_DEFINITIONS(%s)' % x)
        else:
            raise ValueError("Could not parse compiler flag '%s'." % x)
    for x in linker_flags:
        if x.startswith('-l'):
            lines.append('TARGET_LINK_LIBRARIES(%s %s)' % (target, x))
        elif x.startswith('-L'):
            libdir = x.split('-L')[-1]
            if platform._is_win:
                libdir = libdir.replace('\\', re.escape('\\'))
            lines.append('LINK_DIRECTORIES(%s)' % libdir)
        elif x.startswith('/LIBPATH:'):  # pragma: windows
            libdir = x.split('/LIBPATH:')[-1]
            if '"' in libdir:
                libdir = libdir.split('"')[1]
            if platform._is_win:
                libdir = libdir.replace('\\', re.escape('\\'))
            lines.append('LINK_DIRECTORIES(%s)' % libdir)
        elif os.path.isfile(x):
            xd, xf = os.path.split(x)
            xl, xe = os.path.splitext(xf)
            if xe in ['.so', '.dll']:
                lines.append('ADD_LIBRARY(%s SHARED IMPORTED)' % xl)
            else:
                lines.append('ADD_LIBRARY(%s STATIC IMPORTED)' % xl)
            lines.append('SET_TARGET_PROPERTIES(')
            lines.append('    %s PROPERTIES' % xl)
            # lines.append('    PROPERTIES LINKER_LANGUAGE CXX')
            if platform._is_win:
                lines.append('    IMPORTED_LOCATION %s)' %
                             x.replace('\\', re.escape('\\')))
            else:
                lines.append('    IMPORTED_LOCATION %s)' % x)
            lines.append('TARGET_LINK_LIBRARIES(%s %s)' % (target, xl))
            # lines.append('FIND_LIBRARY(VAR%d %s HINTS %s)' % (var_count, xf, xd))
            # lines.append('TARGET_LINK_LIBRARIES(%s ${VAR%s})' % (target, var_count))
            var_count += 1
        elif x.startswith('-') or x.startswith('/'):
            raise ValueError("Could not parse linker flag '%s'." % x)
        else:
            lines.append('TARGET_LINK_LIBRARIES(%s %s)' % (target, x))
    if fname is None:
        return lines
    else:
        if os.path.isfile(fname):  # pragma: debug
            os.remove(fname)
        with open(fname, 'w') as fd:
            fd.write('\n'.join(lines))