Merge pull request #15420 from swgillespie/test-build-perf
[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(EXISTS ${ProfilePath})
32             if(WIN32)
33                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE        " /LTCG /USEPROFILE:PGD=${ProfilePath}")
34                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
35             else(WIN32)
36                 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
37                     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
38                         if(HAVE_LTO)
39                             target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date)
40                             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
41                         else(HAVE_LTO)
42                             message(WARNING "LTO is not supported, skipping profile guided optimizations")
43                         endif(HAVE_LTO)
44                     else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
45                         message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
46                     endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
47                 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
48             endif(WIN32)
49         endif(EXISTS ${ProfilePath})
50     endif(CLR_CMAKE_PGO_INSTRUMENT)
51 endfunction(add_pgo)