makefile: Generate Makefile from a given list of rules ('job').

View source: R/makefile.R

makefileR Documentation

Generate Makefile from a given list of rules (job).

Description

In the (GNU) make jargon, a rule is a sequence of commands to build a result. In this package, a rule should be understood similarly: It is a command or a sequence of commands that optionally produces some files and depends on some other files (such as data files or scripts) or other rules. Moreover, a rule contains a command for cleanup, i.e., for removal of generated files.

Usage

makefile(
  job = list(),
  fileName = NULL,
  makeScript = "Makefile.R",
  vars = NULL,
  all = TRUE,
  tasks = TRUE,
  clean = TRUE,
  makefile = TRUE,
  depends = NULL
)

Arguments

job

A list of rules (i.e., instances of the S3 class rmake.rule - see rule())

fileName

A file to write to. If NULL, the result is returned as a character vector instead of writing to a file.

makeScript

The name of the file that calls this function (used to generate the makefile rule)

vars

A named character vector of shell variables that will be declared in the resulting Makefile (in addition to ⁠[defaultVars]⁠)

all

TRUE if the all rule should be automatically created and added: the created all rule has dependencies on all the other rules, which causes everything to be built if ⁠make all⁠ is executed in the shell's command line.

tasks

TRUE if "task" rules should be automatically created and added – see rule() for more details.

clean

TRUE if the clean rule should be automatically created and added

makefile

TRUE if the Makefile rule should be automatically created and added: this rule ensures that any change in the R script that generates the Makefile (i.e., that calls makefile()) triggers the re-generation of the Makefile at the beginning of any build.

depends

A character vector of file names that the makefile generating script depends on

Details

The makefile() function takes a list of rules (see rule()) and generates a Makefile from them. Additionally, all and clean rules are optionally generated too, which can be executed from the shell by issuing the ⁠make all⁠ or ⁠make clean⁠ command, respectively, to build everything or erase all generated files.

If there is a need to group some rules together, it can be done either via dependencies or by using the task mechanism. Each rule may be assigned one or more tasks (see task in rule()). Each task is then created as a standalone rule depending on the assigned rules. That way, executing ⁠make task_name⁠ will build all rules with the assigned task task_name. By default, all rules are assigned to task all, which allows ⁠make all⁠ to build everything.

Value

If fileName is NULL, the function returns a character vector with the contents of the Makefile. Otherwise, the content is written to the given fileName.

Author(s)

Michal Burda

See Also

rule(), rmakeSkeleton()

Examples

# create some jobs
job <- list(
    rRule('dataset.rds', 'preprocess.R', 'dataset.csv'),
    markdownRule('report.pdf', 'report.Rmd', 'dataset.rds'),
    markdownRule('details.pdf', 'details.Rmd', 'dataset.rds'))

# generate Makefile (output as a character vector)
makefile(job)

# generate to file
tmp <- tempdir()
makefile(job, file.path(tmp, "Makefile"))

rmake documentation built on Nov. 12, 2025, 9:06 a.m.