From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 17545A034C; Mon, 14 Feb 2022 12:37:06 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 11B94410F3; Mon, 14 Feb 2022 12:37:04 +0100 (CET) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mails.dpdk.org (Postfix) with ESMTP id ED72B4067E for ; Mon, 14 Feb 2022 12:37:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644838621; x=1676374621; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pjX+mx97VoeEUCRAGvzhtBBr34EDSL09KhLFuikBuwg=; b=kYv3V0XmpasUD3XfG9sLfd+HM57VmtCX5oVWH18sVUEG2zUb3fDqhTNA izcaUtH3mbEyWmiXV1o9p6cLV/N5JobwWfqXKDQpz4eBPoZoLQ3Ax8jGR 9Sd4mZH6BHKw3469XIs4m1Mmq+qqFkasBxmSbTBTzZ+IewzaL9MdebtVY hdkzGM8BsEVavIzgKe8BlTPbxrelwFQg/QCptjsodghtBZmGhq6ukzzwf y1IWEdR70Maf7Ji56IKVaSuZzyXItT98Jdsx6aoxEXlC+CJpJGGGthlnH vx1yyarEdT4B+8jlBUjm++apQn1vFQ8d+1AG+df9ktqYtPTMHxb6ZfFwv w==; X-IronPort-AV: E=McAfee;i="6200,9189,10257"; a="233619803" X-IronPort-AV: E=Sophos;i="5.88,367,1635231600"; d="scan'208";a="233619803" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2022 03:37:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,367,1635231600"; d="scan'208";a="632086854" Received: from silpixa00401215.ir.intel.com ([10.55.128.96]) by fmsmga002.fm.intel.com with ESMTP; 14 Feb 2022 03:36:58 -0800 From: Sean Morrissey To: Cc: dev@dpdk.org, Sean Morrissey , Conor Fogarty , Bruce Richardson Subject: [PATCH v7 01/50] devtools: script to remove unused headers includes Date: Mon, 14 Feb 2022 11:35:43 +0000 Message-Id: <20220214113632.3184921-2-sean.morrissey@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220214113632.3184921-1-sean.morrissey@intel.com> References: <20220202094802.3618978-1-sean.morrissey@intel.com> <20220214113632.3184921-1-sean.morrissey@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This script can be used for removing headers flagged for removal by the include-what-you-use (IWYU) tool. The script has the ability to remove headers from specified sub-directories or dpdk as a whole and tests the build after each removal by calling meson compile. example usages: Remove headers flagged by iwyu_tool output file $ ./devtools/process_iwyu.py iwyu.out -b build Remove headers flagged by iwyu_tool output file from sub-directory $ ./devtools/process_iwyu.py iwyu.out -b build -d lib/kvargs Remove headers directly piped from the iwyu_tool $ iwyu_tool -p build | ./devtools/process_iwyu.py - -b build Signed-off-by: Sean Morrissey Signed-off-by: Conor Fogarty Reviewed-by: Bruce Richardson --- devtools/process_iwyu.py | 109 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 devtools/process_iwyu.py diff --git a/devtools/process_iwyu.py b/devtools/process_iwyu.py new file mode 100755 index 0000000000..50f3d4c5c7 --- /dev/null +++ b/devtools/process_iwyu.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2021 Intel Corporation +# + +import argparse +import fileinput +import sys +from os.path import abspath, relpath, join +from pathlib import Path +from mesonbuild import mesonmain + + +def args_parse(): + "parse arguments and return the argument object back to main" + parser = argparse.ArgumentParser(description="This script can be used to remove includes which are not in use\n") + parser.add_argument('-b', '--build_dir', type=str, default='build', + help="The path to the build directory in which the IWYU tool was used in.") + parser.add_argument('-d', '--sub_dir', type=str, default='', + help="The sub-directory to remove headers from.") + parser.add_argument('file', type=Path, + help="The path to the IWYU log file or output from stdin.") + + return parser.parse_args() + + +def run_meson(args): + "Runs a meson command logging output to process.log" + with open('process_iwyu.log', 'a') as sys.stdout: + ret = mesonmain.run(args, abspath('meson')) + sys.stdout = sys.__stdout__ + return ret + + +def remove_includes(filepath, include, build_dir): + "Attempts to remove include, if it fails then revert to original state" + with open(filepath) as f: + lines = f.readlines() # Read lines when file is opened + + with open(filepath, 'w') as f: + for ln in lines: # Removes the include passed in + if not ln.startswith(include): + f.write(ln) + + # run test build -> call meson on the build folder, meson compile -C build + ret = run_meson(['compile', '-C', build_dir]) + if (ret == 0): # Include is not needed -> build is successful + print('SUCCESS') + else: + # failed, catch the error + # return file to original state + with open(filepath, 'w') as f: + f.writelines(lines) + print('FAILED') + + +def get_build_config(builddir, condition): + "returns contents of rte_build_config.h" + with open(join(builddir, 'rte_build_config.h')) as f: + return [ln for ln in f.readlines() if condition(ln)] + + +def uses_libbsd(builddir): + "return whether the build uses libbsd or not" + return bool(get_build_config(builddir, lambda ln: 'RTE_USE_LIBBSD' in ln)) + + +def process(args): + "process the iwyu output on a set of files" + filepath = None + build_dir = abspath(args.build_dir) + directory = args.sub_dir + + print("Warning: The results of this script may include false positives which are required for different systems", + file=sys.stderr) + + keep_str_fns = uses_libbsd(build_dir) # check for libbsd + if keep_str_fns: + print("Warning: libbsd is present, build will fail to detect incorrect removal of rte_string_fns.h", + file=sys.stderr) + # turn on werror + run_meson(['configure', build_dir, '-Dwerror=true']) + # Use stdin if no iwyu_tool out file given + for line in fileinput.input(args.file): + if 'should remove' in line: + # If the file path in the iwyu_tool output is an absolute path it + # means the file is outside of the dpdk directory, therefore ignore it. + # Also check to see if the file is within the specified sub directory. + filename = line.split()[0] + if (filename != abspath(filename) and + directory in filename): + filepath = relpath(join(build_dir, filename)) + elif line.startswith('-') and filepath: + include = '#include ' + line.split()[2] + print(f"Remove {include} from {filepath} ... ", end='', flush=True) + if keep_str_fns and '' in include: + print('skipped') + continue + remove_includes(filepath, include, build_dir) + else: + filepath = None + + +def main(): + process(args_parse()) + + +if __name__ == '__main__': + main() -- 2.25.1