Checking file size in makefile, stopping if file is too short

六眼飞鱼酱① 提交于 2019-12-10 09:36:14

问题


Is there a way to check if the size of a particular file is less than some constant? I'm assuming things about size in the makefile and want to make sure I'll get an error if my assumptions are not met. Something like assert, but in makefile.

if filesize(file) > C then error
else continue compilation

回答1:


Put this in your rule, somewhere before the compilation:

 test -n "$$(find filename -a -size +NNNc)"

where filename is the filename and NNN is the size in octets. This returns false and halts make when the size is less than or equal to NNN.




回答2:


My approach for this, is probably not the most pretty solution, but it worked for me :)

CHECKFILE = \
    if [ ! -f "file" ]; then \
        echo "file does not exist" ; exit 1 ; \
    fi; \
    FSIZE=$$(du -b "file" | cut -f 1) ; \
    if [ $$FSIZE -lt 100000 ]; then \
        echo "filesize too small" ; exit 1 ; \
    fi

all:
    @$(CHECKFILE)



回答3:


I prefer this-- continue if greater than 256 bytes, otherwise stop make-- for readability:

test `wc -c <$<` -gt 256;

Within the backticks, the file $< is directed into wc -c, which returns the size in bytes of $<. After the backticks evaluate the size of $<, test is then used with the -gt "greater than" operator.



来源:https://stackoverflow.com/questions/10673189/checking-file-size-in-makefile-stopping-if-file-is-too-short

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!