user10996
user10996

Reputation:

Using the value of an option in a Makefile

Is there a way to get and use the value of an option (not env variable) passed to a Makefile? E.g. if the -j option (for parallelism) is passed on the command line, how do I access it inside the Makefile?

Upvotes: 0

Views: 184

Answers (3)

MadScientist
MadScientist

Reputation: 100926

As Michael Livshin says, you can't know... unless you upgrade to GNU make 4.2 or higher; from the NEWS file:

* The amount of parallelism can be determined by querying MAKEFLAGS, even when
  the job server is enabled (previously MAKEFLAGS would always contain only
  "-j", with no number, when job server was enabled).

Which basically means that the -j2 will appear in MAKEFLAGS always, not just -j as before.

Be aware that changing this value won't have any effect, though, if that's what you're interested in... the amount of parallelism is set when the first make command is invoked and all other make commands will use that same value. All you can do is disable parallelism completely, you can't change the amount of it.

Upvotes: 1

Michael Livshin
Michael Livshin

Reputation: 401

In GNU Make, most options are detectable by inspecting the value of MAKEFLAGS (using findstring). That value is a string of "canonical" one-letter variants of options, usually. For example if you run Make as make --keep-going --question, MAKEFLAGS will be "kq".

-j is different, though. If you run make -j, MAKEFLAGS will contain -j. If you run make -j2, MAKEFLAGS will contain -j --jobserver-ids,3,4.

So you have enough information to distinguish the cases "not parallel at all", "parallel and unlimited" and "parallel and limited". No way to know what the <num> in the last case is, I'm afraid.

Upvotes: 1

uzsolt
uzsolt

Reputation: 6037

If you want the parameter of -j you can use the .MAKE.JOBS. Generally the MAKEFLAGS variable is your desired variable.

Maybe there aren't available in every make (they are available in FreeBSD's make).

Upvotes: 1

Related Questions