xref: /btstack/port/esp32/create_examples.py (revision c2cab3fe946018ab554adcc484d0964427940b89)
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
11example_cmake_template = '''
12# BTstack example 'EXAMPLE' for ESP32 port
13#
14# Generated by TOOL
15# On DATE
16
17# The following lines of boilerplate have to be in your project's
18# CMakeLists in this exact order for cmake to work correctly
19cmake_minimum_required(VERSION 3.5)
20
21include($ENV{IDF_PATH}/tools/cmake/project.cmake)
22project(EXAMPLE)
23'''
24
25main_cmake_template = '''
26idf_component_register(
27        SRCS MAIN_FILES
28        INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}")
29'''
30
31main_cmake_gatt_add_on = '''
32if(NOT CMAKE_BUILD_EARLY_EXPANSION)
33    add_custom_command(
34            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h
35            COMMAND ${CMAKE_SOURCE_DIR}/../../../../tool/compile_gatt.py ${COMPONENT_DIR}/EXAMPLE.gatt ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h
36            DEPENDS EXAMPLE.gatt
37            VERBATIM
38    )
39    add_custom_target(GATT_DB DEPENDS EXAMPLE.h)
40    add_dependencies(${COMPONENT_LIB} GATT_DB)
41endif()
42'''
43
44def create_examples(script_path, suffix):
45    # path to examples
46    examples_embedded = script_path + "/../../example/"
47
48    # path to samples
49    example_folder = script_path + "/example" + suffix + "/"
50
51    print("Creating examples folder")
52    if not os.path.exists(example_folder):
53        os.makedirs(example_folder)
54
55    print("Creating examples in examples folder")
56
57    # iterate over btstack examples
58    for file in os.listdir(examples_embedded):
59        if not file.endswith(".c"):
60            continue
61        if file in ['panu_demo.c', 'ant_test.c']:
62            continue
63        if 'demo_util' in file:
64            continue
65
66        example = file[:-2]
67        gatt_path = examples_embedded + example + ".gatt"
68
69        # create folder
70        apps_folder = example_folder + example + "/"
71        if os.path.exists(apps_folder):
72            shutil.rmtree(apps_folder)
73        os.makedirs(apps_folder)
74
75        # copy files
76        for item in ['sdkconfig']:
77            src = script_path + '/template/' + item
78            if item == 'sdkconfig':
79                src = src + suffix
80            dst = apps_folder + '/' + item
81            shutil.copyfile(src, dst)
82
83        # create CMakeLists.txt file
84        with open(apps_folder + "CMakeLists.txt", "wt") as fout:
85            fout.write(example_cmake_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
86
87        # create main folder
88        main_folder = apps_folder + "main/"
89        if not os.path.exists(main_folder):
90            os.makedirs(main_folder)
91
92        # copy main file
93        shutil.copyfile(script_path + '/template/main/main.c', apps_folder + "/main/main.c")
94
95        # copy example file
96        main_files = '"main.c" "' + example + '.c"'
97        shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
98
99        # add sco_demo_util.c for audio examples
100        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
101            shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
102            shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
103            main_files += ' "sco_demo_util.c"'
104
105        # create CMakeLists.txt file
106        main_cmake_file = apps_folder + "/main/CMakeLists.txt"
107        with open(main_cmake_file, "wt") as fout:
108            fout.write(main_cmake_template.replace("MAIN_FILES", main_files))
109
110        # add rules to compile gatt db if .gatt file is present
111        gatt_path = examples_embedded + example + ".gatt"
112        if os.path.exists(gatt_path):
113            shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt")
114            with open(main_cmake_file, "a") as fout:
115                fout.write(main_cmake_gatt_add_on.replace("EXAMPLE", example))
116            print("- %s including GATT DB compilation rules" % example)
117        else:
118            print("- %s" % example)
119
120if __name__ == '__main__':
121    # get script path
122    script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
123    suffix = ''
124    if len(sys.argv) > 1:
125        suffix = sys.argv[1]
126    create_examples(script_path, suffix)
127
128
129