Skip to content

Commit

Permalink
Merge branch 'release/4.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kanjoe24 committed Dec 31, 2024
2 parents 4a0b1e1 + 7ec7896 commit 34eb2a2
Show file tree
Hide file tree
Showing 28 changed files with 1,495 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"cwd": "${workspaceFolder}/tests/build/bin/",
"program": "${workspaceFolder}/tests/build/bin/ut-test",
"environment": [ {"name": "LD_LIBRARY_PATH", "value":"${workspaceFolder}/tests/build/bin/"} ],
"args": [ "-l", "${workspaceFolder}/tests/logs/", "-p", "${workspaceFolder}/tests/build/bin/assets/test_kvp.yaml"],
"args": [ "-l", "${workspaceFolder}/tests/logs/", "-p", "${workspaceFolder}/tests/build/bin/assets/test_kvp.yaml", "-p", "${workspaceFolder}/tests/build/bin/assets/config-test.yaml"],
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [4.4.0](https://github.com/rdkcentral/ut-core/compare/4.3.0...4.4.0)

- gh #162 : Adding changes for creating weak stubs and linking the same in Makefile [`#163`](https://github.com/rdkcentral/ut-core/pull/163)
- gh #78 : Adding tests in ut-core to support multiple inputs [`#158`](https://github.com/rdkcentral/ut-core/pull/158)
- gh #152 : Adding a demo directory to demo usage of weak, strong and dlopen [`#153`](https://github.com/rdkcentral/ut-core/pull/153)
- gh #154 : Upgrading auto-generate script to generate weak stubs [`#159`](https://github.com/rdkcentral/ut-core/pull/159)
- Add gh #152 : Adding headers to all files and 2 more use cases [`0397422`](https://github.com/rdkcentral/ut-core/commit/039742226fae8d783640ff2b2972a94f33f53279)
- Add gh #152 : Adding demo directory demo_dlopen_based [`f339bfb`](https://github.com/rdkcentral/ut-core/commit/f339bfbb3a7c7f17b1b497d10431599269349254)
- Update gh #162 : Updating the weak stub lib to shared libraray [`cecb8f4`](https://github.com/rdkcentral/ut-core/commit/cecb8f4c056649b005cb8002946cf2b7cf72803b)

#### [4.3.0](https://github.com/rdkcentral/ut-core/compare/4.2.0...4.3.0)

> 5 December 2024
- gh#149 : Adding changes for menu driven gtest tests [`#150`](https://github.com/rdkcentral/ut-core/pull/150)
- Add gh#149 : Adding dynamic menus to gtest tests as in CUNIT counterpart [`66e0d37`](https://github.com/rdkcentral/ut-core/commit/66e0d377538a323c3a4d1a80a520c4dbc88e8cab)
- Add gh#149 : Adding not implemented message for the options [`433c118`](https://github.com/rdkcentral/ut-core/commit/433c118c0eabdec0209daa2fbd95625dd68fbe13)
Expand Down
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
ifdef BUILD_WEAK_STUBS_SRC

# Define variables for the static library and its source files
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: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ popd > /dev/null # ${MY_DIR}
# Therefore in that case it warns you but doesnt' chnage to that version, which could cause your tests to break.
# Change this to upgrade your ut-control Major versions. Non ABI Changes 1.x.x are supported, between major revisions

UT_CONTROL_PROJECT_VERSION="1.6.1" # Fixed version
UT_CONTROL_PROJECT_VERSION="1.6.2" # Fixed version

# Clone the Unit Test Requirements
[email protected]:rdkcentral/ut-control.git
Expand Down
24 changes: 24 additions & 0 deletions scripts/autogenerate_skeletons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ function AGT_generate_skeletons()
mv *.c src/
fi

# Process all .c files in the directory to be weak functions
for file in src/*.c;
do
# Skip if no .c files are found
[ -e "$file" ] || continue

temp_file="${file}.tmp"

# Process the file to add __attribute__((weak)) to function definitions
awk '
{
# Match function definitions: return type, function name, and arguments
if ($0 ~ /^[a-zA-Z_][a-zA-Z0-9_]*[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\(/) {
print "__attribute__((weak)) " $0;
} else {
print $0;
}
}' "$file" > "$temp_file"

# Replace the original file with the updated file
mv "$temp_file" "$file"
AGT_SUCCESS "$file updated with weak attribute"
done

AGT_SUCCESS "Skeletons are generated successfully"
cd - &> /dev/null

Expand Down
25 changes: 23 additions & 2 deletions scripts/autogenerate_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ function AGT_add_common_to_tests_file()
AGT_add_headers_to_files "${1}.c" true
}

# Function to add headers to the start of the skeleton src
function AGT_add_common_to_skeleton_src_file()
{
# Change to the target directory, exit on failure
cd "${AGT_SKELETONS_SRC}" || {
return 1
}

COPYRGT_MESSAGE=`cat ${AGT_COPYRGT_TEMPLATE}`

# Process all .c files in the directory
for filename in *.c; do
# Ensure the file exists (in case no .c files are found)
[ -e "$filename" ] || continue

# Call the helper functions with the actual filename
eval "echo -e \"${COPYRGT_MESSAGE}\\n\"" | cat - "$filename" > temp && mv temp "$filename"
done
}

# Function to make a copy of the config template and update it for each test
# @param [in] Keyname to be changed
# @param [in] Value to be changed into
Expand Down Expand Up @@ -348,8 +368,8 @@ function AGT_generate_l1_l2_tests()
continue
fi

# Get the list of functions and copy into another temp file
cat ${AGT_TEMP_FUNC_DEF_FILE} | cut -d"(" -f1 | cut -d" " -f2 > ${AGT_TEMP_FUNC_NAMES_FILE}
# Get the list of functions and copy into another temp file making sure the attribute is removed
cat ${AGT_TEMP_FUNC_DEF_FILE} | grep -oP '\w+\s\w+(?=\()' | awk '{print $2}' > ${AGT_TEMP_FUNC_NAMES_FILE}

# Set up the L1 filename
new_L1_filename="test_l1_`echo $filename| cut -d"." -f1`"
Expand Down Expand Up @@ -418,3 +438,4 @@ AGT_delete_and_create_ut_src
AGT_add_main_file
AGT_generate_l1_l2_tests
AGT_add_test_register_file
AGT_add_common_to_skeleton_src_file
4 changes: 3 additions & 1 deletion 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 All @@ -78,7 +80,7 @@ framework:
make -C ../ framework TARGET=$(TARGET) VARIANT=${VARIANT}
@${ECHOE} ${GREEN}Copy Assets to [${BIN_DIR}/assets] ${NC}
@mkdir -p ${BIN_DIR}/assets
@cp ${ROOT_DIR}/src/assets/* ${BIN_DIR}/assets
@cp -r ${ROOT_DIR}/src/assets/* ${BIN_DIR}/assets
@cp ${ROOT_DIR}/src/*.sh ${BIN_DIR}/.
@${ECHOE} ${GREEN}Build Complete${NC}

Expand Down
50 changes: 50 additions & 0 deletions tests/demo/demo_dlopen_based/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# *
# * 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.
# *

# Makefile for demonstrating weak vs strong symbols using dlopen

ifeq ($(TARGET),)
CC = gcc
else
#CC = rm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer
endif
CFLAGS = -Wall -fPIC
LDFLAGS = -ldl

# Targets
EXECUTABLE = main
SHARED_LIB = libplugin.so

# Source files
MAIN_SRC = main.c
PLUGIN_SRC = plugin.c

.PHONY: all clean

all: $(EXECUTABLE) $(SHARED_LIB)

$(EXECUTABLE): $(MAIN_SRC)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

$(SHARED_LIB): $(PLUGIN_SRC)
$(CC) $(CFLAGS) -shared -o $@ $^

clean:
rm -f $(EXECUTABLE) $(SHARED_LIB)

83 changes: 83 additions & 0 deletions tests/demo/demo_dlopen_based/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Plugin Loader Project using dlopen

## Overview
This project demonstrates how to create a simple C program that dynamically loads a shared library (plugin) and executes its functions. The project consists of :
- a main program (`main.c`) that loads the plugin at runtime
- and interacts with it through an interface defined in `plugin.h`.
- The plugin itself is implemented in `plugin.c`.

## Prerequisites

To build and run this project, you need:

- A C compiler (e.g., `gcc`)
- The `dl` library for dynamic linking (commonly available on Linux)
- GNU `make`

## Compilation Instructions

1. Build the project for the native architecture:
```bash
make
```

2. Build the project for an ARM target:
```bash
make TARGET=arm
```
Ensure you have the appropriate cross-compiler toolchain configured for ARM in the `Makefile`.

3. Clean up the generated files:
```bash
make clean
```

## Usage

The program dynamically loads the shared library and attempts to use the strong implementations. If a strong implementation is not found, the weak implementation is used instead.

1. Example Output for linux
```bash
./main
Plugin initialized.
Plugin action performed.
Plugin cleaned up.
```

2. Example Output for arm
```bash
root@xione-uk:/opt/jyo# ./main
Plugin initialized.
Plugin action performed.
Plugin cleaned up.
```

3. Example when plugin not found:
```bash
Plugin not found. Running without plugin.
```

## Project Details

**main.c:**

This file contains the main program that dynamically loads the libplugin.so shared library and uses the PluginInterface to call the plugin’s functions.

**plugin.c:**

This file defines the implementation of the functions specified in the PluginInterface. The functions include plugin_initialize(), plugin_perform_action(), and plugin_cleanup().

**plugin.h:**

The header file that defines the PluginInterface structure, which declares the function pointers used by the plugin.


## Dependencies

- GCC for compilation.
- `libdl` (part of the standard library) for dynamic linking.

## Notes

- Ensure the shared library (`libplugin.so`) is located in the same directory as the `main` executable, or adjust the `LD_LIBRARY_PATH` environment variable to include its location.
- This example is designed for educational purposes and demonstrates basic concepts of dynamic linking and symbol resolution in C.
Loading

0 comments on commit 34eb2a2

Please sign in to comment.