def list_user_platforms():
    platforms = platform_list()
    print
    print core.bold_string("User platforms:")
    print
    for platform in platforms:
        if ( platform.group == 'user' ):
            if ( platform.current ):
                print "  %d) %s%s%s%s" %(platform.id,platform.family,os.sep,platform.name,core.red_string("*"))
            else:
                print "  %d) %s%s%s" %(platform.id,platform.family,os.sep,platform.name)
def list_user_toolchains():
    toolchains = toolchain_list()
    print
    print core.bold_string("User toolchains:")
    print
    for toolchain in toolchains:
        if ( toolchain.group == 'user' ):
            if ( toolchain.current ):
                print "  %d) %s%s%s%s" %(toolchain.id,toolchain.family,os.sep,toolchain.name,core.red_string("*"))
            else:
                print "  %d) %s%s%s" %(toolchain.id,toolchain.family,os.sep,toolchain.name)
def set_debug_mode(mode):
    if not validate_mode(mode):
        return 1
    if not os.path.exists(core.rosconfig_cmake()):
        create_vanilla_config()
    for line in fileinput.input(core.rosconfig_cmake(),inplace=1,mode='r'):
        if ( line.find("set(ROS_BUILD_TYPE") != -1):
            line = "  set(ROS_BUILD_TYPE "+mode+")"
        if line.endswith("\n"):
            line = line[:-1]
        print line
    print core.bold_string("New build mode: ") + mode
    return 0
def show_current_build_mode():
    pretext = core.bold_string("Current build mode: ")
    found = False
    if os.path.exists(core.rosconfig_cmake()):
        for line in fileinput.input(core.rosconfig_cmake(),mode='r'):
            if (line.find("set(ROS_BUILD_TYPE Debug") != -1):
                sys.stdout.write(pretext + "Debug\n")
                found = True
                break
            elif (line.find("set(ROS_BUILD_TYPE Release") != -1):
                sys.stdout.write(pretext + "Release\n")
                found = True
                break
            elif (line.find("set(ROS_BUILD_TYPE RelWithDebInfo") != -1):
                sys.stdout.write(pretext + "RelWithDebInfo\n")
                found = True
                break
            elif (line.find("set(ROS_BUILD_TYPE MinSizeRel") != -1):
                sys.stdout.write(pretext + "MinSizeRel\n")
                found = True
                break
        if not found:
            print core.red_string("Unknown") + "(please check ROS_ROOT/rosconfig.cmake for problems)."
    else:
        print pretext + "RelWithDebInfo"
def show_current_platform():
    '''
    Print the identity of the currently configured platform:
      - checks eros/user platform libraries for a match
      - if not eros/user platform, checks if platform configured, but unknown
      - otherwise prints none 
    '''
    pretext = core.bold_string("Current platform: ")
    platforms = platform_list()
    found = False
    for platform in platforms:
        if ( platform.current ):
            found = True
            current_platform = platform
    if ( found ):
        print pretext + current_platform.family + os.sep + current_platform.name
    else:
        if ( os.path.exists(core.rosconfig_cmake()) ):
            print pretext + "unknown"
        else:
            print pretext + "none"
def show_current_toolchain():
    '''
    Print the identity of the currently configured toolchain:
      - checks eros/user toolchain libraries for a match
      - if not eros/user toolchain, checks if toolchain configured, but unknown
      - otherwise prints none 
    '''
    pretext = core.bold_string("Current toolchain: ")
    toolchains = toolchain_list()
    found = False
    for toolchain in toolchains:
        if ( toolchain.current ):
            found = True
            current_toolchain = toolchain
    if ( found ):
        print pretext + current_toolchain.family + os.sep + current_toolchain.name
    else:
        if ( os.path.exists(core.rostoolchain_cmake()) ):
            print pretext + "unknown"
        else:
            print pretext + "none"
def print_valid_modes():
    print core.bold_string("Available Build Modes")
    print "  Debug"
    print "  RelWithDebInfo"
    print "  Release"
    print "  MinSizeRel"
