| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """A module to add ninja support to cr.""" | 5 """A module to add ninja support to cr.""" |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 | 8 |
| 9 import cr | 9 import cr |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 NINJA_GOMA_CC=os.path.join('{GOMA_DIR}', 'gomacc'), | 27 NINJA_GOMA_CC=os.path.join('{GOMA_DIR}', 'gomacc'), |
| 28 NINJA_GOMA_LINE='cc = {NINJA_GOMA_CC} $', | 28 NINJA_GOMA_LINE='cc = {NINJA_GOMA_CC} $', |
| 29 ) | 29 ) |
| 30 # A placeholder for the system detected configuration | 30 # A placeholder for the system detected configuration |
| 31 DETECTED = cr.Config('DETECTED') | 31 DETECTED = cr.Config('DETECTED') |
| 32 | 32 |
| 33 def __init__(self): | 33 def __init__(self): |
| 34 super(NinjaBuilder, self).__init__() | 34 super(NinjaBuilder, self).__init__() |
| 35 self._targets = [] | 35 self._targets = [] |
| 36 | 36 |
| 37 def Build(self, context, targets, arguments): | 37 def Build(self, targets, arguments): |
| 38 # Make sure Goma is started if Ninja is set to use it. | 38 # Make sure Goma is started if Ninja is set to use it. |
| 39 # This may be redundant, but it currently improves reliability. | 39 # This may be redundant, but it currently improves reliability. |
| 40 try: | 40 try: |
| 41 with open(context.Get('NINJA_BUILD_FILE'), 'r') as f: | 41 with open(cr.context.Get('NINJA_BUILD_FILE'), 'r') as f: |
| 42 if f.readline().rstrip('\n') == context.Get('NINJA_GOMA_LINE'): | 42 if f.readline().rstrip('\n') == cr.context.Get('NINJA_GOMA_LINE'): |
| 43 # Goma is active, so make sure it's started. | 43 # Goma is active, so make sure it's started. |
| 44 cr.Host.ExecuteSilently( | 44 cr.Host.ExecuteSilently( |
| 45 context, | |
| 46 '{NINJA_GOMA_CTL}', | 45 '{NINJA_GOMA_CTL}', |
| 47 'ensure_start' | 46 'ensure_start' |
| 48 ) | 47 ) |
| 49 except IOError: | 48 except IOError: |
| 50 pass | 49 pass |
| 51 | 50 |
| 52 build_arguments = [target.build_target for target in targets] | 51 build_arguments = [target.build_target for target in targets] |
| 53 build_arguments.extend(arguments) | 52 build_arguments.extend(arguments) |
| 54 cr.Host.Execute( | 53 cr.Host.Execute( |
| 55 context, | |
| 56 '{NINJA_BINARY}', | 54 '{NINJA_BINARY}', |
| 57 '-C{CR_BUILD_DIR}', | 55 '-C{CR_BUILD_DIR}', |
| 58 '-j{NINJA_JOBS}', | 56 '-j{NINJA_JOBS}', |
| 59 '-l{NINJA_PROCESSORS}', | 57 '-l{NINJA_PROCESSORS}', |
| 60 *build_arguments | 58 *build_arguments |
| 61 ) | 59 ) |
| 62 | 60 |
| 63 def Clean(self, context, targets, arguments): | 61 def Clean(self, targets, arguments): |
| 64 build_arguments = [target.build_target for target in targets] | 62 build_arguments = [target.build_target for target in targets] |
| 65 build_arguments.extend(arguments) | 63 build_arguments.extend(arguments) |
| 66 cr.Host.Execute( | 64 cr.Host.Execute( |
| 67 context, | |
| 68 '{NINJA_BINARY}', | 65 '{NINJA_BINARY}', |
| 69 '-C{CR_BUILD_DIR}', | 66 '-C{CR_BUILD_DIR}', |
| 70 '-tclean', | 67 '-tclean', |
| 71 *build_arguments | 68 *build_arguments |
| 72 ) | 69 ) |
| 73 | 70 |
| 74 def GetTargets(self, context): | 71 def GetTargets(self): |
| 75 """Overridden from Builder.GetTargets.""" | 72 """Overridden from Builder.GetTargets.""" |
| 76 if not self._targets: | 73 if not self._targets: |
| 77 try: | 74 try: |
| 78 context.Get('CR_BUILD_DIR', raise_errors=True) | 75 cr.context.Get('CR_BUILD_DIR', raise_errors=True) |
| 79 except KeyError: | 76 except KeyError: |
| 80 return self._targets | 77 return self._targets |
| 81 output = cr.Host.Capture( | 78 output = cr.Host.Capture( |
| 82 context, | |
| 83 '{NINJA_BINARY}', | 79 '{NINJA_BINARY}', |
| 84 '-C{CR_BUILD_DIR}', | 80 '-C{CR_BUILD_DIR}', |
| 85 '-ttargets', | 81 '-ttargets', |
| 86 'all' | 82 'all' |
| 87 ) | 83 ) |
| 88 for line in output.split('\n'): | 84 for line in output.split('\n'): |
| 89 line = line.strip() | 85 line = line.strip() |
| 90 if line.endswith(_PHONY_SUFFIX): | 86 if line.endswith(_PHONY_SUFFIX): |
| 91 target = line[:-len(_PHONY_SUFFIX)].strip() | 87 target = line[:-len(_PHONY_SUFFIX)].strip() |
| 92 self._targets.append(target) | 88 self._targets.append(target) |
| 93 elif line.endswith(_LINK_SUFFIX): | 89 elif line.endswith(_LINK_SUFFIX): |
| 94 target = line[:-len(_LINK_SUFFIX)].strip() | 90 target = line[:-len(_LINK_SUFFIX)].strip() |
| 95 self._targets.append(target) | 91 self._targets.append(target) |
| 96 return self._targets | 92 return self._targets |
| 97 | 93 |
| 98 @classmethod | 94 @classmethod |
| 99 def ClassInit(cls): | 95 def ClassInit(cls): |
| 100 # TODO(iancottrell): If we can't detect ninja, we should be disabled. | 96 # TODO(iancottrell): If we can't detect ninja, we should be disabled. |
| 101 ninja_binaries = cr.Host.SearchPath('ninja') | 97 ninja_binaries = cr.Host.SearchPath('ninja') |
| 102 if ninja_binaries: | 98 if ninja_binaries: |
| 103 cls.DETECTED.Set(NINJA_BINARY=ninja_binaries[0]) | 99 cls.DETECTED.Set(NINJA_BINARY=ninja_binaries[0]) |
| OLD | NEW |