Subversion Repositories DevTools

Rev

Rev 369 | Rev 4003 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

# -*- mode: perl; tabs: 8; indent-width: 4; show-tabs: yes; -*-
#
# Module name   : GCC
# Module type   : Makefile system
# Compiler(s)   : ANSI C
# Environment(s): GCC
#
# Description:
#       GCC C/C++ toolset
#
#............................................................................#

use strict;
use warnings;

##############################################################################
#   Configuration information
#   Cross reference from CrossCompiler Alias to actual paths
#   Structure:
#   Hash reference: Array of toolchain information
#       Mandatory
#          ROOT             => Root of compiler
#          BASE             => Base for binaries
#       Optional
#          CC_OPTS          => Additonal Compiler options
#          UNCONTROLLED     => Boolean to create warning

my %ToolsetConfig = (

    'i386-unknown-linux-gnu'    => {
        ROOT => '/opt/crosstool/gcc-4.1.1-glibc-2.5/i586-unknown-linux-gnu',
        BASE => 'bin/i586-unknown-linux-gnu-',
    },

    'arm-9tdmi-linux-gnu'       => {
        ROOT => '/opt/crosstool/gcc-4.1.1-glibc-2.5/arm-9tdmi-linux-gnu',
        BASE => 'bin/arm-9tdmi-linux-gnu-'
        },

    'powerpc-603e-linux-gnu'    => {
       ROOT => '/opt/crosstool/gcc-4.1.1-glibc-2.5/powerpc-603e-linux-gnu',
       BASE => 'bin/powerpc-603e-linux-gnu-'
    },

    'arm-926ejs-linux-gnueabi'  => {
       ROOT => '/opt/crosstool/gcc-4.4.3-glibc-2.9/arm-926ejs-linux-gnueabi',
       BASE => 'bin/arm-926ejs-linux-gnueabi-',
       CC_OPTS => '-Wno-psabi',
    },

    #
    #   Old (not to be used) version of the embedded toolchain
    #   This was depricated in favor of gcc-4.1.1-glibc-2.5
    #   It is not possible to reproduce old packages using the old compiler
    #   This is a known issue
    #
    'i386-unknown-linux-gnu.glibc-2.3.2' => {
       ROOT => '/opt/crosstool/gcc-4.1.0-glibc-2.3.2/i386-unknown-linux-gnu',
       BASE => 'bin/i386-unknown-linux-gnu-'
    },

    'arm-9tdmi-linux-gnu.glibc-2.3.2' => {
       ROOT => '/opt/crosstool/gcc-4.1.0-glibc-2.3.2/arm-9tdmi-linux-gnu',
       BASE => 'bin/arm-9tdmi-linux-gnu-'
    },

    #
    #   Not too sure where this is used
    #
    'armv4l-unknown-linux-gcc' => {
       ROOT => '/opt/host/armv4l',
       BASE => 'bin/armv4l-unknown-linux-'
    },

    #
    #   The compiler for the current local machine
    #
    'i386-unknown-linux-gcc' => {
       ROOT => '/usr',
       BASE => 'bin/',
       UNCONTROLLED => 1,
    },
);

#
#   Cross reference from GCCTarget to GBE_MACHTYPE for which it can
#   build using the 'native gcc'. This is NOT the preferred mode of operation
#   as the compiler is not as controlled as the cross compilers.
#
my %NativeCompilers = (
    'Linux i386'       => 'linux_i386',
    );

##############################################################################
#   ToolsetInit()
#       Runtime initialisation
#
##############################################################################

ToolsetInit();

