[Tizen] Add CoreCLR tests build dependencies v3.1.3 for armel, arm64
[platform/upstream/coreclr.git] / functions.cmake
1 function(clr_unknown_arch)
2     if (WIN32)
3         message(FATAL_ERROR "Only AMD64, ARM64, ARM and I386 are supported")
4     elseif(CLR_CROSS_COMPONENTS_BUILD)
5         message(FATAL_ERROR "Only AMD64, I386 host are supported for linux cross-architecture component")
6     else()
7         message(FATAL_ERROR "Only AMD64, ARM64 and ARM are supported")
8     endif()
9 endfunction()
10
11 # Build a list of compiler definitions by putting -D in front of each define.
12 function(get_compile_definitions DefinitionName)
13     # Get the current list of definitions
14     get_directory_property(COMPILE_DEFINITIONS_LIST COMPILE_DEFINITIONS)
15
16     foreach(DEFINITION IN LISTS COMPILE_DEFINITIONS_LIST)
17         if (${DEFINITION} MATCHES "^\\$<\\$<CONFIG:([^>]+)>:([^>]+)>$")
18             # The entries that contain generator expressions must have the -D inside of the
19             # expression. So we transform e.g. $<$<CONFIG:Debug>:_DEBUG> to $<$<CONFIG:Debug>:-D_DEBUG>
20             set(DEFINITION "$<$<CONFIG:${CMAKE_MATCH_1}>:-D${CMAKE_MATCH_2}>")
21         else()
22             set(DEFINITION -D${DEFINITION})
23         endif()
24         list(APPEND DEFINITIONS ${DEFINITION})
25     endforeach()
26     set(${DefinitionName} ${DEFINITIONS} PARENT_SCOPE)
27 endfunction(get_compile_definitions)
28
29 # Build a list of include directories
30 function(get_include_directories IncludeDirectories)
31     get_directory_property(dirs INCLUDE_DIRECTORIES)
32     foreach(dir IN LISTS dirs)
33
34       if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
35         list(APPEND INC_DIRECTORIES /I${dir})
36       else()
37         list(APPEND INC_DIRECTORIES -I${dir})
38       endif(CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
39
40     endforeach()
41     set(${IncludeDirectories} ${INC_DIRECTORIES} PARENT_SCOPE)
42 endfunction(get_include_directories)
43
44 # Build a list of include directories for consumption by the assembler
45 function(get_include_directories_asm IncludeDirectories)
46     get_directory_property(dirs INCLUDE_DIRECTORIES)
47
48     if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
49         list(APPEND INC_DIRECTORIES "-I ")
50     endif()
51
52     foreach(dir IN LISTS dirs)
53       if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
54         list(APPEND INC_DIRECTORIES ${dir};)
55       else()
56         list(APPEND INC_DIRECTORIES -I${dir})
57       endif()
58     endforeach()
59
60     set(${IncludeDirectories} ${INC_DIRECTORIES} PARENT_SCOPE)
61 endfunction(get_include_directories_asm)
62
63 # Set the passed in RetSources variable to the list of sources with added current source directory
64 # to form absolute paths.
65 # The parameters after the RetSources are the input files.
66 function(convert_to_absolute_path RetSources)
67     set(Sources ${ARGN})
68     foreach(Source IN LISTS Sources)
69         list(APPEND AbsolutePathSources ${CMAKE_CURRENT_SOURCE_DIR}/${Source})
70     endforeach()
71     set(${RetSources} ${AbsolutePathSources} PARENT_SCOPE)
72 endfunction(convert_to_absolute_path)
73
74 #Preprocess exports definition file
75 function(preprocess_def_file inputFilename outputFilename)
76   get_compile_definitions(PREPROCESS_DEFINITIONS)
77   get_include_directories(ASM_INCLUDE_DIRECTORIES)
78   add_custom_command(
79     OUTPUT ${outputFilename}
80     COMMAND ${CMAKE_CXX_COMPILER} ${ASM_INCLUDE_DIRECTORIES} /P /EP /TC ${PREPROCESS_DEFINITIONS}  /Fi${outputFilename}  ${inputFilename}
81     DEPENDS ${inputFilename}
82     COMMENT "Preprocessing ${inputFilename} - ${CMAKE_CXX_COMPILER} ${ASM_INCLUDE_DIRECTORIES} /P /EP /TC ${PREPROCESS_DEFINITIONS}  /Fi${outputFilename}  ${inputFilename}"
83   )
84
85   set_source_files_properties(${outputFilename}
86                               PROPERTIES GENERATED TRUE)
87 endfunction()
88
89 function(generate_exports_file)
90   set(INPUT_LIST ${ARGN})
91   list(GET INPUT_LIST -1 outputFilename)
92   list(REMOVE_AT INPUT_LIST -1)
93
94   if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
95     set(AWK_SCRIPT generateexportedsymbols.awk)
96   else()
97     set(AWK_SCRIPT generateversionscript.awk)
98   endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
99
100   add_custom_command(
101     OUTPUT ${outputFilename}
102     COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${INPUT_LIST} >${outputFilename}
103     DEPENDS ${INPUT_LIST} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
104     COMMENT "Generating exports file ${outputFilename}"
105   )
106   set_source_files_properties(${outputFilename}
107                               PROPERTIES GENERATED TRUE)
108 endfunction()
109
110 function(generate_exports_file_prefix inputFilename outputFilename prefix)
111
112   if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
113     set(AWK_SCRIPT generateexportedsymbols.awk)
114   else()
115     set(AWK_SCRIPT generateversionscript.awk)
116     if (NOT ${prefix} STREQUAL "")
117         set(AWK_VARS ${AWK_VARS} -v prefix=${prefix})
118     endif()
119   endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
120
121   add_custom_command(
122     OUTPUT ${outputFilename}
123     COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${AWK_VARS} ${inputFilename} >${outputFilename}
124     DEPENDS ${inputFilename} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
125     COMMENT "Generating exports file ${outputFilename}"
126   )
127   set_source_files_properties(${outputFilename}
128                               PROPERTIES GENERATED TRUE)
129 endfunction()
130
131 function(add_precompiled_header header cppFile targetSources)
132   if(MSVC)
133     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
134
135     set_source_files_properties(${cppFile}
136                                 PROPERTIES COMPILE_FLAGS "/Yc\"${header}\" /Fp\"${precompiledBinary}\""
137                                            OBJECT_OUTPUTS "${precompiledBinary}")
138     set_source_files_properties(${${targetSources}}
139                                 PROPERTIES COMPILE_FLAGS "/Yu\"${header}\" /Fp\"${precompiledBinary}\""
140                                            OBJECT_DEPENDS "${precompiledBinary}")
141     # Add cppFile to SourcesVar
142     set(${targetSources} ${${targetSources}} ${cppFile} PARENT_SCOPE)
143   endif(MSVC)
144 endfunction()
145
146 function(strip_symbols targetName outputFilename)
147   if (CLR_CMAKE_PLATFORM_UNIX)
148     if (STRIP_SYMBOLS)
149
150       # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
151       # generator expression doesn't work correctly returning the wrong path and on
152       # the newer cmake versions the LOCATION property isn't supported anymore.
153       if (CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
154           set(strip_source_file $<TARGET_FILE:${targetName}>)
155       else()
156           get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
157       endif()
158
159       if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
160         set(strip_destination_file ${strip_source_file}.dwarf)
161
162         add_custom_command(
163           TARGET ${targetName}
164           POST_BUILD
165           VERBATIM
166           COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
167           COMMAND ${STRIP} -S ${strip_source_file}
168           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
169         )
170       else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
171         set(strip_destination_file ${strip_source_file}.dbg)
172
173         add_custom_command(
174           TARGET ${targetName}
175           POST_BUILD
176           VERBATIM
177           COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
178           COMMAND ${OBJCOPY} --strip-debug ${strip_source_file}
179           COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
180           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
181         )
182       endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
183
184       set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
185     endif (STRIP_SYMBOLS)
186   endif(CLR_CMAKE_PLATFORM_UNIX)
187 endfunction()
188
189 function(install_clr targetName)
190   list(FIND CLR_CROSS_COMPONENTS_LIST ${targetName} INDEX)
191   if (NOT DEFINED CLR_CROSS_COMPONENTS_LIST OR NOT ${INDEX} EQUAL -1)
192     strip_symbols(${targetName} strip_destination_file)
193     # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
194     # generator expression doesn't work correctly returning the wrong path and on
195     # the newer cmake versions the LOCATION property isn't supported anymore.
196     if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
197        set(install_source_file $<TARGET_FILE:${targetName}>)
198     else()
199         get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)
200     endif()
201
202     install(PROGRAMS ${install_source_file} DESTINATION .)
203     if(WIN32)
204         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)
205     else()
206         install(FILES ${strip_destination_file} DESTINATION .)
207     endif()
208     if(CLR_CMAKE_PGO_INSTRUMENT)
209         if(WIN32)
210             install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pgd DESTINATION PGD OPTIONAL)
211         endif()
212     endif()
213   endif()
214 endfunction()
215
216 # Disable PAX mprotect that would prevent JIT and other codegen in coreclr from working.
217 # PAX mprotect prevents:
218 # - changing the executable status of memory pages that were
219 #   not originally created as executable,
220 # - making read-only executable pages writable again,
221 # - creating executable pages from anonymous memory,
222 # - making read-only-after-relocations (RELRO) data pages writable again.
223 function(disable_pax_mprotect targetName)
224   if (NOT PAXCTL STREQUAL "PAXCTL-NOTFOUND")
225     add_custom_command(
226       TARGET ${targetName}
227       POST_BUILD
228       VERBATIM
229       COMMAND ${PAXCTL} -c -m $<TARGET_FILE:${targetName}>
230     )
231   endif()
232 endfunction()
233
234 function(_add_executable)
235     if(NOT WIN32)
236       add_executable(${ARGV} ${VERSION_FILE_PATH})
237       disable_pax_mprotect(${ARGV})
238     else()
239       add_executable(${ARGV})
240     endif(NOT WIN32)
241     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)
242     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)
243      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)
244     endif()
245 endfunction()
246
247 function(_add_library)
248     if(NOT WIN32)
249       add_library(${ARGV} ${VERSION_FILE_PATH})
250     else()
251       add_library(${ARGV})
252     endif(NOT WIN32)
253     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)
254     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)
255      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)
256     endif()
257 endfunction()
258
259 function(_install)
260     if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
261       install(${ARGV})
262     endif()
263 endfunction()
264
265 function(verify_dependencies targetName errorMessage)
266     set(SANITIZER_BUILD OFF)
267
268     if (CLR_CMAKE_PLATFORM_UNIX)
269         if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
270             string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
271             string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
272             if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
273                 set(SANITIZER_BUILD ON)
274             endif()
275         endif()
276     endif()
277
278     # We don't need to verify dependencies on OSX, since missing dependencies
279     # result in link error over there.
280     # Also don't verify dependencies for Asan build because in this case shared
281     # libraries can contain undefined symbols
282     if (NOT CLR_CMAKE_PLATFORM_DARWIN AND NOT CLR_CMAKE_PLATFORM_ANDROID AND NOT SANITIZER_BUILD)
283         add_custom_command(
284             TARGET ${targetName}
285             POST_BUILD
286             VERBATIM
287             COMMAND ${CMAKE_SOURCE_DIR}/verify-so.sh
288                 $<TARGET_FILE:${targetName}>
289                 ${errorMessage}
290             COMMENT "Verifying ${targetName} dependencies"
291         )
292     endif()
293 endfunction()
294
295 function(add_library_clr)
296     _add_library(${ARGV})
297 endfunction()
298
299 function(add_executable_clr)
300     _add_executable(${ARGV})
301 endfunction()