Re: Suggestion : warning when using == instead of = in a Makefile variable assignment

Martin Dorey <[email protected]> Tue, 29 Jul 2025 00:10:33 +0000
Newsgroups gmane.comp.gnu.make.bugs
Message-ID <SJ0PR08MB67043ED876A00C0C8C87BF83FA5AA@SJ0PR08MB6704.namprd08.prod.outlook.com>
  *
Make simply reported an undefined variable

For me, it was treated as an assignment:

martind@stormy:~/tmp/jerome-2025-07-28$ cat Makefile
CXX == g++
martind@stormy:~/tmp/jerome-2025-07-28$ make -p 2>&1 | grep ^CXX
CXX = = g++
martind@stormy:~/tmp/jerome-2025-07-28$

... so the variable being undefined seems like an additional puzzle.


  *
`==` is encountered in a context that ... isn’t a conditional test?

Make doesn't use == for conditional tests.

https://www.gnu.org/software/make/manual/html_node/Setting.html

... suggests it does have, yie ai ai, four different assignment operators, though there's a fifth at:

https://www.gnu.org/software/make/manual/html_node/Conditional-Assignment.html

You could try adding a warning if unexpected punctuation is adjacent to the equals sign, but I bet it'd go off on some real makefiles that don't put spaces around the assignment operators.  If you were to only fire off the warning when there's a space before the assignment, then maybe the false positives would be rare enough to be acceptable.

________________________________
From: [email protected] <[email protected]> on behalf of Jerome Saint-Martin <[email protected]>
Sent: Monday, July 28, 2025 01:59
To: [email protected] <[email protected]>
Subject: Suggestion : warining when using == instead of = in a Makefile variable assignation

***** EXTERNAL EMAIL *****
Hello GNU Make team,

    Recently, I spent quite some time debugging a Makefile issue, only to realize that I had mistakenly written `==` instead of `=` when assigning a variable (e.g., `CXX == g++` instead of `CXX = g++`). Make simply reported an undefined variable later on, which made the root cause harder to spot.

    This kind of mistake is easy to make, especially for C++ developers who are used to writing `==` in conditionals. In C++, compilers like GCC emit warnings such as “value computed is not used” when an expression like `x == y;` is written outside a conditional context.

    Would it be feasible for GNU Make to emit a warning when `==` is encountered in a context that looks like a variable assignment, but isn’t a conditional test? I believe this could help many users catch such typos early.

    Thanks for considering this suggestion, and thank you for your work on Make!

Best regards,
Jerome