add "src" arch
[platform/upstream/libzypp.git] / zypp / Arch.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Arch.cc
10  *
11 */
12 #include <iostream>
13 #include <set>
14 #include <sys/utsname.h>
15
16 #include "zypp/Arch.h"
17
18 using namespace std;
19
20 ///////////////////////////////////////////////////////////////////
21 namespace
22 { /////////////////////////////////////////////////////////////////
23
24   /** Dumb Arch compat table
25    * \todo improve
26    */
27   struct CompatTable
28   {
29       static set<string> _compatTable;
30
31       /** \return Whether \a lhs is compatible with \a rhs. */
32       static bool compatible( const zypp::Arch & lhs, const zypp::Arch & rhs )
33           {
34               if ( lhs == zypp::Arch_noarch )
35                   return true;
36
37               if ( _compatTable.empty() )
38               {
39                   // initialize
40 #define DEF_COMPAT(L,R) _compatTable.insert( #L "|" #R )
41                   DEF_COMPAT( noarch,   i386 );
42                   DEF_COMPAT( noarch,   i486 );
43                   DEF_COMPAT( i386,     i486 );
44                   DEF_COMPAT( noarch,   i586 );
45                   DEF_COMPAT( i386,     i586 );
46                   DEF_COMPAT( i486,     i586 );
47                   DEF_COMPAT( noarch,   i686 );
48                   DEF_COMPAT( i386,     i686 );
49                   DEF_COMPAT( i486,     i686 );
50                   DEF_COMPAT( i586,     i686 );
51                   DEF_COMPAT( noarch,   athlon );
52                   DEF_COMPAT( i386,     athlon );
53                   DEF_COMPAT( i486,     athlon );
54                   DEF_COMPAT( i586,     athlon );
55                   DEF_COMPAT( i686,     athlon );
56                   DEF_COMPAT( noarch,   x86_64 );
57                   DEF_COMPAT( i386,     x86_64 );
58                   DEF_COMPAT( i486,     x86_64 );
59                   DEF_COMPAT( i586,     x86_64 );
60                   DEF_COMPAT( i686,     x86_64 );
61                   DEF_COMPAT( athlon,   x86_64 );
62
63                   DEF_COMPAT( noarch,   s390 );
64                   DEF_COMPAT( noarch,   s390x );
65                   DEF_COMPAT( s390,     s390x );
66
67                   DEF_COMPAT( noarch,   ppc );
68                   DEF_COMPAT( noarch,   ppc64 );
69                   DEF_COMPAT( ppc,      ppc64 );
70
71                   DEF_COMPAT( noarch,   ia64 );
72 #undef DEF_COMPAT
73               }
74
75               return _compatTable.find( lhs.asString()+"|"+rhs.asString() )
76                   != _compatTable.end();
77           }
78   };
79
80     set<string> CompatTable::_compatTable;
81
82     /////////////////////////////////////////////////////////////////
83 } // namespace
84 ///////////////////////////////////////////////////////////////////
85
86 ///////////////////////////////////////////////////////////////////
87 namespace zypp
88 { /////////////////////////////////////////////////////////////////
89
90   ///////////////////////////////////////////////////////////////////
91 #define DEF_BUILTIN(A) const Arch Arch_##A( #A )
92
93   DEF_BUILTIN( noarch );
94   DEF_BUILTIN( src );
95
96   DEF_BUILTIN( x86_64 );
97   DEF_BUILTIN( athlon );
98   DEF_BUILTIN( i686 );
99   DEF_BUILTIN( i586 );
100   DEF_BUILTIN( i486 );
101   DEF_BUILTIN( i386 );
102
103   DEF_BUILTIN( s390x );
104   DEF_BUILTIN( s390 );
105
106   DEF_BUILTIN( ppc64 );
107   DEF_BUILTIN( ppc );
108
109   DEF_BUILTIN( ia64 );
110
111 #undef DEF_BUILTIN
112
113  static const string system_arch (void);
114  static const string canonical_arch (const string & arch);
115
116     
117   const Arch Arch::System = Arch(system_arch ());
118       
119  //---------------------------------------------------------------------------
120  // architecture stuff
121       
122  static const string
123  canonical_arch (const string & arch)
124  {
125      typedef struct { char *from; char *to; } canonical;
126      // convert machine string to known_arch
127      static canonical canonical_archs[] = {
128          { "noarch",  "noarch" },
129          { "unknown", "unknown" },
130          { "any",        "any" },
131          { "all",     "any" },
132          { "i386",    "i386" },
133          { "ix86",    "i386" }, /* OpenPKG uses this */
134          { "i486",    "i486" },
135          { "i586",    "i586" },
136          { "i686",    "i686" },
137          { "x86_64",  "x86_64" },
138          { "ia32e",   "ia32e" },
139          { "athlon",  "athlon" },
140          { "ppc",     "ppc" },
141          { "ppc64",   "ppc64" },
142          { "s390",    "s390" },
143          { "s390x",   "s390x" },
144          { "ia64",    "ia64" },
145          { "sparc",   "sparc" },
146          { "sun4c",   "sparc" },
147          { "sun4d",   "sparc" },
148          { "sun4m",   "sparc" },
149          { "sparc64", "sparc64" },
150          { "sun4u",   "sparc64" },
151          { "sparcv9", "sparc64" },
152          { 0 }
153      };
154       
155      for (canonical *ptr = canonical_archs; ptr->from; ptr++) {
156          if (arch == ptr->from) {
157              return ptr->to;
158          }
159      }
160       
161      return "canonical";
162  }
163       
164       
165  static const string
166  system_arch (void)
167  {
168      static struct utsname buf;
169      static bool checked = false;
170       
171      if (!checked) {
172          if (uname (&buf) < 0) {
173              return NULL;
174          }
175          checked = true;
176      }
177       
178      return string (buf.machine);
179  }
180       
181       
182  //---------------------------------------------------------------------------
183
184
185     
186   ///////////////////////////////////////////////////////////////////
187
188   ///////////////////////////////////////////////////////////////////
189   //
190   //    METHOD NAME : Arch::Arch
191   //    METHOD TYPE : Ctor
192   //
193   Arch::Arch( const std::string & rhs )
194   : _value( rhs )
195   {}
196
197   ///////////////////////////////////////////////////////////////////
198   //
199   //    METHOD NAME : Arch::compatibleWith
200   //    METHOD TYPE : bool
201   //
202   bool Arch::compatibleWith( const Arch & rhs ) const
203   {
204     return CompatTable::compatible( *this, rhs );
205   }
206
207   /////////////////////////////////////////////////////////////////
208 } // namespace zypp
209 ///////////////////////////////////////////////////////////////////