From patchwork Wed Sep 10 09:37:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 279 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757497086; bh=rsZgujyH3lHH0ilBZaTMeevKmUoCnt8iK4qc5Bs2JJM=; h=From:To:Date:CC:Subject:List-Id:List-Archive:List-Help:List-Owner: List-Post:List-Subscribe:List-Unsubscribe:From; b=WWBRGCuiZYq3KI0HZDsJzGf5vimZK1M3q5VoeRwmQrUuOAm7i3UXoRzkxcQxRvNEz 2y1XtEBdB/IvAkbBytppg6Iaj4dmuHBssSe+BsJg+TUG+POPMGfpwiLUMbuy+fW3kL +Q0dLzKxZv0K8U9usNerByGeT2cZ8ApqRsoPjhpaorBeMJ17fM5A4XEoja0H7y3X/l BPllI8XQdx8n+YqkSnZwYD8kCfLr0W9v1i6poBRzIirVtr/PWtzJGnnk3kTz3dytbl Id7FBX2l7bDryoxb5zNv2YHcg9ufwlP3AWzwQm/ibug2mgm+PVGZTnvfeQncjvKfxe +fLZ2/SpX8pQA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 306BA679E7 for ; Wed, 10 Sep 2025 03:38:06 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id NeDtIctbLFiI for ; Wed, 10 Sep 2025 03:38:06 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757497086; bh=rsZgujyH3lHH0ilBZaTMeevKmUoCnt8iK4qc5Bs2JJM=; h=From:To:Date:CC:Subject:List-Id:List-Archive:List-Help:List-Owner: List-Post:List-Subscribe:List-Unsubscribe:From; b=WWBRGCuiZYq3KI0HZDsJzGf5vimZK1M3q5VoeRwmQrUuOAm7i3UXoRzkxcQxRvNEz 2y1XtEBdB/IvAkbBytppg6Iaj4dmuHBssSe+BsJg+TUG+POPMGfpwiLUMbuy+fW3kL +Q0dLzKxZv0K8U9usNerByGeT2cZ8ApqRsoPjhpaorBeMJ17fM5A4XEoja0H7y3X/l BPllI8XQdx8n+YqkSnZwYD8kCfLr0W9v1i6poBRzIirVtr/PWtzJGnnk3kTz3dytbl Id7FBX2l7bDryoxb5zNv2YHcg9ufwlP3AWzwQm/ibug2mgm+PVGZTnvfeQncjvKfxe +fLZ2/SpX8pQA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1E04E67A16 for ; Wed, 10 Sep 2025 03:38:06 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757497084; bh=Wxk8jOac/LtGUXDOf39HMXWGWClFUp47v0BCBaoIISc=; h=From:To:Cc:Subject:Date:From; b=FSqugAOBtZ/HPlPHwnXAUpv3K6BfHxvshrxYXeGUShp3OTweuv2kxjQpx2hX3gakl 4LZpsYBC4PwdEnYu2L1C6QwmFAit8yTAFAd9dQe1k2YIx9Kb+po6rC6svHLElAALah AyOgghnoNSaIaErFdrZgkExKvmRzooq/ibuA5RxmXZjPq2Bjel0JxOee8olMJIaD5n 96kUvngpKZ+gq32T0XVf84OabGqhfKhnWYl4eExGZ4ozJSrDQn83+CL63F8gPwnAiw MHFQuvZLWlAh5u58hHU3HgDxcmIOQIcswrRDGmjGqTwZA9/bja9FWWqkfeo0YnAlXG vV2e9q3rcGP0Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 282D7679E7; Wed, 10 Sep 2025 03:38:04 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id qZjH9mYgiXKb; Wed, 10 Sep 2025 03:38:04 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757497079; bh=9BENUx9w+PC6usNlXPFH5fypemxyW6AI8BttjpPutcc=; h=From:To:Cc:Subject:Date:From; b=pWO5ZsPFk5u14qNVCbVBCFwpM95JBteozXjHV9NQdUQaCE3NNDIw9yFTMs1lSkMOx 73glNepQHOMhSYT+/YZMzDj8PkrSIOjCku4lA4d0wTocPqpJhU4HIMlSX6vq6sBcXR dUchtJm8JJb3yUs1BqPoTl2U8poQr+WCiH/TXFyETb9D5pA5T1xx5eai96zUI2mmOE lNMguor1g/9jxXnV3sfQfhkhXUQJqgTCWiK7iHML0iNoEjIft0appAmfZIV/eszwIP PQsAoTYYkTq7j252i5EQ0wC9BBSkG+Pn/wqXEQMEjvR3BmSAFZok009YTsNlFh7MqQ uxvbXeErfMtOw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id A083F6789C; Wed, 10 Sep 2025 03:37:59 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Wed, 10 Sep 2025 03:37:47 -0600 Message-ID: <20250910093751.4004211-1-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Message-ID-Hash: QQOUM56FFGNEBJFBA4EEW4TZYY4DORCB X-Message-ID-Hash: QQOUM56FFGNEBJFBA4EEW4TZYY4DORCB X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 1/2] ulib: Refactor the Makefile to support multiple programs List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass The current Makefile for the ulib examples is not very easy to modify. Split it into three parts: - config.mk containing the variables used by the build tools - a list of programs and the objects in each one - rules.mk with some rules to make it all work Use sys-objs to indicate objects which should be built with system headers. Use U-Boot headers by default. With this it is fairly simply to add a new program. Of course we could make use of U-Boot's kbuild implementation to tidy this up, but the purpose of the examples is to show how to do things outside the U-Boot build system. Co-developed-by: Claude Co-developed-by: Simon Glass Signed-off-by: Simon Glass --- examples/ulib/Makefile | 72 +++++++---------------------------------- examples/ulib/config.mk | 47 +++++++++++++++++++++++++++ examples/ulib/rules.mk | 39 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 examples/ulib/config.mk create mode 100644 examples/ulib/rules.mk diff --git a/examples/ulib/Makefile b/examples/ulib/Makefile index 81885cf8726..a11c5dad9be 100644 --- a/examples/ulib/Makefile +++ b/examples/ulib/Makefile @@ -25,72 +25,22 @@ # PLATFORM_LIBS - Platform-specific libraries # LIB_STATIC_LDS - Linker script for static library -# For standalone builds, provide default values -EXAMPLE_DIR ?= . -OUTDIR ?= . -CC ?= gcc -SDL_CONFIG ?= sdl2-config -PLATFORM_LIBS ?= $(shell $(SDL_CONFIG) --libs) -LIB_STATIC_LDS ?= static.lds -# The main Makefile passes in Q=@ for quiet output -Q ?= +# Include configuration and helper functions +include config.mk -# Common compiler flags for programs using system headers first -SYSTEM_CFLAGS := -I$(UBOOT_BUILD)/include -idirafter$(srctree)/include \ - -include $(srctree)/include/linux/compiler_attributes.h +# Programs to build +progs := demo -# Common compiler flags for programs using U-Boot headers first (like U-Boot -# internal build) -UBOOT_CFLAGS := -nostdinc -isystem $(shell $(CC) -print-file-name=include) \ - -I$(UBOOT_BUILD)/include \ - -I$(srctree)/include \ - -I$(srctree)/arch/sandbox/include \ - -include $(UBOOT_BUILD)/include/config.h \ - -include $(srctree)/include/linux/kconfig.h \ - -I$(srctree)/dts/upstream/include \ - -D__KERNEL__ -DCONFIG_SYS_TEXT_BASE=0 +# Program definitions - list of object files for each program +demo_objs := demo.o demo_helper.o -SHARED_LDFLAGS := -L$(UBOOT_BUILD) -lu-boot -Wl,-rpath,$(UBOOT_BUILD) +# Objects that need system headers (default is U-Boot headers) +sys-objs := demo_helper.o -STATIC_LDFLAGS := -Wl,-T,$(LIB_STATIC_LDS) \ - -Wl,--whole-archive $(UBOOT_BUILD)/libu-boot.a -Wl,--no-whole-archive \ - -lpthread -ldl $(PLATFORM_LIBS) -Wl,-z,noexecstack - -# Program definitions - can be single file or multi-object -DEMO_SRC := $(EXAMPLE_DIR)/demo.c -demo-objs := demo.o demo_helper.o -DEMO_BINS := $(OUTDIR)/demo $(OUTDIR)/demo_static - -ALL_BINS := $(DEMO_BINS) - -# Default target builds both programs -all: $(ALL_BINS) - -# Create the output directory if it doesn't exist -$(OUTDIR): - @mkdir -p $@ - -# Pattern rule for building object files with system headers first (default) -$(OUTDIR)/%.o: $(EXAMPLE_DIR)/%.c | $(OUTDIR) - $(CC) $(CFLAGS) $(SYSTEM_CFLAGS) -c -o $@ $< - -# Pattern rule for building object files with U-Boot headers first -$(OUTDIR)/%_uboot.o: $(EXAMPLE_DIR)/%.c | $(OUTDIR) - $(CC) $(CFLAGS) $(UBOOT_CFLAGS) -c -o $@ $< - -# The U-Boot library must be built before we can link against it -# Order-only prerequisites ensure libraries exist before linking -$(ALL_BINS): | $(UBOOT_BUILD)/libu-boot.a $(UBOOT_BUILD)/libu-boot.so $(OUTDIR) - -# Build demo (dynamically linked with libu-boot.so) -$(OUTDIR)/demo: $(if $(demo-objs),$(addprefix $(OUTDIR)/,$(demo-objs)),$(DEMO_SRC)) - $(if $(demo-objs),$(CC) $(CFLAGS) -o $@ $^ $(SHARED_LDFLAGS),$(CC) $(CFLAGS) $(SYSTEM_CFLAGS) -o $@ $< $(SHARED_LDFLAGS)) - -# Build demo_static (statically linked with libu-boot.a) -$(OUTDIR)/demo_static: $(if $(demo-objs),$(addprefix $(OUTDIR)/,$(demo-objs)),$(DEMO_SRC)) - $(if $(demo-objs),$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LDFLAGS),$(CC) $(CFLAGS) $(SYSTEM_CFLAGS) -o $@ $< $(STATIC_LDFLAGS)) +# Include build rules (must come after variable definitions) +include rules.mk clean: - $(Q)rm -f $(DEMO_BINS) + $(Q)rm -f $(all_bins) $(OUTDIR)/*.o .PHONY: all clean diff --git a/examples/ulib/config.mk b/examples/ulib/config.mk new file mode 100644 index 00000000000..993f0923b4f --- /dev/null +++ b/examples/ulib/config.mk @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Configuration and helpers for U-Boot library examples +# +# Copyright 2025 Canonical Ltd. +# Written by Simon Glass + +# For standalone builds, provide default values +EXAMPLE_DIR ?= . +OUTDIR ?= . +CC ?= gcc +SDL_CONFIG ?= sdl2-config +PLATFORM_LIBS ?= $(shell $(SDL_CONFIG) --libs) +LIB_STATIC_LDS ?= static.lds +# The main Makefile passes in Q=@ for quiet output +Q ?= + +# Common compiler flags for programs using system headers +SYSTEM_CFLAGS := -I$(UBOOT_BUILD)/include \ + -idirafter$(srctree)/include \ + -include $(srctree)/include/linux/compiler_attributes.h + +# Common compiler flags for programs using U-Boot headers (these match the +# U-Boot internal build) +UBOOT_CFLAGS := -nostdinc \ + -isystem $(shell $(CC) -print-file-name=include) \ + -I$(UBOOT_BUILD)/include \ + -I$(srctree)/include \ + -I$(srctree)/arch/sandbox/include \ + -include $(UBOOT_BUILD)/include/config.h \ + -include $(srctree)/include/linux/kconfig.h \ + -I$(srctree)/dts/upstream/include \ + "-DMBEDTLS_CONFIG_FILE=\"mbedtls_def_config.h\"" \ + -I$(srctree)/lib/mbedtls \ + -I$(srctree)/lib/mbedtls/port \ + -I$(srctree)/lib/mbedtls/external/mbedtls \ + -I$(srctree)/lib/mbedtls/external/mbedtls/include \ + -Wno-builtin-declaration-mismatch \ + -D__KERNEL__ -DCONFIG_SYS_TEXT_BASE=0 + +# Linking flags +SHARED_LDFLAGS := -L$(UBOOT_BUILD) -lu-boot -Wl,-rpath,$(UBOOT_BUILD) + +STATIC_LDFLAGS := -Wl,-T,$(LIB_STATIC_LDS) \ + -Wl,--whole-archive $(UBOOT_BUILD)/libu-boot.a \ + -Wl,--no-whole-archive \ + -lpthread -ldl $(PLATFORM_LIBS) -Wl,-z,noexecstack diff --git a/examples/ulib/rules.mk b/examples/ulib/rules.mk new file mode 100644 index 00000000000..aba6c63d8b6 --- /dev/null +++ b/examples/ulib/rules.mk @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Build rules for U-Boot library examples +# +# Copyright 2025 Canonical Ltd. +# Written by Simon Glass + +# Generate normal and statically linked binary names from progs variable +all_bins := $(foreach prog,$(progs),$(OUTDIR)/$(prog) \ + $(OUTDIR)/$(prog)_static) + +# Default target builds both programs +all: $(all_bins) + +# System headers rule for objects that need system APIs +$(foreach obj,$(sys-objs),$(eval $(OUTDIR)/$(obj): \ + $(EXAMPLE_DIR)/$(obj:.o=.c) | $(OUTDIR) ; \ + $$(CC) $$(CFLAGS) $$(SYSTEM_CFLAGS) -c -o $$@ $$<)) + +# Automatic build rules for all programs +$(foreach prog,$(progs),$(eval $(OUTDIR)/$(prog): \ + $$(addprefix $(OUTDIR)/,$$($(prog)_objs)) ; \ + $$(CC) $$(CFLAGS) -o $$@ $$^ $$(SHARED_LDFLAGS))) +$(foreach prog,$(progs),$(eval $(OUTDIR)/$(prog)_static: \ + $$(addprefix $(OUTDIR)/,$$($(prog)_objs)) ; \ + $$(CC) $$(CFLAGS) -o $$@ $$^ $$(STATIC_LDFLAGS))) + +# Create the output directory if it doesn't exist +$(OUTDIR): + @mkdir -p $@ + +# Default rule: compile with U-Boot headers +$(OUTDIR)/%.o: $(EXAMPLE_DIR)/%.c | $(OUTDIR) + $(CC) $(CFLAGS) $(UBOOT_CFLAGS) -c -o $@ $< + +# The U-Boot library must be built before we can link against it +# Order-only prerequisites ensure libraries exist before linking +$(all_bins): | $(UBOOT_BUILD)/libu-boot.a $(UBOOT_BUILD)/libu-boot.so \ + $(OUTDIR) \ No newline at end of file