1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/Exception.h"
18 #include "zypp/base/NonCopyable.h"
19 #include "zypp/base/Tr1hash.h"
20 #include "zypp/Arch.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 // CLASS NAME : Arch::CompatEntry
33 /** Holds an architecture ID and it's compatible relation.
34 * An architecture is compatibleWith, if it's _idBit is set in
35 * _compatBits. noarch has ID 0, non builtin archs ID 1 and
36 * have to be treated specialy.
38 struct Arch::CompatEntry
40 /** Bitfield for architecture IDs and compatBits relation.
41 * \note Need one bit for each builtin Arch.
42 * \todo Migrate to some infinite BitField
44 typedef bit::BitField<uint64_t> CompatBits;
46 CompatEntry( const std::string & archStr_r,
47 CompatBits::IntT idBit_r = 1 )
49 , _archStr( archStr_r )
51 , _compatBits( idBit_r )
54 CompatEntry( IdString archStr_r,
55 CompatBits::IntT idBit_r = 1 )
57 , _archStr( archStr_r.asString() )
59 , _compatBits( idBit_r )
62 void addCompatBit( const CompatBits & idBit_r ) const
64 if ( idBit_r && ! (_compatBits & idBit_r) )
66 _compatBits |= idBit_r;
70 /** Return whether \c this is compatible with \a targetEntry_r.*/
71 bool compatibleWith( const CompatEntry & targetEntry_r ) const
73 switch ( _idBit.value() )
76 // this is noarch and always comatible
80 // this is a non builtin: self compatible only
81 return _archStr == targetEntry_r._archStr;
84 // This is a builtin: compatible if mentioned in targetEntry_r
85 return targetEntry_r._compatBits & _idBit;
88 /** compare by score, then archStr. */
89 int compare( const CompatEntry & rhs ) const
91 if ( _idBit.value() != rhs. _idBit.value() )
92 return( _idBit.value() < rhs. _idBit.value() ? -1 : 1 );
93 return _archStr.compare( rhs._archStr ); // Id 1: non builtin
96 bool isBuiltIn() const
97 { return( _idBit != CompatBits(1) ); }
99 IdString::IdType id() const
100 { return _idStr.id(); }
103 std::string _archStr; // frequently used by the UI so we keep a reference
105 mutable CompatBits _compatBits;
107 ///////////////////////////////////////////////////////////////////
109 /** \relates Arch::CompatEntry Stream output */
110 inline std::ostream & operator<<( std::ostream & str, const Arch::CompatEntry & obj )
112 Arch::CompatEntry::CompatBits bit( obj._idBit );
119 return str << str::form( "%-15s ", obj._archStr.c_str() ) << str::numstring(bitnum,2) << ' '
120 << obj._compatBits << ' ' << obj._compatBits.value();
123 /** \relates Arch::CompatEntry */
124 inline bool operator==( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
125 { return lhs._idStr == rhs._idStr; }
126 /** \relates Arch::CompatEntry */
127 inline bool operator!=( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
128 { return ! ( lhs == rhs ); }
130 /////////////////////////////////////////////////////////////////
132 ///////////////////////////////////////////////////////////////////
134 ZYPP_DEFINE_ID_HASHABLE( zypp::Arch::CompatEntry );
136 ///////////////////////////////////////////////////////////////////
138 { /////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////
142 { /////////////////////////////////////////////////////////////////
144 // Builtin architecture STRING VALUES to be
145 // used in defCompatibleWith below!
147 // const IdString _foo( "foo" );
149 // NOTE: Builtin CLASS Arch CONSTANTS are defined below.
150 // You have to change them accordingly.
152 // NOTE: Thake care CompatBits::IntT is able to provide one
153 // bit for each architecture.
155 #define DEF_BUILTIN(A) const IdString _##A( #A );
156 DEF_BUILTIN( noarch );
162 DEF_BUILTIN( athlon );
163 DEF_BUILTIN( x86_64 );
165 DEF_BUILTIN( pentium3 );
166 DEF_BUILTIN( pentium4 );
169 DEF_BUILTIN( s390x );
172 DEF_BUILTIN( ppc64 );
176 DEF_BUILTIN( alphaev67 );
177 DEF_BUILTIN( alphaev6 );
178 DEF_BUILTIN( alphapca56 );
179 DEF_BUILTIN( alphaev56 );
180 DEF_BUILTIN( alphaev5 );
181 DEF_BUILTIN( alpha );
183 DEF_BUILTIN( sparc64v );
184 DEF_BUILTIN( sparcv9v );
185 DEF_BUILTIN( sparc64 );
186 DEF_BUILTIN( sparcv9 );
187 DEF_BUILTIN( sparcv8 );
188 DEF_BUILTIN( sparc );
190 DEF_BUILTIN( armv7tnhl );
191 DEF_BUILTIN( armv7thl );
192 DEF_BUILTIN( armv7nhl );
193 DEF_BUILTIN( armv7hl );
194 DEF_BUILTIN( armv7l );
195 DEF_BUILTIN( armv6l );
196 DEF_BUILTIN( armv5tejl );
197 DEF_BUILTIN( armv5tel );
198 DEF_BUILTIN( armv5l );
199 DEF_BUILTIN( armv4tl );
200 DEF_BUILTIN( armv4l );
201 DEF_BUILTIN( armv3l );
209 ///////////////////////////////////////////////////////////////////
211 // CLASS NAME : CompatSet
213 /** Maintain architecture compatibility (Singleton by the way it is used).
215 * Povides \ref Arch::CompatEntry for \ref Arch. Defines the
216 * compatibleWith relation.
217 * \li \c noarch has _idBit 0
218 * \li \c nonbuiltin archs have _idBit 1
220 struct ArchCompatSet : private base::NonCopyable
222 typedef Arch::CompatEntry CompatEntry;
223 typedef CompatEntry::CompatBits CompatBits;
225 typedef std::tr1::unordered_set<CompatEntry> Set;
226 typedef Set::iterator iterator;
227 typedef Set::const_iterator const_iterator;
229 /** Singleton access. */
230 static ArchCompatSet & instance()
232 static ArchCompatSet _instance;
236 /** Return the entry related to \a archStr_r.
237 * Creates an entry for nonbuiltin archs.
239 const Arch::CompatEntry & assertDef( const std::string & archStr_r )
240 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
242 const Arch::CompatEntry & assertDef( IdString archStr_r )
243 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
245 const_iterator begin() const
246 { return _compatSet.begin(); }
248 const_iterator end() const
249 { return _compatSet.end(); }
253 int operator()( const CompatEntry & lhs, const CompatEntry & rhs ) const
254 { return lhs._idBit.value() < rhs._idBit.value(); }
257 std::ostream & dumpOn( std::ostream & str ) const
259 str << "ArchCompatSet:";
260 std::list<CompatEntry> ov( _compatSet.begin(), _compatSet.end() );
261 ov.sort( DumpOnCompare() );
262 for_( it, ov.begin(), ov.end() )
264 str << endl << ' ' << *it;
270 /** Singleton ctor. */
273 // _noarch must have _idBit 0.
274 // Other builtins have 1-bit set
275 // and are initialized done on the fly.
276 _compatSet.insert( Arch::CompatEntry( _noarch, 0 ) );
277 ///////////////////////////////////////////////////////////////////
278 // Define the CompatibleWith relation:
280 // NOTE: Order of definition is significant! (Arch::compare)
281 // - define compatible (less) architectures first!
283 defCompatibleWith( _i386, _noarch );
284 defCompatibleWith( _i486, _noarch,_i386 );
285 defCompatibleWith( _i586, _noarch,_i386,_i486 );
286 defCompatibleWith( _i686, _noarch,_i386,_i486,_i586 );
287 defCompatibleWith( _athlon, _noarch,_i386,_i486,_i586,_i686 );
288 defCompatibleWith( _x86_64, _noarch,_i386,_i486,_i586,_i686,_athlon );
290 defCompatibleWith( _pentium3, _noarch,_i386,_i486,_i586,_i686 );
291 defCompatibleWith( _pentium4, _noarch,_i386,_i486,_i586,_i686,_pentium3 );
293 defCompatibleWith( _ia64, _noarch,_i386,_i486,_i586,_i686 );
295 defCompatibleWith( _s390, _noarch );
296 defCompatibleWith( _s390x, _noarch,_s390 );
298 defCompatibleWith( _ppc, _noarch );
299 defCompatibleWith( _ppc64, _noarch,_ppc );
301 defCompatibleWith( _alpha, _noarch );
302 defCompatibleWith( _alphaev5, _noarch,_alpha );
303 defCompatibleWith( _alphaev56, _noarch,_alpha,_alphaev5 );
304 defCompatibleWith( _alphapca56, _noarch,_alpha,_alphaev5,_alphaev56 );
305 defCompatibleWith( _alphaev6, _noarch,_alpha,_alphaev5,_alphaev56,_alphapca56 );
306 defCompatibleWith( _alphaev67, _noarch,_alpha,_alphaev5,_alphaev56,_alphapca56,_alphaev6 );
308 defCompatibleWith( _sparc, _noarch );
309 defCompatibleWith( _sparcv8, _noarch,_sparc );
310 defCompatibleWith( _sparcv9, _noarch,_sparc,_sparcv8 );
311 defCompatibleWith( _sparcv9v, _noarch,_sparc,_sparcv8,_sparcv9 );
313 defCompatibleWith( _sparc64, _noarch,_sparc,_sparcv8,_sparcv9 );
314 defCompatibleWith( _sparc64v, _noarch,_sparc,_sparcv8,_sparcv9,_sparcv9v,_sparc64 );
316 defCompatibleWith( _armv3l, _noarch );
317 defCompatibleWith( _armv4l, _noarch,_armv3l );
318 defCompatibleWith( _armv4tl, _noarch,_armv3l,_armv4l );
319 defCompatibleWith( _armv5l, _noarch,_armv3l,_armv4l,_armv4tl );
320 defCompatibleWith( _armv5tel, _noarch,_armv3l,_armv4l,_armv4tl,_armv5l );
321 defCompatibleWith( _armv5tejl, _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel );
322 defCompatibleWith( _armv6l, _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel,_armv5tejl );
323 defCompatibleWith( _armv7l, _noarch,_armv3l,_armv4l,_armv4tl,_armv5l,_armv5tel,_armv5tejl,_armv6l );
324 defCompatibleWith( _armv7hl, _noarch );
325 defCompatibleWith( _armv7nhl, _noarch, _armv7hl );
326 defCompatibleWith( _armv7thl, _noarch,_armv7hl );
327 defCompatibleWith( _armv7tnhl, _noarch,_armv7hl,_armv7thl,_armv7nhl );
329 defCompatibleWith( _sh3, _noarch );
331 defCompatibleWith( _sh4, _noarch );
332 defCompatibleWith( _sh4a, _noarch,_sh4 );
334 ///////////////////////////////////////////////////////////////////
335 // dumpOn( USR ) << endl;
339 /** Return the next avialable _idBit.
340 * Ctor injects _noarch into the _compatSet, 1 is for
341 * nonbuiltin archs, so we can use <tt>size</tt> for
344 CompatBits::IntT nextIdBit() const
346 if ( CompatBits::size == _compatSet.size() )
348 // Provide more bits in CompatBits::IntT
349 INT << "Need more than " << CompatBits::size << " bits to encode architectures." << endl;
350 ZYPP_THROW( Exception("Need more bits to encode architectures.") );
352 CompatBits::IntT nextBit = CompatBits::IntT(1) << (_compatSet.size());
356 /** Assert each builtin Arch gets an unique _idBit when
357 * inserted into the _compatSet.
359 const CompatEntry & assertCompatSetEntry( IdString archStr_r )
360 { return *_compatSet.insert( Arch::CompatEntry( archStr_r, nextIdBit() ) ).first; }
362 /** Initialize builtin Archs and set _compatBits.
364 void defCompatibleWith( IdString targetArch_r,
366 IdString arch1_r = IdString(),
367 IdString arch2_r = IdString(),
368 IdString arch3_r = IdString(),
369 IdString arch4_r = IdString(),
370 IdString arch5_r = IdString(),
371 IdString arch6_r = IdString(),
372 IdString arch7_r = IdString(),
373 IdString arch8_r = IdString(),
374 IdString arch9_r = IdString() )
376 const CompatEntry & target( assertCompatSetEntry( targetArch_r ) );
377 target.addCompatBit( assertCompatSetEntry( arch0_r )._idBit );
378 #define _SETARG(N) if ( arch##N##_r.empty() ) return; target.addCompatBit( assertCompatSetEntry( arch##N##_r )._idBit )
379 _SETARG(1); _SETARG(2); _SETARG(3); _SETARG(4);
380 _SETARG(5); _SETARG(6); _SETARG(7); _SETARG(8); _SETARG(9);
388 /////////////////////////////////////////////////////////////////
390 ///////////////////////////////////////////////////////////////////
392 ///////////////////////////////////////////////////////////////////
396 ///////////////////////////////////////////////////////////////////
398 const Arch Arch_empty ( IdString::Empty );
399 const Arch Arch_noarch( _noarch );
401 const Arch Arch_i386( _i386 );
402 const Arch Arch_i486( _i486 );
403 const Arch Arch_i586( _i586 );
404 const Arch Arch_i686( _i686 );
405 const Arch Arch_athlon( _athlon );
406 const Arch Arch_x86_64( _x86_64 );
408 const Arch Arch_pentium3( _pentium3 );
409 const Arch Arch_pentium4( _pentium4 );
411 const Arch Arch_s390( _s390 );
412 const Arch Arch_s390x( _s390x );
414 const Arch Arch_ppc( _ppc );
415 const Arch Arch_ppc64( _ppc64 );
417 const Arch Arch_ia64( _ia64 );
419 const Arch Arch_alphaev67( _alphaev67 );
420 const Arch Arch_alphaev6( _alphaev6 );
421 const Arch Arch_alphapca56( _alphapca56 );
422 const Arch Arch_alphaev56( _alphaev56 );
423 const Arch Arch_alphaev5( _alphaev5 );
424 const Arch Arch_alpha( _alpha );
426 const Arch Arch_sparc64v( _sparc64v );
427 const Arch Arch_sparc64( _sparc64 );
428 const Arch Arch_sparcv9v( _sparcv9v );
429 const Arch Arch_sparcv9( _sparcv9 );
430 const Arch Arch_sparcv8( _sparcv8 );
431 const Arch Arch_sparc( _sparc );
433 const Arch Arch_armv7tnhl( _armv7tnhl );
434 const Arch Arch_armv7thl( _armv7thl );
435 const Arch Arch_armv7nhl ( _armv7nhl );
436 const Arch Arch_armv7hl ( _armv7hl );
437 const Arch Arch_armv7l( _armv7l );
438 const Arch Arch_armv6l( _armv6l );
439 const Arch Arch_armv5tejl( _armv5tejl );
440 const Arch Arch_armv5tel( _armv5tel );
441 const Arch Arch_armv5l( _armv5l );
442 const Arch Arch_armv4tl( _armv4tl );
443 const Arch Arch_armv4l( _armv4l );
444 const Arch Arch_armv3l( _armv3l );
446 const Arch Arch_sh3( _sh3 );
448 const Arch Arch_sh4( _sh4 );
449 const Arch Arch_sh4a( _sh4a );
451 ///////////////////////////////////////////////////////////////////
453 // METHOD NAME : Arch::Arch
454 // METHOD TYPE : Ctor
457 : _entry( &ArchCompatSet::instance().assertDef( _noarch ) )
460 Arch::Arch( IdString::IdType id_r )
461 : _entry( &ArchCompatSet::instance().assertDef( IdString(id_r) ) )
464 Arch::Arch( const IdString & idstr_r )
465 : _entry( &ArchCompatSet::instance().assertDef( idstr_r ) )
468 Arch::Arch( const std::string & str_r )
469 : _entry( &ArchCompatSet::instance().assertDef( str_r ) )
472 Arch::Arch( const char * cstr_r )
473 : _entry( &ArchCompatSet::instance().assertDef( cstr_r ) )
476 Arch::Arch( const CompatEntry & rhs )
480 ///////////////////////////////////////////////////////////////////
482 // METHOD NAME : Arch::idStr
483 // METHOD TYPE : IdString
485 IdString Arch::idStr() const
486 { return _entry->_idStr; }
488 ///////////////////////////////////////////////////////////////////
490 // METHOD NAME : Arch::asString
491 // METHOD TYPE : const std::string &
493 const std::string & Arch::asString() const
494 { return _entry->_archStr; }
496 ///////////////////////////////////////////////////////////////////
498 // METHOD NAME : Arch::isBuiltIn
499 // METHOD TYPE : bool
501 bool Arch::isBuiltIn() const
502 { return _entry->isBuiltIn(); }
504 ///////////////////////////////////////////////////////////////////
506 // METHOD NAME : Arch::compatibleWith
507 // METHOD TYPE : bool
509 bool Arch::compatibleWith( const Arch & targetArch_r ) const
510 { return _entry->compatibleWith( *targetArch_r._entry ); }
512 ///////////////////////////////////////////////////////////////////
514 // METHOD NAME : Arch::baseArch
515 // METHOD TYPE : Arch
517 Arch Arch::baseArch( ) const
519 // check the multilib archs:
520 if (Arch_x86_64.compatibleWith(*this))
524 if (Arch_sparc64v.compatibleWith(*this))
526 return Arch_sparc64v;
528 if (Arch_sparc64.compatibleWith(*this))
532 if (Arch_ppc64.compatibleWith(*this))
536 if (Arch_s390x.compatibleWith(*this))
540 // Here: no multilib; return arch before noarch
541 CompatSet cset( compatSet( *this ) );
542 if ( cset.size() > 2 ) // systemArchitecture, ..., basearch, noarch
544 return *(++cset.rbegin());
549 ///////////////////////////////////////////////////////////////////
551 // METHOD NAME : Arch::compare
552 // METHOD TYPE : bool
554 int Arch::compare( const Arch & rhs ) const
555 { return _entry->compare( *rhs._entry ); }
557 ///////////////////////////////////////////////////////////////////
559 // METHOD NAME : Arch::compatSet
560 // METHOD TYPE : Arch::CompatSet
562 Arch::CompatSet Arch::compatSet( const Arch & targetArch_r )
566 for ( ArchCompatSet::const_iterator it = ArchCompatSet::instance().begin();
567 it != ArchCompatSet::instance().end(); ++it )
569 if ( it->compatibleWith( *targetArch_r._entry ) )
571 ret.insert( Arch(*it) );
578 /////////////////////////////////////////////////////////////////
580 ///////////////////////////////////////////////////////////////////