|
- #!/usr/bin/env python3
- # -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil -*-
- # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
- #
- # THIS FILE IS PART OF adtools PROJECT
- #
- # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- # Version 2, December 2004
- #
- # Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
- #
- # Everyone is permitted to copy and distribute verbatim or modified
- # copies of this license document, and changing it is allowed as long
- # as the name is changed.
- #
- # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- #
- # 0. You just DO WHAT THE FUCK YOU WANT TO.
-
- import os
- import sys
- import argparse
- import re
- from functools import cached_property
-
- from .__init__ import __version__
- from .adutil import AdUtil, Logger, D, W, E
- from .adgit import AdGitHelper
- from .command import Command
-
- __author__ = ['"anjingyu" <anjingyu_ws@foxmail.com>']
-
- SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-
-
- class AdMakeHelper(object):
- """Parse the cmake script and retrieve all the direct and in-direct dependencies"""
-
- def __init__(self, cur_dir="."):
- self._dir = os.path.abspath(cur_dir)
-
- def modules(self):
- all_modules = list()
- deps_dict = dict()
- mods = self.__modules(self._dir)
-
- for mod in mods:
- deps_dict[mod] = True
-
- for mod in mods:
- all_modules.extend(
- self.__deps(
- os.path.join(os.path.dirname(os.getenv("AD_TOOLS")), mod), deps_dict
- )
- )
-
- all_modules.extend(mods)
-
- return list(set(all_modules))
-
- def __deps(self, cur_dir=".", deps_dict={}):
- mods = self.__modules(cur_dir)
- deps = list()
-
- for mod in mods:
- if mod not in deps_dict:
- deps_dict[mod] = True
- deps.append(mod)
- deps.extend(
- self.__deps(
- os.path.join(os.path.dirname(Ad.get_env("AD_TOOLS")), mod),
- deps_dict,
- )
- )
-
- return deps
-
- def __modules(self, cur_dir="."):
- abspath = os.path.abspath(cur_dir)
- module_name = os.path.basename(abspath)
- cmake_file = os.path.join(abspath, "cmake", f"{module_name}.cmake")
- cmake_list_file = os.path.join(abspath, "CMakeLists.txt")
-
- mods = list()
-
- if os.path.isfile(cmake_file):
- # If the cmake_file is existent, means this project maybe a library
- mods.extend(self.__find_modules_in_file(cmake_file))
-
- if os.path.isfile(cmake_list_file):
- mods.extend(self.__find_modules_in_file(cmake_list_file))
-
- return list(set(mods))
-
- def __find_modules_in_file(self, file_path):
- result = list()
- with open(file_path) as f:
- # Findout all the `find_package` directive
- txt = f.read()
- mods = re.findall("find_package\s*\((.*?)\)", txt, re.M | re.S)
- for mod in mods:
- result.append(x.strip()[0])
- return result
-
-
- def main():
- with AdUtil.time_consumed():
- cmd = Command()
- return cmd.run(sys.argv)
-
-
- if __name__ == "__main__":
- sys.exit(main())
|