1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2021, The OpenThread Authors. 4*cfb92d14SAndroid Build Coastguard Worker# All rights reserved. 5*cfb92d14SAndroid Build Coastguard Worker# 6*cfb92d14SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 7*cfb92d14SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 8*cfb92d14SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright 9*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer. 10*cfb92d14SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright 11*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer in the 12*cfb92d14SAndroid Build Coastguard Worker# documentation and/or other materials provided with the distribution. 13*cfb92d14SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the 14*cfb92d14SAndroid Build Coastguard Worker# names of its contributors may be used to endorse or promote products 15*cfb92d14SAndroid Build Coastguard Worker# derived from this software without specific prior written permission. 16*cfb92d14SAndroid Build Coastguard Worker# 17*cfb92d14SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*cfb92d14SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*cfb92d14SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*cfb92d14SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*cfb92d14SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*cfb92d14SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*cfb92d14SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*cfb92d14SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*cfb92d14SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*cfb92d14SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*cfb92d14SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*cfb92d14SAndroid Build Coastguard Worker# 29*cfb92d14SAndroid Build Coastguard Worker 30*cfb92d14SAndroid Build Coastguard Worker# This script updates different make/build files (CMakeLists.txt, BUILD.gn, 31*cfb92d14SAndroid Build Coastguard Worker# Android.mk, Android.bp, auto-make) in OpenThread repo based on the 32*cfb92d14SAndroid Build Coastguard Worker# current files present in `./src/core/` & `./include/openthread/` 33*cfb92d14SAndroid Build Coastguard Worker# folders. This script MUST be called from openthread root folder. 34*cfb92d14SAndroid Build Coastguard Worker 35*cfb92d14SAndroid Build Coastguard Workerimport os 36*cfb92d14SAndroid Build Coastguard Worker 37*cfb92d14SAndroid Build Coastguard Worker#---------------------------------------------------------------------------------------------- 38*cfb92d14SAndroid Build Coastguard Worker# Helper functions 39*cfb92d14SAndroid Build Coastguard Worker 40*cfb92d14SAndroid Build Coastguard Worker 41*cfb92d14SAndroid Build Coastguard Workerdef get_file_list(path, extension): 42*cfb92d14SAndroid Build Coastguard Worker """Get a sorted list of full file names (with path) in a given `path` folder having a given `extension`""" 43*cfb92d14SAndroid Build Coastguard Worker return sorted([ 44*cfb92d14SAndroid Build Coastguard Worker "{}/{}".format(dir_path, file_name)[2:] 45*cfb92d14SAndroid Build Coastguard Worker for dir_path, dir_names, file_names in os.walk(path) 46*cfb92d14SAndroid Build Coastguard Worker for file_name in file_names 47*cfb92d14SAndroid Build Coastguard Worker if file_name.endswith(extension) 48*cfb92d14SAndroid Build Coastguard Worker ]) 49*cfb92d14SAndroid Build Coastguard Worker 50*cfb92d14SAndroid Build Coastguard Worker 51*cfb92d14SAndroid Build Coastguard Workerdef read_txt_file(file_name): 52*cfb92d14SAndroid Build Coastguard Worker """Read the content of a text file with name `file_name` and return content as a list of lines""" 53*cfb92d14SAndroid Build Coastguard Worker with open(file_name, 'r') as file: 54*cfb92d14SAndroid Build Coastguard Worker lines = file.readlines() 55*cfb92d14SAndroid Build Coastguard Worker return lines 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Worker 58*cfb92d14SAndroid Build Coastguard Workerdef write_txt_file(file_name, lines): 59*cfb92d14SAndroid Build Coastguard Worker """Write a text file with name `file_name` with the content given as a list of `lines`""" 60*cfb92d14SAndroid Build Coastguard Worker with open(file_name, 'w') as file: 61*cfb92d14SAndroid Build Coastguard Worker file.writelines(lines) 62*cfb92d14SAndroid Build Coastguard Worker 63*cfb92d14SAndroid Build Coastguard Worker 64*cfb92d14SAndroid Build Coastguard Workerdef update_build_file(file_name, start_string, end_string, new_list, search_string=None): 65*cfb92d14SAndroid Build Coastguard Worker """ 66*cfb92d14SAndroid Build Coastguard Worker Update the file given by `file_name` by replacing the list after the first occurrence of `start_string` up to 67*cfb92d14SAndroid Build Coastguard Worker `end_string` with the `new_list`. If `search_string` is given, then the search for `start_string` only starts 68*cfb92d14SAndroid Build Coastguard Worker after seeing the `search_string` line in the file. 69*cfb92d14SAndroid Build Coastguard Worker """ 70*cfb92d14SAndroid Build Coastguard Worker STATE_SEARCH = 1 71*cfb92d14SAndroid Build Coastguard Worker STATE_MATCH_START = 2 72*cfb92d14SAndroid Build Coastguard Worker STATE_MATCH_END = 3 73*cfb92d14SAndroid Build Coastguard Worker STATE_DONE = 4 74*cfb92d14SAndroid Build Coastguard Worker 75*cfb92d14SAndroid Build Coastguard Worker lines = read_txt_file(file_name) 76*cfb92d14SAndroid Build Coastguard Worker new_lines = [] 77*cfb92d14SAndroid Build Coastguard Worker state = STATE_SEARCH if search_string else STATE_MATCH_START 78*cfb92d14SAndroid Build Coastguard Worker 79*cfb92d14SAndroid Build Coastguard Worker for line in lines: 80*cfb92d14SAndroid Build Coastguard Worker if state == STATE_SEARCH: 81*cfb92d14SAndroid Build Coastguard Worker new_lines.append(line) 82*cfb92d14SAndroid Build Coastguard Worker if line.startswith(search_string): 83*cfb92d14SAndroid Build Coastguard Worker state = STATE_MATCH_START 84*cfb92d14SAndroid Build Coastguard Worker elif state == STATE_MATCH_START: 85*cfb92d14SAndroid Build Coastguard Worker new_lines.append(line) 86*cfb92d14SAndroid Build Coastguard Worker if line.startswith(start_string): 87*cfb92d14SAndroid Build Coastguard Worker new_lines.extend(new_list) 88*cfb92d14SAndroid Build Coastguard Worker state = STATE_MATCH_END 89*cfb92d14SAndroid Build Coastguard Worker elif state == STATE_MATCH_END: 90*cfb92d14SAndroid Build Coastguard Worker if line.startswith(end_string): 91*cfb92d14SAndroid Build Coastguard Worker new_lines.append(line) 92*cfb92d14SAndroid Build Coastguard Worker state = STATE_DONE 93*cfb92d14SAndroid Build Coastguard Worker else: 94*cfb92d14SAndroid Build Coastguard Worker new_lines.append(line) 95*cfb92d14SAndroid Build Coastguard Worker 96*cfb92d14SAndroid Build Coastguard Worker if state != STATE_DONE: 97*cfb92d14SAndroid Build Coastguard Worker raise RuntimeError('failed to update build file: {}'.format(file_name)) 98*cfb92d14SAndroid Build Coastguard Worker 99*cfb92d14SAndroid Build Coastguard Worker if new_lines != lines: 100*cfb92d14SAndroid Build Coastguard Worker write_txt_file(file_name, new_lines) 101*cfb92d14SAndroid Build Coastguard Worker 102*cfb92d14SAndroid Build Coastguard Worker 103*cfb92d14SAndroid Build Coastguard Worker#---------------------------------------------------------------------------------------------- 104*cfb92d14SAndroid Build Coastguard Worker 105*cfb92d14SAndroid Build Coastguard Worker# Get the list of hpp/h/cpp files in different folders. 106*cfb92d14SAndroid Build Coastguard Worker 107*cfb92d14SAndroid Build Coastguard Workersrc_core_path = "./src/core" 108*cfb92d14SAndroid Build Coastguard Workercore_h_files = get_file_list(src_core_path, '.h') 109*cfb92d14SAndroid Build Coastguard Workercore_hpp_files = get_file_list(src_core_path, '.hpp') 110*cfb92d14SAndroid Build Coastguard Workercore_cpp_files = get_file_list(src_core_path, '.cpp') 111*cfb92d14SAndroid Build Coastguard Workercore_cpp_files = [item for item in core_cpp_files if not item.endswith("extension_example.cpp")] 112*cfb92d14SAndroid Build Coastguard Workercore_hpp_cpp_files = sorted(core_hpp_files + core_cpp_files) 113*cfb92d14SAndroid Build Coastguard Workercore_h_hpp_files = sorted(core_h_files + core_hpp_files) 114*cfb92d14SAndroid Build Coastguard Worker 115*cfb92d14SAndroid Build Coastguard Workerinclude_path = "./include/openthread" 116*cfb92d14SAndroid Build Coastguard Workerinclude_h_files = get_file_list(include_path, '.h') 117*cfb92d14SAndroid Build Coastguard Workerinclude_ot_h_files = [name[8:] for name in include_h_files if not name.startswith("include/openthread/platform")] 118*cfb92d14SAndroid Build Coastguard Workerinclude_platform_h_files = [name[8:] for name in include_h_files if name.startswith("include/openthread/platform")] 119*cfb92d14SAndroid Build Coastguard Worker 120*cfb92d14SAndroid Build Coastguard Worker#---------------------------------------------------------------------------------------------- 121*cfb92d14SAndroid Build Coastguard Worker# Update CMakeLists.txt files 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid Build Coastguard Workercore_cmakelist_txt_file = "./src/core/CMakeLists.txt" 124*cfb92d14SAndroid Build Coastguard Worker 125*cfb92d14SAndroid Build Coastguard Workerformatted_list = [" {}\n".format(file_name[9:]) for file_name in core_cpp_files] 126*cfb92d14SAndroid Build Coastguard Workerupdate_build_file(core_cmakelist_txt_file, "set(COMMON_SOURCES\n", ")\n", formatted_list) 127*cfb92d14SAndroid Build Coastguard Worker 128*cfb92d14SAndroid Build Coastguard Workerprint("Updated " + core_cmakelist_txt_file) 129*cfb92d14SAndroid Build Coastguard Worker 130*cfb92d14SAndroid Build Coastguard Worker#---------------------------------------------------------------------------------------------- 131*cfb92d14SAndroid Build Coastguard Worker# Update Build.gn files 132*cfb92d14SAndroid Build Coastguard Worker 133*cfb92d14SAndroid Build Coastguard Workercore_build_gn_file = "./src/core/BUILD.gn" 134*cfb92d14SAndroid Build Coastguard Worker 135*cfb92d14SAndroid Build Coastguard Workerformatted_list = [" \"{}\",\n".format(file_name[9:]) for file_name in core_hpp_cpp_files] 136*cfb92d14SAndroid Build Coastguard Workerupdate_build_file(core_build_gn_file, "openthread_core_files = [\n", "]\n", formatted_list) 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Workerformatted_list = [" \"{}\",\n".format(file_name[9:]) for file_name in core_h_files] 139*cfb92d14SAndroid Build Coastguard Workerupdate_build_file(core_build_gn_file, " public = [\n", " ]\n", formatted_list) 140*cfb92d14SAndroid Build Coastguard Worker 141*cfb92d14SAndroid Build Coastguard Workerprint("Updated " + core_build_gn_file) 142*cfb92d14SAndroid Build Coastguard Worker 143*cfb92d14SAndroid Build Coastguard Workerinclude_build_gn_file = "./include/openthread/BUILD.gn" 144*cfb92d14SAndroid Build Coastguard Worker 145*cfb92d14SAndroid Build Coastguard Workerformatted_list = [" \"{}\",\n".format(file_name[19:]) for file_name in include_h_files] 146*cfb92d14SAndroid Build Coastguard Workerupdate_build_file(include_build_gn_file, " public = [\n", " ]\n", formatted_list) 147*cfb92d14SAndroid Build Coastguard Worker 148*cfb92d14SAndroid Build Coastguard Workerprint("Updated " + include_build_gn_file) 149