Any interesting uses of Makefiles to share?

后端 未结 7 1704
北恋
北恋 2020-12-23 18:14

\"make\" is not only useful for building your programming project, but it seems to be under-used in other areas.

For example, many shell scripts can be rewritten as

7条回答
  •  长情又很酷
    2020-12-23 18:44

    Make can also be used as a sync utility. For example:

    #!/usr/bin/make -f
    
    FILES = *.html *.js *.css images/*.jpg subpage/*.html
    LAST_SYNC = .last_sync
    SCP = scp webserver.com:/home/john/public_html
    
    .PHONY: sync
    
    sync : $(LAST_SYNC)
    
    $(LAST_SYNC) : $(FILES)
        $(SCP) $?
        touch $(LAST_SYNC)
    

    This Makefile creates an empty file .last_sync that is used to record the last time that we synced the local repository with the remote one. Each time this script is run, Make will check whether any of the files listed in FILES have been modified since the last time we ran the sync, will copy them to the server and will then update the timestamp on .last_sync. The key is the automatic variable $?, which holds the list of all prerequisites that are newer than the target of the rule.

提交回复
热议问题