Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 1 | """ |
| 2 | Generate the contents of the git_sha1.h file. |
Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 3 | """ |
| 4 | |
Eric Engestrom | 7088622 | 2017-10-25 14:04:35 +0100 | [diff] [blame] | 5 | import argparse |
Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 6 | import os |
Jose Fonseca | b99dcbf | 2017-08-01 14:36:16 +0100 | [diff] [blame] | 7 | import os.path |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 11 | |
| 12 | def get_git_sha1(): |
| 13 | """Try to get the git SHA1 with git rev-parse.""" |
| 14 | git_dir = os.path.join(os.path.dirname(sys.argv[0]), '..', '.git') |
| 15 | try: |
| 16 | git_sha1 = subprocess.check_output([ |
| 17 | 'git', |
| 18 | '--git-dir=' + git_dir, |
| 19 | 'rev-parse', |
| 20 | 'HEAD', |
| 21 | ], stderr=open(os.devnull, 'w')).decode("ascii") |
Eric Engestrom | c909370 | 2020-05-20 01:22:42 +0200 | [diff] [blame] | 22 | except Exception: |
Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 23 | # don't print anything if it fails |
| 24 | git_sha1 = '' |
| 25 | return git_sha1 |
| 26 | |
Eric Engestrom | ba44990 | 2020-05-20 01:23:35 +0200 | [diff] [blame] | 27 | |
Eric Engestrom | c5dd022 | 2018-08-14 18:02:04 +0100 | [diff] [blame] | 28 | def write_if_different(contents): |
| 29 | """ |
| 30 | Avoid touching the output file if it doesn't need modifications |
| 31 | Useful to avoid triggering rebuilds when nothing has changed. |
| 32 | """ |
| 33 | if os.path.isfile(args.output): |
| 34 | with open(args.output, 'r') as file: |
| 35 | if file.read() == contents: |
| 36 | return |
| 37 | with open(args.output, 'w') as file: |
| 38 | file.write(contents) |
| 39 | |
Eric Engestrom | ba44990 | 2020-05-20 01:23:35 +0200 | [diff] [blame] | 40 | |
Eric Engestrom | 7088622 | 2017-10-25 14:04:35 +0100 | [diff] [blame] | 41 | parser = argparse.ArgumentParser() |
| 42 | parser.add_argument('--output', help='File to write the #define in', |
Eric Engestrom | c2e00f9 | 2018-08-14 18:05:55 +0100 | [diff] [blame] | 43 | required=True) |
Eric Engestrom | 7088622 | 2017-10-25 14:04:35 +0100 | [diff] [blame] | 44 | args = parser.parse_args() |
Brian Paul | a3b2e60 | 2017-10-09 13:47:20 -0600 | [diff] [blame] | 45 | |
| 46 | git_sha1 = os.environ.get('MESA_GIT_SHA1_OVERRIDE', get_git_sha1())[:10] |
| 47 | if git_sha1: |
Eric Engestrom | bc8abc1 | 2018-08-14 18:04:58 +0100 | [diff] [blame] | 48 | write_if_different('#define MESA_GIT_SHA1 " (git-' + git_sha1 + ')"') |
Eric Engestrom | 2117d03 | 2017-10-29 22:06:28 +0000 | [diff] [blame] | 49 | else: |
Eric Engestrom | bc8abc1 | 2018-08-14 18:04:58 +0100 | [diff] [blame] | 50 | write_if_different('#define MESA_GIT_SHA1 ""') |