深入了解编译构建子系统

基于4.1-release,(因为本人主c++,对于python中的一些方法可能有误欢迎大家指出)

一.build.py解读

命令行执行./build.py --product-name oriole –ccache --no-prebuilt-sdk

调用当前目录下的build.py脚本文件后面为相应参数

1.进入main函数

def main():
    root_path = find_top()
    return build(root_path, sys.argv[1:])
​
​
if __name__ == "__main__":
    sys.exit(main())

因此,执行到到 if __name__ == "__main__": 这个条件,调用 main() 函数,

root_path = find_top()调用前面定义的函数

2.进入find_top()函数

def find_top() -> str:
    cur_dir = os.getcwd()
    while cur_dir != "/":
        build_config_file = os.path.join(
            cur_dir, 'build/config/BUILDCONFIG.gn')
        if os.path.exists(build_config_file):
            return cur_dir
        cur_dir = os.path.dirname(cur_dir)

#find_top() -> str 以字符串的格式返回结果

类似python导入os,在 C++ 中,你可以使用 <filesystem> 标准库(C++17 引入)来进行文件系统操作,其中包含了一系列用于文件路径操作的函数和类。比如,你可以使用 std::filesystem::path 类来表示文件路径,并进行路径的拼接、分解等操作。

举个例子,你可以像这样使用 <filesystem> 标准库来构建文件路径:

#include <iostream>
#include <filesystem>
​
namespace fs = std::filesystem;
​
int main() {
    fs::path cur_dir = fs::current_path();
    fs::path build_config_file = cur_dir / "build" / "config" / "BUILDCONFIG.gn";
​
    std::cout << "Build config file path: " << build_config_file << std::endl;
​
    return 0;
}

这段代码会输出构建的文件路径,其中 cur_dir 是当前工作目录的路径,build_config_file 是要构建的文件的路径。通过使用 <filesystem> 标准库,你可以方便地进行跨平台的文件系统操作。

while cur_dir != "/": #当路径不为顶级目录 os.getcwd() 在根目录执行时会返回空字符串而不是根目录本身,所以即使是根目录也会执行循环

build_config_file = os.path.join(cur_dir, 'build/config/BUILDCONFIG.gn')

#os.path.join拼接两个路径并传给build_config_file

if os.path.exists(build_config_file): return cur_dir

cur_dir = os.path.dirname(cur_dir) # 在则跳出循环返回cur_dir给主函数,否则就向上(父级目录)找

3.进入主函数

root_path = find_top() #主函数读取到字符串, return build(root_path, sys.argv[1:])

4,进入build函数

#执行build函数(当前路径,接受敲入命令行第一个以外的所有参数)及./build.py除外 --product-name oriole –ccache -- no-prebuilt-sdk 其作用既是构建该路径下的项目

def build(path: str, args_list: list) -> str:
    python_dir = None
    if "--python-dir" in args_list:
        index = args_list.index("--python-dir")
        if index < len(args_list) - 1:
            python_dir = args_list[index + 1]
            del args_list[index: index + 2]
        else:
            print("-python-dir parmeter missing value.")
            sys.exit()
    python_executable = get_python(python_dir)
    cmd = [python_executable, 'build/hb/main.py', 'build'] + args_list
    return check_output(cmd, cwd=path)

--python-dir 是一个选项,用于指定 Python 的安装目录或路径。通常情况下,它可以让用户指定程序在哪里找到 Python 解释器或 Python 模块

如果 --python-dir 存在于参数列表中,就会找到它在列表中的位置, if index < len(args_list) - 1:python_dir = args_list[index + 1]检查其后是否存在对应的值。如果存在值,则将其赋给 python_dir 变量,并从参数列表中删除 --python-dir 及其对应的值。如果不存在对应的值,则输出错误信息并退出程序。

所以命令行中没有指定python路径不进入判断

python_executable = get_python(python_dir) 这行代码的作用是根据指定的 Python 路径(python_dir)来获取 Python 解释器的可执行文件路径。

在 Unix/Linux 系统上,Python 解释器通常被安装在 /usr/bin/python 或者 /usr/local/bin/python 等位置

5.进入get_python函数

函数的作用是根据给定的相对路径找到 Python 解释器的可执行文件路径。

def get_python(python_relative_dir: str) -> str:
    topdir = find_top()
    if python_relative_dir is None:
        python_relative_dir = 'prebuilts/python'
    python_base_dir = os.path.join(topdir, python_relative_dir)
​
    if os.path.exists(python_base_dir):
        python_dir = search(python_base_dir, 'python3')
        return os.path.join(python_dir, 'python3')
    else:
        print("please execute build/prebuilts_download.sh.",
            "if you used '--python-dir', check whether the input path is valid.")
        sys.exit()

get_python 函数,接受一个参数 python_relative_dir,(我们这里应该为none)。

函数调用了 find_top() 函数来获取顶级目录的路径,

然后,如果未提供 python_relative_dir 参数,函数将其默认设置为 'prebuilts/python'。这意味着如果用户没有指定 Python 的相对路径,将使用默认路径 'prebuilts/python'。也就是我们在配置环境时build/prebuilts_download.sh

接着,函数将顶级目录路径和 Python 相对路径拼接起来,得到 Python 的基础目录路径 python_base_dir

接下来,函数检查 python_base_dir 是否存在。如果存在,函数调用了 search() 函数来在 Python 基础目录中查找名为 'python3' 的文件。

def search(findir: str, target: str) -> str or bool:
    for root, _, files in os.walk(findir):
        if target in files:
            ret
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值