-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
executable file
·274 lines (235 loc) · 9.57 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# *
# * 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.
# *
# Terminal colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
ECHOE = /bin/echo -e
MKDIR_P ?= @mkdir -p
# Directories and PATH setup
UT_CORE_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
export PATH := $(UT_CORE_DIR)/toolchain:$(PATH)
# Set target based on TARGET variable
TARGET := $(if $(TARGET),$(TARGET),linux)
export TARGET
# Moveable Directories based on the caller makefile
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
# Non-Moveable Directories
FRAMEWORK_DIR = $(UT_CORE_DIR)/framework
UT_CONTROL = $(FRAMEWORK_DIR)/ut-control
# Flags
XCFLAGS := $(KCFLAGS)
INC_DIRS += $(UT_CORE_DIR)/include $(UT_CONTROL)/include $(UT_CORE_DIR)/src
TARGET_EXEC ?= hal_test
# Set up compiler and source directories based on VARIANT
ifneq ($(VARIANT),CPP) # CUNIT case
VARIANT = C
# Enable CUnit Requirements
XCFLAGS += -DUT_CUNIT
CUNIT_DIR = $(FRAMEWORK_DIR)/CUnit-2.1-3/CUnit
INC_DIRS += $(CUNIT_DIR)/Headers $(UT_CORE_DIR)/src/c_source
SRC_DIRS += $(CUNIT_DIR)/Sources/Framework $(UT_CORE_DIR)/src
XLDFLAGS += $(YLDFLAGS) $(LDFLAGS) -L$(UT_CONTROL)/build/$(TARGET)/lib -lut_control -Wl,-rpath, -pthread -lpthread
# Source files
SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s)
else # GTEST case
VARIANT = CPP
SRC_DIRS += $(UT_CORE_DIR)/src
EXCLUDE_DIRS = $(SRCDIR)/c_source
GTEST_SRC = $(FRAMEWORK_DIR)/gtest/$(TARGET)/googletest-1.15.2
INC_DIRS += $(GTEST_SRC)/googletest/include $(UT_CORE_DIR)/src/cpp_source $(UT_CORE_DIR)/src
TEST_LIB_DIR = $(UT_CORE_DIR)/build/$(TARGET)/cpp_libs/lib/
XLDFLAGS += $(YLDFLAGS) $(LDFLAGS) -L$(UT_CONTROL)/build/$(TARGET)/lib -L$(TEST_LIB_DIR) -lgtest_main -lgtest -lut_control -lpthread
# Source files
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)
COMPILER := $(if $(filter CPP,$(VARIANT)),$(CXX),$(CC))
#CC := arm-rdk-linux-gnueabi-gcc -mthumb -mfpu=vfp -mcpu=cortex-a9 -mfloat-abi=soft -mabi=aapcs-linux -mno-thumb-interwork -ffixed-r8 -fomit-frame-pointer
# CFLAGS will be overriden by Caller as required
INC_DIRS += $(UT_CORE_DIR)/sysroot/usr/include
else
COMPILER := $(if $(filter CPP,$(VARIANT)),g++ -ggdb -o0 -Wall, gcc -ggdb -o0 -Wall)
endif
# Common object file setup
OBJS = $(SRCS:.c=.o) $(SRCS:.cpp=.o)
OBJS := $(patsubst $(TOP_DIR)/%, $(BUILD_DIR)/%, $(OBJS)) # Prefix the object files with the build directory using pattern substitution
OBJS := $(filter %.o, $(sort $(OBJS))) # Filter and sort the object files and remove duplicates
# Dependency files
DEPS := $(OBJS:.o=.d)
# Final flags
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
VERSION := $(shell git describe --tags | head -n1)
XCFLAGS += $(CFLAGS) $(INC_FLAGS) -D UT_VERSION=\"$(VERSION)\"
CXXFLAGS += $(INC_FLAGS)
# Initial output
$(info $(shell ${ECHOE} ${GREEN}TARGET_EXEC [$(TARGET_EXEC)]${NC}))
$(info TARGET [$(TARGET)])
$(info COMPILER [$(COMPILER)])
$(info $(shell ${ECHOE} ${GREEN}VARIANT [$(VARIANT)]${NC}))
# Search for prerequisites (like .c, .cpp, or .h files) in specified directories when those files are not in the current directory.
VPATH += $(UT_CORE_DIR) $(TOP_DIR)
# Default target
.PHONY: clean list arm linux framework test createdirs all printenv
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
framework: checkvariantchange createdirs download_and_build
@${ECHOE} ${GREEN}Framework downloaded and built${NC}
@make test VARIANT=${VARIANT}
@cp -r $(UT_CONTROL)/build/$(TARGET)/lib/libut_control.* $(LIB_DIR) $(BIN_DIR)
@${ECHOE} ${GREEN}ut-control LIB Copied to [${BIN_DIR}]${NC}
@if [ -d "$(TOP_DIR)/../include" ] && [ -d "$(BUILD_DIR)/src" ]; then \
${UT_CORE_DIR}/compare-functions-in-headers-testsuite.sh $(TOP_DIR)/../include $(BIN_DIR)/${TARGET_EXEC} ${TARGET}; \
fi
download_and_build:
@${ECHOE} ${GREEN}Ensure ut-core frameworks are present${NC}
@${UT_CORE_DIR}/build.sh TARGET=${TARGET} VARIANT=${VARIANT}
@${ECHOE} ${GREEN}Completed${NC}
@${ECHOE} ${GREEN}Entering ut-control [TARGET=${TARGET}]${NC}
@${MAKE} -C $(UT_CONTROL) TARGET=${TARGET}
# Build the test binary
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)/
ifneq ("$(wildcard $(HAL_LIB_DIR)/*.so)","")
cp $(HAL_LIB_DIR)/*.so* $(BIN_DIR)
endif
# Create necessary directories
createdirs:
@echo "$(VARIANT)" > $(VARIANT_FILE)
@$(MKDIR_P) ${BIN_DIR} ${LIB_DIR} $(if $(BUILD_WEAK_STUBS_SRC),${WEAK_STUBS_OUTPUT_DIR})
# Compilation rules
$(BUILD_DIR)/%.o: %.c
@${ECHOE} ${GREEN}Building [${YELLOW}$<${GREEN}]${NC}
@$(MKDIR_P) $(dir $@)
@$(COMPILER) $(XCFLAGS) -c $< -o $@
$(BUILD_DIR)/%.o: %.cpp
@${ECHOE} ${GREEN}Building [${YELLOW}$<${GREEN}]${NC}
@$(MKDIR_P) $(dir $@)
@$(COMPILER) $(CXXFLAGS) -c $< -o $@
# Rule to check if the VARIANT has changed
checkvariantchange:
@if [ -f "$(VARIANT_FILE)" ]; then \
PREVIOUS_VARIANT=$$(cat $(VARIANT_FILE)); \
if [ "$$PREVIOUS_VARIANT" != "$(VARIANT)" ]; then \
${ECHOE} ${RED}********************************************************************${NC}; \
${ECHOE} ${RED}Error: VARIANT has changed from $$PREVIOUS_VARIANT to $(VARIANT)${NC}; \
${ECHOE} ${RED}Please ${YELLOW} make clean TARGET=${TARGET}\; make -C tests\/ clean TARGET=${TARGET} ${NC}; \
${ECHOE} ${RED}the build and try again.${NC}; \
${ECHOE} ${RED}********************************************************************${NC}; \
exit 1; \
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
linux: framework
make TARGET=linux
# Clean targets
clean:
@${ECHOE} ${GREEN}Performing Clean for $(TARGET) ${BUILD_DIR} ${NC}
@$(RM) -rf $(BUILD_DIR)
@${ECHOE} ${GREEN}Performing Clean for $(TARGET) $(VARIANT_FILE) ${NC}
@$(RM) -rf $(VARIANT_FILE)
@${ECHOE} ${GREEN}Performing Clean for $(TARGET) ${LIB_DIR} ${NC}
@${RM} -rf ${LIB_DIR}
@${ECHOE} ${GREEN}Clean Completed${NC}
cleanall: clean
@${ECHOE} ${GREEN}Performing Clean on [$(UT_CORE_DIR)/framework]${NC}
@$(RM) -rf $(UT_CORE_DIR)/framework
@${ECHOE} ${GREEN}Performing Clean on [$(UT_CORE_DIR)/build]${NC}
@$(RM) -rf $(UT_CORE_DIR)/build/
@${ECHOE} ${GREEN}Performing Clean on [$(TOP_DIR)/build/]${NC}
@$(RM) -rf $(TOP_DIR)/build/
@${ECHOE} ${GREEN}Clean Completed${NC}
printenv:
@${ECHOE} "Environment variables: [ut-core]"
@${ECHOE} "---------------------------"
@$(foreach v, $(.VARIABLES), $(info $(v) = $($(v))))
@${ECHOE} "---------------------------"
# List target
list:
@${ECHOE} --------- ut_core ----------------
@${ECHOE}
@${ECHOE} ${YELLOW}CC:${NC} $(CC)
@${ECHOE}
@${ECHOE} COMPILER:$(COMPILER)
@${ECHOE}
@${ECHOE} ${YELLOW}UT_CORE_DIR:${NC} $(UT_CORE_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}TOP_DIR:${NC} $(TOP_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}BIN_DIR:${NC} $(BIN_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}LIB_DIR:${NC} $(LIB_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}BUILD_DIR:${NC} $(BUILD_DIR)
@${ECHOE}
@${ECHOE} ${YELLOW}CFLAGS:${NC} $(CFLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}XCFLAGS:${NC} $(XCFLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}LDFLAGS:${NC} $(LDFLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}YLDFLAGS:${NC} $(YLDFLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}XLDFLAGS:${NC} $(XLDFLAGS)
@${ECHOE}
@${ECHOE} ${YELLOW}OBJS:${NC} $(OBJS)
@${ECHOE}
@${ECHOE} ${YELLOW}SRCS:${NC} $(SRCS)
@${ECHOE}
@${ECHOE} ${YELLOW}SRC_DIRS:${NC} $(SRC_DIRS)
@${ECHOE}
@${ECHOE} ${YELLOW}INC_DIRS:${NC} $(INC_DIRS)
@${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 ----------------
@${MAKE} -C $(UT_CONTROL) list
-include $(DEPS)