NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.51k stars 13.69k forks source link

R cannot find 'cstdlib' header on Darwin #34615

Closed mnacamura closed 6 years ago

mnacamura commented 6 years ago

Issue description

On Darwin, clang executed inside R cannot find a C++ header 'cstdlib'. This problem can be solved by installing libcxx and putting

LDFLAGS += -L${HOME}/.nix-profile/lib
CPPFLAGS += -I${HOME}/.nix-profile/include/c++/v1

in ~/.R/Makevars. However, it would be better if R does find 'cstdlib' without this hack.

Steps to reproduce

  1. Install R with rstan, which uses 'StanHeaders' including cstdlib:

    rWrapper.override { packages = [ rPackages.rstan ]; }                   
  2. Compile a Stan model, for example, this example.stan:

    
    data {
    int<lower=0> N;
    real X[N];
    }

parameters { real Mu; real Sigma; }

model { Mu ~ cauchy(0, 5); Sigma ~ cauchy(0, 5); X ~ normal(Mu, Sigma); }

Then, 'cstdlib' is missing:
```R
> library(rstan); stan_model("example.stan")
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.16.2, packaged: 2017-07-03 09:24:58 UTC, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
In file included from file164ca24ec5966.cpp:8:
In file included from /nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/src/stan/model/model_header.hpp:4:
In file included from /nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/stan/math.hpp:4:
In file included from /nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/stan/math/rev/mat.hpp:4:
In file included from /nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/stan/math/rev/core.hpp:4:
In file included from /nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/stan/math/rev/core/autodiffstackstorage.hpp:4:
/nix/store/vmlv8fccwzykmrqahv8qm8qzy752a4zx-r-StanHeaders-2.16.0-1/library/StanHeaders/include/stan/math/memory/stack_alloc.hpp:8:10: fatal error: 'cstdlib' file not found
#include <cstdlib>
         ^~~~~~~~~
1 error generated.

Technical details

mnacamura commented 6 years ago

This test passed:

#! /usr/bin/env nix-shell --pure

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "r-darwin-libcxx-test";

  buildInputs = [ (rWrapper.override { packages = [rPackages.rstan]; }) ];

  shellHook = ''
    cat > example.stan << CODE
    data {
        int<lower=0> N;
        real X[N];
    }

    parameters {
        real Mu;
        real<lower=0> Sigma;
    }

    model {
        Mu ~ cauchy(0, 5);
        Sigma ~ cauchy(0, 5);
        X ~ normal(Mu, Sigma);
    }
    CODE
    Rscript -e 'library(rstan); stan_model("example.stan")'
    exit 0
  '';
}

Inside nix-shell, no problem. Outside nix-shell, clang called by R cannot find lbcxx headers. I guess the point is that inside nix-shell, NIX_LDFLAGS and NIX_CFLAGS_COMPILE include paths to libcxx but outside nix-shell, these environmental variables are missing.

LnL7 commented 6 years ago

Ah! That explains the issue, linux would probably have the same problem but with stdc++.

mnacamura commented 6 years ago

It seems a more general issue (#6390, https://nixos.org/nixpkgs/manual/#builds-on-darwin-fail-with-math.h-not-found).

mnacamura commented 6 years ago

I confirmed that it is okay on Linux. Close this issue for the moment.