Skip to content

Commit 057fbc8

Browse files
nashifAnas Nashif
authored andcommitted
dts: make extract script take options
Use argeparse for options and add a fixup option to add on top of generated file. This was previously done in the top Makefile and was generated defines outside of the header main if statement. Jira: ZEP-2147 Change-Id: If65f34a11de27baa770d4ce0ef4fca2abbd30258 Signed-off-by: Anas Nashif <[email protected]>
1 parent c16b0f1 commit 057fbc8

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,15 @@ zephyr: $(zephyr-deps) $(KERNEL_BIN_NAME)
950950
ifeq ($(CONFIG_HAS_DTS),y)
951951
define filechk_generated_dts_board.h
952952
(echo "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */"; \
953-
$(ZEPHYR_BASE)/scripts/extract_dts_includes.py dts/$(ARCH)/$(BOARD_NAME).dts_compiled $(ZEPHYR_BASE)/dts/$(ARCH)/yaml; \
954953
if test -e $(ZEPHYR_BASE)/dts/$(ARCH)/$(BOARD_NAME).fixup; then \
955-
echo; echo; \
956-
echo "/* Following definitions fixup the generated include */"; \
957-
echo; \
958-
cat $(ZEPHYR_BASE)/dts/$(ARCH)/$(BOARD_NAME).fixup; \
954+
$(ZEPHYR_BASE)/scripts/extract_dts_includes.py \
955+
-d dts/$(ARCH)/$(BOARD_NAME).dts_compiled \
956+
-y $(ZEPHYR_BASE)/dts/$(ARCH)/yaml \
957+
-f $(ZEPHYR_BASE)/dts/$(ARCH)/$(BOARD_NAME).fixup; \
958+
else \
959+
$(ZEPHYR_BASE)/scripts/extract_dts_includes.py \
960+
-d dts/$(ARCH)/$(BOARD_NAME).dts_compiled \
961+
-y $(ZEPHYR_BASE)/dts/$(ARCH)/yaml; \
959962
fi; \
960963
)
961964
endef

scripts/extract_dts_includes.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/usr/bin/env python3
2+
3+
# vim: ai:ts=4:sw=4
4+
25
import sys
36
from os import walk
47
import os
58
import re
69
import yaml
710
import pprint
11+
import argparse
812

913
from devicetree import parse_file
1014

@@ -503,7 +507,7 @@ def print_key_value(k, v, tabstop):
503507

504508
return
505509

506-
def generate_include_file(defs):
510+
def generate_include_file(defs, fixup):
507511
compatible = reduced['/']['props']['compatible'][0]
508512

509513
sys.stdout.write("/**************************************************\n")
@@ -539,18 +543,40 @@ def generate_include_file(defs):
539543
print_key_value(prop, defs[node].get(prop), maxtabstop)
540544
sys.stdout.write("\n")
541545

542-
sys.stdout.write("#endif\n");
543-
544-
def main(args):
545-
if len(args) < 2:
546-
print('Usage: %s filename.dts path_to_yaml' % args[0])
546+
if fixup and os.path.exists(fixup):
547+
sys.stdout.write("\n")
548+
sys.stdout.write("/* Following definitions fixup the generated include */\n")
549+
try:
550+
with open(fixup, "r") as fd:
551+
for line in fd.readlines():
552+
sys.stdout.write(line)
553+
sys.stdout.write("\n")
554+
except:
555+
raise Exception("Input file " + os.path.abspath(fixup) + " does not exist.")
556+
557+
sys.stdout.write("#endif\n")
558+
559+
def parse_arguments():
560+
561+
parser = argparse.ArgumentParser(description = __doc__,
562+
formatter_class = argparse.RawDescriptionHelpFormatter)
563+
parser.add_argument("-d", "--dts", help="DTS file")
564+
parser.add_argument("-y", "--yaml", help="YAML file")
565+
parser.add_argument("-f", "--fixup", help="Fixup file")
566+
567+
return parser.parse_args()
568+
569+
def main():
570+
args = parse_arguments()
571+
if not args.dts or not args.yaml:
572+
print('Usage: %s -d filename.dts -y path_to_yaml' % sys.argv[0])
547573
return 1
548574

549575
try:
550-
with open(args[1], "r") as fd:
576+
with open(args.dts, "r") as fd:
551577
d = parse_file(fd)
552578
except:
553-
raise Exception("Input file " + os.path.abspath(args[1]) + " does not exist.")
579+
raise Exception("Input file " + os.path.abspath(args.dts) + " does not exist.")
554580

555581
# compress list to nodes w/ paths, add interrupt parent
556582
compress_nodes(d['/'], '/')
@@ -572,7 +598,7 @@ def main(args):
572598

573599
# scan YAML files and find the ones we are interested in
574600
yaml_files = []
575-
for (dirpath, dirnames, filenames) in walk(args[2]):
601+
for (dirpath, dirnames, filenames) in walk(args.yaml):
576602
yaml_files.extend([f for f in filenames if re.match('.*\.yaml\Z', f)])
577603
yaml_files = [dirpath + '/' + t for t in yaml_files]
578604
break
@@ -628,9 +654,7 @@ def main(args):
628654
extract_reg_prop(chosen['zephyr,sram'], None, defs, "CONFIG_SRAM", 1024)
629655

630656
# generate include file
631-
generate_include_file(defs)
657+
generate_include_file(defs, args.fixup)
632658

633659
if __name__ == '__main__':
634-
# test1.py executed as script
635-
# do something
636-
sys.exit(main(sys.argv))
660+
main()

0 commit comments

Comments
 (0)