Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

/** -*- mode: c; tabs: 4 -*- *************************************************
* Module name   : gcc.c
* Module type   : CMDFILE source file
* Environment(s): n/a
*
* Description:
     gcc support

Returns:
     vlibgcc,
        Retrieve the VPATH from the specified gcc compiler.
*
* Version   Who      Date       Description
            APY      17/05/04   Created
*
* $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/gcc.c,v $
* $Revision: 1.3 $ $Date: 2004/10/08 03:07:37 $ $State: Exp $
* $Author: ayoung $ $Locker:  $
*...........................................................................*/

/*
 *  Include Files
 *  ---------------
 */
#include <ctype.h>
#include <unistd.h>
#include "cmdfile.h"

#define INSTALL         0
#define PROGRAMS        1
#define LIBRARIES       2

static void             gcc_search_dirs( const char *program );

static int              libpath_len;            /* library search path */
static char             libpath_buf[ 4*1024 ];  

static int              incpath_len;            /* include search path */
static char             incpath_buf[ 4*1024 ];  

extern char             vpath_sep;
extern const char      *vpath_lib_default;

int
do_vlibgcc( String_t *str, const char *s )
{
    static  int done;
    char    program[ PATH_MAX ];
    int     len;

    if (expected(s++, ',') > 0 &&
            (len = pathargument(s, program, PATH_MAX)) > 0)
    {
        if (done == 0)                          /* one-shot search */
            gcc_search_dirs( program ), done = 1;

        if (libpath_len > 0)
            vpath_lib_default = libpath_buf;    /* (re) assign */

        verbose( " vlibgcc(%s) = %s", program, libpath_buf );
        return (1+len);
    }
    return (-1);
}


static void
gcc_search_dirs( const char *program )
{
    char    buffer[ PATH_MAX*2 ];
    char    tname[32];
    int     tfd;
    int     ret;

    (void) strcpy( tname, TMPNAME );
    if ((tfd = mkstemp( tname )) == -1)
        fatalerr("Cannot build temp file name \"%s\" : %d (%s)",
            tname, errno, strerror(errno));
    close( tfd );

#if defined(unix)
    snprintf( buffer, sizeof(buffer), "%s -print-search-dirs >%s", program, tname );
#else
        sprintf( buffer, "%s -print-search-dirs >%s", program, tname );
#endif
    if ((ret = system( buffer )) == -1)
        fatalerr("Cannot exec \"%s\" : %d (%s)",
            buffer, errno, strerror(errno));
    if (vflg >= 2)
        verbose( " system(%s) : %d", buffer, ret );

    if (ret == 0)
    {
        FILE    *f;
        int     ch;

        if ((f = fopen( tname, "r" )) != NULL)
        {
            while (fscanf( f, "%s:", buffer ) == 1)
            {
                if ((ch = fgetc(f)) == ' ')
                {
                    if ( strcmp(buffer, "install:") == 0 )
                    {                           /* installation path */
                    }

                    else if ( strcmp(buffer, "programs:") == 0)
                    {                           /* program search path */
                    }

                    else if ( strcmp(buffer, "libraries:") == 0 )
                    {                           /* libraries search path */
                        char *p = libpath_buf + libpath_len;
                        
                        do {
                            if (fscanf( f, "%[^:\n]", buffer ) == 1)
                            {                   /* foreach path */
                                const char *b = (buffer[0] == '=' ? buffer+1 : buffer);
                                int len = strlen(b);

                                verbose( "   adding vpath(%s)", b );

                                if ((libpath_len += len + 1) >= sizeof(libpath_buf))
                                    fatalerr( "gcc vpath buffer overflow (>%dKBytes)",
                                        sizeof(libpath_buf)/1024 );

                                (void) memcpy( p, (const char *)b, len );
                                p += len;
                                *p++ = vpath_sep;
                                *p = '\0';
                            }
                        } while ((ch = fgetc(f)) == ':');
                    }
                }

                                                /* eat trailing characters */
                while (!feof(f) && !ferror(f) && ch != '\n')
                    ch = fgetc(f);
            }

            fclose(f);
        }    
    }

    if (!tflg)
        remove(tname);
}