/ scripts / site_scons / site_tools / gitversion.py
gitversion.py
  1  #
  2  # Copyright (C) 2008-2020 Advanced Micro Devices, Inc. All rights reserved.
  3  #
  4  # Redistribution and use in source and binary forms, with or without modification,
  5  # are permitted provided that the following conditions are met:
  6  # 1. Redistributions of source code must retain the above copyright notice,
  7  #    this list of conditions and the following disclaimer.
  8  # 2. Redistributions in binary form must reproduce the above copyright notice,
  9  #    this list of conditions and the following disclaimer in the documentation
 10  #    and/or other materials provided with the distribution.
 11  # 3. Neither the name of the copyright holder nor the names of its contributors
 12  #    may be used to endorse or promote products derived from this software without
 13  #    specific prior written permission.
 14  #
 15  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 18  # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 19  # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 20  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 21  # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 22  # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 23  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 24  # POSSIBILITY OF SUCH DAMAGE.
 25  
 26  import subprocess
 27  import sys
 28  
 29  import SCons.Action
 30  
 31  __gitversion = None
 32  
 33  def get_git_version(env):
 34      if env.GetOption('help') or env.GetOption('clean'):
 35          return
 36  
 37      global __gitversion
 38  
 39      if __gitversion:
 40          return __gitversion
 41  
 42      try:
 43          #if not conf.CheckProg('git'):
 44          #    __gitversion = 'unknown'
 45          #    return
 46  
 47          kw = { 'stdin' : 'devnull',
 48                 'stdout' : subprocess.PIPE,
 49                 'stderr' : subprocess.PIPE,
 50                 'universal_newlines' : True,
 51          }
 52          p = SCons.Action._subproc(env, ['git', 'describe', '--dirty= (modified)', '--always'], **kw)
 53          out,err = p.communicate()
 54          status = p.wait()
 55          if err:
 56              sys.stderr.write(unicode(err))
 57  
 58          if status:
 59              __gitversion = 'unknown'
 60          else:
 61              __gitversion = out.strip()
 62      finally:
 63          return __gitversion
 64  
 65  
 66  # AMD Libm version template
 67  
 68  __amd_libm_version_template="""/*
 69  * This file is automatically generated by the build process
 70  * DO NOT EDIT!
 71  */
 72  
 73  static const char VERSION_STRING[] = "%s";
 74  
 75  static const char* alm_get_build(void);
 76  static const char* alm_get_build(void)
 77  {
 78          return VERSION_STRING;
 79  }
 80  """
 81  
 82  def generate_version(env, target):
 83      """Generate the version file with the current version in it"""
 84      #print("generate_version", "target:", target, "env:", env)
 85  
 86      version = get_git_version(env)
 87  
 88      contents = __amd_libm_version_template % (version)
 89  
 90      fd = open(target, 'w')
 91      fd.write(contents)
 92      fd.close()
 93  
 94      return 0
 95  
 96  
 97  def generate(env):
 98      env.AddMethod(generate_version, 'GenerateVersion')
 99  
100  
101  def exists(env):
102      return True
103