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

如何仅将参数值传递给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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题