{"id":29,"date":"2014-01-30T23:55:50","date_gmt":"2014-01-30T22:55:50","guid":{"rendered":"http:\/\/paguilar.org\/?p=29"},"modified":"2014-01-31T00:03:24","modified_gmt":"2014-01-30T23:03:24","slug":"building-gnu-libmatheval-for-arm","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=29","title":{"rendered":"Building GNU libmatheval for ARM"},"content":{"rendered":"<p><strong>Overview<\/strong><\/p>\n<p><a href=\"http:\/\/www.gnu.org\/software\/libmatheval\/\" target=\"_blank\">GNU libmatheval<\/a> is a library used for parsing and evaluating symbolic expressions input as text.<\/p>\n<p>A simple and common use of this library is when you have a C program and you have mathematical expressions. For example,<\/p>\n<p><em>x * y &gt; x^2<\/em><\/p>\n<p>contains two variables whose values can vary over time since they could be analog inputs and therefore you need to evaluate it often and execute different actions according to the resulting comparison.<\/p>\n<p>This library can be quite useful in an embedded system that provides some control functionality, but it has the disadvantage that some of its requirements are not straight forward to cross-build and have significant memory and storage footprints.<\/p>\n<p>I managed to build it and run it successfully on an Atmel AT91SAM9260 at 210MHz with 64MB RAM and a rootfs with 128MB NAND flash, so it should be sufficient for many embedded systems, but if you have a slower processor or less memory (it&#8217;s still common to find industrial embedded devices with significantly fewer processor horsepower and\/or memory) you could have performance issues.<br \/>\nFor this article, I&#8217;m using the more powerful Atmel Cortex-A5 ATSAMA5D35 with 512MB RAM.<\/p>\n<p><strong>Requirements<\/strong><\/p>\n<p>I used <a title=\"Buildroot\" href=\"http:\/\/buildroot.org\/\" target=\"_blank\">buildroot<\/a> for building the toolchain and the rootfs for the evaluation board. Unfortunately, buildroot does not include libmatheval nor some of it&#8217;s requirements since they are not typically used in embedded systems.<\/p>\n<p>Building a toolchain and a rootfs with buildroot is a straight-forward task that is well documented. By using it, you can easily build some of libmatheval&#8217;s requirements:<\/p>\n<ul>\n<li>libtool<\/li>\n<li>libgmp<\/li>\n<li>libffi<\/li>\n<li>pthreads<\/li>\n<li>flex<\/li>\n<\/ul>\n<p>The procedure that I&#8217;m describing here is for building libmatheval and the requirements that are <em>not<\/em> included in buildroot.<\/p>\n<p>First, let&#8217;s export common environment variables that make our life easier:<\/p>\n<pre lang=\"bash\">export HOST=arm-linux\r\nexport BUILD=i386-linux\r\nexport ROOTFS=\/home\/projects\/sama5\/rootfs<\/pre>\n<p>Now we can start building the requirements:<\/p>\n<p><span style=\"color: #5077d7;\">libuninstring<\/span><br \/>\nPackage: <a href=\"http:\/\/ftp.gnu.org\/gnu\/libunistring\/libunistring-0.9.3.tar.gz\">libunistring 0.9.3<\/a><\/p>\n<p>This library is used for advanced text processing. The mathematical expressions we use need advanced text analysis.<\/p>\n<pre lang=\"bash\">.\/configure --prefix=$ROOTFS --host=$HOST --build=$BUILD --enable-threads=posix<\/pre>\n<p>Apply the following patch. The commented code should not be a problem since it&#8217;s for glibc &lt; 2.4 and most embedded systems use uclibc or newlib and a newer GCC:<\/p>\n<pre lang=\"bash\">\r\n--- a\/lib\/localename.c  2010-04-03 11:43:57.000000000 +0200\r\n+++ b\/lib\/localename.c  2014-01-04 23:36:13.392018313 +0100\r\n@@ -2613,10 +2613,10 @@\r\n            See <http:\/\/sourceware.org\/bugzilla\/show_bug.cgi?id=10968>.  *\/  \r\n         const char *name =\r\n           nl_langinfo (_NL_ITEM ((category), _NL_ITEM_INDEX (-1)));\r\n-        if (name[0] == '\\0')\r\n+        \/*if (name[0] == '\\0')*\/\r\n           \/* Fallback code for glibc < 2.4, which did not implement\r\n              nl_langinfo (_NL_LOCALE_NAME (category)).  *\/  \r\n-          name = thread_locale->__names[category];\r\n+        \/*name = thread_locale->__names[category];*\/\r\n         return name;\r\n #  endif\r\n #  if defined __APPLE__ && defined __MACH__ \/* MacOS X *\/\r\n\r\n<\/pre>\n<p>Build and install in <em>$ROOTFS<\/em><\/p>\n<pre lang=\"bash\">make\r\nmake  DESTDIR=$ROOTFS install<\/pre>\n<p><span style=\"color: #5077d7;\">bdw-gc<\/span><br \/>\nPackage: <a href=\"http:\/\/www.hpl.hp.com\/personal\/Hans_Boehm\/gc\/gc_source\/gc-7.2e.tar.gz\">gc-7.2e<\/a><\/p>\n<p>This is a C garbage collector for C <em>malloc<\/em> or C++ <em>new<\/em>. As with libunistring, this library is needed for the following requirement (Guile).<\/p>\n<p>Configure, build and install in $ROOTFS:<\/p>\n<pre lang=\"bash\">.\/configure \\\r\n--prefix=$ROOTFS \\\r\n--host=$HOST \\\r\n--build=$BUILD \\\r\n--enable-threads=posix \\\r\n--disable-gcj-support\r\n\r\nmake\r\n\r\nmake  DESTDIR=$ROOTFS install<\/pre>\n<p><span style=\"color: #5077d7;\">guile<\/span><br \/>\nPackage: <a title=\"Guile\" href=\"ftp:\/\/ftp.gnu.org\/gnu\/guile\/guile-1.8.8.tar.gz\">guile 1.8.8<\/a><\/p>\n<p>Guile is a library, interpreter and compiler of Scheme, a symbolic programming language. It&#8217;s an ideal option for evaluating mathematical expressions, but it&#8217;s not the best companion for small embedded systems due to its memory and storage footprints. For example, the size of the shared lib <em>libguile.so.17.4.0<\/em> in Guile 1.8.8 is ~3.1MB and ~790KB when stripped.<\/p>\n<p>When building it, you must take care that the Guile version that you want to install in your embedded device must be equal or less than the version you have installed in your host since the cross-building process uses the native Guile. In my case I had guile 2.0.5 installed in my host, but I used guile 1.8.8 since I had several problems while trying to build 2.0.x versions.<\/p>\n<p>First, apply this patch that removes the use of csqrt() and calculates it <em>manually<\/em>. I think this is a problem with my uclibc.<\/p>\n<pre lang=\"bash\">--- guile-1.8.8\/libguile\/numbers.c\t2010-12-13 18:25:01.000000000 +0100\r\n+++ guile-1.8.8.a\/libguile\/numbers.c\t2014-01-20 00:01:38.106510706 +0100\r\n@@ -6168,8 +6168,8 @@\r\n   if (SCM_COMPLEXP (x))\r\n     {\r\n #if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT && defined (SCM_COMPLEX_VALUE)\r\n-      return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));\r\n-#else\r\n+        \/*return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));*\/\r\n+        \/*#else*\/\r\n       double re = SCM_COMPLEX_REAL (x);\r\n       double im = SCM_COMPLEX_IMAG (x);\r\n       return scm_c_make_polar (sqrt (hypot (re, im)),<\/pre>\n<p>Configure by specifying where to find <em>ffi<\/em> and <em>bdw-gc<\/em>:<\/p>\n<pre lang=\"bash\"> \r\nCPPFLAGS=\"-I$ROOTFS\/include -I$ROOTFS\/usr\/include\" \\\r\nLDFLAGS=\"-L$ROOTFS\/lib -L$ROOTFS\/usr\/lib\" \\\r\nLIBFFI_CFLAGS=\"-I$ROOTFS\/usr\/include\" \\\r\nLIBFFI_LIBS=\"-L$ROOTFS\/usr\/lib\" \\\r\nBDW_GC_CFLAGS=\"-I$ROOTFS\/usr\/include\" \\\r\nBDW_GC_LIBS=\"-L$ROOTFS\/usr\/lib\" \\\r\nGUILE_FOR_BUILD=\/usr\/bin\/guile \\\r\n.\/configure \\\r\n--prefix=$ROOTFS \\\r\n--host=$HOST \\\r\n--build=$BUILD<\/pre>\n<p>Edit the file <em>libguile\/Makefile<\/em>.<br \/>\nRemove the flag <em>-Werror<\/em> to variable <em>CFLAGS<\/em>, otherwise it will complain that several variables are set but not used <em>[-Werror=unused-but-set-variable]<\/em>.<\/p>\n<pre lang=\"bash\"> \r\nCFLAGS = -g -O2 -Wall -Wmissing-prototypes<\/pre>\n<p>Build and install:<\/p>\n<pre lang=\"bash\"> \r\nmake\r\nmake install<\/pre>\n<p><span style=\"color: #5077d7;\">libmatheval<\/span><br \/>\nPackage: <a href=\"http:\/\/ftp.gnu.org\/gnu\/libmatheval\/libmatheval-1.1.11.tar.gz\">libmatheval 1.1.11<\/a><\/p>\n<p>Now we can finally build libmatheval!<\/p>\n<p>Edit the file <em>Makefile.in<\/em> and remove the directory <em>tests<\/em> from the building process:<\/p>\n<pre lang=\"bash\">SUBDIRS = doc lib<\/pre>\n<p>Configure, build and install:<\/p>\n<pre lang=\"bash\">CPPFLAGS=\"-I$ROOTFS\/include -I$ROOTFS\/usr\/include\" \\\r\nLDFLAGS=\"-L$ROOTFS\/lib -L$ROOTFS\/usr\/lib\" \\\r\n.\/configure \\\r\n--prefix=$ROOTFS \\\r\n--host=$HOST \\\r\n--build=$BUILD\r\n\r\nmake\r\n\r\nmake install<\/pre>\n<p>For compiling a program tha uses libmatheval you just need to add the linking flag <em>-lmatheval<\/em> and, of course, include the directories where it was installed, typically <em>\/your\/rootfs\/usr\/include<\/em> and <em>\/your\/rootfs\/usr\/lib<\/em>.<\/p>\n<p>References:<\/p>\n<ul>\n<li><a title=\"libmatheval\" href=\"http:\/\/www.gnu.org\/software\/libmatheval\/\" target=\"_blank\">libmatheval<\/a><\/li>\n<li><a title=\"Guile\" href=\"https:\/\/www.gnu.org\/software\/guile\/\" target=\"_blank\">Guile<\/a><\/li>\n<li><a title=\"C\/C++ garbage collector\" href=\"http:\/\/www.hpl.hp.com\/personal\/Hans_Boehm\/gc\/\" target=\"_blank\">bdw-gc<\/a><\/li>\n<li><a title=\"libunistring\" href=\"http:\/\/www.gnu.org\/software\/libunistring\/\" target=\"_blank\">libunistring<\/a><\/li>\n<li><a title=\"Buildroot doc\" href=\"http:\/\/buildroot.org\/docs.html\" target=\"_blank\">Buildroot doc<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview GNU libmatheval is a library used for parsing and evaluating symbolic expressions input as text. A simple and common use of this library is when you have a C program and you have mathematical expressions. For example, x * y &gt; x^2 contains two variables whose values can vary over time since they could\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=29\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-compiling"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":37,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}