Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: #162 : Adding changes for creating weak stubs and linking the same in Makefile #163

Merged
merged 5 commits into from
Dec 31, 2024
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
32 changes: 29 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ else # GTEST case
SRCS := $(shell find $(SRC_DIRS) -type f \( -name '*.cpp' -o -name '*.c' \) | grep -v "$(EXCLUDE_DIRS)")
endif

# Check if BUILD_WEAK_STUBS_SRC is defined
Ulrond marked this conversation as resolved.
Show resolved Hide resolved
ifdef BUILD_WEAK_STUBS_SRC

# Define variables for the static library and its source files
kanjoe24 marked this conversation as resolved.
Show resolved Hide resolved
WEAK_STUBS_LIB := $(LIB_DIR)/libweak_stubs_libs.so
WEAK_STUBS_OUTPUT_DIR ?= $(BUILD_DIR)/weak_stubs/src
WEAK_STUBS_SRC := $(shell find $(BUILD_WEAK_STUBS_SRC) -name *.c)
WEAK_STUBS_OBJ := $(patsubst $(BUILD_WEAK_STUBS_SRC)/%, $(WEAK_STUBS_OUTPUT_DIR)/%, $(WEAK_STUBS_SRC:.c=.o)) # Apply the pattern substitution to create object file paths

XLDFLAGS := $(XLDFLAGS) -L$(LIB_DIR) -lweak_stubs_libs
endif

VARIANT_FILE := .variant

ifeq ($(TARGET),arm)
Expand Down Expand Up @@ -112,7 +124,7 @@ VPATH += $(UT_CORE_DIR) $(TOP_DIR)
# Default target
.PHONY: clean list arm linux framework test createdirs all printenv

all: framework $(OBJS)
all: framework $(OBJS) $(if $(BUILD_WEAK_STUBS_SRC),$(WEAK_STUBS_LIB))

# Build framework
# Recursive make is needed as src files are not available during the first iteration
Expand All @@ -133,7 +145,7 @@ download_and_build:
@${MAKE} -C $(UT_CONTROL) TARGET=${TARGET}

# Build the test binary
test: $(OBJS) createdirs
test: $(OBJS) createdirs $(if $(BUILD_WEAK_STUBS_SRC),$(WEAK_STUBS_LIB))
@${ECHOE} ${GREEN}Linking $@ $(BUILD_DIR)/$(TARGET_EXEC)${NC}
@$(COMPILER) $(OBJS) -o $(BUILD_DIR)/$(TARGET_EXEC) $(XCFLAGS) $(XLDFLAGS)
@cp $(BUILD_DIR)/$(TARGET_EXEC) $(BIN_DIR)/
Expand All @@ -144,7 +156,7 @@ endif
# Create necessary directories
createdirs:
@echo "$(VARIANT)" > $(VARIANT_FILE)
@$(MKDIR_P) ${BIN_DIR} ${LIB_DIR}
@$(MKDIR_P) ${BIN_DIR} ${LIB_DIR} $(if $(BUILD_WEAK_STUBS_SRC),${WEAK_STUBS_OUTPUT_DIR})

# Compilation rules
$(BUILD_DIR)/%.o: %.c
Expand All @@ -171,6 +183,18 @@ checkvariantchange:
fi \
fi

# Create the library weak_stubs_libs
$(WEAK_STUBS_LIB): $(WEAK_STUBS_OBJ)
@${ECHOE} ${GREEN}Building shared library weak_stubs_libs...${NC}
@$(COMPILER) -shared -o $@ $^
@${ECHOE} ${GREEN}Copy shared library weak_stubs_libs to [${BIN_DIR}]${NC}
cp $(WEAK_STUBS_LIB) $(BIN_DIR)

# Rule to compile .c files into .o files in the correct directory
$(WEAK_STUBS_OUTPUT_DIR)/%.o: $(BUILD_WEAK_STUBS_SRC)/%.c
@$(MKDIR_P) $(dir $@)
@$(COMPILER) $(XCFLAGS) -fPIC -c $< -o $@

