| 25857 |
murdoch |
1 |
Writing packages for R
|
|
|
2 |
======================
|
|
|
3 |
|
|
|
4 |
See the Writing R Extensions manual for a full description of how
|
|
|
5 |
to write a package.
|
|
|
6 |
|
|
|
7 |
|
| 9469 |
ripley |
8 |
Building from a source-code library under Windows
|
|
|
9 |
=================================================
|
|
|
10 |
|
| 10545 |
ripley |
11 |
[If you are reading this file as part of the full R source distribution,
|
|
|
12 |
you need to build R first: see file INSTALL.]
|
|
|
13 |
|
| 21130 |
ripley |
14 |
|
|
|
15 |
*** This file contains a lot of prescriptive comments. They are
|
|
|
16 |
here as a result of bitter experience. Please do not report problems
|
|
|
17 |
to R-help unless you have followed all the prescriptions. ***
|
|
|
18 |
|
|
|
19 |
|
| 9940 |
pd |
20 |
If your package has no C nor Fortran nor C++ sources, see `Simple ports'
|
| 9469 |
ripley |
21 |
near the bottom of this file.
|
|
|
22 |
|
| 10228 |
ripley |
23 |
First collect the tools that you need. There is a `portal' with
|
|
|
24 |
current links at http://www.stats.ox.ac.uk/pub/Rtools/.
|
| 9469 |
ripley |
25 |
|
| 10374 |
ripley |
26 |
|
|
|
27 |
You will need suitable versions of at least make, sh, cat, cp, diff,
|
| 15414 |
ripley |
28 |
echo, mkdir, mv, rm, sed; we have packaged a set at
|
| 9469 |
ripley |
29 |
http://www.stats.ox.ac.uk/pub/Rtools/tools.zip extracted from the
|
| 13282 |
ripley |
30 |
cygwin distribution (http://www.cygwin.com/mirrors.html) or (gzip)
|
| 19840 |
ripley |
31 |
compiled from the sources.
|
| 9469 |
ripley |
32 |
|
| 18004 |
ripley |
33 |
|
| 10374 |
ripley |
34 |
BEWARE: `Native' ports of make are _not_ suitable (including that at
|
| 14462 |
ripley |
35 |
the mingw site). There were also problems with several earlier
|
| 13158 |
ripley |
36 |
versions of the cygwin tools and dll. To avoid frustration, please
|
| 17439 |
ripley |
37 |
use our tool set, and make sure it is at the front of your path.
|
| 9469 |
ripley |
38 |
|
| 25863 |
ripley |
39 |
You need a recent version of the Mingw port of gcc from
|
| 26170 |
ripley |
40 |
http://sourceforge.net/projects/mingw/. There will be a distribution
|
|
|
41 |
called something like MinGW-3.0.0-1.exe. Earlier versions may work,
|
|
|
42 |
but mingw-runtime >= 2.0 and w32api >= 2.0 are essential. The release
|
|
|
43 |
candidates of gcc-3.3.1 have also been tested.
|
| 9469 |
ripley |
44 |
|
| 17319 |
ripley |
45 |
No earlier Mingw compiler has been tested and support for using the
|
|
|
46 |
Cygwin compiler has been withdrawn. If you see errors about `-shared'
|
|
|
47 |
not being understood or `__imp_foo' missing, you are not using the
|
|
|
48 |
recommended compiler.
|
| 10374 |
ripley |
49 |
|
| 9469 |
ripley |
50 |
|
| 15268 |
ripley |
51 |
The Windows port of perl5, available via
|
|
|
52 |
http://www.activestate.com/Products/ActivePerl/.
|
| 19692 |
maechler |
53 |
BEWARE: you do need the *Windows* port and not the Cygwin one.
|
| 9469 |
ripley |
54 |
|
| 10374 |
ripley |
55 |
|
| 9469 |
ripley |
56 |
If you want to make compiled html (.chm) files you will need the
|
| 15268 |
ripley |
57 |
Microsoft HTML Help Workshop, currently available for download at
|
| 20009 |
ripley |
58 |
http://msdn.microsoft.com/library/en-us/htmlhelp/html/hwmicrosofthtmlhelpdownloads.asp
|
| 16156 |
ripley |
59 |
and http://www.microsoft.com/office/ork/xp/appndx/appa06.htm
|
| 15268 |
ripley |
60 |
|
| 13282 |
ripley |
61 |
You may need this on the same drive as the other tools. (Although we
|
| 12630 |
ripley |
62 |
have successfully used it elsewhere, others have reported problems).
|
| 9469 |
ripley |
63 |
|
|
|
64 |
|
|
|
65 |
For large packages it is helpful to make zipped help and/or data
|
|
|
66 |
files: for that you need zip and unzip from the Info-ZIP project
|
|
|
67 |
(www.info-zip.org and mirrors, and included in our tools.zip).
|
|
|
68 |
|
| 10228 |
ripley |
69 |
|
| 9469 |
ripley |
70 |
All of these need to be installed and in your path, and the
|
| 20879 |
ripley |
71 |
appropriate environment variables set.
|
|
|
72 |
Edit MkRules to set the appropriate paths as needed.
|
| 21544 |
ripley |
73 |
BEWARE: MkRules contains tabs and some editors (e.g. WinEdt) silently
|
|
|
74 |
remove them.
|
|
|
75 |
|
| 27917 |
ripley |
76 |
Do not use filepaths with spaces in: you can always use the short forms
|
|
|
77 |
(found by dir /x at the Windows command line).
|
| 20879 |
ripley |
78 |
Do not use unnecessary quotes in your PATH variable.
|
| 23336 |
ripley |
79 |
Do not have our toolset and any other version of cygwin1.dll in your path.
|
| 9469 |
ripley |
80 |
|
| 20879 |
ripley |
81 |
|
| 11470 |
ripley |
82 |
BEWARE: if you have Norton Anti-Virus you may need to disable it.
|
| 19840 |
ripley |
83 |
Some versions lock up the machine when windres is run. (Norton
|
|
|
84 |
Anti-Virus 2002 causes no problems.)
|
| 11470 |
ripley |
85 |
|
| 12256 |
pd |
86 |
BEWARE: Don't expect this to work if the path to R_HOME contains spaces.
|
| 25863 |
ripley |
87 |
It may work, but we don't recommend it.
|
| 11793 |
ripley |
88 |
|
| 17439 |
ripley |
89 |
Then
|
| 9469 |
ripley |
90 |
|
| 17439 |
ripley |
91 |
cd R_HOME\src\gnuwin32
|
| 19891 |
ripley |
92 |
make libR.a libRblas.a
|
| 17439 |
ripley |
93 |
|
|
|
94 |
which might take up to a minute. This only needs to be done once for
|
| 19372 |
ripley |
95 |
each R release, and will be done automatically when needed if
|
| 17439 |
ripley |
96 |
omitted at this point.
|
|
|
97 |
|
|
|
98 |
|
| 10228 |
ripley |
99 |
At this point there are two approaches:
|
|
|
100 |
|
|
|
101 |
1) Streamlined way
|
|
|
102 |
|
| 27917 |
ripley |
103 |
Assuming that ...\rw10xx\bin is in your path, use
|
| 10228 |
ripley |
104 |
|
|
|
105 |
Rcmd INSTALL package
|
|
|
106 |
|
|
|
107 |
where `package' can be a directory or a .tar.gz file. See
|
|
|
108 |
Rcmd INSTALL --help for a full list of options, which include
|
|
|
109 |
|
|
|
110 |
-l, --library=LIB install packages to library tree LIB
|
|
|
111 |
--docs=TYPE type(s) of documentation to build and install
|
|
|
112 |
--use-zip-data collect data files in zip archive
|
|
|
113 |
--use-zip-help collect help and examples into zip archives
|
|
|
114 |
--use-zip combine `--use-zip-data' and `--use-zip-help'
|
|
|
115 |
|
| 27917 |
ripley |
116 |
TYPE can be "none" or "normal" or "chm" (the default) or "all"
|
| 10228 |
ripley |
117 |
|
|
|
118 |
|
| 10374 |
ripley |
119 |
2) Manual `full-control'
|
|
|
120 |
|
| 9469 |
ripley |
121 |
For each package you want to install, unpack it to a directory, say
|
| 12256 |
pd |
122 |
mypkg, in R_HOME\src\library, and run
|
| 9469 |
ripley |
123 |
|
| 12256 |
pd |
124 |
cd R_HOME\src\gnuwin32
|
| 9469 |
ripley |
125 |
make pkg-mypkg
|
|
|
126 |
|
| 10374 |
ripley |
127 |
|
|
|
128 |
Customising compilation
|
|
|
129 |
=======================
|
|
|
130 |
|
| 9469 |
ripley |
131 |
The Makefiles can be customized: in particular the name of the DLL can
|
|
|
132 |
be set (for example we once needed integrate-DLLNM=adapt), the compile
|
|
|
133 |
flags can be set (see the examples in MakeDll) and the types of help
|
|
|
134 |
(if any) to be generated can be chosen (variables HELP and WINHELP).
|
|
|
135 |
The simplest way to customize the compilation steps is to set variables in
|
| 10374 |
ripley |
136 |
a file src\Makevars.win, which will automatically be included by MakeDLL.
|
|
|
137 |
For example, for RODBC src\Makevars.win could include the line
|
| 9469 |
ripley |
138 |
|
|
|
139 |
DLLLIBS+=-lodbc32
|
|
|
140 |
|
|
|
141 |
or, equivalently,
|
|
|
142 |
|
|
|
143 |
RODBC-DLLLIBS=-lodbc32
|
|
|
144 |
|
|
|
145 |
If you have a file src\Makefile.win, that will be used as the makefile
|
| 10374 |
ripley |
146 |
for source compilation in place of our makefile MakeDll.
|
| 9469 |
ripley |
147 |
|
| 10374 |
ripley |
148 |
BEWARE: files src\Makefile or src\Makevars will be used if they exist
|
|
|
149 |
and the .win equivalents do not. Such files included in package
|
|
|
150 |
sources are usually designed for use under Unix and are best removed.
|
| 9469 |
ripley |
151 |
|
| 10558 |
ripley |
152 |
BEWARE: references to variables in R.dll are converted to the
|
| 15732 |
ripley |
153 |
right form by using the header files. You must include them.
|
| 10374 |
ripley |
154 |
|
| 10558 |
ripley |
155 |
|
| 9469 |
ripley |
156 |
Using zipped help files
|
|
|
157 |
=======================
|
|
|
158 |
|
|
|
159 |
You will need zip installed, of course. Just run
|
|
|
160 |
|
|
|
161 |
make ziponly-mypkg
|
|
|
162 |
|
|
|
163 |
after building mypkg. Target `ziphelp-mypkg' will make the zip files
|
|
|
164 |
but not remove the separate files: this can be used for testing.
|
|
|
165 |
|
| 10374 |
ripley |
166 |
This is an option to Rcmd INSTALL.
|
| 9469 |
ripley |
167 |
|
| 10374 |
ripley |
168 |
|
| 9469 |
ripley |
169 |
Using zipped data files
|
|
|
170 |
=======================
|
|
|
171 |
|
|
|
172 |
You will need zip installed. Just run
|
|
|
173 |
|
|
|
174 |
make zipdata-mypkg
|
|
|
175 |
|
|
|
176 |
after building mypkg. This is recommended if you have either many small
|
|
|
177 |
data files (as in package Devore5) or a few large data files.
|
|
|
178 |
|
| 12256 |
pd |
179 |
This is an option to Rcmd INSTALL.
|
| 9469 |
ripley |
180 |
|
| 12256 |
pd |
181 |
|
| 10374 |
ripley |
182 |
Checking and building packages
|
|
|
183 |
==============================
|
| 9469 |
ripley |
184 |
|
|
|
185 |
The equivalent of `R CMD check mypkg' on Unix is
|
|
|
186 |
|
| 10228 |
ripley |
187 |
Rcmd check mypkg
|
| 9469 |
ripley |
188 |
|
| 12256 |
pd |
189 |
Use `Rcmd check --help' for full details. There is an analogous call for
|
|
|
190 |
build.
|
| 9469 |
ripley |
191 |
|
| 22669 |
ripley |
192 |
You may need to set TMPDIR to (the absolute path to) a suitable
|
| 23146 |
ripley |
193 |
temporary directory: the default is c:/TEMP. (Use forward slashes,
|
| 27917 |
ripley |
194 |
although the code will try to convert backslashes as from 1.7.0. Do
|
|
|
195 |
not have spaces in the filepath, although from 1.9.0 we attempt to
|
|
|
196 |
work around this.)
|
| 9469 |
ripley |
197 |
|
| 21082 |
ripley |
198 |
These have been used successfully under NT/2000/XP. We have seen
|
| 15268 |
ripley |
199 |
problems with some Perl versions on Win 95/98/ME, but believe these
|
|
|
200 |
are now resolved. make pkgcheck-mypkg will perform the tests of the
|
| 19136 |
ripley |
201 |
examples and tests directory, as far as we know on all versions of Windows.
|
| 9469 |
ripley |
202 |
|
| 12256 |
pd |
203 |
|
| 25857 |
murdoch |
204 |
Building the package reference manual
|
|
|
205 |
=====================================
|
|
|
206 |
|
|
|
207 |
See the Writing R Extensions manual for full instructions on converting
|
|
|
208 |
package help files to DVI or PDF format. If you have the necessary tools
|
|
|
209 |
installed,
|
|
|
210 |
|
|
|
211 |
Rcmd rd2dvi.sh --pdf mypkg
|
|
|
212 |
|
|
|
213 |
will use LaTeX to build a PDF version of the reference manual.
|
|
|
214 |
|
|
|
215 |
|
| 9469 |
ripley |
216 |
Debugging
|
|
|
217 |
=========
|
|
|
218 |
|
|
|
219 |
See the rw-FAQ.
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
Using Visual C++
|
|
|
223 |
================
|
|
|
224 |
|
|
|
225 |
You may if you prefer use Visual C++ to make the DLLs (unless they use
|
|
|
226 |
Fortran source!). First build the import library R.lib by
|
|
|
227 |
|
|
|
228 |
lib /def:R.exp /out:Rdll.lib
|
|
|
229 |
|
|
|
230 |
Then you can compile the objects and build the DLL by
|
|
|
231 |
|
|
|
232 |
cl /MT /Ox /D "WIN32" /c *.c
|
|
|
233 |
link /dll /def:mypkg.def /out:mypkg.dll *.obj Rdll.lib
|
|
|
234 |
|
|
|
235 |
where you will need to create the .def file by hand listing the entry
|
|
|
236 |
points to be exported. (If there are just a few you can use /export
|
|
|
237 |
flags instead.) If the C sources use R header files you will need to
|
|
|
238 |
arrange for these to be searched, perhaps by including in the cl line
|
|
|
239 |
|
|
|
240 |
/I ..\..\..\include
|
|
|
241 |
|
|
|
242 |
If you build a debug version of the DLL in the development
|
|
|
243 |
environment, you can debug the DLL code there just by setting the
|
|
|
244 |
executable to be debugged as the full path to the R front-end.
|
|
|
245 |
|
| 9940 |
pd |
246 |
Extra care is needed when referencing variables (rather than
|
|
|
247 |
functions) exported from R.dll. These must be declared
|
| 15732 |
ripley |
248 |
__declspec(dllimport) (as in R's own header files). A list of the
|
|
|
249 |
relevant variables is in the file `exported-vars'.
|
| 9469 |
ripley |
250 |
|
| 9940 |
pd |
251 |
|
| 9469 |
ripley |
252 |
Using Borland C++
|
|
|
253 |
=================
|
|
|
254 |
|
|
|
255 |
Borland C++5.5 is available as a free download from
|
|
|
256 |
http://www.borland.com/bcppbuilder/freecompiler/ and as part of C++
|
|
|
257 |
Builder 5. The following will make convolve.dll from convolve.c (flag
|
| 17319 |
ripley |
258 |
-6 optimizes for a Pentium Pro/II/III/4, and -u- removes extra underscores)
|
| 9469 |
ripley |
259 |
|
|
|
260 |
bcc32 -u- -6 -O2 -WDE convolve.c
|
|
|
261 |
|
|
|
262 |
You can build an import library for R.dll by copying R.exp to R.def and
|
|
|
263 |
using
|
|
|
264 |
|
|
|
265 |
implib R.lib R.def
|
|
|
266 |
|
|
|
267 |
and then add R.lib to the bcc32 command line, for example (from
|
| 9940 |
pd |
268 |
Venables & Ripley's `S Programming')
|
| 9469 |
ripley |
269 |
|
| 19136 |
ripley |
270 |
bcc32 -u- -6 -O2 -WDE -I\R\rw1050\src\include VCrndR.c R.lib
|
| 9469 |
ripley |
271 |
|
| 10374 |
ripley |
272 |
We believe that when referencing variables (rather than functions)
|
|
|
273 |
exported from R.dll these must be declared __declspec(dllimport) just
|
|
|
274 |
as for VC++.
|
| 9469 |
ripley |
275 |
|
| 9940 |
pd |
276 |
|
| 9469 |
ripley |
277 |
Using other compilers and languages
|
|
|
278 |
===================================
|
|
|
279 |
|
| 9940 |
pd |
280 |
To use C++ see the section in the R for Windows FAQ. You can include
|
| 14462 |
ripley |
281 |
C++ code in packages and the supplied Makefiles will compile with g++
|
|
|
282 |
and link the DLL using g++ (and hence link against libstc++.a). Use
|
|
|
283 |
of C++ I/O may or may not work, and has been seen to crash R.
|
| 9469 |
ripley |
284 |
|
|
|
285 |
For other compilers you will need to arrange to produce a DLL with
|
| 14462 |
ripley |
286 |
cdecl (also known as _cdecl or __cdecl) linkage. The mingw port (and
|
|
|
287 |
VC++) uses no `name mangling' at all, so that if for example your
|
| 9469 |
ripley |
288 |
compiler adds leading or trailing underscores you will need to use the
|
|
|
289 |
transformed symbol in the call to .C in your R code. Many compilers
|
|
|
290 |
can produce cdecl DLLs by a suitable choice of flags, but if yours
|
|
|
291 |
cannot you may need to write some `glue' code in C to interface to the
|
|
|
292 |
DLL.
|
|
|
293 |
|
|
|
294 |
If you use .Fortran this appends an underscore and does no case
|
|
|
295 |
conversion at all to the symbol name. It is normally best to use
|
|
|
296 |
.C with compilers other than g77 and map the name manually if necessary.
|
|
|
297 |
|
|
|
298 |
Care is needed in passing character strings to and from a DLL by .C:
|
|
|
299 |
they must be equivalent to the C type char** and null-terminated. Not
|
| 14462 |
ripley |
300 |
even the mingw g77 Fortran uses null-terminated strings.
|
| 9469 |
ripley |
301 |
|
| 10374 |
ripley |
302 |
WARNING: DLLs made with some compilers reset the FPU in their startup
|
| 14462 |
ripley |
303 |
code (Delphi has been one), and this will cause operations such as
|
|
|
304 |
0./0. to crash R. You can re-set the FPU to the correct values by a
|
|
|
305 |
call to the C entry point Rwin_fpset().
|
| 9469 |
ripley |
306 |
|
| 10089 |
ripley |
307 |
|
| 9469 |
ripley |
308 |
Simple Ports
|
|
|
309 |
============
|
|
|
310 |
|
| 10374 |
ripley |
311 |
If your package has no C nor Fortran nor C++ sources, several steps
|
| 9940 |
pd |
312 |
can be omitted.
|
| 9469 |
ripley |
313 |
|
|
|
314 |
You will need
|
|
|
315 |
|
| 11470 |
ripley |
316 |
suitable versions of Unix tools including make, sh, rm, sed, awk,
|
|
|
317 |
mkdir, echo, cp and cat; we have packaged a set at
|
|
|
318 |
http://www.stats.ox.ac.uk/pub/Rtools/tools.zip.
|
| 9469 |
ripley |
319 |
|
|
|
320 |
perl5, available via http://www.activestate.com/Products/ActivePerl/
|
|
|
321 |
|
|
|
322 |
All of these need to be installed and in your path, and the
|
|
|
323 |
appropriate environment variables set.
|
|
|
324 |
|
|
|
325 |
For each package you want to install, unpack it to a directory, say
|
|
|
326 |
mypkg, in RHOME\src\library, and run
|
|
|
327 |
|
|
|
328 |
cd RHOME\src\gnuwin32
|
|
|
329 |
make pkg-mypkg
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
Non-standard locations
|
|
|
333 |
======================
|
|
|
334 |
|
|
|
335 |
You can specify the location of the package source by PKGDIR and the
|
|
|
336 |
library in which to install the package by RLIB, as in
|
|
|
337 |
|
|
|
338 |
make PKGDIR=/mysources RLIB=/R/library pkg-mypkg
|
|
|
339 |
|
| 10228 |
ripley |
340 |
which installs the package in \mysources\mypkg as \R\library\mypkg.
|
| 9469 |
ripley |
341 |
|
| 10228 |
ripley |
342 |
It may well be simpler to use
|
| 9469 |
ripley |
343 |
|
| 10228 |
ripley |
344 |
Rcmd INSTALL -l /R/library /mysources/mypkg
|
|
|
345 |
|
| 27917 |
ripley |
346 |
Once again, if the paths contain spaces use the short forms, although
|
|
|
347 |
Rcmd INSTALL will attempt to convert the paths if needed (but they
|
|
|
348 |
will need to be quoted on the command line).
|
| 10228 |
ripley |
349 |
|
| 27917 |
ripley |
350 |
|
| 9469 |
ripley |
351 |
Cross-building packages on Linux
|
|
|
352 |
================================
|
|
|
353 |
|
| 9524 |
ripley |
354 |
It is straightforward to build a package on a ix86-linux system,
|
| 14462 |
ripley |
355 |
although it is not possible (and we have tried, including using WINE)
|
| 25863 |
ripley |
356 |
to cross-build .chm files. You will need an installation of R for
|
|
|
357 |
Windows, either copied from a Windows system or cross-compiled.
|
| 9469 |
ripley |
358 |
|
| 10545 |
ripley |
359 |
First you need to set up the cross-compilers and tools (see file
|
|
|
360 |
src/gnuwin32/INSTALL in the full source distribution) and have them
|
| 22658 |
ripley |
361 |
in your path. We will assume that your Linux installation has Perl5,
|
| 10545 |
ripley |
362 |
unzip and zip.
|
| 9469 |
ripley |
363 |
|
|
|
364 |
Edit MkRules to set BUILD=CROSS and the appropriate paths (including
|
| 22658 |
ripley |
365 |
HEADER) as needed.
|
| 9469 |
ripley |
366 |
|
|
|
367 |
Then packages can be made as natively, for example by
|
|
|
368 |
|
|
|
369 |
cd .../src/gnuwin32
|
| 17495 |
ripley |
370 |
make PKGDIR=/mysources RLIB=/R/win/library pkg-mypkg
|
| 9469 |
ripley |
371 |
make PKGDIR=/mysources RLIB=/R/win/library pkgcheck-mypkg
|
|
|
372 |
cd /R/win/library
|
| 25863 |
ripley |
373 |
zip -r9X /dest/mypkg.zip mypkg
|
| 9469 |
ripley |
374 |
|
| 17495 |
ripley |
375 |
(Rcmd is a Windows executable, so cannot be used.)
|
| 9469 |
ripley |
376 |
|
| 10228 |
ripley |
377 |
|
| 9469 |
ripley |
378 |
Feedback
|
|
|
379 |
========
|
|
|
380 |
|
| 18569 |
ripley |
381 |
Please send comments and bug reports to
|
| 9469 |
ripley |
382 |
|
| 18581 |
ripley |
383 |
R-windows@r-project.org
|
| 18569 |
ripley |
384 |
|