Add the missing ICU APIs to the ICU shim (#7840)
[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     else()
5         message(FATAL_ERROR "Only AMD64, ARM64 and ARM are supported")
6     endif()
7 endfunction()
8
9 # Build a list of compiler definitions by putting -D in front of each define.
10 function(get_compile_definitions DefinitionName)
11     # Get the current list of definitions
12     get_directory_property(COMPILE_DEFINITIONS_LIST COMPILE_DEFINITIONS)
13
14     foreach(DEFINITION IN LISTS COMPILE_DEFINITIONS_LIST)
15         if (${DEFINITION} MATCHES "^\\$<\\$<CONFIG:([^>]+)>:([^>]+)>$")
16             # The entries that contain generator expressions must have the -D inside of the
17             # expression. So we transform e.g. $<$<CONFIG:Debug>:_DEBUG> to $<$<CONFIG:Debug>:-D_DEBUG>
18             set(DEFINITION "$<$<CONFIG:${CMAKE_MATCH_1}>:-D${CMAKE_MATCH_2}>")
19         else()
20             set(DEFINITION -D${DEFINITION})
21         endif()
22         list(APPEND DEFINITIONS ${DEFINITION})
23     endforeach()
24     set(${DefinitionName} ${DEFINITIONS} PARENT_SCOPE)
25 endfunction(get_compile_definitions)
26
27 # Build a list of include directories
28 function(get_include_directories IncludeDirectories)
29     get_directory_property(dirs INCLUDE_DIRECTORIES)
30     foreach(dir IN LISTS dirs)
31
32       if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
33         list(APPEND INC_DIRECTORIES /I${dir})
34       else()
35         list(APPEND INC_DIRECTORIES -I${dir})
36       endif(CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
37
38     endforeach()
39     set(${IncludeDirectories} ${INC_DIRECTORIES} PARENT_SCOPE)
40 endfunction(get_include_directories)
41
42 # Build a list of include directories for consumption by the assembler
43 function(get_include_directories_asm IncludeDirectories)
44     get_directory_property(dirs INCLUDE_DIRECTORIES)
45     
46     if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
47         list(APPEND INC_DIRECTORIES "-I ")
48     endif()
49     
50     foreach(dir IN LISTS dirs)
51       if (CLR_CMAKE_PLATFORM_ARCH_ARM AND WIN32)
52         list(APPEND INC_DIRECTORIES ${dir};)
53       else()
54         list(APPEND INC_DIRECTORIES -I${dir})
55       endif()
56     endforeach()
57
58     set(${IncludeDirectories} ${INC_DIRECTORIES} PARENT_SCOPE)
59 endfunction(get_include_directories_asm)
60
61 # Set the passed in RetSources variable to the list of sources with added current source directory
62 # to form absolute paths.
63 # The parameters after the RetSources are the input files.
64 function(convert_to_absolute_path RetSources)
65     set(Sources ${ARGN})
66     foreach(Source IN LISTS Sources)
67         list(APPEND AbsolutePathSources ${CMAKE_CURRENT_SOURCE_DIR}/${Source})
68     endforeach()
69     set(${RetSources} ${AbsolutePathSources} PARENT_SCOPE)
70 endfunction(convert_to_absolute_path)
71
72 #Preprocess exports definition file
73 function(preprocess_def_file inputFilename outputFilename)
74   get_compile_definitions(PREPROCESS_DEFINITIONS)
75   get_include_directories(ASM_INCLUDE_DIRECTORIES)
76   add_custom_command(
77     OUTPUT ${outputFilename}
78     COMMAND ${CMAKE_CXX_COMPILER} ${ASM_INCLUDE_DIRECTORIES} /P /EP /TC ${PREPROCESS_DEFINITIONS}  /Fi${outputFilename}  ${inputFilename}
79     DEPENDS ${inputFilename}
80     COMMENT "Preprocessing ${inputFilename} - ${CMAKE_CXX_COMPILER} ${ASM_INCLUDE_DIRECTORIES} /P /EP /TC ${PREPROCESS_DEFINITIONS}  /Fi${outputFilename}  ${inputFilename}"
81   )
82
83   set_source_files_properties(${outputFilename}
84                               PROPERTIES GENERATED TRUE)
85 endfunction()
86
87 function(generate_exports_file inputFilename outputFilename)
88
89   if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
90     set(AWK_SCRIPT generateexportedsymbols.awk)
91   else()
92     set(AWK_SCRIPT generateversionscript.awk)
93   endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
94
95   add_custom_command(
96     OUTPUT ${outputFilename}
97     COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${inputFilename} >${outputFilename}
98     DEPENDS ${inputFilename} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
99     COMMENT "Generating exports file ${outputFilename}"
100   )
101   set_source_files_properties(${outputFilename}
102                               PROPERTIES GENERATED TRUE)
103 endfunction()
104
105 function(add_precompiled_header header cppFile targetSources)
106   if(MSVC)
107     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
108
109     set_source_files_properties(${cppFile}
110                                 PROPERTIES COMPILE_FLAGS "/Yc\"${header}\" /Fp\"${precompiledBinary}\""
111                                            OBJECT_OUTPUTS "${precompiledBinary}")
112     set_source_files_properties(${${targetSources}}
113                                 PROPERTIES COMPILE_FLAGS "/Yu\"${header}\" /Fp\"${precompiledBinary}\""
114                                            OBJECT_DEPENDS "${precompiledBinary}")
115     # Add cppFile to SourcesVar
116     set(${targetSources} ${${targetSources}} ${cppFile} PARENT_SCOPE)
117   endif(MSVC)
118 endfunction()
119
120 function(strip_symbols targetName outputFilename)
121   if (CLR_CMAKE_PLATFORM_UNIX)
122     if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
123
124       # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
125       # generator expression doesn't work correctly returning the wrong path and on
126       # the newer cmake versions the LOCATION property isn't supported anymore.
127       if (CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
128           set(strip_source_file $<TARGET_FILE:${targetName}>)
129       else()
130           get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
131       endif()
132
133       if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
134         set(strip_destination_file ${strip_source_file}.dwarf)
135
136         add_custom_command(
137           TARGET ${targetName}
138           POST_BUILD
139           VERBATIM 
140           COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
141           COMMAND ${STRIP} -S ${strip_source_file}
142           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
143         )
144       else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
145         set(strip_destination_file ${strip_source_file}.dbg)
146
147         add_custom_command(
148           TARGET ${targetName}
149           POST_BUILD
150           VERBATIM 
151           COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
152           COMMAND ${OBJCOPY} --strip-debug ${strip_source_file}
153           COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
154           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
155         )
156       endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
157
158       set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
159     endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
160   endif(CLR_CMAKE_PLATFORM_UNIX)
161 endfunction()
162
163 function(install_clr targetName)
164   list(FIND CLR_CROSS_COMPONENTS_LIST ${targetName} INDEX)  
165   if (NOT DEFINED CLR_CROSS_COMPONENTS_LIST OR NOT ${INDEX} EQUAL -1)  
166     strip_symbols(${targetName} strip_destination_file) 
167     # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE  
168     # generator expression doesn't work correctly returning the wrong path and on  
169     # the newer cmake versions the LOCATION property isn't supported anymore.  
170     if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)  
171        set(install_source_file $<TARGET_FILE:${targetName}>)  
172     else()  
173         get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)  
174     endif()  
175   
176     install(PROGRAMS ${install_source_file} DESTINATION .)  
177     if(WIN32)  
178         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)  
179     else()  
180         install(FILES ${strip_destination_file} DESTINATION .)  
181     endif()  
182     if(CLR_CMAKE_PGO_INSTRUMENT)
183         if(WIN32)
184             install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pgd DESTINATION PGD OPTIONAL)
185         endif()
186     endif()
187   endif()  
188 endfunction()
189
190 function(_add_executable)
191     if(NOT WIN32)
192       add_executable(${ARGV} ${VERSION_FILE_PATH})
193     else()
194       add_executable(${ARGV})
195     endif(NOT WIN32)
196     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
197     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
198      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
199     endif()
200 endfunction()  
201
202 function(_add_library)
203     if(NOT WIN32)
204       add_library(${ARGV} ${VERSION_FILE_PATH})
205     else()
206       add_library(${ARGV})
207     endif(NOT WIN32)
208     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
209     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
210      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
211     endif()  
212 endfunction()
213
214 function(_install)
215     if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
216       install(${ARGV})
217     endif()
218 endfunction()