first commit
[pkgdev.git] / R / load.R
CommitLineData
156f8ca6
BA
1# Core function to load a package (source R files and compile/load C libraries)
2# @param path Location of the (non-standard) package to be loaded
3# @param cc Compilator to be used (e.g. 'gcc -std=gnu99' [default])
4.pkgdev.load = function(path, cc) {
5
6 # Get package name from path
7 pathTokens = strsplit(path, c(.Platform$file.sep))[[1]]
8 pkgName = pathTokens[length(pathTokens)]
9
10 # Create base directory for pkgName under R_HOME_USER/pkgdev/pkgs/pkgName (if not existing)
11 pkdev_path = file.path(Sys.getenv("R_HOME_USER"), "pkgdev")
12 dir.create(file.path(pkdev_path,"pkgs",pkgName), showWarnings=FALSE)
13
14 # R code first
15 # Warning: R/tests folder should not be sourced
16 forbiddenPath = file.path(path,"R","tests")
17 for (fileOrDir in list.files( file.path(path,"R"),full.names=TRUE )) {
18 if (fileOrDir != forbiddenPath) {
19 if (file.info(fileOrDir)$isdir) {
20 rFiles = list.files(fileOrDir, pattern="\\.[RrSsq]$",
672594cf 21 full.names=TRUE, recursive=TRUE)
156f8ca6
BA
22 # NOTE: potential unexported functions are not hidden;
23 # the developer is assumed to handle this
24 lapply(rFiles, source)
25 }
26 else source(fileOrDir)
27 }
28 }
29
30 # Also load datasets (if any)
31 rData = list.files(file.path(path,"data"),pattern="\\.R(d|D)ata$",full.names=TRUE)
32 lapply(rData, load)
33
34 # This file tells if the package is currently loaded
35 pkgLoadFile = file.path(pkdev_path,"pkgs",pkgName,"loaded")
672594cf 36
156f8ca6
BA
37 if (file.exists(file.path(path,"src"))) {
38 # C code -- Warning: src/tests folder should not be listed
672594cf
BA
39 cFiles = list.files( file.path(path,"src"), pattern="\\.[cChH]$",
40 full.names=TRUE, recursive=TRUE, no..=TRUE )
41 cFiles = lapply(cFiles, function(x) {
42 if (nchar(x)>=5 && substr(x,1,5)=="tests") return ("")
43 return (x)
44 })
156f8ca6
BA
45
46 # Create folder R_HOME_USER/pkgdev/pkgs/pkgName/src (if not existing)
47 dir.create(file.path(pkdev_path,"pkgs",pkgName,"src"), showWarnings=FALSE)
48
49 # Generate suitable Makefile (all object files go at R_HOME_USER/pkgdev/pkgs/pkgName/src)
50 .generateMakefileLoad(path, cFiles, pkgName, cc)
51
52 # Compile in the right folder (R_HOME_USER/pkgdev/pkgs/pkgName/src)
53 save_wd = getwd()
54 setwd( file.path(pkdev_path,"pkgs",pkgName,"src") )
55 library(parallel)
56 system( paste( Sys.getenv("MAKE"), "depend", sep=' ') )
57 system( paste( Sys.getenv("MAKE"), "-j", detectCores(), "all", sep=' ') )
58 setwd(save_wd)
59
60 # Finally load library
61 sharedLib =
62 file.path(pkdev_path,"pkgs",pkgName,paste(pkgName,.Platform$dynlib.ext,sep=''))
63 if (file.exists(pkgLoadFile)) dyn.unload(sharedLib)
64 dyn.load(sharedLib)
65 }
66
67 # Mark package as 'loaded'
672594cf 68 file.create(pkgLoadFile)
156f8ca6
BA
69}
70
71# Generate appropriate Makefile under R_HOME_USER/pkgdev/pkgs/pkgName/src
72.generateMakefileLoad = function(path, cFiles, pkgName, cc) {
73
74 # Preparation: separate cFiles into codes and headers
75 codeFiles = grep(".*(c|C)$", cFiles, value=TRUE)
76 headerFiles = grep(".*(h|H)$", cFiles, value=TRUE)
77
78 # objectFiles = all .o files in current folder, duplicating file structure under path/src/
79 basePathFrom = file.path(path, "src")
80 pkdev_path = file.path(Sys.getenv("R_HOME_USER"), "pkgdev")
81 basePathTo = file.path(pkdev_path,"pkgs",pkgName,"src")
82 for (fileOrDir in list.files(basePathFrom, recursive=TRUE, include.dirs=TRUE)) {
83 if (file.info(file.path(basePathFrom,fileOrDir))$isdir) {
84 # Process folders only
85 dir.create(file.path(basePathTo,fileOrDir),showWarnings=FALSE,recursive=TRUE)
86 }
87 }
88 objectFiles = c()
89 for (codeFile in codeFiles) {
90 objectFiles = c(
91 objectFiles,
92 sub("(.*)\\.(c|C)$","\\1\\.o", sub(basePathFrom,basePathTo,codeFile,fixed=TRUE)))
93 }
94
95 # Build Makefile
96 makefile = paste('
97CC = ', cc, '
98INCLUDES = -I/usr/include/R/ -I/usr/local/include -I/usr/share/R/include
99LIBRARIES = -L/usr/lib -L/usr/lib/R/lib -lR -lm
100CFLAGS = -DNDEBUG -fpic -march=native -mtune=generic -O2 -pipe \\
101 -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2
102LDFLAGS = -shared -Wl,-O1,--sort-common,--as-needed,-z,relro
103LIB = ', paste(file.path("..",pkgName), .Platform$dynlib.ext, sep=''), '
104SRCS = ', paste(codeFiles, sep='', collapse=' '), '
105HEDS = ', paste(headerFiles, sep='', collapse=' '), '
106OBJS = ', paste(objectFiles, sep='', collapse= ' '), '
107all: $(LIB)
108$(LIB) : $(OBJS)
109 $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(LIB) $(LIBRARIES)', sep='')
110 compileObjects = ""
111for (i in 1:length(codeFiles)) {
112 compileObjects = paste(compileObjects, '
113', objectFiles[i], ' : ', codeFiles[i], '
114 $(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@', sep='')
115}
116 makefile = paste(makefile, compileObjects, '
117.PHONY: clean delib depend
118clean:
119 rm -f $(OBJS) ./.depend
120delib:
121 rm -f $(LIB)
122depend: .depend
123.depend: $(SRCS) $(HEDS)
124 rm -f ./.depend
125 $(CC) -MM $^ > ./.depend
126include .depend
127', sep='')
128
129 # Write it to disk
130 writeLines(makefile, file.path(pkdev_path,"pkgs",pkgName,"src","Makefile"))
131}