build-python.sh 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh

# [description]
#
#     Prepare a source distribution (sdist) or built distribution (wheel)
#     of the Python package, and optionally install it.
#
# [usage]
#
#     # build sdist and put it in dist/
#     sh ./build-python.sh sdist
#
#     # build wheel and put it in dist/
#     sh ./build-python.sh bdist_wheel [OPTIONS]
#
#     # compile lib_lightgbm and install the Python package wrapping it
#     sh ./build-python.sh install [OPTIONS]
#
#     # install the Python package using a pre-compiled lib_lightgbm
#     # (assumes lib_lightgbm.{dll,so} is located at the root of the repo)
#     sh ./build-python.sh install --precompile
#
# [options]
#
25
26
#     --boost-dir=FILEPATH
#                                   Directory with Boost package configuration file.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#     --boost-include-dir=FILEPATH
#                                   Directory containing Boost headers.
#     --boost-librarydir=FILEPATH
#                                   Preferred Boost library directory.
#     --boost-root=FILEPATH
#                                   Boost preferred installation prefix.
#     --opencl-include-dir=FILEPATH
#                                   OpenCL include directory.
#     --opencl-library=FILEPATH
#                                   Path to OpenCL library.
#     --bit32
#                                   Compile 32-bit version.
#     --cuda
#                                   Compile CUDA version.
#     --gpu
#                                   Compile GPU version.
#     --integrated-opencl
#                                   Compile integrated OpenCL version.
#     --mingw
#                                   Compile with MinGW.
#     --mpi
#                                   Compile MPI version.
49
50
51
#     --no-isolation
#                                   Assume all build and install dependencies are already installed,
#                                   don't go to the internet to get them.
52
53
54
55
56
57
58
59
60
61
62
#     --nomp
#                                   Compile version without OpenMP support.
#     --precompile
#                                   Use precompiled library.
#                                   Only used with 'install' command.
#     --time-costs
#                                   Output time costs for different internal routines.
#     --user
#                                   Install into user-specific instead of global site-packages directory.
#                                   Only used with 'install' command.

63
set -e -E -u
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

echo "building lightgbm"

# Default values of arguments
INSTALL="false"
BUILD_SDIST="false"
BUILD_WHEEL="false"

PIP_INSTALL_ARGS=""
BUILD_ARGS=""
PRECOMPILE="false"

while [ $# -gt 0 ]; do
  case "$1" in
    ############################
    # sub-commands of setup.py #
    ############################
    install)
      INSTALL="true"
      ;;
    sdist)
      BUILD_SDIST="true"
      ;;
    bdist_wheel)
      BUILD_WHEEL="true"
      ;;
    ############################
    # customized library paths #
    ############################
93
94
95
96
97
    --boost-dir|--boost-dir=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        BOOST_DIR="${1#*=}"
98
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_DIR='${BOOST_DIR}'"
99
        ;;
100
101
102
103
104
    --boost-include-dir|--boost-include-dir=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        BOOST_INCLUDE_DIR="${1#*=}"
105
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_INCLUDE_DIR='${BOOST_INCLUDE_DIR}'"
106
107
108
109
110
111
        ;;
    --boost-librarydir|--boost-librarydir=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        BOOST_LIBRARY_DIR="${1#*=}"
112
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.BOOST_LIBRARYDIR='${BOOST_LIBRARY_DIR}'"
113
114
115
116
117
118
        ;;
    --boost-root|--boost-root=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        BOOST_ROOT="${1#*=}"
119
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_ROOT='${BOOST_ROOT}'"
120
121
122
123
124
125
        ;;
    --opencl-include-dir|--opencl-include-dir=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        OPENCL_INCLUDE_DIR="${1#*=}"
126
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.OpenCL_INCLUDE_DIR='${OPENCL_INCLUDE_DIR}'"
127
128
129
130
131
132
        ;;
    --opencl-library|--opencl-library=*)
        if [[ "$1" != *=* ]];
            then shift;
        fi
        OPENCL_LIBRARY="${1#*=}"
133
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.OpenCL_LIBRARY='${OPENCL_LIBRARY}'"
134
135
136
137
138
        ;;
    #########
    # flags #
    #########
    --bit32)
139
140
141
        export CMAKE_GENERATOR="Visual Studio 17 2022"
        export CMAKE_GENERATOR_PLATFORM="Win32"
        echo "[INFO] Attempting to build 32-bit version of LightGBM, which is only supported on Windows with generator '${CMAKE_GENERATOR}'."
142
143
        ;;
    --cuda)
