1#############################################################################
2# $Id: CMake.NCBIptb.ntest.cmake 620493 2020-11-23 18:43:24Z ivanov $
3#############################################################################
4#############################################################################
5##
6##  NCBI CMake wrapper extension
7##  In NCBI CMake wrapper, adds NCBI tests (which use NCBI testing framework)
8##    Author: Andrei Gourianov, gouriano@ncbi
9##
10
11
12##############################################################################
13macro(NCBI_internal_process_ncbi_test_requires _test)
14    set(NCBITEST_REQUIRE_NOTFOUND "")
15    set(_all ${NCBITEST__REQUIRES} ${NCBITEST_${_test}_REQUIRES})
16    if (NOT "${_all}" STREQUAL "")
17        list(REMOVE_DUPLICATES _all)
18    endif()
19
20    foreach(_req IN LISTS _all)
21        NCBI_util_parse_sign( ${_req} _value _negate)
22        if (${_value} OR NCBI_REQUIRE_${_value}_FOUND OR NCBI_COMPONENT_${_value}_FOUND)
23            if (_negate)
24                set(NCBITEST_REQUIRE_NOTFOUND ${NCBITEST_REQUIRE_NOTFOUND} ${_req})
25            endif()
26        else()
27            if (NOT _negate)
28                set(NCBITEST_REQUIRE_NOTFOUND ${NCBITEST_REQUIRE_NOTFOUND} ${_req})
29            endif()
30        endif()
31    endforeach()
32endmacro()
33
34##############################################################################
35function(NCBI_internal_add_ncbi_test _test)
36    if( NOT DEFINED NCBITEST_${_test}_CMD)
37        set(NCBITEST_${_test}_CMD ${NCBI_${NCBI_PROJECT}_OUTPUT})
38    endif()
39    get_filename_component(_ext ${NCBITEST_${_test}_CMD} EXT)
40    if("${_ext}" STREQUAL ".sh")
41        if(EXISTS ${NCBI_CURRENT_SOURCE_DIR}/${NCBITEST_${_test}_CMD})
42            set(NCBITEST_${_test}_ASSETS   ${NCBITEST_${_test}_ASSETS}   ${NCBITEST_${_test}_CMD})
43        endif()
44    endif()
45    set(_requires ${NCBITEST__REQUIRES} ${NCBITEST_${_test}_REQUIRES})
46    set(_assets   ${NCBITEST__ASSETS}   ${NCBITEST_${_test}_ASSETS})
47    set(_watcher  ${NCBI__WATCHER}      ${NCBI_${NCBI_PROJECT}_WATCHER})
48    if (DEFINED NCBITEST_${_test}_TIMEOUT)
49        set(_timeout ${NCBITEST_${_test}_TIMEOUT})
50    elseif(DEFINED NCBITEST__TIMEOUT)
51        set(_timeout ${NCBITEST__TIMEOUT})
52    endif()
53    string(REPLACE ";" " " _args     "${NCBITEST_${_test}_ARG}")
54    string(REPLACE ";" " " _assets   "${_assets}")
55    string(REPLACE ";" " " _requires "${_requires}")
56    string(REPLACE ";" " " _watcher  "${_watcher}")
57    file(RELATIVE_PATH _outdir "${NCBI_SRC_ROOT}" "${NCBI_CURRENT_SOURCE_DIR}")
58
59    NCBI_internal_process_ncbi_test_requires(${_test})
60    if ( NOT "${NCBITEST_REQUIRE_NOTFOUND}" STREQUAL "")
61        if(NCBI_VERBOSE_ALLPROJECTS OR NCBI_VERBOSE_PROJECT_${NCBI_PROJECT})
62            message("${NCBI_PROJECT} (${NCBI_CURRENT_SOURCE_DIR}): Test ${_test} is excluded because of unmet requirements: ${NCBITEST_REQUIRE_NOTFOUND}")
63        endif()
64        return()
65    endif()
66
67    set(_resources "")
68    set(_all ${NCBITEST__RESOURCES} ${NCBITEST_${_test}_RESOURCES})
69    if (NOT "${_all}" STREQUAL "")
70        list(SORT _all)
71        list(REMOVE_DUPLICATES _all)
72    endif()
73    foreach(_res IN LISTS _all)
74        if(DEFINED NCBITEST_RESOURCE_${_res}_AMOUNT)
75            get_property(_count  GLOBAL PROPERTY NCBITEST_RESOURCE_NTEST_${_res}_COUNT)
76            if("${_count}" STREQUAL "")
77                set(_count 0)
78            endif()
79            if(NCBI_VERBOSE_ALLPROJECTS OR NCBI_VERBOSE_PROJECT_${NCBI_PROJECT})
80                message("${NCBI_PROJECT} (${NCBI_CURRENT_SOURCE_DIR}): Test ${_test} uses resource ${_res} (${NCBITEST_RESOURCE_${_res}_AMOUNT})")
81            endif()
82            set(_resources "${_resources} ${_res}_${_count}")
83            math(EXPR _count "${_count} + 1")
84            if("${_count}" GREATER_EQUAL "${NCBITEST_RESOURCE_${_res}_AMOUNT}")
85                set(_count 0)
86            endif()
87            set_property(GLOBAL PROPERTY NCBITEST_RESOURCE_NTEST_${_res}_COUNT ${_count})
88        else()
89            if(NCBI_VERBOSE_ALLPROJECTS OR NCBI_VERBOSE_PROJECT_${NCBI_PROJECT})
90                message("${NCBI_PROJECT} (${NCBI_CURRENT_SOURCE_DIR}): Test ${_test} uses resource ${_res} (1)")
91            endif()
92            set(_resources "${_resources} ${_res}")
93        endif()
94    endforeach()
95
96    set(_s "____")
97    string(APPEND _t "${_outdir} ${_s} ${NCBI_PROJECT} ${_s} ${NCBI_${NCBI_PROJECT}_OUTPUT} ${_s} ")
98    string(APPEND _t "${NCBITEST_${_test}_CMD} ${_args} ${_s} ")
99    string(APPEND _t "${_test} ${_s} ${_assets} ${_s} ${_timeout} ${_s} ")
100    string(APPEND _t "${_requires} ${_s} ${_watcher} ${_s} ${_resources} ${_s} ${NCBITEST_${_test}_TESTALIAS}")
101    get_property(_checklist GLOBAL PROPERTY NCBI_PTBPROP_CHECKLIST)
102    LIST(APPEND _checklist "${_t}\n")
103    set_property(GLOBAL PROPERTY NCBI_PTBPROP_CHECKLIST ${_checklist})
104endfunction()
105
106##############################################################################
107function(NCBI_internal_AddNCBITest _variable _access)
108    if("${_access}" STREQUAL "MODIFIED_ACCESS" AND DEFINED NCBI_${NCBI_PROJECT}_ALLTESTS)
109        foreach(_test IN LISTS NCBI_${NCBI_PROJECT}_ALLTESTS)
110            NCBI_internal_add_ncbi_test(${_test})
111        endforeach()
112    endif()
113endfunction()
114
115##############################################################################
116function(NCBI_internal_create_ncbi_checklist _variable _access)
117    if(NOT "${_access}" STREQUAL "MODIFIED_ACCESS")
118        return()
119    endif()
120
121    get_property(_checklist GLOBAL PROPERTY NCBI_PTBPROP_CHECKLIST)
122#    if(NOT "${_checklist}" STREQUAL "")
123#        list(SORT _checklist)
124#    endif()
125    set(_checkdir ../check)
126    set(_listdir ${NCBI_BUILD_ROOT}/${NCBI_DIRNAME_BUILD}/${_checkdir})
127    if (WIN32 OR XCODE)
128        set(VALGRIND_PATH "")
129    else()
130        set(VALGRIND_PATH "valgrind")
131    endif()
132    set(CHECK_TIMEOUT_MULT 1)
133    set(CHECK_OS_NAME "${HOST_OS_DISTR}")
134    file(WRITE ${_listdir}/check.sh.list ${_checklist})
135
136    if (EXISTS ${NCBI_BUILD_ROOT}/status)
137        file(REMOVE_RECURSE ${NCBI_BUILD_ROOT}/status/*)
138    else()
139        file(MAKE_DIRECTORY ${NCBI_BUILD_ROOT}/status)
140    endif()
141    foreach( _comp IN LISTS NCBI_ALL_COMPONENTS NCBI_ALL_REQUIRES)
142#            file(TOUCH ${NCBI_BUILD_ROOT}/status/${_comp}.enabled)
143        file(WRITE ${NCBI_BUILD_ROOT}/status/${_comp}.enabled "")
144    endforeach()
145
146    if (EXISTS "${NCBI_TREE_BUILDCFG}/check.cfg.in")
147        configure_file(${NCBI_TREE_BUILDCFG}/check.cfg.in ${_listdir}/check.cfg @ONLY)
148    endif()
149    if (EXISTS "${NCBI_TREE_BUILDCFG}/sysdep.sh.in")
150        set(script_shell "#! /bin/sh")
151        set(TAIL_N "tail -n ")
152        configure_file(${NCBI_TREE_BUILDCFG}/sysdep.sh.in ${NCBI_BUILD_ROOT}/sysdep.sh @ONLY)
153        file(COPY ${NCBI_BUILD_ROOT}/sysdep.sh
154                DESTINATION ${_listdir}
155                FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
156        file(REMOVE ${NCBI_BUILD_ROOT}/sysdep.sh)
157    endif()
158endfunction()
159
160##############################################################################
161function(NCBI_internal_add_ncbi_checktarget)
162    set(SCRIPT_NAME "${NCBITK_TREE_ROOT}/${NCBI_DIRNAME_COMMON_SCRIPTS}/check/check_make_unix_cmake.sh")
163    set(WORKDIR ${NCBI_BUILD_ROOT}/${NCBI_DIRNAME_BUILD})
164    set(_checkdir ../check)
165    set(_checkroot ${NCBI_BUILD_ROOT}/${NCBI_DIRNAME_BUILD}/${_checkdir})
166
167    if (WIN32)
168        set(_knowndir "C:/Apps/Admin_Installs/Cygwin64/bin;C:/cygwin64/bin;$ENV{PATH}")
169        string(REPLACE "\\" "/" _knowndir "${_knowndir}")
170        foreach(_dir IN LISTS _knowndir)
171            if (NOT "${_dir}" STREQUAL "" AND EXISTS "${_dir}/sh.exe")
172                message(STATUS "Found Cygwin: ${_dir}")
173
174                string(REPLACE "/" "\\" _dir "${_dir}")
175                string(REPLACE ":" ""   _script "${SCRIPT_NAME}")
176                set(_script "/cygdrive/${_script}")
177                string(REPLACE ":" ""   _root "${NCBI_TREE_ROOT}")
178                set(_root "/cygdrive/${_root}")
179                set(_cmdstart set PATH=.$<SEMICOLON>${_dir}$<SEMICOLON>\%PATH\%& set DIAG_SILENT_ABORT=Y&)
180                set(_cmdstart ${_cmdstart} sh -c 'set -o igncr$<SEMICOLON>export SHELLOPTS$<SEMICOLON>)
181if(OFF)
182# on first build, RUN_CHECKS always creates check.sh in both configurations
183# subsequent builds do not create check.sh
184                foreach(_cfg ${NCBI_CONFIGURATION_TYPES})
185                    set(_cmd ${_cmdstart}${_script} ${_checkdir}/check.sh.list ${NCBI_SIGNATURE_${_cfg}} . ${_root} ${_checkdir} check.sh ${_cfg}')
186                    add_custom_command(OUTPUT "${_checkroot}/${_cfg}/check.sh"
187                        COMMAND ${_cmd}
188                        DEPENDS "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
189                        WORKING_DIRECTORY ${WORKDIR}
190                        COMMENT "Creating ${_checkroot}/${_cfg}/check.sh"
191                    )
192                endforeach()
193
194                set(_cmd ${_cmdstart}${_checkroot}/$<CONFIG>/check.sh run')
195                add_custom_target(RUN_CHECKS
196                    COMMAND ${_cmd}
197                    DEPENDS "${_checkroot}/$<CONFIG>/check.sh"
198                    WORKING_DIRECTORY ${WORKDIR}
199                    COMMENT "Running tests"
200                    SOURCES "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
201                )
202elseif(OFF)
203# on every build creates check.sh in one configuration, then runs it.
204                set(_cmd ${_cmdstart}${_script} ${_checkdir}/check.sh.list ${NCBITEST_SIGNATURE} . ${_root} ${_checkdir} check.sh $<CONFIG>')
205                add_custom_target(RUN_CREATE_CHECKS
206                    COMMAND ${_cmd}
207                    DEPENDS "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
208                    SOURCES "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
209                    WORKING_DIRECTORY ${WORKDIR}
210                    COMMENT "Creating ${_checkroot}/<CONFIG>/check.sh"
211                )
212
213                set(_cmd ${_cmdstart}${_checkroot}/$<CONFIG>/check.sh run')
214                add_custom_target(RUN_CHECKS
215                    COMMAND ${_cmd}
216                    DEPENDS "${_checkroot}/$<CONFIG>/check.sh"
217                    WORKING_DIRECTORY ${WORKDIR}
218                    COMMENT "Running tests"
219                )
220                add_dependencies(RUN_CHECKS RUN_CREATE_CHECKS)
221else()
222# on every build creates check.sh in one configuration, then runs it.
223                set(_cmd ${_cmdstart}${_script} ${_checkdir}/check.sh.list ${NCBITEST_SIGNATURE} . ${_root} ${_checkdir} check.sh $<CONFIG>)
224                set(_cmd ${_cmd}$<SEMICOLON>echo Running tests$<SEMICOLON>${_checkroot}/$<CONFIG>/check.sh run)
225                if ($ENV{NCBI_AUTOMATED_BUILD})
226                    set(_cmd ${_cmd}$<SEMICOLON>echo Collecting errors$<SEMICOLON>${_checkroot}/$<CONFIG>/check.sh concat_err)
227                endif()
228                set(_cmd ${_cmd}')
229                add_custom_target(RUN_CHECKS
230                    COMMAND ${_cmd}
231                    DEPENDS "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
232                    SOURCES "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
233                    WORKING_DIRECTORY ${WORKDIR}
234                    COMMENT "Preparing tests"
235                )
236endif()
237                return()
238            endif()
239        endforeach()
240        message("NOT FOUND Cygwin")
241    elseif(XCODE)
242        set(_cmdstart export DIAG_SILENT_ABORT=Y$<SEMICOLON>)
243        set(_cmd ${_cmdstart}${SCRIPT_NAME} ${_checkdir}/check.sh.list ${NCBITEST_SIGNATURE} . ${NCBI_TREE_ROOT} ${_checkdir} check.sh $<CONFIG>)
244        set(_cmd ${_cmd}$<SEMICOLON>echo Running tests$<SEMICOLON>${_checkroot}/$<CONFIG>/check.sh run)
245        if ($ENV{NCBI_AUTOMATED_BUILD})
246            set(_cmd ${_cmd}$<SEMICOLON>echo Collecting errors$<SEMICOLON>${_checkroot}/$<CONFIG>/check.sh concat_err)
247        endif()
248        add_custom_target(RUN_CHECKS
249            COMMAND ${_cmd}
250            DEPENDS "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
251            SOURCES "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
252            WORKING_DIRECTORY ${WORKDIR}
253            COMMENT "Preparing tests"
254        )
255    else()
256        set(_cmdstart export DIAG_SILENT_ABORT=Y$<SEMICOLON>)
257        set(_cmd ${_cmdstart}${SCRIPT_NAME} ${_checkdir}/check.sh.list ${NCBITEST_SIGNATURE} . ${NCBI_TREE_ROOT} ${_checkdir} check.sh$<SEMICOLON>)
258        set(_cmd ${_cmd}echo Running tests$<SEMICOLON>${_checkroot}/check.sh run)
259        if ($ENV{NCBI_AUTOMATED_BUILD})
260            set(_cmd ${_cmd}$<SEMICOLON>echo Collecting errors$<SEMICOLON>${_checkroot}/check.sh concat_err)
261        endif()
262        add_custom_target(check
263            COMMAND ${_cmd}
264            DEPENDS "${_checkroot}/check.sh.list;${SCRIPT_NAME}"
265            WORKING_DIRECTORY ${WORKDIR}
266            COMMENT "Preparing tests"
267        )
268    endif()
269endfunction()
270
271#############################################################################
272NCBI_register_hook(TARGET_ADDED  NCBI_internal_AddNCBITest)
273NCBI_register_hook(ALL_ADDED     NCBI_internal_create_ncbi_checklist)
274
275NCBI_internal_add_ncbi_checktarget()
276
277