EventPipe: Don't Generate and Emit Stacks on Rundown (#16083)
[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(add_precompiled_header header cppFile targetSources)
111   if(MSVC)
112     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
113
114     set_source_files_properties(${cppFile}
115                                 PROPERTIES COMPILE_FLAGS "/Yc\"${header}\" /Fp\"${precompiledBinary}\""
116                                            OBJECT_OUTPUTS "${precompiledBinary}")
117     set_source_files_properties(${${targetSources}}
118                                 PROPERTIES COMPILE_FLAGS "/Yu\"${header}\" /Fp\"${precompiledBinary}\""
119                                            OBJECT_DEPENDS "${precompiledBinary}")
120     # Add cppFile to SourcesVar
121     set(${targetSources} ${${targetSources}} ${cppFile} PARENT_SCOPE)
122   endif(MSVC)
123 endfunction()
124
125 function(strip_symbols targetName outputFilename)
126   if (CLR_CMAKE_PLATFORM_UNIX)
127     if (STRIP_SYMBOLS)
128
129       # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
130       # generator expression doesn't work correctly returning the wrong path and on
131       # the newer cmake versions the LOCATION property isn't supported anymore.
132       if (CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
133           set(strip_source_file $<TARGET_FILE:${targetName}>)
134       else()
135           get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
136       endif()
137
138       if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
139         set(strip_destination_file ${strip_source_file}.dwarf)
140
141         add_custom_command(
142           TARGET ${targetName}
143           POST_BUILD
144           VERBATIM
145           COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
146           COMMAND ${STRIP} -S ${strip_source_file}
147           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
148         )
149       else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
150         set(strip_destination_file ${strip_source_file}.dbg)
151
152         add_custom_command(
153           TARGET ${targetName}
154           POST_BUILD
155           VERBATIM
156           COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
157           COMMAND ${OBJCOPY} --strip-debug ${strip_source_file}
158           COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
159           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
160         )
161       endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
162
163       set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
164     endif (STRIP_SYMBOLS)
165   endif(CLR_CMAKE_PLATFORM_UNIX)
166 endfunction()
167
168 function(install_clr targetName)
169   list(FIND CLR_CROSS_COMPONENTS_LIST ${targetName} INDEX)
170   if (NOT DEFINED CLR_CROSS_COMPONENTS_LIST OR NOT ${INDEX} EQUAL -1)
171     strip_symbols(${targetName} strip_destination_file)
172     # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
173     # generator expression doesn't work correctly returning the wrong path and on
174     # the newer cmake versions the LOCATION property isn't supported anymore.
175     if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
176        set(install_source_file $<TARGET_FILE:${targetName}>)
177     else()
178         get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)
179     endif()
180
181     install(PROGRAMS ${install_source_file} DESTINATION .)
182     if(WIN32)
183         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)
184     else()
185         install(FILES ${strip_destination_file} DESTINATION .)
186     endif()
187     if(CLR_CMAKE_PGO_INSTRUMENT)
188         if(WIN32)
189             install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pgd DESTINATION PGD OPTIONAL)
190         endif()
191     endif()
192   endif()
193 endfunction()
194
195 # Disable PAX mprotect that would prevent JIT and other codegen in coreclr from working.
196 # PAX mprotect prevents:
197 # - changing the executable status of memory pages that were
198 #   not originally created as executable,
199 # - making read-only executable pages writable again,
200 # - creating executable pages from anonymous memory,
201 # - making read-only-after-relocations (RELRO) data pages writable again.
202 function(disable_pax_mprotect targetName)
203   if (NOT PAXCTL STREQUAL "PAXCTL-NOTFOUND")
204     add_custom_command(
205       TARGET ${targetName}
206       POST_BUILD
207       VERBATIM
208       COMMAND ${PAXCTL} -c -m $<TARGET_FILE:${targetName}>
209     )
210   endif()
211 endfunction()
212
213 function(_add_executable)
214     if(NOT WIN32)
215       add_executable(${ARGV} ${VERSION_FILE_PATH})
216       disable_pax_mprotect(${ARGV})
217     else()
218       add_executable(${ARGV})
219     endif(NOT WIN32)
220     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)
221     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)
222      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)
223     endif()
224 endfunction()
225
226 function(_add_library)
227     if(NOT WIN32)
228       add_library(${ARGV} ${VERSION_FILE_PATH})
229     else()
230       add_library(${ARGV})
231     endif(NOT WIN32)
232     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)
233     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)
234      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)
235     endif()
236 endfunction()
237
238 function(_install)
239     if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
240       install(${ARGV})
241     endif()
242 endfunction()
243
244 function(verify_dependencies targetName errorMessage)
245     set(SANITIZER_BUILD OFF)
246
247     if (CLR_CMAKE_PLATFORM_UNIX)
248         if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
249             string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
250             string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
251             if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
252                 set(SANITIZER_BUILD ON)
253             endif()
254         endif()
255     endif()
256
257     # We don't need to verify dependencies on OSX, since missing dependencies
258     # result in link error over there.
259     # Also don't verify dependencies for Asan build because in this case shared
260     # libraries can contain undefined symbols
261     if (NOT CLR_CMAKE_PLATFORM_DARWIN AND NOT CLR_CMAKE_PLATFORM_ANDROID AND NOT SANITIZER_BUILD)
262         add_custom_command(
263             TARGET ${targetName}
264             POST_BUILD
265             VERBATIM
266             COMMAND ${CMAKE_SOURCE_DIR}/verify-so.sh
267                 $<TARGET_FILE:${targetName}>
268                 ${errorMessage}
269             COMMENT "Verifying ${targetName} dependencies"
270         )
271     endif()
272 endfunction()
273
274 function(add_library_clr)
275     _add_library(${ARGV})
276 endfunction()
277
278 function(add_executable_clr)
279     _add_executable(${ARGV})
280 endfunction()