jjzjj

go - Makefile 规则始终执行,尽管目标存在

coder 2024-07-06 原文

我正在尝试为 my GoLang project 调整我的 Makefile .我有几条规则应该:

  1. 设置一个 git pre-commit 钩子(Hook)(不想提交二进制文件并意外违反版权法)
  2. 通过youtube-dl下载一个mp3文件
  3. 通过 ffmpeg 提取该视频的一个片段

以前我是通过 shellscript 执行此操作并手动检查每个文件,但在将我的脚本转换为 Makefile 的过程中我似乎遗漏了一些东西。

唯一没有重新运行的规则是预 Hook ,但我认为那是因为我没有为我的目标/规则名称使用变量?

.default: install
.phony: install generate clean

export bin_directory = bin
export asset_directory = data/assets
export song_url = https://www.youtube.com/watch?v=z8cgNLGnnK4
export song_file = ${bin_directory}/nsp-you-spin-me-cover.mp3
export loop_file = ${asset_directory}/spin-loop.mp3

install:
    go install .

generate: $(loop_file)
    go generate ./data

$(loop_file): $(song_file)
    mkdir -p "${asset_directory}"
    ffmpeg -i "${song_file}" -ss 00:01:13.30 -to 00:01:30.38 -c copy "${loop_file}"

$(song_file): .git/hooks/pre-commit
    mkdir -p "${bin_directory}"
    youtube-dl "${song_url}" --extract-audio --audio-format mp3 --exec "mv {} ${song_file}"

.git/hooks/pre-commit:
    cp ./pre-commit .git/hooks/pre-commit
    chmod +x .git/hooks/pre-commit

clean:
    git clean -xdf

更新:我发现如果我像这样将所有依赖项都集中到 generate 规则上(这似乎是错误的做法),这会正常工作

.default: install
.phony: install generate clean

bin_directory:=bin
asset_directory:=data/assets
song_url:=https://www.youtube.com/watch?v=z8cgNLGnnK4
song_file:=$(bin_directory)/nsp-you-spin-me-cover.mp3
loop_file:=$(asset_directory)/spin-loop.mp3

install:
    go install .

.git/hooks/pre-commit:
    cp ./pre-commit .git/hooks/pre-commit
    chmod +x .git/hooks/pre-commit

$(song_file):
    mkdir -p "${bin_directory}"
    youtube-dl "${song_url}" --extract-audio --audio-format mp3 --exec "mv {} ${song_file}"

$(loop_file):
    mkdir -p "${asset_directory}"
    ffmpeg -i "${song_file}" -ss 00:01:13.30 -to 00:01:30.38 -c copy "${loop_file}"

generate: .git/hooks/pre-commit $(song_file) $(loop_file)
    go generate ./data

clean:
    git clean -xdf

最佳答案

您没有指定,但我假设您在看到此行为时正在运行 make generate。在提问时,最好显示你输入的命令、你得到的输出(剪切和粘贴并正确格式化)或至少足以看出问题,并准确指出输出的哪一部分是意外的以及什么你期望发生。

假设上面准确描述了您的环境和 makefile,那么您所看到的行为的最明显原因是此命令:

youtube-dl "${song_url}" --extract-audio --audio-format mp3 --exec "mv {} ${song_file}"

没有更新输出文件 $(song_file) 上的时间戳,所以它看起来总是比依赖它的目标更旧,所以 make 总是重建它。

运行 makefile 后,对所有预期的输出文件使用 ls -l 并查看它们的修改时间是否已更新。如果不是,您可能需要在 youtube-dl 命令之后将 touch $@ 添加到您的规则中,以确保它已更新:

$(song_file): .git/hooks/pre-commit
        mkdir -p "${@D}"
        youtube-dl "${song_url}" --extract-audio --audio-format mp3 --exec "mv {} $@"
        touch $@

同样如上面评论中所述,以及 GNU make 文档中所述,特殊目标是 .DEFAULT(大写)和 .PHONY(大写),不是 .default.phony。后者只是您定义的目标,就像 foo 或其他;它们对 GNU make 没有特殊意义。与所有 UNIX/POSIX 工具和语言一样,makefile 区分大小写,因此请小心使用 GNU make 手册中描述的关键字的正确大小写(以及您在 makefile 中定义的目标)

删除 export 不会改变任何东西:GNU make 允许在同一行上分配和导出变量。

关于go - Makefile 规则始终执行,尽管目标存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57564494/

有关go - Makefile 规则始终执行,尽管目标存在的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  3. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  4. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  5. ruby-on-rails - rspec - 如何检查方法是否存在? - 2

    我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu

  6. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  7. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  8. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  9. ruby-on-rails - Rake 任务仅调用一次时执行两次 - 2

    我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里

  10. ruby-on-rails - ActiveRecord 的 find_or_create* 方法是否存在根本性缺陷? - 2

    有几种方法:first_or_create_by、find_or_create_by等,它们的工作原理是:与数据库对话以尝试找到我们想要的东西如果我们找不到,就自己做保存到数据库显然,并发调用这些方法可能会使两个线程都找不到它们想要的东西,并且在第3步中一个线程会意外失败。似乎更好的解决方案是,创建或查找即:提前在您的数据库中创建合理的唯一性约束。如果你想保存一些东西,就保存它如果有效,那就太好了。如果它因为RecordNotUnique异常而无法工作,它已经存在,太好了,加载它那么在什么情况下我想使用Rails内置的东西而不是我自己的(看起来更可靠)create_or_find?

随机推荐