Command-line tools error in pre-installation phase of Rstan (mac)

Hi,

I am trying to install Rstan on my 10.13.6 High Sierra mac, with 3.5.1 ‘Feather Spray’ R version and a 1.1.419 Rstudio version, following the instructions for ‘Installing RStan on Mac or Linux’ on the rstan github page. I also posted this question on stackoverflow but wonder if this is a better forum for this question?

Because the macOS R toolchain installer kept giving an error during installation, I opted for the manual steps described here.

On my first attempt I simply updated my Xcode and followed the instructions, but as this did not seemed to work, I removed Xcode (also from my bin as suggested here) and reinstalled Xcode 10.1 (10B61), and accepted the license by running ‘sudo xcodebuild -license’ in the terminal as described here.

The command line tools seem available as running

> system('clang++ -v')

gives this output:

Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target:
x86_64-apple-darwin17.7.0 Thread model: posix InstalledDir:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

In the next step I ran the chunk of code for the configuration of clang++, after which running this line:

# print the contents to the console 
> cat(readLines(MAKEVARS), sep = "\n")

resulted into this:

LDFLAGS=  -L/usr/local/clang4/lib   # The following statement changes
the Fortran linking path  
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0   #
End Fortran linking path statement   # The following statement changes
the Fortran linking path  
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0   #
End Fortran linking path statement CC=/usr/local/clang4/bin/clang
CXX=/usr/local/clang4/bin/clang++ CXX1X=/usr/local/clang4/bin/clang++
CXX98=/usr/local/clang4/bin/clang++
CXX11=/usr/local/clang4/bin/clang++
CXX14=/usr/local/clang4/bin/clang++
CXX17=/usr/local/clang4/bin/clang++   # The following statement
changes the Fortran linking path  
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0   #
End Fortran linking path statement

CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable
-Wno-unused-function  -Wno-unknown-pragmas CC=clang CXX14=clang++ -arch x86_64 -ftemplate-depth-256

CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable
-Wno-unused-function  -Wno-unknown-pragmas CC=clang CXX14=clang++ -arch x86_64 -ftemplate-depth-256

Next, I installed the pkgbuild package as according to the instructions, and the next instructions were to run:

pkgbuild::has_build_tools(debug = TRUE)

To check that pkgbuild believes your toolchain is working. It also states:

If this returns FALSE, then go back to the previous section and install a toolchain correctly.

Upon running the line, I am prompted by Rstudio with this comment:

Building R package from source requires installation of additional build tools. Do you want to install the additional tools now?

If I click ‘no’ on the question whether I want to install the additional tools, R returns:

[1] FALSE

If I click ‘yes’ I arrive at this page. I have followed the instructions on that page to download and install the command line tools from Apple’s developer page (the second option mentioned), it made no difference and I am still prompet that additional build tools are required when checking the pkgbuild line. Trying through terminal running:

xcode-select --install

results in:

error: command line tools are already installed, use "Software Update"
to install updates

However, there are no software updates in my app store.

If I continue to run the next step of the instructions (so ignoring the pkgbuild error):

> fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
+   return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
+                            ' )

I get this output:

/Library/Frameworks/R.framework/Resources/bin/R: line 142:
/Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths: No such
file or directory

ERROR(s) during compilation: source code errors or compiler
configuration errors!

Program source:   1:    2: // includes from the plugin   3: #include
<R.h>   4: #include <Rdefines.h>   5: #include <R_ext/Error.h>   6:   
7:    8: // user includes   9:   10:   11: // declarations  12: extern
"C" {  13: SEXP file758212ccc99c( SEXP x, SEXP y) ;  14: }  15:   16:
// definition  17:   18: SEXP file758212ccc99c( SEXP x, SEXP y ){  19:
20:     return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;  21:           
22: Rf_warning("your C++ program does not return anything");   23: 
return R_NilValue ;   24: }  25:   26:  Error in compileCode(f, code,
language = language, verbose = verbose) :    Compilation ERROR,
function(s)/method(s) not created!
/Library/Frameworks/R.framework/Resources/bin/R: line 142:
/Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths: No such
file or directory In addition: Warning message: In system(cmd, intern
= !verbose) :   running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB
file758212ccc99c.cpp 2> file758212ccc99c.cpp.err.txt' had status 1

This makes me think maybe something is wrong with the path to the Rtools is that correct? My apologies if I am doing something obviously wrong or there is already a thread on this, but I could not find input on previous issues on this (pkgbuild check) stage of the process, and would highly appreciate any input of what I can try next!

If I should just remove everything (also Rstudio and R?) from my laptop I would also appreciate advise on how to make sure everything from these failed attempts is gone if possible, as I think I might have done that wrong last time?

Thank you!

1 Like

The installer of the C++ toolchain for Macs is in the process of being updated by @coatless .

In the meantime, I think it is possible to get Stan to work with Xcode. In R, execute

file.remove("~/.R/Makevars") # we can recreate this if necessary

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
MAKEVARS <- file.path(dotR, "Makevars")
if (!file.exists(MAKEVARS)) file.create(MAKEVARS)

