New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[python] [setup] improving installation #880
Changes from 10 commits
a5cfbda
5285ba4
7ed297e
7d3d3f1
f527e75
0dd47b9
cf0355f
56e98ae
72d57a2
c426442
d98b822
05dbbbe
478468b
fa07c5e
1631fa1
0668aa7
4031d3b
d6fdb92
f4e149f
c1814e8
541b2e5
2c04458
ef28139
17f6400
cc8c212
9d7fa73
ab8f81c
a4d90f4
710d8d7
8364407
749761e
30e83a4
27d19a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ | |
from __future__ import absolute_import | ||
|
||
import distutils | ||
import logging | ||
import os | ||
import shutil | ||
import struct | ||
import subprocess | ||
import sys | ||
|
||
from setuptools import find_packages, setup | ||
|
@@ -22,7 +24,7 @@ def find_lib(): | |
exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath) | ||
|
||
LIB_PATH = [os.path.relpath(path, CURRENT_DIR) for path in libpath['find_lib_path']()] | ||
print("Install lib_lightgbm from: %s" % LIB_PATH) | ||
logging.info("Installing lib_lightgbm from: %s" % LIB_PATH) | ||
return LIB_PATH | ||
|
||
|
||
|
@@ -40,20 +42,31 @@ def copy_files_helper(folder_name): | |
if not os.path.isfile('./_IS_SOURCE_PACKAGE.txt'): | ||
copy_files_helper('include') | ||
copy_files_helper('src') | ||
copy_files_helper('windows') | ||
if use_gpu: | ||
copy_files_helper('compute') | ||
distutils.file_util.copy_file("../CMakeLists.txt", "./lightgbm/") | ||
distutils.file_util.copy_file("../LICENSE", "./") | ||
|
||
|
||
def clear_path(path): | ||
contents = os.listdir(path) | ||
for file in contents: | ||
file_path = os.path.join(path, file) | ||
if os.path.isfile(file_path): | ||
os.remove(file_path) | ||
else: | ||
shutil.rmtree(file_path) | ||
if os.path.isdir(path): | ||
contents = os.listdir(path) | ||
for file in contents: | ||
file_path = os.path.join(path, file) | ||
if os.path.isfile(file_path): | ||
os.remove(file_path) | ||
else: | ||
shutil.rmtree(file_path) | ||
|
||
|
||
def silent_call(cmd): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not so sure about what's this function for, why makes it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @StrikerRUS ok, got it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this function be designed like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wxchan Do you mean that I should change this code
to
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I think it's better, and you can move raise Exception into function to avoid duplicates, but it's trivial, up to you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wxchan Moving raising Exception into the function will cause dozens of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't sth like below work? why need try/except in main code? not sure, LGTM now, it's up to you.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wxchan It seems you are right! |
||
try: | ||
with open(os.devnull, "w") as shut_up: | ||
subprocess.check_output(cmd, stderr=shut_up) | ||
return 0 | ||
except Exception: | ||
return 1 | ||
|
||
|
||
def compile_cpp(use_mingw=False, use_gpu=False): | ||
|
@@ -63,33 +76,56 @@ def compile_cpp(use_mingw=False, use_gpu=False): | |
os.makedirs("build_cpp") | ||
os.chdir("build_cpp") | ||
|
||
cmake_cmd = "cmake " | ||
build_cmd = "make _lightgbm" | ||
logger.info("Starting to compile the library.") | ||
|
||
cmake_cmd = ["cmake", "../lightgbm/"] | ||
if use_gpu: | ||
cmake_cmd += " -DUSE_GPU=ON " | ||
cmake_cmd.append("-DUSE_GPU=ON") | ||
if os.name == "nt": | ||
if use_mingw: | ||
cmake_cmd += " -G \"MinGW Makefiles\" " | ||
os.system(cmake_cmd + " ../lightgbm/") | ||
build_cmd = "mingw32-make.exe _lightgbm" | ||
logger.info("Starting to compile with CMake and MinGW.") | ||
status = silent_call(cmake_cmd + ["-G", "MinGW Makefiles"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wxchan Will There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ignore this. |
||
status += silent_call(["mingw32-make.exe", "_lightgbm"]) | ||
if status != 0: | ||
raise Exception('Please install CMake and MinGW first') | ||
else: | ||
vs_versions = ["Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64", "Visual Studio 12 2013 Win64"] | ||
try_vs = 1 | ||
for vs in vs_versions: | ||
tmp_cmake_cmd = "%s -G \"%s\"" % (cmake_cmd, vs) | ||
try_vs = os.system(tmp_cmake_cmd + " ../lightgbm/") | ||
if try_vs == 0: | ||
cmake_cmd = tmp_cmake_cmd | ||
break | ||
else: | ||
clear_path("./") | ||
if try_vs != 0: | ||
raise Exception('Please install Visual Studio or MS Build first') | ||
|
||
build_cmd = "cmake --build . --target _lightgbm --config Release" | ||
print("Start to compile library.") | ||
os.system(cmake_cmd + " ../lightgbm/") | ||
os.system(build_cmd) | ||
status = 1 | ||
lib_path = "../lightgbm/windows/x64/DLL/lib_lightgbm.dll" | ||
if not use_gpu: | ||
logger.info("Starting to compile with MSBuild from existing solution file.") | ||
platform_toolsets = ("v141", "v140", "v120") | ||
for pt in platform_toolsets: | ||
status = silent_call(["MSBuild", "../lightgbm/windows/LightGBM.sln", | ||
"/p:Configuration=DLL", | ||
"/p:Platform=x64", | ||
"/p:PlatformToolset={0}".format(pt)]) | ||
if status == 0 and os.path.exists(lib_path): | ||
break | ||
else: | ||
clear_path("../lightgbm/windows/x64") | ||
if status != 0 or not os.path.exists(lib_path): | ||
logger.warning("Compilation with MSBuild from existing solution file failed.") | ||
if status != 0 or not os.path.exists(lib_path): | ||
vs_versions = ("Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64", "Visual Studio 12 2013 Win64") | ||
for vs in vs_versions: | ||
logger.info("Starting to compile with %s." % vs) | ||
status = silent_call(cmake_cmd + ["-G", vs]) | ||
if status == 0: | ||
break | ||
else: | ||
clear_path("./") | ||
if status != 0: | ||
raise Exception('Please install Visual Studio or MS Build first') | ||
|
||
status = silent_call(["cmake", "--build", ".", "--target", "_lightgbm", "--config", "Release"]) | ||
if status != 0: | ||
raise Exception('Please install CMake first') | ||
else: # Linux, Darwin (OS X), etc. | ||
logger.info("Starting to compile with CMake.") | ||
status = silent_call(cmake_cmd) | ||
status += silent_call(["make", "_lightgbm"]) | ||
if status != 0: | ||
raise Exception('Please install CMake first') | ||
os.chdir("..") | ||
|
||
|
||
|
@@ -154,6 +190,9 @@ def run(self): | |
|
||
sys.path.insert(0, '.') | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger('LightGBM') | ||
|
||
setup(name='lightgbm', | ||
version=version, | ||
description='LightGBM Python Package', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
look like
file
is a built-in, can you help rename it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wxchan Sure.