From ripley at stats.ox.ac.uk  Mon Aug  6 21:37:14 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 6 Aug 2007 20:37:14 +0100 (BST)
Subject: [Rd] Makefile for embedding OpenBUGS in R package
In-Reply-To: <W906017980119021186410641@nocme1bl6.telenet-ops.be>
References: <W906017980119021186410641@nocme1bl6.telenet-ops.be>
Message-ID: <Pine.LNX.4.64.0708062029410.27542@gannet.stats.ox.ac.uk>

On Mon, 6 Aug 2007, Tobias Verbeke wrote:

>> ----- Oorspronkelijk bericht -----
>> Van: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
>> Verzonden: maandag, augustus 6, 2007 03:46 PM
>> Aan: 'Tobias Verbeke'
>> CC: r-devel at r-project.org
>> Onderwerp: Re: [Rd] Makefile for embedding OpenBUGS in R package
>>
>> On Mon, 6 Aug 2007, Tobias Verbeke wrote:
>>
>>>   Dear list,
>>>
>>>   I'm trying to embed OpenBUGS in an R package for use of it
>>>   on 64-bit Linux. In order to get the CLI working one has to
>>>   compile C code contained in $OpenBUGS/Manuals/CBugs.html
>>
>> same as
>>
>> http://mathstat.helsinki.fi/openbugs/data/Docu/CBugs.html
>>
>> I presume.
>
> Actually, these files appear to differ. The file I referred to
> was the file ./Manuals/CBugs.html contained in the current OpenBUGS
> release:
>
> http://mathstat.helsinki.fi/openbugs/OpenBUGS.zip

I am baffled by that C file: why not just link an even simpler stub 
against brugs.so rather than play around with dlopen?


>>>   (copied to say CBugs.c) using
>>>
>>>   gcc -m32 -o bugs CBugs.c -ldl
>>>
>>>   I put the OpenBUGS distribution in the ./inst subdirectory of
>>>   the package root. Where should I now put the CBugs.c and how
>>
>> Why do you want to install CBugs.c?
>
> The CBugs.c file itself is indeed not needed in the built package.
>
>>
>>>   should the Makefile look like in order to be able to call
>>>   $PKG_ROOT/OpenBUGS/bugs afterwards ?
>>>
>>>   Naively putting the following Makefile in ./src does not work
>>
>> What does 'does not work' mean?  It's hard to know whether this is just
>> not doing what you wanted, or something else is wrong.
>
> Apologies. This is the error message when putting the Makefile
> in ./src and launching the package checker:
>
> $ R.250 CMD check CGHmix
> DISPLAY=localhost:10.0
> * checking for working latex ... OK
> * using log directory '/home/tverbek1/pkg/CGHmix.Rcheck'
> * using R version 2.5.0 (2007-04-23)
> * checking for file 'CGHmix/DESCRIPTION' ... OK
> * checking extension type ... Package
> * this is package 'CGHmix' version '0.1-2'
> * checking package dependencies ... OK
> * checking if this is a source package ... OK
> * checking whether package 'CGHmix' can be installed ... ERROR
> Installation failed.
> See '/home/tverbek1/pkg/CGHmix.Rcheck/00install.out' for details.
>
> The file 00install.out contains:
>
> * Installing *source* package 'CGHmix' ...
> ** libs
> ** arch -
> gcc -m32 -o ../inst/OpenBUGS/bugs ../inst/OpenBUGS/CBugs.c -ldl
> cp: cannot stat `*.so': No such file or directory
> ERROR: compilation failed for package 'CGHmix'

Ah, so you need to circumvent the installation mechanism as you do not 
have a DLL in your package.  See package Rserve for a workaround.

[rest not needed for the reply.]

> ** Removing '/home/tverbek1/pkg/CGHmix.Rcheck/CGHmix'
>
>>
>>>   -%--------
>>>   bugs: ../inst/OpenBUGS/CBugs.c
>>>        gcc -m32 -o bugs ../inst/OpenBUGS/CBugs.c -ldl
>>>   -%-------
>>>
>>>   The objective is to use something along the following
>>>
>>>   execfile <- system.file("OpenBUGS", "bugs", package = mypkg)
>>>   system(paste(execfile, "< somescript.script > somefile.out"))
>>>
>>>   This system call to the CLI is currently the only (non-WINE)
>>>   way of using OpenBUGS on Linux in batch mode.
>>
>> I think you need to make ../inst/OpenBUGS/bugs, not src/bugs.  So
>> something like
>>
>> all: ../inst/OpenBUGS/bugs
>>
>> ../inst/OpenBUGS/bugs: ../inst/OpenBUGS/CBugs.c
>> 	gcc -m32 -o ../inst/OpenBUGS/bugs ../inst/OpenBUGS/CBugs.c -ldl
>
> Thank you.
>
>> However, -m32 builds a 32-bit executable on 64-bit linux.  Is that what you
>> wanted?
>
> Yes. The shared object (brugs.so) comes with the OpenBUGS distribution
> and currently can only be cross-compiled by the main OpenBUGS developer
> (Andrew Thomas) who knows all secrets of the Windows only BlackBox
> (nomen omen) Component Pascal compiler.
>
> $ file brugs.so
> brugs.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), stripped
>
> Many thanks for your ever instructive answer.
>
> Tobias
>
> P.S. Contents of CBugs.c from OpenBUGS distribution (3.0.1)
>
> /* GNU General Public Licence
>
>   This small C program loads the brugs.so ELF shared library and calls the CLI function.
>   Save it as a .c file and then compile it on Linux using gcc -o bugs CBugs.c -ldl
>
> */
>
> #include <dlfcn.h>
> #include <stdio.h>
> #include <string.h>
>
> int main (int argc, char **argv)
> {
>  void * handle;
>  void (*cli)(void);
>
>  handle = dlopen("./brugs.so", RTLD_LAZY);
>  if (!handle)
>    return 1;
>
>  * (void **) (&cli) = dlsym(handle, "CLI");
>  (*cli)();
>  dlclose(handle);
>
>  return 0;
>
> }

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Makefile for embedding OpenBUGS in R package
Prof Brian Ripley