Shared Haskell .so library with GHC 6.10.4 and Cabal

•03/01/2010 • 2 Comments

I wanted to extend an app written in ANSI C with a functionality written in Haskell. This can be somewhat complicated process. If you use GHC 6.12.1 or newer it can supposedly be easily done with -shared flag. Well, I’m not sure how it works there, but installing GHC 6.12.1 is a painful process. Instead I wanted to do the same with GHC 6.10.4. Turns out it can be done with a simple Makefile like this:

shared:
      ghc -O --make \
           -no-hs-main -optl '-shared' -optc '-DMODULE=HaskellExts' \
           -o HaskellExts.so src/HaskellExts.hs src/module_init.c

module_init.c is also pretty simple and generic:
#define CAT(a,b) XCAT(a,b)
#define XCAT(a,b) a ## b
#define STR(a) XSTR(a)
#define XSTR(a) #a

#include

extern void CAT (__stginit_, MODULE) (void);

static void library_init (void) __attribute__ ((constructor));
static void
library_init (void)
{
      /* This seems to be a no-op, but it makes the GHCRTS envvar work. */
      static char *argv[] = { STR (MODULE) ".so", 0 }, **argv_ = argv;
      static int argc = 1;
     
      hs_init (&argc, &argv_);
      hs_add_root (CAT (__stginit_, MODULE));
}

static void library_exit (void) __attribute__ ((destructor));
static void
library_exit (void)
{
hs_exit ();
}

I took both from http://wiki.python.org/moin/PythonVsHaskell.

Now, this method works in general. However, I wanted to make a .cabal package description, because cabal-install handles some common tasks (like pulling dependencies and cleaning) automatically. This turned out to be little tricky. I had to cheat: the shared library is actually build as executable. Here is the core part of .cabal:

executable HaskellExts.so
      build-depends: base == 4.*, directory == 1.0.* , filepath == 1.1.*, edit-distance == 0.1.2.*
      hs-source-dirs: src
      ghc-options: -optl-shared -optc-DMODULE=HaskellExts -no-hs-main
      main-is: HaskellExts.hs
      c-sources: src/module_init.c
      include-dirs: src
      install-includes: HaskellExts.h
      cc-options: -DMODULE=HaskellExts -shared
      ld-options: -shared

I’m not sure it is the minimal configuration that works. But it works for me so I’m happy with it. The whole code is available here: http://dl.dropbox.com/u/1479187/blog/killer-haskell-exts.7z

Another day, another release.

•05/12/2009 • Leave a Comment

It’s been a rich time in terms of number of packages I’ve released recently.

I’ve had several half-baked packages on my hard drive I’ve never actually made polished. I made a decision it can no longer be this way – I should contribute to the Haskell community I owe so much.

Today brings the release of a small wrapping package: readline-statevar. It brings StateVar API for readline package, which wraps libreadline.

Handful of links:
Announcement post
Git repo
Issue tracker
Hackage page

Kinda sorty

•01/12/2009 • Leave a Comment

I needed a tool to perform relatively simple sorting. An output from ‘du -h’ looks like this:

~/ du -a -h cups-pdf
216K cups-pdf/job_41-_stdin_.pdf
2,6M cups-pdf/job_46-202aw__Polska_do_Droga_bez_nazwy_-_Mapy_Google.pdf
424K cups-pdf/job_45-274e_-_Lifestyle_-_studente.pdf
68K cups-pdf/job_44-Google_Maps_API_-_API_Key_Signup.pdf
40K cups-pdf/job_50-Wersja_do_druku___MICKIEWICZA_-_rondo.pdf
136K cups-pdf/job_58-Studia_magisterskie_-_program_po_zmianach_w_roku_2005.pdf
536K cups-pdf/job_40-_stdin_.pdf
1,6M cups-pdf/job_39-_stdin_.pdf
40K cups-pdf/job_52-263j.pdf
104K cups-pdf/job_43-cracking_wpa__Aircrack-ng_.pdf
34M cups-pdf/job_60-DjVu_PostScript_document.pdf
80K cups-pdf/job_56-Bluetooth_GPRS_Howto_-_ArchWiki.pdf
40K cups-pdf/job_51-Wersja_do_druku___Al_Podwale-Palisadowa.pdf
1,1M cups-pdf/job_42-_stdin_.pdf
41M cups-pdf

