1# Keep track if we have all dependencies.
2set(ENABLE_CHECK_TARGETS TRUE)
3
4# Function to find required dependencies for testing.
5function(find_standalone_test_dependencies)
6  include(FindPythonInterp)
7
8  if (NOT PYTHONINTERP_FOUND)
9    message(STATUS "Could not find Python.")
10    message(WARNING "The check targets will not be available!")
11    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
12    return()
13  endif()
14
15  # Find executables.
16  find_program(OPENMP_LLVM_LIT_EXECUTABLE
17    NAMES llvm-lit lit.py lit
18    PATHS ${OPENMP_LLVM_TOOLS_DIR})
19  if (NOT OPENMP_LLVM_LIT_EXECUTABLE)
20    message(STATUS "Cannot find llvm-lit.")
21    message(STATUS "Please put llvm-lit in your PATH, set OPENMP_LLVM_LIT_EXECUTABLE to its full path, or point OPENMP_LLVM_TOOLS_DIR to its directory.")
22    message(WARNING "The check targets will not be available!")
23    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
24    return()
25  endif()
26
27  find_program(OPENMP_FILECHECK_EXECUTABLE
28    NAMES FileCheck
29    PATHS ${OPENMP_LLVM_TOOLS_DIR})
30  if (NOT OPENMP_FILECHECK_EXECUTABLE)
31    message(STATUS "Cannot find FileCheck.")
32    message(STATUS "Please put FileCheck in your PATH, set OPENMP_FILECHECK_EXECUTABLE to its full path, or point OPENMP_LLVM_TOOLS_DIR to its directory.")
33    message(WARNING "The check targets will not be available!")
34    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
35    return()
36  endif()
37
38  find_program(OPENMP_NOT_EXECUTABLE
39    NAMES not
40    PATHS ${OPENMP_LLVM_TOOLS_DIR})
41  if (NOT OPENMP_NOT_EXECUTABLE)
42    message(STATUS "Cannot find 'not'.")
43    message(STATUS "Please put 'not' in your PATH, set OPENMP_NOT_EXECUTABLE to its full path, or point OPENMP_LLVM_TOOLS_DIR to its directory.")
44    message(WARNING "The check targets will not be available!")
45    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
46    return()
47  endif()
48endfunction()
49
50if (${OPENMP_STANDALONE_BUILD})
51  find_standalone_test_dependencies()
52
53  # Set lit arguments.
54  set(DEFAULT_LIT_ARGS "-sv --show-unsupported --show-xfail")
55  if (MSVC OR XCODE)
56    set(DEFAULT_LIT_ARGS "${DEFAULT_LIT_ARGS} --no-progress-bar")
57  endif()
58  set(OPENMP_LIT_ARGS "${DEFAULT_LIT_ARGS}" CACHE STRING "Options for lit.")
59  separate_arguments(OPENMP_LIT_ARGS)
60else()
61  set(OPENMP_FILECHECK_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/FileCheck)
62  set(OPENMP_NOT_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/not)
63endif()
64
65# Macro to extract information about compiler from file. (no own scope)
66macro(extract_test_compiler_information lang file)
67  file(READ ${file} information)
68  list(GET information 0 path)
69  list(GET information 1 id)
70  list(GET information 2 version)
71  list(GET information 3 openmp_flags)
72  list(GET information 4 has_tsan_flags)
73
74  set(OPENMP_TEST_${lang}_COMPILER_PATH ${path})
75  set(OPENMP_TEST_${lang}_COMPILER_ID ${id})
76  set(OPENMP_TEST_${lang}_COMPILER_VERSION ${version})
77  set(OPENMP_TEST_${lang}_COMPILER_OPENMP_FLAGS ${openmp_flags})
78  set(OPENMP_TEST_${lang}_COMPILER_HAS_TSAN_FLAGS ${has_tsan_flags})
79endmacro()
80
81# Function to set variables with information about the test compiler.
82function(set_test_compiler_information dir)
83  extract_test_compiler_information(C ${dir}/CCompilerInformation.txt)
84  extract_test_compiler_information(CXX ${dir}/CXXCompilerInformation.txt)
85  if (NOT("${OPENMP_TEST_C_COMPILER_ID}" STREQUAL "${OPENMP_TEST_CXX_COMPILER_ID}" AND
86          "${OPENMP_TEST_C_COMPILER_VERSION}" STREQUAL "${OPENMP_TEST_CXX_COMPILER_VERSION}"))
87    message(STATUS "Test compilers for C and C++ don't match.")
88    message(WARNING "The check targets will not be available!")
89    set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
90  else()
91    set(OPENMP_TEST_COMPILER_ID "${OPENMP_TEST_C_COMPILER_ID}" PARENT_SCOPE)
92    set(OPENMP_TEST_COMPILER_VERSION "${OPENMP_TEST_C_COMPILER_VERSION}" PARENT_SCOPE)
93    set(OPENMP_TEST_COMPILER_OPENMP_FLAGS "${OPENMP_TEST_C_COMPILER_OPENMP_FLAGS}" PARENT_SCOPE)
94    set(OPENMP_TEST_COMPILER_HAS_TSAN_FLAGS "${OPENMP_TEST_C_COMPILER_HAS_TSAN_FLAGS}" PARENT_SCOPE)
95
96    # Determine major version.
97    string(REGEX MATCH "[0-9]+" major "${OPENMP_TEST_C_COMPILER_VERSION}")
98    string(REGEX MATCH "[0-9]+\\.[0-9]+" majorminor "${OPENMP_TEST_C_COMPILER_VERSION}")
99    set(OPENMP_TEST_COMPILER_VERSION_MAJOR "${major}" PARENT_SCOPE)
100    set(OPENMP_TEST_COMPILER_VERSION_MAJOR_MINOR "${majorminor}" PARENT_SCOPE)
101  endif()
102endfunction()
103
104if (${OPENMP_STANDALONE_BUILD})
105  # Detect compiler that should be used for testing.
106  # We cannot use ExternalProject_Add() because its configuration runs when this
107  # project is built which is too late for detecting the compiler...
108  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/DetectTestCompiler)
109  execute_process(
110    COMMAND ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} ${CMAKE_CURRENT_LIST_DIR}/DetectTestCompiler
111      -DCMAKE_C_COMPILER=${OPENMP_TEST_C_COMPILER}
112      -DCMAKE_CXX_COMPILER=${OPENMP_TEST_CXX_COMPILER}
113    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/DetectTestCompiler
114    OUTPUT_VARIABLE DETECT_COMPILER_OUT
115    ERROR_VARIABLE DETECT_COMPILER_ERR
116    RESULT_VARIABLE DETECT_COMPILER_RESULT)
117  if (DETECT_COMPILER_RESULT)
118    message(STATUS "Could not detect test compilers.")
119    message(WARNING "The check targets will not be available!")
120    set(ENABLE_CHECK_TARGETS FALSE)
121  else()
122    set_test_compiler_information(${CMAKE_CURRENT_BINARY_DIR}/DetectTestCompiler)
123  endif()
124else()
125  # Set the information that we know.
126  set(OPENMP_TEST_COMPILER_ID "Clang")
127  # Cannot use CLANG_VERSION because we are not guaranteed that this is already set.
128  set(OPENMP_TEST_COMPILER_VERSION "${LLVM_VERSION}")
129  set(OPENMP_TEST_COMPILER_VERSION_MAJOR "${LLVM_VERSION_MAJOR}")
130  set(OPENMP_TEST_COMPILER_VERSION_MAJOR_MINOR "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
131  # Unfortunately the top-level cmake/config-ix.cmake file mangles CMake's
132  # CMAKE_THREAD_LIBS_INIT variable from the FindThreads package, so work
133  # around that, until it is fixed there.
134  if("${CMAKE_THREAD_LIBS_INIT}" STREQUAL "-lpthread")
135    set(OPENMP_TEST_COMPILER_THREAD_FLAGS "-pthread")
136  else()
137    set(OPENMP_TEST_COMPILER_THREAD_FLAGS "${CMAKE_THREAD_LIBS_INIT}")
138  endif()
139  if(TARGET tsan)
140    set(OPENMP_TEST_COMPILER_HAS_TSAN_FLAGS 1)
141  else()
142    set(OPENMP_TEST_COMPILER_HAS_TSAN_FLAGS 0)
143  endif()
144  # TODO: Implement blockaddress in GlobalISel and remove this flag!
145  set(OPENMP_TEST_COMPILER_OPENMP_FLAGS "-fopenmp ${OPENMP_TEST_COMPILER_THREAD_FLAGS} -fno-experimental-isel")
146endif()
147
148# Function to set compiler features for use in lit.
149function(set_test_compiler_features)
150  if ("${OPENMP_TEST_COMPILER_ID}" STREQUAL "GNU")
151    set(comp "gcc")
152  elseif ("${OPENMP_TEST_COMPILER_ID}" STREQUAL "Intel")
153    set(comp "icc")
154  else()
155    # Just use the lowercase of the compiler ID as fallback.
156    string(TOLOWER "${OPENMP_TEST_COMPILER_ID}" comp)
157  endif()
158  set(OPENMP_TEST_COMPILER_FEATURES "['${comp}', '${comp}-${OPENMP_TEST_COMPILER_VERSION_MAJOR}', '${comp}-${OPENMP_TEST_COMPILER_VERSION_MAJOR_MINOR}', '${comp}-${OPENMP_TEST_COMPILER_VERSION}']" PARENT_SCOPE)
159endfunction()
160set_test_compiler_features()
161
162# Function to add a testsuite for an OpenMP runtime library.
163function(add_openmp_testsuite target comment)
164  if (NOT ENABLE_CHECK_TARGETS)
165    add_custom_target(${target}
166      COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, dependencies not found.")
167    message(STATUS "${target} does nothing.")
168    return()
169  endif()
170
171  cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "DEPENDS;ARGS" ${ARGN})
172  # EXCLUDE_FROM_CHECK_ALL excludes the test ${target} out of check-openmp.
173  if (NOT ARG_EXCLUDE_FROM_CHECK_ALL)
174    # Register the testsuites and depends for the check-openmp rule.
175    set_property(GLOBAL APPEND PROPERTY OPENMP_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS})
176    set_property(GLOBAL APPEND PROPERTY OPENMP_LIT_DEPENDS ${ARG_DEPENDS})
177  endif()
178
179  if (${OPENMP_STANDALONE_BUILD})
180    set(LIT_ARGS ${OPENMP_LIT_ARGS} ${ARG_ARGS})
181    add_custom_target(${target}
182      COMMAND ${PYTHON_EXECUTABLE} ${OPENMP_LLVM_LIT_EXECUTABLE} ${LIT_ARGS} ${ARG_UNPARSED_ARGUMENTS}
183      COMMENT ${comment}
184      DEPENDS ${ARG_DEPENDS}
185      USES_TERMINAL
186    )
187  else()
188    if (ARG_EXCLUDE_FROM_CHECK_ALL)
189      add_lit_testsuite(${target}
190        ${comment}
191        ${ARG_UNPARSED_ARGUMENTS}
192        EXCLUDE_FROM_CHECK_ALL
193        DEPENDS clang clang-resource-headers FileCheck ${ARG_DEPENDS}
194        ARGS ${ARG_ARGS}
195      )
196    else()
197      add_lit_testsuite(${target}
198        ${comment}
199        ${ARG_UNPARSED_ARGUMENTS}
200        DEPENDS clang clang-resource-headers FileCheck ${ARG_DEPENDS}
201        ARGS ${ARG_ARGS}
202      )
203    endif()
204  endif()
205endfunction()
206
207function(construct_check_openmp_target)
208  get_property(OPENMP_LIT_TESTSUITES GLOBAL PROPERTY OPENMP_LIT_TESTSUITES)
209  get_property(OPENMP_LIT_DEPENDS GLOBAL PROPERTY OPENMP_LIT_DEPENDS)
210
211  # We already added the testsuites themselves, no need to do that again.
212  add_openmp_testsuite(check-openmp "Running OpenMP tests" ${OPENMP_LIT_TESTSUITES} EXCLUDE_FROM_CHECK_ALL DEPENDS ${OPENMP_LIT_DEPENDS})
213endfunction()
214