dongyan3616 2018-06-09 13:13
浏览 382
已采纳

如何仅将参数值传递给Makefile目标?

I come from the NodeJS world so I consider the Makefile as the "scripts" part in an npm package.json, which may be wrong (or not ?) to do so.

So my idea is to automate repetitive actions when installing a new dependency by typing:

make install github.com/stretchr/testify

And find a way to get the github.com/stretchr/testify parameter without having to use the heavy parameter name-value declaration FOO=bar (=> make install DEP=github.com/stretchr/testify) generally suggested.

So, following this answer, I tried this:

install %:
    go get $*
    godep save ./...
    git add Godeps vendor
    git commit -m "godep: add $*"

but unsuccessfully: it runs go get without any param and git commit -m "godep: add".

Trials

1 - When I do that:

install %:
    echo $*

I see my "github.com/stretchr/testify".

2 - When I do that:

install %:
    go get ${*}

it loops twice and first run go get without any param, then runs go get github.com/stretchr/testify (as wished).

It looks like ${*} represents an "array" of params parsing characters groups after the target, the first one being the space between install and github.com/stretchr/testify and the second one being github.com/stretchr/testify.

  • 写回答

2条回答 默认 最新

  • dongquan0024 2018-06-10 00:31
    关注

    You cannot use explicit targets and patterns in the same rule, so your rule % install: will not work.

    You can do this with GNU make using the CMDGOALS variable, but it's very hackish and error-prone and I don't recommend it.

    ARG := $(filter-out install,$(MAKECMDGOALS))
    
    install:
            go get $(ARG)
            godep save ./...
            git add Godeps vendor
            git commit -m "godep: add $(ARG)"
    

    As you can see you'll need to add handling for situations where there are no other arguments, or where there are more than one other argument, and of course you can't add any more targets without putting them in the filter-out list, etc.

    Just... not a good way to do it IMO.

    Why don't you do something like this instead:

    install-%:
            go get $*
            godep save ./...
            git add Godeps vendor
            git commit -m "godep: add $*"
    

    Then run:

    make install-github.com/stretchr/testify
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化