rxcodemacosclang

compile R packages on macos


I've been using Posit's package manager for binary installs of package, and that works for most things. However, installing packages from source that require clang-compiling fail.

I think it's related to the using SDK: '' empty part ...

For example,

> install.packages("jsonlite")
Installing package into ‘/Users/r2/Library/R/4.4’
(as ‘lib’ is unspecified)
trying URL 'https://packagemanager.posit.co/cran/latest/src/contrib/jsonlite_1.9.1.tar.gz'
Content type 'binary/octet-stream' length 1051291 bytes (1.0 MB)
==================================================
downloaded 1.0 MB

* installing *source* package ‘jsonlite’ ...
** package ‘jsonlite’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.6)’
using SDK: ‘’
clang -I"/opt/homebrew/Cellar/r/4.4.3_1/lib/R/include" -DNDEBUG -Iyajl/api  -I/opt/homebrew/opt/gettext/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/xz/include -I/opt/homebrew/include     -fPIC  -g -O2  -c base64.c -o base64.o
clang -I"/opt/homebrew/Cellar/r/4.4.3_1/lib/R/include" -DNDEBUG -Iyajl/api  -I/opt/homebrew/opt/gettext/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/xz/include -I/opt/homebrew/include     -fPIC  -g -O2  -c collapse_array.c -o collapse_array.o
...
clang -I"/opt/homebrew/Cellar/r/4.4.3_1/lib/R/include" -DNDEBUG -Iyajl/api  -I/opt/homebrew/opt/gettext/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/xz/include -I/opt/homebrew/include     -fPIC  -g -O2  -c yajl/yajl_parser.c -o yajl/yajl_parser.o
clang -I"/opt/homebrew/Cellar/r/4.4.3_1/lib/R/include" -DNDEBUG -Iyajl/api  -I/opt/homebrew/opt/gettext/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/xz/include -I/opt/homebrew/include     -fPIC  -g -O2  -c yajl/yajl_tree.c -o yajl/yajl_tree.o
ar rcs yajl/libstatyajl.a yajl/yajl.o yajl/yajl_alloc.o yajl/yajl_buf.o yajl/yajl_encode.o yajl/yajl_gen.o yajl/yajl_lex.o yajl/yajl_parser.o yajl/yajl_tree.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L/opt/homebrew/Cellar/r/4.4.3_1/lib/R/lib -L/opt/homebrew/opt/gettext/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/xz/lib -L/opt/homebrew/lib -o jsonlite.so base64.o collapse_array.o collapse_object.o collapse_pretty.o escape_chars.o integer64_to_na.o is_datelist.o is_recordlist.o is_scalarlist.o modp_numtoa.o null_to_na.o num_to_char.o parse.o prettify.o push_parser.o r-base64.o register.o row_collapse.o transpose_list.o validate.o -Lyajl -lstatyajl -L/opt/homebrew/Cellar/r/4.4.3_1/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
ld: archive member '/' not a mach-o file in '/private/var/folders/3f/0hlmq7rx34ddjqtlc1lqbxl80000gn/T/RtmpuoriDi/R.INSTALLae611eb747f2/jsonlite/src/yajl/libstatyajl.a'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/opt/homebrew/Cellar/r/4.4.3_1/lib/R/share/make/shlib.mk:10: jsonlite.so] Error 1
ERROR: compilation failed for package ‘jsonlite’

That error is in R that was installed via homebrew, but it's also an issue with "R.app". Nearly identical output, but using the /Library/Framework/... paths for R include headers and such:

> install.packages("jsonlite", type='source')
Installing package into ‘/Users/r2/Library/R/4.4’
(as ‘lib’ is unspecified)
trying URL 'https://packagemanager.posit.co/cran/latest/src/contrib/jsonlite_1.9.1.tar.gz'
Content type 'binary/octet-stream' length 1051291 bytes (1.0 MB)
==================================================
downloaded 1.0 MB

* installing *source* package ‘jsonlite’ ...
** package ‘jsonlite’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.6)’
using SDK: ‘’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Iyajl/api  -I/opt/R/arm64/include     -fPIC  -falign-functions=64 -Wall -g -O2  -c base64.c -o base64.o
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Iyajl/api  -I/opt/R/arm64/include     -fPIC  -falign-functions=64 -Wall -g -O2  -c collapse_array.c -o collapse_array.o
...
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Iyajl/api  -I/opt/R/arm64/include     -fPIC  -falign-functions=64 -Wall -g -O2  -c yajl/yajl_parser.c -o yajl/yajl_parser.o
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -Iyajl/api  -I/opt/R/arm64/include     -fPIC  -falign-functions=64 -Wall -g -O2  -c yajl/yajl_tree.c -o yajl/yajl_tree.o
ar rcs yajl/libstatyajl.a yajl/yajl.o yajl/yajl_alloc.o yajl/yajl_buf.o yajl/yajl_encode.o yajl/yajl_gen.o yajl/yajl_lex.o yajl/yajl_parser.o yajl/yajl_tree.o
clang -arch arm64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o jsonlite.so base64.o collapse_array.o collapse_object.o collapse_pretty.o escape_chars.o integer64_to_na.o is_datelist.o is_recordlist.o is_scalarlist.o modp_numtoa.o null_to_na.o num_to_char.o parse.o prettify.o push_parser.o r-base64.o register.o row_collapse.o transpose_list.o validate.o -Lyajl -lstatyajl -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: archive member '/' not a mach-o file in '/private/var/folders/3f/0hlmq7rx34ddjqtlc1lqbxl80000gn/T/RtmpVNvRR6/R.INSTALLb2f9120e3fb/jsonlite/src/yajl/libstatyajl.a'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Library/Frameworks/R.framework/Resources/share/make/shlib.mk:10: jsonlite.so] Error 1
ERROR: compilation failed for package ‘jsonlite’

Some basic info:

> R.version
               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          4.2                         
year           2024                        
month          10                          
day            31                          
svn rev        87279                       
language       R                           
version.string R version 4.4.2 (2024-10-31)
nickname       Pile of Leaves              

Sys.which(c("gcc", "clang", "make"))
#                                          gcc 
#                               "/usr/bin/gcc" 
#                                        clang 
#                             "/usr/bin/clang" 
#                                         make 
# "/opt/homebrew/opt/make/libexec/gnubin/make"

Possibly relevant information:

Edit 1:

XCode details:

$ xcode-select -p
/Applications/Xcode.app/Contents/Developer
$ xcrun --sdk macosx --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk

Edit 2:

I am not attempting to compile R for MacOS, but following https://mac.r-project.org/tools/ prompts me to install gfortran*.pkg. After doing that successfully (which includes the need for xattr -cr ~/Downloads/gfortran*.pkg to be able to install it).

Additionally, I tried adding the following to my ~/.Renviron (restart R, check Sys.getenv("SDKROOT") is correct):

SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk

This results in the source compile reporting using SDK: ‘MacOSX.sdk’ (which is a change) but it still failed with the same error.

Lastly, I uninstalled brew's make, so now Sys.which("make") reports "/usr/bin/make".


Solution

  • Posting my comment as an answer, glad that solved it:

    Check which ar you are using. It should be /usr/bin/ar

    Its a universal binary

    % file /usr/bin/ar
    /usr/bin/ar: Mach-O universal binary with 2 architectures: ...
    

    The homebrew one might fail.