Merge pull request #11187 from wtgodbe/NonWindowsR2R
[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 inputFilename outputFilename)
90
91   if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
92     set(AWK_SCRIPT generateexportedsymbols.awk)
93   else()
94     set(AWK_SCRIPT generateversionscript.awk)
95   endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
96
97   add_custom_command(
98     OUTPUT ${outputFilename}
99     COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${inputFilename} >${outputFilename}
100     DEPENDS ${inputFilename} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
101     COMMENT "Generating exports file ${outputFilename}"
102   )
103   set_source_files_properties(${outputFilename}
104                               PROPERTIES GENERATED TRUE)
105 endfunction()
106
107 function(add_precompiled_header header cppFile targetSources)
108   if(MSVC)
109     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
110
111     set_source_files_properties(${cppFile}
112                                 PROPERTIES COMPILE_FLAGS "/Yc\"${header}\" /Fp\"${precompiledBinary}\""
113                                            OBJECT_OUTPUTS "${precompiledBinary}")
114     set_source_files_properties(${${targetSources}}
115                                 PROPERTIES COMPILE_FLAGS "/Yu\"${header}\" /Fp\"${precompiledBinary}\""
116                                            OBJECT_DEPENDS "${precompiledBinary}")
117     # Add cppFile to SourcesVar
118     set(${targetSources} ${${targetSources}} ${cppFile} PARENT_SCOPE)
119   endif(MSVC)
120 endfunction()
121
122 function(strip_symbols targetName outputFilename)
123   if (CLR_CMAKE_PLATFORM_UNIX)
124     if (STRIP_SYMBOLS)
125
126       # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
127       # generator expression doesn't work correctly returning the wrong path and on
128       # the newer cmake versions the LOCATION property isn't supported anymore.
129       if (CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
130           set(strip_source_file $<TARGET_FILE:${targetName}>)
131       else()
132           get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
133       endif()
134
135       if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
136         set(strip_destination_file ${strip_source_file}.dwarf)
137
138         add_custom_command(
139           TARGET ${targetName}
140           POST_BUILD
141           VERBATIM 
142           COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
143           COMMAND ${STRIP} -S ${strip_source_file}
144           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
145         )
146       else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
147         set(strip_destination_file ${strip_source_file}.dbg)
148
149         add_custom_command(
150           TARGET ${targetName}
151           POST_BUILD
152           VERBATIM 
153           COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
154           COMMAND ${OBJCOPY} --strip-debug ${strip_source_file}
155           COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
156           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
157         )
158       endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
159
160       set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
161     endif (STRIP_SYMBOLS)
162   endif(CLR_CMAKE_PLATFORM_UNIX)
163 endfunction()
164
165 function(install_clr targetName)
166   list(FIND CLR_CROSS_COMPONENTS_LIST ${targetName} INDEX)  
167   if (NOT DEFINED CLR_CROSS_COMPONENTS_LIST OR NOT ${INDEX} EQUAL -1)  
168     strip_symbols(${targetName} strip_destination_file) 
169     # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE  
170     # generator expression doesn't work correctly returning the wrong path and on  
171     # the newer cmake versions the LOCATION property isn't supported anymore.  
172     if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)  
173        set(install_source_file $<TARGET_FILE:${targetName}>)  
174     else()  
175         get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)  
176     endif()  
177   
178     install(PROGRAMS ${install_source_file} DESTINATION .)  
179     if(WIN32)  
180         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)  
181     else()  
182         install(FILES ${strip_destination_file} DESTINATION .)  
183     endif()  
184     if(CLR_CMAKE_PGO_INSTRUMENT)
185         if(WIN32)
186             install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pgd DESTINATION PGD OPTIONAL)
187         endif()
188     endif()
189   endif()  
190 endfunction()
191
192 function(_add_executable)
193     if(NOT WIN32)
194       add_executable(${ARGV} ${VERSION_FILE_PATH})
195     else()
196       add_executable(${ARGV})
197     endif(NOT WIN32)
198     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
199     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
200      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
201     endif()
202 endfunction()  
203
204 function(_add_library)
205     if(NOT WIN32)
206       add_library(${ARGV} ${VERSION_FILE_PATH})
207     else()
208       add_library(${ARGV})
209     endif(NOT WIN32)
210     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
211     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
212      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
213     endif()  
214 endfunction()
215
216 function(_install)
217     if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
218       install(${ARGV})
219     endif()
220 endfunction()
221
222 function(verify_dependencies targetName errorMessage)
223     # We don't need to verify dependencies on OSX, since missing dependencies
224     # result in link error over there.
225     if (NOT CLR_CMAKE_PLATFORM_DARWIN AND NOT CLR_CMAKE_PLATFORM_ANDROID)
226         add_custom_command(
227             TARGET ${targetName}
228             POST_BUILD
229             VERBATIM
230             COMMAND ${CMAKE_SOURCE_DIR}/verify-so.sh 
231                 $<TARGET_FILE:${targetName}> 
232                 ${errorMessage}
233             COMMENT "Verifying ${targetName} dependencies"
234         )
235     endif()
236 endfunction()