get_defines 3.18 KB
#!/usr/bin/python

# Script for parsing source code file and extracting compile flags and options
# 2020 Roman Bartosinski, daiteq s.r.o.
#
# Line begins with:
# //A:                          assembler flags
# //T:                          C compiler flags
# //D:                          selection of data types for specific testing framework

import sys

meta={}

#print(sys.argv)

if len(sys.argv)>2:
  onlytype = sys.argv[2]
else:
  onlytype = None

try:
  if len(sys.argv)<2:
    raise Exception('no arg')


  with open(sys.argv[1]) as f:
    content = f.readlines()

#  print(content)
  for ln in content:
# skip unsupported lines
    if not ln.startswith('//'):
      continue
    if (len(ln)<5) or (ln[3]!=':'):
      continue
    ctp = ln[2]
    if (onlytype is not None) and (ctp!=onlytype[0]):
      continue
# get data types
    if ctp=='D':
      if 'D' not in meta:
        meta['D'] = ""

      f = ln.find("'")
      i = ln.find("=")
      if f<0:
        f = 3
        l = len(ln)
      else:
        l = ln.rfind("'")
      if i<0:
       i = l
      args = ln[f+1:i]
      if i<l:
        res  = ln[i+1:l]
      else:
        res = ''

      #print(args)
      i = args.find(',')
      if i<0:
        a1 = args.strip()
        a2 = "none"
      else:
        a1 = args[0:i].strip()
        a2 = args[i+1:].strip()

      if a1=='half':
        meta['D'] += " -DPAR_A_HALF=1"
      elif a1=='float':
        meta['D'] += " -DPAR_A_SINGLE=1"
      elif a1=='double':
        meta['D'] += " -DPAR_A_DOUBLE=1"
      elif a1=='int32':
        meta['D'] += " -DPAR_A_INT32=1"
      elif a1=='packhalf':
        meta['D'] += " -DPAR_A_PACKHALF=1"
      else:
        meta['D'] += " -DPAR_A_NONE=1"

      if a2=='none':
        meta['D'] += " -DPAR_B_NONE=1"
      elif a2=='half':
        meta['D'] += " -DPAR_B_HALF=1"
      elif a2=='float':
        meta['D'] += " -DPAR_B_SINGLE=1"
      elif a2=='double':
        meta['D'] += " -DPAR_B_DOUBLE=1"
      elif a2=='int32':
        meta['D'] += " -DPAR_B_INT32=1"
      elif a2=='packhalf':
        meta['D'] += " -DPAR_B_PACKHALF=1"

      res = res.strip()
      if res=='half':
        meta['D'] += " -DRES_HALF=1"
      elif res=='float':
        meta['D'] += " -DRES_SINGLE=1"
      elif res=='double':
        meta['D'] += " -DRES_DOUBLE=1"
      elif res=='int32':
        meta['D'] += " -DRES_INT32=1"
      elif res=='packhalf':
        meta['D'] += " -DRES_PACKHALF=1"
      else:
        meta['D'] += " -DRES_NONE=1"

# get list of compilator flags
    if ctp=='T':
      if 'T' not in meta:
        meta['T'] = []
      f = ln.find("'")
      if f<0:
        f = 4
        l = len(ln)
      else:
        l = ln.rfind("'")
      meta['T'].append(ln[f+1:l].strip())

# get assembler flags
    if ctp=='A':
      if 'A' not in meta:
        meta['A'] = ""

      f = ln.find("'")
      if f<0:
        f = 4
        l = len(ln)
      else:
        l = ln.rfind("'")
      meta['A'] += " " + ln[f+1:l].strip()

except:
  if 'D' not in meta:
    meta['D'] = ""
  meta['D'] += " -DNOTREADY"
  sys.exit

if onlytype is not None:
  if onlytype in meta:
    if isinstance(meta[onlytype], str):
      print(meta[onlytype])
    else:
      print("\n".join(meta[onlytype]))
else:
  print(meta)