On Wed, Apr 15, 2020 at 4:55 AM Glen Huang <hey....@gmail.com> wrote:
>
> I have a makefile where upon a new go binary being built, it builds some 
> other stuff that is not go related.
>
> The go binary is unconditionally built with a FORCE prerequisite, so the go 
> build command always runs, but that command always updates the output binary, 
> which leads to downstream being unnecessarily built.
>
> Is there a way to tell go not to touch the output file if it builds purely 
> from the cache?
>
> Currently it prevents me from using a makefile, and I don't really feel like 
> sidestepping go's cache system by manually listing all go files and have the 
> makefile functions as a cache, besides, it's really difficult to correctly 
> list dependencies if the module is big and the binary being built only 
> depends on a subset.

Updating the binary is intentional, so that `go build` has consistent behavior.

The way to handle this in a Makefile is to use the move-if-change
dance, which looks more or less like

real-target: stamp-target; @true
stamp-target: ...
    go build -o temporary-target ...
    if cmp temporary-target real-target; then \
        rm temporary-target; \
    else \
        mv temporary-target real-target
    fi
    touch stamp-target

With this technique stamp-target will be rebuilt if any of its
dependencies change, and real-target will be rebuilt only if the build
generated a file that was different in some way.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUP2vWJRapdzE32S78B36f%2B7ve%3DNNOVnziBXutxTv%3Ds7g%40mail.gmail.com.

Reply via email to