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: gh #154 : Upgrading autogenerate script to generate weak stubs #159

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 33 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TOP_DIR ?= $(UT_CORE_DIR)
BIN_DIR ?= $(TOP_DIR)/build/bin
LIB_DIR ?= $(TOP_DIR)/build/$(TARGET)/lib
BUILD_DIR ?= $(TOP_DIR)/build/$(TARGET)/obj
LIBRARY_DIR = $(LIB_DIR)
kanjoe24 marked this conversation as resolved.
Show resolved Hide resolved

# Non-Moveable Directories
FRAMEWORK_DIR = $(UT_CORE_DIR)/framework
Expand Down Expand Up @@ -94,6 +95,12 @@ OBJS := $(filter %.o, $(sort $(OBJS))) # Filter and sort the object files and re
# Dependency files
DEPS := $(OBJS:.o=.d)

# Add the library type (static or shared)
LIBRARY_TYPE ?= shared # Default to shared, change to static to build a static library

# Object files for the library
LIBRARY_OBJS = $(subst $(TOP_DIR),$(BUILD_DIR),$(LIBRARY_SRCS:.c=.o))

# Final flags
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
VERSION := $(shell git describe --tags | head -n1)
Expand All @@ -112,7 +119,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 $(LIBRARY_NAME),$(LIBRARY_DIR)/$(LIBRARY_NAME)$(LIBRARY_EXTENSION))

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

# Build the test binary
test: $(OBJS) createdirs
test: $(OBJS) createdirs $(if $(LIBRARY_NAME),$(LIBRARY_DIR)/$(LIBRARY_NAME)$(LIBRARY_EXTENSION))
@${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 Down Expand Up @@ -171,6 +178,22 @@ checkvariantchange:
fi \
fi

# Create the library (shared or static)
$(LIBRARY_DIR)/$(LIBRARY_NAME)$(LIBRARY_EXTENSION): $(LIBRARY_OBJS) createdirs
@if [ "${LIBRARY_TYPE}" = "shared" ]; then \
${ECHOE} ${GREEN}Building shared library $(LIBRARY_NAME)$(LIBRARY_EXTENSION)${NC}; \
$(CC) $(LIBRARY_OBJS) $(LIBRARY_FLAGS) -o $@; \
else \
${ECHOE} ${GREEN}Building static library $(LIBRARY_NAME)$(LIBRARY_EXTENSION)${NC}; \
$(AR) rcs $@ $(LIBRARY_OBJS); \
fi

# Create object files for the library
$(LIBRARY_DIR)/%.o: $(SRC_DIR)/%.c
@${ECHOE} ${GREEN}Building [${YELLOW}$<${GREEN}] for library${NC}
@$(MKDIR_P) $(dir $@)
@$(CC) $(XCFLAGS) $(CFLAGS) -I$(LIBRARY_INC) -c $< -o $@

arm:
make TARGET=arm

Expand Down Expand Up @@ -240,6 +263,14 @@ list:
@${ECHOE}
@${ECHOE} ${YELLOW}INC_FLAGS:${NC} $(INC_FLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}LIBRARY_DIR:${NC} $(LIBRARY_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}LIBRARY_NAME:${NC} $(LIBRARY_NAME)
@${ECHOE}
@${ECHOE} ${YELLOW}LIBRARY_EXTENSION:${NC} $(LIBRARY_EXTENSION)
@${ECHOE}
@${ECHOE} ${YELLOW}LIBRARY_TYPE:${NC} $(LIBRARY_TYPE)
@${ECHOE}
@${ECHOE} ${YELLOW}DEPS:${NC} $(DEPS)
@${ECHOE}
@${ECHOE} --------- ut_control ----------------
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