Tôi sử dụng CMakeLists.txt thông thường và tập lệnh python để cập nhật. Tôi chạy tập lệnh python theo cách thủ công sau khi thêm tệp.
import os
import re
def relFiles(base, sub):
fullSub = os.path.join(base,sub)
abs = [os.path.join(dp, f) for dp, dn, fn in os.walk(fullSub) for f in fn]
return [os.path.relpath(f, base) for f in abs]
def updateAddLibrary(cmakelistsDir, subs):
cmakelists = os.path.join(cmakelistsDir, "CMakeLists.txt")
listings = [relFiles(cmakelistsDir, sub) for sub in subs]
files = [f for listing in listings for f in listing] #flatten
with open(cmakelists, 'r') as file:
text = file.read()
sources = "".join([" %s\n" % f.replace('\\', '/') for f in files])
text = re.sub(r"add_library\s*\(\s*([^\s\)]+).*?\)",
r"add_library(\1\n%s)" % sources,
text, 1, re.DOTALL)
with open(cmakelists, "w") as file:
file.write(text)
dir = os.path.dirname(os.path.abspath(__file__))
updateAddLibrary(dir, ['inc','src'])
Ví dụ trước:
...
add_library(MyLib
inc/a.h
)
...
sau:
...
add_library(MyLib
inc/a.h
inc/sub/b.h
src/a.cpp
)
...
Bạn có thể sử dụng GLOB từ cmake trong một kịch bản và có mà thêm đổ ra tên tập tin. – Damian