Skip to content

Commit

Permalink
[LIBINFO] Enable load .dylib on darwin (dmlc#142)
Browse files Browse the repository at this point in the history
* [LIBINFO] Enable load .dylib on darwin

* Fix makefile
  • Loading branch information
ZihengJiang authored and tqchen committed Aug 25, 2017
1 parent 7cda9d5 commit 2c783cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ include $(NNVM_PLUGINS)
# specify tensor path
.PHONY: clean all test lint doc cython cython3 cyclean

all: lib/libnnvm.a lib/libnnvm_example.so

SRC = $(wildcard src/*.cc src/*/*.cc)
ALL_OBJ = $(patsubst %.cc, build/%.o, $(SRC))
ALL_DEP = $(ALL_OBJ) $(PLUGIN_OBJ)

UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S), Darwin)
SHARED_LIBRARY_SUFFIX := dylib
WHOLE_ARCH= -all_load
NO_WHOLE_ARCH= -noall_load
else
SHARED_LIBRARY_SUFFIX := so
WHOLE_ARCH= --whole-archive
NO_WHOLE_ARCH= --no-whole-archive
endif

all: lib/libnnvm.a lib/libnnvm_example.$(SHARED_LIBRARY_SUFFIX)

SRC = $(wildcard src/*.cc src/*/*.cc)
ALL_OBJ = $(patsubst %.cc, build/%.o, $(SRC))
ALL_DEP = $(ALL_OBJ) $(PLUGIN_OBJ)

include tests/cpp/unittest.mk

Expand All @@ -74,7 +75,7 @@ lib/libnnvm.a: $(ALL_DEP)
@mkdir -p $(@D)
ar crv $@ $(filter %.o, $?)

lib/libnnvm_example.so: example/src/operator.cc lib/libnnvm.a
lib/libnnvm_example.$(SHARED_LIBRARY_SUFFIX): example/src/operator.cc lib/libnnvm.a
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.cc, $^) $(LDFLAGS) -Wl,${WHOLE_ARCH} lib/libnnvm.a -Wl,${NO_WHOLE_ARCH}

Expand All @@ -85,7 +86,7 @@ cython3:
cd python; python3 setup.py build_ext --inplace

cyclean:
rm -rf python/nnvm/*/*.so python/nnvm/*/*.cpp
rm -rf python/nnvm/*/*.so python/nnvm/*/*.dylib python/nnvm/*/*.cpp

lint:
python2 dmlc-core/scripts/lint.py nnvm cpp include src
Expand Down
14 changes: 10 additions & 4 deletions python/nnvm/libinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ def find_lib_path():
api_path = os.path.join(base_path, '../../lib/')
cmake_build_path = os.path.join(base_path, '../../build/Release/')
dll_path = [base_path, api_path, cmake_build_path]
if os.name == 'nt':

if sys.platform.startswith('linux') and os.environ.get('LD_LIBRARY_PATH', None):
dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")])
elif sys.platform.startswith('darwin') and os.environ.get('DYLD_LIBRARY_PATH', None):
dll_path.extend([p.strip() for p in os.environ['DYLD_LIBRARY_PATH'].split(":")])

if sys.platform.startswith('win32'):
vs_configuration = 'Release'
if platform.architecture()[0] == '64bit':
dll_path.append(os.path.join(curr_path, '../../build', vs_configuration))
dll_path.append(os.path.join(curr_path, '../../windows/x64', vs_configuration))
else:
dll_path.append(os.path.join(curr_path, '../../build', vs_configuration))
dll_path.append(os.path.join(curr_path, '../../windows', vs_configuration))
elif os.name == "posix" and os.environ.get('LD_LIBRARY_PATH', None):
dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")])
if os.name == 'nt':
dll_path = [os.path.join(p, '%s.dll' % lib_name) for p in dll_path]
elif sys.platform.startswith('darwin'):
dll_path = [os.path.join(p, '%s.dylib' % lib_name) for p in dll_path]
else:
dll_path = [os.path.join(p, '%s.so' % lib_name) for p in dll_path]

lib_path = [p for p in dll_path if os.path.exists(p) and os.path.isfile(p)]
if len(lib_path) == 0:
raise RuntimeError('Cannot find the files.\n' +
Expand Down

0 comments on commit 2c783cf

Please sign in to comment.