libzypp 17.37.17
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>
19#include <zypp/base/Hash.h>
20#include <zypp/Arch.h>
21#include <zypp/Bit.h>
22
23using std::endl;
24
26namespace 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
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 };
107
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; }
126
127 inline bool operator!=( const Arch::CompatEntry & lhs, const Arch::CompatEntry & rhs )
128 { return ! ( lhs == rhs ); }
129
131} // namespace zypp
133
135
137namespace 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 DEF_BUILTIN( x86_64_v2 );
165 DEF_BUILTIN( x86_64_v3 );
166 DEF_BUILTIN( x86_64_v4 );
167
168 DEF_BUILTIN( pentium3 );
169 DEF_BUILTIN( pentium4 );
170
171 DEF_BUILTIN( s390 );
172 DEF_BUILTIN( s390x );
173
175 DEF_BUILTIN( ppc64 );
176 DEF_BUILTIN( ppc64p7 );
177
178 DEF_BUILTIN( ppc64le );
179
180 DEF_BUILTIN( ia64 );
181
182 DEF_BUILTIN( alphaev67 );
183 DEF_BUILTIN( alphaev6 );
184 DEF_BUILTIN( alphapca56 );
185 DEF_BUILTIN( alphaev56 );
186 DEF_BUILTIN( alphaev5 );
187 DEF_BUILTIN( alpha );
188
189 DEF_BUILTIN( sparc64v );
190 DEF_BUILTIN( sparcv9v );
191 DEF_BUILTIN( sparc64 );
192 DEF_BUILTIN( sparcv9 );
193 DEF_BUILTIN( sparcv8 );
194 DEF_BUILTIN( sparc );
195
196 DEF_BUILTIN( aarch64 );
197
198 DEF_BUILTIN( armv7tnhl ); /* exists? */
199 DEF_BUILTIN( armv7thl ); /* exists? */
200
201 DEF_BUILTIN( armv7hnl ); /* legacy: */DEF_BUILTIN( armv7nhl );
202 DEF_BUILTIN( armv8hl );
203 DEF_BUILTIN( armv7hl );
204 DEF_BUILTIN( armv6hl );
205
206 DEF_BUILTIN( armv8l );
207 DEF_BUILTIN( armv7l );
208 DEF_BUILTIN( armv6l );
209 DEF_BUILTIN( armv5tejl );
210 DEF_BUILTIN( armv5tel );
211 DEF_BUILTIN( armv5tl );
212 DEF_BUILTIN( armv5l );
213 DEF_BUILTIN( armv4tl );
214 DEF_BUILTIN( armv4l );
215 DEF_BUILTIN( armv3l );
216
217 DEF_BUILTIN( riscv64 );
218
220
222 DEF_BUILTIN( sh4a );
223
224 DEF_BUILTIN( m68k );
225
226 DEF_BUILTIN( mips );
227 DEF_BUILTIN( mipsel );
228 DEF_BUILTIN( mips64 );
229 DEF_BUILTIN( mips64el );
230
231 DEF_BUILTIN( loong64 );
232#undef DEF_BUILTIN
233
235 namespace
236 {
237
239 //
240 // CLASS NAME : CompatSet
241 //
249 struct ArchCompatSet : private base::NonCopyable
250 {
251 using CompatEntry = Arch::CompatEntry;
252 using CompatBits = CompatEntry::CompatBits;
253
254 using Set = std::unordered_set<CompatEntry>;
255 using iterator = Set::iterator;
256 using const_iterator = Set::const_iterator;
257
259 static ArchCompatSet & instance()
260 {
261 static ArchCompatSet _instance;
262 return _instance;
263 }
264
268 const Arch::CompatEntry & assertDef( const std::string & archStr_r )
269 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
271 const Arch::CompatEntry & assertDef( IdString archStr_r )
272 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
273
274 const_iterator begin() const
275 { return _compatSet.begin(); }
276
277 const_iterator end() const
278 { return _compatSet.end(); }
279
280 struct DumpOnCompare
281 {
282 int operator()( const CompatEntry & lhs, const CompatEntry & rhs ) const
283 { return lhs._idBit.value() < rhs._idBit.value(); }
284 };
285
286 std::ostream & dumpOn( std::ostream & str ) const
287 {
288 str << "ArchCompatSet:";
289 std::list<CompatEntry> ov( _compatSet.begin(), _compatSet.end() );
290 ov.sort( DumpOnCompare() );
291 for_( it, ov.begin(), ov.end() )
292 {
293 str << endl << ' ' << *it;
294 }
295 return str;
296 }
297
298 private:
300 ArchCompatSet()
301 {
302 // _noarch must have _idBit 0.
303 // Other builtins have 1-bit set
304 // and are initialized done on the fly.
305 _compatSet.insert( Arch::CompatEntry( a_noarch(), 0 ) );
307 // Define the CompatibleWith relation:
308 //
309 // NOTE: Order of definition is significant! (Arch::compare)
310 // - define compatible (less) architectures first!
311 //
312 defCompatibleWith( a_i386(), a_noarch() );
313 defCompatibleWith( a_i486(), a_noarch(),a_i386() );
314 defCompatibleWith( a_i586(), a_noarch(),a_i386(),a_i486() );
315 defCompatibleWith( a_i686(), a_noarch(),a_i386(),a_i486(),a_i586() );
316 defCompatibleWith( a_athlon(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
317 defCompatibleWith( a_x86_64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon() );
318 defCompatibleWith( a_x86_64_v2(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon(),a_x86_64() );
319 defCompatibleWith( a_x86_64_v3(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon(),a_x86_64(),a_x86_64_v2() );
320 defCompatibleWith( a_x86_64_v4(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon(),a_x86_64(),a_x86_64_v2(),a_x86_64_v3() );
321
322 defCompatibleWith( a_pentium3(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
323 defCompatibleWith( a_pentium4(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_pentium3() );
324
325 defCompatibleWith( a_ia64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
326 //
327 defCompatibleWith( a_s390(), a_noarch() );
328 defCompatibleWith( a_s390x(), a_noarch(),a_s390() );
329 //
330 defCompatibleWith( a_ppc(), a_noarch() );
331 defCompatibleWith( a_ppc64(), a_noarch(),a_ppc() );
332 defCompatibleWith( a_ppc64p7(), a_noarch(),a_ppc(),a_ppc64() );
333 //
334 defCompatibleWith( a_ppc64le(), a_noarch() );
335 //
336 defCompatibleWith( a_alpha(), a_noarch() );
337 defCompatibleWith( a_alphaev5(), a_noarch(),a_alpha() );
338 defCompatibleWith( a_alphaev56(), a_noarch(),a_alpha(),a_alphaev5() );
339 defCompatibleWith( a_alphapca56(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56() );
340 defCompatibleWith( a_alphaev6(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56() );
341 defCompatibleWith( a_alphaev67(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56(),a_alphaev6() );
342 //
343 defCompatibleWith( a_sparc(), a_noarch() );
344 defCompatibleWith( a_sparcv8(), a_noarch(),a_sparc() );
345 defCompatibleWith( a_sparcv9(), a_noarch(),a_sparc(),a_sparcv8() );
346 defCompatibleWith( a_sparcv9v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
347 //
348 defCompatibleWith( a_sparc64(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
349 defCompatibleWith( a_sparc64v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9(),a_sparcv9v(),a_sparc64() );
350 //
351 defCompatibleWith( a_armv3l(), a_noarch() );
352 defCompatibleWith( a_armv4l(), a_noarch(),a_armv3l() );
353 defCompatibleWith( a_armv4tl(), a_noarch(),a_armv3l(),a_armv4l() );
354 defCompatibleWith( a_armv5l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl() );
355 defCompatibleWith( a_armv5tl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l() );
356 defCompatibleWith( a_armv5tel(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl() );
357 defCompatibleWith( a_armv5tejl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel() );
358 defCompatibleWith( a_armv6l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl() );
359 defCompatibleWith( a_armv7l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl(),a_armv6l() );
360 defCompatibleWith( a_armv8l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl(),a_armv6l(),a_armv7l() );
361
362 defCompatibleWith( a_armv6hl(), a_noarch() );
363 defCompatibleWith( a_armv7hl(), a_noarch(),a_armv6hl() );
364 defCompatibleWith( a_armv8hl(), a_noarch(),a_armv7hl() );
365 defCompatibleWith( a_armv7hnl(), a_noarch(),a_armv7hl(),a_armv6hl() );
366 /*legacy: rpm uses v7hnl */
367 defCompatibleWith( a_armv7nhl(), a_noarch(),a_armv7hnl(),a_armv7hl(),a_armv6hl() );
368
369 /*?*/defCompatibleWith( a_armv7thl(), a_noarch(),a_armv7hl() );
370 /*?*/defCompatibleWith( a_armv7tnhl(), a_noarch(),a_armv7hl(),a_armv7nhl(),a_armv7thl() );
371
372 defCompatibleWith( a_aarch64(), a_noarch() );
373 //
374 defCompatibleWith( a_riscv64(), a_noarch() );
375 //
376 defCompatibleWith( a_sh3(), a_noarch() );
377 //
378 defCompatibleWith( a_sh4(), a_noarch() );
379 defCompatibleWith( a_sh4a(), a_noarch(),a_sh4() );
380
381 defCompatibleWith( a_m68k(), a_noarch() );
382
383 defCompatibleWith( a_mips(), a_noarch() );
384 defCompatibleWith( a_mipsel(), a_noarch() );
385 defCompatibleWith( a_mips64(), a_noarch() );
386 defCompatibleWith( a_mips64el(), a_noarch() );
387
388 defCompatibleWith( a_loong64(), a_noarch() );
389 //
391 // dumpOn( USR ) << endl;
392 }
393
394 private:
400 CompatBits::IntT nextIdBit() const
401 {
402 if ( CompatBits::size == _compatSet.size() )
403 {
404 // Provide more bits in CompatBits::IntT
405 INT << "Need more than " << CompatBits::size << " bits to encode architectures." << endl;
406 ZYPP_THROW( Exception("Need more bits to encode architectures.") );
407 }
408 CompatBits::IntT nextBit = CompatBits::IntT(1) << (_compatSet.size());
409 return nextBit;
410 }
411
415 const CompatEntry & assertCompatSetEntry( IdString archStr_r )
416 { return *_compatSet.insert( Arch::CompatEntry( archStr_r, nextIdBit() ) ).first; }
417
420 void defCompatibleWith( IdString targetArch_r,
421 IdString arch0_r,
422 IdString arch1_r = IdString(),
423 IdString arch2_r = IdString(),
424 IdString arch3_r = IdString(),
425 IdString arch4_r = IdString(),
426 IdString arch5_r = IdString(),
427 IdString arch6_r = IdString(),
428 IdString arch7_r = IdString(),
429 IdString arch8_r = IdString(),
430 IdString arch9_r = IdString() )
431 {
432 const CompatEntry & target( assertCompatSetEntry( targetArch_r ) );
433 target.addCompatBit( assertCompatSetEntry( arch0_r )._idBit );
434#define SETARG(N) if ( arch##N##_r.empty() ) return; target.addCompatBit( assertCompatSetEntry( arch##N##_r )._idBit )
435 SETARG(1); SETARG(2); SETARG(3); SETARG(4);
436 SETARG(5); SETARG(6); SETARG(7); SETARG(8); SETARG(9);
437#undef SETARG
438 }
439
440 private:
441 Set _compatSet;
442 };
443
445 } // namespace
447
449 //
450 // CLASS NAME : Arch
451 //
453
455 // remaining Arch_* constants are defined by DEF_BUILTIN above.
456
458 //
459 // METHOD NAME : Arch::Arch
460 // METHOD TYPE : Ctor
461 //
463 : _entry( &ArchCompatSet::instance().assertDef( a_noarch() ) )
464 {}
465
467 : _entry( &ArchCompatSet::instance().assertDef( IdString(id_r) ) )
468 {}
469
470 Arch::Arch( const IdString & idstr_r )
471 : _entry( &ArchCompatSet::instance().assertDef( idstr_r ) )
472 {}
473
474 Arch::Arch( const std::string & str_r )
475 : _entry( &ArchCompatSet::instance().assertDef( str_r ) )
476 {}
477
478 Arch::Arch( const char * cstr_r )
479 : _entry( &ArchCompatSet::instance().assertDef( cstr_r ) )
480 {}
481
482 Arch::Arch( const CompatEntry & rhs )
483 : _entry( &rhs )
484 {}
485
487 //
488 // METHOD NAME : Arch::idStr
489 // METHOD TYPE : IdString
490 //
492 { return _entry->_idStr; }
493
495 //
496 // METHOD NAME : Arch::asString
497 // METHOD TYPE : const std::string &
498 //
499 const std::string & Arch::asString() const
500 { return _entry->_archStr; }
501
503 //
504 // METHOD NAME : Arch::isBuiltIn
505 // METHOD TYPE : bool
506 //
507 bool Arch::isBuiltIn() const
508 { return _entry->isBuiltIn(); }
509
511 //
512 // METHOD NAME : Arch::compatibleWith
513 // METHOD TYPE : bool
514 //
515 bool Arch::compatibleWith( const Arch & targetArch_r ) const
516 { return _entry->compatibleWith( *targetArch_r._entry ); }
517
519 //
520 // METHOD NAME : Arch::baseArch
521 // METHOD TYPE : Arch
522 //
524 {
525 // check the multilib archs:
526 if (Arch_x86_64.compatibleWith(*this))
527 {
528 return Arch_x86_64;
529 }
530 if (Arch_sparc64v.compatibleWith(*this))
531 {
532 return Arch_sparc64v;
533 }
534 if (Arch_sparc64.compatibleWith(*this))
535 {
536 return Arch_sparc64;
537 }
538 if (Arch_ppc64.compatibleWith(*this))
539 {
540 return Arch_ppc64;
541 }
542 if (Arch_s390x.compatibleWith(*this))
543 {
544 return Arch_s390x;
545 }
546 // Here: no multilib; return arch before noarch
547 CompatSet cset( compatSet( *this ) );
548 if ( cset.size() > 2 ) // systemArchitecture, ..., basearch, noarch
549 {
550 return *(++cset.rbegin());
551 }
552 return *this;
553 }
554
556 //
557 // METHOD NAME : Arch::compare
558 // METHOD TYPE : bool
559 //
560 int Arch::compare( const Arch & rhs ) const
561 { return _entry->compare( *rhs._entry ); }
562
564 //
565 // METHOD NAME : Arch::compatSet
566 // METHOD TYPE : Arch::CompatSet
567 //
568 Arch::CompatSet Arch::compatSet( const Arch & targetArch_r )
569 {
570 Arch::CompatSet ret;
571
572 for ( ArchCompatSet::const_iterator it = ArchCompatSet::instance().begin();
573 it != ArchCompatSet::instance().end(); ++it )
574 {
575 if ( it->compatibleWith( *targetArch_r._entry ) )
576 {
577 ret.insert( Arch(*it) );
578 }
579 }
580
581 return ret;
582 }
583
585} // namespace zypp
#define SETARG(N)
#define DEF_BUILTIN(A)
Definition Arch.cc:152
Architecture.
Definition Arch.h:37
const CompatEntry * _entry
Definition Arch.h:148
Arch baseArch() const
Definition Arch.cc:523
int compare(const Arch &rhs) const
Arch comparison.
Definition Arch.cc:560
Arch()
Default ctor Arc_noarch.
Definition Arch.cc:462
static CompatSet compatSet(const Arch &targetArch_r)
Return a set of all Arch's compatibleWith a targetArch_r.
Definition Arch.cc:568
bool isBuiltIn() const
Whether this is a buitin (or known) architecture.
Definition Arch.cc:507
IdString idStr() const
String representation of Arch.
Definition Arch.cc:491
const std::string & asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Arch.cc:499
std::set< Arch, CompareByGT< Arch > > CompatSet
Reversed arch order, best Arch first.
Definition Arch.h:117
bool compatibleWith(const Arch &targetArch_r) const
Compatibility relation.
Definition Arch.cc:515
bool operator()(const zypp::Arch &lhs, const zypp::Arch &rhs) const
Default order for std::container based Arch::compare.
Definition Arch.h:370
Access to the sat-pools string space.
Definition IdString.h:44
sat::detail::IdType IdType
Definition IdString.h:46
static const IdString Empty
Empty string.
Definition IdString.h:78
An integral type used as BitField.
Definition Bit.h:160
TInt value() const
Return the value.
Definition Bit.h:179
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
std::string numstring(char n, int w=0)
Definition String.h:290
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
const Arch Arch_empty(IdString::Empty)
zypp::IdString IdString
Definition idstring.h:16
Holds an architecture ID and its compatible relation.
Definition Arch.cc:39
void addCompatBit(const CompatBits &idBit_r) const
Definition Arch.cc:62
std::ostream & operator<<(std::ostream &str, const Arch::CompatEntry &obj)
Stream output.
Definition Arch.cc:110
bool compatibleWith(const CompatEntry &targetEntry_r) const
Return whether this is compatible with targetEntry_r.
Definition Arch.cc:71
int compare(const CompatEntry &rhs) const
compare by score, then archStr.
Definition Arch.cc:89
bool operator==(const Arch::CompatEntry &lhs, const Arch::CompatEntry &rhs)
Definition Arch.cc:124
IdString::IdType id() const
Definition Arch.cc:99
bool operator!=(const Arch::CompatEntry &lhs, const Arch::CompatEntry &rhs)
Definition Arch.cc:127
CompatBits _idBit
Definition Arch.cc:104
std::string _archStr
Definition Arch.cc:103
bool isBuiltIn() const
Definition Arch.cc:96
bit::BitField< uint64_t > CompatBits
Bitfield for architecture IDs and compatBits relation.
Definition Arch.cc:44
CompatEntry(IdString archStr_r, CompatBits::IntT idBit_r=1)
Definition Arch.cc:54
CompatBits _compatBits
Definition Arch.cc:105
CompatEntry(const std::string &archStr_r, CompatBits::IntT idBit_r=1)
Definition Arch.cc:46
static const unsigned size
Definition Bit.h:88
TInt IntT
Definition Bit.h:83
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define ZYPP_DEFINE_ID_HASHABLE(C)
Define hash function for id based classes.
Definition Hash.h:26
#define INT
Definition Logger.h:104