如何去除Makefile中的重复项?

2023-12-03

有没有办法简化 Makefile 中的这种重复?

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

lib/background/build/build.js: lib/background/index.js node_modules component.json
  @mkdir -p lib/background/build
  @$(duo) lib/background/index.js > lib/background/build/build.js

lib/page/build/build.js: lib/page/index.js node_modules component.json
  @mkdir -p lib/page/build
  @$(duo) lib/page/index.js > lib/page/build/build.js

lib/popup/build/build.js: lib/popup/index.js node_modules component.json
  @mkdir -p lib/popup/build
  @$(duo) lib/popup/index.js > lib/popup/build/build.js

lib/background/build/build.css: lib/background/index.css node_modules component.json
  @mkdir -p lib/background/build
  @$(duo) lib/background/index.css | $(myth) > lib/background/build/build.css

lib/page/build/build.css: lib/page/index.css node_modules component.json
  @mkdir -p lib/page/build
  @$(duo) lib/page/index.css | $(myth) > lib/page/build/build.css

lib/popup/build/build.css: lib/popup/index.css node_modules component.json
  @mkdir -p lib/popup/build
  @$(duo) lib/popup/index.css | $(myth) > lib/popup/build/build.css

基本上,我想运行一个简单的make build来自顶层的命令,并且仅在必要时重建这些子项目。我不想为每个子项目使用 Makefile,因为这也是重复的。我尝试过的与通配符路径相关的所有方法都没有成功,所以想知道是否有办法做到这一点。例如,我尝试做这样的事情(对于js和css都类似)但没有运气:

js = $(shell find lib test -type f -name '*.js' ! -path "*build.js")

$(js)/build/build.js: node_modules component.json
  # somehow get the directory such as lib/background based on the make command?
  local dir=$(shell dirname $(shell dirname $@))
  @mkdir -p $(dir)/build
  @$(duo) $(dir)/index.js > $(dir)/build/build.js

有什么想法可以让这个变干吗?


一个好的开始是停止重复目标等。在规则体本身中并使用自动变量代替。所以'$@'对于目标文件名,'$(@D)'对于目录路径(例如dirname) 目标文件名等。

这会让你:

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

lib/background/build/build.js: lib/background/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/background/index.js > '$@'

lib/page/build/build.js: lib/page/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/page/index.js > '$@'

lib/popup/build/build.js: lib/popup/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/popup/index.js > '$@'

lib/background/build/build.css: lib/background/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/background/index.css | $(myth) > '$@'

lib/page/build/build.css: lib/page/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/page/index.css | $(myth) > '$@'

lib/popup/build/build.css: lib/popup/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) lib/popup/index.css | $(myth) > '$@'

然后意识到模式规则当您有共享文件名模式和类似规则体的目标和先决条件时,您也可以开始使用它们。他们还给你额外的自动变量.

你会得到这个(中间)阶段:

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.js > '$@'

%/build/build.js: %/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.js > '$@'

%/build/build.js: %/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.css | $(myth) > '$@'

%/build/build.css: %/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.css | $(myth) > '$@'

%/build/build.css: %/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.css | $(myth) > '$@'

您会发现实际上只有两个重复的规则,因此您将它们组合起来。

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.css | $(myth) > '$@'

然后,由于 make 不能像人们希望的那样处理目录目标/先决条件,因此您可以删除lib/background/build, lib/page/build, and lib/popup/build中间目标并仅列出实际文件作为先决条件build.

duo = ./node_modules/.bin/duo

build: lib/background/build/build.js lib/background/build/build.css \
       lib/page/build/build.js lib/page/build/build.css \
       lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
        @mkdir -p '$(@D)'
        @$(duo) '$*'/index.css | $(myth) > '$@'

我可能应该提到,我没有测试这个(因为缺乏模拟目录布局/等的愿望),但转换是直接的,概念也相当简单,所以它应该工作得很好。但一切皆有可能。

清理build您可以使用以下先决条件:

