From patchwork Tue Sep 9 15:18:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 275 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=1757431177; bh=AcDt4rkP+Hnz0Nv7L/njD3o4WVMiJ/GCWUbshssbWFk=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=kVaceos2JoDy1V333PtsEQAuBlAQM+iSLEeDdSbzYQPdA62byknv15AD5SQc+XrfQ 0h6G9BNDLIlXzlrHMQgKVKZcyZt/3nFQYDvBbHAQazVAu+TBKjHV1DyshqVMUjTtrV F+JO8fJ4t+hb77qOLBbjesO4sAtqHDK8+wIzg++/+FM1tqeIWn73NvxuiL27SxTkFZ m9e4Cljsost99G6Y1ktx4j0Fhi7q+D9sNsueBCL7AIcxN7cSKTvQuCl/h6SBWGhoPS /SOpQJXRV5he9jqJobMtBBBFURo154Qlhzcq6FAVXoef+7BQWkKuOp8sg8OC3YNYIf CIc+6ADaSCmEA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id ECEEC67A80 for ; Tue, 9 Sep 2025 09:19:37 -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 7vARkBjdOCN1 for ; Tue, 9 Sep 2025 09:19:37 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757431177; bh=AcDt4rkP+Hnz0Nv7L/njD3o4WVMiJ/GCWUbshssbWFk=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=kVaceos2JoDy1V333PtsEQAuBlAQM+iSLEeDdSbzYQPdA62byknv15AD5SQc+XrfQ 0h6G9BNDLIlXzlrHMQgKVKZcyZt/3nFQYDvBbHAQazVAu+TBKjHV1DyshqVMUjTtrV F+JO8fJ4t+hb77qOLBbjesO4sAtqHDK8+wIzg++/+FM1tqeIWn73NvxuiL27SxTkFZ m9e4Cljsost99G6Y1ktx4j0Fhi7q+D9sNsueBCL7AIcxN7cSKTvQuCl/h6SBWGhoPS /SOpQJXRV5he9jqJobMtBBBFURo154Qlhzcq6FAVXoef+7BQWkKuOp8sg8OC3YNYIf CIc+6ADaSCmEA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DA9B967A7C for ; Tue, 9 Sep 2025 09:19:37 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757431176; bh=OJ/Gl9PCzxZGKQKTMO4j8BU1QC2xaPHAp05Y2UHEmzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DdZ22sjWH8vlIBA7ZdEYryW2hketwyHb/MBAP9wcfwOPYDeYZWYmJMfKQh7RhBuEX U/tOsv3FGKthJBI1tZhJOcwTqf+w9PwL5cRoZRPsfRZK2r42Oa1D6GCOA8gdGsR8vw xTONZufaXzBBPlQnVJOlTKAaf938VSSWsKfw3YT8xfsr1FYkqmG16erbhu23jKKYMv gzZLaVCQW1jvMYiRWfpovMYXk0TV09zMTbohDat6Jdp6RT4AnVqOeDHWRkvR4M3oHy UN09RhL3JNtNF42vWS0s8Hl3ihXh5pnyXdePX6n23TNGfE8q3+86QeFqKXVBdTEima ZzKiM/xp/77QQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 20BD660026; Tue, 9 Sep 2025 09:19:36 -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 wbgfzAzc9BiB; Tue, 9 Sep 2025 09:19:36 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1757431171; bh=p67Uwe+0jy2lbNVP/CjUYhemFwHtuGXXt8g6/2/Hi7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LMW/KH2Ng+mRElzdrJNr1fHiBDiG70bTY67ZtXZgJjuG4vxW0FW8QHrCm5HRglwYk IavUS6F3A+UHc+P4oxd62teXsexoL9k4R0teRmW5J0dnw9QuYQIwjCjit18f8IEWDC qXJd/g/ttH7dRZN63CB5vmresNU+ZQrgzxOAMMrMmcVqbeEjIRDU+5RC/9btFBUAiR NuE03o4D6CdK6Q4btqUoZ4M9+S2DvlJ7ZguX/dHY9iKaBIBmV+4TXIMXoPEiYYNLHq 8R/TghGnwDQxD7qt9nseaqgWI4VlyIlKPAio9RGfDfPzIuKs9790jD8OjOQlUWmExz eKz337tr8uigQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DC2CE67A7E; Tue, 9 Sep 2025 09:19:30 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Tue, 9 Sep 2025 09:18:12 -0600 Message-ID: <20250909151824.2327219-16-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250909151824.2327219-1-sjg@u-boot.org> References: <20250909151824.2327219-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: AV2TPUUXUJII4Z2OCDRDKLEYZH7VBG7Z X-Message-ID-Hash: AV2TPUUXUJII4Z2OCDRDKLEYZH7VBG7Z 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: Heinrich Schuchardt , Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 15/18] ulib: Expand the ulib example to have two files 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 It is possible to have some files that build in the system environment and some in the U-Boot environment. To build for the system, the system headers must be used, or at least have priority. For a U-Boot file, its headers must be used exclusively. Expand the Makefile example to have two files, one of which is built using U-Boot headers. This shows how a program can be built which straddles both domains. Signed-off-by: Simon Glass --- examples/ulib/Makefile | 58 ++++++++++++++++++++++++++----------- examples/ulib/demo.c | 12 ++++++-- examples/ulib/demo_helper.c | 30 +++++++++++++++++++ examples/ulib/demo_helper.h | 31 ++++++++++++++++++++ 4 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 examples/ulib/demo_helper.c create mode 100644 examples/ulib/demo_helper.h diff --git a/examples/ulib/Makefile b/examples/ulib/Makefile index f70333a3b9b..81885cf8726 100644 --- a/examples/ulib/Makefile +++ b/examples/ulib/Makefile @@ -35,36 +35,60 @@ LIB_STATIC_LDS ?= static.lds # The main Makefile passes in Q=@ for quiet output Q ?= +# 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 + +# 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 + +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 + +# 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: $(DEMO_BINS) +all: $(ALL_BINS) # Create the output directory if it doesn't exist $(OUTDIR): @mkdir -p $@ -# The U-Boot library must be built before we can link against it. This is -# signalled by the presence of the $(UBOOT_BUILD)/examples/ulib directory. -# This is an order-only prerequisite, so it does not trigger a rebuild if the -# timestamp of the directory changes. -$(DEMO_BINS): | $(UBOOT_BUILD)/examples/ulib $(OUTDIR) +# 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: $(DEMO_SRC) - $(CC) $(CFLAGS) \ - -idirafter$(srctree)/include -o $@ $< \ - -L$(UBOOT_BUILD) -lu-boot \ - -Wl,-rpath,$(UBOOT_BUILD) +$(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: $(DEMO_SRC) - $(CC) $(CFLAGS) \ - -idirafter$(srctree)/include -o $@ $< \ - -Wl,-T,$(LIB_STATIC_LDS) \ - -Wl,--whole-archive $(UBOOT_BUILD)/libu-boot.a -Wl,--no-whole-archive \ - -lpthread -ldl $(PLATFORM_LIBS) -Wl,-z,noexecstack +$(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)) clean: $(Q)rm -f $(DEMO_BINS) diff --git a/examples/ulib/demo.c b/examples/ulib/demo.c index 2a9f7720beb..9d916d3878a 100644 --- a/examples/ulib/demo.c +++ b/examples/ulib/demo.c @@ -17,10 +17,11 @@ #include #include #include +#include "demo_helper.h" int main(int argc, char *argv[]) { - int fd, lines = 0; + int fd, result, lines = 0; char line[256]; /* Init U-Boot library */ @@ -29,8 +30,7 @@ int main(int argc, char *argv[]) return 1; } - printf("1U-Boot Library Demo\n"); - printf("================================\n"); + demo_show_banner(); printf("U-Boot version: %s\n", version_string); printf("\n"); @@ -54,6 +54,12 @@ int main(int argc, char *argv[]) printf("\nRead %d line(s) using U-Boot library functions.\n", lines); + /* Test the helper function */ + result = demo_add_numbers(42, 13); + printf("Helper function result: %d\n", result); + + demo_show_footer(); + /* Clean up */ ulib_uninit(); diff --git a/examples/ulib/demo_helper.c b/examples/ulib/demo_helper.c new file mode 100644 index 00000000000..935443657bd --- /dev/null +++ b/examples/ulib/demo_helper.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Helper functions for U-Boot library demo + * + * Copyright 2025 Canonical Ltd. + * Written by Simon Glass + */ + +#include + +void demo_show_banner(void) +{ + ub_printf("=================================\n"); + ub_printf(" U-Boot Library Demo Helper\n"); + ub_printf("=================================\n"); +} + +void demo_show_footer(void) +{ + ub_printf("=================================\n"); + ub_printf(" Demo Complete!\n"); + ub_printf("=================================\n"); +} + +int demo_add_numbers(int a, int b) +{ + ub_printf("Helper: Adding %d + %d = %d\n", a, b, a + b); + + return a + b; +} diff --git a/examples/ulib/demo_helper.h b/examples/ulib/demo_helper.h new file mode 100644 index 00000000000..b4ab862941b --- /dev/null +++ b/examples/ulib/demo_helper.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Helper functions for U-Boot library demo + * + * Copyright 2025 Canonical Ltd. + * Written by Simon Glass + */ + +#ifndef __DEMO_HELPER_H +#define __DEMO_HELPER_H + +/** + * demo_show_banner() - Show the demo banner + */ +void demo_show_banner(void); + +/** + * demo_show_footer() - Show the demo footer + */ +void demo_show_footer(void); + +/** + * demo_add_numbers() - Add two numbers and print the result + * + * @a: First number + * @b: Second number + * Return: Sum of the two numbers + */ +int demo_add_numbers(int a, int b); + +#endif /* __DEMO_HELPER_H */