12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- # -*- coding: utf-8 -*-
- """
- Created on Mon Jun 24 11:03:47 2024
- @author: lijinwen
- @mail: ljwcoke@hotmail.com
- """
- import os
- def scan_files(root_dir):
- """
- 扫描指定目录下的所有 .cc 和 .h 文件
- """
- cc_files = []
- h_files = []
-
- for root, dirs, files in os.walk(root_dir):
- for file in files:
- if file.endswith(".cc"):
- cc_files.append(os.path.join(root, file))
- elif file.endswith(".h"):
- h_files.append(os.path.join(root, file))
-
- return cc_files, h_files
- def generate_makefile(cc_files, output_dir):
- """
- 根据 .cc 文件生成 Makefile,并将所有输出文件放在指定的 output 目录
- """
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
- with open(os.path.join(output_dir, "Makefile"), "w") as f:
- f.write("CXX = g++\n")
- f.write("CXXFLAGS = -Wall -g -shared\n") # 使用 -shared 标志生成共享库
-
- obj_files = [os.path.join("output", os.path.splitext(os.path.relpath(file, output_dir))[0] + ".o") for file in cc_files]
- f.write("OBJS = " + " ".join(obj_files) + "\n\n")
-
- f.write("TARGET = output/ProtocolDecoder.dll\n\n")
-
- f.write("all: $(TARGET)\n\n")
-
- f.write("$(TARGET): $(OBJS)\n")
- f.write("\t$(CXX) $(CXXFLAGS) -o $@ $^\n\n")
-
- for cc_file in cc_files:
- obj_file = os.path.join("output", os.path.splitext(os.path.relpath(cc_file, output_dir))[0] + ".o")
- f.write(f"{obj_file}: {os.path.relpath(cc_file, output_dir)}\n")
- f.write(f"\t$(CXX) $(CXXFLAGS) -c {os.path.relpath(cc_file, output_dir)} -o {obj_file}\n\n")
-
- f.write("clean:\n")
- f.write("\trm -f $(OBJS) $(TARGET)\n")
- if __name__ == "__main__":
- root_dir = "./" # 修改为你的项目路径
- output_dir = "./" # 修改为Makefile生成的路径
- cc_files, h_files = scan_files(root_dir)
- generate_makefile(cc_files, output_dir)
|