xref: /btstack/port/max32630-fthr/scripts/create_examples.py (revision 8f3b3e6c8cdbc58b51946b550146548e05ee81c8)
1c894dca1SMatthias Ringwald#!/usr/bin/env python
2c894dca1SMatthias Ringwald#
3c894dca1SMatthias Ringwald# Create project files for all BTstack embedded examples in WICED/apps/btstack
4c894dca1SMatthias Ringwald
5c894dca1SMatthias Ringwaldimport os
6c894dca1SMatthias Ringwaldimport re
7c894dca1SMatthias Ringwaldimport shutil
8c894dca1SMatthias Ringwaldimport subprocess
9c894dca1SMatthias Ringwaldimport sys
10c894dca1SMatthias Ringwald
11*8f3b3e6cSMatthias Ringwald# build all template
12*8f3b3e6cSMatthias Ringwaldbuild_all = '''
13*8f3b3e6cSMatthias RingwaldSUBDIRS =  \\
14*8f3b3e6cSMatthias Ringwald%s
15*8f3b3e6cSMatthias Ringwald
16*8f3b3e6cSMatthias Ringwaldall:
17*8f3b3e6cSMatthias Ringwald\techo Building all examples
18*8f3b3e6cSMatthias Ringwald\tfor dir in $(SUBDIRS); do \\
19*8f3b3e6cSMatthias Ringwald\t$(MAKE) -C $$dir || exit 1; \\
20*8f3b3e6cSMatthias Ringwald\tdone
21*8f3b3e6cSMatthias Ringwald
22*8f3b3e6cSMatthias Ringwaldclean:
23*8f3b3e6cSMatthias Ringwald\techo Cleaning all ports
24*8f3b3e6cSMatthias Ringwald\tfor dir in $(SUBDIRS); do \\
25*8f3b3e6cSMatthias Ringwald\t$(MAKE) -C $$dir clean; \\
26*8f3b3e6cSMatthias Ringwald\tdone
27*8f3b3e6cSMatthias Ringwald'''
28*8f3b3e6cSMatthias Ringwald
29c894dca1SMatthias Ringwald# get script path
30c894dca1SMatthias Ringwaldscript_path = os.path.abspath(os.path.dirname(sys.argv[0])) + '/../'
31c894dca1SMatthias Ringwald
32c894dca1SMatthias Ringwald# get btstack root
33c894dca1SMatthias Ringwaldbtstack_root = script_path + '../../'
34c894dca1SMatthias Ringwald
35c894dca1SMatthias Ringwald## pick correct init script based on your hardware
36c894dca1SMatthias Ringwald# - init script for CC2564B
37c894dca1SMatthias Ringwaldcc256x_init_script = 'bluetooth_init_cc2564B_1.6_BT_Spec_4.1.c'
38c894dca1SMatthias Ringwald
39c894dca1SMatthias Ringwaldsubprocess.call("make -f ../Makefile -C src " + cc256x_init_script, shell=True)
40c894dca1SMatthias Ringwald
41c894dca1SMatthias Ringwald# fetch init script
42c894dca1SMatthias Ringwald# print("Creating init script %s" % cc256x_init_script)
43c894dca1SMatthias Ringwald# make_template = 'make -f {BTSTACK_ROOT}chipset/cc256x/Makefile.inc -C {SCRIPT_PATH}src/ {INIT_SCRIPT} BTSTACK_ROOT={BTSTACK_ROOT}'
44c894dca1SMatthias Ringwald# make_command = make_template.format(BTSTACK_ROOT=btstack_root, SCRIPT_PATH=script_path, INIT_SCRIPT=cc256x_init_script)
45c894dca1SMatthias Ringwald# print(make_command)
46c894dca1SMatthias Ringwald# subprocess.call(make_command)
47c894dca1SMatthias Ringwald
48c894dca1SMatthias Ringwald# path to examples
49c894dca1SMatthias Ringwaldexamples_embedded = btstack_root + 'example/'
50c894dca1SMatthias Ringwald
51c894dca1SMatthias Ringwald# path to generated example projects
526b7adbe5SMatthias Ringwaldprojects_path = script_path + "example/"
53c894dca1SMatthias Ringwald
54c894dca1SMatthias Ringwald# path to template
556b7adbe5SMatthias Ringwaldtemplate_path = script_path + 'example/template/Makefile'
56c894dca1SMatthias Ringwald
57c894dca1SMatthias Ringwaldprint("Creating example projects:")
58c894dca1SMatthias Ringwald
59c894dca1SMatthias Ringwald# iterate over btstack examples
60c894dca1SMatthias Ringwaldexample_files = os.listdir(examples_embedded)
61c894dca1SMatthias Ringwald
62*8f3b3e6cSMatthias Ringwaldexamples = []
63*8f3b3e6cSMatthias Ringwald
64c894dca1SMatthias Ringwaldfor file in example_files:
65c894dca1SMatthias Ringwald    if not file.endswith(".c"):
66c894dca1SMatthias Ringwald        continue
67c894dca1SMatthias Ringwald    example = file[:-2]
68*8f3b3e6cSMatthias Ringwald    examples.append(example)
69c894dca1SMatthias Ringwald
70c894dca1SMatthias Ringwald    # create folder
71c894dca1SMatthias Ringwald    project_folder = projects_path + example + "/"
72c894dca1SMatthias Ringwald    if not os.path.exists(project_folder):
73c894dca1SMatthias Ringwald        os.makedirs(project_folder)
74c894dca1SMatthias Ringwald
75c894dca1SMatthias Ringwald    # check if .gatt file is present
76c894dca1SMatthias Ringwald    gatt_path = examples_embedded + example + ".gatt"
77c894dca1SMatthias Ringwald    gatt_h = ""
78c894dca1SMatthias Ringwald    if os.path.exists(gatt_path):
79c894dca1SMatthias Ringwald        gatt_h = example+'.h'
80c894dca1SMatthias Ringwald
81c894dca1SMatthias Ringwald    # create makefile
82c894dca1SMatthias Ringwald    with open(project_folder + 'Makefile', 'wt') as fout:
83c894dca1SMatthias Ringwald        with open(template_path, 'rt') as fin:
84c894dca1SMatthias Ringwald            for line in fin:
85c894dca1SMatthias Ringwald                if 'PROJECT=spp_and_le_streamer' in line:
86c894dca1SMatthias Ringwald                    fout.write('PROJECT=%s\n' % example)
87c894dca1SMatthias Ringwald                    continue
88c894dca1SMatthias Ringwald                if 'all: spp_and_le_streamer.h' in line:
89c894dca1SMatthias Ringwald                    if len(gatt_h):
90c894dca1SMatthias Ringwald                        fout.write("all: %s\n" % gatt_h)
91c894dca1SMatthias Ringwald                    continue
92c894dca1SMatthias Ringwald                fout.write(line)
93c894dca1SMatthias Ringwald
94c894dca1SMatthias Ringwald    print("- %s" % example)
95c894dca1SMatthias Ringwald
96*8f3b3e6cSMatthias Ringwaldwith open(projects_path+'Makefile', 'wt') as fout:
97*8f3b3e6cSMatthias Ringwald    fout.write(build_all % ' \\\n'.join(examples))
98*8f3b3e6cSMatthias Ringwald
996b7adbe5SMatthias Ringwaldprint("Projects are ready for compile in example folder. See README for details.")
100