Pre-build step in makefile

前端 未结 5 1038
广开言路
广开言路 2021-01-31 09:04

How can I run a script, which must execute before all other makefile commands? And it will be nice (but not mandatory) to the script is not executed if there is nothing to build

5条回答
  •  故里飘歌
    2021-01-31 09:35

    I propose two solutions. The first mimics what NetBeans IDE generates:

    CC=gcc
    
    .PHONY: all clean
    
    all: post-build
    
    pre-build:
        @echo PRE
    
    post-build: main-build
        @echo POST
    
    main-build: pre-build
        @$(MAKE) --no-print-directory target
    
    target: $(OBJS)
        $(CC) -o $@ $(OBJS)
    
    clean:
        rm -f $(OBJS) target
    

    The second one is inpired by what Eclipse IDE generates:

    CC=gcc
    
    .PHONY: all clean
    .SECONDARY: main-build
    
    all: pre-build main-build
    
    pre-build:
        @echo PRE
    
    post-build:
        @echo POST
    
    main-build: target
    
    target: $(OBJS)
        $(CC) -o $@ $(OBJS)
        @$(MAKE) --no-print-directory post-build
    
    clean:
        rm -f $(OBJS) target
    

    Note that in the first one, pre and post builds are always called regardless of whether the main build is determined to be up to date or not.

    In the second one, the post-build step is not executed if the state of the main build is up to date. While the pre-build step is always executed in both.

提交回复
热议问题