# $Id: Makefile-footer.gmk $
## @file
# VirtualBox Guest Additions kernel module Makefile, common parts.
#
# See Makefile-header.gmk for details of how to use this.
#

#
# Copyright (C) 2006-2025 Oracle and/or its affiliates.
#
# This file is part of VirtualBox base platform packages, as
# available from https://www.virtualbox.org.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, in version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-3.0-only
#

VBOXMOD_0_TARGET = $(VBOXMOD_NAME)

KBUILD_VERBOSE ?= 1 # Variable belongs to our kBuild, not the linux one.
VBOX_LNX_VERBOSE = $(if $(KBUILD_VERBOSE),1,)

#
# Compiler options
#
VBOXMOD_0_KFLAGS := -D__KERNEL__ -DMODULE -DRT_WITHOUT_PRAGMA_ONCE $(addprefix -D,$(VBOXMOD_DEFS))
ifeq ($(VBOX_KBUILD_TARGET_ARCH),amd64)
VBOXMOD_0_KFLAGS += -DRT_ARCH_AMD64
else
 ifeq ($(VBOX_KBUILD_TARGET_ARCH),x86)
  VBOXMOD_0_KFLAGS += -DRT_ARCH_X86
 else
  ifeq ($(VBOX_KBUILD_TARGET_ARCH),arm64)
   VBOXMOD_0_KFLAGS += -DRT_ARCH_ARM64
  else
   ifeq ($(VBOX_KBUILD_TARGET_ARCH),arm32)
    VBOXMOD_0_KFLAGS += -DRT_ARCH_ARM32
   else
    $(error The variable VBOX_KBUILD_TARGET_ARCH doesn't contain any of the following: amd64, x86, arm64, arm32)
   endif
  endif
 endif
endif

ifeq ($(VBOX_KBUILD_TYPE),debug)
# The -Wno-array-bounds is because of a bug in gcc 4.something, see
# https://sourceware.org/bugzilla/show_bug.cgi?id=10001
 VBOXMOD_0_KFLAGS  += -DDEBUG -DDEBUG_$(subst $(subst _, ,_),_,$(USERNAME)) -DDEBUG_USERNAME=$(subst $(subst _, ,_),_,$(USERNAME))
 ifeq ($(shell expr $(KERN_VER) : '[23]\.'),0)
  VBOXMOD_0_KFLAGS  += -Werror -Wall -Wno-array-bounds
# clang objects to calls to __builtin_frame_address with a non-zero argument
# on general principles in -Wall mode. The x86 linux kernel has two such calls
# in the thread_info.h / arch_within_stack_frames(), though probably safe.
  ifndef ($(LLVM),) # (This is how the Linux root Makefile (since 5.7) does the test.)
   VBOXMOD_0_KFLAGS += -Wno-frame-address
  endif
 endif
endif

ifeq ($(VBOX_KERN_GROKS_EXTMOD),)
#
# Pre 2.6.6
#
# Note: While pre 2.6.6 kernels could also do "proper" builds from kbuild, the
# make script needed to support it was somewhat different from 2.6.  Since this
# script works and pre-2.6.6 is not a moving target we will not try do do things
# the "proper" way.
#
VBOXMOD_EXT := o

 ifeq ($(VBOX_KBUILD_TARGET_ARCH),amd64)
VBOXMOD_0_KFLAGS += -mcmodel=kernel
 endif
 ifeq ($(KERN_VERSION),24)
VBOXMOD_0_KFLAGS += -DVBOX_LINUX_2_4
 endif

CFLAGS := -O2 $(VBOXMOD_CFLAGS) $(addprefix -I,$(KERN_INCL) $(VBOXMOD_INCL)) $(VBOXMOD_0_KFLAGS) $(KDEBUG)

# 2.4 Module linking
$(VBOXMOD_0_TARGET).$(VBOXMOD_EXT): $(VBOXMOD_OBJS)
	$(LD) -o $@ -r $(VBOXMOD_OBJS)

all: $(VBOXMOD_0_TARGET)
$(VBOXMOD_0_TARGET): $(VBOXMOD_0_TARGET).$(VBOXMOD_EXT)

install: $(VBOXMOD_0_TARGET)
	@mkdir -p $(MODULE_DIR); \
	install -m 0644 -o root -g root $(VBOXMOD_0_TARGET).$(VBOXMOD_EXT) $(MODULE_DIR); \
	PATH="$(PATH):/bin:/sbin" depmod -a; sync

clean:
	for f in $(sort $(dir $(VBOXMOD_OBJS))); do rm -f $$f/*.o $$f/.*.cmd $$f/.*.flags; done
	rm -rf .$(VBOXMOD_NAME)* .tmp_ver* $(VBOXMOD_NAME).* Modules.symvers modules.order

.PHONY: all $(VBOXMOD_0_TARGET) install clean

else  # VBOX_KERN_GROKS_EXTMOD
#
# 2.6.6 and later
#
VBOXMOD_EXT := ko

# build defs
EXTRA_CFLAGS += $(VBOXMOD_CFLAGS) $(addprefix -I,$(KERN_INCL) $(VBOXMOD_INCL)) $(VBOXMOD_0_KFLAGS) $(KDEBUG)
ccflags-y += $(EXTRA_CFLAGS)
$(VBOXMOD_0_TARGET)-y := $(VBOXMOD_OBJS)
obj-m += $(VBOXMOD_0_TARGET).o

# Trigger parallel make job.
 ifndef VBOX_NOJOBS
JOBS := $(shell (getconf _NPROCESSORS_ONLN || grep -Ec '^processor|^CPU[0-9]' /proc/cpuinfo) 2>/dev/null)
  ifeq ($(JOBS),0)
   override JOBS := 1
  endif
 else
JOBS :=
 endif

VBOX_SUBDIR_VAR=$(if $(VBOX_KERN_GROKS_SUBDIRS),SUBDIRS,M)

# OL/UEK: CONFIG_MODULE_SIG= disables module signing for external modules.
# Ubuntu 18.04.3: CONFIG_MODULE_SIG_ALL= disables signing of all modules.
# We disable signing because we don't have any private key and want to leave
# the key handling and secure boot config to the user.
 ifndef VBOX_ALLOW_MODULE_SIGNING
VBOX_DISABLE_SIGN=CONFIG_MODULE_SIG= CONFIG_MODULE_SIG_ALL=
 endif

# rules:
all: $(VBOXMOD_0_TARGET)

$(VBOXMOD_0_TARGET):
	$(MAKE) V=$(VBOX_LNX_VERBOSE) $(VBOX_DISABLE_SIGN) -C $(KERN_DIR) $(VBOX_SUBDIR_VAR)=$(CURDIR) SRCROOT=$(CURDIR) $(if $(JOBS),-j$(JOBS),) modules

install: $(VBOXMOD_0_TARGET)
	$(MAKE) V=$(VBOX_LNX_VERBOSE) $(VBOX_DISABLE_SIGN) -C $(KERN_DIR) $(VBOX_SUBDIR_VAR)=$(CURDIR) SRCROOT=$(CURDIR) INSTALL_MOD_PATH=$(INSTALL_MOD_PATH) INSTALL_MOD_DIR=$(INSTALL_MOD_DIR) modules_install

modules_install: install

clean:
	$(MAKE) V=$(VBOX_LNX_VERBOSE) $(VBOX_DISABLE_SIGN) -C $(KERN_DIR) $(VBOX_SUBDIR_VAR)=$(CURDIR) SRCROOT=$(CURDIR) clean

.PHONY: all $(VBOXMOD_0_TARGET) install modules_install clean
endif # VBOX_KERN_GROKS_EXTMOD

