/
platform.R
449 lines (403 loc) · 14.1 KB
/
platform.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#' R platforms
#'
#' @details
#' `current_r_platform()` detects the platform of the current R version.
#' `current_r_platform_data()` is similar, but returns the raw data instead
#' of a character scalar.
#'
#' By default pkgcache works with source packages and binary packages for
#' the current platform. You can change this, by providing different
#' platform names as arguments to
#' [`cranlike_metadata_cache$new()`][cranlike_metadata_cache],
#' [repo_status()], etc.
#'
#' These functions accept the following platform names:
#' * `"source"` for source packages,
#' * `"macos"` for macOS binaries that are appropriate for the R versions
#' pkgcache is working with. Packages for incompatible CPU architectures are
#' dropped (defaulting to the CPU of the current macOS machine and x86_64 on
#' non-macOS systems). The macOS Darwin version is selected based on the
#' CRAN macOS binaries. E.g. on R 3.5.0 macOS binaries
#' are built for macOS El Capitan.
#' * `"windows"` for Windows binaries for the default CRAN architecture.
#' This is currently Windows Vista for all supported R versions, but it
#' might change in the future. The actual binary packages in the
#' repository might support both 32 bit and 64 builds, or only one of
#' them. In practice 32-bit only packages are very rare. CRAN builds
#' before and including R 4.1 have both architectures, from R 4.2 they
#' are 64 bit only. `"windows"` is an alias to `i386+x86_64-w64-mingw32`
#' currently.
#' * A platform string like `R.version$platform`, but on Linux the name
#' and version of the distribution are also included. Examples:
#' - `x86_64-apple-darwin17.0`: macOS High Sierra.
#' - `aarch64-apple-darwin20`: macOS Big Sur on arm64.
#' - `x86_64-w64-mingw32`: 64 bit Windows.
#' - `i386-w64-mingw32`: 32 bit Windows.
#' - `i386+x86_64-w64-mingw32`: 64 bit + 32 bit Windows.
#' - `i386-pc-solaris2.10`: 32 bit Solaris. (Some broken 64 Solaris
#' builds might have the same platform string, unfortunately.)
#' - `x86_64-pc-linux-gnu-debian-10`: Debian Linux 10 on x86_64.
#' - `x86_64-pc-linux-musl-alpine-3.14.1`: Alpine Linux.
#' - `x86_64-pc-linux-gnu-unknown`: Unknown Linux Distribution on x86_64.
#' - `s390x-ibm-linux-gnu-ubuntu-20.04`: Ubuntu Linux 20.04 on S390x.
#' - `amd64-portbld-freebsd12.1`: FreeBSD 12.1 on x86_64.
#'
#' @return `current_r_platform()` returns a character scalar.
#'
#' `current_r_platform_data()` returns a data frame with character
#' scalar columns:
#' * `cpu`,
#' * `vendor`,
#' * `os`,
#' * `distribution` (only on Linux),
#' * `release` (only on Linux),
#' * `platform`: the concatenation of the other columns, separated by
#' a dash.
#' @export
#' @examples
#' current_r_platform()
current_r_platform <- function() {
current_r_platform_data()$platform
}
#' @export
#' @rdname current_r_platform
current_r_platform_data <- function() {
forced <- forced_platform()
if (!is.null(forced)) {
platform <- parse_platform(forced)
} else {
raw <- get_platform(forced = FALSE)
platform <- parse_platform(raw)
if (platform$os == "linux" || substr(platform$os, 1, 6) == "linux-") {
platform <- current_r_platform_data_linux(platform)
}
}
platform$platform <- apply(platform, 1, paste, collapse = "-")
platform
}
valid_platform_string <- function(x) {
grepl("^[^-].*[-][^-].*[-][^-].*$", x)
}
forced_platform <- function() {
opt <- getOption("pkg.current_platform")
if (!is.null(opt)) {
if (!is_string(opt)) {
stop("The `pkg.current_platform` option must be a string scalar.")
}
if (!valid_platform_string(opt)) {
stop("The pkg.current_platform` option must be a valid platform ",
"triple: `cpu-vendor-os`. \"", opt, "\" is not.")
}
return(opt)
}
env <- Sys.getenv("PKG_CURRENT_PLATFORM")
if (env != "") {
if (is.na(env) || !valid_platform_string(env)) {
stop("The `PKG_CURRENT_PLATFORM` environment variable must be a valid ",
"platform triple: \"cpu-vendor-os\". \"", env, "\" is not.")
}
return(env)
}
NULL
}
get_platform <- function(forced = TRUE) {
(if (forced) forced_platform()) %||% R.version$platform
}
#' @details
#' `default_platfoms()` returns the default platforms for the current R
#' session. These typically consist of the detected platform of the current
#' R session, and `"source"`, for source packages.
#'
#' @rdname current_r_platform
#' @return `default_platforms()` returns a character vector of the
#' default platforms.
#' @export
#' @examples
#' default_platforms()
default_platforms <- function() {
unique(c(current_r_platform(), "source"))
}
parse_platform <- function(x) {
pcs <- strsplit(x, "-", fixed = TRUE)
data.frame(
stringsAsFactors = FALSE,
cpu = vcapply(pcs, "[", 1),
vendor = vcapply(pcs, "[", 2),
os = vcapply(pcs, function(y) {
if (length(y) < 3) NA_character_ else paste(y[-(1:2)], collapse = "-")
})
)
}
get_cran_extension <- function(platform) {
res <- rep(NA_character_, length(platform))
res[platform == "source"] <- ".tar.gz"
res[platform %in% c("windows", "i386+x86_64-w64-mingw32",
"x86_64-w64-mingw32", "i386-w64-mingw32")] <- ".zip"
res[platform == "macos"] <- ".tgz"
dtl <- parse_platform(platform)
res[!is.na(dtl$os) & grepl("^darwin", dtl$os)] <- ".tgz"
if (anyNA(res)) {
res[is.na(res)] <- paste0("_R_", platform[is.na(res)], ".tar.gz")
}
res
}
get_all_package_dirs <- function(platforms, rversions) {
minors <- unique(get_minor_r_version(rversions))
res <- drop_nulls(lapply(platforms, get_package_dirs_for_platform, minors))
# zero size edge case
empty <- data.frame(
stringsAsFactors = FALSE,
platform = character(),
rversion = character(),
contriburl = character()
)
res <- lapply(res, function(x) { colnames(x) <- names(empty); x })
res <- c(list(empty), res)
mat <- do.call(rbind, c(res, list(stringsAsFactors = FALSE)))
res <- as_data_frame(mat)
res <- unique(res)
res
}
get_package_dirs_for_platform <- function(pl, minors) {
if (any(package_version(minors) < "3.2")) {
stop("pkgcache does not support packages for R versions before R 3.2")
}
## Should we add extra arch repos?
xtr <- getOption(
"pkg.extra_arch_repos",
Sys.getenv("PKG_EXTRA_ARCH_REPOS", FALSE)
)
if (pl == "source") {
return(cbind("source", "*", "src/contrib"))
}
if (pl %in% c("x86_64-w64-mingw32", "i386-w64-mingw32",
"i386+x86_64-w64-mingw32")) {
return(cbind(
pl,
minors,
paste0("bin/windows/contrib/", minors)
))
}
if (pl == "windows") {
return(cbind(
"i386+x86_64-w64-mingw32",
minors,
paste0("bin/windows/contrib/", minors)
))
}
if (pl == "macos") {
res1 <- lapply(minors, function(v) {
rpl <- get_cran_macos_platform(v)
prpl <- parse_platform(rpl$platform)
# On macos we use the current arch, otherwise intel
target_cpu <- if (Sys.info()["sysname"] == "Darwin") {
pcrt <- parse_platform(current_r_platform())
pcrt$cpu
} else {
"x86_64"
}
rpl <- rpl[prpl$cpu == target_cpu,, drop = FALSE ]
if (nrow(rpl)) {
cbind(rpl$platform, v, paste0(
"bin/macosx/",
ifelse(nchar(rpl$subdir), paste0(rpl$subdir, "/"), ""),
"contrib/",
v
))
}
})
return(do.call(rbind, res1))
}
## Which R versions match this platform on CRAN?
mcp <- macos_cran_platforms
cranmrv <- mcp[mcp$platform == pl & mcp$rversion %in% minors,]
rbind(
if (nrow(cranmrv)) {
dirs <- paste0(
"bin/macosx/",
ifelse(nchar(cranmrv$subdir), paste0(cranmrv$subdir, "/"), ""),
"contrib/",
cranmrv$rversion
)
cbind(pl, cranmrv$rversion, dirs)
},
if (xtr) cbind(pl, minors, paste0("bin/", pl, "/", minors))
)
}
macos_cran_platforms <- read.table(
header = TRUE,
stringsAsFactors = FALSE,
textConnection(
"rversion platform subdir
3.1.3 x86_64-apple-darwin10.8.0 mavericks
3.2.0 x86_64-apple-darwin13.4.0 mavericks
3.2.1 x86_64-apple-darwin13.4.0 mavericks
3.2.2 x86_64-apple-darwin13.4.0 mavericks
3.2.3 x86_64-apple-darwin13.4.0 mavericks
3.2.4 x86_64-apple-darwin13.4.0 mavericks
3.2.5 x86_64-apple-darwin13.4.0 mavericks
3.3.0 x86_64-apple-darwin13.4.0 mavericks
3.3.1 x86_64-apple-darwin13.4.0 mavericks
3.3.2 x86_64-apple-darwin13.4.0 mavericks
3.3.3 x86_64-apple-darwin13.4.0 mavericks
3.4.0 x86_64-apple-darwin15.6.0 el-capitan
3.4.1 x86_64-apple-darwin15.6.0 el-capitan
3.4.2 x86_64-apple-darwin15.6.0 el-capitan
3.4.3 x86_64-apple-darwin15.6.0 el-capitan
3.4.4 x86_64-apple-darwin15.6.0 el-capitan
3.5.0 x86_64-apple-darwin15.6.0 el-capitan
3.5.1 x86_64-apple-darwin15.6.0 el-capitan
3.5.2 x86_64-apple-darwin15.6.0 el-capitan
3.5.3 x86_64-apple-darwin15.6.0 el-capitan
3.6.0 x86_64-apple-darwin15.6.0 el-capitan
3.6.1 x86_64-apple-darwin15.6.0 el-capitan
3.6.2 x86_64-apple-darwin15.6.0 el-capitan
3.6.3 x86_64-apple-darwin15.6.0 el-capitan
4.0.0 x86_64-apple-darwin17.0 ''
4.0.1 x86_64-apple-darwin17.0 ''
4.0.2 x86_64-apple-darwin17.0 ''
4.0.3 x86_64-apple-darwin17.0 ''
4.0.4 x86_64-apple-darwin17.0 ''
4.0.5 x86_64-apple-darwin17.0 ''
4.1.0 x86_64-apple-darwin17.0 ''
4.1.0 aarch64-apple-darwin20 big-sur-arm64
4.1.1 x86_64-apple-darwin17.0 ''
4.1.1 aarch64-apple-darwin20 big-sur-arm64
4.2.0 x86_64-apple-darwin17.0 ''
4.2.0 aarch64-apple-darwin20 big-sur-arm64
4.3.0 x86_64-apple-darwin20 big-sur-x86_64
4.3.0 aarch64-apple-darwin20 big-sur-arm64
4.4.0 x86_64-apple-darwin20 big-sur-x86_64
4.4.0 aarch64-apple-darwin20 big-sur-arm64
4.5.0 x86_64-apple-darwin20 big-sur-x86_64
4.5.0 aarch64-apple-darwin20 big-sur-arm64
4.6.0 x86_64-apple-darwin20 big-sur-x86_64
4.6.0 aarch64-apple-darwin20 big-sur-arm64
5.0.0 x86_64-apple-darwin20 big-sur-x86_64
5.0.0 aarch64-apple-darwin20 big-sur-arm64
"))
# For now we only use the minor version number, because the CRAN OS version
# does not change for a patch version.
get_minor_r_version <- function(x) {
x <- package_version(x)
vapply(unclass(x), function(x) paste(x[1:2], collapse = "."), character(1))
}
macos_cran_platforms$rversion <- get_minor_r_version(
macos_cran_platforms$rversion
)
macos_cran_platforms <- unique(macos_cran_platforms)
get_cran_macos_platform <- function(v) {
if (v %in% macos_cran_platforms$rversion) {
macos_cran_platforms[macos_cran_platforms$rversion %in% v,,drop = FALSE]
} else {
utils::tail(macos_cran_platforms, 2)
}
}
#' Query the default CRAN repository for this session
#'
#' If `options("repos")` (see [options()]) contains an entry called
#' `"CRAN"`, then that is returned. If it is a list, it is converted
#' to a character vector.
#'
#' Otherwise the RStudio CRAN mirror is used.
#'
#' @return A named character vector of length one, where the
#' name is `"CRAN"`.
#'
#' @export
#' @examples
#' default_cran_mirror()
default_cran_mirror <- function() {
mirror <- getOption("repos")["CRAN"]
if (is.null(mirror) || is.na(mirror) || mirror == "@CRAN@") {
# This is a deliberate choice over cloud.r-project.org, because of a
# DNS fiasco.
c(CRAN = "https://cran.rstudio.com")
} else {
unlist(mirror)
}
}
#' Query Bioconductor version information
#'
#' Various helper functions to deal with Bioconductor repositories.
#' See <https://www.bioconductor.org/> for more information on
#' Bioconductor.
#'
#' `bioc_version()` queries the matching Bioconductor version for
#' an R version, defaulting to the current R version
#'
#' @param r_version The R version number to match.
#' @param forget Use `TRUE` to avoid caching the Bioconductor mapping.
#' @return `bioc_version()` returns a [package_version] object.
#'
#' @export
#' @examplesIf pkgcache:::run_examples()
#' bioc_version()
#' bioc_version("4.0")
#' bioc_version("4.1")
bioc_version <- function(r_version = getRversion(), forget = FALSE) {
bioconductor$get_bioc_version(r_version, forget)
}
#' @details
#' `bioc_version_map()` returns the current mapping between R versions
#' and Bioconductor versions.
#'
#' @return `bioc_version_map()` returns a data frame with columns:
#' * `bioc_version`: [package_version] object, Bioconductor versions.
#' * `r_version`: [package_version] object, the matching R versions.
#' * `bioc_status`: factor, with levels: `out-of-date`, `release`,
#' `devel`, `future`.
#'
#' @rdname bioc_version
#'
#' @export
#' @examplesIf pkgcache:::run_examples()
#' bioc_version_map()
bioc_version_map <- function(forget = FALSE) {
as_data_frame(bioconductor$get_version_map(forget))
}
#' @details
#' `bioc_devel_version()` returns the version number of the current
#' Bioconductor devel version.
#'
#' @return `bioc_devel_version()` returns a [package_version] object.
#' @rdname bioc_version
#'
#' @export
#' @examplesIf pkgcache:::run_examples()
#' bioc_devel_version()
bioc_devel_version <- function(forget = FALSE) {
bioconductor$get_devel_version(forget)
}
#' @details
#' `bioc_release_version()` returns the version number of the current
#' Bioconductor release.
#'
#' @return `bioc_release_version()` returns a [package_version] object.
#' @rdname bioc_version
#'
#' @export
#' @examplesIf pkgcache:::run_examples()
#' bioc_release_version()
bioc_release_version <- function(forget = FALSE) {
bioconductor$get_release_version(forget)
}
#' @details
#' `bioc_repos()` returns the Bioconductor repository URLs.
#'
#' See the `BioC_mirror` option and the `R_BIOC_MIRROR` and
#' `R_BIOC_VERSION` environment variables in the [pkgcache] manual page.
#' They can be used to customize the desired Bioconductor version.
#'
#' @param bioc_version Bioconductor version string or `package_version`
#' object, or the string `"auto"` to use the one matching the current R
#' version.
#'
#' @return `bioc_repos()` returns a named character vector.
#' @rdname bioc_version
#' @export
#' @examplesIf pkgcache:::run_examples()
#' bioc_repos()
bioc_repos <- function(bioc_version = "auto", forget = FALSE) {
bioconductor$get_repos(bioc_version, forget)
}