setup.py 5.62 KB
Newer Older
wxchan's avatar
wxchan committed
1
# coding: utf-8
2
# pylint: disable=invalid-name, exec-used, C0111
wxchan's avatar
wxchan committed
3
4
"""Setup lightgbm package."""
from __future__ import absolute_import
5

Guolin Ke's avatar
Guolin Ke committed
6
import distutils
7
import os
Guolin Ke's avatar
Guolin Ke committed
8
import shutil
9
10
11
import struct
import sys

12
from setuptools import find_packages, setup
13
14
15
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.sdist import sdist
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

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')
Guolin Ke's avatar
Guolin Ke committed
43
        if use_gpu:
44
45
            copy_files_helper('compute')
        distutils.file_util.copy_file("../CMakeLists.txt", "./lightgbm/")
46
        distutils.file_util.copy_file("../LICENSE", "./")
wxchan's avatar
wxchan committed
47
48


49
50
51
52
53
54
55
56
57
58
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)


59
60
def compile_cpp(use_mingw=False, use_gpu=False):

Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
    if os.path.exists("build_cpp"):
        shutil.rmtree("build_cpp")
    os.makedirs("build_cpp")
    os.chdir("build_cpp")
65
66
67

    cmake_cmd = "cmake "
    build_cmd = "make _lightgbm"
68
69
    if use_gpu:
        cmake_cmd += " -DUSE_GPU=ON "
70
71
72
    if os.name == "nt":
        if use_mingw:
            cmake_cmd += " -G \"MinGW Makefiles\" "
73
            os.system(cmake_cmd + " ../lightgbm/")
74
75
            build_cmd = "mingw32-make.exe _lightgbm"
        else:
76
77
78
79
80
81
82
83
84
85
86
87
88
            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')

89
            build_cmd = "cmake --build . --target _lightgbm  --config Release"
90
    print("Start to compile library.")
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
    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):
Guolin Ke's avatar
Guolin Ke committed
132
        copy_files(use_gpu=True)
133
        open("./_IS_SOURCE_PACKAGE.txt", 'w').close()
Guolin Ke's avatar
Guolin Ke committed
134
135
136
137
        if os.path.exists("./lightgbm/Release/"):
            shutil.rmtree('./lightgbm/Release/')
        if os.path.isfile('./lightgbm/lib_lightgbm.so'):
            os.remove('./lightgbm/lib_lightgbm.so')
138
139
140
141
142
143
144
145
        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.')
146
147
148
149
150
151
152
153
154

    dir_path = os.path.dirname(os.path.realpath(__file__))
    if os.path.isfile(os.path.join('..', 'VERSION.txt')):
        distutils.file_util.copy_file(
            os.path.join('..', 'VERSION.txt'),
            os.path.join('.', 'lightgbm'))
    if os.path.isfile(os.path.join(dir_path, 'lightgbm', 'VERSION.txt')):
        version = open(os.path.join(dir_path, 'lightgbm', 'VERSION.txt')).read().strip()

155
    sys.path.insert(0, '.')
156

Guolin Ke's avatar
Guolin Ke committed
157
158
159
160
161
162
163
164
165
166
167
    setup(name='lightgbm',
          version=version,
          description='LightGBM Python Package',
          install_requires=[
              'numpy',
              'scipy',
              'scikit-learn'
          ],
          maintainer='Guolin Ke',
          maintainer_email='guolin.ke@microsoft.com',
          zip_safe=False,
168
169
170
171
172
          cmdclass={
              'install': CustomInstall,
              'install_lib': CustomInstallLib,
              'sdist': CustomSdist,
          },
Guolin Ke's avatar
Guolin Ke committed
173
174
          packages=find_packages(),
          include_package_data=True,
175
          data_files=[],
176
          license='The MIT License (Microsoft)',
Guolin Ke's avatar
Guolin Ke committed
177
          url='https://github.com/Microsoft/LightGBM')