2018-09-05 20:45:41 +00:00
# SPDX-License-Identifier: GPL-2.0
# Copyright(c) 2013 - 2018 Intel Corporation.
# common Makefile rules useful for out-of-tree Linux driver builds
2016-04-05 01:27:00 +00:00
#
2018-09-05 20:45:41 +00:00
# Usage: include common.mk
2016-04-05 01:27:00 +00:00
#
2018-09-05 20:45:41 +00:00
# After including, you probably want to add a minimum_kver_check call
2016-04-05 01:27:00 +00:00
#
2018-09-05 20:45:41 +00:00
# Required Variables:
# DRIVER
# -- Set to the lowercase driver name
2016-04-05 01:27:00 +00:00
#####################
# Helpful functions #
#####################
readlink = $( shell readlink -f ${ 1 } )
# helper functions for converting kernel version to version codes
get_kver = $( or $( word ${ 2 } ,$( subst ., ,${ 1 } ) ) ,0)
get_kvercode = $( shell [ " ${ 1 } " -ge 0 -a " ${ 1 } " -le 255 2>/dev/null ] && \
[ " ${ 2 } " -ge 0 -a " ${ 2 } " -le 255 2>/dev/null ] && \
[ " ${ 3 } " -ge 0 -a " ${ 3 } " -le 255 2>/dev/null ] && \
printf %d $$ ( ( ( ${ 1 } << 16 ) + ( ${ 2 } << 8 ) + ( ${ 3 } ) ) ) )
################
# depmod Macro #
################
cmd_depmod = /sbin/depmod $( if ${ SYSTEM_MAP_FILE } ,-e -F ${ SYSTEM_MAP_FILE } ) \
$( if $( strip ${ INSTALL_MOD_PATH } ) ,-b ${ INSTALL_MOD_PATH } ) \
-a ${ KVER }
################
2018-09-05 20:45:41 +00:00
# dracut Macro #
2016-04-05 01:27:00 +00:00
################
cmd_initrd := $( shell \
if which dracut > /dev/null 2>& 1 ; then \
echo "dracut --force" ; \
elif which update-initramfs > /dev/null 2>& 1 ; then \
echo "update-initramfs -u" ; \
fi )
#####################
# Environment tests #
#####################
DRIVER_UPPERCASE := $( shell echo ${ DRIVER } | tr "[:lower:]" "[:upper:]" )
i f e q ( , $ { B U I L D _ K E R N E L } )
BUILD_KERNEL = $( shell uname -r)
e n d i f
# Kernel Search Path
# All the places we look for kernel source
2018-09-05 20:45:41 +00:00
KSP := /lib/modules/${ BUILD_KERNEL } /source \
/lib/modules/${ BUILD_KERNEL } /build \
2016-04-05 01:27:00 +00:00
/usr/src/linux-${ BUILD_KERNEL } \
/usr/src/linux-$( ${ BUILD_KERNEL } | sed 's/-.*//' ) \
/usr/src/kernel-headers-${ BUILD_KERNEL } \
/usr/src/kernel-source-${ BUILD_KERNEL } \
/usr/src/linux-$( ${ BUILD_KERNEL } | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/' ) \
/usr/src/linux \
/usr/src/kernels/${ BUILD_KERNEL } \
/usr/src/kernels
# prune the list down to only values that exist and have an include/linux
# sub-directory. We can't use include/config because some older kernels don't
# have this.
test_dir = $( shell [ -e ${ dir } /include/linux ] && echo ${ dir } )
KSP := $( foreach dir, ${ KSP } , ${ test_dir } )
# we will use this first valid entry in the search path
i f e q ( , $ { K S R C } )
KSRC := $( firstword ${ KSP } )
e n d i f
i f e q ( , $ { K S R C } )
$( warning *** Kernel header files not in any of the expected locations.)
$( warning *** Install the appropriate kernel development package, e.g.)
$( error kernel-devel, for building kernel modules and try again)
e l s e
i f e q ( / l i b / m o d u l e s / $ { B U I L D _ K E R N E L } / s o u r c e , $ { K S R C } )
KOBJ := /lib/modules/${ BUILD_KERNEL } /build
e l s e
KOBJ := ${ KSRC }
e n d i f
e n d i f
# Version file Search Path
VSP := ${ KOBJ } /include/generated/utsrelease.h \
${ KOBJ } /include/linux/utsrelease.h \
${ KOBJ } /include/linux/version.h \
${ KOBJ } /include/generated/uapi/linux/version.h \
/boot/vmlinuz.version.h
# Config file Search Path
CSP := ${ KOBJ } /include/generated/autoconf.h \
${ KOBJ } /include/linux/autoconf.h \
/boot/vmlinuz.autoconf.h
# System.map Search Path (for depmod)
MSP := ${ KSRC } /System.map \
/boot/System.map-${ BUILD_KERNEL }
# prune the lists down to only files that exist
test_file = $( shell [ -f ${ file } ] && echo ${ file } )
VSP := $( foreach file, ${ VSP } , ${ test_file } )
CSP := $( foreach file, ${ CSP } , ${ test_file } )
MSP := $( foreach file, ${ MSP } , ${ test_file } )
# and use the first valid entry in the Search Paths
i f e q ( , $ { V E R S I O N _ F I L E } )
VERSION_FILE := $( firstword ${ VSP } )
e n d i f
i f e q ( , $ { C O N F I G _ F I L E } )
CONFIG_FILE := $( firstword ${ CSP } )
e n d i f
i f e q ( , $ { S Y S T E M _ M A P _ F I L E } )
SYSTEM_MAP_FILE := $( firstword ${ MSP } )
e n d i f
i f e q ( , $( wildcard $ {VERSION_FILE }) )
$( error Linux kernel source not configured - missing version header file)
e n d i f
i f e q ( , $( wildcard $ {CONFIG_FILE }) )
$( error Linux kernel source not configured - missing autoconf.h)
e n d i f
i f e q ( , $( wildcard $ {SYSTEM_MAP_FILE }) )
$( warning Missing System.map file - depmod will not check for missing symbols)
e n d i f
#######################
# Linux Version Setup #
#######################
# The following command line parameter is intended for development of KCOMPAT
# against upstream kernels such as net-next which have broken or non-updated
# version codes in their Makefile. They are intended for debugging and
# development purpose only so that we can easily test new KCOMPAT early. If you
# don't know what this means, you do not need to set this flag. There is no
# arcane magic here.
# Convert LINUX_VERSION into LINUX_VERSION_CODE
i f n e q ( $ { L I N U X _ V E R S I O N } , )
LINUX_VERSION_CODE = $( call get_kvercode,$( call get_kver,${ LINUX_VERSION } ,1) ,$( call get_kver,${ LINUX_VERSION } ,2) ,$( call get_kver,${ LINUX_VERSION } ,3) )
e n d i f
# Honor LINUX_VERSION_CODE
i f n e q ( $ { L I N U X _ V E R S I O N _ C O D E } , )
$( warning Forcing target kernel to build with LINUX_VERSION_CODE of ${ LINUX_VERSION_CODE } $( if ${ LINUX_VERSION } , from LINUX_VERSION = ${ LINUX_VERSION } ) . Do this at your own risk.)
KVER_CODE := ${ LINUX_VERSION_CODE }
EXTRA_CFLAGS += -DLINUX_VERSION_CODE= ${ LINUX_VERSION_CODE }
e n d i f
2018-09-05 20:45:41 +00:00
# Determine SLE_LOCALVERSION_CODE for SuSE SLE >= 11 (needed by kcompat)
# This assumes SuSE will continue setting CONFIG_LOCALVERSION to the string
# appended to the stable kernel version on which their kernel is based with
# additional versioning information (up to 3 numbers), a possible abbreviated
# git SHA1 commit id and a kernel type, e.g. CONFIG_LOCALVERSION=-1.2.3-default
# or CONFIG_LOCALVERSION=-999.gdeadbee-default
i f e q ( 1 , $( shell $ {CC } -E -dM $ {CONFIG_FILE } 2> /dev /null |\
grep -m 1 CONFIG_SUSE_KERNEL | awk '{ print $$3 }' ) )
i f n e q ( 1 0 , $( shell $ {CC } -E -dM $ {CONFIG_FILE } 2> /dev /null |\
grep -m 1 CONFIG_SLE_VERSION | awk '{ print $$3 }' ) )
LOCALVERSION := $( shell ${ CC } -E -dM ${ CONFIG_FILE } 2> /dev/null | \
grep -m 1 CONFIG_LOCALVERSION | awk '{ print $$3 }' | \
cut -d'-' -f2 | sed 's/\.g[[:xdigit:]]\{7\}//' )
LOCALVER_A := $( shell echo ${ LOCALVERSION } | cut -d'.' -f1)
LOCALVER_B := $( shell echo ${ LOCALVERSION } | cut -s -d'.' -f2)
LOCALVER_C := $( shell echo ${ LOCALVERSION } | cut -s -d'.' -f3)
SLE_LOCALVERSION_CODE := $( shell expr ${ LOCALVER_A } \* 65536 + \
0${ LOCALVER_B } \* 256 + 0${ LOCALVER_C } )
EXTRA_CFLAGS += -DSLE_LOCALVERSION_CODE= ${ SLE_LOCALVERSION_CODE }
e n d i f
e n d i f
2016-04-05 01:27:00 +00:00
EXTRA_CFLAGS += ${ CFLAGS_EXTRA }
# get the kernel version - we use this to find the correct install path
KVER := $( shell ${ CC } ${ EXTRA_CFLAGS } -E -dM ${ VERSION_FILE } | grep UTS_RELEASE | \
awk '{ print $$3 }' | sed 's/\"//g' )
# assume source symlink is the same as build, otherwise adjust KOBJ
i f n e q ( , $( wildcard /lib /modules /$ {KVER }/build ) )
ifneq ( ${ KSRC } ,$( call readlink,/lib/modules/${ KVER } /build) )
KOBJ = /lib/modules/${ KVER } /build
endif
e n d i f
i f e q ( $ { K V E R _ C O D E } , )
KVER_CODE := $( shell ${ CC } ${ EXTRA_CFLAGS } -E -dM ${ VSP } 2> /dev/null | \
grep -m 1 LINUX_VERSION_CODE | awk '{ print $$3 }' | sed 's/\"//g' )
e n d i f
# minimum_kver_check
#
# helper function to provide uniform output for different drivers to abort the
# build based on kernel version check. Usage: "$(call minimum_kver_check,2,6,XX)".
d e f i n e _ m i n i m u m _ k v e r _ c h e c k
i f e q ( 0 , $( shell [ $ {KVER_CODE } -lt $ ( call get_kvercode ,$ {1},$ {2},$ {3}) ]; echo "$ $ ?") )
$$ ( warning *** Aborting the build.)
$$ ( error This driver is not supported on kernel versions older than ${ 1 } .${ 2 } .${ 3 } )
e n d i f
e n d e f
minimum_kver_check = $( eval $( call _minimum_kver_check,${ 1 } ,${ 2 } ,${ 3 } ) )
################
# Manual Pages #
################
MANSECTION = 7
i f e q ( , $ { M A N D I R } )
# find the best place to install the man page
MANPATH := $( shell ( manpath 2>/dev/null || echo $MANPATH ) | sed 's/:/ /g' )
ifneq ( ,${ MANPATH } )
# test based on inclusion in MANPATH
test_dir = $( findstring ${ dir } , ${ MANPATH } )
else
# no MANPATH, test based on directory existence
test_dir = $( shell [ -e ${ dir } ] && echo ${ dir } )
endif
# our preferred install path
# should /usr/local/man be in here ?
MANDIR := /usr/share/man /usr/man
MANDIR := $( foreach dir, ${ MANDIR } , ${ test_dir } )
MANDIR := $( firstword ${ MANDIR } )
e n d i f
i f e q ( , $ { M A N D I R } )
# fallback to /usr/man
MANDIR := /usr/man
e n d i f
2018-09-05 20:45:41 +00:00
####################
# CCFLAGS variable #
####################
# set correct CCFLAGS variable for kernels older than 2.6.24
i f e q ( 0 , $( shell [ $ {KVER_CODE } -lt $ ( call get_kvercode ,2,6,24) ]; echo $ $ ?) )
CCFLAGS_VAR := EXTRA_CFLAGS
e l s e
CCFLAGS_VAR := ccflags-y
e n d i f
#################
# KBUILD_OUTPUT #
#################
# Only set KBUILD_OUTPUT if the real paths of KOBJ and KSRC differ
i f n e q ( $( call readlink ,$ {KSRC }) , $( call readlink ,$ {KOBJ }) )
export KBUILD_OUTPUT ?= ${ KOBJ }
e n d i f
############################
# Module Install Directory #
############################
# Default to using updates/drivers/net/ethernet/intel/ path, since depmod since
# v3.1 defaults to checking updates folder first, and only checking kernels/
# and extra afterwards. We use updates instead of kernel/* due to desire to
# prevent over-writing built-in modules files.
export INSTALL_MOD_DIR ?= updates/drivers/net/ethernet/intel/${ DRIVER }
######################
# Kernel Build Macro #
######################
# kernel build function
# ${1} is the kernel build target
# ${2} may contain any extra rules to pass directly to the sub-make process
#
# This function is expected to be executed by
# @+$(call kernelbuild,<target>,<extra parameters>)
# from within a Makefile recipe.
#
# The following variables are expected to be defined for its use:
# GCC_I_SYS -- if set it will enable use of gcc-i-sys.sh wrapper to use -isystem
# CCFLAGS_VAR -- the CCFLAGS variable to set extra CFLAGS
# EXTRA_CFLAGS -- a set of extra CFLAGS to pass into the ccflags-y variable
# KSRC -- the location of the kernel source tree to build against
# DRIVER_UPPERCASE -- the uppercase name of the kernel module, set from DRIVER
#
kernelbuild = ${ MAKE } $( if ${ GCC_I_SYS } ,CC= " ${ GCC_I_SYS } " ) \
${ CCFLAGS_VAR } = " ${ EXTRA_CFLAGS } " \
-C " ${ KSRC } " \
CONFIG_${ DRIVER_UPPERCASE } = m \
M = " ${ CURDIR } " \
${ 2 } ${ 1 }