Golang Gin Practice, please get started Makefile
Original address: Golang gin practice of the outside please get started Makefile
Objective
Software engineering with a certain degree of complexity is basically the first to compile a, then rely on B, then compile C ..., and finally execute the build
If each person is programmed, or if every new colleague asks you how Project D is built and rebuilt, what you need to pay attention to ... Wait a minute, don't you want to crash?
We often find Makefile in open source projects, do you have any questions?
This section provides a brief introduction to how Makefile is used, and finally recommends further learning
How to Solve
For build orchestration, Docker has Dockerfile, there is artifact make in Unix ....
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 built according to Makefile's building rules .
Of course, it's all in Makefile. You write your own specific Shell commands according to the Make grammar rules.
It's a tool, and the rules are simple. Within the supported range, compile A, rely on B, and compile C, no problem at all.
Rules
Makefile consists of several rules, each starting with a target, followed by a: colon, which is the prerequisites of the target after the colon (precondition)
Immediately following the new line, you must start with a tab and follow the command, which is the build command you want the target to execute
[target] ... : [prerequisites] ...<tab>[command] ... ...
- Target: A target represents a rule, which can be one or more file names. It can also be the name (tag) of an operation, called a pseudo-target (phony)
- Prerequisites: Pre-condition, this item is an optional parameter . Typically multiple file names, pseudo-targets. Its role is whether the target needs to be rebuilt, and if the precondition does not exist or has been updated (the last time the file was modified), it is considered that the target needs to be rebuilt
- Command: Build a specific set of commands for this target
A simple example
This article will be go-gin-example to write Makefile files, please enter the door of make
Analysis
Before you write Makefile, you need to analyze the sequence, dependencies, problems to be solved, etc.
Write
.PHONY: build clean tool lint helpall: buildbuild: 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 file, used .PHONY
, its role is to declare build/clean/tool/lint/help as a pseudo-target , declared as pseudo-target will be what?
- When declared as a pseudo-target: When the corresponding command is executed, make does not check for the existence of a build/clean/tool/lint/help file, but instead runs the corresponding command of the label each time.
- If you do not declare: There is a corresponding file, make will assume that xx file already exists, there is no need to rebuild
2, this block is relatively simple, the command line execution can see the effect, the following functions are realized:
- Make:make is make all
- Make build: Compile packages and dependencies for the current project
- Make tool: Runs the specified Go tool set
- Make Lint:golint, please.
- Make clean: Delete object files and cache files
- Make Help:help
Why do I print a command that executes
If you actually do, you may have questions. Why did you print to standard output when you just executed the command?
Reason
Make will print each command by default and then execute. This behavior is defined as an echo
Solve
You can add @ before the corresponding command to specify that the command is not printed on the standard output
build: @go build -v .
So, are there any other special symbols? Yes, after class to understand the next +,-the use of
Summary
This is a relatively concise article, hoping to let you have a basic understanding of Makefile
Congratulations on your skills. +1
Reference
Sample code for this series