dpauxqt1281 2019-08-19 14: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-19 16: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部