title: How to distinguish between mips cpu types on linux when dpkg-architecture is absent?
body: <p>Short question: how can I reliably distinguish between <i>mips</i>, <i>mipsel</i>, <i>mips64</i> and <i>mips64el</i> on any linux distribution?</p>
  
  <p>Longer explanation:</p>
  
  <p>We provide statically built/distribution independent binaries (for TeX) for many architectures. Installation script usually runs <code>uname -s</code> and <code>uname -m</code> to determine operating system and architecture. Binaries are then fetched from server based on that decision, so it needs to works reliably. And it does. Almost everywhere except for Mac OS X 10.6 and Debian. Mac would report <i>i386</i> on OS that runs 64-bit applications, while Debian reports <i>mips64</i> for 32-bit OS.</p>
  
  <p>Debian on <i>mips64</i> correctly reports processor type, but that doesn't help me for at least two reasons:</p>
  
  <ol>
  <li>OS is 32-bit, not 64-bit as the name might suggest.</li>
  <li>It is running in little-endian mode. Debian calls that <i>mipsel</i>, not <i>mips</i>. It can often be switched, but OS only runs in one mode and mips software is often incompatible with mipsel.</li>
  </ol>
  
  <p>Here are some outputs from system commands:</p>
  
  <pre><code>$ file my_binary_name
  my_binary_name: ELF 32-bit LSB executable, MIPS, MIPS-I version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x1040000, stripped
  
  $ dpkg-architecture 
  DEB_BUILD_ARCH=mipsel
  DEB_BUILD_ARCH_OS=linux
  DEB_BUILD_ARCH_CPU=mipsel
  DEB_BUILD_ARCH_BITS=32
  DEB_BUILD_ARCH_ENDIAN=little
  DEB_BUILD_GNU_CPU=mipsel
  DEB_BUILD_GNU_SYSTEM=linux-gnu
  DEB_BUILD_GNU_TYPE=mipsel-linux-gnu
  DEB_HOST_ARCH=mipsel
  ...
  </code></pre>
  
  <p>dpkg-architecture would be perfect for the task, except that it is not present on other Linux distributions.</p>
  
  <p>The first problem was already addressed here: <a href="https://stackoverflow.com/questions/246007/how-to-determine-whether-a-given-linux-is-32-bit-or-64-bit">How to determine whether a given Linux is 32 bit or 64 bit?</a></p>
  
  <p>The command</p>
  
  <pre><code>getconf LONG_BIT
  </code></pre>
  
  <p>properly reports 32 on my system.</p>
  
  <p>But how do I determine whether it is big endian or little endian?</p>
  
  <p>I figured out that config.guess can determine the difference, but it does so by running the compiler which may not be present on end user's computer. On top of that config.guess completely ignores the fact that operating system works in 32-bit mode and wrongly reports <i>mips64el</i> instead of <i>mipsel</i>.</p>