1410eae78bcb5bcbc1a7250ed79c474d1a03120b
[platform/upstream/cmake.git] / Tests / RunCMake / ParseImplicitLinkInfo / ParseImplicitLinkInfo.cmake
1 cmake_minimum_required(VERSION 3.14)
2 project(Minimal NONE)
3
4 #
5 # list of targets to test.  to add a target: put its files in the data
6 # subdirectory and add it to this list...  we run each target's
7 # data/*.input file through the parser and check to see if it matches
8 # the corresponding data/*.output file.  note that the empty-* case
9 # has special handling (it should not parse).
10 #
11 set(targets
12   aix-C-XL-13.1.3 aix-CXX-XL-13.1.3
13   aix-C-XLClang-16.1.0.1 aix-CXX-XLClang-16.1.0.1
14   craype-C-Cray-8.7 craype-CXX-Cray-8.7 craype-Fortran-Cray-8.7
15   craype-C-Cray-9.0-hlist-ad craype-CXX-Cray-9.0-hlist-ad craype-Fortran-Cray-9.0-hlist-ad
16   craype-C-GNU-7.3.0 craype-CXX-GNU-7.3.0 craype-Fortran-GNU-7.3.0
17   craype-C-Intel-18.0.2.20180210 craype-CXX-Intel-18.0.2.20180210
18     craype-Fortran-Intel-18.0.2.20180210
19   darwin-C-AppleClang-8.0.0.8000042 darwin-CXX-AppleClang-8.0.0.8000042
20     darwin_nostdinc-C-AppleClang-8.0.0.8000042
21     darwin_nostdinc-CXX-AppleClang-8.0.0.8000042
22   freebsd-C-Clang-3.3.0 freebsd-CXX-Clang-3.3.0 freebsd-Fortran-GNU-4.6.4
23   hand-C-empty hand-CXX-empty
24   hand-C-relative hand-CXX-relative
25   linux-C-GNU-7.3.0 linux-CXX-GNU-7.3.0 linux-Fortran-GNU-7.3.0
26   linux-C-GNU-10.2.1-static-libgcc
27     linux-CXX-GNU-10.2.1-static-libstdc++
28     linux-Fortran-GNU-10.2.1-static-libgfortran
29   linux-C-Intel-18.0.0.20170811 linux-CXX-Intel-18.0.0.20170811
30   linux-C-PGI-18.10.1 linux-CXX-PGI-18.10.1
31     linux-Fortran-PGI-18.10.1 linux_pgf77-Fortran-PGI-18.10.1
32     linux_nostdinc-C-PGI-18.10.1 linux_nostdinc-CXX-PGI-18.10.1
33     linux_nostdinc-Fortran-PGI-18.10.1
34   linux-C-NVHPC-21.1.0 linux-CXX-NVHPC-21.1.0
35   linux-C-XL-12.1.0 linux-CXX-XL-12.1.0 linux-Fortran-XL-14.1.0
36     linux_nostdinc-C-XL-12.1.0 linux_nostdinc-CXX-XL-12.1.0
37     linux_nostdinc_i-C-XL-12.1.0 linux_nostdinc-CXX-XL-12.1.0
38   linux-C-XL-16.1.0.0 linux-CXX-XL-16.1.0.0
39   linux-CUDA-NVIDIA-10.1.168-CLANG linux-CUDA-NVIDIA-10.1.168-XLClang-v
40     linux-CUDA-NVIDIA-9.2.148-GCC
41   linux-Fortran-LLVMFlang-15.0.0
42   linux-custom_clang-C-Clang-13.0.0 linux-custom_clang-CXX-Clang-13.0.0
43   mingw.org-C-GNU-4.9.3 mingw.org-CXX-GNU-4.9.3
44   netbsd-C-GNU-4.8.5 netbsd-CXX-GNU-4.8.5
45     netbsd_nostdinc-C-GNU-4.8.5 netbsd_nostdinc-CXX-GNU-4.8.5
46   openbsd-C-Clang-5.0.1 openbsd-CXX-Clang-5.0.1
47   sunos-C-SunPro-5.13.0 sunos-CXX-SunPro-5.13.0 sunos-Fortran-SunPro-8.8.0
48   )
49
50 if(CMAKE_HOST_WIN32)
51   # The KWSys actual-case cache breaks case sensitivity on Windows.
52   list(FILTER targets EXCLUDE REGEX "-XL|-SunPro")
53 else()
54   # Windows drive letters are not recognized as absolute on other platforms.
55   list(FILTER targets EXCLUDE REGEX "mingw")
56 endif()
57
58 include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
59 include(${CMAKE_ROOT}/Modules/CMakeParseLibraryArchitecture.cmake)
60
61 #
62 # load_compiler_info: read infile, parsing out cmake compiler info
63 # variables as we go.  returns language, a list of variables we set
64 # (so we can clear them later), and the remaining verbose output
65 # from the compiler.
66 #
67 function(load_compiler_info infile lang_var outcmvars_var outstr_var)
68   unset(lang)
69   unset(outcmvars)
70   unset(outstr)
71   file(READ "${infile}" in)
72   string(REGEX REPLACE "\r?\n" ";" in_lines "${in}")
73   foreach(line IN LISTS in_lines)
74     # check for special CMAKE variable lines and parse them if found
75     if("${line}" MATCHES "^CMAKE_([_A-Za-z0-9+]+)=(.*)$")
76       if("${CMAKE_MATCH_1}" STREQUAL "LANG")   # handle CMAKE_LANG here
77         set(lang "${CMAKE_MATCH_2}")
78       else()
79         set(CMAKE_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" PARENT_SCOPE)
80         list(APPEND outcmvars "CMAKE_${CMAKE_MATCH_1}")
81       endif()
82     else()
83       string(APPEND outstr "${line}\n")
84     endif()
85   endforeach()
86   if(NOT lang)
87     message("load_compiler_info: ${infile} no LANG info; default to C")
88     set(lang C)
89   endif()
90
91   set(${lang_var} "${lang}" PARENT_SCOPE)
92   set(${outcmvars_var} "${outcmvars}" PARENT_SCOPE)
93   set(${outstr_var} "${outstr}" PARENT_SCOPE)
94 endfunction()
95
96 #
97 # unload_compiler_info: clear out any CMAKE_* vars load previously set
98 #
99 function(unload_compiler_info cmvars)
100   foreach(var IN LISTS cmvars)
101     unset("${var}" PARENT_SCOPE)
102   endforeach()
103 endfunction()
104
105
106 #
107 # load_platform_info: establish CMAKE_LIBRARY_ARCHITECTURE_REGEX
108 # based on the target platform.
109 #
110 function(load_platform_info target)
111   if(target MATCHES "linux-")
112     set(CMAKE_LIBRARY_ARCHITECTURE_REGEX "[a-z0-9_]+(-[a-z0-9_]+)?-linux-gnu[a-z0-9_]*" PARENT_SCOPE)
113   else()
114     unset(CMAKE_LIBRARY_ARCHITECTURE_REGEX PARENT_SCOPE)
115   endif()
116 endfunction()
117
118 #
119 # main test loop
120 #
121 foreach(t ${targets})
122   set(infile "${CMAKE_SOURCE_DIR}/../ParseImplicitData/${t}.input")
123   set(outfile "${CMAKE_SOURCE_DIR}/results/${t}.output")
124   if (NOT EXISTS ${infile})
125     string(REPLACE  "-empty" "" infile "${infile}")
126     if (NOT EXISTS ${infile})
127       message("missing input file for target ${t} in ${CMAKE_SOURCE_DIR}/../ParseImplicitData/")
128       continue()
129     endif()
130   elseif(NOT EXISTS ${outfile})
131     message("missing files for target ${t} in ${CMAKE_SOURCE_DIR}/results/")
132     continue()
133   endif()
134
135   load_compiler_info(${infile} lang cmvars input)
136   load_platform_info(${t})
137
138   # Need to handle files with empty entries for both libs or dirs
139   set(implicit_lib_output "")
140   set(idirs_output "")
141   set(implicit_objs "")
142   set(library_arch_output "")
143   file(STRINGS ${outfile} outputs)
144   foreach(line IN LISTS outputs)
145     if(line MATCHES "libs=")
146       string(REPLACE "libs=" "" implicit_lib_output "${line}")
147     endif()
148     if(line MATCHES "dirs=")
149       string(REPLACE "dirs=" "" idirs_output "${line}")
150     endif()
151     if(line MATCHES "library_arch=")
152       string(REPLACE "library_arch=" "" library_arch_output "${line}")
153     endif()
154   endforeach()
155
156   cmake_parse_implicit_link_info("${input}" implicit_libs idirs implicit_fwks log
157       "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}"
158       COMPUTE_IMPLICIT_OBJECTS implicit_objs)
159
160   set(library_arch)
161   cmake_parse_library_architecture(${lang} "${idirs}" "${implicit_objs}" library_arch)
162
163   # File format
164   # file(WRITE ${outfile} "libs=${implicit_libs}\ndirs=${idirs}\nlibrary_arch=${library_arch}")
165
166   if(t MATCHES "-empty$")          # empty isn't supposed to parse
167     if("${state}" STREQUAL "done")
168       message("empty parse failed: ${idirs}, log=${log}")
169     endif()
170   elseif(NOT "${idirs}" MATCHES "^${idirs_output}$")
171     message("${t} parse failed: state=${state}, '${idirs}' does not match '^${idirs_output}$'")
172   elseif(NOT "${implicit_libs}" MATCHES "^${implicit_lib_output}$")
173     message("${t} parse failed: state=${state}, '${implicit_libs}' does not match '^${implicit_lib_output}$'")
174   elseif((library_arch OR library_arch_output) AND NOT "${library_arch}" MATCHES "^${library_arch_output}$")
175     message("${t} parse failed: state=${state}, '${library_arch}' does not match '^${library_arch_output}$'")
176   endif()
177
178   unload_compiler_info("${cmvars}")
179 endforeach(t)