cat(
  "\nCXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  -Wno-unknown-pragmas",
  "CC=clang",
  "CXX14=clang++ -arch x86_64 -ftemplate-depth-256",
  if (!file.exists("/usr/include/stdlib.h"))
    "CPPFLAGS = -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
  file = MAKEVARS, 
  sep = "\n", 
  append = TRUE
)

Then, tell us what happens with

pkgbuild::has_build_tools(debug = TRUE)

That is good to know, and thank you for your advise!

Although I am afraid it still returns with the message that I need to install additional tools, and if I click no with ‘False’:

pkgbuild::has_build_tools(debug = TRUE)
[1] FALSE

(on both if functions R returns TRUE)

Do you maybe have an other idea of what I could try?

Can you tell us what is the output of

Sys.which("clang")

?

Yes, the output is:

Sys.which(“clang”)
clang
“/usr/bin/clang”

And

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
	return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )

fx( 2L, 5 ) # should be 10

works or does not work?

I am afraid that line still gives an error too, it seems to be the same error as before in my original post:

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’ return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ; ’ )

/Library/Frameworks/R.framework/Resources/bin/R: line 142: /Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths: No such file or directory

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file758233c4469c( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file758233c4469c( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21:
22: Rf_warning(“your C++ program does not return anything”);
23: return R_NilValue ;
24: }
25:
26:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! /Library/Frameworks/R.framework/Resources/bin/R: line 142: /Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths: No such file or directory
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file758233c4469c.cpp 2> file758233c4469c.cpp.err.txt’ had status 1

And

file.exists("/Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths")

is TRUE or FALSE?

The reply is false:

file.exists(“/Library/Frameworks/R.framework/Resources/etc/x86_64/ldpaths”)
[1] FALSE

I am not sure what that entails I am afraid, does it mean it can not find the path to the Rtools? Is it possible to reset this?

Nah, it just means it cannot find itself. In RStudio, if you do

file.edit("/Library/Frameworks/R.framework/Resources/bin/R")

what do the lines surrounding line 142 say?

Lines 135-150 say:

while test -n "${1}"; do
  case ${1} in
    RHOME|--print-home)
      echo "${R_HOME}"; exit 0 ;;
    CMD)
      shift;
      export R_ARCH
      . "${R_HOME}/etc${R_ARCH}/ldpaths"                   #line 142
      exec sh "${R_HOME}/bin/Rcmd" "${@}" ;;
    -g|--gui)
      if test -n "`echo ${2} | ${SED} 's/^-.*//'`"; then
	gui="${2}"
        args="${args} ${1} ${2}"
	shift
      else
	error "option '${1}' requires an argument"

Can I get a

system2("ls", paste("-l -h", file.path(R.home("etc"), "ldpaths")))

?

Yes of course:

> system2("ls", paste("-l -h", file.path(R.home("etc"), "ldpaths")))
-rw-rw-r--  1 root  admin   820B  5 Jul 21:16 /Library/Frameworks/R.framework/Resources/etc/ldpaths

OK, so the nonexistent file actually does exist. What is

R.home("etc")

then?

How interesting! That line gives this:

> R.home("etc")
[1] "/Library/Frameworks/R.framework/Resources/etc"

What is

Sys.getenv("R_ARCH")

?

That gives:

Sys.getenv(“R_ARCH”)
[1] “/x86_64”

If that could be the problem I think I did something wrong earlier, when I was following the instructions on the github page (after I was already starting to get desperate for it not working):

The environment variable R_ARCH might not be set appropriately for versions earlier than 0.97.168. For version 0.97.168 or later, this step can be skipped (the safe way is to execute Sys.getenv('R_ARCH') and see if the result is not empty). For versions before 0.97.168, it must be set to one of the following values before installing and using Stan :

# for 64-bit RStudio 
> Sys.setenv(R_ARCH = '/x86_64')

Because when I ran Sys.getenv('R_ARCH') it gave me an empty reply, so I was not sure what to do as the sentence states “and see if the result is not empty”, so even though I knew my Rstudio version was newer than 0.97.168 I ran that code (but I only did that because it was already not working). Did I mess it up more with that?

Try

Sys.unsetenv("R_ARCH")

and then back to the beginning with

pkgbuild::has_build_tools(debug = TRUE)

and

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
	return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )

fx( 2L, 5 ) # should be 10

Great! Pkgbuild now seems to work and I am not getting the long error messages anymore (which I also got before I ran the Sys.setenv(R_ARCH = ‘/x86_64’), but then they were different). I still get a warning when creating fx, does that matter?

pkgbuild::has_build_tools(debug = TRUE)
[1] TRUE

fx ← inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’
+
+ return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
+
+ ’ )
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation are out of sync. Falling back to library file for linking.
fx( 2L, 5 ) # should be 10
[1] 10

Sorry for misunderstanding the instructions and making the issue worse, thank you so much for your time and effort to help me!

So, does like

library(rstan)
example(stan_model, run.dontrun = TRUE)

work?