Merge pull request #12528 from pgavlin/NoPgoOption
[platform/upstream/coreclr.git] / pgosupport.cmake
1 function(clr_pgo_unknown_arch)
2     if (WIN32)
3         message(FATAL_ERROR "Only AMD64, ARM and I386 are supported for PGO")
4     else()
5         message(FATAL_ERROR "PGO not currently supported on the current platform")
6     endif()
7 endfunction(clr_pgo_unknown_arch)
8
9 # Adds Profile Guided Optimization (PGO) flags to the current target
10 function(add_pgo TargetName)
11     if(WIN32)
12         set(ProfileFileName "${TargetName}.pgd")
13     else(WIN32)
14         set(ProfileFileName "${TargetName}.profdata")
15     endif(WIN32)
16
17     set(CLR_CMAKE_OPTDATA_PACKAGEWITHRID "optimization.${CLR_CMAKE_TARGET_OS}-${CLR_CMAKE_TARGET_ARCH}.PGO.CoreCLR")
18
19     # On case-sensitive file systems, NuGet packages are restored to lowercase paths
20     string(TOLOWER "${CLR_CMAKE_OPTDATA_PACKAGEWITHRID}/${CLR_CMAKE_OPTDATA_VERSION}" OptDataVersionedSubPath)
21
22     file(TO_NATIVE_PATH
23         "${CLR_CMAKE_PACKAGES_DIR}/${OptDataVersionedSubPath}/data/${ProfileFileName}"
24         ProfilePath
25     )
26
27     if(CLR_CMAKE_PGO_INSTRUMENT)
28         if(WIN32)
29             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE        " /LTCG /GENPROFILE")
30             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /GENPROFILE")
31         else(WIN32)
32             if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
33                 target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-generate)
34                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-generate")
35             endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
36         endif(WIN32)
37     elseif(CLR_CMAKE_PGO_OPTIMIZE)
38         # If we don't have profile data availble, gracefully fall back to a non-PGO opt build
39         if(EXISTS ${ProfilePath})
40             if(WIN32)
41                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE        " /LTCG /USEPROFILE:PGD=${ProfilePath}")
42                 set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO " /LTCG /USEPROFILE:PGD=${ProfilePath}")
43             else(WIN32)
44                 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
45                     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
46                         if(HAVE_LTO)
47                             target_compile_options(${TargetName} PRIVATE -flto -fprofile-instr-use=${ProfilePath} -Wno-profile-instr-out-of-date)
48                             set_property(TARGET ${TargetName} APPEND_STRING PROPERTY LINK_FLAGS " -flto -fuse-ld=gold -fprofile-instr-use=${ProfilePath}")
49                         else(HAVE_LTO)
50                             message(WARNING "LTO is not supported, skipping profile guided optimizations")
51                         endif(HAVE_LTO)
52                     else(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
53                         message(WARNING "PGO is not supported; Clang 3.6 or later is required for profile guided optimizations")
54                     endif(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
55                 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
56             endif(WIN32)
57         endif(EXISTS ${ProfilePath})
58     endif(CLR_CMAKE_PGO_INSTRUMENT)
59 endfunction(add_pgo)
60
61 if(WIN32)
62   if(CLR_CMAKE_PGO_INSTRUMENT)
63     # Instrumented PGO binaries on Windows introduce an additional runtime dependency, pgort<ver>.dll.
64     # Make sure we copy it next to the installed product to make it easier to redistribute the package.
65
66     string(SUBSTRING ${CMAKE_VS_PLATFORM_TOOLSET} 1 -1 VS_PLATFORM_VERSION_NUMBER)
67     set(PGORT_FILENAME "pgort${VS_PLATFORM_VERSION_NUMBER}.dll")
68
69     get_filename_component(PATH_CXX_ROOTDIR ${CMAKE_CXX_COMPILER} DIRECTORY)
70
71     if(CLR_CMAKE_PLATFORM_ARCH_I386)
72       set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/${PGORT_FILENAME}")
73     elseif(CLR_CMAKE_PLATFORM_ARCH_AMD64)
74       set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../amd64/${PGORT_FILENAME}")
75     elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
76       set(PATH_VS_PGORT_DLL "${PATH_CXX_ROOTDIR}/../arm/${PGORT_FILENAME}")
77     else()
78       clr_pgo_unknown_arch()
79     endif()
80
81     if (EXISTS ${PATH_VS_PGORT_DLL})
82       message(STATUS "Found PGO runtime: ${PATH_VS_PGORT_DLL}")
83       install(PROGRAMS ${PATH_VS_PGORT_DLL} DESTINATION .)
84     else()
85       message(FATAL_ERROR "file not found: ${PATH_VS_PGORT_DLL}")
86     endif()
87
88   endif(CLR_CMAKE_PGO_INSTRUMENT)
89 endif(WIN32)