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