From khansen at stat.berkeley.edu  Sun May 24 00:45:19 2009
From: khansen at stat.berkeley.edu (Kasper Daniel Hansen)
Date: Sat, 23 May 2009 15:45:19 -0700
Subject: [Rd] using a "third party" DLL in my package
In-Reply-To: <4A13EA32.5010309@metla.fi>
References: <4A0C079F.7080101@metla.fi>
	<alpine.LFD.2.00.0905150905560.4378@gannet.stats.ox.ac.uk>
	<4A13EA32.5010309@metla.fi>
Message-ID: <0A2C49D8-9EDA-432B-B3CD-04A395EBBAC4@stat.berkeley.edu>


On May 20, 2009, at 4:32 , Seija Sirki? wrote:

> Hello again,
>
> thank you for the comments, especially this one:
>
> Prof Brian Ripley wrote:
>
> > My concern would be that there are different cases that fail under
> > Fortran compiler X and you are just sweeping the problem under the
> > carpet.
>
> It inspired us to go back to search the cause, and we've made some  
> progress: it's not the compiler, it's the compiler options. Simple,  
> but it took a while to figure that out since my experience in these  
> things is limited. When I build the package with default options  
> using INSTALL --build the dll is built with option -O3 as per R's  
> Makeconfig file. If I build the dll by hand, using gfortran with no  
> additional options and dyn.load it, everything works, and also with - 
> O and -Os. (No, I don't fully understand what the differences  
> between all these are, but that's another question).
>
> I'm looking at chapter 5.5 in Writing R Extensions and also 6.3.3 in  
> R Installation and Administration but I can't figure out how to tell  
> "inside" my package that it is not to be built -O3 but with, say, - 
> O. I can see how to add flags in the package (and as far as I can  
> tell, if there are several optimization level flags the last in line  
> is used and that's the wrong one from my point of view), and also  
> how to override flags but only on my computer. Am I blind or am I  
> again attempting something I shouldn't?


This is not trivial, and how you do it is compiler dependent. A quick  
fix was provided by Simon Urbanek a while back and it is _not_  
portable, it assumes you are using GCC. It would be nice to have a  
configure file that detects the compiler and optimization setting and  
then re-sets the optimization level. I have thought about writing one,  
but have never got around to do it.

Anyway, the fix is in the Makevars file from affxparser from  
Bioconductor. Essentially, you use a Makevars file placed in the src  
directory, containing

MYCXXFLAGS=-O0 -Wall

%.o: %.cpp
         $(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) $(MYCXXFLAGS) -c $< -o  
$@

Essentially this makes sure that -O0 (indicating no optimization) is  
placed at the _end_ of the call to the compiler (this is for C++ files  
btw), using the fact that if you have two -O* settings, the last one  
overrides the first.

This ought to be easily adaptable to FORTRAN.

Kasper



[Rd] using a "third party" DLL in my package
Kasper Daniel Hansen