-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathsetup.py
151 lines (124 loc) · 4.66 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# coding: utf-8
# pylint: disable=invalid-name, exec-used, C0111
"""Setup lightgbm package."""
from __future__ import absolute_import
import distutils
import os
import shutil
import struct
import sys
from setuptools import find_packages, setup
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.sdist import sdist
def find_lib():
CURRENT_DIR = os.path.dirname(__file__)
libpath_py = os.path.join(CURRENT_DIR, 'lightgbm/libpath.py')
libpath = {'__file__': libpath_py}
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)
return LIB_PATH
def copy_files(use_gpu=False):
def copy_files_helper(folder_name):
src = os.path.join('..', folder_name)
if os.path.exists(src):
dst = os.path.join('./lightgbm', folder_name)
shutil.rmtree(dst, ignore_errors=True)
distutils.dir_util.copy_tree(src, dst)
else:
raise Exception('Cannot copy {} folder'.format(src))
if not os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
copy_files_helper('include')
copy_files_helper('src')
if use_gpu:
copy_files_helper('compute')
distutils.file_util.copy_file("../CMakeLists.txt", "./lightgbm/")
def compile_cpp(use_mingw=False, use_gpu=False):
if not os.path.exists("build"):
os.makedirs("build")
os.chdir("build")
cmake_cmd = "cmake "
build_cmd = "make _lightgbm"
if os.name == "nt":
if use_mingw:
cmake_cmd += " -G \"MinGW Makefiles\" "
build_cmd = "mingw32-make.exe _lightgbm"
else:
cmake_cmd += " -DCMAKE_GENERATOR_PLATFORM=x64 "
build_cmd = "cmake --build . --target _lightgbm --config Release"
if use_gpu:
cmake_cmd += " -DUSE_GPU=ON "
print("Start to compile libarary.")
os.system(cmake_cmd + " ../lightgbm/")
os.system(build_cmd)
os.chdir("..")
class CustomInstallLib(install_lib):
def install(self):
outfiles = install_lib.install(self)
src = find_lib()[0]
dst = os.path.join(self.install_dir, 'lightgbm')
dst, _ = self.copy_file(src, dst)
outfiles.append(dst)
return outfiles
class CustomInstall(install):
user_options = install.user_options + [
('mingw', 'm', 'compile with mingw'),
('gpu', 'g', 'compile gpu version'),
('precompile', 'p', 'use precompile library')
]
def initialize_options(self):
install.initialize_options(self)
self.mingw = 0
self.gpu = 0
self.precompile = 0
def run(self):
if not self.precompile:
copy_files(use_gpu=self.gpu)
compile_cpp(use_mingw=self.mingw, use_gpu=self.gpu)
self.distribution.data_files = [('lightgbm', find_lib())]
install.run(self)
class CustomSdist(sdist):
def run(self):
copy_files(use_gpu=True)
open("./_IS_SOURCE_PACKAGE.txt", 'w').close()
if os.path.exists("./lightgbm/Release/"):
shutil.rmtree('./lightgbm/Release/')
if os.path.isfile('./lightgbm/lib_lightgbm.so'):
os.remove('./lightgbm/lib_lightgbm.so')
sdist.run(self)
if os.path.isfile('./_IS_SOURCE_PACKAGE.txt'):
os.remove('./_IS_SOURCE_PACKAGE.txt')
if __name__ == "__main__":
if (8 * struct.calcsize("P")) != 64:
raise Exception('Cannot install LightGBM in 32-bit python, please use 64-bit python instead.')
if os.path.isfile('../VERSION.txt'):
distutils.file_util.copy_file("../VERSION.txt", "./lightgbm/")
version = '2.0.3'
if os.path.isfile('./lightgbm/VERSION.txt'):
with open('./lightgbm/VERSION.txt') as file_version:
version = file_version.readline().strip()
sys.path.insert(0, '.')
data_files = []
setup(name='lightgbm',
version=version,
description='LightGBM Python Package',
install_requires=[
'wheel',
'numpy',
'scipy',
'scikit-learn'
],
maintainer='Guolin Ke',
maintainer_email='[email protected]',
zip_safe=False,
cmdclass={
'install': CustomInstall,
'install_lib': CustomInstallLib,
'sdist': CustomSdist,
},
packages=find_packages(),
include_package_data=True,
data_files=data_files,
license='The MIT License (Microsoft)',
url='https://github.com/Microsoft/LightGBM')