Merge pull request #6338 from swgillespie/revert-weakreference-tests
[platform/upstream/coreclr.git] / functions.cmake
1 function(clr_unknown_arch)
2     if (WIN32)
3         message(FATAL_ERROR "Only AMD64, ARM64 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 by putting -I in front of each include dir.
28 function(get_include_directories IncludeDirectories)
29     get_directory_property(dirs INCLUDE_DIRECTORIES)
30     foreach(dir IN LISTS dirs)
31         list(APPEND INC_DIRECTORIES -I${dir})
32     endforeach()
33     set(${IncludeDirectories} ${INC_DIRECTORIES} PARENT_SCOPE)
34 endfunction(get_include_directories)
35
36 # Set the passed in RetSources variable to the list of sources with added current source directory
37 # to form absolute paths.
38 # The parameters after the RetSources are the input files.
39 function(convert_to_absolute_path RetSources)
40     set(Sources ${ARGN})
41     foreach(Source IN LISTS Sources)
42         list(APPEND AbsolutePathSources ${CMAKE_CURRENT_SOURCE_DIR}/${Source})
43     endforeach()
44     set(${RetSources} ${AbsolutePathSources} PARENT_SCOPE)
45 endfunction(convert_to_absolute_path)
46
47 #Preprocess exports definition file
48 function(preprocess_def_file inputFilename outputFilename)
49   get_compile_definitions(PREPROCESS_DEFINITIONS)
50
51   add_custom_command(
52     OUTPUT ${outputFilename}
53     COMMAND ${CMAKE_CXX_COMPILER} /P /EP /TC ${PREPROCESS_DEFINITIONS}  /Fi${outputFilename}  ${inputFilename}
54     DEPENDS ${inputFilename}
55     COMMENT "Preprocessing ${inputFilename}"
56   )
57
58   set_source_files_properties(${outputFilename}
59                               PROPERTIES GENERATED TRUE)
60 endfunction()
61
62 function(generate_exports_file inputFilename outputFilename)
63
64   if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
65     set(AWK_SCRIPT generateexportedsymbols.awk)
66   else()
67     set(AWK_SCRIPT generateversionscript.awk)
68   endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
69
70   add_custom_command(
71     OUTPUT ${outputFilename}
72     COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${inputFilename} >${outputFilename}
73     DEPENDS ${inputFilename} ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT}
74     COMMENT "Generating exports file ${outputFilename}"
75   )
76   set_source_files_properties(${outputFilename}
77                               PROPERTIES GENERATED TRUE)
78 endfunction()
79
80 function(add_precompiled_header header cppFile targetSources)
81   if(MSVC)
82     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
83
84     set_source_files_properties(${cppFile}
85                                 PROPERTIES COMPILE_FLAGS "/Yc\"${header}\" /Fp\"${precompiledBinary}\""
86                                            OBJECT_OUTPUTS "${precompiledBinary}")
87     set_source_files_properties(${${targetSources}}
88                                 PROPERTIES COMPILE_FLAGS "/Yu\"${header}\" /Fp\"${precompiledBinary}\""
89                                            OBJECT_DEPENDS "${precompiledBinary}")
90     # Add cppFile to SourcesVar
91     set(${targetSources} ${${targetSources}} ${cppFile} PARENT_SCOPE)
92   endif(MSVC)
93 endfunction()
94
95 function(strip_symbols targetName outputFilename)
96   if (CLR_CMAKE_PLATFORM_UNIX)
97     if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
98
99       # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
100       # generator expression doesn't work correctly returning the wrong path and on
101       # the newer cmake versions the LOCATION property isn't supported anymore.
102       if (CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
103           set(strip_source_file $<TARGET_FILE:${targetName}>)
104       else()
105           get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
106       endif()
107
108       if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
109         set(strip_destination_file ${strip_source_file}.dwarf)
110
111         add_custom_command(
112           TARGET ${targetName}
113           POST_BUILD
114           VERBATIM 
115           COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
116           COMMAND ${STRIP} -S ${strip_source_file}
117           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
118         )
119       else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
120         set(strip_destination_file ${strip_source_file}.dbg)
121
122         add_custom_command(
123           TARGET ${targetName}
124           POST_BUILD
125           VERBATIM 
126           COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
127           COMMAND ${OBJCOPY} --strip-debug ${strip_source_file}
128           COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
129           COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
130         )
131       endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
132
133       set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
134     endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
135   endif(CLR_CMAKE_PLATFORM_UNIX)
136 endfunction()
137
138 function(install_clr targetName)
139   list(FIND CLR_CROSS_COMPONENTS_LIST ${targetName} INDEX)  
140   if (NOT DEFINED CLR_CROSS_COMPONENTS_LIST OR NOT ${INDEX} EQUAL -1)  
141     strip_symbols(${targetName} strip_destination_file) 
142     # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE  
143     # generator expression doesn't work correctly returning the wrong path and on  
144     # the newer cmake versions the LOCATION property isn't supported anymore.  
145     if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)  
146        set(install_source_file $<TARGET_FILE:${targetName}>)  
147     else()  
148         get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)  
149     endif()  
150   
151     install(PROGRAMS ${install_source_file} DESTINATION .)  
152     if(WIN32)  
153         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)  
154     else()  
155         install(FILES ${strip_destination_file} DESTINATION .)  
156     endif()  
157   endif()  
158 endfunction()
159
160 function(_add_executable)
161     if(NOT WIN32)
162       add_executable(${ARGV} ${VERSION_FILE_PATH})
163     else()
164       add_executable(${ARGV})
165     endif(NOT WIN32)
166     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
167     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
168      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
169     endif()
170 endfunction()  
171
172 function(_add_library)
173     if(NOT WIN32)
174       add_library(${ARGV} ${VERSION_FILE_PATH})
175     else()
176       add_library(${ARGV})
177     endif(NOT WIN32)
178     list(FIND CLR_CROSS_COMPONENTS_LIST ${ARGV0} INDEX)  
179     if (DEFINED CLR_CROSS_COMPONENTS_LIST AND ${INDEX} EQUAL -1)  
180      set_target_properties(${ARGV0} PROPERTIES EXCLUDE_FROM_ALL 1)  
181     endif()  
182 endfunction()
183
184 function(_install)
185     if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
186       install(${ARGV})
187     endif()
188 endfunction()