在构建阶段对CMake目标进行参数化,而不是在配置阶段。

huangapple go评论52阅读模式
英文:

Parameterize CMake target at build stage and not at configuration stage

问题

CMake是否有一种方法可以创建一个目标,并在构建时允许用户手动提供参数?

我想要的是像平常一样运行CMake,然后在调用make时能够提供一个参数,例如:

make mytarget MYPARAMETER=123

MYPARAMETER的值将成为gcc调用中的预处理器定义,如 gcc -DMYPARAMETER=123

"mytarget"目标应该以这样一种方式设置,以便如果MYPARAMETER的值发生更改,目标将被重新构建。

我考虑使用环境变量来实现这一点,但实际上任何构建时参数的方案都适用于我的用例。我想避免的是每次更改MYPARAMETER的值时都必须重新配置整个项目,因为它几乎在每次构建时都会更改。

英文:

Is there any facility in CMake to create a target with an explicit parameter to be provided manually by the user at build-time?

What I would like is to run CMake like normal, and then to be able to provide a parameter at the time that I invoke make, as in:

make mytarget MYPARAMETER=123

The value of MYPARAMETER will become a preprocessor definition in the invokation of gcc, as in gcc -DMYPARAMETER=123.

The target "mytarget" should be set up in such a way that if the value of MYPARAMETER changes, then the target will be re-built.

I was thinking of doing this with environment variables but really any scheme for build-time parameters would work for my use case. What I want to avoid is having to reconfigure my whole project each time I change the value of MYPARAMETER, since it changes on almost every build.

答案1

得分: 1

There is no facility in CMake to create a target with an explicit parameter to be provided manually by the user at build-time. 没有CMake中的任何功能来创建一个目标,用户需要在构建时手动提供显式参数。

You have to rerun CMake configuration stage. 您需要重新运行CMake配置阶段。

You can create your own compiler. Write the following shell script /tmp/cc with executable permissions:

您可以创建自己的编译器。编写以下具有可执行权限的shell脚本/tmp/cc:

#!/bin/sh
gcc -DMYPARAMETER="$MYPARAMETER" "$@"

Configure your cmake with cmake -DCMAKE_C_COMPILER=/tmp/cc ... and compile by exporting the variable first into env MYPARAMETER=123 cmake --build .... That way you can "inject" the value of MYPARAMETER into the compiler invocation.

使用cmake -DCMAKE_C_COMPILER=/tmp/cc ...配置您的cmake,并通过首先将变量导出到环境中来编译MYPARAMETER=123 cmake --build ...。这样,您可以将MYPARAMETER的值“注入”到编译器调用中。

英文:

> Is there any facility in CMake to create a target with an explicit parameter to be provided manually by the user at build-time?

No. You have to rerun CMake configuration stage.


You can create your own compiler. Write the following shell script /tmp/cc with executable permissions:

#!/bin/sh
gcc -DMYPARAMETER="$MYPARAMETER" "$@"

Configure your cmake with cmake -DCMAKE_C_COMPILER=/tmp/cc ... and compile by exporting the variable first into env MYPARAMTER=123 cmake --build .... That way you can "inject" the value of MYPARAMETER into the compiler invocation.

huangapple
  • 本文由 发表于 2023年5月22日 02:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定