博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
makefiles_Makefiles 101:如何将make用作任务自动化工具
阅读量:2526 次
发布时间:2019-05-11

本文共 6043 字,大约阅读时间需要 20 分钟。

makefiles

by Alex Nadalin

通过亚历克斯·纳达林

Makefiles 101:如何将make用作任务自动化工具 (Makefiles 101: how to use make as a task automation tool)

It seems like developers are afraid of using make as they associate it with the painful experience of compiling things from scratch — the dreaded ./configure && make && make install.

似乎开发人员害怕使用make因为他们将其与从头开始编译东西的痛苦经历联系在一起-令人恐惧的./configure && make && make install

Part of this fear is due to the description of what does:

这种担心的部分原因是对所做的描述:

The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.

make实用程序的目的是自动确定大型程序的哪些部分需要重新编译,并发出命令对其进行重新编译。

Free Software Foundation

自由软件基金会

Not everyone is aware that make can easily be used to manage tasks in your projects. In this article, I’d like to share a brief introduction to how in my day to day activities. This brief guide will focus on using make as an automation tool for tasks rather than a tool for compiling code.

并非所有人都知道,make可以轻松地用于管理项目中的任务。 在本文中,我想简要介绍一下如何在日常活动中 。 本简短指南将重点介绍使用make作为执行任务的自动化工具,而不是用于编译代码的工具。

执行任务 (Executing tasks)

Let’s start by simply creating a Makefile, and defining a task to run:

让我们首先简单地创建一个Makefile ,然后定义要运行的任务:

task:  date

If you run make task you will bump into the following error:

如果运行make task ,则会遇到以下错误:

/tmp ᐅ make taskMakefile:2: *** missing separator.  Stop.

And that’s because Makefiles use tabs to indent code. Let’s update our example by using tabs rather than spaces and… Voilà.

这是因为Makefile使用制表符来缩进代码。 让我们通过使用制表符而不是空格和…Voilà更新我们的示例。

/tmp ᐅ make taskdateFri Jun 15 08:34:15 +04 2018

What kind of sorcery is this? Well, make understood you wanted to run the section task of your makefile, and ran the code (date) within that section in a shell, outputting both the command and its output. If you want to skip outputting the command that’s being executed, you can simply prefix it with an @ sign:

这是什么魔法? 好了, make理解你想跑的部分task你的makefile,并运行的代码( date该部分内)的外壳,既输出的命令及其输出。 如果要跳过输出正在执行的命令,可以简单地在其前面加上@符号:

task:  @date

Running the make command again:

再次运行make命令:

/tmp ᐅ make taskFri Jun 15 08:34:15 +04 2018

The first task in a Makefile is the default one, meaning that we can run make without any arguments:

Makefile的第一个任务是默认任务,这意味着我们可以运行不带任何参数的make

/tmp ᐅ make       Fri Jun 15 08:37:11 +04 2018

运行其他任务 (Running additional tasks)

You can add additional tasks in your Makefile and call them with make $TASK:

您可以在Makefile添加其他任务,并使用make $TASK调用它们:

task:  @datesome:  sleep 1  echo "Slept"thing:  cal

Running the make command again:

再次运行make命令:

/tmp ᐅ make thingcal     June 2018        Su Mo Tu We Th Fr Sa                  1  2   3  4  5  6  7  8  9  10 11 12 13 14 15 16  17 18 19 20 21 22 23  24 25 26 27 28 29 30

按特定顺序运行任务 (Running tasks in a specific order)

A lot of times you will want to execute a task before the current one. Think of it as before or after hooks in your automated tests. This can be done by specifying a list of tasks right after your task’s name:

很多时候,您将需要在当前任务之前执行任务。 可以将其视为自动化测试中的钩子beforeafter 。 这可以通过在任务名称后立即指定任务列表来完成:

task: thing some  @date...

Running the make command again:

再次运行make命令:

/tmp ᐅ make taskcal     June 2018        Su Mo Tu We Th Fr Sa                  1  2   3  4  5  6  7  8  9  10 11 12 13 14 15 16  17 18 19 20 21 22 23  24 25 26 27 28 29 30
sleep 1echo "Slept"SleptFri Jun 15 08:40:23 +04 2018

