Embedded Ruby Sings Sinatra

Last Time I got ruby up and running on my Arm based embedded development platform. Here is a quick summary of what Santa and I did over Christmas.

Sockets

Getting support for sockets built into Ruby turned out to not be hard at all. All I had to do has uncomment the extensions I wanted in ext/Setup. Here is everything I decided to turn on.

1 etc
2 fcntl
3 iconv
4 socket
5 stringio
6 strscan
7 syck
8 thread
9 zlib

After a rebuild and reinstall, I could require and use socket. Welcome, Ruby, to the outside world!

Relocating Ruby

I decided to move Ruby to the ‘standard’ install directory (/usr/local/) instead of the root that I had done in the first iteration. This would avoid having to set the RUBYLIB environment variable and keep any mess I made out out of the main system /bin and /lib directories.

First I removed the --prefix from the configure script I had written last time. This will cause Ruby to build assuming the default install location (/usr/local/).

1 #! /bin/sh
2 export ARM_TOOLS=/usr/local/arm/gcc-4.2.3-glibc-2.3.3/arm-unknown-linux-gnu/bin
3 export CC=$ARM_TOOLS/arm-unknown-linux-gnu-gcc
4 export LD=$ARM_TOOLS/arm-unknown-linux-gnu-gcc
5 export AR=$ARM_TOOLS/arm-unknown-linux-gnu-ar
6 export RANLIB=$ARM_TOOLS/arm-unknown-linux-gnu-ranlib
7 export ac_cv_func_getpgrp_void=yes
8 export ac_cv_func_setpgrp_void=yes
9 ./configure --host=arm-unknown-linux --enable-wide-getaddrinfo

After Ruby was built, but before installing it (make install), I hacked the DESTDIR in rbconfig.rb

1 ...
2 TOPDIR = File.dirname(__FILE__).chomp!("/lib/ruby/1.8/arm-linux")
3 DESTDIR = "/~/ruby/install"
4 CONFIG = {}
5 ...

This created the /usr/local tree inside my install directory. I created a /usr/local/ on my file system and copied that tree over. The only problem with this process is that the sh-bang line on the top of all of the Ruby scripts (irb, testrb, etc.) had the wrong path in them.

They had the install path on the host machine (/~/ruby/install/usr/local/bin/ruby) instead of the actual path on the target (/usr/local/bin/ruby). I changed those by hand.

Sinatra

Ahh, now the good part. Using a gem on an embedded system is an interesting problem. I first had to get rubygems installed on the network files system. I downloaded the source code and temporarily placed in on the target files system. On the target I ran the install command.

1 ruby setup.rb --no-rdoc --no-ri

I found it more convenient to install the gems from the host system instead of the target. It would be faster and the gem command had some Ruby dependencies that I hadn’t yet built.

I downloaded Sinatra and Rack and then manually installed them into my target’s file system on the host machine. Note that /arm/fs/ is the root of my networked file system. I switched off rdoc and ri to help keep the install small.

1 sudo gem install sinatra-0.3.2.gem  -i /arm/fs/usr/local/lib/ruby/gems/1.8 --no-rdoc --no-ri
2 sudo gem install rack-0.4.0.gem  -i /arm/fs/usr/local/lib/ruby/gems/1.8 --no-rdoc --no-ri

With Sinatra installed and ready to go, I wrote a quick Sinatra app. Sinatra runs on Mongrel by default, but I wanted to run on the lighter weight Webrick web server which was already in my ruby installation.

 1 require 'rubygems'
 2 require 'rack/handler/webrick'
 3 require 'sinatra'
 4 Sinatra::Application.default_options.merge!(
 5    :run => false,
 6    :env => :production,
 7    :port => 80
 8  )
 9 get '/' do
10         "Hello Sinatra!"
11 end
12 Rack::Handler::WEBrick.run Sinatra.application

Drum roll please…

Next Steps

I've been plugging away on the Linux device and ruby extension side of the project. I’m still on the steep side of the learning curve, but making progress. I’m still after that elusive blinking LED!

Doug Bradbury, Director of Software Services

Doug Bradbury is a maker, a thinker, a craftsman, and a learner.