144
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_CUDA=ON"
145
146
        ;;
    --gpu)
147
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_GPU=ON"
148
149
        ;;
    --integrated-opencl)
150
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.__INTEGRATE_OPENCL=ON"
151
152
        ;;
    --mingw)
153
154
155
        export CMAKE_GENERATOR='MinGW Makefiles'
        # ref: https://stackoverflow.com/a/45104058/3986677
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.CMAKE_SH=CMAKE_SH-NOTFOUND"
156
157
        ;;
    --mpi)
158
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_MPI=ON"
159
        ;;
160
161
162
163
    --no-isolation)
        BUILD_ARGS="${BUILD_ARGS} --no-isolation"
        PIP_INSTALL_ARGS="${PIP_INSTALL_ARGS} --no-build-isolation"
        ;;
164
    --nomp)
165
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_OPENMP=OFF"
166
167
168
169
170
        ;;
    --precompile)
        PRECOMPILE="true"
        ;;
    --time-costs)
171
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_TIMETAG=ON"
172
173
174
175
176
177
        ;;
    --user)
        PIP_INSTALL_ARGS="${PIP_INSTALL_ARGS} --user"
        ;;
    *)
        echo "invalid argument '${1}'"
178
        exit 1
179
180
181
182
183
        ;;
  esac
  shift
done

