Golang Gin Practice Begins Makefile

  golang, linux, makefile, php

Golang Gin Practice Begins Makefile

Original address:Golang Gin Practice Begins Makefile

Preface

Software engineering with a certain complexity basically compiles A, then relies on B, then compiles C … and finally executes the construction.

Wouldn’t it be a collapse if every time people make arrangements, or every new colleague asks you how to build project d, what to pay attention to when rebuilding it, etc?

We often find Makefile in open source projects. Have you ever wondered?

This section will briefly introduce the use of Makefile, and finallyIt is suggested to study deeply.

How to solve

For build choreography, Docker has Dockerfile and in Unix there are artifacts.Make….

Make

What is it?

Make is a build automation tool that looks for Makefile or makefile files in the current directory. If it exists, it will be based on Makefile’sBuild rulesTo complete the construction

Of course, actually within Makefile are specific Shell commands that you write yourself according to the make syntax rules

It is a tool and the rules are simple. Within the scope of support, compiling A, relying on B, and then compiling C is absolutely no problem.

Rules

Makefile consists of several rules, each rule starts with a target, followed by a colon, followed by prerequisites for this target

The next line must begin with a tab followed by a command, which is the build command you want this target to execute.

[target] ... : [prerequisites] ...
<tab>[command]
    ...
    ...
  • Target: A target represents a rule and can be one or more file names. It can also be the name (label) of an operation, calledPhony
  • Prerequisites: Prerequisites, this item isOptional parameters. It is usually multiple file names and dummy targets. Its function is the criterion of whether target needs to be rebuilt. If the precondition does not exist or has been updated (the last modification time of the file), target needs to be rebuilt.
  • Command: Build a specific command set for this target

A simple example

This article willgo-gin-exampleTo write a Makefile, please enter the door of make.

Analysis

Before writing Makefile, you need to analyze the sequence of construction, dependencies, problems to be solved, etc.

write

.PHONY: build clean tool lint help

all: build

build:
    go build -v .

tool:
    go tool vet . |& grep -v vendor; true
    gofmt -w .

lint:
    golint ./...

clean:
    rm -rf go-gin-example
    go clean -i .

help:
    @echo "make: compile packages and dependencies"
    @echo "make tool: run specified go tool"
    @echo "make lint: golint ./..."
    @echo "make clean: remove object files and cached files"

1. In the above documents, the.PHONYWhich is used to declare build/clean/tool/lint/help asFalse targetWhat would happen if it were declared a false target?

  • After being declared as a pseudo target: when executing the corresponding command, make will not check whether there is a file corresponding to build/clean/tool/lint/help, but will run the command corresponding to the label every time.
  • If you do not declare that the corresponding file happens to exist, make will think that xx file already exists and there is no need to rebuild it.

2. This piece is relatively simple. You can see the effect when you execute it on the command line and realize the following functions:

  1. Make: make is make all.
  2. Make build: compiles packages and dependencies for the current project
  3. Make tool: runs the specified Go toolset
  4. Make lint: golint
  5. Make clean: delete object files and cache files
  6. make help: help

Why print the executed command

If you have actually done it, you may have doubts. Why is it printed on the standard output when it is only executing commands?

原因

Make prints every command by default before executing it. This behavior is defined asEcho

解决

You can add @, before the corresponding command, to specify that the command will not be printed on the standard output.

build:
    @go build -v .

So are there any other special symbols? Yes, please find out the use of+,-after class.

Summary

This is a relatively concise article, hoping to give you a basic understanding of Makefile.

Congratulations on your necessary skills +1

References

This series of sample codes

This series of catalogues