build-python.sh 13.3 KB
Newer Older
1
2
3
4
5
#!/bin/sh

# [description]
#
#     Prepare a source distribution (sdist) or built distribution (wheel)
6
#     of the Python-package, and optionally install it.
7
8
9
10
11
12
13
14
15
#
# [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]
#
16
#     # compile lib_lightgbm and install the Python-package wrapping it
17
18
#     sh ./build-python.sh install [OPTIONS]
#
19
#     # install the Python-package using a pre-compiled lib_lightgbm
20
21
22
23
24
#     # (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
#     --nomp
#                                   Compile version without OpenMP support.
#     --precompile
#                                   Use precompiled library.
#                                   Only used with 'install' command.
#     --time-costs
58
#                                   Compile version that outputs time costs for different internal routines.
59
60
61
62
#     --user
#                                   Install into user-specific instead of global site-packages directory.
#                                   Only used with 'install' command.

63
set -e -u
64

65
echo "[INFO] building lightgbm"
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

# 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
    --boost-dir|--boost-dir=*)
94
        if echo "$1" | grep -q '^*=*$';
95
96
97
            then shift;
        fi
        BOOST_DIR="${1#*=}"
98
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_DIR='${BOOST_DIR}'"
99
        ;;
100
    --boost-include-dir|--boost-include-dir=*)
101
        if echo "$1" | grep -q '^*=*$';
102
103
104
            then shift;
        fi
        BOOST_INCLUDE_DIR="${1#*=}"
105
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_INCLUDE_DIR='${BOOST_INCLUDE_DIR}'"
106
107
        ;;
    --boost-librarydir|--boost-librarydir=*)
108
        if echo "$1" | grep -q '^*=*$';
109
110
111
            then shift;
        fi
        BOOST_LIBRARY_DIR="${1#*=}"
112
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.BOOST_LIBRARYDIR='${BOOST_LIBRARY_DIR}'"
113
114
        ;;
    --boost-root|--boost-root=*)
115
        if echo "$1" | grep -q '^*=*$';
116
117
118
            then shift;
        fi
        BOOST_ROOT="${1#*=}"
119
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.Boost_ROOT='${BOOST_ROOT}'"
120
121
        ;;
    --opencl-include-dir|--opencl-include-dir=*)
122
        if echo "$1" | grep -q '^*=*$';
123
124
125
            then shift;
        fi
        OPENCL_INCLUDE_DIR="${1#*=}"
126
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.OpenCL_INCLUDE_DIR='${OPENCL_INCLUDE_DIR}'"
127
128
        ;;
    --opencl-library|--opencl-library=*)
129
        if echo "$1" | grep -q '^*=*$';
130
131
132
            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
        echo "[INFO] Attempting to build 32-bit version of LightGBM, which is only supported on Windows with Visual Studio."
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.args=-AWin32"
141
142
        ;;
    --cuda)
143
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_CUDA=ON"
144
145
        ;;
    --gpu)
146
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_GPU=ON"
147
148
        ;;
    --integrated-opencl)
149
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.__INTEGRATE_OPENCL=ON"
150
151
        ;;
    --mingw)
152
153
        # ref: https://stackoverflow.com/a/45104058/3986677
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.CMAKE_SH=CMAKE_SH-NOTFOUND"
154
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.args=-G'MinGW Makefiles'"
155
156
        ;;
    --mpi)
157
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_MPI=ON"
158
        ;;
159
160
161
162
    --no-isolation)
        BUILD_ARGS="${BUILD_ARGS} --no-isolation"
        PIP_INSTALL_ARGS="${PIP_INSTALL_ARGS} --no-build-isolation"
        ;;
163
    --nomp)
164
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_OPENMP=OFF"
165
166
167
168
169
        ;;
    --precompile)
        PRECOMPILE="true"
        ;;
    --time-costs)
170
        BUILD_ARGS="${BUILD_ARGS} --config-setting=cmake.define.USE_TIMETAG=ON"
171
172
173
174
175
        ;;
    --user)
        PIP_INSTALL_ARGS="${PIP_INSTALL_ARGS} --user"
        ;;
    *)
176
        echo "[ERROR] invalid argument '${1}'. Aborting"
177
        exit 1
178
179
180
181
182
        ;;
  esac
  shift
done

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

185
# create a new directory that just contains the files needed
186
# to build the Python-package
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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

202
203
204
205
206
    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
207
208
209
210
211
212
213
214

    # 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 #
    ######################
215
    mkdir -p ./lightgbm-python/external_libs/fast_double_parser
216
217
    cp \
        external_libs/fast_double_parser/CMakeLists.txt \
218
        ./lightgbm-python/external_libs/fast_double_parser/CMakeLists.txt
219
220
    cp \
        external_libs/fast_double_parser/LICENSE* \
