test.sh 11.2 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
#!/bin/bash

3
if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "gcc" ]]; then
4
5
    export CXX=g++-10
    export CC=gcc-10
6
elif [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "clang" ]]; then
7
8
    export CXX=clang++
    export CC=clang
Guolin Ke's avatar
Guolin Ke committed
9
10
fi

11
if [[ "${TASK}" == "r-package" ]]; then
12
13
14
15
    bash ${BUILD_DIRECTORY}/.ci/test_r_package.sh || exit -1
    exit 0
fi

16
17
18
19
20
21
22
23
if [[ "$TASK" == "cpp-tests" ]]; then
    mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build
    cmake -DBUILD_CPP_TEST=ON -DUSE_OPENMP=OFF ..
    make testlightgbm -j4 || exit -1
    ./../testlightgbm || exit -1
    exit 0
fi

24
25
conda create -q -y -n $CONDA_ENV python=$PYTHON_VERSION
source activate $CONDA_ENV
26
27
28

cd $BUILD_DIRECTORY

29
if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then
30
    cd $BUILD_DIRECTORY/docs
31
32
    conda install -q -y -n $CONDA_ENV -c conda-forge doxygen rstcheck
    pip install --user -r requirements.txt
33
34
35
36
    # check reStructuredText formatting
    cd $BUILD_DIRECTORY/python-package
    rstcheck --report warning `find . -type f -name "*.rst"` || exit -1
    cd $BUILD_DIRECTORY/docs
37
    rstcheck --report warning --ignore-directives=autoclass,autofunction,doxygenfile `find . -type f -name "*.rst"` || exit -1
38
    # build docs
39
    make html || exit -1
40
41
    if [[ $TASK == "check-links" ]]; then
        # check docs for broken links
42
        pip install --user linkchecker
43
44
45
        linkchecker --config=.linkcheckerrc ./_build/html/*.html || exit -1
        exit 0
    fi
46
47
48
    # check the consistency of parameters' descriptions and other stuff
    cp $BUILD_DIRECTORY/docs/Parameters.rst $BUILD_DIRECTORY/docs/Parameters-backup.rst
    cp $BUILD_DIRECTORY/src/io/config_auto.cpp $BUILD_DIRECTORY/src/io/config_auto-backup.cpp
49
    python $BUILD_DIRECTORY/helpers/parameter_generator.py || exit -1
50
51
52
53
    diff $BUILD_DIRECTORY/docs/Parameters-backup.rst $BUILD_DIRECTORY/docs/Parameters.rst || exit -1
    diff $BUILD_DIRECTORY/src/io/config_auto-backup.cpp $BUILD_DIRECTORY/src/io/config_auto.cpp || exit -1
    exit 0
fi
54

55
if [[ $TASK == "lint" ]]; then
56
57
58
    conda install -q -y -n $CONDA_ENV \
        pycodestyle \
        pydocstyle \
59
        r-stringi  # stringi needs to be installed separate from r-lintr to avoid issues like 'unable to load shared object stringi.so'
60
    # r-xfun below has to be upgraded because lintr requires > 0.19 for that package
61
62
    conda install -q -y -n $CONDA_ENV \
        -c conda-forge \
63
            libxml2 \
64
            "r-xfun>=0.19" \
65
            "r-lintr>=2.0"
66
    pip install --user cpplint isort mypy
67
    echo "Linting Python code"
68
69
    pycodestyle --ignore=E501,W503 --exclude=./.nuget,./external_libs . || exit -1
    pydocstyle --convention=numpy --add-ignore=D105 --match-dir="^(?!^external_libs|test|example).*" --match="(?!^test_|setup).*\.py" . || exit -1
70
    isort . --check-only || exit -1
71
    mypy --ignore-missing-imports python-package/ || true
72
73
74
    echo "Linting R code"
    Rscript ${BUILD_DIRECTORY}/.ci/lint_r_code.R ${BUILD_DIRECTORY} || exit -1
    echo "Linting C++ code"
75
    cpplint --filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length --recursive ./src ./include ./R-package ./swig ./tests || exit -1
Guolin Ke's avatar
Guolin Ke committed
76
77
78
79
    exit 0
fi

if [[ $TASK == "if-else" ]]; then
Nikita Titov's avatar
Nikita Titov committed
80
    conda install -q -y -n $CONDA_ENV numpy
81
    mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build && cmake .. && make lightgbm -j4 || exit -1
82
    cd $BUILD_DIRECTORY/tests/cpp_tests && ../../lightgbm config=train.conf convert_model_language=cpp convert_model=../../src/boosting/gbdt_prediction.cpp && ../../lightgbm config=predict.conf output_result=origin.pred || exit -1
83
    cd $BUILD_DIRECTORY/build && make lightgbm -j4 || exit -1
84
    cd $BUILD_DIRECTORY/tests/cpp_tests && ../../lightgbm config=predict.conf output_result=ifelse.pred && python test.py || exit -1
Guolin Ke's avatar
Guolin Ke committed
85
86
87
    exit 0
fi

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
if [[ $TASK == "swig" ]]; then
    mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build
    if [[ $OS_NAME == "macos" ]]; then
        cmake -DUSE_SWIG=ON -DAPPLE_OUTPUT_DYLIB=ON ..
    else
        cmake -DUSE_SWIG=ON ..
    fi
    make -j4 || exit -1
    if [[ $OS_NAME == "linux" ]] && [[ $COMPILER == "gcc" ]]; then
        objdump -T $BUILD_DIRECTORY/lib_lightgbm.so > $BUILD_DIRECTORY/objdump.log || exit -1
        objdump -T $BUILD_DIRECTORY/lib_lightgbm_swig.so >> $BUILD_DIRECTORY/objdump.log || exit -1
        python $BUILD_DIRECTORY/helpers/check_dynamic_dependencies.py $BUILD_DIRECTORY/objdump.log || exit -1
    fi
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
        cp $BUILD_DIRECTORY/build/lightgbmlib.jar $BUILD_ARTIFACTSTAGINGDIRECTORY/lightgbmlib_$OS_NAME.jar
    fi
    exit 0
fi

107
108
109
110
111
112
113
114
# temporary fix for https://github.com/microsoft/LightGBM/issues/4285
if [[ $PYTHON_VERSION == "3.6" ]]; then
    DASK_DEPENDENCIES="dask distributed"
else
    DASK_DEPENDENCIES="dask=2021.4.0 distributed=2021.4.0"
fi

conda install -q -y -n $CONDA_ENV cloudpickle ${DASK_DEPENDENCIES} joblib matplotlib numpy pandas psutil pytest scikit-learn scipy
115
pip install graphviz  # python-graphviz from Anaconda is not allowed to be installed with Python 3.9
Guolin Ke's avatar
Guolin Ke committed
116

117
if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "clang" ]]; then
118
119
    # fix "OMP: Error #15: Initializing libiomp5.dylib, but found libomp.dylib already initialized." (OpenMP library conflict due to conda's MKL)
    for LIBOMP_ALIAS in libgomp.dylib libiomp5.dylib libomp.dylib; do sudo ln -sf "$(brew --cellar libomp)"/*/lib/libomp.dylib $CONDA_PREFIX/lib/$LIBOMP_ALIAS || exit -1; done
