Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions share/build/sbom.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
# Config - set one:
# SBOM_CFLAGS Build CFLAGS whose -D tokens describe the config.
# The driver keeps the -D tokens and forwards -I/-isystem;
# all other flags are dropped.
# all other flags are dropped. An empty value still
# forwards --cflags= unless another config mode is set.
# The driver warns when that value is empty and
# SBOM_SETTINGS_H is unset.
# SBOM_OPTIONS_H A pre-expanded flat #define header (verbatim, scrubbed).
# SBOM_USER_SETTINGS Path to wolfssl/wolfcrypt/settings.h. Also set
# SBOM_USER_SETTINGS_INCLUDES and
Expand Down Expand Up @@ -205,7 +208,7 @@ $(SBOM_TAB)@set -e; \
$(if $(filter 1,$($(2)NO_ARTIFACT_HASH)),, \
$(if $($(2)LIB),--lib "$($(2)LIB)")) \
$(if $(filter 1,$($(2)SOURCE_ONLY)),--source-only) \
$(if $($(2)CFLAGS),--cflags="$$($(2)CFLAGS)") \
$(if $($(2)CFLAGS),--cflags="$$($(2)CFLAGS)",$(if $(or $($(2)OPTIONS_H),$($(2)USER_SETTINGS),$(filter 1,$($(2)SOURCE_ONLY))),,--cflags=)) \
$(if $($(2)SETTINGS_H),--settings-h "$($(2)SETTINGS_H)") \
$(foreach d,$($(2)INCLUDE_DIRS),--include-dir "$(d)") \
$(if $($(2)OPTIONS_H),--options-h "$($(2)OPTIONS_H)") \
Expand Down
30 changes: 22 additions & 8 deletions share/sbom-driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

Config (choose one):
--cflags "..." Raw CFLAGS. -D tokens are expanded through the host
compiler and scrubbed of absolute paths. Pair with
compiler and scrubbed of absolute paths. An empty value
is a configuration with no -D tokens. Pair it with
--settings-h so the capture also sees the header those
-D tokens are interpreted by; without it the dump is the
-D list alone and every derived macro is missing.
tokens are interpreted by. An empty value with no
--settings-h warns on stderr and still generates.
Omit --cflags to choose another config mode.
--options-h PATH A pre-expanded flat #define header, used verbatim (scrubbed).
--user-settings P Path to wolfssl/wolfcrypt/settings.h. Also pass
--user-settings-include and
Expand Down Expand Up @@ -309,8 +311,11 @@ def main():

