Skip to contentSkip to conversation

build: correct cppcheck-misra make rule

PATCH Superseded Jan Beulich 3 participants 5 messages Sep 9, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 build: correct cppcheck-misra make rule

It has been bothering me for a while that I made a bad suggestion during
review: Having cppcheck-misra.json depend on cppcheck-misra.txt does not properly address the multiple targets problem. If cppcheck-misra.json is deleted from the build tree but cppcheck-misra.txt is still there, nothing will re-generate cppcheck-misra.json.

With GNU make 4.3 or newer we could use the &: grouped target separator, but since we support older make as well we need to use some other mechanism. Convert the rule to a pattern one (with "cppcheck" arbitrarily chosen as the stem), thus making known to make that both files are created by a single command invocation. Since, as a result, the JSON file is now "intermediate" from make's perspective, prevent it being deleted again by making it a prereq of .PRECIOUS.

---
I've not been able to spot where / how cppcheck-misra.txt is used. If it's indeed unused, a perhaps better alternative would be to convert the original rule to specify cppcheck-misra.json as (the only) target. One might then even consider using "-o /dev/null" instead of producing an unused *.txt file.

xen/Makefile +3 -5
@@ -746,11 +746,9 @@ cppcheck-version:
 # documentation file. Also generate a json file with the right arguments for
 # cppcheck in json format including the list of rules to ignore.
 #
-cppcheck-misra.txt: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
-	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $@ -j $(@:.txt=.json)
-
-# convert_misra_doc is generating both files.
-cppcheck-misra.json: cppcheck-misra.txt
+.PRECIOUS: %-misra.json
+%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
+	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json
 
 # Put this in generated headers this way it is cleaned by include/Makefile
 $(objtree)/include/generated/compiler-def.h:
#1 lore raw

Hi Jan,

Quoted 3 lines · Jan Beulich

It has been bothering me for a while that I made a bad suggestion during

This is not a sentence for a commit message.

Quoted 13 lines

review: Having cppcheck-misra.json depend on cppcheck-misra.txt does not properly address the multiple targets problem. If cppcheck-misra.json is deleted from the build tree but cppcheck-misra.txt is still there, nothing will re-generate cppcheck-misra.json.

With GNU make 4.3 or newer we could use the &: grouped target separator, but since we support older make as well we need to use some other mechanism. Convert the rule to a pattern one (with "cppcheck" arbitrarily chosen as the stem), thus making known to make that both files are created by a single command invocation. Since, as a result, the JSON file is now "intermediate" from make's perspective, prevent it being deleted again by making it a prereq of .PRECIOUS.

Quoted 7 lines

---
I've not been able to spot where / how cppcheck-misra.txt is used. If it's indeed unused, a perhaps better alternative would be to convert the original rule to specify cppcheck-misra.json as (the only) target. One might then even consider using "-o /dev/null" instead of producing an unused *.txt file.

Txt file is used by cppcheck to give a text description of the rule. If you look inside the json content you will see it mentioned.

Quoted 5 lines
xen/Makefile +0 -0
@@ -746,11 +746,9 @@ cppcheck-version:
 # documentation file. Also generate a json file with the right arguments for

xen/Makefile cppcheck-version: · line 751

Quoted 10 lines
xen/Makefile +3 -5
 # cppcheck in json format including the list of rules to ignore.
 #
-cppcheck-misra.txt: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
-	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $@ -j $(@:.txt=.json)
-
-# convert_misra_doc is generating both files.
-cppcheck-misra.json: cppcheck-misra.txt
+.PRECIOUS: %-misra.json
+%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
+	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json

As far as I know, this is not saying to make that both files are generated by this rule, but that this rule can generate both files so nothing is telling make here that calling it once is enough I think.

Anyway this should work but the commit message needs some rephrasing and I cannot test this right now.

Bertrand

Quoted 3 lines
xen/Makefile +0 -0
 # Put this in generated headers this way it is cleaned by include/Makefile

$(objtree)/include/generated/compiler-def.h:

#2 lore raw
Quoted 11 lines · Bertrand Marquis
Quoted 11 lines · Jan Beulich
xen/Makefile +0 -4
@@ -746,11 +746,9 @@ cppcheck-version:
 # documentation file. Also generate a json file with the right arguments for
 # cppcheck in json format including the list of rules to ignore.
 #
-cppcheck-misra.txt: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
-	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $@ -j $(@:.txt=.json)
-
-# convert_misra_doc is generating both files.
Quoted 8 lines

xen/Makefile cppcheck-version: · line 751

Quoted 4 lines
xen/Makefile +3 -1
-cppcheck-misra.json: cppcheck-misra.txt
+.PRECIOUS: %-misra.json
+%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
+	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json

As far as I know, this is not saying to make that both files are generated by this rule, but that this rule can generate both files so nothing is telling make here that calling it once is enough I think.

A comment could be added, the same one as the one used for syncconfig:
# This exploits the 'multi-target pattern rule' trick.
# convert_misra_doc.py should be executed only once to make all the targets.

Cheers,

