libzypp 17.31.0
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
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 };
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
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
165 DEF_BUILTIN( pentium3 );
166 DEF_BUILTIN( pentium4 );
167
168 DEF_BUILTIN( s390 );
169 DEF_BUILTIN( s390x );
170
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( armv8hl );
200 DEF_BUILTIN( armv7hl );
201 DEF_BUILTIN( armv6hl );
202
203 DEF_BUILTIN( armv8l );
204 DEF_BUILTIN( armv7l );
205 DEF_BUILTIN( armv6l );
206 DEF_BUILTIN( armv5tejl );
207 DEF_BUILTIN( armv5tel );
208 DEF_BUILTIN( armv5tl );
209 DEF_BUILTIN( armv5l );
210 DEF_BUILTIN( armv4tl );
211 DEF_BUILTIN( armv4l );
212 DEF_BUILTIN( armv3l );
213
214 DEF_BUILTIN( riscv64 );
215
217
219 DEF_BUILTIN( sh4a );
220
221 DEF_BUILTIN( m68k );
222
223 DEF_BUILTIN( mips );
224 DEF_BUILTIN( mipsel );
225 DEF_BUILTIN( mips64 );
226 DEF_BUILTIN( mips64el );
227#undef DEF_BUILTIN
228
230 namespace
231 {
232
234 //
235 // CLASS NAME : CompatSet
236 //
244 struct ArchCompatSet : private base::NonCopyable
245 {
246 typedef Arch::CompatEntry CompatEntry;
247 typedef CompatEntry::CompatBits CompatBits;
248
249 typedef std::unordered_set<CompatEntry> Set;
250 typedef Set::iterator iterator;
251 typedef Set::const_iterator const_iterator;
252
254 static ArchCompatSet & instance()
255 {
256 static ArchCompatSet _instance;
257 return _instance;
258 }
259
263 const Arch::CompatEntry & assertDef( const std::string & archStr_r )
264 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
266 const Arch::CompatEntry & assertDef( IdString archStr_r )
267 { return *_compatSet.insert( Arch::CompatEntry( archStr_r ) ).first; }
268
269 const_iterator begin() const
270 { return _compatSet.begin(); }
271
272 const_iterator end() const
273 { return _compatSet.end(); }
274
275 struct DumpOnCompare
276 {
277 int operator()( const CompatEntry & lhs, const CompatEntry & rhs ) const
278 { return lhs._idBit.value() < rhs._idBit.value(); }
279 };
280
281 std::ostream & dumpOn( std::ostream & str ) const
282 {
283 str << "ArchCompatSet:";
284 std::list<CompatEntry> ov( _compatSet.begin(), _compatSet.end() );
285 ov.sort( DumpOnCompare() );
286 for_( it, ov.begin(), ov.end() )
287 {
288 str << endl << ' ' << *it;
289 }
290 return str;
291 }
292
293 private:
295 ArchCompatSet()
296 {
297 // _noarch must have _idBit 0.
298 // Other builtins have 1-bit set
299 // and are initialized done on the fly.
300 _compatSet.insert( Arch::CompatEntry( a_noarch(), 0 ) );
302 // Define the CompatibleWith relation:
303 //
304 // NOTE: Order of definition is significant! (Arch::compare)
305 // - define compatible (less) architectures first!
306 //
307 defCompatibleWith( a_i386(), a_noarch() );
308 defCompatibleWith( a_i486(), a_noarch(),a_i386() );
309 defCompatibleWith( a_i586(), a_noarch(),a_i386(),a_i486() );
310 defCompatibleWith( a_i686(), a_noarch(),a_i386(),a_i486(),a_i586() );
311 defCompatibleWith( a_athlon(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
312 defCompatibleWith( a_x86_64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_athlon() );
313
314 defCompatibleWith( a_pentium3(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
315 defCompatibleWith( a_pentium4(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686(),a_pentium3() );
316
317 defCompatibleWith( a_ia64(), a_noarch(),a_i386(),a_i486(),a_i586(),a_i686() );
318 //
319 defCompatibleWith( a_s390(), a_noarch() );
320 defCompatibleWith( a_s390x(), a_noarch(),a_s390() );
321 //
322 defCompatibleWith( a_ppc(), a_noarch() );
323 defCompatibleWith( a_ppc64(), a_noarch(),a_ppc() );
324 defCompatibleWith( a_ppc64p7(), a_noarch(),a_ppc(),a_ppc64() );
325 //
326 defCompatibleWith( a_ppc64le(), a_noarch() );
327 //
328 defCompatibleWith( a_alpha(), a_noarch() );
329 defCompatibleWith( a_alphaev5(), a_noarch(),a_alpha() );
330 defCompatibleWith( a_alphaev56(), a_noarch(),a_alpha(),a_alphaev5() );
331 defCompatibleWith( a_alphapca56(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56() );
332 defCompatibleWith( a_alphaev6(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56() );
333 defCompatibleWith( a_alphaev67(), a_noarch(),a_alpha(),a_alphaev5(),a_alphaev56(),a_alphapca56(),a_alphaev6() );
334 //
335 defCompatibleWith( a_sparc(), a_noarch() );
336 defCompatibleWith( a_sparcv8(), a_noarch(),a_sparc() );
337 defCompatibleWith( a_sparcv9(), a_noarch(),a_sparc(),a_sparcv8() );
338 defCompatibleWith( a_sparcv9v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
339 //
340 defCompatibleWith( a_sparc64(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9() );
341 defCompatibleWith( a_sparc64v(), a_noarch(),a_sparc(),a_sparcv8(),a_sparcv9(),a_sparcv9v(),a_sparc64() );
342 //
343 defCompatibleWith( a_armv3l(), a_noarch() );
344 defCompatibleWith( a_armv4l(), a_noarch(),a_armv3l() );
345 defCompatibleWith( a_armv4tl(), a_noarch(),a_armv3l(),a_armv4l() );
346 defCompatibleWith( a_armv5l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl() );
347 defCompatibleWith( a_armv5tl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l() );
348 defCompatibleWith( a_armv5tel(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl() );
349 defCompatibleWith( a_armv5tejl(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel() );
350 defCompatibleWith( a_armv6l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl() );
351 defCompatibleWith( a_armv7l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl(),a_armv6l() );
352 defCompatibleWith( a_armv8l(), a_noarch(),a_armv3l(),a_armv4l(),a_armv4tl(),a_armv5l(),a_armv5tl(),a_armv5tel(),a_armv5tejl(),a_armv6l(),a_armv7l() );
353
354 defCompatibleWith( a_armv6hl(), a_noarch() );
355 defCompatibleWith( a_armv7hl(), a_noarch(),a_armv6hl() );
356 defCompatibleWith( a_armv8hl(), a_noarch(),a_armv7hl() );
357 defCompatibleWith( a_armv7hnl(), a_noarch(),a_armv7hl(),a_armv6hl() );
358 /*legacy: rpm uses v7hnl */
359 defCompatibleWith( a_armv7nhl(), a_noarch(),a_armv7hnl(),a_armv7hl(),a_armv6hl() );
360
361 /*?*/defCompatibleWith( a_armv7thl(), a_noarch(),a_armv7hl() );
362 /*?*/defCompatibleWith( a_armv7tnhl(), a_noarch(),a_armv7hl(),a_armv7nhl(),a_armv7thl() );
363
364 defCompatibleWith( a_aarch64(), a_noarch() );
365 //
366 defCompatibleWith( a_riscv64(), a_noarch() );
367 //
368 defCompatibleWith( a_sh3(), a_noarch() );
369 //
370 defCompatibleWith( a_sh4(), a_noarch() );
371 defCompatibleWith( a_sh4a(), a_noarch(),a_sh4() );
372
373 defCompatibleWith( a_m68k(), a_noarch() );
374
375 defCompatibleWith( a_mips(), a_noarch() );
376 defCompatibleWith( a_mipsel(), a_noarch() );
377 defCompatibleWith( a_mips64(), a_noarch() );
378 defCompatibleWith( a_mips64el(), a_noarch() );
379 //
381 // dumpOn( USR ) << endl;
382 }
383
384 private:
390 CompatBits::IntT nextIdBit() const
391 {
392 if ( CompatBits::size == _compatSet.size() )
393 {
394 // Provide more bits in CompatBits::IntT
395 INT << "Need more than " << CompatBits::size << " bits to encode architectures." << endl;
396 ZYPP_THROW( Exception("Need more bits to encode architectures.") );
397 }
398 CompatBits::IntT nextBit = CompatBits::IntT(1) << (_compatSet.size());
399 return nextBit;
400 }
401
405 const CompatEntry & assertCompatSetEntry( IdString archStr_r )
406 { return *_compatSet.insert( Arch::CompatEntry( archStr_r, nextIdBit() ) ).first; }
407
410 void defCompatibleWith( IdString targetArch_r,
411 IdString arch0_r,
412 IdString arch1_r = IdString(),
413 IdString arch2_r = IdString(),
414 IdString arch3_r = IdString(),
415 IdString arch4_r = IdString(),
416 IdString arch5_r = IdString(),
417 IdString arch6_r = IdString(),
418 IdString arch7_r = IdString(),
419 IdString arch8_r = IdString(),
420 IdString arch9_r = IdString() )
421 {
422 const CompatEntry & target( assertCompatSetEntry( targetArch_r ) );
423 target.addCompatBit( assertCompatSetEntry( arch0_r )._idBit );
424#define SETARG(N) if ( arch##N##_r.empty() ) return; target.addCompatBit( assertCompatSetEntry( arch##N##_r )._idBit )
425 SETARG(1); SETARG(2); SETARG(3); SETARG(4);
426 SETARG(5); SETARG(6); SETARG(7); SETARG(8); SETARG(9);
427#undef SETARG
428 }
429
430 private:
432 };
433
435 } // namespace
437
439 //
440 // CLASS NAME : Arch
441 //
443
445 // remaining Arch_* constants are defined by DEF_BUILTIN above.
446
448 //
449 // METHOD NAME : Arch::Arch
450 // METHOD TYPE : Ctor
451 //
453 : _entry( &ArchCompatSet::instance().assertDef( a_noarch() ) )
454 {}
455
457 : _entry( &ArchCompatSet::instance().assertDef( IdString(id_r) ) )
458 {}
459
460 Arch::Arch( const IdString & idstr_r )
461 : _entry( &ArchCompatSet::instance().assertDef( idstr_r ) )
462 {}
463
464 Arch::Arch( const std::string & str_r )
465 : _entry( &ArchCompatSet::instance().assertDef( str_r ) )
466 {}
467
468 Arch::Arch( const char * cstr_r )
469 : _entry( &ArchCompatSet::instance().assertDef( cstr_r ) )
470 {}
471
472 Arch::Arch( const CompatEntry & rhs )
473 : _entry( &rhs )
474 {}
475
477 //
478 // METHOD NAME : Arch::idStr
479 // METHOD TYPE : IdString
480 //
482 { return _entry->_idStr; }
483
485 //
486 // METHOD NAME : Arch::asString
487 // METHOD TYPE : const std::string &
488 //
489 const std::string & Arch::asString() const
490 { return _entry->_archStr; }
491
493 //
494 // METHOD NAME : Arch::isBuiltIn
495 // METHOD TYPE : bool
496 //
497 bool Arch::isBuiltIn() const
498 { return _entry->isBuiltIn(); }
499
501 //
502 // METHOD NAME : Arch::compatibleWith
503 // METHOD TYPE : bool
504 //
505 bool Arch::compatibleWith( const Arch & targetArch_r ) const
506 { return _entry->compatibleWith( *targetArch_r._entry ); }
507
509 //
510 // METHOD NAME : Arch::baseArch
511 // METHOD TYPE : Arch
512 //
514 {
515 // check the multilib archs:
516 if (Arch_x86_64.compatibleWith(*this))
517 {
518 return Arch_x86_64;
519 }
520 if (Arch_sparc64v.compatibleWith(*this))
521 {
522 return Arch_sparc64v;
523 }
524 if (Arch_sparc64.compatibleWith(*this))
525 {
526 return Arch_sparc64;
527 }
528 if (Arch_ppc64.compatibleWith(*this))
529 {
530 return Arch_ppc64;
531 }
532 if (Arch_s390x.compatibleWith(*this))
533 {
534 return Arch_s390x;
535 }
536 // Here: no multilib; return arch before noarch
537 CompatSet cset( compatSet( *this ) );
538 if ( cset.size() > 2 ) // systemArchitecture, ..., basearch, noarch
539 {
540 return *(++cset.rbegin());
541 }
542 return *this;
543 }
544
546 //
547 // METHOD NAME : Arch::compare
548 // METHOD TYPE : bool
549 //
550 int Arch::compare( const Arch & rhs ) const
551 { return _entry->compare( *rhs._entry ); }
552
554 //
555 // METHOD NAME : Arch::compatSet
556 // METHOD TYPE : Arch::CompatSet
557 //
558 Arch::CompatSet Arch::compatSet( const Arch & targetArch_r )
559 {
560 Arch::CompatSet ret;
561
562 for ( ArchCompatSet::const_iterator it = ArchCompatSet::instance().begin();
563 it != ArchCompatSet::instance().end(); ++it )
564 {
565 if ( it->compatibleWith( *targetArch_r._entry ) )
566 {
567 ret.insert( Arch(*it) );
568 }
569 }
570
571 return ret;
572 }
573
575} // namespace zypp
Set _compatSet
Definition: Arch.cc:431
ZYPP_DEFINE_ID_HASHABLE(zypp::Arch::CompatEntry)
#define SETARG(N)
Architecture.
Definition: Arch.h:37
const Arch Arch_ppc64
Definition: Arch.h:196
const CompatEntry * _entry
Definition: Arch.h:145
Arch baseArch() const
Definition: Arch.cc:513
std::set< Arch, CompareByGT< Arch > > CompatSet
Reversed arch order, best Arch first.
Definition: Arch.h:117
const Arch Arch_sparc64
Definition: Arch.h:219
const Arch Arch_sparc64v
Definition: Arch.h:217
const Arch Arch_s390x
Definition: Arch.h:186
int compare(const Arch &rhs) const
Arch comparison.
Definition: Arch.cc:550
Arch()
Default ctor Arc_noarch.
Definition: Arch.cc:452
static CompatSet compatSet(const Arch &targetArch_r)
Return a set of all Arch's compatibleWith a targetArch_r.
Definition: Arch.cc:558
bool isBuiltIn() const
Whether this is a buitin (or known) architecture.
Definition: Arch.cc:497
IdString idStr() const
String representation of Arch.
Definition: Arch.cc:481
const std::string & asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Arch.cc:489
bool compatibleWith(const Arch &targetArch_r) const
Compatibility relation.
Definition: Arch.cc:505
const Arch Arch_x86_64
Definition: Arch.h:173
Access to the sat-pools string space.
Definition: IdString.h:43
sat::detail::IdType IdType
Definition: IdString.h:45
IdType id() const
Expert backdoor.
Definition: IdString.h:122
static const IdString Empty
Empty string.
Definition: IdString.h:77
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:289
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
DEF_BUILTIN(noarch)
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:567
const Arch Arch_empty(IdString::Empty)
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
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define INT
Definition: Logger.h:100