1#
2#//===----------------------------------------------------------------------===//
3#//
4#//                     The LLVM Compiler Infrastructure
5#//
6#// This file is dual licensed under the MIT and the University of Illinois Open
7#// Source Licenses. See LICENSE.txt for details.
8#//
9#//===----------------------------------------------------------------------===//
10#
11
12# The following micro-tests are small tests to perform on the library just created.
13# There are currently five micro-tests:
14# (1) test-touch
15#  - Compile and run a small program using newly created libomp library
16#  - Fails if test-touch.c does not compile or if test-touch.c does not run after compilation
17#  - Program dependencies: gcc or g++, grep, bourne shell
18#  - Available for all Unix,Mac,Windows builds.  Not available on Intel(R) MIC Architecture builds.
19# (2) test-relo
20#  - Tests dynamic libraries for position-dependent code (can not have any position dependent code)
21#  - Fails if TEXTREL is in output of readelf -d libomp.so command
22#  - Program dependencies: readelf, grep, bourne shell
23#  - Available for Unix, Intel(R) MIC Architecture dynamic library builds. Not available otherwise.
24# (3) test-execstack
25#  - Tests if stack is executable
26#  - Fails if stack is executable. Should only be readable and writable. Not exectuable.
27#  - Program dependencies: perl, readelf
28#  - Available for Unix dynamic library builds. Not available otherwise.
29# (4) test-instr (Intel(R) MIC Architecutre only)
30#  - Tests Intel(R) MIC Architecture libraries for valid instruction set
31#  - Fails if finds invalid instruction for Intel(R) MIC Architecture (wasn't compiled with correct flags)
32#  - Program dependencies: perl, objdump
33#  - Available for Intel(R) MIC Architecture and i386 builds. Not available otherwise.
34# (5) test-deps
35#  - Tests newly created libomp for library dependencies
36#  - Fails if sees a dependence not listed in td_exp variable below
37#  - Program dependencies: perl, (unix)readelf, (mac)otool[64], (windows)link.exe
38#  - Available for Unix,Mac,Windows, Intel(R) MIC Architecture dynamic builds and Windows
39#    static builds. Not available otherwise.
40
41# get library location
42if(WIN32)
43  get_target_property(LIBOMP_OUTPUT_DIRECTORY omp RUNTIME_OUTPUT_DIRECTORY)
44  get_target_property(LIBOMPIMP_OUTPUT_DIRECTORY ompimp ARCHIVE_OUTPUT_DIRECTORY)
45  if(NOT LIBOMPIMP_OUTPUT_DIRECTORY)
46    set(LIBOMPIMP_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
47  endif()
48else()
49  get_target_property(LIBOMP_OUTPUT_DIRECTORY omp LIBRARY_OUTPUT_DIRECTORY)
50endif()
51if(NOT LIBOMP_OUTPUT_DIRECTORY)
52  set(LIBOMP_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
53endif()
54
55# test-touch
56find_program(LIBOMP_SHELL sh)
57if(WIN32)
58  if(LIBOMP_SHELL)
59    set(libomp_test_touch_targets test-touch-md/.success test-touch-mt/.success)
60  endif()
61  # pick test-touch compiler
62  set(libomp_test_touch_compiler ${CMAKE_C_COMPILER})
63  # test-touch compilation flags
64  libomp_append(libomp_test_touch_cflags /nologo)
65  libomp_append(libomp_test_touch_libs ${LIBOMPIMP_OUTPUT_DIRECTORY}/${LIBOMP_IMP_LIB_FILE})
66  if(${IA32})
67    libomp_append(libomp_test_touch_ldflags /safeseh)
68  endif()
69else() # (Unix based systems, Intel(R) MIC Architecture, and Mac)
70  if(LIBOMP_SHELL)
71    set(libomp_test_touch_targets test-touch-rt/.success)
72  endif()
73  # pick test-touch compiler
74  if(${LIBOMP_USE_STDCPPLIB})
75    set(libomp_test_touch_compiler ${CMAKE_CXX_COMPILER})
76  else()
77    set(libomp_test_touch_compiler ${CMAKE_C_COMPILER})
78  endif()
79  # test-touch compilation flags
80  libomp_append(libomp_test_touch_libs "${CMAKE_THREAD_LIBS_INIT}")
81  if(${IA32})
82    libomp_append(libomp_test_touch_cflags -m32 LIBOMP_HAVE_M32_FLAG)
83  endif()
84  libomp_append(libomp_test_touch_libs ${LIBOMP_OUTPUT_DIRECTORY}/${LIBOMP_LIB_FILE})
85  libomp_append(libomp_test_touch_libs "${LIBOMP_HWLOC_LIBRARY}" LIBOMP_USE_HWLOC)
86  if(APPLE)
87    set(libomp_test_touch_env "DYLD_LIBRARY_PATH=.:${LIBOMP_OUTPUT_DIRECTORY}:$ENV{DYLD_LIBRARY_PATH}")
88    libomp_append(libomp_test_touch_ldflags "-Wl,-rpath,${LIBOMP_HWLOC_LIBRARY_DIR}" LIBOMP_USE_HWLOC)
89  else()
90    set(libomp_test_touch_env "LD_LIBRARY_PATH=.:${LIBOMP_OUTPUT_DIRECTORY}:$ENV{LD_LIBRARY_PATH}")
91    libomp_append(libomp_test_touch_ldflags "-Wl,-rpath=${LIBOMP_HWLOC_LIBRARY_DIR}" LIBOMP_USE_HWLOC)
92  endif()
93endif()
94macro(libomp_test_touch_recipe test_touch_dir)
95  set(libomp_test_touch_dependencies ${LIBOMP_SRC_DIR}/test-touch.c omp)
96  set(libomp_test_touch_exe ${test_touch_dir}/test-touch${CMAKE_EXECUTABLE_SUFFIX})
97  set(libomp_test_touch_obj ${test_touch_dir}/test-touch${CMAKE_C_OUTPUT_EXTENSION})
98  if(WIN32)
99    if(${RELEASE_BUILD} OR ${RELWITHDEBINFO_BUILD})
100      if(${test_touch_dir} MATCHES "test-touch-mt")
101        libomp_append(libomp_test_touch_cflags /MT)
102      else()
103        libomp_append(libomp_test_touch_cflags /MD)
104      endif()
105    else()
106      if(${test_touch_dir} MATCHES "test-touch-mt")
107        libomp_append(libomp_test_touch_cflags /MTd)
108      else()
109        libomp_append(libomp_test_touch_cflags /MDd)
110      endif()
111    endif()
112    set(libomp_test_touch_out_flags -Fe${libomp_test_touch_exe} -Fo${libomp_test_touch_obj})
113    list(APPEND libomp_test_touch_dependencies ompimp)
114  else()
115    set(libomp_test_touch_out_flags -o ${libomp_test_touch_exe})
116  endif()
117  add_custom_command(
118    OUTPUT  ${test_touch_dir}/.success ${libomp_test_touch_exe} ${libomp_test_touch_obj}
119    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${test_touch_dir}
120    COMMAND ${CMAKE_COMMAND} -E remove -f ${test_touch_dir}/*
121    COMMAND ${libomp_test_touch_compiler} ${libomp_test_touch_out_flags} ${libomp_test_touch_cflags}
122      ${LIBOMP_SRC_DIR}/test-touch.c ${libomp_test_touch_ldflags} ${libomp_test_touch_libs}
123    COMMAND ${LIBOMP_SHELL} -c \"${libomp_test_touch_env} ${libomp_test_touch_exe}\"
124    COMMAND ${CMAKE_COMMAND} -E touch ${test_touch_dir}/.success
125    DEPENDS ${libomp_test_touch_dependencies}
126  )
127endmacro()
128libomp_append(libomp_test_touch_env "KMP_VERSION=1")
129add_custom_target(libomp-test-touch DEPENDS ${libomp_test_touch_targets})
130if(WIN32)
131  libomp_test_touch_recipe(test-touch-mt)
132  libomp_test_touch_recipe(test-touch-md)
133else()
134  libomp_test_touch_recipe(test-touch-rt)
135endif()
136
137# test-relo
138add_custom_target(libomp-test-relo DEPENDS test-relo/.success)
139add_custom_command(
140  OUTPUT  test-relo/.success test-relo/readelf.log
141  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/test-relo
142  COMMAND readelf -d ${LIBOMP_OUTPUT_DIRECTORY}/${LIBOMP_LIB_FILE} > test-relo/readelf.log
143  COMMAND grep -e TEXTREL test-relo/readelf.log \; test $$? -eq 1
144  COMMAND ${CMAKE_COMMAND} -E touch test-relo/.success
145  DEPENDS omp
146)
147
148# test-execstack
149add_custom_target(libomp-test-execstack DEPENDS test-execstack/.success)
150add_custom_command(
151  OUTPUT  test-execstack/.success
152  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/test-execstack
153  COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/check-execstack.pl
154    --arch=${LIBOMP_PERL_SCRIPT_ARCH} ${LIBOMP_OUTPUT_DIRECTORY}/${LIBOMP_LIB_FILE}
155  COMMAND ${CMAKE_COMMAND} -E touch test-execstack/.success
156  DEPENDS omp
157)
158
159# test-instr
160add_custom_target(libomp-test-instr DEPENDS test-instr/.success)
161add_custom_command(
162  OUTPUT  test-instr/.success
163  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/test-instr
164  COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/check-instruction-set.pl --os=${LIBOMP_PERL_SCRIPT_OS}
165    --arch=${LIBOMP_PERL_SCRIPT_ARCH} --show --mic-arch=${LIBOMP_MIC_ARCH} ${LIBOMP_OUTPUT_DIRECTORY}/${LIBOMP_LIB_FILE}
166  COMMAND ${CMAKE_COMMAND} -E touch test-instr/.success
167  DEPENDS omp ${LIBOMP_TOOLS_DIR}/check-instruction-set.pl
168)
169
170# test-deps
171add_custom_target(libomp-test-deps DEPENDS test-deps/.success)
172set(libomp_expected_library_deps)
173if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
174  set(libomp_expected_library_deps libc.so.7 libthr.so.3)
175  libomp_append(libomp_expected_library_deps libhwloc.so.5 LIBOMP_USE_HWLOC)
176elseif(CMAKE_SYSTEM_NAME MATCHES "NetBSD")
177  set(libomp_expected_library_deps libc.so.12 libpthread.so.1 libm.so.0)
178  libomp_append(libomp_expected_library_deps libhwloc.so.5 LIBOMP_USE_HWLOC)
179elseif(APPLE)
180  set(libomp_expected_library_deps /usr/lib/libSystem.B.dylib)
181elseif(WIN32)
182  set(libomp_expected_library_deps kernel32.dll)
183  libomp_append(libomp_expected_library_deps psapi.dll LIBOMP_OMPT_SUPPORT)
184else()
185  if(${MIC})
186    set(libomp_expected_library_deps libc.so.6 libpthread.so.0 libdl.so.2)
187    if("${LIBOMP_MIC_ARCH}" STREQUAL "knf")
188      libomp_append(libomp_expected_library_deps ld-linux-l1om.so.2)
189      libomp_append(libomp_expected_library_deps libgcc_s.so.1)
190    elseif("${LIBOMP_MIC_ARCH}" STREQUAL "knc")
191      libomp_append(libomp_expected_library_deps ld-linux-k1om.so.2)
192    endif()
193  else()
194    set(libomp_expected_library_deps libdl.so.2 libgcc_s.so.1)
195    if(${IA32})
196      libomp_append(libomp_expected_library_deps libc.so.6)
197      libomp_append(libomp_expected_library_deps ld-linux.so.2)
198    elseif(${INTEL64})
199      libomp_append(libomp_expected_library_deps libc.so.6)
200      libomp_append(libomp_expected_library_deps ld-linux-x86-64.so.2)
201    elseif(${ARM})
202      libomp_append(libomp_expected_library_deps libc.so.6)
203      libomp_append(libomp_expected_library_deps libffi.so.6)
204      libomp_append(libomp_expected_library_deps libffi.so.5)
205      libomp_append(libomp_expected_library_deps ld-linux-armhf.so.3)
206    elseif(${PPC64})
207      libomp_append(libomp_expected_library_deps libc.so.6)
208      libomp_append(libomp_expected_library_deps ld64.so.1)
209    elseif(${MIPS} OR ${MIPS64})
210      libomp_append(libomp_expected_library_deps libc.so.6)
211      libomp_append(libomp_expected_library_deps ld.so.1)
212    endif()
213    libomp_append(libomp_expected_library_deps libpthread.so.0 IF_FALSE STUBS_LIBRARY)
214    libomp_append(libomp_expected_library_deps libhwloc.so.5 LIBOMP_USE_HWLOC)
215  endif()
216  libomp_append(libomp_expected_library_deps libstdc++.so.6 LIBOMP_USE_STDCPPLIB)
217  libomp_append(libomp_expected_library_deps libm.so.6 LIBOMP_STATS)
218endif()
219# Perl script expects comma separated list
220string(REPLACE ";" "," libomp_expected_library_deps "${libomp_expected_library_deps}")
221add_custom_command(
222  OUTPUT  test-deps/.success
223  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/test-deps
224  COMMAND ${PERL_EXECUTABLE} ${LIBOMP_TOOLS_DIR}/check-depends.pl --os=${LIBOMP_PERL_SCRIPT_OS}
225    --arch=${LIBOMP_PERL_SCRIPT_ARCH} --expected="${libomp_expected_library_deps}" ${LIBOMP_OUTPUT_DIRECTORY}/${LIBOMP_LIB_FILE}
226  COMMAND ${CMAKE_COMMAND} -E touch test-deps/.success
227  DEPENDS omp ${LIBOMP_TOOLS_DIR}/check-depends.pl
228)
229