arm:
make TARGET=arm

Expand Down Expand Up @@ -240,6 +264,8 @@ list:
@${ECHOE}
@${ECHOE} ${YELLOW}INC_FLAGS:${NC} $(INC_FLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}BUILD_WEAK_STUBS_SRC:${NC} $(BUILD_WEAK_STUBS_SRC)
@${ECHOE}
@${ECHOE} ${YELLOW}DEPS:${NC} $(DEPS)
@${ECHOE}
@${ECHOE} --------- ut_control ----------------
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Date (DD/MM/YY) | Comment | Document Version |
|--------|---------|---------|
| 24/12/24 | Updated usage of Weak Library | 2.0.3
| 07/11/24 | Updated How to use Autogenerate script url | 2.0.2|
| 19/06/24 | Extended LD_LIBRARY_PATH | 2.0.1|
| 02/01/24 | Added Release Notes | 2.0.0|
Expand Down Expand Up @@ -216,6 +217,37 @@ This will build the following directories `src/*.c`, in addition to core functio

The final output binary is build as `hal_test` and resides in the `bin` directory, the framework .so files will be copied to the same directory.

### Feature: `BUILD_WEAK_STUBS_SRC` for Weak Library Compilation

The `BUILD_WEAK_STUBS_SRC` variable enables clients to define and export a list of source files that will be compiled into a weak library. This allows testing suites to use stubbed implementations of functions during development, with the strong implementation provided by vendors or third parties at a later stage.

#### Benefits:
1. **Supports Testing Development**: Enables testing suites to run without waiting for strong implementations.
2. **Decouples Development**: Testing can proceed independently of vendor timelines.
3. **Seamless Replacement**: Strong implementations can replace weak stubs without changes to the testing suites.

#### Key Makefile Logic:
```make
WEAK_STUBS_LIB := $(LIB_DIR)/libweak_stubs_libs.so
WEAK_STUBS_OUTPUT_DIR ?= $(BUILD_DIR)/weak_stubs/src
WEAK_STUBS_SRC := $(shell find $(BUILD_WEAK_STUBS_SRC) -name *.c)
WEAK_STUBS_OBJ := $(patsubst $(BUILD_WEAK_STUBS_SRC)/%, $(WEAK_STUBS_OUTPUT_DIR)/%, $(WEAK_STUBS_SRC:.c=.o))
```
- `BUILD_WEAK_STUBS_SRC`: Path to source files for weak stubs.
- `WEAK_STUBS_LIB`: Compiled weak library (`libweak_stubs_libs.so`).
- `WEAK_STUBS_SRC` and `WEAK_STUBS_OBJ`: Dynamically map source to object files.

#### Workflow:
1. **Define Stubs**: Clients export `BUILD_WEAK_STUBS_SRC` with the path to stub source files.
```bash
export BUILD_WEAK_STUBS_SRC=/path/to/stubs
```
2. **Build Library**: The Makefile compiles the stubs into a weak library.
3. **Develop and Test**: Testing suites use the weak library.
4. **Integrate Strong Implementation**: Vendors replace the weak stubs when ready.

This approach ensures smooth testing suite development while awaiting third-party components.

## Running on the target

Copy files from `bin/*` to the target.
Expand Down
2 changes: 2 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ TARGET_EXEC = ut-test

# Switch on Linux & disable asserts for the testing suites
CFLAGS += -DNDEBUG
BUILD_WEAK_STUBS_SRC = $(ROOT_DIR)/src_weak
ifneq ($(VARIANT),CPP)
VARIANT = C
XCFLAGS += -DUT_CUNIT
Expand Down Expand Up @@ -55,6 +56,7 @@ export TARGET
export TARGET_EXEC
export CFLAGS
export LDFLAGS
export BUILD_WEAK_STUBS_SRC

GREEN='\033[0;32m'
NC='\033[0m'
Expand Down
Loading