Skip to contentSkip to conversation

build: try to get kernel version from kernel source

PATCH Superseded Ferdinand Thiessen 4 participants 5 messages Feb 26, 2022 – Jun 8, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 build: try to get kernel version from kernel source

When building the kernel modules, try to get the kernel version from the kernel sources first. This fixes the kernel modules installation directory if the target kernel version differs from the host kernel version, like for CI build or when packaging for linux distributions.

---
 kernel/linux/meson.build | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
kernel/linux/meson.build +12 -6
index d8fb20c1c3..78f28ffb0c 100644
@@ -10,17 +10,23 @@ install = not meson.is_cross_build()
 cross_args = []
 
 if not meson.is_cross_build()
-    # native build
-    kernel_version = run_command('uname', '-r', check: true).stdout().strip()
+    # native target build
+    kernel_version = run_command('uname', '-r').stdout().strip()
+    if kernel_source_dir != ''
+        # Try kernel release from sources first
+        r = run_command('make', '-s', '-C', kernel_source_dir, 'kernelrelease', check: false)
+        if r.returncode() == 0
+            kernel_version = r.stdout().strip()
+        endif
+    else
+        # use default path for native builds
+        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
+    endif
     kernel_install_dir = '/lib/modules/' + kernel_version + '/extra/dpdk'
     if kernel_build_dir == ''
         # use default path for native builds
         kernel_build_dir = '/lib/modules/' + kernel_version + '/build'
     endif
-    if kernel_source_dir == ''
-        # use default path for native builds
-        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
-    endif
 
     # test running make in kernel directory, using "make kernelversion"
     make_returncode = run_command('make', '-sC', kernel_build_dir,
Signature · 1 line
2.35.1
#1 lore raw
Quoted 8 lines · Ferdinand Thiessen

When building the kernel modules, try to get the kernel version from the kernel sources first. This fixes the kernel modules installation directory if the target kernel version differs from the host kernel version, like for CI build or when packaging for linux distributions.

---

On initial review I don't see any problems with the overall approach taken by this patch, but I think others more familiar with kernel build paths could do with reviewing it too. One small comment inline below.

/Bruce

Quoted 8 lines
 kernel/linux/meson.build | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
kernel/linux/meson.build +0 -0
index d8fb20c1c3..78f28ffb0c 100644

kernel/linux/meson.build install = not meson.is_cross_build() · line 14

Quoted 7 lines
kernel/linux/meson.build +2 -2
@@ -10,17 +10,23 @@ install = not meson.is_cross_build()
 cross_args = []
 
 if not meson.is_cross_build()
-    # native build
-    kernel_version = run_command('uname', '-r', check: true).stdout().strip()
+    # native target build
+    kernel_version = run_command('uname', '-r').stdout().strip()

These two lines can be dropped from the patch, I think. The comment change doesn't appear significant, and we definitely want to keep the "check:
true" in the run_command call to avoid meson warnings.

Quoted 25 lines
kernel/linux/meson.build +10 -4
+    if kernel_source_dir != ''
+        # Try kernel release from sources first
+        r = run_command('make', '-s', '-C', kernel_source_dir, 'kernelrelease', check: false)
+        if r.returncode() == 0
+            kernel_version = r.stdout().strip()
+        endif
+    else
+        # use default path for native builds
+        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
+    endif
     kernel_install_dir = '/lib/modules/' + kernel_version + '/extra/dpdk'
     if kernel_build_dir == ''
         # use default path for native builds
         kernel_build_dir = '/lib/modules/' + kernel_version + '/build'
     endif
-    if kernel_source_dir == ''
-        # use default path for native builds
-        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
-    endif
 
     # test running make in kernel directory, using "make kernelversion"
     make_returncode = run_command('make', '-sC', kernel_build_dir,
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 1/1 build: try to get kernel version from kernel source

In reply to Ferdinand Thiessen

When building the kernel modules, try to get the kernel version from the kernel sources first. This fixes the kernel modules installation directory if the target kernel version differs from the host kernel version, like for CI build or when packaging for linux distributions.

---
 kernel/linux/meson.build | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
kernel/linux/meson.build +10 -4
index d8fb20c1c3..16a0948994 100644
@@ -12,15 +12,21 @@ cross_args = []
 if not meson.is_cross_build()
     # native build
     kernel_version = run_command('uname', '-r', check: true).stdout().strip()
+    if kernel_source_dir != ''
+        # Try kernel release from sources first
+        r = run_command('make', '-s', '-C', kernel_source_dir, 'kernelrelease', check: false)
+        if r.returncode() == 0
+            kernel_version = r.stdout().strip()
+        endif
+    else
+        # use default path for native builds
+        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
+    endif
     kernel_install_dir = '/lib/modules/' + kernel_version + '/extra/dpdk'
     if kernel_build_dir == ''
         # use default path for native builds
         kernel_build_dir = '/lib/modules/' + kernel_version + '/build'
     endif
-    if kernel_source_dir == ''
-        # use default path for native builds
-        kernel_source_dir = '/lib/modules/' + kernel_version + '/source'
-    endif
 
     # test running make in kernel directory, using "make kernelversion"
     make_returncode = run_command('make', '-sC', kernel_build_dir,
Signature · 1 line
2.35.1
Tested-by on v2 1/1
#3 lore raw
Quoted 7 lines · Ferdinand Thiessen

When building the kernel modules, try to get the kernel version from the kernel sources first. This fixes the kernel modules installation directory if the target kernel version differs from the host kernel version, like for CI build or when packaging for linux distributions.

lgtm

Acked-by Bruce Richardson Applied
#4 lore raw
Quoted 9 lines · Ferruh Yigit
Quoted 7 lines · Ferdinand Thiessen

When building the kernel modules, try to get the kernel version from the kernel sources first. This fixes the kernel modules installation directory if the target kernel version differs from the host kernel version, like for CI build or when packaging for linux distributions.

Quoted 1 line

Applied, thanks.

Signature · 1 line
David Marchand