长沙百度网站制作,国际阿里巴巴官网首页,模板和网站是一体的吗,百度推广排名代发本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
本作品 (李兆龙 博文, 由 李兆龙 创作)#xff0c;由 李兆龙 确认#xff0c;转载请注明版权。 文章目录 正文样例代码 正文
Bazel项目引用仅使用Cmake依赖项目#xff0c;目前业界最为普遍…本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
本作品 (李兆龙 博文, 由 李兆龙 创作)由 李兆龙 确认转载请注明版权。 文章目录 正文样例代码 正文
Bazel项目引用仅使用Cmake依赖项目目前业界最为普遍的集成方法是将依赖项目中需要的全部文件打包成一个Bazel中的Target。
原生支持Bazel的项目一般会使用细粒度的Target划分项目就像Cmake中在不同的模块使用add_library和target_include_directories打包成.a最后在生成可执行程序时一并链接一来可以增加测试代码的编译速度二来项目划分也更为清晰。
Bazel Cpp集成一个复杂项目时一般存在很多麻烦包括不限于
符号冲突多个编译单元编译选项不同导致实例化不同链接失败编译选项确实或错误繁杂的库依赖包括依赖的依赖特殊版本库依赖
所以如果把所有的代码集成到一个Target同时编译开始报错会非常多而且因为多线程编译每次的报错还不太一样。很自然的思路就是是否可以逐模块引入依赖项目
来想下一般Cmake的编译流程
各个模块所有的文件执行预处理编译汇编生成多个.o文件每一个cpp是一个编译单元ar将一个模块的文件打包为一个静态库此时还没有链接每个.a中符号调用还没有分配偏移地址生成可执行文件链接基础依赖库和之前生成的所有静态库
Bazel的原理和上述流程基本一致但是有一个更强的保证即多个Target之间不允许循环依赖。
这有助于让代码的结构更为清晰但是对于细粒度的集成依赖来说是一切灾难的开始。
举个简单的例子
// A.cpp
#include A.hint main()
{return 0;
}// A.h
#include B.h// B.cpp
#include A.h// B.h
#include xxxxxx
这种情况下Cmake是不存在循环依赖的因为不存在头文件的互相依赖B.o和A.o在链接阶段会互相找到符号的定义。但是在Bazel中就不一样了因为Target 必须包含对方的定义也就成了
// BUILD.a
cc_library(name A,srcs [ A.h, A.cpp ],includes [lib],deps [//xxx:B,],
)// BUILD.b
cc_library(name B,srcs [ B.h, B.cpp ],includes [lib],deps [//xxx:A,],
)还没有进入链接阶段在Bazel的准备阶段就已经报错循环引用了。这种情况就只能把A和B包含为一个Target。
如何判断Bazel集成仅使用Cmake的依赖项是否可以细粒度拆分呢步骤其实很清晰即
把编译的过程看做一个有向图每个cpp文件是一个节点cpp文件包含的.h和cpp文件对应的.h包含的所有.h为有向边
这种情况下判断是否存在环。
此时对上一轮发现的环执行缩点忽略不是环的节点但是保留缩点后的和其他缩点节点的边如果还存在环就要继续缩点直到不存在环。最差的结果是最后只有一个点。
缩点的原始节点就是在bazel中必须包含在一个Target的文件。
其实一般顶级开源项目的模块划分都很清晰一般不会出现多个模块之间大规模的互相引用但是出现后这种判断Cmake项目是否可以逐模块拆分为Bazel的方法非常有效。
但是有一个问题执行完这个分析后得出的不存在环的结论文件级别的这个时候最差的情况是需要大规模的逐文件去写bazel中对应Target虽然看起来这个流程是可以自动化的但是确实没有精力去研究这个了。
这里就有两个劣势:
逐文件写Target过于复杂有些本末倒置越复杂的项目Target写的越复杂而且极难修改如果要升级依赖的项目对应项目存在大规模路径变动上面的步骤就要再来一次了
所以综上所属“Bazel集成仅使用Cmake的依赖项目” 的通用方法就是
把所有的文件打包成一个Target复杂依赖项目的集成需要对代码结构有所了解最小化引入
样例代码
这里是一个上面提到的缩点的代码实现原则上可以判断全部cpp项目的依赖关系判断是否可以 “轻松” 的拆分为Bazel的Target。
import os
import re
from collections import defaultdictEXCLUDED_DIRS {tests, test, benchmarks, fuzzer, docs, examples, tool, experimental}def find_cpp_files(directory):Find all .cpp files in the given directory, excluding certain subdirectories.cpp_files []for root, dirs, files in os.walk(directory):dirs[:] [d for d in dirs if d not in EXCLUDED_DIRS]for file in files:if file.endswith(.cpp):cpp_files.append(os.path.join(root, file))return cpp_filesdef extract_includes(cpp_file):Extract included header files from a .cpp file.includes []with open(cpp_file, r) as f:for line in f:match re.match(r^\s*#\s*include\s([^]), line)if match:includes.append(match.group(1))return includesdef build_dependency_map(cpp_files):Build a map of cpp files to their header file dependencies, including .h files.dependency_map {}for cpp_file in cpp_files:includes extract_includes(cpp_file)relative_path os.path.relpath(cpp_file, /data1/exercise/velox/)base_name os.path.splitext(relative_path)[0]dependencies [os.path.splitext(include)[0]for include in includes if os.path.splitext(include)[0] ! base_name]if base_name velox/type/Tokenizer:print(, dependencies)h_file_path os.path.splitext(cpp_file)[0] .hif os.path.exists(h_file_path):h_includes extract_includes(h_file_path)dependencies.extend([os.path.splitext(include)[0] for include in h_includesif os.path.splitext(include)[0] ! base_name])if base_name velox/type/Tokenizer:print(, dependencies)dependency_map[base_name] list(set(dependencies)) return dependency_mapdef find_cycles(dependency_map):Detect cycles in the dependency map and return all cycle paths.visited set()stack set()cycles []def dfs(node, path):if node in stack:cycle_start_index path.index(node)cycles.append(path[cycle_start_index:] [node])return Trueif node in visited:return Falsevisited.add(node)stack.add(node)path.append(node)for neighbor in dependency_map.get(node, []):dfs(neighbor, path)stack.remove(node)path.pop()return Falsefor node in dependency_map:if node not in visited:dfs(node, [])return cycles# 此时只需要关心缩点后的超级点因为其他点已经确定不存在循环依赖
def build_scc_graph(cycles, dependency_map):Build a new graph with strongly connected components (SCCs).scc_map {}scc_to_nodes_map defaultdict(list)for i, cycle in enumerate(cycles):for node in cycle:scc_map[node] fSCC_{i} scc_to_nodes_map[fSCC_{i}].append(node)#print(f Node {node} added to SCC_{i})scc_graph defaultdict(set)for node, scc in scc_map.items():for neighbor in dependency_map.get(node, []):if neighbor in scc_map and scc_map[neighbor] ! scc:scc_graph[scc].add(scc_map[neighbor])print(\nSCC to Node List Mapping:)for scc, nodes in scc_to_nodes_map.items():print(f{scc}: {nodes})return scc_graph, scc_mapdef detect_cycles_in_scc_graph(scc_graph):Detect cycles in the SCC graph and return cycles with their corresponding SCCs.visited set()stack set()cycles []def dfs(node, path):if node in stack:cycle_start_index path.index(node)cycles.append(path[cycle_start_index:] [node]) return Trueif node in visited:return Falsevisited.add(node)stack.add(node)path.append(node)for neighbor in scc_graph.get(node, []):dfs(neighbor, path)stack.remove(node)path.pop()return Falsefor node in scc_graph:if node not in visited:dfs(node, [])return cycles def main(directory):cpp_files find_cpp_files(directory)dependency_map build_dependency_map(cpp_files)cycles find_cycles(dependency_map)if cycles:print(发现循环依赖:)# for cycle in cycles:# print( - .join(cycle))scc_graph, scc_map build_scc_graph(cycles, dependency_map)scc_cycles detect_cycles_in_scc_graph(scc_graph)if scc_cycles:print(缩点后的图中存在循环依赖:)for cycle in scc_cycles:print( - .join(cycle))scc_nodes [node for node in cycle if node in scc_map]print(fSCC {cycle}: 包含节点 {scc_nodes})else:print(缩点后的图中不存在循环依赖。)else:print(系统中不存在循环依赖。)if __name__ __main__:directory_to_check /data1/exercise/xxxxxxxxmain(directory_to_check)参考
GNU GCC使用ld链接器进行链接的完整过程是怎样的c基础-头文件相互引用与循环依赖问题