Arch.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include <list>
00014 #include <inttypes.h>
00015 
00016 #include "zypp/base/Logger.h"
00017 #include "zypp/base/Exception.h"
00018 #include "zypp/base/NonCopyable.h"
00019 #include "zypp/base/Tr1hash.h"
00020 #include "zypp/Arch.h"
00021 #include "zypp/Bit.h"
00022 
00023 using std::endl;
00024 
00026 namespace zypp
00027 { 
00028 
00030   //
00031   //    CLASS NAME : Arch::CompatEntry
00032   //
00038   struct Arch::CompatEntry
00039   {
00044     typedef bit::BitField<uint64_t> CompatBits;
00045 
00046     CompatEntry( const std::string & archStr_r,
00047                  CompatBits::IntT idBit_r = 1 )
00048     : _idStr( archStr_r )
00049     , _archStr( archStr_r )
00050     , _idBit( idBit_r )
00051     , _compatBits( idBit_r )
00052     {}
00053 
00054     CompatEntry( IdString archStr_r,
00055                  CompatBits::IntT idBit_r = 1 )
00056     : _idStr( archStr_r )
00057     , _archStr( archStr_r.asString() )
00058     , _idBit( idBit_r )
00059     , _compatBits( idBit_r )
00060     {}
00061 
00062     void addCompatBit( const CompatBits & idBit_r ) const
00063     {
00064       if ( idBit_r && ! (_compatBits & idBit_r) )
00065         {
00066           _compatBits |= idBit_r;
00067         }
00068     }
00069 
00071     bool compatibleWith( const CompatEntry & targetEntry_r ) const
00072     {
00073       switch ( _idBit.value() )
00074         {
00075         case 0:
00076           // this is noarch and always comatible
00077           return true;
00078           break;
00079         case 1:
00080           // this is a non builtin: self compatible only
00081           return _archStr == targetEntry_r._archStr;
00082           break;
00083         }
00084       // This is a builtin: compatible if mentioned in targetEntry_r
00085       return targetEntry_r._compatBits & _idBit;
00086     }
00087 
00089     int compare( const CompatEntry & rhs ) const
00090     {
00091       if ( _idBit.value() != rhs. _idBit.value() )
00092         return( _idBit.value() < rhs. _idBit.value() ? -1 : 1 );
00093       return _archStr.compare( rhs._archStr ); // Id 1: non builtin
00094     }
00095 
00096     bool isBuiltIn() const
00097     { return( _idBit != CompatBits(1) ); }
00098 
00099     IdString::IdType id() const
00100     { return _idStr.id(); }
00101 
00102     IdString            _idStr;
00103     std::string         _archStr; // frequently used by the UI so we keep a reference
00104     CompatBits          _idBit;
00105     mutable CompatBits  _compatBits;
00106   };
00108 
00110   inline std::ostream & operator<<( std::ostream & str, const Arch::CompatEntry & obj )
00111   {
00112     Arch::CompatEntry::CompatBits bit( obj._idBit );
00113     unsigned bitnum = 0;
00114     while ( bit )
00115     {
00116       ++bitnum;
00117       bit >>= 1;
00118     }
00119     return str << str::form( "%-15s ", obj._archStr.c_str() ) << str::numstring(bitnum,2) << ' '
00120                << obj._compatBits << ' ' << obj._compatBits.value();
00121   }
00122 
00124   inline bool operator==( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
00125   { return lhs._idStr == rhs._idStr; }
00127   inline bool operator!=( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
00128   { return ! ( lhs == rhs ); }
00129 
00131 } // namespace zypp
00133 
00134 ZYPP_DEFINE_ID_HASHABLE( zypp::Arch::CompatEntry );
00135 
00137 namespace zypp
00138 { 
00139 
00141   namespace
00142   { 
00143 
00144     // Builtin architecture STRING VALUES to be
00145     // used in defCompatibleWith below!
00146     //
00147     // const IdString  _foo( "foo" );
00148     //
00149     // NOTE: Builtin CLASS Arch CONSTANTS are defined below.
00150     //       You have to change them accordingly.
00151     //
00152     // NOTE: Thake care CompatBits::IntT is able to provide one
00153     //       bit for each architecture.
00154     //
00155 #define DEF_BUILTIN(A) const IdString  _##A( #A );
00156     DEF_BUILTIN( noarch );
00157 
00158     DEF_BUILTIN( i386 );
00159     DEF_BUILTIN( i486 );
00160     DEF_BUILTIN( i586 );
00161     DEF_BUILTIN( i686 );
00162     DEF_BUILTIN( athlon );
00163     DEF_BUILTIN( x86_64 );
00164 
00165     DEF_BUILTIN( pentium3 );
00166     DEF_BUILTIN( pentium4 );
00167 
00168     DEF_BUILTIN( s390 );
00169     DEF_BUILTIN( s390x );
00170 
00171     DEF_BUILTIN( ppc );
00172     DEF_BUILTIN( ppc64 );
00173 
00174     DEF_BUILTIN( ia64 );
00175 
00176     DEF_BUILTIN( alphaev67 );
00177     DEF_BUILTIN( alphaev6 );
00178     DEF_BUILTIN( alphapca56 );
00179     DEF_BUILTIN( alphaev56 );
00180     DEF_BUILTIN( alphaev5 );
00181     DEF_BUILTIN( alpha );
00182 
00183     DEF_BUILTIN( sparc64v );
00184     DEF_BUILTIN( sparcv9v );
00185     DEF_BUILTIN( sparc64 );
00186     DEF_BUILTIN( sparcv9 );
00187     DEF_BUILTIN( sparcv8 );
00188     DEF_BUILTIN( sparc );
00189 
00190     DEF_BUILTIN( armv7l );
00191     DEF_BUILTIN( armv6l );
00192     DEF_BUILTIN( armv5tejl );
00193     DEF_BUILTIN( armv5tel );
00194     DEF_BUILTIN( armv5l );
00195     DEF_BUILTIN( armv4tl );
00196     DEF_BUILTIN( armv4l );
00197     DEF_BUILTIN( armv3l );
00198 
00199     DEF_BUILTIN( sh3 );
00200 
00201     DEF_BUILTIN( sh4 );
00202     DEF_BUILTIN( sh4a );
00203 #undef DEF_BUILTIN
00204 
00206     //
00207     //  CLASS NAME : CompatSet
00208     //
00216     struct ArchCompatSet : private base::NonCopyable
00217     {
00218       typedef Arch::CompatEntry       CompatEntry;
00219       typedef CompatEntry::CompatBits CompatBits;
00220 
00221       typedef std::tr1::unordered_set<CompatEntry> Set;
00222       typedef Set::iterator           iterator;
00223       typedef Set::const_iterator     const_iterator;
00224 
00226       static ArchCompatSet & instance()
00227       {
00228         static ArchCompatSet _instance;
00229         return _instance;
00230       }
00231 
00235       const Arch::CompatEntry & assertDef( const std::string & archStr_r )
00236       { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
00238       const Arch::CompatEntry & assertDef( IdString archStr_r )
00239       { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
00240 
00241       const_iterator begin() const
00242       { return _compatSet.begin(); }
00243 
00244       const_iterator end() const
00245       { return _compatSet.end(); }
00246 
00247       struct DumpOnCompare
00248       {
00249         int operator()( const CompatEntry & lhs,  const CompatEntry & rhs ) const
00250         { return lhs._idBit.value() < rhs._idBit.value(); }
00251       };
00252 
00253       std::ostream & dumpOn( std::ostream & str ) const
00254       {
00255         str << "ArchCompatSet:";
00256         std::list<CompatEntry> ov( _compatSet.begin(), _compatSet.end() );
00257         ov.sort( DumpOnCompare() );
00258         for_( it, ov.begin(), ov.end() )
00259           {
00260             str << endl << ' ' << *it;
00261           }
00262         return str;
00263       }
00264 
00265     private:
00267       ArchCompatSet()
00268       {
00269         // _noarch must have _idBit 0.
00270         // Other builtins have 1-bit set
00271         // and are initialized done on the fly.
00272         _compatSet.insert( Arch::CompatEntry( _noarch, 0 ) );
00274         // Define the CompatibleWith relation:
00275         //
00276         // NOTE: Order of definition is significant! (Arch::compare)
00277         //       - define compatible (less) architectures first!
00278         //
00279         defCompatibleWith( _i386,       _noarch );
00280         defCompatibleWith( _i486,       _noarch,_i386 );
00281         defCompatibleWith( _i586,       _noarch,_i386,_i486 );
00282         defCompatibleWith( _i686,       _noarch,_i386,_i486,_i586 );
00283         defCompatibleWith( _athlon,     _noarch,_i386,_i486,_i586,_i686 );
00284         defCompatibleWith( _x86_64,     _noarch,_i386,_i486,_i586,_i686,_athlon );
00285 
00286         defCompatibleWith( _pentium3,   _noarch,_i386,_i486,_i586,_i686 );
00287         defCompatibleWith( _pentium4,   _noarch,_i386,_i486,_i586,_i686,_pentium3 );
00288 
00289         defCompatibleWith( _ia64,       _noarch,_i386,_i486,_i586,_i686 );
00290         //
00291         defCompatibleWith( _s390,       _noarch );
00292         defCompatibleWith( _s390x,      _noarch,_s390 );
00293         //
00294         defCompatibleWith( _ppc,        _noarch );
00295         defCompatibleWith( _ppc64,      _noarch,_ppc );
00296         //
00297         defCompatibleWith( _alpha,      _noarch );
00298         defCompatibleWith( _alphaev5,   _noarch,_alpha );
00299         defCompatibleWith( _alphaev56,  _noarch,_alpha,_alphaev5 );
00300         defCompatibleWith( _alphapca56, _noarch,_alpha,_alphaev5,_alphaev56 );
00301         defCompatibleWith( _alphaev6,   _noarch,_alpha,_alphaev5,_alphaev56,_alphapca56 );
00302         defCompatibleWith( _alphaev67,  _noarch,_alpha,_alphaev5,_alphaev56,_alphapca56,_alphaev6 );
00303         //
00304         defCompatibleWith( _sparc,      _noarch );
00305         defCompatibleWith( _sparcv8,    _noarch,_sparc );
00306         defCompatibleWith( _sparcv9,    _noarch,_sparc,_sparcv8 );
00307         defCompatibleWith( _sparcv9v,   _noarch,_sparc,_sparcv8,_sparcv9 );
00308         //
00309         defCompatibleWith( _sparc64,    _noarch,_sparc,_sparcv8,_sparcv9 );
00310         defCompatibleWith( _sparc64v,   _noarch,_sparc,_sparcv8,_sparcv9,_sparcv9v,_sparc64 );
00311         //
00312         defCompatibleWith( _armv3l,     _noarch );
00313         defCompatibleWith( _armv4l,     _noarch,_armv3l );
00314         defCompatibleWith( _armv4tl,    _noarch,_armv3l,_armv4l );
00315         defCompatibleWith( _armv5l,     _noarch,_armv3l,_armv4l,_armv4tl );
00316         defCompatibleWith( _armv5tel,   _noarch,_armv3l,_armv4l,_armv4tl,_armv5l );
00317         defCompatibleWith( _armv5tejl,  _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel );
00318         defCompatibleWith( _armv6l,     _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel,_armv5tejl );
00319         defCompatibleWith( _armv7l,     _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel,_armv5tejl,_armv6l );
00320         //
00321         defCompatibleWith( _sh3,        _noarch );
00322         //
00323         defCompatibleWith( _sh4,        _noarch );
00324         defCompatibleWith( _sh4a,       _noarch,_sh4 );
00325         //
00327         // dumpOn( USR ) << endl;
00328       }
00329 
00330     private:
00336       CompatBits::IntT nextIdBit() const
00337       {
00338         if ( CompatBits::size == _compatSet.size() )
00339         {
00340           // Provide more bits in CompatBits::IntT
00341           INT << "Need more than " << CompatBits::size << " bits to encode architectures." << endl;
00342           ZYPP_THROW( Exception("Need more bits to encode architectures.") );
00343         }
00344         CompatBits::IntT nextBit = CompatBits::IntT(1) << (_compatSet.size());
00345         return nextBit;
00346       }
00347 
00351       const CompatEntry & assertCompatSetEntry( IdString archStr_r )
00352       { return *_compatSet.insert( Arch::CompatEntry( archStr_r, nextIdBit() ) ).first; }
00353 
00356       void defCompatibleWith( IdString targetArch_r,
00357                               IdString arch0_r,
00358                               IdString arch1_r = IdString(),
00359                               IdString arch2_r = IdString(),
00360                               IdString arch3_r = IdString(),
00361                               IdString arch4_r = IdString(),
00362                               IdString arch5_r = IdString(),
00363                               IdString arch6_r = IdString(),
00364                               IdString arch7_r = IdString(),
00365                               IdString arch8_r = IdString(),
00366                               IdString arch9_r = IdString() )
00367       {
00368         const CompatEntry & target( assertCompatSetEntry( targetArch_r ) );
00369         target.addCompatBit( assertCompatSetEntry( arch0_r )._idBit );
00370 #define _SETARG(N) if ( arch##N##_r.empty() ) return; target.addCompatBit( assertCompatSetEntry( arch##N##_r )._idBit )
00371         _SETARG(1); _SETARG(2); _SETARG(3); _SETARG(4);
00372         _SETARG(5); _SETARG(6); _SETARG(7); _SETARG(8); _SETARG(9);
00373 #undef _SETARG
00374       }
00375 
00376     private:
00377       Set _compatSet;
00378     };
00379 
00381   } // namespace
00383 
00385   //
00386   //    CLASS NAME : Arch
00387   //
00389 
00390   const Arch Arch_empty ( IdString::Empty );
00391   const Arch Arch_noarch( _noarch );
00392 
00393   const Arch Arch_i386( _i386 );
00394   const Arch Arch_i486( _i486 );
00395   const Arch Arch_i586( _i586 );
00396   const Arch Arch_i686( _i686 );
00397   const Arch Arch_athlon( _athlon );
00398   const Arch Arch_x86_64( _x86_64 );
00399 
00400   const Arch Arch_pentium3( _pentium3 );
00401   const Arch Arch_pentium4( _pentium4 );
00402 
00403   const Arch Arch_s390( _s390 );
00404   const Arch Arch_s390x( _s390x );
00405 
00406   const Arch Arch_ppc( _ppc );
00407   const Arch Arch_ppc64( _ppc64 );
00408 
00409   const Arch Arch_ia64( _ia64 );
00410 
00411   const Arch Arch_alphaev67( _alphaev67 );
00412   const Arch Arch_alphaev6( _alphaev6 );
00413   const Arch Arch_alphapca56( _alphapca56 );
00414   const Arch Arch_alphaev56( _alphaev56 );
00415   const Arch Arch_alphaev5( _alphaev5 );
00416   const Arch Arch_alpha( _alpha );
00417 
00418   const Arch Arch_sparc64v( _sparc64v );
00419   const Arch Arch_sparc64( _sparc64 );
00420   const Arch Arch_sparcv9v( _sparcv9v );
00421   const Arch Arch_sparcv9( _sparcv9 );
00422   const Arch Arch_sparcv8( _sparcv8 );
00423   const Arch Arch_sparc( _sparc );
00424 
00425   const Arch Arch_armv7l( _armv7l );
00426   const Arch Arch_armv6l( _armv6l );
00427   const Arch Arch_armv5tejl( _armv5tejl );
00428   const Arch Arch_armv5tel( _armv5tel );
00429   const Arch Arch_armv5l( _armv5l );
00430   const Arch Arch_armv4tl( _armv4tl );
00431   const Arch Arch_armv4l( _armv4l );
00432   const Arch Arch_armv3l( _armv3l );
00433 
00434   const Arch Arch_sh3( _sh3 );
00435 
00436   const Arch Arch_sh4( _sh4 );
00437   const Arch Arch_sh4a( _sh4a );
00438 
00440   //
00441   //    METHOD NAME : Arch::Arch
00442   //    METHOD TYPE : Ctor
00443   //
00444   Arch::Arch()
00445   : _entry( &ArchCompatSet::instance().assertDef( _noarch ) )
00446   {}
00447 
00448   Arch::Arch( IdString::IdType id_r )
00449   : _entry( &ArchCompatSet::instance().assertDef( IdString(id_r) ) )
00450   {}
00451 
00452   Arch::Arch( const IdString & idstr_r )
00453   : _entry( &ArchCompatSet::instance().assertDef( idstr_r ) )
00454   {}
00455 
00456   Arch::Arch( const std::string & str_r )
00457   : _entry( &ArchCompatSet::instance().assertDef( str_r ) )
00458   {}
00459 
00460   Arch::Arch( const char * cstr_r )
00461   : _entry( &ArchCompatSet::instance().assertDef( cstr_r ) )
00462   {}
00463 
00464   Arch::Arch( const CompatEntry & rhs )
00465   : _entry( &rhs )
00466   {}
00467 
00469   //
00470   //    METHOD NAME : Arch::idStr
00471   //    METHOD TYPE : IdString
00472   //
00473   IdString Arch::idStr() const
00474   { return _entry->_idStr; }
00475 
00477   //
00478   //    METHOD NAME : Arch::asString
00479   //    METHOD TYPE : const std::string &
00480   //
00481   const std::string & Arch::asString() const
00482   { return _entry->_archStr; }
00483 
00485   //
00486   //    METHOD NAME : Arch::isBuiltIn
00487   //    METHOD TYPE : bool
00488   //
00489   bool Arch::isBuiltIn() const
00490   { return _entry->isBuiltIn(); }
00491 
00493   //
00494   //    METHOD NAME : Arch::compatibleWith
00495   //    METHOD TYPE : bool
00496   //
00497   bool Arch::compatibleWith( const Arch & targetArch_r ) const
00498   { return _entry->compatibleWith( *targetArch_r._entry ); }
00499 
00501   //
00502   //    METHOD NAME : Arch::compare
00503   //    METHOD TYPE : bool
00504   //
00505   int Arch::compare( const Arch & rhs ) const
00506   { return _entry->compare( *rhs._entry ); }
00507 
00509   //
00510   //    METHOD NAME : Arch::compatSet
00511   //    METHOD TYPE : Arch::CompatSet
00512   //
00513   Arch::CompatSet Arch::compatSet( const Arch & targetArch_r )
00514   {
00515     Arch::CompatSet ret;
00516 
00517     for ( ArchCompatSet::const_iterator it = ArchCompatSet::instance().begin();
00518           it != ArchCompatSet::instance().end(); ++it )
00519       {
00520         if ( it->compatibleWith( *targetArch_r._entry ) )
00521           {
00522             ret.insert( Arch(*it) );
00523           }
00524       }
00525 
00526     return ret;
00527   }
00528 
00530 } // namespace zypp
Generated on Fri Mar 2 09:45:51 2012 for libzypp by  doxygen 1.6.3