dpauxqt1281 2019-08-19 22:10
浏览 498
已采纳

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

I'm trying to adjust my Makefile for my GoLang project. I have several rules which should:

  1. Setup a git pre-commit hook (Don't want to commit the binary files and break copyright law by accident)
  2. Download an mp3 file via youtube-dl
  3. Extract a subsection of that video via ffmpeg

Previously I was doing this via shellscript and checking for each file manually, but in the act of converting my script to a Makefile I seem to be missing something.

The only rule that doesn't re-run is the pre-hook, but I think that's because I'm not using a variable for my target/rule name?

.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

UPDATE: I've found this works correctly if I lump all the dependencies onto the generate rule like so (which seems like the wrong thing to do)

.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
  • 写回答

2条回答 默认 最新

  • dongxuan1314 2019-08-20 00:51
    关注

    You don't specify but I assume you're running make generate when you see this behavior. It's always best when asking questions if you show the command you typed, the output you got (cut and paste and formatted properly) or at least sufficient of it to see the problem, and point out exactly which part of the output is unexpected AND what you expected to happen instead.

    Assuming your environment and makefile are accurately described above, then the most obvious cause of the behavior you're seeing is that this command:

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

    is not updating the timestamp on the output file $(song_file), so it always looks older than the targets that depend on it, so make always rebuilds it.

    After you run your makefile, use ls -l on all the expected output files and see if their modification times are updated. If not you may need to add touch $@ to your rule after the youtube-dl command, to make sure it was updated:

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

    Also as mentioned in the comments above, and as described in the GNU make documentation, the special targets are .DEFAULT (uppercase) and .PHONY (uppercase), not .default and .phony. Those latter are simply targets you've defined, just like foo or whatever; they have no special meaning to GNU make. makefiles, like all UNIX/POSIX tools and languages, are case-sensitive so be careful to use the correct case for keywords as described in the GNU make manual (and for the targets you define in your makefile)

    Removing the export won't change anything: GNU make allows a variable to be assigned and exported on the same line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效