github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/tools/genmake (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 Google Inc. All Rights Reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Generate a Makefile for Python targets in a GOPATH directory."""
    18  
    19  import argparse
    20  import os
    21  import subprocess
    22  import sys
    23  
    24  
    25  parser = argparse.ArgumentParser()
    26  parser.add_argument('dir', help='GOPATH dir to scan for Python modules')
    27  parser.add_argument('-all_target', default='all',
    28                      help='make target that will build all modules')
    29  
    30  
    31  def _PrintRule(target, prereqs, rules):
    32    print '{}: {}'.format(target, ' '.join(prereqs))
    33    if rules:
    34      print '\t@mkdir -p $(@D)'
    35      for rule in rules:
    36        print '\t@{}'.format(rule)
    37    print
    38  
    39  
    40  def main(args):
    41    try:
    42      proc = subprocess.Popen('go env GOOS GOARCH', shell=True,
    43                              stdout=subprocess.PIPE)
    44    except OSError as e:
    45      print >> sys.stderr, str(e)
    46      return 1
    47    out, _ = proc.communicate()
    48    if proc.returncode:
    49      print >> sys.stderr, 'go exited with status: {}'.format(proc.returncode)
    50      return 1
    51    goos, goarch = out.split()
    52  
    53    if args.all_target:
    54      print '{}:\n'.format(args.all_target)
    55  
    56    gopath = os.path.normpath(args.dir)
    57    pkg_dir = os.path.join(gopath, 'pkg', '{}_{}'.format(goos, goarch))
    58    pydir = os.path.join(gopath, 'src', '__python__')
    59    for dirpath, _, filenames in os.walk(pydir):
    60      for filename in filenames:
    61        if not filename.endswith('.py'):
    62          continue
    63        basename = os.path.relpath(dirpath, pydir)
    64        if filename != '__init__.py':
    65          basename = os.path.normpath(
    66              os.path.join(basename, filename[:-3]))
    67        modname = basename.replace(os.sep, '.')
    68        ar_name = os.path.join(pkg_dir, '__python__', basename + '.a')
    69        go_file = os.path.join(pydir, basename, 'module.go')
    70        _PrintRule(go_file,
    71                   [os.path.join(dirpath, filename)],
    72                   ['grumpc -modname={} $< > $@'.format(modname)])
    73        recipe = (r"""pydeps -modname=%s $< | awk '{gsub(/\./, "/", $$0); """
    74                  r"""print "%s: %s/__python__/" $$0 ".a"}' > $@""")
    75        dep_file = os.path.join(pydir, basename, 'module.d')
    76        _PrintRule(dep_file, [os.path.join(dirpath, filename)],
    77                   [recipe % (modname, ar_name, pkg_dir)])
    78        go_package = '__python__/' + basename.replace(os.sep, '/')
    79        recipe = 'go tool compile -o $@ -p {} -complete -I {} -pack $<'
    80        _PrintRule(ar_name, [go_file], [recipe.format(go_package, pkg_dir)])
    81        if args.all_target:
    82          _PrintRule(args.all_target, [ar_name], [])
    83        print '-include {}\n'.format(dep_file)
    84  
    85  
    86  if __name__ == '__main__':
    87    sys.exit(main(parser.parse_args()))