From e1a1d8f94ff63b4aa690887dbfe0f208911bc7b4 Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Tue, 4 Feb 2014 17:34:05 -0500 Subject: [PATCH] Port elements of the build system from ndiswrapper This eliminates compilation to find the kernel version. The new Makefile provides more sanity checks and more verbose reporting. --- Makefile.inc | 75 +++++++++++++++++++++++++------------------------ kernelversion.c | 21 -------------- 2 files changed, 39 insertions(+), 57 deletions(-) delete mode 100644 kernelversion.c diff --git a/Makefile.inc b/Makefile.inc index 0749410..9741cce 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -49,47 +49,50 @@ endif # DESTDIR is used as path prefix during installation. DESTDIR ?= -# KERNELPATH is the path to the Linux kernel build tree. Unless a -# separate build directory was used for the kernel build, it's the same -# as the kernel source tree. KERNELPATH is used to access the kernel -# configuration, include files and the build system. To build for -# another kernel, set KERNELPATH manually, for example with: -# make KERNELPATH=/path/to/kernel/source - -# The default KERNELPATH points to the directory where the currently -# running kernel was compiled. Note that the configuration and the -# version of the kernel tree might have changed since then. -ifeq ($(wildcard $(KERNELPATH)),) -KERNELPATH = /lib/modules/$(shell uname -r)/build -# sanity check: does KERNELPATH exist? -ifeq ($(shell cd $(KERNELPATH) && pwd),) -$(error $(KERNELPATH) is missing, please set KERNELPATH) -endif -export KERNELPATH -endif - -# KERNELRELEASE is the target kernel's version. It's always taken from -# the kernel build tree. Kernel Makefile doesn't always know the exact -# kernel version (especially for vendor stock kernels), so we get it -# from instead. But simply grepping it from version.h -# doesn't work, since some distributions have multiple UTS_RELEASE -# in that file. -# This trick has been inspired by the lm_sensors project. -ifndef KERNELRELEASE -KERNELRELEASE := $(shell $(CC) -I $(KERNELPATH)/include -E $(TOP)/kernelversion.c | grep uts_release | cut -f2 -d'"') -ifeq (,$(KERNELRELEASE)) -$(error Cannot detect kernel version - please check compiler and KERNELPATH) +# By default, we try to compile the modules for the currently running +# kernel. But it's the first approximation, as we will re-read the +# version from the kernel sources. +KVERS_UNAME ?= $(shell uname -r) + +# KERNELPATH is the path to the Linux kernel build tree. It is usually the +# same as the kernel source tree, except when the kernel was compiled in +# a separate directory. +KERNELPATH ?= $(shell readlink -f /lib/modules/$(KVERS_UNAME)/build) + +ifeq (,$(KERNELPATH)) +$(error Kernel build tree not found - please set KERNELPATH to configured kernel) endif + +KERNELCONF := $(KERNELPATH)/.config +ifeq (,$(wildcard $(KERNELCONF))) +$(error No .config found in $(KERNELPATH), please set KERNELPATH to configured kernel) endif -# KERNELCONF is the name of the file that holds the configuration -# of the target kernel. -KERNELCONF ?= $(KERNELPATH)/.config +ifneq (,$(wildcard $(KERNELPATH)/include/linux/version.h)) +ifneq (,$(wildcard $(KERNELPATH)/include/generated/uapi/linux/version.h)) +$(error Multiple copies of version.h found, please clean your build tree) +endif +endif -# sanity check: does KERNELCONF exist? -ifeq ($(wildcard $(KERNELCONF)),) -$(error KERNELCONF: $(KERNELCONF) does not exist.) +# Kernel Makefile doesn't always know the exact kernel version, so we +# get it from the kernel headers instead and pass it to make. +VERSION_H := $(KERNELPATH)/include/generated/utsrelease.h +ifeq (,$(wildcard $(VERSION_H))) +VERSION_H := $(KERNELPATH)/include/linux/utsrelease.h +endif +ifeq (,$(wildcard $(VERSION_H))) +VERSION_H := $(KERNELPATH)/include/linux/version.h endif +ifeq (,$(wildcard $(VERSION_H))) +$(error Please run 'make modules_prepare' in $(KERNELPATH)) +endif + +KERNELRELEASE := $(shell sed -ne 's/"//g;s/^\#define UTS_RELEASE //p' $(VERSION_H)) + +ifeq (,$(KERNELRELEASE)) +$(error Cannot find UTS_RELEASE in $(VERSION_H), please report) +endif + include $(KERNELCONF) # KMODPATH nominates the directory where the modules will be diff --git a/kernelversion.c b/kernelversion.c deleted file mode 100644 index 9a6845d..0000000 --- a/kernelversion.c +++ /dev/null @@ -1,21 +0,0 @@ -/* This file is used for a trick to determine the version of the kernel - * build tree. Simply grepping doesn't work, since - * some distributions have multiple UTS_RELEASE definitions in that - * file. - * Taken from the lm_sensors project. - * - * $Id$ - */ -#include - -#ifndef UTS_RELEASE -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) -/* Linux 2.6.33+ uses */ -#include -#else -/* Linux 2.6.18 - 2.6.32 uses */ -#include -#endif -#endif - -char *uts_release = UTS_RELEASE; -- 2.35.1