Update dependencies from https://github.com/dotnet/corefx build 20190820.10 (#26291)
[platform/upstream/coreclr.git] / pgosupport.cmake
1 # Adds Profile Guided Optimization (PGO) flags to the current target
2 function(add_pgo TargetName)
3     if(WIN32)
4         set(ProfileFileName "${TargetName}.pgd")
5     else(WIN32)
6         set(ProfileFileName "${TargetName}.profdata")
7     endif(WIN32)
8
9     set(CLR_CMAKE_OPTDATA_PACKAGEWITHRID "optimization.${CLR_CMAKE_TARGET_OS}-${CLR_CMAKE_TARGET_ARCH}.PGO.CoreCLR")
10
11     # On case-sensitive file systems, NuGet packages are restored to lowercase paths
12     string(TOLOWER "${CLR_CMAKE_OPTDATA_PACKAGEWITHRID}/${CLR_CMAKE_OPTDATA_VERSION}" OptDataVersionedSubPath)
13
14     file(TO_NATIVE_PATH
15         "${CLR_CMAKE_PACKAGES_DIR}/${OptDataVersionedSubPath}/data/${ProfileFileName}"
16         ProfilePath
17     )
18
19     if(CLR_CMAKE_PGO_INSTRUMENT)
20         if(WIN32)
21             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE        " /LTCG /GENPROFILE")
22             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /GENPROFILE")
23         else(WIN32)
24             if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
25                 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-generate)
26                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-generate")
27             endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
28         endif(WIN32)
29     elseif(CLR_CMAKE_PGO_OPTIMIZE)
30         # If we don't have profile data availble, gracefully fall back to a non-PGO opt build
31         if(NOT EXISTS ${ProfilePath})
32             message("PGO data file NOT found: ${ProfilePath}")
33         else(NOT EXISTS ${ProfilePath})
34             if(WIN32)
35                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE        " /LTCG /USEPROFILE:PGD=${ProfilePath}")
36                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
37             else(WIN32)
38                 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
39                     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
40                         if(HAVE_LTO)
41                             target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled)
42                             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
43                         else(HAVE_LTO)
44                             message(WARNING "LTO is not supported, skipping profile guided optimizations")
45                         endif(HAVE_LTO)
46                     else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
47                         message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
48                     endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
49                 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
50             endif(WIN32)
51         endif(NOT EXISTS ${ProfilePath})
52     endif(CLR_CMAKE_PGO_INSTRUMENT)
53 endfunction(add_pgo)