test.sh 11.1 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
conda create -q -y -n $CONDA_ENV python=$PYTHON_VERSION
source activate $CONDA_ENV
18
19
20

cd $BUILD_DIRECTORY

21
if [[ $TASK == "check-docs" ]] || [[ $TASK == "check-links" ]]; then
22
    cd $BUILD_DIRECTORY/docs
23
24
    conda install -q -y -n $CONDA_ENV -c conda-forge doxygen rstcheck
    pip install --user -r requirements.txt
25
26
27
28
    # check reStructuredText formatting
    cd $BUILD_DIRECTORY/python-package
    rstcheck --report warning `find . -type f -name "*.rst"` || exit -1
    cd $BUILD_DIRECTORY/docs
29
    rstcheck --report warning --ignore-directives=autoclass,autofunction,doxygenfile `find . -type f -name "*.rst"` || exit -1
30
    # build docs
31
    make html || exit -1
32
33
    if [[ $TASK == "check-links" ]]; then
        # check docs for broken links
34
        pip install --user linkchecker
35
36
37
        linkchecker --config=.linkcheckerrc ./_build/html/*.html || exit -1
        exit 0
    fi
38
39
40
    # 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
41
    python $BUILD_DIRECTORY/helpers/parameter_generator.py || exit -1
42
43
44
45
    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
46

47
if [[ $TASK == "lint" ]]; then
48
49
50
    conda install -q -y -n $CONDA_ENV \
        pycodestyle \
        pydocstyle \
51
        r-stringi  # stringi needs to be installed separate from r-lintr to avoid issues like 'unable to load shared object stringi.so'
52
    # r-xfun below has to be upgraded because lintr requires > 0.19 for that package
53
54
    conda install -q -y -n $CONDA_ENV \
        -c conda-forge \
55
            libxml2 \
56
            "r-xfun>=0.19" \
57
            "r-lintr>=2.0"
58
    pip install --user cpplint isort mypy
59
    echo "Linting Python code"
60
61
    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
62
    isort . --check-only || exit -1
63
    mypy --ignore-missing-imports python-package/ || true
64
65
66
    echo "Linting R code"
    Rscript ${BUILD_DIRECTORY}/.ci/lint_r_code.R ${BUILD_DIRECTORY} || exit -1
    echo "Linting C++ code"
67
    cpplint --filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length --recursive ./src ./include ./R-package || exit -1
Guolin Ke's avatar
Guolin Ke committed
68
69
70
71
    exit 0
fi

if [[ $TASK == "if-else" ]]; then
Nikita Titov's avatar
Nikita Titov committed
72
    conda install -q -y -n $CONDA_ENV numpy
73
    mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build && cmake .. && make lightgbm -j4 || exit -1
74
    cd $BUILD_DIRECTORY/tests/cpp_test && ../../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
75
    cd $BUILD_DIRECTORY/build && make lightgbm -j4 || exit -1
76
    cd $BUILD_DIRECTORY/tests/cpp_test && ../../lightgbm config=predict.conf output_result=ifelse.pred && python test.py || exit -1
Guolin Ke's avatar
Guolin Ke committed
77
78
79
    exit 0
fi

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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

99
conda install -q -y -n $CONDA_ENV cloudpickle joblib matplotlib numpy pandas psutil pytest scikit-learn scipy
100
101
102

# graphviz must come from conda-forge to avoid this on some linux distros:
# https://github.com/conda-forge/graphviz-feedstock/issues/18
103
104
105
106
#
# dask and distributed must come from conda-forge because they need to be kept
# in sync and conda-forge packages are updated more quickly (automatically based
# on pushes to PyPI)
107
108
109
conda install -q -y \
    -n $CONDA_ENV \
    -c conda-forge \
110
111
        'dask>=2021.3.0' \
        'distributed>=2021.3.0' \
112
113
        python-graphviz \
        xorg-libxau
Guolin Ke's avatar
Guolin Ke committed
114

115
if [[ $OS_NAME == "macos" ]] && [[ $COMPILER == "clang" ]]; then
116
117
    # 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
118
119
fi

Guolin Ke's avatar
Guolin Ke committed
120
if [[ $TASK == "sdist" ]]; then
121
    cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
122
    pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v || exit -1
123
    if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
124
125
126
        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
127
128
    exit 0
elif [[ $TASK == "bdist" ]]; then
129
    if [[ $OS_NAME == "macos" ]]; then
130
        cd $BUILD_DIRECTORY/python-package && python setup.py bdist_wheel --plat-name=macosx --python-tag py3 || exit -1
131
        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
132
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
133
            cp dist/lightgbm-$LGB_VER-py3-none-macosx*.whl $BUILD_ARTIFACTSTAGINGDIRECTORY
134
        fi
Guolin Ke's avatar
Guolin Ke committed
135
    else
136
137
138
139
140
141
142
        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
143
        if [[ $PRODUCES_ARTIFACTS == "true" ]]; then
144
            cp dist/lightgbm-$LGB_VER-py3-none-$PLATFORM.whl $BUILD_ARTIFACTSTAGINGDIRECTORY
145
        fi
Guolin Ke's avatar
Guolin Ke committed
146
    fi
147
    pip install --user $BUILD_DIRECTORY/python-package/dist/*.whl || exit -1
148
    pytest $BUILD_DIRECTORY/tests || exit -1
Guolin Ke's avatar
Guolin Ke committed
149
150
151
    exit 0
fi

152
153
mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build

Guolin Ke's avatar
Guolin Ke committed
154
if [[ $TASK == "gpu" ]]; then
155
156
    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
157
    if [[ $METHOD == "pip" ]]; then
158
        cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
159
        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
160
        pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
Guolin Ke's avatar
Guolin Ke committed
161
        exit 0
162
163
164
165
166
167
168
    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
169
    fi
170
171
172
173
174
175
176
177
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
178
179
180
181
182
183
184
    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 ..
185
    fi
186
elif [[ $TASK == "mpi" ]]; then
187
    if [[ $METHOD == "pip" ]]; then
188
        cd $BUILD_DIRECTORY/python-package && python setup.py sdist || exit -1
189
        pip install --user $BUILD_DIRECTORY/python-package/dist/lightgbm-$LGB_VER.tar.gz -v --install-option=--mpi || exit -1
190
        pytest $BUILD_DIRECTORY/tests/python_package_test || exit -1
191
        exit 0
192
193
194
195
196
197
198
    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 ..
199
    fi
Guolin Ke's avatar
Guolin Ke committed
200
else
201
    cmake ..
Guolin Ke's avatar
Guolin Ke committed
202
203
fi

204
make _lightgbm -j4 || exit -1
Guolin Ke's avatar
Guolin Ke committed
205

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

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