Saturday, March 23, 2013

mkstatic: join binary and it's libraries together

You just built your program on your own notebook. And noticed, you don't have many of libraries on the other host. The host, where you want to check out your job. Nothing unusual. First approach to install 'em (lucky if you got root-access). Another approach is to recompile your program linking all the libraries statically; and it's good if you have static brothers for your dynlibs. So, it doesn't work everywhere. Especially, on production server.

But what if you just want to launch your program on another machine and you don't want to bother yourself with tons of libraries it depends on? To do this job quickly you may use mkstatic Perl script. It creates .static package which contains your binary and all it's libraries within.

No magic, again :-(

Surely, it doesn't perform anything you can't do manually (if you familiar with ld.so). But I think you won't do this job so accurate. So, what mkstatic does for you:

  • it collects all dependencies of your binary_file (and remembers symlinks to libraries)
  • it creates .tgz which is actually placed in Shell-file. This archive includes binary, libraries and bootstrapping code
  • you may use binary.staic as usual binary file
I believe the latest point is the most critical. Because all the mess are hidden from you: all it works just like your original binary. With absolutely the same usage. And requires nothing from target host.

Known limitations

Keen reader may guess: "Hey, this will work for all binaries!". Yes, you can't [easily] copy your Chromium distribution this way to empty machine. Simply because it depends not only from libraries, but from many data (drivers for your Xorg, font configs). And mkstatic doesn't know anything about them. But you can use mkstatic, for instance, with Midnight Commander :-). Or any your executable which uses "data-free" libraries (libogg, libboost, libstdc++, etc).

Let's test it

Surely, it's better to test mkstatic in two machines: one which has all the bunch of libraries, and the fresh one. But I'll show you how I've tested this thing.

First of all, you have to build .static package. Use Midnight Commander' binary as example:
$ ./mkstatic -o /tmp/mc.static `which mc`
executable package is ready: /tmp/mc.static
I'm using xubuntu-12.04 (Precise Pangolin). As any Debian-like distribution it contains debootstrap utility. So, launch:
$ sudo debootstrap precise precise-chroot http://mirror.yandex.ru/ubuntu/
$ sudo cp /tmp/mc.static precise-chroot/tmp/
$ sudo chroot precise-chroot /bin/bash # now you're in test environment
# /tmp/mc.static

Wuala! Midnight Commander is working on your chroot environment, though you don't have libgpm.so within. You may say what Midnight commander is pretty simple. Surely! But you may use mkstatic with much more heavy binaries like mencoder which requires about 100 libraries. Or even Skype! All programs containing one executable binary file is mkstatic-friendly. Try it!

As usually, there is manual-page in package. See mkstatic --man for details.

P.S. If you just interested in approach self-extractable archive, you may see makself. It's widely used for binary installations on Unix world (Nvidia drivers, VirtualBox, etc).

No comments:

Post a Comment