From 8defc832e8777ac09c4a068cd15e1c28c490de10 Mon Sep 17 00:00:00 2001 From: Lars Kakavandi-Nielsen Date: Wed, 22 Jul 2026 16:09:02 +0200 Subject: [PATCH 1/2] 130: Fixing using lower case o when setting optimisation level -o0 was replaced with -O0 to correctly set optimisation level. This issue only happens with build target LINUX Fixes #130 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c35a352..b9c90c0 100755 --- a/Makefile +++ b/Makefile @@ -114,7 +114,7 @@ endif # Defaults for target linux ifeq ($(TARGET),linux) -CC ?= gcc -ggdb -o0 -Wall +CC ?= gcc -ggdb -O0 -Wall endif SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) From b72f7f75909e5e8426b988165a8c9597229d4f02 Mon Sep 17 00:00:00 2001 From: Ulrond <131959864+Ulrond@users.noreply.github.com> Date: Mon, 27 Jul 2026 21:54:34 +0100 Subject: [PATCH 2/2] test: guard against mistyped compiler flags Adds check_makefile_flags to the tests suite, run before build.sh so a flag typo is reported in seconds rather than after a full build. It walks the build files for a lowercase -o glued to an optimisation level (-o0, -os, -og). gcc accepts these silently and a later -o $@ on the same command line wins, so nothing ever fails - which is why #130 went unnoticed. Comments are stripped before matching, so prose describing the typo does not trip the check. The script also supports asserting what a variable expands to (--expect CC=-ggdb,-O0,-Wall) via make printenv. That is deliberately not wired up yet: 'CC ?=' in the top level Makefile never fires, since GNU make treats ?= as ifeq (origin CC,undefined) and the built-in CC has origin default. The Makefile records this so the expectation can be turned on once that is resolved. Verified by reintroducing -o0: the check fails, exit 1. Refs #130 --- tests/Makefile | 20 ++- tests/check_makefile_flags.py | 223 ++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 2 deletions(-) create mode 100755 tests/check_makefile_flags.py diff --git a/tests/Makefile b/tests/Makefile index 5d415ff..5c64485 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -32,6 +32,8 @@ BIN_DIR := $(ROOT_DIR)/build/bin TOP_DIR := $(ROOT_DIR) ECHOE = /bin/echo -e +FLAGS_SCRIPT = $(ROOT_DIR)/check_makefile_flags.py + SRC_DIRS = $(ROOT_DIR)/src INC_DIRS := $(ROOT_DIR)/../include LIB_DIR = $(ROOT_DIR)/../build/${TARGET}/lib @@ -71,9 +73,23 @@ export TARGET_EXEC export BUILD_DIR export LIB_DIR -.PHONY: clean list build skeleton +.PHONY: clean list build skeleton check_makefile_flags + +# Walk the build files for a lowercase -o glued to an optimisation level +# (-o0, -os, -og). gcc accepts these silently and a later -o $@ on the same +# command line wins, so nothing ever fails - which is why #130 went unnoticed. +# Runs before build.sh so a flag typo is reported in seconds. +# +# The script also supports asserting what a variable expands to +# (--expect CC=-ggdb,-O0,-Wall). Not wired up here yet: `CC ?=` in the top +# level Makefile never fires, because GNU make treats ?= as +# "ifeq ($(origin CC),undefined)" and the built-in CC has origin default. Once +# that is resolved the expectation can be turned on. +check_makefile_flags: + @$(ECHOE) UT [$@] + @$(FLAGS_SCRIPT) $(ROOT_DIR)/.. -build: +build: check_makefile_flags @$(ECHOE) UT [$@] $(ROOT_DIR)/build.sh make -C ./ut-core test diff --git a/tests/check_makefile_flags.py b/tests/check_makefile_flags.py new file mode 100755 index 0000000..267c927 --- /dev/null +++ b/tests/check_makefile_flags.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python3 +# /* +# * If not stated otherwise in this file or this component's LICENSE file the +# * following copyright and licenses apply: +# * +# * Copyright 2023 RDK Management +# * +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# */ + +"""Guard against mistyped compiler flags in the build files. + +Two checks, both static enough to run before anything is compiled: + + scan walk the build files looking for a lowercase `-o` immediately + followed by an optimisation level (`-o0`, `-os`, `-og`, ...). + Lowercase `-o` means "write output to this file", so `-o0` asks + for a file named `0` rather than setting optimisation. gcc accepts + it silently, and a later `-o $@` on the same command line wins, so + nothing ever fails - see ut-core issue #256 / ut-control issue #130. + + expect ask `make printenv` what a variable actually expands to and assert + the flags we intend are present. This catches the flag being + dropped or overwritten as well as mistyped. +""" + +import argparse +import os +import re +import subprocess +import sys + +# ANSI escape codes for colored output +RED = "\033[91m" +GREEN = "\033[92m" +YELLOW = "\033[93m" +RESET = "\033[0m" + +# Build files worth scanning: makefiles and the shell scripts that drive them. +SCANNED_NAMES = ("Makefile", "makefile", "GNUmakefile") +SCANNED_SUFFIXES = (".mk", ".sh") + +# Directories holding third party or generated content, not our build files. +SKIPPED_DIRS = {".git", "build", "framework", "sysroot", "cpp_libs", "node_modules"} + +# A lowercase -o glued directly to an optimisation level. The trailing lookahead +# keeps `-o3rdparty.o` and similar output filenames out of the results. +OPT_TYPO_PATTERN = re.compile(r"(?