]> git.donarmstrong.com Git - cran2deb.git/blob - pkg/trunk/R/build.R
build: always force the build of packages mentioned on the command line.
[cran2deb.git] / pkg / trunk / R / build.R
1
2 build <- function(name,extra_deps,force=F) {
3     # can't, and hence don't need to, build base packages
4     if (name %in% base_pkgs) {
5         return(T)
6     }
7     log_clear()
8     dir <- setup()
9
10     # obtain the Debian version-to-be
11     version <- try(new_build_version(name))
12     if (inherits(version,'try-error')) {
13         error('failed to build',name)
14         return(NULL)
15     }
16
17     result <- try((function() {
18         if (!force && !needs_build(name,version)) {
19             notice('skipping build of',name)
20             return(NULL)
21         }
22
23         pkg <- prepare_new_debian(prepare_pkg(dir,name),extra_deps)
24         if (pkg$debversion != version) {
25             fail('expected Debian version',version,'not equal to actual version',pkg$debversion)
26         }
27         # delete the current archive (XXX: assumes mini-dinstall)
28         for (subdir in c('mini-dinstall','unstable')) {
29             path = file.path(dinstall_archive,subdir)
30             if (file.exists(path)) {
31                 unlink(path,recursive=T)
32             }
33         }
34
35         # delete notes of upload
36         file.remove(Sys.glob(file.path(pbuilder_results,'*.upload')))
37
38         # make mini-dinstall generate the skeleton of the archive
39         ret = log_system('umask 022;mini-dinstall --batch -c',dinstall_config)
40         if (ret != 0) {
41             fail('failed to create archive')
42         }
43
44         # pull in all the R dependencies
45         notice('dependencies:',paste(pkg$depends$r,collapse=', '))
46         for (dep in pkg$depends$r) {
47             if (pkgname_as_debian(dep) %in% debian_pkgs) {
48                 notice('using Debian package of',dep)
49                 next
50             }
51             # otherwise, convert to source package name
52             srcdep = pkgname_as_debian(dep,binary=F)
53
54             notice('uploading',srcdep)
55             ret = log_system('umask 022;dput','-c',shQuote(dput_config),'local'
56                         ,changesfile(srcdep))
57             if (ret != 0) {
58                 fail('upload of dependency failed! maybe you did not build it first?')
59             }
60         }
61         build_debian(pkg)
62
63         # upload the package
64         ret = log_system('umask 022;dput','-c',shQuote(dput_config),'local'
65                     ,changesfile(pkg$srcname,pkg$debversion))
66         if (ret != 0) {
67             fail('upload failed!')
68         }
69
70         return(pkg$debversion)
71     })())
72     cleanup(dir)
73     if (is.null(result)) {
74         # nothing was done so escape asap.
75         return(result)
76     }
77
78     # otherwise record progress
79     failed = inherits(result,'try-error')
80     if (failed) {
81         error('failure of',name,'means these packages will fail:'
82                      ,paste(r_dependency_closure(name,forward_arcs=F),collapse=', '))
83     }
84     db_record_build(name, version, log_retrieve(), !failed)
85     return(!failed)
86 }
87
88 needs_build <- function(name,version) {
89     # see if the last build was successful
90     build <- db_latest_build(name)
91     if (!is.null(build) && build$success) {
92         # then something must have changed for us to attempt this
93         # build
94         if (build$r_version == version_upstream(version) &&
95             build$deb_epoch == version_epoch(version) &&
96             build$db_version == db_get_version()) {
97             return(F)
98         }
99     } else {
100         # always rebuild on failure or no record
101         return(T)
102     }
103     # see if it has already been built
104     srcname <- pkgname_as_debian(name,binary=F)
105     debname <- pkgname_as_debian(name,binary=T)
106     if (file.exists(changesfile(srcname, version))) {
107         notice('already built',srcname,'version',version)
108         return(F)
109     }
110
111     # XXX: what about building newer versions of Debian packages?
112     if (debname %in% debian_pkgs) {
113         notice(srcname,' exists in Debian (perhaps a different version)')
114         return(F)
115     }
116
117     rm(debname,srcname)
118     return(T)
119 }
120
121 build_debian <- function(pkg) {
122     wd <- getwd()
123     setwd(pkg$path)
124     notice('building Debian package'
125                  ,pkg$debname
126                  ,paste('(',pkg$debversion,')',sep='')
127                  ,'...')
128
129     cmd = paste('pdebuild --configfile',shQuote(pbuilder_config))
130     if (version_revision(pkg$debversion) > 2) {
131         cmd = paste(cmd,'--debbuildopts','-sd')
132         notice('build should exclude original source')
133     }
134     ret = log_system(cmd)
135     setwd(wd)
136     if (ret != 0) {
137         fail('Failed to build package.')
138     }
139 }
140