221
        ./lightgbm-python/external_libs/fast_double_parser/
222

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

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

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

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

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

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

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

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

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
295
296
297
298
299
300
301
302
303
        BUILD_SDIST=true
        BUILD_WHEEL=false
        BUILD_ARGS=""
        rm -rf \
            ./cmake \
            ./CMakeLists.txt \
            ./external_libs \
            ./include \
            ./src \
304
            ./swig
305
306
307
        # 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
308
        # shellcheck disable=SC2129
309
310
311
312
        echo '[build-system]' >> ./pyproject.toml
        echo 'requires = ["setuptools"]' >> ./pyproject.toml
        echo 'build-backend = "setuptools.build_meta"' >> ./pyproject.toml
        echo "" >> ./pyproject.toml
313
        echo "recursive-include lightgbm *.dll *.dylib *.so" > ./MANIFEST.in
314
315
316
        echo "" >> ./MANIFEST.in
        mkdir -p ./lightgbm/lib
        if test -f ../lib_lightgbm.so; then
317
            echo "[INFO] found pre-compiled lib_lightgbm.so"
318
            cp ../lib_lightgbm.so ./lightgbm/lib/lib_lightgbm.so
319
        elif test -f ../lib_lightgbm.dylib; then
320
            echo "[INFO] found pre-compiled lib_lightgbm.dylib"
321
            cp ../lib_lightgbm.dylib ./lightgbm/lib/lib_lightgbm.dylib
322
323
324
        elif test -f ../lib_lightgbm.dll; then
            echo "[INFO] found pre-compiled lib_lightgbm.dll"
            cp ../lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
325
        elif test -f ../Release/lib_lightgbm.dll; then
326
            echo "[INFO] found pre-compiled Release/lib_lightgbm.dll"
327
328
            cp ../Release/lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
        elif test -f ../windows/x64/DLL/lib_lightgbm.dll; then
329
            echo "[INFO] found pre-compiled windows/x64/DLL/lib_lightgbm.dll"
330
331
            cp ../windows/x64/DLL/lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
            cp ../windows/x64/DLL/lib_lightgbm.lib ./lightgbm/lib/lib_lightgbm.lib
332
333
334
335
336
337
338
        elif test -f ../windows/x64/Debug_DLL/lib_lightgbm.dll; then
            echo "[INFO] found pre-compiled windows/x64/Debug_DLL/lib_lightgbm.dll"
            cp ../windows/x64/Debug_DLL/lib_lightgbm.dll ./lightgbm/lib/lib_lightgbm.dll
            cp ../windows/x64/Debug_DLL/lib_lightgbm.lib ./lightgbm/lib/lib_lightgbm.lib
        else
            echo "[ERROR] cannot find pre-compiled library. Aborting"
            exit 1
339
340
        fi
        rm -f ./*.bak
341
342
343
344
345
346
347
    else
        BUILD_SDIST="false"
        BUILD_WHEEL="true"
    fi
fi

if test "${BUILD_SDIST}" = true; then
348
    echo "[INFO] --- building sdist ---"
349
    rm -f ../dist/*.tar.gz
350
351
352
353
354
    # use xargs to work with args that contain whitespaces
    # note that empty echo string leads to that xargs doesn't run the command
    # in some implementations of xargs
    # ref: https://stackoverflow.com/a/8296746
    echo "--sdist --outdir ../dist ${BUILD_ARGS} ." | xargs python -m build
355
356
357
fi

if test "${BUILD_WHEEL}" = true; then
358
    echo "[INFO] --- building wheel ---"
359
    rm -f ../dist/*.whl || true
360
361
362
363
364
    # use xargs to work with args that contain whitespaces
    # note that empty echo string leads to that xargs doesn't run the command
    # in some implementations of xargs
    # ref: https://stackoverflow.com/a/8296746
    echo "--wheel --outdir ../dist ${BUILD_ARGS} ." | xargs python -m build
365
366
367
fi

if test "${INSTALL}" = true; then
368
    echo "[INFO] --- installing lightgbm ---"
369
    cd ../dist
370
    if test "${BUILD_WHEEL}" = true; then
371
        PACKAGE_NAME="$(echo lightgbm*.whl)"
372
    else
373
        PACKAGE_NAME="$(echo lightgbm*.tar.gz)"
374
    fi
375
    # ref for use of '--find-links': https://stackoverflow.com/a/52481267/3986677
376
    # shellcheck disable=SC2086
377
378
    pip install \
        ${PIP_INSTALL_ARGS} \
379
        --force-reinstall \
380
        --no-cache-dir \
381
        --no-deps \
382
        --find-links=. \
383
        "${PACKAGE_NAME}"
384
385
386
    cd ../
fi

387
echo "[INFO] cleaning up"
388
rm -rf ./lightgbm-python