xref: /btstack/port/esp32/create_examples.py (revision 6ccd8248590f666db07dd7add13fecb4f5664fb5)
1#!/usr/bin/env python3
2#
3# Create project files for all BTstack embedded examples in local port/esp32 folder
4
5import os
6import shutil
7import sys
8import time
9import subprocess
10
11mk_template = '''#
12# BTstack example 'EXAMPLE' for ESP32 port
13#
14# Generated by TOOL
15# On DATE
16
17PROJECT_NAME := EXAMPLE
18
19include $(IDF_PATH)/make/project.mk
20'''
21
22component_mk_gatt_add_on = '''
23# app depends on compiled gatt db
24EXAMPLE.o: EXAMPLE.h
25
26# rule to compile gatt db
27EXAMPLE.h: $(COMPONENT_PATH)/EXAMPLE.gatt
28\t$(IDF_PATH)/components/btstack/tool/compile_gatt.py $^ $@
29
30# remove compiled gatt db on clean
31COMPONENT_EXTRA_CLEAN = EXAMPLE.h
32'''
33
34example_cmake_template = '''
35# BTstack example 'EXAMPLE' for ESP32 port
36#
37# Generated by TOOL
38# On DATE
39
40# The following lines of boilerplate have to be in your project's
41# CMakeLists in this exact order for cmake to work correctly
42cmake_minimum_required(VERSION 3.5)
43
44include($ENV{IDF_PATH}/tools/cmake/project.cmake)
45project(EXAMPLE)
46'''
47
48main_cmake_template = '''
49idf_component_register(
50        SRCS "EXAMPLE.c" "main.c"
51        INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}")
52'''
53
54main_cmake_gatt_add_on = '''
55if(NOT CMAKE_BUILD_EARLY_EXPANSION)
56    add_custom_command(
57            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h
58            COMMAND ${CMAKE_SOURCE_DIR}/../../../../tool/compile_gatt.py ${COMPONENT_DIR}/EXAMPLE.gatt ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h
59            DEPENDS EXAMPLE.gatt
60            VERBATIM
61    )
62    add_custom_target(GATT_DB DEPENDS EXAMPLE.h)
63    add_dependencies(${COMPONENT_LIB} GATT_DB)
64endif()
65'''
66
67def create_examples(script_path, suffix):
68    # path to examples
69    examples_embedded = script_path + "/../../example/"
70
71    # path to samples
72    example_folder = script_path + "/example" + suffix + "/"
73
74    print("Creating examples folder")
75    if not os.path.exists(example_folder):
76        os.makedirs(example_folder)
77
78    print("Creating examples in examples folder")
79
80    # iterate over btstack examples
81    for file in os.listdir(examples_embedded):
82        if not file.endswith(".c"):
83            continue
84        if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']:
85            continue
86
87        example = file[:-2]
88        gatt_path = examples_embedded + example + ".gatt"
89
90        # create folder
91        apps_folder = example_folder + example + "/"
92        if os.path.exists(apps_folder):
93            shutil.rmtree(apps_folder)
94        os.makedirs(apps_folder)
95
96        # copy files
97        for item in ['sdkconfig', 'set_port.sh']:
98            src = script_path + '/template/' + item
99            if item == 'sdkconfig':
100                src = src + suffix
101            dst = apps_folder + '/' + item
102            shutil.copyfile(src, dst)
103
104        # mark set_port.sh as executable
105        os.chmod(apps_folder + '/set_port.sh', 0o755)
106
107        # create Makefile file
108        with open(apps_folder + "Makefile", "wt") as fout:
109            fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
110
111        # create CMakeLists.txt file
112        with open(apps_folder + "CMakeLists.txt", "wt") as fout:
113            fout.write(example_cmake_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
114
115        # create main folder
116        main_folder = apps_folder + "main/"
117        if not os.path.exists(main_folder):
118            os.makedirs(main_folder)
119
120        # copy main file
121        shutil.copyfile(script_path + '/template/main/main.c', apps_folder + "/main/main.c")
122
123        # copy example file
124        shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
125
126        # add sco_demo_util.c for audio examples
127        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
128            shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
129            shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
130
131        # add component.mk file to main folder
132        main_component_mk = apps_folder + "/main/component.mk"
133        shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk)
134
135        # create CMakeLists.txt file
136        main_cmake_file = apps_folder + "/main/CMakeLists.txt"
137        with open(main_cmake_file, "wt") as fout:
138            fout.write(main_cmake_template.replace("EXAMPLE", example))
139
140        # add rules to compile gatt db if .gatt file is present
141        gatt_path = examples_embedded + example + ".gatt"
142        if os.path.exists(gatt_path):
143            shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt")
144            with open(main_component_mk, "a") as fout:
145                fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example))
146            with open(main_cmake_file, "a") as fout:
147                fout.write(main_cmake_gatt_add_on.replace("EXAMPLE", example))
148            print("- %s including GATT DB compilation rules" % example)
149        else:
150            print("- %s" % example)
151
152if __name__ == '__main__':
153    # get script path
154    script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
155    suffix = ''
156    if len(sys.argv) > 1:
157        suffix = sys.argv[1]
158    create_examples(script_path, suffix)
159
160
161