I needed it sorted per filesize, which is present in first column. Unfortunately I couldn’t find a way to do this with “sort” utility. So I wrote this tool. It works as it should:

█▓▒░tener@laptener░▒▓██▓▒░ śro gru 02 01:16:12
~/ du -a -h cups-pdf | sorty

40K cups-pdf/job_50-Wersja_do_druku___MICKIEWICZA_-_rondo.pdf
40K cups-pdf/job_52-263j.pdf
40K cups-pdf/job_51-Wersja_do_druku___Al_Podwale-Palisadowa.pdf
68K cups-pdf/job_44-Google_Maps_API_-_API_Key_Signup.pdf
80K cups-pdf/job_56-Bluetooth_GPRS_Howto_-_ArchWiki.pdf
104K cups-pdf/job_43-cracking_wpa__Aircrack-ng_.pdf
136K cups-pdf/job_58-Studia_magisterskie_-_program_po_zmianach_w_roku_2005.pdf
216K cups-pdf/job_41-_stdin_.pdf
424K cups-pdf/job_45-274e_-_Lifestyle_-_studente.pdf
536K cups-pdf/job_40-_stdin_.pdf
1,6M cups-pdf/job_39-_stdin_.pdf
1,1M cups-pdf/job_42-_stdin_.pdf
2,6M cups-pdf/job_46-202aw__Polska_do_Droga_bez_nazwy_-_Mapy_Google.pdf
34M cups-pdf/job_60-DjVu_PostScript_document.pdf
41M cups-pdf

I’ve put it on Hackage here, and the repo is on GitHub here.

Released package-vt-0.1.3.3

•27/11/2009 • Leave a Comment

Just a quick update now. I’ve just released a brand new tool called package-vt. It should be used for detecting changes in packages that should cause a version bump.

You can read more on Haskell Cafe.
Page on Hackage.
Git repo.

Stay tuned for more info!

Rsync over ssh with custom port

•14/11/2009 • 3 Comments

It’s just a quick tip. How can you force rsync to use a specific port when connecting to remote machine via ssh? Turns out it’s pretty easy, if you dig into man page deep enough. Simply specify needed port in ‘-e’ option like this:

rsync -av -e "ssh -p 123" example.com:/path/on/remote/server ~/local/path

As a bonus, with the same option you can add a lot more useful options, like compression and compression ratio:

... -e "ssh -p 123 -C -oCompressionLevel=9" ...

I’m not dead

•10/11/2009 • Leave a Comment

… yet. It’s actually quite the opposite: I’m alive but quite busy. It’s hard for me to find any time to write anything for the blog. I’ve been writing quite a lot recently, most of which you can read in this article.

To be more specific: I wrote an introductory article about basics of Haskell for a polish computer science portal. I put quite some effort in it (~25h), but I hope there are people that will benefit from it greatly.

Haskell jobs

•26/10/2009 • 2 Comments

Recently I’ve been looking for a job involving Haskell or functional programming in general. No luck. I know that there are is page in Haskell wiki about that, but it’s almost empty, if I filter out those which doesn’t match my preferences.

First thing is: most companies are from US. Next came UK finance companies (like Barclays or Credit Suisse), which are way too far for me. There are also a couple of links to now-dead startups that meant to use Haskell as a silver bullet. No luck I suppose. The one interesting company listed there is Starling Software. Too bad they are from Japan. Another notable exception is Erlang Training and Consulting Ltd. Sadly, while they do have an office in Kraków, they don’t seem to willing to expand their staff there.

I also tried several job search engines for a FP job in Poland, without luck.

My research seems to agree with a previous work you can read about here.

It is sad that most companies that use FP seem to be either too far away or long dead.

I would really love to see one of those near Wrocław.