| 1 |
#' @include with_.R |
| 2 |
NULL |
| 3 |
|
| 4 |
# Makevars -------------------------------------------------------------------- |
| 5 |
|
| 6 |
#' Create a new `Makevars` file, by adding new variables |
| 7 |
#' |
| 8 |
#' You probably want [with_makevars()] instead of this function. |
| 9 |
#' |
| 10 |
#' Unlike [with_makevars()], it does not activate the new `Makevars` |
| 11 |
#' file, i.e. it does not set the `R_MAKEVARS_USER` environment variable. |
| 12 |
#' |
| 13 |
#' @param variables `[named character]`\cr new variables and their values |
| 14 |
#' @param old_path `[character(1)]`\cr location of existing `Makevars` |
| 15 |
#' file to modify. |
| 16 |
#' @param new_path `[character(1)]`\cr location of the new `Makevars` file |
| 17 |
#' @param assignment `[character(1)]`\cr assignment type to use. |
| 18 |
#' |
| 19 |
#' @keywords internal |
| 20 |
#' @export |
| 21 |
set_makevars <- function( |
| 22 |
variables, |
| 23 |
old_path = makevars_user(), |
| 24 |
new_path = tempfile(), |
| 25 |
assignment = c("=", ":=", "?=", "+=") |
| 26 |
) { |
| 27 |
if (length(variables) == 0) { |
| 28 |
return() |
| 29 |
} |
| 30 |
stopifnot(is.named(variables)) |
| 31 |
|
| 32 |
assignment <- match.arg(assignment) |
| 33 |
|
| 34 |
old <- NULL |
| 35 |
if (length(old_path) == 1 && file.exists(old_path)) { |
| 36 |
lines <- readLines(old_path) |
| 37 |
old <- lines |
| 38 |
for (var in names(variables)) { |
| 39 |
loc <- grep( |
| 40 |
paste(c("^[[:space:]]*", var, "[[:space:]]*", "="), collapse = ""), |
| 41 |
lines |
| 42 |
) |
| 43 |
if (length(loc) == 0) { |
| 44 |
lines <- append(lines, paste(sep = assignment, var, variables[var])) |
| 45 |
} else if (length(loc) == 1) { |
| 46 |
lines[loc] <- paste(sep = assignment, var, variables[var]) |
| 47 |
} else { |
| 48 |
stop( |
| 49 |
"Multiple results for ", |
| 50 |
var, |
| 51 |
" found, something is wrong.", |
| 52 |
.call = FALSE |
| 53 |
) |
| 54 |
} |
| 55 |
} |
| 56 |
} else { |
| 57 |
lines <- paste(names(variables), variables, sep = assignment) |
| 58 |
} |
| 59 |
|
| 60 |
if (!identical(old, lines)) { |
| 61 |
writeLines(con = new_path, lines) |
| 62 |
} |
| 63 |
|
| 64 |
old |
| 65 |
} |
| 66 |
|
| 67 |
#' Makevars variables |
| 68 |
#' |
| 69 |
#' Temporarily change contents of an existing `Makevars` file. |
| 70 |
#' |
| 71 |
#' @details If no `Makevars` file exists or the fields in `new` do |
| 72 |
#' not exist in the existing `Makevars` file then the fields are added to |
| 73 |
#' the new file. Existing fields which are not included in `new` are |
| 74 |
#' appended unchanged. Fields which exist in `Makevars` and in `new` |
| 75 |
#' are modified to use the value in `new`. |
| 76 |
#' |
| 77 |
#' @template with |
| 78 |
#' @param new,.new `[named character]`\cr New variables and their values |
| 79 |
#' @param path,.path `[character(1)]`\cr location of existing `Makevars` file to modify. |
| 80 |
#' @param ... Additional new variables and their values. |
| 81 |
#' @param assignment,.assignment `[character(1)]`\cr assignment type to use. |
| 82 |
#' @inheritParams with_collate |
| 83 |
#' @examples |
| 84 |
#' writeLines("void foo(int* bar) { *bar = 1; }\n", "foo.c") |
| 85 |
#' system("R CMD SHLIB --preclean -c foo.c") |
| 86 |
#' with_makevars(c(CFLAGS = "-O3"), system("R CMD SHLIB --preclean -c foo.c")) |
| 87 |
#' unlink(c("foo.c", "foo.so")) |
| 88 |
#' @export |
| 89 |
with_makevars <- function( |
| 90 |
new, |
| 91 |
code, |
| 92 |
path = makevars_user(), |
| 93 |
assignment = c("=", ":=", "?=", "+=") |
| 94 |
) { |
| 95 |
assignment <- match.arg(assignment) |
| 96 |
makevars_file <- tempfile() |
| 97 |
on.exit(unlink(makevars_file), add = TRUE) |
| 98 |
force(path) |
| 99 |
with_envvar(c(R_MAKEVARS_USER = makevars_file), { |
| 100 |
set_makevars(new, path, makevars_file, assignment = assignment) |
| 101 |
force(code) |
| 102 |
}) |
| 103 |
} |
| 104 |
|
| 105 |
#' @rdname with_makevars |
| 106 |
#' @export |
| 107 |
local_makevars <- function( |
| 108 |
.new = list(), |
| 109 |
..., |
| 110 |
.path = makevars_user(), |
| 111 |
.assignment = c("=", ":=", "?=", "+="), |
| 112 |
.local_envir = parent.frame() |
| 113 |
) { |
| 114 |
.new <- utils::modifyList(as.list(.new), list(...)) |
| 115 |
.new <- as_character(.new) |
| 116 |
|
| 117 |
.assignment <- match.arg(.assignment) |
| 118 |
makevars_file <- tempfile() |
| 119 |
defer(unlink(makevars_file), envir = .local_envir) |
| 120 |
force(.path) |
| 121 |
local_envvar(c(R_MAKEVARS_USER = makevars_file), .local_envir = .local_envir) |
| 122 |
invisible(set_makevars(.new, .path, makevars_file, assignment = .assignment)) |
| 123 |
} |