1 | # $Id: Makefile-header.gmk 100195 2023-06-16 08:25:34Z vboxsync $
|
---|
2 | ## @file
|
---|
3 | # VirtualBox Guest Additions kernel module Makefile, common parts.
|
---|
4 | #
|
---|
5 | # (For 2.6.x, the main file must be called 'Makefile'!)
|
---|
6 | #
|
---|
7 |
|
---|
8 | #
|
---|
9 | # Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
10 | #
|
---|
11 | # This file is part of VirtualBox base platform packages, as
|
---|
12 | # available from https://www.virtualbox.org.
|
---|
13 | #
|
---|
14 | # This program is free software; you can redistribute it and/or
|
---|
15 | # modify it under the terms of the GNU General Public License
|
---|
16 | # as published by the Free Software Foundation, in version 3 of the
|
---|
17 | # License.
|
---|
18 | #
|
---|
19 | # This program is distributed in the hope that it will be useful, but
|
---|
20 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | # General Public License for more details.
|
---|
23 | #
|
---|
24 | # You should have received a copy of the GNU General Public License
|
---|
25 | # along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | #
|
---|
27 | # SPDX-License-Identifier: GPL-3.0-only
|
---|
28 | #
|
---|
29 |
|
---|
30 | # Testing:
|
---|
31 | # * Building with KERN_DIR set uses the value specified and
|
---|
32 | # the default value for the unspecified one if any.
|
---|
33 |
|
---|
34 | #
|
---|
35 | # These file should be included by the Makefiles for any kernel modules we
|
---|
36 | # build as part of the Guest Additions. The intended way of doing this is as
|
---|
37 | # follows:
|
---|
38 | #
|
---|
39 | # # Linux kbuild sets this to our source directory if we are called from there
|
---|
40 | # obj ?= $(CURDIR)
|
---|
41 | # include $(obj)/Makefile-header.gmk
|
---|
42 | # VBOXMOD_NAME = <name of the module to be built, without extension>
|
---|
43 | # VBOXMOD_OBJS = <list of object files which should be included>
|
---|
44 | # VBOXMOD_DEFS = <any additional defines which this module needs>
|
---|
45 | # VBOXMOD_INCL = <any additional include paths which this module needs>
|
---|
46 | # VBOXMOD_CFLAGS = <any additional CFLAGS which this module needs>
|
---|
47 | # include $(obj)/Makefile-footer.gmk
|
---|
48 | #
|
---|
49 | # To avoid potential confusion between kmk/kBuild and linux/kbuild,
|
---|
50 | # we use VBOX_KBUILD_TARGET_ARCH instead of KBUILD_TARGET_ARCH and
|
---|
51 | # VBOX_KBUILD_TYPE instead of KBUILD_TYPE. The VBOX_KBUILD_ variable
|
---|
52 | # variant takes percedence over the kmk/kBuild ones.
|
---|
53 | #
|
---|
54 |
|
---|
55 |
|
---|
56 | #
|
---|
57 | # First, figure out which architecture we're targeting and the build type.
|
---|
58 | # (We have to support basic cross building (ARCH=i386|x86_64).)
|
---|
59 | # While at it, warn about *BUILD_* vars found to help with user problems.
|
---|
60 | #
|
---|
61 |
|
---|
62 | # VBOX_KBUILD_TARGET_ARCH = amd64|x86|arm64|arm32
|
---|
63 | ifeq ($(filter-out x86_64 amd64 AMD64,$(shell uname -m)),)
|
---|
64 | VBOX_KBUILD_TARGET_ARCH_DEFAULT := amd64
|
---|
65 | else
|
---|
66 | ifeq ($(filter-out aarch64,$(shell uname -m)),)
|
---|
67 | VBOX_KBUILD_TARGET_ARCH_DEFAULT := arm64
|
---|
68 | else
|
---|
69 | ifeq ($(filter-out armv7,$(shell uname -m)),)
|
---|
70 | VBOX_KBUILD_TARGET_ARCH_DEFAULT := arm32
|
---|
71 | else
|
---|
72 | VBOX_KBUILD_TARGET_ARCH_DEFAULT := x86
|
---|
73 | endif
|
---|
74 | endif
|
---|
75 | endif
|
---|
76 | ifdef VBOX_KBUILD_TARGET_ARCH
|
---|
77 | ifneq ($(filter-out amd64 x86 arm64,$(VBOX_KBUILD_TARGET_ARCH)),)
|
---|
78 | $(warning Ignoring unknown VBOX_KBUILD_TARGET_ARCH value '$(VBOX_KBUILD_TARGET_ARCH)'.)
|
---|
79 | VBOX_KBUILD_TARGET_ARCH :=
|
---|
80 | endif
|
---|
81 | else
|
---|
82 | ifdef KBUILD_TARGET_ARCH
|
---|
83 | ifneq ($(filter-out amd64 x86 arm64,$(KBUILD_TARGET_ARCH)),)
|
---|
84 | $(warning Ignoring unknown KBUILD_TARGET_ARCH value '$(KBUILD_TARGET_ARCH)'.)
|
---|
85 | VBOX_KBUILD_TARGET_ARCH :=
|
---|
86 | endif
|
---|
87 | else
|
---|
88 | ifdef BUILD_TARGET_ARCH
|
---|
89 | $(warning BUILD_TARGET_ARCH is deprecated, use VBOX_KBUILD_TARGET_ARCH instead.)
|
---|
90 | ifneq ($(filter-out amd64 x86 arm64,$(BUILD_TARGET_ARCH)),)
|
---|
91 | $(warning Ignoring unknown BUILD_TARGET_ARCH value '$(BUILD_TARGET_ARCH)'.)
|
---|
92 | VBOX_KBUILD_TARGET_ARCH :=
|
---|
93 | endif
|
---|
94 | endif
|
---|
95 | endif
|
---|
96 | endif
|
---|
97 | ifeq ($(VBOX_KBUILD_TARGET_ARCH),)
|
---|
98 | ifeq ($(ARCH),x86_64)
|
---|
99 | VBOX_KBUILD_TARGET_ARCH := amd64
|
---|
100 | else
|
---|
101 | ifeq ($(ARCH),i386)
|
---|
102 | VBOX_KBUILD_TARGET_ARCH := x86
|
---|
103 | else
|
---|
104 | VBOX_KBUILD_TARGET_ARCH := $(VBOX_KBUILD_TARGET_ARCH_DEFAULT)
|
---|
105 | endif
|
---|
106 | endif
|
---|
107 | else
|
---|
108 | ifneq ($(VBOX_KBUILD_TARGET_ARCH),$(VBOX_KBUILD_TARGET_ARCH_DEFAULT))
|
---|
109 | $(warning Using VBOX_KBUILD_TARGET_ARCH='$(VBOX_KBUILD_TARGET_ARCH)' from the $(origin VBOX_KBUILD_TARGET_ARCH).)
|
---|
110 | endif
|
---|
111 | endif
|
---|
112 |
|
---|
113 | # VBOX_KBUILD_TYPE = release|debug|profile|strict|asan
|
---|
114 | ifdef VBOX_KBUILD_TYPE
|
---|
115 | VBOX_KBUILD_TYPE_VAR := VBOX_KBUILD_TYPE
|
---|
116 | ifneq ($(filter-out release profile debug strict asan,$(VBOX_KBUILD_TYPE)),)
|
---|
117 | $(warning Ignoring unknown VBOX_KBUILD_TYPE value '$(VBOX_KBUILD_TYPE)'.)
|
---|
118 | VBOX_KBUILD_TYPE :=
|
---|
119 | endif
|
---|
120 | else
|
---|
121 | ifdef KBUILD_TYPE
|
---|
122 | VBOX_KBUILD_TYPE_VAR := KBUILD_TYPE
|
---|
123 | VBOX_KBUILD_TYPE := $(KBUILD_TYPE)
|
---|
124 | ifneq ($(filter-out release profile debug strict asan,$(KBUILD_TYPE)),)
|
---|
125 | $(warning Ignoring unknown KBUILD_TYPE value '$(KBUILD_TYPE)'.)
|
---|
126 | VBOX_KBUILD_TYPE :=
|
---|
127 | endif
|
---|
128 | else
|
---|
129 | ifdef BUILD_TYPE
|
---|
130 | $(warning BUILD_TYPE is deprecated, use VBOX_KBUILD_TYPE instead.)
|
---|
131 | VBOX_KBUILD_TYPE_VAR := BUILD_TYPE
|
---|
132 | VBOX_KBUILD_TYPE := $(BUILD_TYPE)
|
---|
133 | ifneq ($(filter-out release profile debug strict asan,$(BUILD_TYPE)),)
|
---|
134 | $(warning Ignoring unknown BUILD_TYPE value '$(BUILD_TYPE)'.)
|
---|
135 | VBOX_KBUILD_TYPE :=
|
---|
136 | endif
|
---|
137 | endif
|
---|
138 | endif
|
---|
139 | endif
|
---|
140 | ifeq ($(VBOX_KBUILD_TYPE),)
|
---|
141 | VBOX_KBUILD_TYPE_VAR = VBOX_KBUILD_TYPE
|
---|
142 | VBOX_KBUILD_TYPE := release
|
---|
143 | else
|
---|
144 | ifneq ($(VBOX_KBUILD_TYPE),release)
|
---|
145 | ifndef VBOX_KERN_QUIET
|
---|
146 | $(warning Using VBOX_KBUILD_TYPE='$(VBOX_KBUILD_TYPE)' from the $(origin $(VBOX_KBUILD_TYPE_VAR)) ($(VBOX_KBUILD_TYPE_VAR)).)
|
---|
147 | endif
|
---|
148 | endif
|
---|
149 | endif
|
---|
150 |
|
---|
151 | ifeq ($(USERNAME),)
|
---|
152 | USERNAME := noname
|
---|
153 | endif
|
---|
154 |
|
---|
155 | ifeq ($(KERNELRELEASE),)
|
---|
156 |
|
---|
157 | #
|
---|
158 | # building from this directory
|
---|
159 | #
|
---|
160 |
|
---|
161 | # kernel base directory
|
---|
162 | ifdef KERN_DIR
|
---|
163 | ifndef KERN_VER
|
---|
164 | ifeq ($(filter %/build,$(KERN_DIR)),)
|
---|
165 | $(error The variable KERN_DIR must be a kernel build folder and end with /build without a trailing slash, or KERN_VER must be set)
|
---|
166 | endif
|
---|
167 | endif
|
---|
168 | endif
|
---|
169 |
|
---|
170 | ifndef KERN_VER
|
---|
171 | ifdef KERN_DIR
|
---|
172 | KERN_VER = $(notdir $(patsubst %/build,%,$(KERN_DIR)))
|
---|
173 | ifeq ($(shell expr $(KERN_VER) : '[0-9]*\.[0-9]*.[0-9]*'),0)
|
---|
174 | $(error The kernel build folder path must end in <version>/build, or the variable KERN_VER must be set)
|
---|
175 | endif
|
---|
176 | endif
|
---|
177 | KERN_VER ?= $(shell uname -r)
|
---|
178 | endif
|
---|
179 |
|
---|
180 | ifeq ($(KERN_DIR),)
|
---|
181 | KERN_DIR := /lib/modules/$(KERN_VER)/build
|
---|
182 | endif
|
---|
183 |
|
---|
184 | # Is this 2.4 or < 2.6.6? The UTS_RELEASE "2.x.y.z" define is present in the header until 2.6.1x something.
|
---|
185 | ifeq ($(shell if grep '"2\.4\.' $(KERN_DIR)/include/linux/version.h > /dev/null 2>&1; then echo yes; fi),yes)
|
---|
186 | KERN_VERSION := 24
|
---|
187 | VBOX_KERN_GROKS_EXTMOD :=
|
---|
188 | else
|
---|
189 | KERN_VERSION := 26
|
---|
190 | VBOX_KERN_GROKS_EXTMOD := yes
|
---|
191 | ifeq ($(shell if grep '"2\.6\.[012345][."]' $(KERN_DIR)/include/linux/version.h > /dev/null 2>&1; then echo yes; fi),yes)
|
---|
192 | VBOX_KERN_GROKS_EXTMOD :=
|
---|
193 | endif
|
---|
194 | VBOX_KERN_GROKS_SUBDIRS :=
|
---|
195 | ifeq ($(shell if grep '"[432]\.' $(KERN_DIR)/include/linux/version.h > /dev/null 2>&1; then echo yes; fi),yes)
|
---|
196 | VBOX_KERN_GROKS_SUBDIRS := yes
|
---|
197 | endif
|
---|
198 | endif
|
---|
199 |
|
---|
200 | #
|
---|
201 | # Hack for Ubuntu 4.10 where we determine 2.6.8.1-3-generic-amd64 here, but the
|
---|
202 | # the next invocation (M/SUBDIR) ends up with KERNELRELEASE=2.6.8.1-3.
|
---|
203 | #
|
---|
204 | ifeq ($(shell if grep '"[2]\.' $(KERN_DIR)/include/linux/version.h > /dev/null 2>&1; then echo yes; fi),yes)
|
---|
205 | export KERN_VER KERN_DIR
|
---|
206 | else
|
---|
207 | # This makefile received some variables in the command line which should
|
---|
208 | # not be passed to the recursive make invocations (of the Linux makefile
|
---|
209 | # for building kernel modules), since they should derive KERN_DIR from the
|
---|
210 | # respective command line variables to come up with the value they expect.
|
---|
211 | unexport KERN_VER KERN_DIR
|
---|
212 | MAKEOVERRIDES := $(filter-out KERN_VER=% KERN_DIR=%,$(MAKEOVERRIDES))
|
---|
213 | endif
|
---|
214 |
|
---|
215 | else # neq($(KERNELRELEASE),)
|
---|
216 |
|
---|
217 | #
|
---|
218 | # building from kbuild (make -C <kernel_directory> M=`pwd`)
|
---|
219 | #
|
---|
220 |
|
---|
221 | # guess kernel version (24 or 26)
|
---|
222 | ifeq ($(VERSION).$(PATCHLEVEL),2.4)
|
---|
223 | KERN_VERSION := 24
|
---|
224 | VBOX_KERN_GROKS_EXTMOD :=
|
---|
225 | else
|
---|
226 | KERN_VERSION := 26
|
---|
227 | VBOX_KERN_GROKS_EXTMOD := yes
|
---|
228 | ifeq ($(VERSION).$(PATCHLEVEL),2.6)
|
---|
229 | ifeq ($(findstring @$(SUBLEVEL)@,@0@1@2@3@4@5@),@$(SUBLEVEL)@)
|
---|
230 | VBOX_KERN_GROKS_EXTMOD :=
|
---|
231 | endif
|
---|
232 | endif
|
---|
233 | VBOX_KERN_GROKS_SUBDIRS :=
|
---|
234 | ifeq ($(VERSION),2)
|
---|
235 | VBOX_KERN_GROKS_SUBDIRS := yes
|
---|
236 | endif
|
---|
237 | ifeq ($(VERSION),3)
|
---|
238 | VBOX_KERN_GROKS_SUBDIRS := yes
|
---|
239 | endif
|
---|
240 | ifeq ($(VERSION),4)
|
---|
241 | VBOX_KERN_GROKS_SUBDIRS := yes
|
---|
242 | endif
|
---|
243 | endif
|
---|
244 |
|
---|
245 | KERN_VER := $(KERNELRELEASE)
|
---|
246 |
|
---|
247 | ifeq ($(KERN_DIR),)
|
---|
248 | ifneq ($(srctree),)
|
---|
249 | KERN_DIR := $(srctree)
|
---|
250 | else
|
---|
251 | KERN_DIR := /lib/modules/$(KERN_VER)/build
|
---|
252 | endif
|
---|
253 | endif
|
---|
254 | endif # neq($(KERNELRELEASE),)
|
---|
255 |
|
---|
256 | # Kernel build folder
|
---|
257 | ifneq ($(shell if test -d $(KERN_DIR); then echo yes; fi),yes)
|
---|
258 | $(error Error: unable to find the headers of the Linux kernel to build against (KERN_DIR=$(KERN_DIR)). \
|
---|
259 | Specify KERN_VER=<version> (currently $(KERN_VER)) and run Make again)
|
---|
260 | endif
|
---|
261 | # Kernel include folder
|
---|
262 | KERN_INCL := $(KERN_DIR)/include
|
---|
263 | # module install folder
|
---|
264 | INSTALL_MOD_DIR ?= misc
|
---|
265 | MODULE_DIR := $(INSTALL_MOD_PATH)/lib/modules/$(KERN_VER)/$(INSTALL_MOD_DIR)
|
---|
266 |
|
---|
267 | # For VBOX_GCC_CHECK_CC
|
---|
268 | VBOX_CLOSEPAR := )
|
---|
269 | VBOX_DOLLAR := $$
|
---|
270 | ## Modified VBOX_GCC_CHECK_EX_CC_CXX macro from /Config.kmk.
|
---|
271 | # @param 1 The option to test for.
|
---|
272 | # @param 2 The return value when supported.
|
---|
273 | # @param 3 The return value when NOT supported.
|
---|
274 | VBOX_GCC_CHECK_CC = $(shell \
|
---|
275 | > /tmp/$(VBOX_DOLLAR)$(VBOX_DOLLAR).check.c; \
|
---|
276 | if $(CC) $(subst -Wno-,-W,$(1)) -Werror -c -o /dev/null /tmp/$(VBOX_DOLLAR)$(VBOX_DOLLAR).check.c > /dev/null 2>&1; then \
|
---|
277 | case "`LC_ALL=C $(CC) $(subst -Wno-,-W,$(1)) -Werror -c -o /dev/null /tmp/$(VBOX_DOLLAR)$(VBOX_DOLLAR).check.c 2>&1`" in \
|
---|
278 | "error: unknown warning option"*$(VBOX_CLOSEPAR) echo "$(3)";; \
|
---|
279 | *$(VBOX_CLOSEPAR) echo "$(2)";; \
|
---|
280 | esac; \
|
---|
281 | else echo "$(3)"; fi; \
|
---|
282 | rm -f /tmp/$(VBOX_DOLLAR)$(VBOX_DOLLAR).check.c; )
|
---|
283 |
|
---|
284 | #
|
---|
285 | # Guess the module directory ASSUMING that this file is located in that directory.
|
---|
286 | # Note! The special MAKEFILE_LIST variable was introduced in GNU make 3.80.
|
---|
287 | #
|
---|
288 | ifndef VBOX_MODULE_SRC_DIR
|
---|
289 | ifdef MAKEFILE_LIST
|
---|
290 | VBOX_MODULE_SRC_DIR := $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
|
---|
291 | else
|
---|
292 | VBOX_MODULE_SRC_DIR := $(CURDIR)/
|
---|
293 | endif
|
---|
294 | endif
|
---|
295 |
|
---|
296 |
|
---|
297 | # debug - show guesses.
|
---|
298 | ifdef DEBUG
|
---|
299 | ifndef VBOX_KERN_QUIET
|
---|
300 | $(warning dbg: INSTALL_MOD_PATH = $(INSTALL_MOD_PATH))
|
---|
301 | $(warning dbg: INSTALL_MOD_DIR = $(INSTALL_MOD_DIR))
|
---|
302 | $(warning dbg: KERN_DIR = $(KERN_DIR))
|
---|
303 | $(warning dbg: KERN_INCL = $(KERN_INCL))
|
---|
304 | $(warning dbg: KERN_VERSION = $(KERN_VERSION))
|
---|
305 | $(warning dbg: MODULE_DIR = $(MODULE_DIR))
|
---|
306 | $(warning dbg: VBOX_MODULE_SRC_DIR = $(VBOX_MODULE_SRC_DIR))
|
---|
307 | endif
|
---|
308 | endif
|
---|
309 |
|
---|