libzypp  16.22.5
Arch.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <list>
14 #include <inttypes.h>
15 
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/Exception.h"
18 #include "zypp/base/NonCopyable.h"
19 #include "zypp/base/Hash.h"
20 #include "zypp/Arch.h"
21 #include "zypp/Bit.h"
22 
23 using std::endl;
24 
26 namespace zypp
27 {
28 
30  //
31  // CLASS NAME : Arch::CompatEntry
32  //
39  {
45 
46  CompatEntry( const std::string & archStr_r,
47  CompatBits::IntT idBit_r = 1 )
48  : _idStr( archStr_r )
49  , _archStr( archStr_r )
50  , _idBit( idBit_r )
51  , _compatBits( idBit_r )
52  {}
53 
54  CompatEntry( IdString archStr_r,
55  CompatBits::IntT idBit_r = 1 )
56  : _idStr( archStr_r )
57  , _archStr( archStr_r.asString() )
58  , _idBit( idBit_r )
59  , _compatBits( idBit_r )
60  {}
61 
62  void addCompatBit( const CompatBits & idBit_r ) const
63  {
64  if ( idBit_r && ! (_compatBits & idBit_r) )
65  {
66  _compatBits |= idBit_r;
67  }
68  }
69 
71  bool compatibleWith( const CompatEntry & targetEntry_r ) const
72  {
73  switch ( _idBit.value() )
74  {
75  case 0:
76  // this is noarch and always comatible
77  return true;
78  break;
79  case 1:
80  // this is a non builtin: self compatible only
81  return _archStr == targetEntry_r._archStr;
82  break;
83  }
84  // This is a builtin: compatible if mentioned in targetEntry_r
85  return bool( targetEntry_r._compatBits & _idBit );
86  }
87 
89  int compare( const CompatEntry & rhs ) const
90  {
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
94  }
95 
96  bool isBuiltIn() const
97  { return( _idBit != CompatBits(1) ); }
98 
100  { return _idStr.id(); }
101 
103  std::string _archStr; // frequently used by the UI so we keep a reference
106  };
108 
110  inline std::ostream & operator<<( std::ostream & str, const Arch::CompatEntry & obj )
111  {
113  unsigned bitnum = 0;
114  while ( bit )
115  {
116  ++bitnum;
117  bit >>= 1;
118  }
119  return str << str::form( "%-15s ", obj._archStr.c_str() ) << str::numstring(bitnum,2) << ' '
120  << obj._compatBits << ' ' << obj._compatBits.value();
121  }
122 
124  inline bool operator==( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
125  { return lhs._idStr == rhs._idStr; }
127  inline bool operator!=( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
128  { return ! ( lhs == rhs ); }
129 
131 } // namespace zypp
133 
135 
137 namespace zypp
138 {
139 
140  // Builtin architecture STRING VALUES to be
141  // used in defCompatibleWith below!
142  //
143  // const IdString _foo( "foo" );
144  // const Arch Arch_foo( _foo() );
145  //
146  // NOTE: Builtin CLASS Arch CONSTANTS are defined below.
147  // You have to change them accordingly in Arch.h.
148  //
149  // NOTE: Thake care CompatBits::IntT is able to provide one
150  // bit for each architecture.
151  //
152  #define DEF_BUILTIN(A) \
153  namespace { static inline const IdString & a_##A () { static IdString _str(#A); return _str; } } \
154  const Arch Arch_##A( a_##A() )
155 
156  DEF_BUILTIN( noarch );
157 
158  DEF_BUILTIN( i386 );
159  DEF_BUILTIN( i486 );
160  DEF_BUILTIN( i586 );
161  DEF_BUILTIN( i686 );
162  DEF_BUILTIN( athlon );
163  DEF_BUILTIN( x86_64 );
164 
165  DEF_BUILTIN( pentium3 );
166  DEF_BUILTIN( pentium4 );
167 
168  DEF_BUILTIN( s390 );
169  DEF_BUILTIN( s390x );
170 
171  DEF_BUILTIN( ppc );
172  DEF_BUILTIN( ppc64 );
173  DEF_BUILTIN( ppc64p7 );
174 
175  DEF_BUILTIN( ppc64le );
176 
177  DEF_BUILTIN( ia64 );
178 
179  DEF_BUILTIN( alphaev67 );
180  DEF_BUILTIN( alphaev6 );
181  DEF_BUILTIN( alphapca56 );
182  DEF_BUILTIN( alphaev56 );
183  DEF_BUILTIN( alphaev5 );
184  DEF_BUILTIN( alpha );
185 
186  DEF_BUILTIN( sparc64v );
187  DEF_BUILTIN( sparcv9v );
188  DEF_BUILTIN( sparc64 );
189  DEF_BUILTIN( sparcv9 );
190  DEF_BUILTIN( sparcv8 );
191  DEF_BUILTIN( sparc );
192 
193  DEF_BUILTIN( aarch64 );
194 
195  DEF_BUILTIN( armv7tnhl ); /* exists? */
196  DEF_BUILTIN( armv7thl ); /* exists? */
197 
198  DEF_BUILTIN( armv7hnl ); /* legacy: */DEF_BUILTIN( armv7nhl );
199  DEF_BUILTIN( armv7hl );
200  DEF_BUILTIN( armv6hl );
201 
202  DEF_BUILTIN( armv7l );
203  DEF_BUILTIN( armv6l );
204  DEF_BUILTIN( armv5tejl );
205  DEF_BUILTIN( armv5tel );
206  DEF_BUILTIN( armv5tl );
207  DEF_BUILTIN( armv5l );
208  DEF_BUILTIN( armv4tl );
209  DEF_BUILTIN( armv4l );
210  DEF_BUILTIN( armv3l );
211 
212  DEF_BUILTIN( sh3 );
213 
214  DEF_BUILTIN( sh4 );
215  DEF_BUILTIN( sh4a );
216 
217  DEF_BUILTIN( m68k );
218 
219  DEF_BUILTIN( mips );
220  DEF_BUILTIN( mipsel );
221  DEF_BUILTIN( mips64 );
222  DEF_BUILTIN( mips64el );
223 #undef DEF_BUILTIN
224 
226  namespace
227  {
228 
230  //
231  // CLASS NAME : CompatSet
232  //
240  struct ArchCompatSet : private base::NonCopyable
241  {
242  typedef Arch::CompatEntry CompatEntry;
243  typedef CompatEntry::CompatBits CompatBits;
244 
245  typedef std::unordered_set<CompatEntry> Set;
246  typedef Set::iterator iterator;
247  typedef Set::const_iterator const_iterator;
248 
250  static ArchCompatSet & instance()
251  {
252  static ArchCompatSet _instance;
253  return _instance;
254  }
255 
259  const Arch::CompatEntry & assertDef( const std::string & archStr_r )
260  { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
262  const Arch::CompatEntry & assertDef( IdString archStr_r )
263  { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
264 
265  const_iterator begin() const
266  { return _compatSet.begin(); }
267 
268  const_iterator end() const
269  { return _compatSet.end(); }
270 
271  struct DumpOnCompare
272  {
273  int operator()( const CompatEntry & lhs, const CompatEntry & rhs ) const
274  { return lhs._idBit.value() < rhs._idBit.value(); }
275  };
276 
277  std::ostream & dumpOn( std::ostream & str ) const
278  {
279  str << "ArchCompatSet:";
280  std::list<CompatEntry> ov( _compatSet.begin(), _compatSet.end() );
281  ov.sort( DumpOnCompare() );
282  for_( it, ov.begin(), ov.end() )
283  {
284  str << endl << ' ' << *it;
285  }
286  return str;
287  }
288 
289  private:
291  ArchCompatSet()
292  {
293  // _noarch must have _idBit 0.
294  // Other builtins have 1-bit set
295  // and are initialized done on the fly.
296  _compatSet.insert( Arch::CompatEntry( a_noarch(), 0 ) );
298  // Define the CompatibleWith relation:
299  //
300  // NOTE: Order of definition is significant! (Arch::compare)
301  // - define compatible (less) architectures first!
302  //
303  defCompatibleWith( a_i386(), a_noarch() );
304  defCompatibleWith( a_i486(), a_noarch(),a_i386() );
305  defCompatibleWith( a_i586(), a_noarch(),a_i386(),a_i486() );
306  defCompatibleWith( a_i686(), a_noarch(),a_i386(),a_i486(),a_i586() );
307  defCompatibleWith( a_athlon(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
308  defCompatibleWith( a_x86_64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon() );
309 
310  defCompatibleWith( a_pentium3(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
311  defCompatibleWith( a_pentium4(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_pentium3() );
312 
313  defCompatibleWith( a_ia64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
314  //
315  defCompatibleWith( a_s390(), a_noarch() );
316  defCompatibleWith( a_s390x(), a_noarch(),a_s390() );
317  //
318  defCompatibleWith( a_ppc(), a_noarch() );
319  defCompatibleWith( a_ppc64(), a_noarch(),a_ppc() );
320  defCompatibleWith( a_ppc64p7(), a_noarch(),a_ppc(),a_ppc64() );
321  //
322  defCompatibleWith( a_ppc64le(), a_noarch() );
323  //
324  defCompatibleWith( a_alpha(), a_noarch() );
325  defCompatibleWith( a_alphaev5(), a_noarch(),a_alpha() );
326  defCompatibleWith( a_alphaev56(), a_noarch(),a_alpha(),a_alphaev5() );
327  defCompatibleWith( a_alphapca56(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56() );
328  defCompatibleWith( a_alphaev6(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56() );
329  defCompatibleWith( a_alphaev67(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56(),a_alphaev6() );
330  //
331  defCompatibleWith( a_sparc(), a_noarch() );
332  defCompatibleWith( a_sparcv8(), a_noarch(),a_sparc() );
333  defCompatibleWith( a_sparcv9(), a_noarch(),a_sparc(),a_sparcv8() );
334  defCompatibleWith( a_sparcv9v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
335  //
336  defCompatibleWith( a_sparc64(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
337  defCompatibleWith( a_sparc64v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9(),a_sparcv9v(),a_sparc64() );
338  //
339  defCompatibleWith( a_armv3l(), a_noarch() );
340  defCompatibleWith( a_armv4l(), a_noarch(),a_armv3l() );
341  defCompatibleWith( a_armv4tl(), a_noarch(),a_armv3l(),a_armv4l() );
342  defCompatibleWith( a_armv5l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl() );
343  defCompatibleWith( a_armv5tl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l() );
344  defCompatibleWith( a_armv5tel(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl() );
345  defCompatibleWith( a_armv5tejl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel() );
346  defCompatibleWith( a_armv6l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl() );
347  defCompatibleWith( a_armv7l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl(),a_armv6l() );
348 
349  defCompatibleWith( a_armv6hl(), a_noarch() );
350  defCompatibleWith( a_armv7hl(), a_noarch(),a_armv6hl() );
351  defCompatibleWith( a_armv7hnl(), a_noarch(),a_armv7hl(),a_armv6hl() );
352  /*legacy: rpm uses v7hnl */
353  defCompatibleWith( a_armv7nhl(), a_noarch(),a_armv7hnl(),a_armv7hl(),a_armv6hl() );
354 
355  /*?*/defCompatibleWith( a_armv7thl(), a_noarch(),a_armv7hl() );
356  /*?*/defCompatibleWith( a_armv7tnhl(), a_noarch(),a_armv7hl(),a_armv7nhl(),a_armv7thl() );
357 
358  defCompatibleWith( a_aarch64(), a_noarch() );
359  //
360  defCompatibleWith( a_sh3(), a_noarch() );
361  //
362  defCompatibleWith( a_sh4(), a_noarch() );
363  defCompatibleWith( a_sh4a(), a_noarch(),a_sh4() );
364 
365  defCompatibleWith( a_m68k(), a_noarch() );
366 
367  defCompatibleWith( a_mips(), a_noarch() );
368  defCompatibleWith( a_mipsel(), a_noarch() );
369  defCompatibleWith( a_mips64(), a_noarch() );
370  defCompatibleWith( a_mips64el(), a_noarch() );
371  //
373  // dumpOn( USR ) << endl;
374  }
375 
376  private:
382  CompatBits::IntT nextIdBit() const
383  {
384  if ( CompatBits::size == _compatSet.size() )
385  {
386  // Provide more bits in CompatBits::IntT
387  INT << "Need more than " << CompatBits::size << " bits to encode architectures." << endl;
388  ZYPP_THROW( Exception("Need more bits to encode architectures.") );
389  }
390  CompatBits::IntT nextBit = CompatBits::IntT(1) << (_compatSet.size());
391  return nextBit;
392  }
393 
397  const CompatEntry & assertCompatSetEntry( IdString archStr_r )
398  { return *_compatSet.insert( Arch::CompatEntry( archStr_r, nextIdBit() ) ).first; }
399 
402  void defCompatibleWith( IdString targetArch_r,
403  IdString arch0_r,
404  IdString arch1_r = IdString(),
405  IdString arch2_r = IdString(),
406  IdString arch3_r = IdString(),
407  IdString arch4_r = IdString(),
408  IdString arch5_r = IdString(),
409  IdString arch6_r = IdString(),
410  IdString arch7_r = IdString(),
411  IdString arch8_r = IdString(),
412  IdString arch9_r = IdString() )
413  {
414  const CompatEntry & target( assertCompatSetEntry( targetArch_r ) );
415  target.addCompatBit( assertCompatSetEntry( arch0_r )._idBit );
416 #define SETARG(N) if ( arch##N##_r.empty() ) return; target.addCompatBit( assertCompatSetEntry( arch##N##_r )._idBit )
417  SETARG(1); SETARG(2); SETARG(3); SETARG(4);
418  SETARG(5); SETARG(6); SETARG(7); SETARG(8); SETARG(9);
419 #undef SETARG
420  }
421 
422  private:
424  };
425 
427  } // namespace
429 
431  //
432  // CLASS NAME : Arch
433  //
435 
436  const Arch Arch_empty ( IdString::Empty );
437  // remaining Arch_* constants are defined by DEF_BUILTIN above.
438 
440  //
441  // METHOD NAME : Arch::Arch
442  // METHOD TYPE : Ctor
443  //
445  : _entry( &ArchCompatSet::instance().assertDef( a_noarch() ) )
446  {}
447 
449  : _entry( &ArchCompatSet::instance().assertDef( IdString(id_r) ) )
450  {}
451 
452  Arch::Arch( const IdString & idstr_r )
453  : _entry( &ArchCompatSet::instance().assertDef( idstr_r ) )
454  {}
455 
456  Arch::Arch( const std::string & str_r )
457  : _entry( &ArchCompatSet::instance().assertDef( str_r ) )
458  {}
459 
460  Arch::Arch( const char * cstr_r )
461  : _entry( &ArchCompatSet::instance().assertDef( cstr_r ) )
462  {}
463 
464  Arch::Arch( const CompatEntry & rhs )
465  : _entry( &rhs )
466  {}
467 
469  //
470  // METHOD NAME : Arch::idStr
471  // METHOD TYPE : IdString
472  //
474  { return _entry->_idStr; }
475 
477  //
478  // METHOD NAME : Arch::asString
479  // METHOD TYPE : const std::string &
480  //
481  const std::string & Arch::asString() const
482  { return _entry->_archStr; }
483 
485  //
486  // METHOD NAME : Arch::isBuiltIn
487  // METHOD TYPE : bool
488  //
489  bool Arch::isBuiltIn() const
490  { return _entry->isBuiltIn(); }
491 
493  //
494  // METHOD NAME : Arch::compatibleWith
495  // METHOD TYPE : bool
496  //
497  bool Arch::compatibleWith( const Arch & targetArch_r ) const
498  { return _entry->compatibleWith( *targetArch_r._entry ); }
499 
501  //
502  // METHOD NAME : Arch::baseArch
503  // METHOD TYPE : Arch
504  //
506  {
507  // check the multilib archs:
508  if (Arch_x86_64.compatibleWith(*this))
509  {
510  return Arch_x86_64;
511  }
512  if (Arch_sparc64v.compatibleWith(*this))
513  {
514  return Arch_sparc64v;
515  }
516  if (Arch_sparc64.compatibleWith(*this))
517  {
518  return Arch_sparc64;
519  }
520  if (Arch_ppc64.compatibleWith(*this))
521  {
522  return Arch_ppc64;
523  }
524  if (Arch_s390x.compatibleWith(*this))
525  {
526  return Arch_s390x;
527  }
528  // Here: no multilib; return arch before noarch
529  CompatSet cset( compatSet( *this ) );
530  if ( cset.size() > 2 ) // systemArchitecture, ..., basearch, noarch
531  {
532  return *(++cset.rbegin());
533  }
534  return *this;
535  }
536 
538  //
539  // METHOD NAME : Arch::compare
540  // METHOD TYPE : bool
541  //
542  int Arch::compare( const Arch & rhs ) const
543  { return _entry->compare( *rhs._entry ); }
544 
546  //
547  // METHOD NAME : Arch::compatSet
548  // METHOD TYPE : Arch::CompatSet
549  //
550  Arch::CompatSet Arch::compatSet( const Arch & targetArch_r )
551  {
552  Arch::CompatSet ret;
553 
554  for ( ArchCompatSet::const_iterator it = ArchCompatSet::instance().begin();
555  it != ArchCompatSet::instance().end(); ++it )
556  {
557  if ( it->compatibleWith( *targetArch_r._entry ) )
558  {
559  ret.insert( Arch(*it) );
560  }
561  }
562 
563  return ret;
564  }
565 
567 } // namespace zypp
const Arch Arch_sparc64
Definition: Arch.h:219
bool compatibleWith(const Arch &targetArch_r) const
Compatibility relation.
Definition: Arch.cc:497
int compare(const CompatEntry &rhs) const
compare by score, then archStr.
Definition: Arch.cc:89
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:350
const std::string & asString() const
Definition: Arch.cc:481
Architecture.
Definition: Arch.h:36
IdType id() const
Expert backdoor.
Definition: IdString.h:115
std::set< Arch, CompareByGT< Arch > > CompatSet
Reversed arch order, best Arch first.
Definition: Arch.h:117
#define INT
Definition: Logger.h:68
const Arch Arch_empty(IdString::Empty)
IdString idStr() const
String representation of Arch.
Definition: Arch.cc:473
Access to the sat-pools string space.
Definition: IdString.h:41
const Arch Arch_s390x
Definition: Arch.h:186
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
CompatEntry(const std::string &archStr_r, CompatBits::IntT idBit_r=1)
Definition: Arch.cc:46
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
#define SETARG(N)
int compare(const Arch &rhs) const
Arch comparison.
Definition: Arch.cc:542
Set _compatSet
Definition: Arch.cc:423
const CompatEntry * _entry
Definition: Arch.h:145
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
static const IdString Empty
Empty string.
Definition: IdString.h:70
sat::detail::IdType IdType
Definition: IdString.h:44
std::ostream & operator<<(std::ostream &str, const Arch::CompatEntry &obj)
Definition: Arch.cc:110
DEF_BUILTIN(noarch)
void addCompatBit(const CompatBits &idBit_r) const
Definition: Arch.cc:62
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:444
bit::BitField< uint64_t > CompatBits
Bitfield for architecture IDs and compatBits relation.
Definition: Arch.cc:44
std::string numstring(char n, int w=0)
Definition: String.h:305
static CompatSet compatSet(const Arch &targetArch_r)
Return a set of all Arch's compatibleWith a targetArch_r.
Definition: Arch.cc:550
const Arch Arch_x86_64
Definition: Arch.h:173
TInt value() const
Return the value.
Definition: Bit.h:179
const Arch Arch_ppc64
Definition: Arch.h:196
bool operator!=(const Arch::CompatEntry &lhs, const Arch::CompatEntry &rhs)
Definition: Arch.cc:127
const Arch Arch_sparc64v
Definition: Arch.h:217
bool isBuiltIn() const
Definition: Arch.cc:96
Arch()
Default ctor Arc_noarch.
Definition: Arch.cc:444
ZYPP_DEFINE_ID_HASHABLE(zypp::Arch::CompatEntry)
bool compatibleWith(const CompatEntry &targetEntry_r) const
Return whether this is compatible with targetEntry_r.
Definition: Arch.cc:71
IdString::IdType id() const
Definition: Arch.cc:99
Arch baseArch() const
Definition: Arch.cc:505
bool isBuiltIn() const
Whether this is a buitin (or known) architecture.
Definition: Arch.cc:489
CompatBits _compatBits
Definition: Arch.cc:105
std::string _archStr
Definition: Arch.cc:103
bool operator==(const Arch::CompatEntry &lhs, const Arch::CompatEntry &rhs)
Definition: Arch.cc:124
CompatEntry(IdString archStr_r, CompatBits::IntT idBit_r=1)
Definition: Arch.cc:54
CompatBits _idBit
Definition: Arch.cc:104
Holds an architecture ID and it's compatible relation.
Definition: Arch.cc:38