makefile太难了,学不会,自己用Python写个简单的构建工具

博主因觉得makefile复杂,转而使用Python创建了一个简易构建工具,并经过测试,证明其可用性。文中展示了如何用Python实现类似makefile的功能,并提到了在树莓派上的应用探索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

makefile 太复杂了,学的不精。自己用Python写一个构建工具,经过简单测试,可用



#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
  
import sys  
import argparse  
import os  
import commands  
import re  
  
  
# .c对应的.o文件  
def obj_file_path(srcpath):  
    return srcpath[:-1] + 'o'  
  
  
# 检测.[hc]是否比.o文件新  
def is_newer_than(src, obj):  
    srcmtime = os.stat(src).st_mtime  
    if os.path.exists(obj):  
        objmtime = os.stat(obj).st_mtime  
    else:  
        objmtime = 0  
    return srcmtime > objmtime  
  
  
# 检查是否需要重新编译.c文件  
def need_compile_src(srcpath):  
    output = commands.getoutput('{} -MM {}'.format(cc, srcpath))  
    hlist = output.split(':')[1].strip().split(' ')  
    hfiles = [h for h in hlist if h.endswith('.h')]  
    objpath = obj_file_path(srcpath)

    # 检查依赖的头文件是否更新了
    for h in hfiles:  
        if is_newer_than(h, objpath):  
            return True

    # 检查源文件是否更新了
    if is_newer_than(srcpath, objpath):  
        return True
    
    # 检查配置文件是否更新了
    cfgpath = os.path.abspath('buildconfig')
    execpath = os.path.abspath(execname)
    if is_newer_than(cfgpath, execpath):
        return True

    return False  
      
  
# target build  
def bu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值