在make中使用变量 (Using variables with make)

Defining and using variables is fairly straightforward:

定义和使用变量非常简单:

VAR=123
print_var:        echo ${VAR}...

Running the make command again:

再次运行make命令:

/tmp ᐅ make print_var    echo 123123

But watch out, as your shell variables won’t work out of the box:

但是要当心,因为您的shell变量无法立即使用:

print_user:        echo $USER

Running the make command again:

再次运行make命令:

/tmp ᐅ make print_user   echo SERSER

You’ll will need to escape them with either ${VAR} or $$VAR.

您将需要使用${VAR}$$VAR对其进行转义。

Passing flags is also a bit different from what you might be used to. They’re positioned as flags but use the same syntax as environment variables:

传递标志也可能与您习惯的有所不同。 它们被定位为标志,但是使用与环境变量相同的语法:

print_foo:  echo $$FOO

Running the make command again:

再次运行make命令:

/tmp ᐅ make print_fooecho $FOO
/tmp ᐅ make print_foo FOO=barecho $FOObar

制作和外壳 (Make and the shell)

5.3.1 Choosing the Shell------------------------
The program used as the shell is taken from the variable `SHELL'.  If this variable is not set in your makefile, the program `/bin/sh' is used as the shell.

Make will use sh to execute code in a task. This means that some stuff might not work, as you’re probably using some syntax that’s specific to bash. In order to switch, you can simply specify the SHELL variable. In our case, we would want to use SHELL:=/bin/bash.

Make将使用sh执行任务中的代码。 这意味着某些东西可能不起作用,因为您可能使用了bash特有的语法。 为了进行切换,您只需指定SHELL变量即可。 在我们的例子中,我们想使用SHELL:=/bin/bash

As seen before, sometimes you will need to use a quirky, custom syntax to get a regular shell command to work in make. Just like variables need to be escaped with a $$ or ${...}, you will need to use shell when using :

如前所述,有时您需要使用古怪的自定义语法来使常规shell命令在make工作。 就像变量需要使用$$${...}进行转义一样,在使用时,您将需要使用shell

subshell:  echo $(shell echo ${USER})

Running the make command again:

再次运行make命令:

/tmp ᐅ make subshellecho alexalex

Don’t believe me? Try removing the shell instruction. Here’s what you’re going to get:

不相信我吗 尝试删除shell指令。 这是您将获得的:

/tmp ᐅ make subshellecho

结论 (Conclusion)

There’s so much more make can do, and so many more quirky things you might need to find out to decrease the WPS (WTF per second) when working with it. ?

make可以做的事情很多,并且在处理WPS(每秒WTF)时,您可能需要发现更多奇怪的事情。 ?

That doesn’t negate the fact that make is an extremely helpful tool that allows us to automate workflows with ease (without having to setup very complicated pipelines) by writing tab-separated lines with a bunch of shell commands instead.

但这并不能否认make是一个非常有用的工具,它使我们可以通过使用一堆shell命令编写制表符分隔的行来轻松地自动化工作流程(而不必设置非常复杂的管道)。

Originally published at (15th June 2018).You can follow me on - rants are welcome! ?

最初发布于 (2018年6月15日)。 您可以在关注我-欢迎咆哮!

翻译自:

makefiles

转载地址:http://pikzd.baihongyu.com/

你可能感兴趣的文章
机试题:N的阶乘(N很大时)
查看>>
机试题:最小花费
查看>>
循环语句中 i++和++i有什么区别吗
查看>>
机试题 10进制2进制逆序数
查看>>
机试题:谁是你的潜在朋友?
查看>>
密码翻译
查看>>
机试题:最简真分数
查看>>
中位数
查看>>
机试题:小白鼠排队
查看>>
约瑟夫环问题思路解读
查看>>
机试题:大整数的因子
查看>>
关于Java BigInteger 踩坑记录
查看>>
机试:放苹果(递归问题)
查看>>
机试题:进制转换
查看>>
机试题:全排列
查看>>
数据结构:关于树
查看>>
机试题:单词替换
查看>>
机试题:求二叉树公共父节点
查看>>
机试题:吃糖果(递归)
查看>>
机试题:与7无关数的平方和
查看>>