2424# Builds libR
2525#
2626# We build all the source files with the given compiler (CC), which may be the Labs LLVM clang that produces embedded bitcode.
27- # When building the final artifact, we either build only libR.so from the source files in common and NFI specific directories,
28- # and if FASTR_RFFI is "LLVM", we build also libR.sol, which is a regular so file with embedded bitcode, but that one is built
29- # from the source files in common and LLVM specific directories.
27+ #
28+ # In order to support running both backend (NFI and Sulong) in one process, we produce three artifacts here:
29+ # * libR.so - dummy library, which standard name in standard location that most of the packages will link with
30+ # - when the native loader/Sulong is loading a package (e.g., stats.so), it loads this dummy libR.so
31+ # * libRnative.so - library with actual implementation of the R API for NFI
32+ # - dlopen'ed by FastR into the global space (RTLD_GLOBAL)
33+ # * libRllvm.so - library with actual implementation of the R API for Sulong
34+ # - FastR explicitly loads this via Sulong
3035#
3136# This is the only situation, where we produce different artifacts for NFI/LLVM. When building pacakges,
3237# we produce only one so file with embedded bitcode that is supposed to be used for both NFI and LLVM execution
@@ -52,11 +57,12 @@ endif
5257.PHONY : all clean
5358
5459C_LIBNAME := libR$(DYLIB_EXT )
55- LLVM_LIBNAME := libR$(DYLIB_EXT ) l
56- C_LIB := $(FASTR_LIB_DIR ) /$(C_LIBNAME )
60+ NFI_LIBNAME := libRnative$(DYLIB_EXT )
61+ LLVM_LIBNAME := libRllvm$(DYLIB_EXT )
62+
63+ R_LIB := $(FASTR_LIB_DIR ) /$(C_LIBNAME )
64+ NFI_LIB := $(FASTR_LIB_DIR ) /$(NFI_LIBNAME )
5765LLVM_LIB := $(FASTR_LIB_DIR ) /$(LLVM_LIBNAME )
58- R_LIBNAME := libR$(DYLIB_EXT )
59- R_LIB := $(FASTR_LIB_DIR ) /$(R_LIBNAME )
6066
6167ifeq ($(OS_NAME ) , Darwin)
6268VERSION_FLAGS := -current_version $(R_VERSION ) -compatibility_version $(R_VERSION )
@@ -75,34 +81,31 @@ ifeq ($(FASTR_RFFI),managed)
7581 # nop
7682else
7783ifeq ($(OS_NAME ) ,Darwin)
78- $(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -o $(R_LIB) $(wildcard lib/nfi/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -ldl -lRblas -lRlapack -lpcre -lz $(F2C) -liconv $(VERSION_FLAGS)
79- $(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -o $(LLVM_LIB) $(wildcard lib/llvm/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -L$(LLVM_LIBS_DIR) -lpolyglot-mock -ldl -lRblas -lRlapack -lpcre -lz $(F2C) -liconv $(VERSION_FLAGS)
84+ $(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -o $(R_LIB) -L$(FASTR_LIB_DIR) -lRblas -lRlapack $(VERSION_FLAGS)
85+ $(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -Wl,-undefined,dynamic_lookup -o $(NFI_LIB) $(wildcard lib/nfi/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -ldl -lR -lpcre -lz $(F2C) -liconv $(VERSION_FLAGS)
86+ $(DYLIB_LD) $(DYLIB_LDFLAGS) -Wl,-rpath,@loader_path/ -Wl,-undefined,dynamic_lookup -o $(LLVM_LIB) $(wildcard lib/llvm/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -L$(LLVM_LIBS_DIR) -lpolyglot-mock -ldl -lR -lpcre -lz $(F2C) -liconv $(VERSION_FLAGS)
8087
8188 install_name_tool -add_rpath @loader_path/ $(FASTR_LIB_DIR)/libRblas.dylib
8289 install_name_tool -add_rpath @loader_path/ $(FASTR_LIB_DIR)/libRlapack.dylib
83-
84- # The following commands will be changed to use @loader_path (or @rpath) instead of the absolute paths $(LLVM_LIBS_DIR) and $(FASTR_LIB_DIR)
85- # when Sulong supports @loader_path (resp. @rpath) substitution.
90+
8691 install_name_tool -change bin/libpolyglot-mock.dylib $(LLVM_LIBS_DIR)/libpolyglot-mock.dylib $(LLVM_LIB)
87- # install_name_tool -change @rpath/libRblas.dylib $(FASTR_LIB_DIR)/libRblas.dylib $(LLVM_LIB)
88- # install_name_tool -change @rpath/libRlapack.dylib $(FASTR_LIB_DIR)/libRlapack.dylib $(LLVM_LIB)
89- # install_name_tool -change @rpath/libpcre.dylib $(FASTR_LIB_DIR)/libpcre.dylib $(LLVM_LIB)
90- # install_name_tool -change @rpath/libz.dylib $(FASTR_LIB_DIR)/libz.dylib $(LLVM_LIB)
91- # install_name_tool -change @rpath/libf2c.so $(FASTR_LIB_DIR)/libf2c$(DYLIB_EXT) $(LLVM_LIB)
9292
93- install_name_tool -change libRblas.dylib @rpath/libRblas.dylib $(R_LIB)
94- install_name_tool -change libRlapack.dylib @rpath/libRlapack.dylib $(R_LIB)
95- install_name_tool -change libf2c.so @rpath/libf2c$(DYLIB_EXT) $(R_LIB)
93+ install_name_tool -change libRblas.dylib @rpath/libRblas.dylib $(NFI_LIB)
94+ install_name_tool -change libRlapack.dylib @rpath/libRlapack.dylib $(NFI_LIB)
95+ install_name_tool -change libf2c.so @rpath/libf2c$(DYLIB_EXT) $(NFI_LIB)
96+
9697 install_name_tool -id @rpath/libR.dylib $(R_LIB)
97- install_name_tool -id @rpath/libR.dylibl $(LLVM_LIB)
98- # check if we captured libpcre/libz, rpath those in libR
98+ install_name_tool -id @rpath/$(NFI_LIBNAME) $(NFI_LIB)
99+ install_name_tool -id @rpath/$(LLVM_LIBNAME) $(LLVM_LIB)
100+ # check if we captured libpcre/libz, rpath those in libR
99101 python $(FASTR_R_HOME)/mx.fastr/copylib.py updatelib $(FASTR_LIB_DIR) $(FASTR_R_HOME)
100102else
101103 # not Darwin:
102- $(DYLIB_LD) $(DYLIB_LDFLAGS) $(shell echo $(PKG_LDFLAGS_OVERRIDE)) -Wl,-rpath,'$$ORIGIN' -o $(R_LIB) $(wildcard lib/nfi/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -lRblas -lRlapack -ldl -lpcre -lz $(F2C)
104+ $(DYLIB_LD) $(DYLIB_LDFLAGS) $(shell echo $(PKG_LDFLAGS_OVERRIDE)) -Wl,-rpath,'$$ORIGIN' -o $(R_LIB) -L$(FASTR_LIB_DIR) -lRblas -lRlapack
105+ $(DYLIB_LD) $(DYLIB_LDFLAGS) $(shell echo $(PKG_LDFLAGS_OVERRIDE)) -Wl,-rpath,'$$ORIGIN' -o $(NFI_LIB) $(wildcard lib/nfi/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -lRblas -lRlapack -ldl -lpcre -lz $(F2C)
103106 # We do not link with Rlapack and Rblas so that we can load those libraries manually later after loading libR
104107 # Otherwise Sulong would attempt to load Rlapack and Rblas and fail, because there is a dependency cycle:
105- # libR provides xerbla_, but Rlapack/Rblas depend on it, it seems that Sulong is not able to resolve this unlike the native loader
108+ # libR provides xerbla_, but Rlapack/Rblas depend on it, it seems that Sulong is not able to resolve this unlike the native loader
106109 $(DYLIB_LD) $(DYLIB_LDFLAGS) $(shell echo $(PKG_LDFLAGS_OVERRIDE)) -Wl,-rpath,'$$ORIGIN' -o $(LLVM_LIB) $(wildcard lib/llvm/*.o) $(wildcard lib/common/*.o) $(wildcard lib/JavaGD/*.o) -L$(FASTR_LIB_DIR) -ldl -lpcre -lz $(F2C)
107110endif # Darwin
108111endif # managed
@@ -131,13 +134,13 @@ common.done:
131134
132135clean :
133136 $(MAKE ) -C src/common clean
134- rm -rf src/truffle_* /* .o
135- rm -rf $(R_LIB )
136- rm -rf fficall.done
137- rm -rf common.done
138- rm -rf fficallllvm.done
137+ rm -f src/truffle_* /* .o
138+ rm -f $(R_LIB ) $( NFI_LIB ) $( LLVM_LIB )
139+ rm -f fficall.done
140+ rm -f common.done
141+ rm -f fficallllvm.done
139142 $(MAKE ) -C src/JavaGD clean
140- rm -rf JavaGD.done
143+ rm -f JavaGD.done
141144ifneq ($(FASTR_RFFI ) ,managed)
142145 $(MAKE) -C src/truffle_nfi clean
143146 $(MAKE) -C src/truffle_llvm clean
0 commit comments