1#!/usr/bin/env python3 2 3import glob 4import re 5import sys 6import os 7import datetime 8from pathlib import Path 9 10program_info = ''' 11Generate .h and .c with BTstack copyright header in BTstack tree 12 13Usage: tool/btstack_code_template.py path/filename (without extension) 14''' 15 16copyright = """/* 17 * Copyright (C) {copyright_year} BlueKitchen GmbH 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 23 * 1. Redistributions of source code must retain the above copyright 24 * notice, this list of conditions and the following disclaimer. 25 * 2. Redistributions in binary form must reproduce the above copyright 26 * notice, this list of conditions and the following disclaimer in the 27 * documentation and/or other materials provided with the distribution. 28 * 3. Neither the name of the copyright holders nor the names of 29 * contributors may be used to endorse or promote products derived 30 * from this software without specific prior written permission. 31 * 4. Any redistribution, use, or modification is done solely for 32 * personal benefit and not for any commercial purpose or for 33 * monetary gain. 34 * 35 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 39 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 43 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 45 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * Please inquire about commercial licensing options at 49 * [email protected] 50 * 51 */ 52""" 53 54include_template = ''' 55/** 56 * @brief TODO 57 */ 58 59#ifndef {include_guard} 60#define {include_guard} 61 62#if defined __cplusplus 63extern "C" {{ 64#endif 65 66#if defined __cplusplus 67}} 68#endif 69#endif // {include_guard} 70''' 71 72source_template = ''' 73#define BTSTACK_FILE__ "{btstack_file}" 74 75#include "{header_file}" 76 77#include "btstack_bool.h" 78#include "btstack_config.h" 79#include "btstack_debug.h" 80 81#include <stdint.h> 82''' 83 84btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 85 86if len(sys.argv) == 1: 87 print(program_info) 88 sys.exit(0) 89 90path = sys.argv[1] 91path_object = Path(path) 92 93# include file 94path_include = path + '.h' 95copyright_year = datetime.datetime.now().year 96include_guard = path_object.name.replace('/','_').upper()+'_H' 97with open(btstack_root + '/' + path_include, 'wt') as fout: 98 fout.write(copyright.format(copyright_year=copyright_year)) 99 fout.write(include_template.format(include_guard=include_guard)) 100 101# source file 102path_source = path + ".c" 103btstack_file = path_object.name + '.c' 104if path_include.startswith('src/'): 105 # keep path for src/ folder 106 header_file = path_include.replace('src/','') 107else: 108 # only use filename for everything else 109 header_file = Path(path).name + '.h' 110with open(btstack_root + '/' + path_source, 'wt') as fout: 111 fout.write(copyright.format(copyright_year=copyright_year)) 112 fout.write(source_template.format(btstack_file=btstack_file,header_file=header_file)) 113 114print("Generated {file}.h and {file}.c in {path}".format(file=path_object.name,path=str(path_object.parent))) 115