def show_current_install_prefix():
    prefix = get_install_prefix()
    if ( prefix is not None ):
        print core.bold_string("Current install prefix:") + " " + prefix
    else:
        print core.bold_string("Current install prefix:") + " not configured (using cmake default)."
def create_platform():
    '''
    Create a platform configuration.
    '''
    print
    print core.bold_string("  Creating a User-Defined Eros Platform Configuration")
    print
    print "This is an interactive assistant to help define a new eros style cmake platform"
    print "configuration. It will prompt you for a few custom strings and then save the"
    print "configured platform module in ROS_HOME/eros/platforms (~/.ros/eros/platforms on linux)."
    print "It can then be listed and selected in the same way as as a regular eros platform"
    print "configuration."
    print
    print core.bold_string("  Platform Family")
    print 
    print "  This is simply a convenience variable that helps sort platforms in the eros"
    print "  and user-defined libraries. Common examples include: intel, arm etc."
    print
    platform_family = raw_input('  Enter a string for the platform family [custom]: ')
    if ( platform_family == '' ):
        platform_family = 'custom'
    print
    print core.bold_string("  Platform Name")
    print 
    print "  This is unique identifier usually denoting the specific cpu that it represents"
    print "  and will be the name of the resulting cmake file. Common examples include: "
    print "  core2, arm1176jzf-s etc."
    print
    platform_name = raw_input('  Enter a string for the platform name [generic]: ')
    print
    print core.bold_string("  Platform Compile flags")
    print 
    print "  A string containing any extra compile flags to be used for this configuration."
    print "  e.g. '-march=core2 -sse4. This is in addition to the default ros flags."
    print 
    platform_compile_flags = raw_input('  Enter a string for the platform compile flags []: ')
    print
    print core.bold_string("  Platform Link flags")
    print 
    print "  A string containing any extra link flags to be used for this configuration."
    print "  e.g. '-Wl,--as-needed'. This is in addition to the default ros flags."
    print 
    platform_link_flags = raw_input('  Enter a string for the platform link flags []: ')
    
    platform_template = open(eros_platform_template()).read()
    platform_template = platform_template.replace('${platform_family}',platform_family)
    platform_template = platform_template.replace('${platform_name}',platform_name)
    platform_template = platform_template.replace('${platform_compile_flags}',platform_compile_flags)
    platform_template = platform_template.replace('${platform_link_flags}',platform_link_flags)
    #print platform_template
    user_defined_platform_pathname = os.path.join(user_platform_dir(),platform_family,platform_name+'.cmake')
    if ( os.path.exists(user_defined_platform_pathname) ):
        print core.red_string("  Aborting, this platform configuration already exists (use --delete to remove).")
        return 1
    if not os.path.exists( os.path.dirname(user_defined_platform_pathname) ): # Make sure the dir exists before we open for writing
        os.makedirs(os.path.dirname(user_defined_platform_pathname))
    f = open(user_defined_platform_pathname, 'w')
    f.write(platform_template)
    rosconfig_tail = open(eros_rosconfig_tail()).read()
    f.write(rosconfig_tail)
    f.close()
    print
    print core.bold_string("Platform Finalised")
    print "-- Family: %s" %platform_family
    print "-- Name: %s" %platform_name
    print "-- CFlags: %s" %platform_compile_flags
    print "-- LFlags: %s" %platform_link_flags
    print "-- File: %s" %user_defined_platform_pathname
    print
    check_install_prefix()
