Skip to content

Commit

Permalink
Update gh #154 :Updating makefile to provide flexibility to users to …
Browse files Browse the repository at this point in the history
…build libs
  • Loading branch information
kanjoe24 committed Dec 13, 2024
1 parent 588fb63 commit 6d2d7e1
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 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)

# Non-Moveable Directories
FRAMEWORK_DIR = $(UT_CORE_DIR)/framework
Expand Down Expand Up @@ -94,6 +95,13 @@ 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 = $(patsubst $(SRC_DIR)/%.c,$(LIBRARY_DIR)/%.o,$(LIBRARY_SRCS)) //working
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 +120,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 +141,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 +179,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 +264,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

0 comments on commit 6d2d7e1

Please sign in to comment.