<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-system-multiarch-wrapper" role="wrap">
  <?dbhtml filename="multiarch_wrapper.html"?>

  <title>Creating a Multiarch Wrapper</title>

  <indexterm zone="ch-system-multiarch-wrapper">
    <primary sortas="a-Multiarch Wrapper">Multiarch Wrapper</primary>
  </indexterm>

  <sect2 role="package">
    <title/>

    <para>The Multiarch Wrapper is used to wrap certain binaries that have
    hardcoded paths to libraries or are architecture specific.</para>

  </sect2>

  <sect2 role="installation">
    <title>Installation of The Multiarch Wrapper</title>

    <para>Create the source file:</para>

<screen><userinput remap="pre">cat &gt; multiarch_wrapper.c &lt;&lt; "EOF"
#define _GNU_SOURCE

#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;unistd.h&gt;

#ifndef DEF_SUFFIX
#  define DEF_SUFFIX "64"
#endif

int main(int argc, char **argv)
{
  char *filename;
  char *suffix;

  if(!(suffix = getenv("USE_ARCH")))
    if(!(suffix = getenv("BUILDENV")))
      suffix = DEF_SUFFIX;

  if (asprintf(&amp;filename, "%s-%s", argv[0], suffix) &lt; 0) {
    perror(argv[0]);
    return -1;
  }

  int status = EXIT_FAILURE;
  pid_t pid = fork();

  if (pid == 0) {
    execvp(filename, argv);
    perror(filename);
  } else if (pid &lt; 0) {
    perror(argv[0]);
  } else {
    if (waitpid(pid, &amp;status, 0) != pid) {
      status = EXIT_FAILURE;
      perror(argv[0]);
    } else {
      status = WEXITSTATUS(status);
    }
  }

  free(filename);

  return status;
}

EOF</userinput></screen>

    <para>Compile and Install the Multiarch Wrapper:</para>

<screen><userinput remap="install">gcc multiarch_wrapper.c -o /usr/bin/multiarch_wrapper</userinput></screen>

    <para>This multiarch wrapper is going to be used later on in the book
    with Perl. It will also be very useful outside of the base LFS-multilib system.</para>

    <para>Create a testcase:</para>

<screen><userinput remap="test">echo 'echo "32bit Version"' &gt; test-32
echo 'echo "x32 ABI Version"' &gt; test-x32
echo 'echo "64bit Version"' &gt; test-64
chmod -v 755 test-32 test-x32 test-64
ln -sv /usr/bin/multiarch_wrapper test</userinput></screen>

    <para os="h">Test the wrapper:</para>

<screen><userinput remap="test">USE_ARCH=32 ./test
USE_ARCH=x32 ./test
USE_ARCH=64 ./test</userinput></screen>

    <para>The output of the above command should be:</para>

<screen><computeroutput>32bit Version
x32 ABI Version
64bit Version</computeroutput></screen>

  <para>Remove the testcase source, binaries, and link:</para>

<screen><userinput>rm -v multiarch_wrapper.c test{,-32,-x32,-64}</userinput></screen>

  </sect2>

  <sect2 id="contents-multiarch-wrapper" role="content">
    <title>Contents of The Multiarch Wrapper</title>

    <segmentedlist>
      <segtitle>Installed programs</segtitle>

      <seglistitem>
        <seg>multiarch_wrapper</seg>
      </seglistitem>
    </segmentedlist>

    <variablelist>
      <bridgehead renderas="sect3">Short Descriptions</bridgehead>
      <?dbfo list-presentation="list"?>
      <?dbhtml list-presentation="table"?>

      <varlistentry id="multiarch_wrapper">
        <term><command>c++</command></term>
        <listitem>
          <para>Will execute a different program based on the
          <envar>USE_ARCH</envar> variable. The <envar>USE_ARCH</envar>
          variable will be the suffix of the executed program.</para>
          <indexterm zone="ch-system-multiarch-wrapper multiarch_wrapper">
            <primary sortas="b-multiarch_wrapper">c++</primary>
          </indexterm>
        </listitem>
      </varlistentry>

    </variablelist>

  </sect2>

</sect1>
