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', 'sco_demo_util.c', 'ant_test.c']: 62 continue 63 64 example = file[:-2] 65 gatt_path = examples_embedded + example + ".gatt" 66 67 # create folder 68 apps_folder = example_folder + example + "/" 69 if os.path.exists(apps_folder): 70 shutil.rmtree(apps_folder) 71 os.makedirs(apps_folder) 72 73 # copy files 74 for item in ['sdkconfig']: 75 src = script_path + '/template/' + item 76 if item == 'sdkconfig': 77 src = src + suffix 78 dst = apps_folder + '/' + item 79 shutil.copyfile(src, dst) 80 81 # create CMakeLists.txt file 82 with open(apps_folder + "CMakeLists.txt", "wt") as fout: 83 fout.write(example_cmake_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 84 85 # create main folder 86 main_folder = apps_folder + "main/" 87 if not os.path.exists(main_folder): 88 os.makedirs(main_folder) 89 90 # copy main file 91 shutil.copyfile(script_path + '/template/main/main.c', apps_folder + "/main/main.c") 92 93 # copy example file 94 main_files = '"main.c" "' + example + '.c"' 95 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 96 97 # add sco_demo_util.c for audio examples 98 if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 99 shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/') 100 shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/') 101 main_files += ' "sco_demo_util.c"' 102 103 # create CMakeLists.txt file 104 main_cmake_file = apps_folder + "/main/CMakeLists.txt" 105 with open(main_cmake_file, "wt") as fout: 106 fout.write(main_cmake_template.replace("MAIN_FILES", main_files)) 107 108 # add rules to compile gatt db if .gatt file is present 109 gatt_path = examples_embedded + example + ".gatt" 110 if os.path.exists(gatt_path): 111 shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt") 112 with open(main_cmake_file, "a") as fout: 113 fout.write(main_cmake_gatt_add_on.replace("EXAMPLE", example)) 114 print("- %s including GATT DB compilation rules" % example) 115 else: 116 print("- %s" % example) 117 118if __name__ == '__main__': 119 # get script path 120 script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 121 suffix = '' 122 if len(sys.argv) > 1: 123 suffix = sys.argv[1] 124 create_examples(script_path, suffix) 125 126 127