andLinuxに入れたRuby 1.9.1からCaboChaを呼んでみた

Ruby 1.9.1はすんなり入る気がする。ソースから。今回はruby(1.8)とruby19(1.9.1)に分けた。

$ ./configure --program-suffix=19
$ make 
$ make test
$ sudo make install
$ ruby -v
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
$ ruby19 -v
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]

CaboChaについてはSWIGやらGCCやらが新しくなってたりするあたりでいろいろコケたので、備忘録。
まず、SWIGは1.3.29から%typemapに言語の名前を入れてはいけないと言われるので、#ifdef(#if defined())で書きなおす。

cd cabocha-0.53/swig

で、diffとった。

--- arg.i.orig
+++ arg.i
@@ -1,11 +1,12 @@
 %include typemaps.i
 
 /* java */ 
-%typemap(java,jni)    (int argc, char *argv[]) "jobjectArray" 
-%typemap(java,jtype)  (int argc, char *argv[]) "String[]"
-%typemap(java,jstype) (int argc, char *argv[]) "String[]"
+#if defined(SWIGJAVA)
+%typemap(jni)    (int argc, char *argv[]) "jobjectArray" 
+%typemap(jtype)  (int argc, char *argv[]) "String[]"
+%typemap(jstype) (int argc, char *argv[]) "String[]"
 
-%typemap(java, in) (int argc, char *argv[]) (jstring *jsarray) {
+%typemap(in) (int argc, char *argv[]) (jstring *jsarray) {
   int i;
 
   $1 = JCALL1 (GetArrayLength, jenv, $input);
@@ -24,7 +25,7 @@
 
 %typemap(argout) (int argc, char *argv[]) "" /* override char *[] default */
 
-%typemap(java, freearg) (int argc, char *argv[]) {
+%typemap(freearg) (int argc, char *argv[]) {
   int i;
   for (i = 0; i < $1; i++) {
     JCALL2 (ReleaseStringUTFChars, jenv, jsarray$argnum[i], $2[i]);
@@ -33,7 +34,8 @@
 }
 
 /* perl */
-%typemap(perl5,in) (int argc, char *argv[]) {
+#elif defined(SWIGPERL)
+%typemap(in) (int argc, char *argv[]) {
   AV *tempav;
   SV **tv;
   I32 len;
@@ -56,12 +58,13 @@
   $2[i] = 0;
 }
 
-%typemap(perl5,freearg) (int argc, char *argv[]) {
+%typemap(freearg) (int argc, char *argv[]) {
   delete [] $2;
 }
 
-/* python */ 
-%typemap(python,in) (int argc, char *argv[]) {
+/* python */
+#elif defined(SWIGPYTHON) 
+%typemap(in) (int argc, char *argv[]) {
   int i;
   if (!PyList_Check($input)) {
     SWIG_exception(SWIG_ValueError, "Expecting a list");
@@ -84,12 +87,13 @@
   $2[i] = 0;
 }
 
-%typemap(python,freearg) (int argc, char *argv[]) {
+%typemap(freearg) (int argc, char *argv[]) {
   delete [] $2;
 }
 
 /* ruby */
-%typemap(ruby,in) (int argc, char *argv[]) {
+#elif defined(SWIGRUBY)
+%typemap(in) (int argc, char *argv[]) {
   int i;
   
   if (TYPE($input) != T_ARRAY) {
@@ -111,6 +115,7 @@
   $2[i] = 0;
 }
 
-%typemap(ruby,freearg) (int argc, char *argv[]) {
+%typemap(freearg) (int argc, char *argv[]) {
   delete [] $2;
 }
+#endif

これでSWIGは通る。

$ cd cabocha-0.53/swig
$ make ruby

さらにこのあと、コンパイルでコケる。たぶん、GCCのバージョンが新しいせいでエラーが出てる。ちょこちょこ書き直し。ostringstreamのところは、正しいのかどうかよくわからない。

cd /usr/local/include
--- cabocha.h.orig
+++ cabocha.h
@@ -107,7 +107,7 @@
 #include <string>
 #include <vector>
 #include <iostream>
-#include <strstream>
+#include <sstream>
 
 namespace CaboCha
 {
@@ -129,7 +129,7 @@
   private:
     FreeList<char>  *freelist1;
     FreeList<Chunk> *freelist2;
-    std::ostrstream *ostrs;
+    std::ostringstream *ostrs;
 
     std::ostream& writeLattice (std::ostream &, int);
     std::ostream& writeTree    (std::ostream &, int);

Ruby 1.9.1ではRArrayの仕様が変更されてるっぽい。includeのほうは、言われるままに書き換え。

$ cd cabocha-0.53/ruby
--- CaboCha_wrap.cpp.orig
+++ CaboCha_wrap.cpp
@@ -1576,7 +1576,7 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
-#include "rubyio.h"
+#include "ruby/io.h"
 #ifdef __cplusplus
 }
 #endif
@@ -2797,7 +2797,7 @@
     if (TYPE(argv[0]) != T_ARRAY) {
       SWIG_exception(SWIG_ValueError, "Expected an array");
     }
-    arg1 = RARRAY(argv[0])->len;
+    arg1 = RARRAY_LEN(RARRAY(argv[0]));
     if (arg1 == 0) {
       SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
     }

これで一応コンパイルが通り、Ruby 1.9.1から呼べるようになった。
あと、一番ハマってたのは、make installするのを忘れてたこと