build: $(foreach d,background page popup,$(addprefix lib/$d/build/,build.js build.css))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何去除Makefile中的重复项? 的相关文章

  • ROWNUM 的 OracleType 是什么

    我试图参数化所有现有的 sql 但以下代码给了我一个问题 command CommandText String Format SELECT FROM 0 WHERE ROWNUM lt maxRecords command CommandT
  • 属性对象什么时候创建?

    由于属性实际上只是附加到程序集的元数据 这是否意味着属性对象仅根据请求创建 例如当您调用 GetCustomAttributes 时 或者它们是在创建对象时创建的 或者 前两个的组合 在由于 CLR 的属性扫描而创建对象时创建 从 CLR
  • Signalr 在生产服务器中总是陷入长轮询

    当我在服务器中托管应用程序时 它会检查服务器端事件并始终回退到长轮询 服务器托管环境为Windows Server 2012 R1和IIS 7 5 无论如何 我们是否可以解决这个问题 https cloud githubuserconten
  • Func 方法参数的首选命名约定是什么?

    我承认这个问题是主观的 但我对社区的观点感兴趣 我有一个缓存类 它采用类型的缓存加载器函数Func
  • 如何在没有 Control.Invoke() 的情况下从后台线程修改控件属性

    最近 我们遇到了一些旧版 WinForms 应用程序 我们需要更新一些新功能 在专家测试该应用程序时 发现一些旧功能被破坏 无效的跨线程操作 现在 在您认为我是新手之前 我确实有一些 Windows 窗体应用程序的经验 我不是专家 但我认为
  • 嵌入式系统中的malloc [重复]

    这个问题在这里已经有答案了 我正在使用嵌入式系统 该应用程序在 AT91SAMxxxx 和 cortex m3 lpc17xxx 上运行 我正在研究动态内存分配 因为它会极大地改变应用程序的外观 并给我更多的力量 我认为我唯一真正的路线是为
  • SSH 主机密钥指纹与模式 C# WinSCP 不匹配

    我尝试通过 WinSCP 使用 C 连接到 FTPS 服务器 但收到此错误 SSH 主机密钥指纹 与模式不匹配 经过大量研究 我相信这与密钥的长度有关 当使用 服务器和协议信息 下的界面进行连接时 我从 WinSCP 获得的密钥是xx xx
  • 如何在我的应用程序中使用 Windows Key

    Like Windows Key E Opens a new Explorer Window And Windows Key R Displays the Run command 如何在应用程序的 KeyDown 事件中使用 Windows
  • 为什么 POSIX 允许在只读模式下超出现有文件结尾 (fseek) 进行搜索

    为什么寻找文件结尾很有用 为什么 POSIX 让我们像示例中那样在以只读方式打开的文件中进行查找 c http en cppreference com w c io fseek http en cppreference com w c io
  • 如何在 WPF RichTextBox 中跟踪 TextPointer?

    我正在尝试了解 WPF RichTextBox 中的 TextPointer 类 我希望能够跟踪它们 以便我可以将信息与文本中的区域相关联 我目前正在使用一个非常简单的示例来尝试弄清楚发生了什么 在 PreviewKeyDown 事件中 我
  • 如何针对 Nancy 中的 Active Directory 进行身份验证?

    这是一篇过时的文章 但是http msdn microsoft com en us library ff650308 aspx paght000026 step3 http msdn microsoft com en us library
  • 在 ASP.Net Core 2.0 中导出到 Excel

    我曾经使用下面的代码在 ASP NET MVC 中将数据导出到 Excel Response AppendHeader content disposition attachment filename ExportedHtml xls Res
  • 可空属性与可空局部变量

    我对以下行为感到困惑Nullable types class TestClass public int value 0 TestClass test new TestClass Now Nullable GetUnderlyingType
  • 在 URL 中发送之前对特殊字符进行百分比编码

    我需要传递特殊字符 如 等 Facebook Twitter 和此类社交网站的 URL 为此 我将这些字符替换为 URL 转义码 return valToEncode Replace 21 Replace 23 Replace 24 Rep
  • EPPlus Excel 更改单元格颜色

    我正在尝试将给定单元格的颜色设置为另一个单元格的颜色 该单元格已在模板中着色 但worksheet Cells row col Style Fill BackgroundColor似乎没有get财产 是否可以做到这一点 或者我是否必须在互联
  • 作为字符串的动态属性名称

    使用 DocumentDB 创建新文档时 我想设置属性名称动态地 目前我设置SomeProperty 像这样 await client CreateDocumentAsync dbs db colls x new SomeProperty
  • 如何构建印度尼西亚电话号码正则表达式

    这些是一些印度尼西亚的电话号码 08xxxxxxxxx 至少包含 11 个字符长度 08xxxxxxxxxxx 始终以 08 开头 我发现这个很有用 Regex regex new Regex 08 0 9 0 9 0 9 0 9 0 9
  • 在 ASP.NET 中将事件冒泡为父级

    我已经说过 ASP NET 中的层次结构 page user control 1 user control 2 control 3 我想要做的是 当控件 3 它可以是任何类型的控件 我一般都想这样做 让用户用它做一些触发回发的事情时 它会向
  • 如何在 C# 中播放在线资源中的 .mp3 文件?

    我的问题与此非常相似question https stackoverflow com questions 7556672 mp3 play from stream on c sharp 我有音乐网址 网址如http site com aud
  • 如何连接字符串和常量字符?

    我需要将 hello world 放入c中 我怎样才能做到这一点 string a hello const char b world const char C string a hello const char b world a b co