120
121
fi

Guolin Ke's avatar
Guolin Ke committed
122
if [[ $TASK == "sdist" ]]; then
123
    cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
124
    pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v || exit -1
125
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
126
127
128
        cp $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz $BUILD_ARTIFACTSTAGINGDIRECTORY
    fi
    pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
Guolin Ke's avatar
Guolin Ke committed
129
130
    exit 0
elif [[ $TASK == "bdist" ]]; then
131
    if [[ $OS_NAME == "macos" ]]; then
132
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --plat-name=macosx --python-tag py3 || exit -1
133
        mv dist/lightgbm-$LGB_VER-py3-none-macosx.whl dist/lightgbm-$LGB_VER-py3-none-macosx_10_14_x86_64.macosx_10_15_x86_64.macosx_11_0_x86_64.whl
134
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
135
            cp dist/lightgbm-$LGB_VER-py3-none-macosx*.whl $BUILD_ARTIFACTSTAGINGDIRECTORY
136
        fi
Guolin Ke's avatar
Guolin Ke committed
137
    else
138
139
140
141
142
143
144
        ARCH=$(uname -m)
        if [[ $ARCH == "x86_64" ]]; then
            PLATFORM="manylinux1_x86_64"
        else
            PLATFORM="manylinux2014_$ARCH"
        fi
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --plat-name=$PLATFORM --python-tag py3 || exit -1
145
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
146
            cp dist/lightgbm-$LGB_VER-py3-none-$PLATFORM.whl $BUILD_ARTIFACTSTAGINGDIRECTORY
147
        fi
Guolin Ke's avatar
Guolin Ke committed
148
    fi
