xref: /aosp_15_r20/external/cronet/build/gn_run_binary.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker# Copyright 2014 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker# found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker"""Helper script for GN to run an arbitrary binary. See compiled_action.gni.
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard WorkerRun with:
8*6777b538SAndroid Build Coastguard Worker  python gn_run_binary.py <binary_name> [args ...]
9*6777b538SAndroid Build Coastguard Worker"""
10*6777b538SAndroid Build Coastguard Worker
11*6777b538SAndroid Build Coastguard Worker
12*6777b538SAndroid Build Coastguard Workerimport os
13*6777b538SAndroid Build Coastguard Workerimport subprocess
14*6777b538SAndroid Build Coastguard Workerimport sys
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard Worker# This script is designed to run binaries produced by the current build. We
17*6777b538SAndroid Build Coastguard Worker# may prefix it with "./" to avoid picking up system versions that might
18*6777b538SAndroid Build Coastguard Worker# also be on the path.
19*6777b538SAndroid Build Coastguard Workerpath = sys.argv[1]
20*6777b538SAndroid Build Coastguard Workerif not os.path.isabs(path):
21*6777b538SAndroid Build Coastguard Worker  path = './' + path
22*6777b538SAndroid Build Coastguard Worker
23*6777b538SAndroid Build Coastguard Worker# The rest of the arguments are passed directly to the executable.
24*6777b538SAndroid Build Coastguard Workerargs = [path] + sys.argv[2:]
25*6777b538SAndroid Build Coastguard Worker
26*6777b538SAndroid Build Coastguard Workerret = subprocess.call(args)
27*6777b538SAndroid Build Coastguard Workerif ret != 0:
28*6777b538SAndroid Build Coastguard Worker  if ret <= -100:
29*6777b538SAndroid Build Coastguard Worker    # Windows error codes such as 0xC0000005 and 0xC0000409 are much easier to
30*6777b538SAndroid Build Coastguard Worker    # recognize and differentiate in hex. In order to print them as unsigned
31*6777b538SAndroid Build Coastguard Worker    # hex we need to add 4 Gig to them.
32*6777b538SAndroid Build Coastguard Worker    print('%s failed with exit code 0x%08X' % (sys.argv[1], ret + (1 << 32)))
33*6777b538SAndroid Build Coastguard Worker  else:
34*6777b538SAndroid Build Coastguard Worker    print('%s failed with exit code %d' % (sys.argv[1], ret))
35*6777b538SAndroid Build Coastguard Workersys.exit(ret)
36