1##' Helper function to establish minimal compiler versions, currently limited
2##' only to \code{g++} which (particularly for older RHEL/CentOS releases) is
3##' too far behind current C++11 standards required for some packages.
4##'
5##' This function looks up \code{g++} (as well as optional values in the
6##' \code{CXX} and \code{CXX1X} environment variables) in the \code{PATH}.  For
7##' all values found, the output of \code{g++ -v} is analyzed for the version
8##' string, which is then compared to the given minimal version.
9##' @title Check for Minimal (g++) Compiler Version
10##' @param minVersion An object of type \code{package_version}, with a default
11##'  of version 4.6.0
12##' @return A boolean value is returned, indicating if the minimal version is
13##'  being met
14##' @author Dirk Eddelbuettel
15compilerCheck <- function(minVersion=package_version("4.6.0")) { # nocov start
16
17    binaries <- c("g++", Sys.getenv("CXX", unset=""), Sys.getenv("CXX1X", unset=""))
18    binpaths <- lapply(binaries, function(b) { if (b=="") NULL else Sys.which(b) })
19
20    allgood <- FALSE
21    rl <- lapply(binpaths, function(b) {
22        if (is.null(b)) return(NULL)
23        con <- pipe(paste(b, "-v 2>&1"), "r") # NB: not --version, but -v
24        lines <- readLines(con)
25        close(con)
26        lines <- lines[grepl("^g.. version", lines)]
27        if (length(lines) == 0) return(NULL)
28        ver <- strsplit(lines, " ")[[1]][3]   # format is 'gcc version x.y.z ....'
29        package_version(ver) >= minVersion
30    })
31    all(do.call(c, rl))                 # drops NULLs
32}                                       # nocov end
33
34## TODO: maybe not limit to gcc/g++
35## TODO: maybe be smarter about combination of path, CXX and CXX1X ?
36## TODO: maybe make env.var optional arguments too
37