test_r_package.sh 10.6 KB
Newer Older
1
2
3
#!/bin/bash

# set up R environment
4
CRAN_MIRROR="https://cran.rstudio.com"
5
6
R_LIB_PATH=~/Rlib
mkdir -p $R_LIB_PATH
7
export R_LIBS=$R_LIB_PATH
8
9
export PATH="$R_LIB_PATH/R/bin:$PATH"

10
11
12
13
14
15
# don't fail builds for long-running examples unless they're very long.
# See https://github.com/microsoft/LightGBM/issues/4049#issuecomment-793412254.
if [[ $R_BUILD_TYPE != "cran" ]]; then
    export _R_CHECK_EXAMPLE_TIMING_THRESHOLD_=30
fi

16
# Get details needed for installing R components
17
18
19
R_MAJOR_VERSION=( ${R_VERSION//./ } )
if [[ "${R_MAJOR_VERSION}" == "3" ]]; then
    export R_MAC_VERSION=3.6.3
20
    export R_MAC_PKG_URL=${CRAN_MIRROR}/bin/macosx/R-${R_MAC_VERSION}.pkg
21
22
23
    export R_LINUX_VERSION="3.6.3-1bionic"
    export R_APT_REPO="bionic-cran35/"
elif [[ "${R_MAJOR_VERSION}" == "4" ]]; then
James Lamb's avatar
James Lamb committed
24
    export R_MAC_VERSION=4.2.2
25
    export R_MAC_PKG_URL=${CRAN_MIRROR}/bin/macosx/base/R-${R_MAC_VERSION}.pkg
26
27
    export R_LINUX_VERSION="4.2.2-1.2204.0"
    export R_APT_REPO="jammy-cran40/"
28
29
30
else
    echo "Unrecognized R version: ${R_VERSION}"
    exit -1
31
32
fi

33
34
35
# installing precompiled R for Ubuntu
# https://cran.r-project.org/bin/linux/ubuntu/#installation
# adding steps from https://stackoverflow.com/a/56378217/3986677 to get latest version
36
37
#
# `devscripts` is required for 'checkbashisms' (https://github.com/r-lib/actions/issues/111)
38
if [[ $OS_NAME == "linux" ]]; then
39
40
    sudo apt-key adv \
        --keyserver keyserver.ubuntu.com \
41
        --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 || exit -1
42
    sudo add-apt-repository \
43
        "deb ${CRAN_MIRROR}/bin/linux/ubuntu ${R_APT_REPO}" || exit -1
44
45
46
    sudo apt-get update
    sudo apt-get install \
        --no-install-recommends \
47
        -y \
48
            devscripts \
49
            r-base-core=${R_LINUX_VERSION} \
50
            r-base-dev=${R_LINUX_VERSION} \
51
            texinfo \
52
            texlive-latex-extra \
53
54
55
56
57
            texlive-latex-recommended \
            texlive-fonts-recommended \
            texlive-fonts-extra \
            qpdf \
            || exit -1
58
59
60
61
62
63

    if [[ $R_BUILD_TYPE == "cran" ]]; then
        sudo apt-get install \
            --no-install-recommends \
            -y \
                autoconf=$(cat R-package/AUTOCONF_UBUNTU_VERSION) \
64
                automake \
65
66
                || exit -1
    fi
67
68
69
70
71
72
73
74
75
    if [[ $INSTALL_CMAKE_FROM_RELEASES == "true" ]]; then
        curl -O -L \
            https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-x86_64.sh \
        || exit -1

        sudo mkdir /opt/cmake || exit -1
        sudo sh cmake-3.25.1-linux-x86_64.sh --skip-license --prefix=/opt/cmake || exit -1
        sudo ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake || exit -1
    fi
76
77
78
79
fi

# Installing R precompiled for Mac OS 10.11 or higher
if [[ $OS_NAME == "macos" ]]; then
80
    brew update-reset && brew update
81
    if [[ $R_BUILD_TYPE == "cran" ]]; then
82
        brew install automake || exit -1
83
    fi
84
85
    brew install \
        checkbashisms \
86
        qpdf || exit -1
87
    brew install basictex || exit -1
88
    export PATH="/Library/TeX/texbin:$PATH"
89
    sudo tlmgr --verify-repo=none update --self || exit -1
James Lamb's avatar
James Lamb committed
90
    sudo tlmgr --verify-repo=none install inconsolata helvetic rsfs || exit -1
91

92
    curl -sL ${R_MAC_PKG_URL} -o R.pkg || exit -1
93
94
    sudo installer \
        -pkg $(pwd)/R.pkg \
95
        -target / || exit -1
96

97
98
99
100
101
102
103
104
105
106
107
108
109
    # Older R versions (<= 4.1.2) on newer macOS (>= 11.0.0) cannot create the necessary symlinks.
    # See https://github.com/r-lib/actions/issues/412.
    if [[ $(sw_vers -productVersion | head -c2) -ge "11" ]]; then
        sudo ln \
            -sf \
            /Library/Frameworks/R.framework/Resources/bin/R \
            /usr/local/bin/R
        sudo ln \
            -sf \
            /Library/Frameworks/R.framework/Resources/bin/Rscript \
            /usr/local/bin/Rscript
    fi

110
111
112
113
114
115
116
117
118
119
120
    # Fix "duplicate libomp versions" issue on Mac
    # by replacing the R libomp.dylib with a symlink to the one installed with brew
    if [[ $COMPILER == "clang" ]]; then
        ver_arr=( ${R_MAC_VERSION//./ } )
        R_MAJOR_MINOR="${ver_arr[0]}.${ver_arr[1]}"
        sudo ln -sf \
            "$(brew --cellar libomp)"/*/lib/libomp.dylib \
            /Library/Frameworks/R.framework/Versions/${R_MAJOR_MINOR}/Resources/lib/libomp.dylib
    fi
fi

121
# Manually install Depends and Imports libraries + 'knitr', 'RhpcBLASctl', 'rmarkdown', 'testthat'
122
# to avoid a CI-time dependency on devtools (for devtools::install_deps())
123
124
# NOTE: testthat is not required when running rchk
if [[ "${TASK}" == "r-rchk" ]]; then
125
    packages="c('data.table', 'jsonlite', 'knitr', 'Matrix', 'R6', 'RhpcBLASctl', 'rmarkdown')"
126
else
127
    packages="c('data.table', 'jsonlite', 'knitr', 'Matrix', 'R6', 'RhpcBLASctl', 'rmarkdown', 'testthat')"
128
fi
129
compile_from_source="both"
130
if [[ $OS_NAME == "macos" ]]; then
131
132
    packages+=", type = 'binary'"
    compile_from_source="never"
133
fi
134
Rscript --vanilla -e "options(install.packages.compile.from.source = '${compile_from_source}'); install.packages(${packages}, repos = '${CRAN_MIRROR}', lib = '${R_LIB_PATH}', dependencies = c('Depends', 'Imports', 'LinkingTo'), Ncpus = parallel::detectCores())" || exit -1
135
136
137

cd ${BUILD_DIRECTORY}

Guolin Ke's avatar
Guolin Ke committed
138
PKG_TARBALL="lightgbm_*.tar.gz"
139
LOG_FILE_NAME="lightgbm.Rcheck/00check.log"
140
if [[ $R_BUILD_TYPE == "cmake" ]]; then
141
    Rscript build_r.R -j4 --skip-install || exit -1
142
143
144
145
146
elif [[ $R_BUILD_TYPE == "cran" ]]; then

    # on Linux, we recreate configure in CI to test if
    # a change in a PR has changed configure.ac
    if [[ $OS_NAME == "linux" ]]; then
147
        ${BUILD_DIRECTORY}/R-package/recreate-configure.sh
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

        num_files_changed=$(
            git diff --name-only | wc -l
        )
        if [[ ${num_files_changed} -gt 0 ]]; then
            echo "'configure' in the R package has changed. Please recreate it and commit the changes."
            echo "Changed files:"
            git diff --compact-summary
            echo "See R-package/README.md for details on how to recreate this script."
            echo ""
            exit -1
        fi
    fi

    ./build-cran-package.sh || exit -1

164
165
166
167
168
169
170
171
172
173
174
175
176
    if [[ "${TASK}" == "r-rchk" ]]; then
        echo "Checking R package with rchk"
        mkdir -p packages
        cp ${PKG_TARBALL} packages
        RCHK_LOG_FILE="rchk-logs.txt"
        docker run \
            -v $(pwd)/packages:/rchk/packages \
            kalibera/rchk:latest \
            "/rchk/packages/${PKG_TARBALL}" \
        2>&1 > ${RCHK_LOG_FILE} \
        || (cat ${RCHK_LOG_FILE} && exit -1)
        cat ${RCHK_LOG_FILE}

James Lamb's avatar
James Lamb committed
177
        # the exceptions below are from R itself and not LightGBM:
178
179
180
181
        # https://github.com/kalibera/rchk/issues/22#issuecomment-656036156
        exit $(
            cat ${RCHK_LOG_FILE} \
            | grep -v "in function strptime_internal" \
James Lamb's avatar
James Lamb committed
182
            | grep -v "in function RunGenCollect" \
183
184
185
186
            | grep --count -E '\[PB\]|ERROR'
        )
    fi

187
188
189
190
191
192
193
194
195
    # Test CRAN source .tar.gz in a directory that is not this repo or below it.
    # When people install.packages('lightgbm'), they won't have the LightGBM
    # git repo around. This is to protect against the use of relative paths
    # like ../../CMakeLists.txt that would only work if you are in the repo
    R_CMD_CHECK_DIR="${HOME}/tmp-r-cmd-check/"
    mkdir -p ${R_CMD_CHECK_DIR}
    mv ${PKG_TARBALL} ${R_CMD_CHECK_DIR}
    cd ${R_CMD_CHECK_DIR}
fi
196
197
198

# fails tests if either ERRORs or WARNINGs are thrown by
# R CMD CHECK
199
check_succeeded="yes"
200
201
202
(
    R CMD check ${PKG_TARBALL} \
        --as-cran \
203
        --run-donttest \
204
205
206
207
208
209
210
211
212
213
214
215
    || check_succeeded="no"
) &

# R CMD check suppresses output, some CIs kill builds after
# a few minutes with no output. This trick gives R CMD check more time
#     * https://github.com/travis-ci/travis-ci/issues/4190#issuecomment-169987525
#     * https://stackoverflow.com/a/29890106/3986677
CHECK_PID=$!
while kill -0 ${CHECK_PID} >/dev/null 2>&1; do
    echo -n -e " \b"
    sleep 5
done
216
217

echo "R CMD check build logs:"
218
219
BUILD_LOG_FILE=lightgbm.Rcheck/00install.out
cat ${BUILD_LOG_FILE}
220
221
222
223

if [[ $check_succeeded == "no" ]]; then
    exit -1
fi
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
used_correct_r_version=$(
    cat $LOG_FILE_NAME \
    | grep --count "using R version ${R_VERSION}"
)
if [[ $used_correct_r_version -ne 1 ]]; then
    echo "Unexpected R version was used. Expected '${R_VERSION}'."
    exit -1
fi

if [[ $R_BUILD_TYPE == "cmake" ]]; then
    passed_correct_r_version_to_cmake=$(
        cat $BUILD_LOG_FILE \
        | grep --count "R version passed into FindLibR.cmake: ${R_VERSION}"
    )
    if [[ $used_correct_r_version -ne 1 ]]; then
        echo "Unexpected R version was passed into cmake. Expected '${R_VERSION}'."
        exit -1
    fi
fi


246
247
if grep -q -E "NOTE|WARNING|ERROR" "$LOG_FILE_NAME"; then
    echo "NOTEs, WARNINGs, or ERRORs have been found by R CMD check"
248
249
    exit -1
fi
250

251
# this check makes sure that CI builds of the package actually use OpenMP
252
253
254
if [[ $OS_NAME == "macos" ]] && [[ $R_BUILD_TYPE == "cran" ]]; then
    omp_working=$(
        cat $BUILD_LOG_FILE \
255
        | grep --count -E "checking whether OpenMP will work .*yes"
256
    )
257
258
259
260
261
262
263
264
265
266
267
elif [[ $R_BUILD_TYPE == "cmake" ]]; then
    omp_working=$(
        cat $BUILD_LOG_FILE \
        | grep --count -E ".*Found OpenMP: TRUE.*"
    )
else
    omp_working=1
fi
if [[ $omp_working -ne 1 ]]; then
    echo "OpenMP was not found"
    exit -1
268
fi
269

270
# this check makes sure that CI builds of the package
271
272
273
274
275
276
# actually use MM_PREFETCH preprocessor definition
if [[ $R_BUILD_TYPE == "cran" ]]; then
    mm_prefetch_working=$(
        cat $BUILD_LOG_FILE \
        | grep --count -E "checking whether MM_PREFETCH work.*yes"
    )
277
278
279
280
281
282
283
284
285
else
    mm_prefetch_working=$(
        cat $BUILD_LOG_FILE \
        | grep --count -E ".*Performing Test MM_PREFETCH - Success"
    )
fi
if [[ $mm_prefetch_working -ne 1 ]]; then
    echo "MM_PREFETCH test was not passed"
    exit -1
286
287
fi

288
# this check makes sure that CI builds of the package
289
290
291
292
293
294
# actually use MM_MALLOC preprocessor definition
if [[ $R_BUILD_TYPE == "cran" ]]; then
    mm_malloc_working=$(
        cat $BUILD_LOG_FILE \
        | grep --count -E "checking whether MM_MALLOC work.*yes"
    )
295
296
297
298
299
300
301
302
303
else
    mm_malloc_working=$(
        cat $BUILD_LOG_FILE \
        | grep --count -E ".*Performing Test MM_MALLOC - Success"
    )
fi
if [[ $mm_malloc_working -ne 1 ]]; then
    echo "MM_MALLOC test was not passed"
    exit -1
304
305
fi

306
307
308
309
310
# this check makes sure that no "warning: unknown pragma ignored" logs
# reach the user leading them to believe that something went wrong
if [[ $R_BUILD_TYPE == "cran" ]]; then
    pragma_warning_present=$(
        cat $BUILD_LOG_FILE \
311
        | grep --count -E "warning: unknown pragma ignored"
312
313
314
315
316
317
    )
    if [[ $pragma_warning_present -ne 0 ]]; then
        echo "Unknown pragma warning is present, pragmas should have been removed before build"
        exit -1
    fi
fi