2 Star 6 Fork 3

稀风/KOS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
UserBuild.py 3.87 KB
一键复制 编辑 原始数据 按行查看 历史
'''
* 文件名称: Build.py
* 内容摘要: 编译 user 下的工程文件
* 修改记录:
'''
import os
import json
OUTPUT = "../output/user"
ROOT = os.getcwd() # 获取当前所在路径的绝对路径作为工程根目录
BUILD_CFG = "BUILD.json" # 编译配置文件名
project = {} # 通过递归遍历编译配置文件 BUILD_CFG 后所得信息放入 project 字典中
# 该函数通过递归遍历路径 path 下的所有的 BUILD.json 文件,
# 并将遍历后的结果以 '源文件文件名':['源文件所在路径', ['源文件所包含的头文件路径1', '源文件所包含的头文件路径1']]
# 这种形式的数据添加到字典 project 中
def Parse_BUILD_CFG(path):
JsonPathName = os.path.join(path, BUILD_CFG)
# 以只读方式打开文件 JsonPathName
with open(JsonPathName, 'r') as f:
# 读取文件内容到 json_text
json_text = f.read()
# 将 json_text 内容转为 python 字典 json_dict
json_dict = json.loads(json_text)
# 遍历字典 json_dict
for key in json_dict:
if key == 'src': # 如果是源文件,则将 '源文件文件名':['源文件所在路径', ['源文件所包含的头文件路径1', '源文件所包含的头文件路径1']] 这种形式的数据添加到字典 project 中
if json_dict[key]:
for item in json_dict[key]:
project[item] = [path, json_dict['inc']]
elif key == 'dir': # 如果是文件夹,则进入该文件夹内,递归调用 Parse_BUILD_CFG
if json_dict[key]:
for item in json_dict[key]:
new_path = os.path.join(path, item)
Parse_BUILD_CFG(new_path)
elif key == 'inc': # 如果是头文件路径,则不处理
pass
else:
print("Invalid key")
return project
# 编译 user
def compile_user(user_src):
# 遍历 user 源文件
for key in user_src:
if key[-2:] == '.c':
# 切换到 .c 源文件所在的目录
cmd_str = os.path.join(ROOT, user_src[key][0])
print('Enter: ' + cmd_str)
os.chdir(cmd_str)
# 获取包含头文件路径
include = ''
for item in user_src[key][1]:
tmp = ' -I' + os.path.join(ROOT, item)
include += tmp
# 编译 .c 文件
# 例:gcc -m32 -Iinclude -c main.c -o main.o
src = key
obj = key[0:-2] + '.o'
out = os.path.join(ROOT, OUTPUT, obj)
cmd_str = 'gcc -m32 -nostdinc -fno-stack-protector' + include + " -c " + src + " -o " + out
print(cmd_str)
os.system(cmd_str)
elif key[-4:] == '.asm':
# 切换到 .asm 源文件所在的目录
cmd_str = os.path.join(ROOT, user_src[key][0])
print('Enter: ' + cmd_str)
os.chdir(cmd_str)
# 编译 .asm 文件
src = key
obj = key[0:-4] + '.o'
out = os.path.join(ROOT, OUTPUT, obj)
cmd_str = "nasm -f elf32 " + src + " -o " + out
print(cmd_str)
os.system(cmd_str)
else:
print(key + " is unknow.")
# 打印 project
def print_project(project):
for item in project:
str_print = item + ': ' + str(project[item])
print(str_print)
def main():
# 路径为空即表示当前所在目录
root_path = ''
project = Parse_BUILD_CFG(root_path)
# 打印 project
# print_project(project)
# 创建文件夹 output
if not os.path.exists(OUTPUT):
os.makedirs(OUTPUT)
# 编译 user
compile_user(project)
print("User code compiled successfully")
if __name__ == '__main__':
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/thin-wind/KOS.git
git@gitee.com:thin-wind/KOS.git
thin-wind
KOS
KOS
main

搜索帮助