Signature · 1 line
Anthony PERARD
#3 lore raw
In reply to Bertrand Marquis
Quoted 5 lines · Bertrand Marquis
Quoted 3 lines · Jan Beulich

It has been bothering me for a while that I made a bad suggestion during

This is not a sentence for a commit message.

How else should I express the motivation for the change?

Quoted 21 lines
Quoted 13 lines

review: Having cppcheck-misra.json depend on cppcheck-misra.txt does not properly address the multiple targets problem. If cppcheck-misra.json is deleted from the build tree but cppcheck-misra.txt is still there, nothing will re-generate cppcheck-misra.json.

With GNU make 4.3 or newer we could use the &: grouped target separator, but since we support older make as well we need to use some other mechanism. Convert the rule to a pattern one (with "cppcheck" arbitrarily chosen as the stem), thus making known to make that both files are created by a single command invocation. Since, as a result, the JSON file is now "intermediate" from make's perspective, prevent it being deleted again by making it a prereq of .PRECIOUS.

Quoted 7 lines

---
I've not been able to spot where / how cppcheck-misra.txt is used. If it's indeed unused, a perhaps better alternative would be to convert the original rule to specify cppcheck-misra.json as (the only) target. One might then even consider using "-o /dev/null" instead of producing an unused *.txt file.

Quoted 2 lines

Txt file is used by cppcheck to give a text description of the rule. If you look inside the json content you will see it mentioned.

Oh, that's properly hidden then.

Quoted 10 lines
Quoted 10 lines
xen/Makefile +0 -4
@@ -746,11 +746,9 @@ cppcheck-version:
 # documentation file. Also generate a json file with the right arguments for
 # cppcheck in json format including the list of rules to ignore.
 #
-cppcheck-misra.txt: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
-	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $@ -j $(@:.txt=.json)
-
-# convert_misra_doc is generating both files.
Quoted 8 lines

xen/Makefile cppcheck-version: · line 751

Quoted 4 lines
xen/Makefile +3 -1
-cppcheck-misra.json: cppcheck-misra.txt
+.PRECIOUS: %-misra.json
+%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
+	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json

As far as I know, this is not saying to make that both files are generated by this rule, but that this rule can generate both files so nothing is telling make here that calling it once is enough I think.

As said in the description - it specifically has this effect. We're using this elsewhere already, see e.g. tools/libs/light/Makefile generating three headers and a C file all in one go. Iirc this is also explicitly described in make documentation (and contrasted to the different behavior for non-pattern rules).

Jan

#4 lore raw

Hi Jan,

Quoted 10 lines · Jan Beulich
Quoted 5 lines · Bertrand Marquis
Quoted 3 lines · Jan Beulich

It has been bothering me for a while that I made a bad suggestion during

This is not a sentence for a commit message.

How else should I express the motivation for the change?

I would say, start with “cppcheck-misra.json depend on …” and remove everything before.

Being bothered is not really something interesting to read in the git log.

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines

review: Having cppcheck-misra.json depend on cppcheck-misra.txt does not properly address the multiple targets problem. If cppcheck-misra.json is deleted from the build tree but cppcheck-misra.txt is still there, nothing will re-generate cppcheck-misra.json.

With GNU make 4.3 or newer we could use the &: grouped target separator, but since we support older make as well we need to use some other mechanism. Convert the rule to a pattern one (with "cppcheck" arbitrarily chosen as the stem), thus making known to make that both files are created by a single command invocation. Since, as a result, the JSON file is now "intermediate" from make's perspective, prevent it being deleted again by making it a prereq of .PRECIOUS.

---
I've not been able to spot where / how cppcheck-misra.txt is used. If it's indeed unused, a perhaps better alternative would be to convert the original rule to specify cppcheck-misra.json as (the only) target. One might then even consider using "-o /dev/null" instead of producing an unused *.txt file.

Quoted 4 lines
Quoted 2 lines

Txt file is used by cppcheck to give a text description of the rule. If you look inside the json content you will see it mentioned.

Oh, that's properly hidden then.

This is how cppcheck needs it and why I added a comment but it might needs improving.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
xen/Makefile +0 -4
@@ -746,11 +746,9 @@ cppcheck-version:
 # documentation file. Also generate a json file with the right arguments for
 # cppcheck in json format including the list of rules to ignore.
 #
-cppcheck-misra.txt: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
-	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $@ -j $(@:.txt=.json)
-
-# convert_misra_doc is generating both files.
Quoted 8 lines

xen/Makefile cppcheck-version: · line 751

Quoted 4 lines
xen/Makefile +3 -1
-cppcheck-misra.json: cppcheck-misra.txt
+.PRECIOUS: %-misra.json
+%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
+	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json

As far as I know, this is not saying to make that both files are generated by this rule, but that this rule can generate both files so nothing is telling make here that calling it once is enough I think.

Quoted 5 lines

As said in the description - it specifically has this effect. We're using this elsewhere already, see e.g. tools/libs/light/Makefile generating three headers and a C file all in one go. Iirc this is also explicitly described in make documentation (and contrasted to the different behavior for non-pattern rules).

Then I think the comment suggested by Anthony makes sense to add.

Cheers
Bertrand

Quoted 2 lines

Jan