149
    pip install --user $BUILD_DIRECTORY/python-package/dist/*.whl || exit -1
150
    pytest $BUILD_DIRECTORY/tests || exit -1
Guolin Ke's avatar
Guolin Ke committed
151
152
153
    exit 0
fi

154
155
mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build

Guolin Ke's avatar
Guolin Ke committed
156
if [[ $TASK == "gpu" ]]; then
157
158
    sed -i'.bak' 's/std::string device_type = "cpu";/std::string device_type = "gpu";/' $BUILD_DIRECTORY/include/LightGBM/config.h
    grep -q 'std::string device_type = "gpu"' $BUILD_DIRECTORY/include/LightGBM/config.h || exit -1  # make sure that changes were really done
Guolin Ke's avatar
Guolin Ke committed
159
    if [[ $METHOD == "pip" ]]; then
160
        cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
161
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v --install-option=--gpu --install-option="--opencl-include-dir=$AMDAPPSDK_PATH/include/" || exit -1
162
        pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
Guolin Ke's avatar
Guolin Ke committed
163
        exit 0
164
165
166
167
168
169
170
    elif [[ $METHOD == "wheel" ]]; then
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --gpu --opencl-include-dir="$AMDAPPSDK_PATH/include/" || exit -1
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER*.whl -v || exit -1
        pytest $BUILD_DIRECTORY/tests || exit -1
        exit 0
    elif [[ $METHOD == "source" ]]; then
        cmake -DUSE_GPU=ON -DOpenCL_INCLUDE_DIR=$AMDAPPSDK_PATH/include/ ..
Guolin Ke's avatar
Guolin Ke committed
171
    fi
172
173
174
175
176
177
178
179
elif [[ $TASK == "cuda" ]]; then
    sed -i'.bak' 's/std::string device_type = "cpu";/std::string device_type = "cuda";/' $BUILD_DIRECTORY/include/LightGBM/config.h
    grep -q 'std::string device_type = "cuda"' $BUILD_DIRECTORY/include/LightGBM/config.h || exit -1  # make sure that changes were really done
    if [[ $METHOD == "pip" ]]; then
        cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v --install-option=--cuda || exit -1
        pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
        exit 0
180
181
182
183
184
185
186
    elif [[ $METHOD == "wheel" ]]; then
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --cuda || exit -1
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER*.whl -v || exit -1
        pytest $BUILD_DIRECTORY/tests || exit -1
        exit 0
    elif [[ $METHOD == "source" ]]; then
        cmake -DUSE_CUDA=ON ..
187
    fi
188
elif [[ $TASK == "mpi" ]]; then
189
    if [[ $METHOD == "pip" ]]; then
190
        cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
191
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v --install-option=--mpi || exit -1
192
        pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
193
        exit 0
194
195
196
197
198
199
200
    elif [[ $METHOD == "wheel" ]]; then
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --mpi || exit -1
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER*.whl -v || exit -1
        pytest $BUILD_DIRECTORY/tests || exit -1
        exit 0
    elif [[ $METHOD == "source" ]]; then
        cmake -DUSE_MPI=ON -DUSE_DEBUG=ON ..
201
    fi
Guolin Ke's avatar
Guolin Ke committed
202
else
203
    cmake ..
Guolin Ke's avatar
Guolin Ke committed
204
205
fi

206
make _lightgbm -j4 || exit -1
Guolin Ke's avatar
Guolin Ke committed
207

208
cd $BUILD_DIRECTORY/python-package && python setup.py install --precompile --user || exit -1
209
pytest $BUILD_DIRECTORY/tests || exit -1
Guolin Ke's avatar
Guolin Ke committed
210
211

if [[ $TASK == "regular" ]]; then
212
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
213
214
215
        if [[ $OS_NAME == "macos" ]]; then
            cp $BUILD_DIRECTORY/lib_lightgbm.so $BUILD_ARTIFACTSTAGINGDIRECTORY/lib_lightgbm.dylib
        else
216
217
218
219
            if [[ $COMPILER == "gcc" ]]; then
                objdump -T $BUILD_DIRECTORY/lib_lightgbm.so > $BUILD_DIRECTORY/objdump.log || exit -1
                python $BUILD_DIRECTORY/helpers/check_dynamic_dependencies.py $BUILD_DIRECTORY/objdump.log || exit -1
            fi
220
221
            cp $BUILD_DIRECTORY/lib_lightgbm.so $BUILD_ARTIFACTSTAGINGDIRECTORY/lib_lightgbm.so
        fi
Guolin Ke's avatar
Guolin Ke committed
222
    fi
223
    cd $BUILD_DIRECTORY/examples/python-guide
Guolin Ke's avatar
Guolin Ke committed
224
225
226
227
    sed -i'.bak' '/import lightgbm as lgb/a\
import matplotlib\
matplotlib.use\(\"Agg\"\)\
' plot_example.py  # prevent interactive window mode
228
    sed -i'.bak' 's/graph.render(view=True)/graph.render(view=False)/' plot_example.py
229
    for f in *.py **/*.py; do python $f || exit -1; done  # run all examples
230
    cd $BUILD_DIRECTORY/examples/python-guide/notebooks
Nikita Titov's avatar
Nikita Titov committed
231
    conda install -q -y -n $CONDA_ENV ipywidgets notebook
232
    jupyter nbconvert --ExecutePreprocessor.timeout=180 --to notebook --execute --inplace *.ipynb || exit -1  # run all notebooks
Guolin Ke's avatar
Guolin Ke committed
233
fi