sub ToolsetInit
{
    my( $GCCTarget, $GCCToolchain, $GCCRoot, $GCCBin, $GCCAr, $GCCFlags, $GCCObjCopy );
    my( $arg_alias, $tools_found );

#.. Toolset configuration
#
    $::ScmToolsetVersion = "1.0.0";             # our version
    $::ScmToolsetGenerate = 0;                  # GEN generate optional
    $::ScmToolsetCompilerPath = 1;              # Exports Compiler path to makefile via SCM_COMPILERPATH
    $::ScmToolsetProgDependancies = 0;          # handle Prog dependancies myself
    $::ScmToolsetSoName = 1;                    # Shared library supports SoName

#.. Standard.rul requirements
#
    $::s = "asm";
    $::o = "o";
    $::so = "so";
    $::a = "a";
    $::exe = "";

#.. Parse arguments
#
    foreach $_ ( @::ScmToolsetArgs ) {
        if (/^--Target=(.*)/) {                 # OS Version
            $GCCTarget = "$1";

        } elsif (/^--CrossAlias=(.*)/) {        # CrossCompiler target-alias
            $arg_alias = $1;

        } else {
            Message( "gcc toolset: unknown option $_ -- ignored\n" );
        }
    }

    foreach $_ ( @::ScmPlatformArgs ) {
        if (/^--product=(.*)/) {                # GBE product

        } else {
            Message( "gcc toolset: unknown platform argument $_ -- ignored\n" );
        }
    }

    Error ("TOOLSET/gcc - Target undefined" )
        unless ($GCCTarget);

#.. Cross compile support
#
#   Toolchain=root,[bin]
#
    if ( $arg_alias )
    {
        if ( exists $ToolsetConfig{ $arg_alias } )
        {
            $GCCToolchain = $ToolsetConfig{ $arg_alias };
            $tools_found = (-d $GCCToolchain->{ROOT});
            Warning ("gcc toolset: CrossPlatform toolchain not found for: $arg_alias",
                     "Path: $GCCToolchain->{ROOT}" ) unless $tools_found;
        }
        else
        {
            Error("gcc toolset: CrossPlatform Alias not configured: $arg_alias");
        }

        Warning ("Uncontrolled toolchain used: $arg_alias")
            if ( exists($GCCToolchain->{UNCONTROLLED}) && $GCCToolchain->{UNCONTROLLED} );
    }

    #
    #   If no Cross compiler toolchain is found (preferred method), then attempt
    #   to match a native compiler. Only known targets allow a native build
    #
    unless ( $tools_found )
    {
        if ( exists ( $NativeCompilers{$GCCTarget} ))
        {
            if ( $NativeCompilers{$GCCTarget} eq $::GBE_MACHTYPE )
            {
                $tools_found = 1;
                $GCCToolchain = undef;
            }
        }
    }

    #
    #   Must have a toolset by now, either a cross compiler or Native
    #
    Error ("gcc toolset: Toolchain not found for: $GCCTarget" )
        unless ( $tools_found );


    if ( defined $GCCToolchain )
    {
        #
        #   Parse GCCToolchain. Potential parts to create
        #       GCCRoot     - Location to find the effective /usr/include directory
        #       GCCBin      - Path to the gcc executable
        #       GCCAr       - Path to the ar executable
        #       GCCFlags    - Additional compiler flags
        #
        $GCCRoot = $GCCToolchain->{ROOT};
        $GCCBin = '${GCC_ROOT}/' . $GCCToolchain->{BASE} . 'gcc';
        $GCCAr =  '${GCC_ROOT}/' . $GCCToolchain->{BASE} . 'ar';
        $GCCObjCopy =  '${GCC_ROOT}/' . $GCCToolchain->{BASE} . 'objcopy';
        $GCCFlags = $GCCToolchain->{CC_OPTS};
    }
    else
    {
        $GCCRoot = '/usr';
        $GCCBin = 'gcc';
        $GCCAr = 'ar';
        $GCCObjCopy =  'objcopy';
    }

#.. Define GCC environment
#
    PlatformDefine( "
#################################################
# GCC toolchain definitions 
#
#..");
    PlatformDefine( "GCC_TARGET         := $GCCTarget" );
    PlatformDefine( "GCC_ROOT           := $GCCRoot" );
    PlatformDefine( "GCC_CC             := $GCCBin" );
    PlatformDefine( "GCC_AR             := $GCCAr" );
    PlatformDefine( "GCC_OBJCOPY        := $GCCObjCopy" );
    PlatformDefine( "" );

    PlatformDefine( "GCC_CFLAGS         := $GCCFlags" ) if defined $GCCFlags;


    #
    #   Required since this toolset advertises: ScmToolsetCompilerPath
    #
    PlatformDefine( "SCM_COMPILERPATH   := \$\{GCC_CC\}" );
    PlatformDefine( "" );


#.. Piece the world together
#
    Init( "gcc" );
    ToolsetDefines( "gcc.def" );
    ToolsetRules( "gcc.rul" );
    ToolsetRules( "standard.rul" );

#   Create a standard data structure
#   This is a hash of hashes
#       The first hash is keyed by CompileOption keyword
#       The second hash contains pairs of values to set or remove
#
    %::ScmToolsetCompilerOptions =
    (
        #
        #   Control the thread model to use
        #   This will affect the compiler options and the linker options
        #
        'staticprogs'        => { 'STATIC_PROGS' , '1' },      # Progams link staticlly
        'no_staticprogs'     => { 'STATIC_PROGS' , undef },    # Default
    );

    #
    #   Set default options
    #
    $::ScmCompilerOpts{'STATIC_PROGS'} = undef;
}


###############################################################################
#   ToolsetCC( $source, $obj, \@args )
#       This subroutine takes the user options and builds the rule(s)
#       required to compile the source file 'source' to 'obj'
#
###############################################################################

sub ToolsetCC
{
    my( $source, $obj, $pArgs ) = @_;
    my( $cflags, $file ) = "";

    foreach $_ ( @$pArgs ) {
        if (/--Shared$/) {                      # Building a 'shared' object
            $cflags  = "$cflags \$(SHCFLAGS)";
        } else {
            Message( "CC: unknown option $_ -- ignored\n" );
        }
    }
                   
    MakePrint( "\n\t\$(CC)\n" );
    if ( $cflags )
    {                                           # object specific CFLAGS
        MakePadded( 4, "\$(OBJDIR)/$obj.$::o:" );
        MakePrint( "\tCFLAGS +=$cflags\n" );
    }

    $file = StripExt( $obj );                   # Metric working file
    ToolsetGenerate( "\$(OBJDIR)/$file.met" );
}


###############################################################################
#   ToolsetCCDepend( $depend, \@sources )
#       This subroutine takes the user options and builds the
#       rule(s) required to build the dependencies for the source
#       files 'sources' to 'depend'.
#
###############################################################################

sub ToolsetCCDepend
{
    MakePrint( "\t\$(CCDEPEND)\n" );
}


###############################################################################
#   ToolsetCXX( $source, $obj, \@args )
#       This subroutine takes the user options and builds the rule(s)
#       required to compile the source file 'source' to 'obj'
#
###############################################################################

sub ToolsetCXX
{
    my( $source, $obj, $pArgs ) = @_;
    my( $cflags, $file ) = "";

    foreach $_ ( @$pArgs ) {
        if (/--Shared$/) {                      # Building a 'shared' object
            $cflags  = "$cflags \$(SHCXXFLAGS)";
        } else {
            Message( "CXX: unknown option $_ -- ignored\n" );
        }
    }
                   
    MakePrint( "\n\t\$(CXX)\n" );
    if ( $cflags )
    {                                           # object specific CFLAGS
        MakePadded( 4, "\$(OBJDIR)/$obj.$::o:" );
        MakePrint( "\tCXXFLAGS +=$cflags\n" );
    }

    $file = StripExt( $obj );                   # Metric working file
    ToolsetGenerate( "\$(OBJDIR)/$file.met" );
}


###############################################################################
#   ToolsetCXXDepend( $depend, \@sources )
#       This subroutine takes the user options and builds the
#       rule(s) required to build the dependencies for the source
#       files 'sources' to 'depend'.
#
###############################################################################

sub ToolsetCXXDepend
{
    ToolsetCCDepend();
}


###############################################################################
#   ToolsetAS( $source, $obj, \@args )
#       This subroutine takes the user options and builds the rule(s)
#       required to compile the source file 'source' to 'obj'
#
###############################################################################

sub ToolsetAS
{
    MakePrint( "\n\t\$(AS)\n" );
}

sub ToolsetASDepend
{
}


###############################################################################
#   ToolsetAR( $name, \@args, \@objs )
#       This subroutine takes the user options and builds the rules
#       required to build the library 'name'.
#
#   Arguments:
#       n/a
#
#   Output:
#       $(BINDIR)/name$.${a}:   .... ]
#           $(AR)
#
#       name_ld += ...  Linker command file
#           :
#
#       name_dp += ...  Dependency list
#           :
#
###############################################################################

sub ToolsetAR
{
    my( $name, $pArgs, $pObjs ) = @_;

#.. Parse arguments
#
    foreach $_ ( @$pArgs )
    {
        Message( "AR: unknown option $_ -- ignored\n" );
    }

#.. Target
#
    MakePrint( "#.. Library ($name)\n\n" );     # label

    MakeEntry( "\$(LIBDIR)/$name\$(GBE_TYPE).$::a:\t",
        "", "\\\n\t\t", ".$::o", @$pObjs );

#.. Build library rule (just append to standard rule)
#
    MakePrint( "\n\t\$(AR)\n\n" );
}


###############################################################################
#   ToolsetARMerge( $name, \@args, \@libs )
#       This subroutine takes the user options and builds the rules
#       required to build the library 'name' by merging the specified
#       libaries
#
#   Arguments:
#       --xxx                   No arguments currently defined
#
#   Output:
#       [ $(LIBDIR)/name$.${a}:   .... ]
#           ...
#
###############################################################################

sub ToolsetARMerge
{
    MakePrint( "\n\t\$(ARMERGE)\n\n" );
}


###############################################################################
#   ToolsetSHLD( $name, \@args, \@objs, \@libraries, $ver )
#       This subroutine takes the user options and builds the rules
#       required to link the Shared Library 'name'.
#
#   Arguments:
#   n/a
#
#   Output:
#       $(LIBDIR)/name:     $(LIBDIR)/shared
#           ln -s $shared $name
#
#       $(LIBDIR)/name.dep: $(GBE_OBJDIR)
#       $(LIBDIR)/name.dep: $(GBE_LIBDIR)
#       $(LIBDIR)/name.dep: $(GBE_PLATFORM).mk
#           $(SHLDDEPEND)
#
#       $(LIBDIR)/shared:   SHNAME=name
#       $(LIBDIR)/shared:   SHBASE=base
#       $(LIBDIR)/shared:   $(LIBDIR)/name.dep  \
#           $(OBJECTS)
#                           
#       ifneq "$(findstring $(IFLAG),23)" ""
#       -include            "$(LIBDIR)/name.dep"
#       endif
#
#       name_ld += ...
#           :
#
###############################################################################

sub ToolsetSHLD
{
    my( $name, $pArgs, $pObjs, $pLibs, $ver ) = @_;
    my( $linkname, $soname, $shared, $dbgname, $def, $def_pref, $multi_scan );
    my $sosuffix = '.' . $ver;

#.. Parse arguments
#
    foreach $_ ( @$pArgs )
    {
        if (/^--Def=(.*?)(\,(.*))?$/) {         # Library definition
            #
            #   Locate the Def file.
            #   If it is a generate file so it will be in the SRCS hash
            #   Otherwise the user will have to use Src to locate the file
            #
            $def = MakeSrcResolve($1);
            $def_pref = '';
            if ( $1 =~ m~\.def$~ ) {
                $def_pref = '--version-script='; # Old def file
            }
        } elsif ( /^--MultiScan/i ) {
            $multi_scan = 1;

        } elsif ( /^--NoMultiScan/i ) {
            $multi_scan = 0;

        } elsif ( /^--SoNameSuffix=(.*)/i ) {
            $sosuffix = $1;

        } else {
            Message( "SHLD: unknown option $_ -- ignored\n" );
        }
    }

#.. Various library names
#
    $linkname = "$name\$(GBE_TYPE).$::so";
    $shared = "$linkname.$ver";
    $soname = "$linkname$sosuffix";
    
    my $shared_path = "\$(LIBDIR)/${shared}";
    my $dbg_path =  $shared_path . '.dbg';

#.. Cleanup rules
#
#   map     Map file
#   ln      Link from LIBDIR to BINDIR
#
    ToolsetGenerate( "\$(LIBDIR)/${name}.map" );
    ToolsetGenerate( "\$(LIBDIR)/${shared}" );
    ToolsetGenerate( "\$(BINDIR)/${soname}" );
    ToolsetGenerate( $dbg_path );

#.. Build rules
#
#   name        Base name
#   shared      Library name, includes GBE_TYPE specification
#
    my ($io) = ToolsetPrinter::New();
    my $dep = $io->SetShldTarget($shared);

    $io->Label( "Shared library", $name );
    PackageShlibAddFiles( $name, "\$(LIBDIR)/$shared" );
    PackageShlibAddFiles( $name, $dbg_path );
    
    $io->Prt( "\$(LIBDIR)/${shared}:\tSHBASE=${name}\n" );
    $io->Prt( "\$(LIBDIR)/${shared}:\tSHNAME=${soname}\n" );
    $io->Prt( "\$(LIBDIR)/${shared}: \\\n\t\t${dep}" );
    $io->Entry( "", "", " \\\n\t\t", ".$::o", @$pObjs );
    $io->Prt( "\\\n\t\t$def" ) if($def);
    $io->Prt( "\n\t\$(SHLD)" );
    $io->Prt( "\n\t\$(call LDSTRIP,$shared_path,$dbg_path)\n\n" );
    
#
#   Create soft links
#       'Real Name' to its 'Link Name'
#       'Real Name' to 'SoName' in the BINDIR (for testing I think)
#       'Real Name' to 'SoName' in the LIBDIR (if different)
#
    $io->Label( "Shared library Symbolic Links", $name );
    PackageShlibAddFiles( $name, "\$(LIBDIR)/$linkname" );
    $io->Prt( "\$(LIBDIR)/$linkname:\t\\\n" .
              "\t\t\$(GBE_BINDIR)\\\n" .
              "\t\t\$(LIBDIR)/${shared}\n" .
              "\t\$(AA_PRE)(rm -f \$@; ln -s ./$shared \$@)\n" .
              "\t\$(AA_PRE)(rm -f \$(BINDIR)/$soname; ln -s ../\$(LIBDIR)/$shared \$(BINDIR)/$soname)\n\n" );

    if ( $soname ne $shared && $soname ne $linkname)
    {
        $io->Label( "Shared library SoName Symbolic Links", $name );
        PackageShlibAddFiles( $name, "\$(LIBDIR)/$soname" );
        $io->Prt( "\$(LIBDIR)/$soname:\t\\\n" .
                  "\t\t\$(GBE_LIBDIR)\\\n" .
                  "\t\t\$(LIBDIR)/${shared}\n" .
                  "\t\$(AA_PRE)(rm -f \$@; ln -s ./$shared \$@)\n" );
    }

#.. Linker command file
#
#       Now the fun part... piecing together a variable $(name_shld)
#       which ends up in the command file.
#
    $io->Newline();
    $io->SetTag( "${name}_shld" );              # command tag
    $io->SetTerm( "\n" );

    $io->Label( "Linker commands", $name );     # label

                                                # object list
    $io->ObjList( $name, $pObjs, \&ToolsetObjRecipe );

    ToolsetLibStd( $pLibs );                    # push standard libraries

    $io->Cmd( "-Wl,$def_pref$def" ) if ($def);

    $io->Cmd( "-Wl,--start-group" ) if ($multi_scan);
    $io->LibList( $name, $pLibs, \&ToolsetLibRecipe );
    $io->Cmd( "-Wl,--end-group" ) if ($multi_scan);

    $io->Newline();

    #.. Dependency link,
    #   Create a library dependency file
    #       Create command file to build applicaton dependency list
    #       from the list of dependent libraries
    #
    #       Create makefile directives to include the dependency
    #       list into the makefile.
    #
    $io->DepRules( $pLibs, \&ToolsetLibRecipe, "\$(LIBDIR)/${shared}" );
    $io->SHLDDEPEND($name, $soname);
}


###############################################################################
# Function        : ToolsetLD
#
# Description     : Takes the user options and builds the rules required to
#                   link the program 'name'.
#
# Inputs          : $name           - base name of the program
#                   $pArgs          - Ref to program arguments
#                   $pObjs          - Ref to program objects
#                   $pLibs          - Ref to program library list
#
# Returns         : Nothing
#
# Output:         : Rules and recipes to create a program
#                       Create program rules and recipes
#                       Create linker input script
#                       Create library dependency list
#                       Include library dependency information
#
sub ToolsetLD
{
    my( $name, $pArgs, $pObjs, $pLibs ) = @_;
    my $static = $::ScmCompilerOpts{'STATIC_PROGS'};
    my $multi_scan;

#.. Parse arguments
#
    foreach $_ ( @$pArgs )
    {
        if ( m/^--Static$/ ) {
            $static = 1;

        } elsif ( m/^--Shared/ ) {
            $static = 0;

        } elsif ( /^--MultiScan/i ) {
            $multi_scan = 1;

        } elsif ( /^--NoMultiScan/i ) {
            $multi_scan = 0;

        } else {
            Message( "LD: unknown option $_ -- ignored\n" );
        }
    }

#.. Names of programs and components
#
    my $base = "\$(BINDIR)/${name}";
    my $full = $base . $::exe;
    my $map  = $base . '.map';
    my $ld  =  $base . '.ld';
    my $dbg =  $base . '.dbg';

#.. Cleanup rules
#
    ToolsetGenerate( $ld );
    ToolsetGenerate( $map );
    ToolsetGenerate( $dbg );

#.. Build rules
#
    my ($io) = ToolsetPrinter::New();
    my $dep = $io->SetLdTarget( $name );

    $io->Prt( "$full : $dep " );
    $io->Entry( "", "", "\\\n\t", ".$::o ", @$pObjs );
    $io->Prt( "\n\t\$(LD)" );
    $io->Prt( "\n\t\$(call LDSTRIP,$full,$dbg)\n\n" );


#.. Linker command file
#
#       Now the fun part... piecing together a variable $(name_ld)
#       which ends up in the command file.
#
    $io->SetTag( "${name}_ld" );                # macro tag
    $io->SetTerm( "\n" );

    $io->Label( "Linker commands", $name );     # label

    $io->Cmd( "-static" ) if ($static);         # Link as a static program

                                                # object list
    $io->ObjList( $name, $pObjs, \&ToolsetObjRecipe );

    ToolsetLibStd( $pLibs );                    # push standard libraries

                                                # library list
    $io->Cmd( "-Wl,--start-group" ) if ($multi_scan);
    $io->LibList( $name, $pLibs, \&ToolsetLibRecipe );
    $io->Cmd( "-Wl,--end-group" ) if ($multi_scan);

    $io->Newline();

    #.. Dependency link,
    #   Create a library dependency file
    #       Create command file to build applicaton dependency list
    #       from the list of dependent libraries
    #
    #       Create makefile directives to include the dependency
    #       list into the makefile.
    #
    $io->DepRules( $pLibs, \&ToolsetLibRecipe, $base );
    $io->LDDEPEND();

#.. Package up the program and other artifacts
#
    PackageProgAddFiles ( $name, $full );
    PackageProgAddFiles ( $name, $dbg );

}


########################################################################
#
#   Push standard "system" libraries. This is a helper function
#   used within this toolset.
#
#   Arguments:
#       $plib       Reference to library array.
#
########################################################################

sub ToolsetLibStd
{
}


########################################################################
#
#   Generate a linker object recipe.  This is a helper function used 
#   within this toolset.
#
#   Arguments:
#       $io         I/O stream
#
#       $target     Name of the target
#
#       $obj        Library specification
#
########################################################################

sub ToolsetObjRecipe
{
    my ($io, $target, $obj) = @_;

    $io->Cmd( "\$(strip $obj).$::o" );
}


###############################################################################
#
#   Parse a linker lib list
#   This is a helper function used within this toolset
#
#   Arguments:
#       $target     Name of the target
#
#       $lib        Library specification
#
#       $tag        Tag (user specified)
#
#       $dp         If building a depend list, the full target name.
#
###############################################################################

sub ToolsetLibRecipe
{
    my ($io, $target, $lib, $dp) = @_;

    if ( ! defined($dp) ) {                     # linker
        $lib =~ s/^lib//;                       # .. remove leading 'lib'
        $io->Cmd( "-l$lib" );
    
    } else {                                    # depend
        $io->Cmd( "$dp:\t@(vlib2,$lib,GCC_LIB)" );

    }
}

#.. Successful termination
1;