1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""A wrapper to run yasm.
6
7Its main job is to provide a Python wrapper for GN integration, and to write
8the makefile-style output yasm generates in stdout to a .d file for dependency
9management of .inc files.
10
11Run with:
12  python run_yasm.py <yasm_binary_path> <all other yasm args>
13
14Note that <all other yasm args> must include an explicit output file (-o). This
15script will append a ".d" to this and write the dependencies there. This script
16will add "-M" to cause yasm to write the deps to stdout, so you don't need to
17specify that.
18"""
19
20import argparse
21import sys
22import subprocess
23
24# Extract the output file name from the yasm command line so we can generate a
25# .d file with the same base name.
26parser = argparse.ArgumentParser()
27parser.add_argument("-o", dest="objfile")
28options, _ = parser.parse_known_args()
29
30objfile = options.objfile
31depfile = objfile + '.d'
32
33# Assemble.
34result_code = subprocess.call(sys.argv[1:])
35if result_code != 0:
36  sys.exit(result_code)
37
38# Now generate the .d file listing the dependencies. The -M option makes yasm
39# write the Makefile-style dependencies to stdout, but it seems that inhibits
40# generating any compiled output so we need to do this in a separate pass.
41# However, outputting deps seems faster than actually assembling, and yasm is
42# so fast anyway this is not a big deal.
43#
44# This guarantees proper dependency management for assembly files. Otherwise,
45# we would have to require people to manually specify the .inc files they
46# depend on in the build file, which will surely be wrong or out-of-date in
47# some cases.
48deps = subprocess.check_output(sys.argv[1:] + ['-M'])
49with open(depfile, "wb") as f:
50  f.write(deps)
51
52