184
185
pip install --prefer-binary 'build>=0.10.0'

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# create a new directory that just contains the files needed
# to build the Python package
create_isolated_source_dir() {
    rm -rf \
        ./lightgbm-python \
        ./lightgbm \
        ./python-package/build \
        ./python-package/build_cpp \
        ./python-package/compile \
        ./python-package/dist \
        ./python-package/lightgbm.egg-info

    cp -R ./python-package ./lightgbm-python

    cp LICENSE ./lightgbm-python/
    cp VERSION.txt ./lightgbm-python/lightgbm/VERSION.txt

203
204
205
206
207
208
    cp -R ./cmake ./lightgbm-python
    cp CMakeLists.txt ./lightgbm-python
    cp -R ./include ./lightgbm-python
    cp -R ./src ./lightgbm-python
    cp -R ./swig ./lightgbm-python
    cp -R ./windows ./lightgbm-python
209
210
211
212
213
214
215
216

    # include only specific files from external_libs, to keep the package
    # small and avoid redistributing code with licenses incompatible with
    # LightGBM's license

    ######################
    # fast_double_parser #
    ######################
217
    mkdir -p ./lightgbm-python/external_libs/fast_double_parser
218
219
    cp \
        external_libs/fast_double_parser/CMakeLists.txt \
220
        ./lightgbm-python/external_libs/fast_double_parser/CMakeLists.txt
221
222
    cp \
        external_libs/fast_double_parser/LICENSE* \
223
        ./lightgbm-python/external_libs/fast_double_parser/
224

225
    mkdir -p ./lightgbm-python/external_libs/fast_double_parser/include/
226
227
    cp \
        external_libs/fast_double_parser/include/fast_double_parser.h \
228
        ./lightgbm-python/external_libs/fast_double_parser/include/
229
230
231
232

    #######
    # fmt #
    #######
233
    mkdir -p ./lightgbm-python/external_libs/fmt
234
235
    cp \
        external_libs/fast_double_parser/CMakeLists.txt \
236
        ./lightgbm-python/external_libs/fmt/CMakeLists.txt
237
238
    cp \
        external_libs/fmt/LICENSE* \
239
        ./lightgbm-python/external_libs/fmt/
240

241
    mkdir -p ./lightgbm-python/external_libs/fmt/include/fmt
242
243
    cp \
        external_libs/fmt/include/fmt/*.h \
244
        ./lightgbm-python/external_libs/fmt/include/fmt/
245
246
247
248

    #########
    # Eigen #
    #########
249
    mkdir -p ./lightgbm-python/external_libs/eigen/Eigen
250
251
    cp \
        external_libs/eigen/CMakeLists.txt \
252
        ./lightgbm-python/external_libs/eigen/CMakeLists.txt
253
254
255
256
257

    modules="Cholesky Core Dense Eigenvalues Geometry Householder Jacobi LU QR SVD"
    for eigen_module in ${modules}; do
        cp \
            external_libs/eigen/Eigen/${eigen_module} \
258
            ./lightgbm-python/external_libs/eigen/Eigen/${eigen_module}
259
        if [ ${eigen_module} != "Dense" ]; then
260
            mkdir -p ./lightgbm-python/external_libs/eigen/Eigen/src/${eigen_module}/
261
262
263
            cp \
                -R \
                external_libs/eigen/Eigen/src/${eigen_module}/* \
264
                ./lightgbm-python/external_libs/eigen/Eigen/src/${eigen_module}/
265
266
267
        fi
    done

268
    mkdir -p ./lightgbm-python/external_libs/eigen/Eigen/misc
269
270
271
    cp \
        -R \
        external_libs/eigen/Eigen/src/misc \
272
        ./lightgbm-python/external_libs/eigen/Eigen/src/misc/
273

274
    mkdir -p ./lightgbm-python/external_libs/eigen/Eigen/plugins
275
276
277
    cp \
        -R \
        external_libs/eigen/Eigen/src/plugins \
278
        ./lightgbm-python/external_libs/eigen/Eigen/src/plugins/
279
280
281
282

    ###################
    # compute (Boost) #
    ###################
283
    mkdir -p ./lightgbm-python/external_libs/compute
284
285
286
    cp \
        -R \
        external_libs/compute/include \
287
        ./lightgbm-python/external_libs/compute/include/
288
289
290
291
292
293
294
295
296
}

create_isolated_source_dir

cd ./lightgbm-python

# installation involves building the wheel + `pip install`-ing it
if test "${INSTALL}" = true; then
    if test "${PRECOMPILE}" = true; then
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        BUILD_SDIST=true
        BUILD_WHEEL=false
        BUILD_ARGS=""
        rm -rf \
            ./cmake \
            ./CMakeLists.txt \
            ./external_libs \
            ./include \
            ./src \
            ./swig \
            ./windows
        # use regular-old setuptools for these builds, to avoid
        # trying to recompile the shared library
        sed -i.bak -e '/start:build-system/,/end:build-system/d' pyproject.toml
        echo '[build-system]' >> ./pyproject.toml
        echo 'requires = ["setuptools"]' >> ./pyproject.toml
        echo 'build-backend = "setuptools.build_meta"' >> ./pyproject.toml
        echo "" >> ./pyproject.toml
315
        echo "recursive-include lightgbm *.dll *.dylib *.so" > ./MANIFEST.in
316
317
318
319
320
        echo "" >> ./MANIFEST.in
        mkdir -p ./lightgbm/lib
        if test -f ../lib_lightgbm.so; then
            echo "found pre-compiled lib_lightgbm.so"
            cp ../lib_lightgbm.so ./lightgbm/lib/lib_lightgbm.so
321
322
323
        elif test -f ../lib_lightgbm.dylib; then
            echo "found pre-compiled lib_lightgbm.dylib"
            cp ../lib_lightgbm.dylib ./lightgbm/lib/lib_lightgbm.dylib
324
325
326
327
328
329
330
331
332
        elif test -f ../Release/lib_lightgbm.dll; then
            echo "found pre-compiled Release/lib_lightgbm.dll"
            cp ../Release/lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
        elif test -f ../windows/x64/DLL/lib_lightgbm.dll; then
            echo "found pre-compiled windows/x64/DLL/lib_lightgbm.dll"
            cp ../windows/x64/DLL/lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
            cp ../windows/x64/DLL/lib_lightgbm.lib ./lightgbm/lib/lib_lightgbm.lib
        fi
        rm -f ./*.bak
333
334
335
336
337
338
339
340
341
    else
        BUILD_SDIST="false"
        BUILD_WHEEL="true"
    fi
fi

if test "${BUILD_SDIST}" = true; then
    echo "--- building sdist ---"
    rm -f ../dist/*.tar.gz
342
343
344
    python -m build \
        --sdist \
        --outdir ../dist \
345
        ${BUILD_ARGS} \
346
        .
347
348
349
fi

if test "${BUILD_WHEEL}" = true; then
350
    echo "--- building wheel ---"
351
    rm -f ../dist/*.whl || true
352
353
354
355
356
    python -m build \
        --wheel \
        --outdir ../dist \
        ${BUILD_ARGS} \
        .
357
358
359
360
361
fi

if test "${INSTALL}" = true; then
    echo "--- installing lightgbm ---"
    cd ../dist
362
    # ref for use of '--find-links': https://stackoverflow.com/a/52481267/3986677
363
364
    pip install \
        ${PIP_INSTALL_ARGS} \
365
        --ignore-installed \
366
        --no-cache-dir \
367
        --no-deps \
368
369
370
371
372
373
374
        --find-links=. \
        lightgbm
    cd ../
fi

echo "cleaning up"
rm -rf ./lightgbm-python