Skip to contentSkip to conversation

perf build: Update build rule for generated files

PATCH Superseded Namhyung Kim 3 participants 10 messages Jul 26, 2023 – Jul 28, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf build: Update build rule for generated files

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +8 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,$(host)cc_o_c)
+
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +4 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
 	$(call rule_mkdir)
 	$(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+	$(call rule_mkdir)
+	$(call if_changed_dep,$(host)cc_o_c)
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author maintainer
#1 lore raw

PATCH 2/2 perf build: Include generated header files properly

The flex and bison generate header files from the source. When user specified a build directory with O= option, it'd generate files under the directory. The build command has -I option to specify the header include directory.

But the -I option only affects the files included like <...>. Let's change the flex and bison headers to use it instead of "...".

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/perf/pmu-events/jevents.py | 2 +-
 tools/perf/util/bpf-filter.c     | 4 ++--
 tools/perf/util/expr.c           | 4 ++--
 tools/perf/util/parse-events.c   | 4 ++--
 tools/perf/util/pmu.c            | 4 ++--
 5 files changed, 9 insertions(+), 9 deletions(-)
tools/perf/pmu-events/jevents.py +1 -1
index 12e80bb7939b..82c42c46d886 100755
@@ -999,7 +999,7 @@ such as "arm/cortex-a34".''',
   _args = ap.parse_args()
 
   _args.output_file.write("""
-#include "pmu-events/pmu-events.h"
+#include <pmu-events/pmu-events.h>
 #include "util/header.h"
 #include "util/pmu.h"
 #include <string.h>
tools/perf/util/bpf-filter.c +2 -2
index 0b30688d78a7..47f01df658d9 100644
@@ -9,8 +9,8 @@
 #include "util/evsel.h"
 
 #include "util/bpf-filter.h"
-#include "util/bpf-filter-flex.h"
-#include "util/bpf-filter-bison.h"
+#include <util/bpf-filter-flex.h>
+#include <util/bpf-filter-bison.h>
 
 #include "bpf_skel/sample-filter.h"
 #include "bpf_skel/sample_filter.skel.h"
tools/perf/util/expr.c +2 -2
index 4814262e3805..7410a165f68b 100644
@@ -10,8 +10,8 @@
 #include "debug.h"
 #include "evlist.h"
 #include "expr.h"
-#include "expr-bison.h"
-#include "expr-flex.h"
+#include <util/expr-bison.h>
+#include <util/expr-flex.h>
 #include "util/hashmap.h"
 #include "smt.h"
 #include "tsc.h"
tools/perf/util/parse-events.c +2 -2
index acde097e327c..f3773467c103 100644
@@ -18,8 +18,8 @@
 #include "debug.h"
 #include <api/fs/tracing_path.h>
 #include <perf/cpumap.h>
-#include "parse-events-bison.h"
-#include "parse-events-flex.h"
+#include <util/parse-events-bison.h>
+#include <util/parse-events-flex.h>
 #include "pmu.h"
 #include "pmus.h"
 #include "asm/bug.h"
tools/perf/util/pmu.c +2 -2
index 7f984a7f16ca..b6654b9f55d2 100644
@@ -19,8 +19,8 @@
 #include "evsel.h"
 #include "pmu.h"
 #include "pmus.h"
-#include "pmu-bison.h"
-#include "pmu-flex.h"
+#include <util/pmu-bison.h>
+#include <util/pmu-flex.h>
 #include "parse-events.h"
 #include "print-events.h"
 #include "header.h"
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
reviewer
#2 lore raw
In reply to Namhyung Kim
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

Quoted 8 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 7 lines
tools/perf/pmu-events/Build +4 -0
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Thanks,
Ian

Quoted 3 lines

--
2.41.0.487.g6d72f3e995-goog

author maintainer
#3 lore raw

Hi Ian,

Quoted 52 lines · Ian Rogers
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 10 lines
Quoted 10 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Thanks,
Namhyung

reviewer
#4 lore raw
Quoted 89 lines · Namhyung Kim

Hi Ian,

Quoted 52 lines · Ian Rogers
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 10 lines
Quoted 10 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```

Thanks,
Ian

Quoted 2 lines

Thanks,
Namhyung

author maintainer
#5 lore raw
Quoted 95 lines · Ian Rogers
Quoted 89 lines · Namhyung Kim

Hi Ian,

Quoted 52 lines · Ian Rogers
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 10 lines
Quoted 10 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Quoted 7 lines

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```

Probably, but I wonder if it affects the normal .c files expecting them in the OUTPUT directory.

Thanks,
Namhyung

reviewer
#6 lore raw
Quoted 105 lines · Namhyung Kim
Quoted 95 lines · Ian Rogers
Quoted 89 lines · Namhyung Kim

Hi Ian,

Quoted 52 lines · Ian Rogers
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 10 lines
Quoted 10 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Quoted 7 lines

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```
Quoted 2 lines

Probably, but I wonder if it affects the normal .c files expecting them in the OUTPUT directory.

Hmm.. I think the longer matches may be necessary to trigger the "more specific" ordering:
https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html

I'm not keen on these extra rules that mirror existing rules, it is a bit cryptic what is going on. I wonder if it would be cleaner just to fail the build if the bogus pmu-events.c exists. For example:

```
--- a/tools/perf/pmu-events/Build
+++ b/tools/perf/pmu-events/Build
@@ -12,6 +12,14 @@ EMPTY_PMU_EVENTS_C = pmu-events/empty-pmu-events.c
PMU_EVENTS_C   =  $(OUTPUT)pmu-events/pmu-events.c
METRIC_TEST_LOG        =  $(OUTPUT)pmu-events/metric_test.log

+ifneq ($(OUTPUT),)
+ifneq ($(wildcard pmu-events/pmu-events.c),)
+dummy := $(error OUTPUT set but tools/perf/pmu-events/pmu-events.c exists. \
+           This can mean the generated version of pmu-events.c is not
compiled. \
+           Try 'cd tools/perf; make clean; cd -' and then rebuilding.)
+endif
+endif
+
ifeq ($(JEVENTS_ARCH),)
JEVENTS_ARCH=$(SRCARCH)
endif
```

Thanks,
Ian

Quoted 2 lines

Thanks,
Namhyung

author maintainer
#7 lore raw
Quoted 110 lines · Ian Rogers
Quoted 105 lines · Namhyung Kim
Quoted 95 lines · Ian Rogers
Quoted 89 lines · Namhyung Kim

Hi Ian,

Quoted 52 lines · Ian Rogers
Quoted 37 lines · Namhyung Kim

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 10 lines
Quoted 10 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Quoted 7 lines

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```
Quoted 2 lines

Probably, but I wonder if it affects the normal .c files expecting them in the OUTPUT directory.

Quoted 7 lines

Hmm.. I think the longer matches may be necessary to trigger the "more specific" ordering:
https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html

I'm not keen on these extra rules that mirror existing rules, it is a bit cryptic what is going on. I wonder if it would be cleaner just to fail the build if the bogus pmu-events.c exists. For example:

I prefer just making it build instead of failing. But not strongly against your idea. It'd be nice to hear what others think.. Arnaldo?

Quoted 11 lines

```

tools/perf/pmu-events/Build +0 -0
@@ -12,6 +12,14 @@ EMPTY_PMU_EVENTS_C = pmu-events/empty-pmu-events.c

PMU_EVENTS_C = $(OUTPUT)pmu-events/pmu-events.c
METRIC_TEST_LOG = $(OUTPUT)pmu-events/metric_test.log

tools/perf/pmu-events/Build +4 -0
+ifneq ($(OUTPUT),)
+ifneq ($(wildcard pmu-events/pmu-events.c),)
+dummy := $(error OUTPUT set but tools/perf/pmu-events/pmu-events.c exists. \
+           This can mean the generated version of pmu-events.c is not
Quoted 2 lines

compiled. \
+ Try 'cd tools/perf; make clean; cd -' and then rebuilding.)

I've tested this and this message is buried in the stream of parallel build outputs. :(

Thanks,
Namhyung

Quoted 7 lines
tools/perf/pmu-events/Build +3 -0
+endif
+endif
+

ifeq ($(JEVENTS_ARCH),)
JEVENTS_ARCH=$(SRCARCH)
endif
```

#8 lore raw

Em Wed, Jul 26, 2023 at 11:45:44PM -0700, Namhyung Kim escreveu:

Quoted 105 lines · Ian Rogers
Quoted 104 lines · Namhyung Kim
Quoted 96 lines · Ian Rogers
Quoted 87 lines · Namhyung Kim
Quoted 82 lines · Ian Rogers
Quoted 50 lines · Namhyung Kim
Quoted 36 lines

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 9 lines
Quoted 9 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Quoted 7 lines

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```
Quoted 2 lines

Probably, but I wonder if it affects the normal .c files expecting them in the OUTPUT directory.

Hmm.. I think the longer matches may be necessary to trigger the "more specific" ordering:
https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html

Quoted 6 lines
Quoted 3 lines

I'm not keen on these extra rules that mirror existing rules, it is a bit cryptic what is going on. I wonder if it would be cleaner just to fail the build if the bogus pmu-events.c exists. For example:

I prefer just making it build instead of failing. But not strongly against your idea. It'd be nice to hear what others think.. Arnaldo?

I think that we should just ignore any build files in the source tree when using 'O=', i.e.:

make -C tools/perf

genereated files (in the source tree) should not be used when, right after running it, we run:

make -C tools/perf O=/some/build/dir

If we run 'make -C tools/perf' and there is a pmu-events.c, use it if its timestamp is more recent than the files from which it was generated, as usual for a make managed build.

- Arnaldo

Quoted 28 lines
Quoted 11 lines

```

tools/perf/pmu-events/Build +0 -0
@@ -12,6 +12,14 @@ EMPTY_PMU_EVENTS_C = pmu-events/empty-pmu-events.c

PMU_EVENTS_C = $(OUTPUT)pmu-events/pmu-events.c
METRIC_TEST_LOG = $(OUTPUT)pmu-events/metric_test.log

tools/perf/pmu-events/Build +4 -0
+ifneq ($(OUTPUT),)
+ifneq ($(wildcard pmu-events/pmu-events.c),)
+dummy := $(error OUTPUT set but tools/perf/pmu-events/pmu-events.c exists. \
+           This can mean the generated version of pmu-events.c is not
Quoted 2 lines

compiled. \
+ Try 'cd tools/perf; make clean; cd -' and then rebuilding.)

I've tested this and this message is buried in the stream of parallel build outputs. :(

Thanks,
Namhyung

Quoted 7 lines
tools/perf/pmu-events/Build +3 -0
+endif
+endif
+

ifeq ($(JEVENTS_ARCH),)
JEVENTS_ARCH=$(SRCARCH)
endif
```

Signature · 1 line
- Arnaldo
author maintainer
#9 lore raw

Hi Arnaldo,

Quoted 119 lines · Arnaldo Carvalho de Melo

Em Wed, Jul 26, 2023 at 11:45:44PM -0700, Namhyung Kim escreveu:

Quoted 105 lines · Ian Rogers
Quoted 104 lines · Namhyung Kim
Quoted 96 lines · Ian Rogers
Quoted 87 lines · Namhyung Kim
Quoted 82 lines · Ian Rogers
Quoted 50 lines · Namhyung Kim
Quoted 36 lines

The bison and flex generate C files from the source (.y and .l) files. When O= option is used, they are saved in a separate directory but the default build rule assumes the .C files are in the source directory. So it might read invalid file if there are generated files from an old version. The same is true for the pmu-events files.

For example, the following command would cause a build failure:

  $ git checkout v6.3
  $ make -C tools/perf  # build in the same directory
  $ git checkout v6.5-rc2
  $ mkdir build  # create a build directory
  $ make -C tools/perf O=build  # build in a different directory but it
                                # refers files in the source directory

Let's update the build rule to specify those cases explicitly to depend on the files in the output directory.

Note that it's not a complete fix and it needs the next patch for the include path too.

Fixes 80eeb67fe577 ("perf jevents: Program to convert JSON file")
---
 tools/build/Makefile.build  | 8 ++++++++
 tools/perf/pmu-events/Build | 4 ++++
 2 files changed, 12 insertions(+)
tools/build/Makefile.build +0 -0
index 89430338a3d9..f9396696fcbf 100644
@@ -117,6 +117,14 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)

tools/build/Makefile.build $(OUTPUT)%.s: %.c FORCE · line 127

Quoted 10 lines
tools/build/Makefile.build +8 -0
        $(call if_changed_dep,cc_s_c)
 
+$(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+
+$(OUTPUT)%-flex.o: $(OUTPUT)%-flex.c FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)
+

Hi Namhyung,

Quoted 7 lines

as we have:

```
$(OUTPUT)%.o: %.c FORCE
       $(call rule_mkdir)
       $(call if_changed_dep,$(host)cc_o_c)
```

I'm not sure what the 2 additional rules achieve.

The above rule assumes the .c files are in the source directory (without $(OUTPUT) prefix). It caused a trouble when the flex and bison files are generated in the output directory and you have an old version of them in the source directory.

Quoted 9 lines
Quoted 9 lines
tools/build/Makefile.build +0 -0
 # Gather build data:
 #   obj-y        - list of build objects
 #   subdir-y     - list of directories to nest
tools/perf/pmu-events/Build +0 -0
index 150765f2baee..f38a27765604 100644
@@ -35,3 +35,7 @@ $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L
        $(call rule_mkdir)
Quoted 8 lines

tools/perf/pmu-events/Build $(PMU_EVENTS_C): $(JSON) $(JSON_TEST) $(JEVENTS_PY) $(METRIC_PY) $(METRIC_TEST_L · line 41

Quoted 6 lines
tools/perf/pmu-events/Build +4 -0
        $(Q)$(call echo-cmd,gen)$(PYTHON) $(JEVENTS_PY) $(JEVENTS_ARCH) $(JEVENTS_MODEL) pmu-events/arch $@
 endif
+
+$(OUTPUT)pmu-events/pmu-events.o: $(PMU_EVENTS_C)
+       $(call rule_mkdir)
+       $(call if_changed_dep,$(host)cc_o_c)

If we add this, do the Makefile.build changes still need to happen?

Quoted 3 lines

The Makefile.build changes are specific to flex and bison files. So yes, we need this for pmu-events.c to work properly with O= option.

Quoted 7 lines

Got it, you are right I was confusing the flex/bison with the jevents case. Can we get away with a single rule then:

```
 $(OUTPUT)%.o:  $(OUTPUT)%.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,$(host)cc_o_c)
```
Quoted 2 lines

Probably, but I wonder if it affects the normal .c files expecting them in the OUTPUT directory.

Hmm.. I think the longer matches may be necessary to trigger the "more specific" ordering:
https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html

Quoted 6 lines
Quoted 3 lines

I'm not keen on these extra rules that mirror existing rules, it is a bit cryptic what is going on. I wonder if it would be cleaner just to fail the build if the bogus pmu-events.c exists. For example:

I prefer just making it build instead of failing. But not strongly against your idea. It'd be nice to hear what others think.. Arnaldo?

I think that we should just ignore any build files in the source tree when using 'O=', i.e.:

make -C tools/perf

Quoted 8 lines

genereated files (in the source tree) should not be used when, right after running it, we run:

make -C tools/perf O=/some/build/dir

If we run 'make -C tools/perf' and there is a pmu-events.c, use it if its timestamp is more recent than the files from which it was generated, as usual for a make managed build.

Right, this is what my patch does by adding specific rules for generated files. I'll add a comment to describe why it's needed.

Thanks,
Namhyung