随机推荐

  • 在高性能环境中的分叉工作进程之间共享状态

    这是我的后续行动上一个问题 正如 Tim Peters 所建议的 使用Manager可能不一定是最好的方法 不幸的是我有太多的脚手架代码来发布SSCCE 相反 我将尝试提供我的问题的详细解释 请随意浏览整个代码库Github 但现在有点混乱
  • 如何使用vba for excel获取另一个工作簿中定义名称的值

    我有几本包含计算的工作簿 我正在将所有这些工作表中的数据结合起来进行 自动概述 为了查找数据 我使用命名范围 这些工作正常 然而 有一个定义的名称不涉及范围 这是一个公式 我想要的是访问该公式的结果 以open计算书 来自我的概述书 明显地
  • RxJava:“java.lang.IllegalStateException:仅允许一名订阅者!”

    我正在使用 RxJava 来计算 Android 中某些传感器数据的归一化自相关性 奇怪的是 我的代码抛出一个异常 java lang IllegalStateException 只允许一个订阅者 我不确定该怎么做 我知道 GroupedO
  • 将图像从 URL 保存到服务器

    我建立了一个迷你内容管理系统CKEditor 用户可以粘贴图像URL来自另一个网站 有没有办法在用户提交内容时获取所有图像 URL 将所有这些图像保存到服务器 并用我的服务器的 URL 替换其他服务器的 URL 例如 用户写了这样的内容 i
  • Kivy - 通过 id 删除小部件

    我有以下代码 from kivy app import App from kivy uix floatlayout import FloatLayout class GUI FloatLayout def remove self self
  • 导入错误:没有名为plotly.plotly的模块

    我正在处理项目并收到此错误 导入错误 没有名为plotly plotly的模块 I tried pip 情节安装 pip install plotly 升级 But import plotly plotly as py没用 不知道pyzo
  • 一次将 100 个文本文件导入 Excel

    我有这个宏可以在 Excel 电子表格中批量导入同一文件夹中包含的 100 多个 txt 文件 Sub QueryImportText Dim sPath As String sName As String Dim i As Long qt
  • FileUpload1.HasFile 为 False

    我尝试在网上搜索这个问题 发现大家都在询问UpdatePanel内的FileUpload控件的问题 首先 我没有使用 UpdatePanel 下面是我的代码 HTML
  • ImportError:运行从 pyinstaller 获取的可执行文件时没有名为 Geometry 的模块

    Traceback most recent call last File
  • odoo 中的 Many2one 字段的域过滤器?

    下面的代码是资产继承类 这里我要补充一下 place 场与 卡恩 邦 肯杰里 and 卡恩 邦 马勒什瓦拉姆 对于 Karn Bang Kengeri 将添加带有 A 和 B 的 asset catg id 然后对于 karn bang m
  • MySQL GROUP 和 COUNT 多个表

    我有一个由 3 部分组成的问题 这给我带来了麻烦 如果我查询 1 个表 我知道如何让表正常工作 一次但我似乎不知道如何将两者结合起来tags and more tags表以获得相同的结果 下面列出了我遇到的 3 个主要问题 问题1 我希望能
  • 如果React.js是MVC中的V,那么其他字母是什么?

    我正在学习 React 但我仍然不确定如何将其发展为成熟的应用程序 对于 M 和 C 哪些框架很适合使用 如果我想发挥功能怎么办 我应该只使用 jQuery 吗 路由 Ajax 以及许多框架为我们提供的其他功能怎么样 你实际上可以简单地去F
  • 为什么我不能在另一个函数中定义一个函数?

    这不是 lambda 函数问题 我知道我可以将 lambda 分配给变量 允许我们在代码中声明但不定义函数有什么意义 例如 include
  • Rails db:迁移关系不存在

    我有这样的模型 学生 class Student lt ActiveRecord Base has many participations dependent destroy has many subject item notes depe
  • setbuf() 会影响 cout 吗?

    我的老师再次无法回答我的问题 我知道谁能够 所以 我从来没有真正学过 C 在 C 中 显然我一直都会使用 cout 语句 在最近的一次作业中 我的老师告诉我们要确保 setbuf stdout NULL 在 main 的顶部以获得无缓冲的输
  • 如何获取属性文件的路径并在运行时将其传递给 bean

    我有一个由 Spring 创建的 bean 实际的类驻留在与 Spring 不同的 JAR 中 该 bean 传递一个路径作为构造函数参数 但是 我很难检索文件的句柄 该文件位于 WEB INF classes 中 我尝试过基于 WEB I
  • Unix“查找”命令用法

    这是一个 bash 安装脚本 脚本 foo sh 将 DIRECTORY 作为参数 说 有一个目录 lt HOME gt TEST TEST 1A TEST 2A TEST 3和另一个目录 lt HOME gt TEST TEST 1B T
  • PostgreSQL 或 MySQL 中的默认选择顺序是什么?

    我在 PostgreSQL 文档中读到 如果没有 ORDER 语句 SELECT 将以未指定的顺序返回记录 最近在一次采访中 有人问我如何按照插入的顺序 SELECT 记录 而无需 PK 或created at 或其他可用于排序的字段 采访
  • Boost Spirit x3 示例计算器(calc8、calc9)链接器错误

    我对提升精神 以及提升 非常陌生 它非常有趣的图书馆 我使用 qtcreator MinGW 5 3 我只是添加每个源文件git hub calc8进入新项目并添加一些 boost 库 但我在尝试构建时遇到以下错误 所有其他示例都工作正常
  • 如何去除Makefile中的重复项?

    有没有办法简化 Makefile 中的这种重复 duo node modules bin duo build lib background build lib page build lib popup build lib backgroun