1#!/usr/bin/env python
2
3# Copyright (c) 2013 Google Inc. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8Make sure PGO is working properly.
9"""
10
11import TestGyp
12
13import os
14import sys
15
16if sys.platform == 'win32':
17  test = TestGyp.TestGyp(formats=['msvs', 'ninja'])
18
19  CHDIR = 'linker-flags'
20  test.run_gyp('pgo.gyp', chdir=CHDIR)
21
22  def IsPGOAvailable():
23    """Returns true if the Visual Studio available here supports PGO."""
24    test.build('pgo.gyp', 'gen_linker_option', chdir=CHDIR)
25    tmpfile = test.read(test.built_file_path('linker_options.txt', chdir=CHDIR))
26    return any(line.find('PGOPTIMIZE') for line in tmpfile)
27
28  # Test generated build files look fine.
29  if test.format == 'ninja':
30    ninja = test.built_file_path('obj/test_pgo_instrument.ninja', chdir=CHDIR)
31    test.must_contain(ninja, '/LTCG:PGINSTRUMENT')
32    test.must_contain(ninja, 'test_pgo.pgd')
33    ninja = test.built_file_path('obj/test_pgo_optimize.ninja', chdir=CHDIR)
34    test.must_contain(ninja, '/LTCG:PGOPTIMIZE')
35    test.must_contain(ninja, 'test_pgo.pgd')
36    ninja = test.built_file_path('obj/test_pgo_update.ninja', chdir=CHDIR)
37    test.must_contain(ninja, '/LTCG:PGUPDATE')
38    test.must_contain(ninja, 'test_pgo.pgd')
39  elif test.format == 'msvs':
40    LTCG_FORMAT = '<LinkTimeCodeGeneration>%s</LinkTimeCodeGeneration>'
41    vcproj = test.workpath('linker-flags/test_pgo_instrument.vcxproj')
42    test.must_contain(vcproj, LTCG_FORMAT % 'PGInstrument')
43    test.must_contain(vcproj, 'test_pgo.pgd')
44    vcproj = test.workpath('linker-flags/test_pgo_optimize.vcxproj')
45    test.must_contain(vcproj, LTCG_FORMAT % 'PGOptimization')
46    test.must_contain(vcproj, 'test_pgo.pgd')
47    vcproj = test.workpath('linker-flags/test_pgo_update.vcxproj')
48    test.must_contain(vcproj, LTCG_FORMAT % 'PGUpdate')
49    test.must_contain(vcproj, 'test_pgo.pgd')
50
51  # When PGO is available, try building binaries with PGO.
52  if IsPGOAvailable():
53    pgd_path = test.built_file_path('test_pgo.pgd', chdir=CHDIR)
54
55    # Test if 'PGInstrument' generates PGD (Profile-Guided Database) file.
56    if os.path.exists(pgd_path):
57      test.unlink(pgd_path)
58    test.must_not_exist(pgd_path)
59    test.build('pgo.gyp', 'test_pgo_instrument', chdir=CHDIR)
60    test.must_exist(pgd_path)
61
62    # Test if 'PGOptimize' works well
63    test.build('pgo.gyp', 'test_pgo_optimize', chdir=CHDIR)
64    test.must_contain_any_line(test.stdout(), ['profiled functions'])
65
66    # Test if 'PGUpdate' works well
67    test.build('pgo.gyp', 'test_pgo_update', chdir=CHDIR)
68    # With 'PGUpdate', linker should not complain that sources are changed after
69    # the previous training run.
70    test.touch(test.workpath('linker-flags/inline_test_main.cc'))
71    test.unlink(test.built_file_path('test_pgo_update.exe', chdir=CHDIR))
72    test.build('pgo.gyp', 'test_pgo_update', chdir=CHDIR)
73    test.must_contain_any_line(test.stdout(), ['profiled functions'])
74
75  test.pass_test()
76