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

# set up R environment
4
CRAN_MIRROR="https://cloud.r-project.org/"
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
# hack to get around this:
# https://stat.ethz.ch/pipermail/r-package-devel/2020q3/005930.html
export _R_CHECK_SYSTEM_CLOCK_=0

14
15
16
17
18
19
20
21
22
# ignore R CMD CHECK NOTE checking how long it has
# been since the last submission
export _R_CHECK_CRAN_INCOMING_REMOTE_=0

# CRAN ignores the "installed size is too large" NOTE,
# so our CI can too. Setting to a large value here just
# to catch extreme problems
export _R_CHECK_PKG_SIZES_THRESHOLD_=60

23
# Get details needed for installing R components
24
25
26
27
28
29
R_MAJOR_VERSION=( ${R_VERSION//./ } )
if [[ "${R_MAJOR_VERSION}" == "3" ]]; then
    export R_MAC_VERSION=3.6.3
    export R_LINUX_VERSION="3.6.3-1bionic"
    export R_APT_REPO="bionic-cran35/"
elif [[ "${R_MAJOR_VERSION}" == "4" ]]; then
30
31
    export R_MAC_VERSION=4.0.4
    export R_LINUX_VERSION="4.0.4-1.1804.0"
32
33
34
35
    export R_APT_REPO="bionic-cran40/"
else
    echo "Unrecognized R version: ${R_VERSION}"
    exit -1
36
37
fi

38
39
40
# 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
41
42
#
# `devscripts` is required for 'checkbashisms' (https://github.com/r-lib/actions/issues/111)
43
if [[ $OS_NAME == "linux" ]]; then
44
45
46
    sudo apt-key adv \
        --keyserver keyserver.ubuntu.com \
        --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
47
    sudo add-apt-repository \
48
        "deb https://cloud.r-project.org/bin/linux/ubuntu ${R_APT_REPO}"
49
50
51
    sudo apt-get update
    sudo apt-get install \
        --no-install-recommends \
Guolin Ke's avatar
Guolin Ke committed
52
        -y --allow-downgrades \
53
            devscripts \
54
            r-base-dev=${R_LINUX_VERSION} \
55
56
57
58
59
60
            texinfo \
            texlive-latex-recommended \
            texlive-fonts-recommended \
            texlive-fonts-extra \
            qpdf \
            || exit -1
61
62
63
64
65
66
67
68

    if [[ $R_BUILD_TYPE == "cran" ]]; then
        sudo apt-get install \
            --no-install-recommends \
            -y \
                autoconf=$(cat R-package/AUTOCONF_UBUNTU_VERSION) \
                || exit -1
    fi
69
70
71
72
fi

# Installing R precompiled for Mac OS 10.11 or higher
if [[ $OS_NAME == "macos" ]]; then
73
    if [[ $R_BUILD_TYPE == "cran" ]]; then
74
        brew install automake
75
    fi
76
77
78
    brew install \
        checkbashisms \
        qpdf
79
    brew install --cask basictex
80
    export PATH="/Library/TeX/texbin:$PATH"
81
82
    sudo tlmgr --verify-repo=none update --self
    sudo tlmgr --verify-repo=none install inconsolata helvetic
83

84
    curl -sL https://cran.r-project.org/bin/macosx/R-${R_MAC_VERSION}.pkg -o R.pkg
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    sudo installer \
        -pkg $(pwd)/R.pkg \
        -target /

    # 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

# Manually install Depends and Imports libraries + 'testthat'
# to avoid a CI-time dependency on devtools (for devtools::install_deps())
102
packages="c('data.table', 'jsonlite', 'Matrix', 'R6', 'testthat')"
103
compile_from_source="both"
104
if [[ $OS_NAME == "macos" ]]; then
105
106
    packages+=", type = 'binary'"
    compile_from_source="never"
107
fi
108
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'))" || exit -1
109
110
111

cd ${BUILD_DIRECTORY}

Guolin Ke's avatar
Guolin Ke committed
112
PKG_TARBALL="lightgbm_*.tar.gz"
113
LOG_FILE_NAME="lightgbm.Rcheck/00check.log"
114
115
116
117
118
119
120
if [[ $R_BUILD_TYPE == "cmake" ]]; then
    Rscript build_r.R --skip-install || exit -1
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
121
        ${BUILD_DIRECTORY}/R-package/recreate-configure.sh
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

        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

    # 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
147
148
149

# fails tests if either ERRORs or WARNINGs are thrown by
# R CMD CHECK
150
check_succeeded="yes"
151
152
153
(
    R CMD check ${PKG_TARBALL} \
        --as-cran \
154
        --run-donttest \
155
156
157
158
159
160
161
162
163
164
165
166
    || 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
167
168

echo "R CMD check build logs:"
169
170
BUILD_LOG_FILE=lightgbm.Rcheck/00install.out
cat ${BUILD_LOG_FILE}
171
172
173
174

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

176
177
if grep -q -E "NOTE|WARNING|ERROR" "$LOG_FILE_NAME"; then
    echo "NOTEs, WARNINGs, or ERRORs have been found by R CMD check"
178
179
    exit -1
fi
180
181
182
183
184
185

# this check makes sure that CI builds of the CRAN package on Mac
# actually use OpenMP
if [[ $OS_NAME == "macos" ]] && [[ $R_BUILD_TYPE == "cran" ]]; then
    omp_working=$(
        cat $BUILD_LOG_FILE \
186
        | grep --count -E "checking whether OpenMP will work .*yes"
187
188
189
190
191
192
    )
    if [[ $omp_working -ne 1 ]]; then
        echo "OpenMP was not found, and should be when testing the CRAN package on macOS"
        exit -1
    fi
fi
193
194
195
196
197
198

# 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 \
199
        | grep --count -E "warning: unknown pragma ignored"
200
201
202
203
204
205
    )
    if [[ $pragma_warning_present -ne 0 ]]; then
        echo "Unknown pragma warning is present, pragmas should have been removed before build"
        exit -1
    fi
fi