# Config (choose one).
grp = ap.add_mutually_exclusive_group()
grp.add_argument("--cflags", default="",
help="Build CFLAGS; -D tokens expanded through the host cc.")
grp.add_argument("--cflags", default=None,
Comment thread
sameehj marked this conversation as resolved.
Resolved
help="Build CFLAGS; -D tokens expanded through the host "
"cc. An empty value is a configuration with no -D "
"tokens. An empty value with no --settings-h warns. "
"Omit this flag to choose another config mode.")
ap.add_argument("--settings-h", default="",
help="Settings header -include'd during the --cflags "
"capture, so macros the header derives from the -D "
Expand Down Expand Up @@ -455,10 +460,11 @@ def main():
cmd += ["--lib", args.lib]

# Config.
if args.settings_h and not args.cflags:
if args.settings_h and args.cflags is None:
# --settings-h only feeds the --cflags capture. Ignoring it in
# silence is how a product ends up believing it recorded a
# configuration it never captured.
# configuration it never captured. An empty --cflags= is a real
# configuration, so it still reaches the capture below.
print("WARNING: --settings-h is only used with --cflags; "
"ignoring it here.", file=sys.stderr)
if args.user_settings:
Expand All @@ -475,7 +481,15 @@ def main():
sys.exit(f"ERROR: --options-h '{args.options_h}' does not exist.")
with open(args.options_h, encoding="utf-8", errors="replace") as f:
defines_text = f.read()
elif args.cflags:
elif args.cflags is not None:
Comment thread
sameehj marked this conversation as resolved.
Resolved
# CMake always passes --cflags=, including when DEFS is empty.
# That is a real configuration when --settings-h is set. With
# neither -D flags nor a settings header, warn instead of
# recording a silent empty configuration.
if not args.cflags.strip() and not args.settings_h:
print("WARNING: --cflags is empty and --settings-h is "
"unset; the capture has no -D flags.",
file=sys.stderr)
# A relative path is left to the compiler's -include search.
if (os.path.isabs(args.settings_h)
and not os.path.isfile(args.settings_h)):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_compdb_sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def test_identity_and_dependency_flags_reach_driver(self):
self.assertIn(pair, pairs)
self.assertIn("--cflags=-DFOO=1", argv)

def test_no_defines_passes_empty_cflags(self):
self.write_db([("a.c", [])])
r = self.run_compdb()
self.assertEqual(r.returncode, 0, r.stderr)
self.assertIn("--cflags=", self.driver_argv())

def test_options_h_replaces_cflags(self):
self.write_db([("a.c", ["-DFOO=1"])])
opts = os.path.join(self.d, "options.h")
Expand Down
63 changes: 63 additions & 0 deletions tests/test_sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,69 @@ def unit_tests():
check(drv.is_absolute_path_token(r'C:\Users\ci\sdk'),
"a preserved Windows path is still recognised by the scrub")

# --cflags defaults to None so an explicit empty value is distinct from
# an omitted flag. A compilation database with no -D flags passes
# --cflags= and must still generate, including a --settings-h capture.
print("[unit] empty --cflags")
with tempfile.TemporaryDirectory() as d:
stub = os.path.join(d, "stub-gen-sbom.py")
captured = os.path.join(d, "captured.txt")
with open(stub, "w", encoding="utf-8") as f:
f.write(
"import os, sys\n"
"argv = sys.argv[1:]\n"
"text = ''\n"
"if '--options-h' in argv:\n"
" p = argv[argv.index('--options-h') + 1]\n"
" with open(p, encoding='utf-8') as src:\n"
" text = src.read()\n"
"with open(os.environ['CAPTURE'], 'w', encoding='utf-8') as out:\n"
" out.write(text)\n"
"sys.exit(0)\n")
settings = os.path.join(d, "settings.h")
with open(settings, "w", encoding="utf-8") as f:
f.write("#define FROM_HEADER 1\n")
lic = os.path.join(d, "LICENSE")
with open(lic, "w", encoding="utf-8") as f:
f.write("MIT\n")
env = dict(os.environ, CAPTURE=captured)
base = [sys.executable, os.path.join(SHARE, "sbom-driver.py"),
"--name", "selftest",
"--version", "1.0.0", "--license-file", lic,
"--gen-sbom", stub, "--root", d, "--no-artifact-hash",
"--cdx-out", "e.cdx.json", "--spdx-out", "e.spdx.json"]
r = subprocess.run(base, cwd=d, capture_output=True, text=True, env=env)
check(r.returncode != 0 and "pass one of --cflags" in r.stderr,
"an omitted config mode still fails")
r = subprocess.run(base + ["--settings-h", settings],
cwd=d, capture_output=True, text=True, env=env)
check(r.returncode != 0 and "pass one of --cflags" in r.stderr,
"--settings-h without --cflags still fails")
check("only used with --cflags" in r.stderr,
"--settings-h without --cflags warns")
if os.path.isfile(captured):
os.remove(captured)
r = subprocess.run(base + ["--cflags="],
cwd=d, capture_output=True, text=True, env=env)
check(r.returncode == 0,
Comment thread
sameehj marked this conversation as resolved.
Resolved
"empty --cflags= generates\n" + r.stderr)
check("no -D flags" in r.stderr,
"empty --cflags= without --settings-h warns")
if os.path.isfile(captured):
os.remove(captured)
r = subprocess.run(base + ["--cflags=", "--settings-h", settings],
cwd=d, capture_output=True, text=True, env=env)
text = ""
if os.path.isfile(captured):
with open(captured, encoding="utf-8") as f:
text = f.read()
check(r.returncode == 0 and "#define FROM_HEADER 1" in text,
"empty --cflags= still captures --settings-h\n" + r.stderr)
check("only used with --cflags" not in r.stderr,
"empty --cflags= keeps --settings-h")
check("no -D flags" not in r.stderr,
"empty --cflags= with --settings-h does not warn")

# Validator: a good CycloneDX passes with the right prefix; a wrong prefix
# fails.
with tempfile.TemporaryDirectory() as d:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_sbom_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ def test_no_artifact_hash_alone(self):
self.assertNotIn("--lib", args)
self.assertNotIn("--srcs-file", args)

def test_empty_cflags_forwarded_with_settings_header(self):
result = self._make(
"SBOM_NAME := demo\nSBOM_LIB := libdemo.a\n"
"SBOM_SETTINGS_H := cfg.h\n")
self.assertEqual(result.returncode, 0, result.stderr)
args = self._args()
self.assertIn("--cflags=", args)
self.assertEqual(args[args.index("--settings-h") + 1], "cfg.h")

def test_options_header_does_not_add_empty_cflags(self):
result = self._make(
"SBOM_NAME := demo\nSBOM_LIB := libdemo.a\n"
"SBOM_OPTIONS_H := opt.h\n")
self.assertEqual(result.returncode, 0, result.stderr)
args = self._args()
self.assertFalse(any(a.startswith("--cflags") for a in args))
self.assertEqual(args[args.index("--options-h") + 1], "opt.h")


if __name__ == "__main__":
unittest.main()
Loading