def create_toolchain():
    '''
    Create a cross-compiler cmake configuration. Note: hardwired for gcc
    cross compiler configurations at this point in time - is there even a use
    case that is different right now?
    '''
    print
    print core.bold_string("  Creating a User-Defined Eros Toolchain")
    print
    print "This is an interactive assistant to help define a new eros style cmake toolchain."
    print "It will prompt you for a few custom strings and then save the configured toolchain"
    print "in ROS_HOME/eros/toolchains (~/.ros/eros/toolchains on linux platforms). It can then be"
    print "listed and selected in the same way as as a regular eros toolchain."
    print
    print core.bold_string("  Toolchain Family")
    print 
    print "  This is simply a convenience variable that helps sort toolchains in the eros"
    print "  and user-defined libraries. Common examples include: crossdev, ubuntu,"
    print "  openembedded, etc."
    print
    toolchain_family = raw_input('  Enter a string for the toolchain family [custom]: ')
    if ( toolchain_family == '' ):
        toolchain_family = 'custom'
    print
    print core.bold_string("  Toolchain Tuple")
    print 
    print "  This is essential so that cmake can find the gcc cross-compiler. The toolchain"
    print "  tuple should match the prefix to your toolchain's cross-compilers, e.g. if your"
    print "  cross-compiler is i686-pc-linux-gnu-gcc, then the tuple is i686-pc-linux-gnu."
    print "  Compilers need to be in your system's PATH."
    print
    toolchain_tuple = raw_input('  Enter a string for the toolchain tuple: ')
    print
    print core.bold_string("  Toolchain Sysroot")
    print 
    print "  This is the root directory from which system headers and libraries can be found."
    print "  e.g. if toolchain pthreads header is /usr/i686-pc-linux-gnu/usr/include/pthread.h"
    print "  then your sysroot would be /usr/i686-pc-linux-gnu."
    print 
    toolchain_sysroot_default = "/usr/" + toolchain_tuple
    toolchain_sysroot = raw_input("  Enter a string for the toolchain sysroot [" + toolchain_sysroot_default + "]: ")
    if ( toolchain_sysroot == ''):
        toolchain_sysroot = toolchain_sysroot_default
    print
    print core.bold_string("  Toolchain Install Prefix")
    print 
    print "  This is the where your headers and libraries will get installed."
    print "  e.g. if your toolchain sysroot is /usr/i686-pc-linux-gnu/ then typically"
    print "  your install prefix will be /usr/i686-pc-linux-gnu/usr"
    print 
    toolchain_install_prefix_default = toolchain_sysroot + "/usr/"
    toolchain_install_prefix = raw_input("  Enter a string for the toolchain install prefix [" + toolchain_install_prefix_default + "]: ")
    if ( toolchain_install_prefix == ''):
        toolchain_install_prefix = toolchain_install_prefix_default
    print
    
    toolchain_template = open(eros_toolchain_template()).read()
    toolchain_template = toolchain_template.replace('${toolchain_family}',toolchain_family)
    toolchain_template = toolchain_template.replace('${toolchain_tuple}',toolchain_tuple)
    toolchain_template = toolchain_template.replace('${toolchain_sysroot}',toolchain_sysroot)
    toolchain_template = toolchain_template.replace('${toolchain_install_prefix}',toolchain_install_prefix)
    #print toolchain_template
    user_defined_toolchain_pathname = os.path.join(user_toolchain_dir(),toolchain_family,toolchain_tuple+'.cmake')
    if ( os.path.exists(user_defined_toolchain_pathname) ):
        print core.red_string("  Aborting, this toolchain configuration already exists (use --delete to remove).")
        return 1
    if not os.path.exists( os.path.dirname(user_defined_toolchain_pathname) ): # Make sure the dir exists before we open for writing
        os.makedirs(os.path.dirname(user_defined_toolchain_pathname))
    f = open(user_defined_toolchain_pathname, 'w')
    f.write(toolchain_template)
    f.close()
    print core.bold_string("Toolchain Finalised")
    print
    print "-- Family: %s" %toolchain_family
    print "-- Tuple: %s" %toolchain_tuple
    print "-- Sysroot: %s" %toolchain_sysroot
    print "-- Install Prefix: %s" %toolchain_install_prefix
    print "-- File: %s" %user_defined_toolchain_pathname
    print