<commit_before># esp-open-rtos common Makefile
#
# To use this Makefile, define the variable TARGET with the program
# name (plus any other variables you want to override) in your
# progream's makefile, then include common.mk as the last line
# in the Makefile.
#
# See the programs in the 'examples' directory for examples.
#
# Note: your program does not have to be under the top esp-open-rtos
# directory - you can build out of tree programs.

# This makefile is adapted from the esp-mqtt makefile by @tuanpmt
# https://github.com/tuanpmt/esp_mqtt, but it has change significantly
# since then.

# To override variables assigned below with ?= for the local system,
# rather than per-project, add the entries to this optional local.mk
# file, or specify on the command line, or pass as environment vars.
-include local.mk

# esptool defaults
ESPTOOL ?= esptool.py
ESPBAUD ?= 115200

ifndef TARGET
	$(error "Set the TARGET environment variable in your Makefile before including common.mk"
endif

# Output directors to store intermediate compiled files
# relative to the target directory
BUILD_DIR ?= $(TARGET_DIR)build/
FW_BASE ?= $(TARGET_DIR)firmware/

# we create two different files for uploading into the flash
# these are the names and options to generate them
FW_1	= 0x00000
FW_2	= 0x40000

FLAVOR ?= release # or debug

# Compiler names, etc. assume gdb
ESPPORT ?= /dev/ttyUSB0
CROSS ?= xtensa-lx106-elf-

AR = $(CROSS)ar
CC = $(CROSS)gcc
LD = $(CROSS)gcc
NM = $(CROSS)nm
CPP = $(CROSS)g++
SIZE = $(CROSS)size
OBJCOPY = $(CROSS)objcopy

# Source components to compile and link. Each of these are subdirectories
# of the root, with a 'component.mk' file.
COMPONENTS     ?= FreeRTOS lwip axtls

# binary esp-iot-rtos SDK libraries to link. These are pre-processed prior to linking.
SDK_LIBS		?= main net80211 phy pp wpa

# open source libraries linked in
LIBS ?= gcc hal

ENTRY_SYMBOL ?= sdk_call_user_start

CFLAGS		= -Wall -Werror -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -std=gnu99
LDFLAGS		= -nostdlib -Wl,--no-check-sections -Wl,-L$(BUILD_DIR)sdklib -Wl,-L$(ROOT)lib -u $(ENTRY_SYMBOL) -Wl,-static -Wl,-Map=build/${TARGET}.map

ifeq ($(FLAVOR),debug)
    CFLAGS += -g -O0
    LDFLAGS += -g -O0
else
    CFLAGS += -g -O2 -DGITSHORTREV=$(GITSHORTREV)
    LDFLAGS += -g -O2
endif

LINKER_SCRIPTS  = ld/eagle.app.v6.ld ld/eagle.rom.addr.v6.ld

####
#### no user configurable options below here
####

# hacky way to get a single space value
empty :=
space := $(empty) $(empty)

# assume the target_dir is the directory the top-level makefile was run in
TARGET_DIR := $(dir $(firstword $(MAKEFILE_LIST)))

# assume the 'root' directory (ie top of the tree) is the directory common.mk is in
ROOT := $(dir $(lastword $(MAKEFILE_LIST)))

# derive various parts of compiler/linker arguments
SDK_LIB_ARGS         = $(addprefix -l,$(SDK_LIBS))
LIB_ARGS             = $(addprefix -l,$(LIBS))
TARGET_OUT   = $(BUILD_DIR)$(TARGET).out
LDFLAGS      += $(addprefix -T$(ROOT),$(LINKER_SCRIPTS))
FW_FILE_1    = $(addprefix $(FW_BASE)/,$(FW_1).bin)
FW_FILE_2    = $(addprefix $(FW_BASE)/,$(FW_2).bin)

# Common include directories, shared across all "components"
# components will add their include directories to this argument
#
# Placing $(TARGET_DIR) and $(TARGET_DIR)include first allows
# targets to have their own copies of header config files for components
# , which is useful for overriding things.
INC_DIRS      = $(TARGET_DIR) $(TARGET_DIR)include $(ROOT)include

ifeq ("$(V)","1")
Q :=
vecho := @true
else
Q := @
vecho := @echo
endif

.PHONY: all clean debug_print

all: $(TARGET_OUT) $(FW_FILE_1) $(FW_FILE_2)

# component_compile_rules: Produces compilation rules for a given
# component
#
# Call arguments are:
# $(1) - component name
#
# Expects that the following component-specific variables are defined:
#
# $(1)_ROOT    = Top-level dir containing component. Can be in-tree or out-of-tree.
# $(1)_SRC_DIR = List of source directories for the component. All must be under $(1)_ROOT
# $(1)_INC_DIR = List of include directories specific for the component
#
# Optional variables:
# $(1)_CFLAGS  = CFLAGS to override the default CFLAGS for this component only.
#
# Each call appends to COMPONENT_ARS which is a list of archive files for compiled components
COMPONENT_ARS =
define component_compile_rules
$(1)_OBJ_DIR   = $(BUILD_DIR)$(1)/
### determine source files and object files ###
$(1)_SRC_FILES = $$(foreach sdir,$$($(1)_SRC_DIR),$$(realpath $$(wildcard $$(sdir)/*.c)))
$(1)_REAL_ROOT = $$(realpath $$($(1)_ROOT))
# patsubst here substitutes real paths for the relative OBJ_DIR path, making things short again
$(1)_OBJ_FILES = $$(patsubst $$($(1)_REAL_ROOT)%.c,$$($(1)_OBJ_DIR)%.o,$$($(1)_SRC_FILES))

### determine compiler arguments ###
$(1)_CFLAGS ?= $(CFLAGS)
$(1)_CC_ARGS = $(Q) $(CC) $(addprefix -I,$(INC_DIRS)) $$(addprefix -I,$$($(1)_INC_DIR)) $$($(1)_CFLAGS)
$(1)_AR = $(BUILD_DIR)$(1).a

$$($(1)_OBJ_DIR)%.o: $$($(1)_REAL_ROOT)%.c
	$(vecho) "CC $$<"
	$(Q) mkdir -p $$(dir $$@)
	$$($(1)_CC_ARGS) -c $$< -o $$@
	$$($(1)_CC_ARGS) -MM -MT $$@ -MF $$(@:.o=.d) $$<
	$(Q) $(OBJCOPY) --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal $$@

$$($(1)_AR): $$($(1)_OBJ_FILES)
	$(vecho) "AR $$@"
	$(Q) $(AR) cru $$@ $$^

COMPONENT_ARS += $$($(1)_AR)

-include $$($(1)_OBJ_FILES:.o=.d)
endef

GITSHORTREV=\"$(shell cd $(ROOT); git rev-parse --short -q HEAD)\"

## Linking rules for SDK libraries
## SDK libraries are preprocessed to:
# - prefix all defined symbols with 'sdk_'
# - weaken all global symbols so they can be overriden from the open SDK side

# SDK binary libraries are preprocessed into build/lib<commit_msg>Fixup git revision in axTLS output<commit_after># esp-open-rtos common Makefile
#
# To use this Makefile, define the variable TARGET with the program
# name (plus any other variables you want to override) in your
# progream's makefile, then include common.mk as the last line
# in the Makefile.
#
# See the programs in the 'examples' directory for examples.
#
# Note: your program does not have to be under the top esp-open-rtos
# directory - you can build out of tree programs.

# This makefile is adapted from the esp-mqtt makefile by @tuanpmt
# https://github.com/tuanpmt/esp_mqtt, but it has change significantly
# since then.

# To override variables assigned below with ?= for the local system,
# rather than per-project, add the entries to this optional local.mk
# file, or specify on the command line, or pass as environment vars.
-include local.mk

# esptool defaults
ESPTOOL ?= esptool.py
ESPBAUD ?= 115200

ifndef TARGET
	$(error "Set the TARGET environment variable in your Makefile before including common.mk"
endif

# Output directors to store intermediate compiled files
# relative to the target directory
BUILD_DIR ?= $(TARGET_DIR)build/
FW_BASE ?= $(TARGET_DIR)firmware/

# we create two different files for uploading into the flash
# these are the names and options to generate them
FW_1	= 0x00000
FW_2	= 0x40000

FLAVOR ?= release # or debug

# Compiler names, etc. assume gdb
ESPPORT ?= /dev/ttyUSB0
CROSS ?= xtensa-lx106-elf-

AR = $(CROSS)ar
CC = $(CROSS)gcc
LD = $(CROSS)gcc
NM = $(CROSS)nm
CPP = $(CROSS)g++
SIZE = $(CROSS)size
OBJCOPY = $(CROSS)objcopy

# Source components to compile and link. Each of these are subdirectories
# of the root, with a 'component.mk' file.
COMPONENTS     ?= FreeRTOS lwip axtls

# binary esp-iot-rtos SDK libraries to link. These are pre-processed prior to linking.
SDK_LIBS		?= main net80211 phy pp wpa

# open source libraries linked in
LIBS ?= gcc hal

ENTRY_SYMBOL ?= sdk_call_user_start

CFLAGS		= -Wall -Werror -Wl,-EL -nostdlib -mlongcalls -mtext-section-literals -std=gnu99
LDFLAGS		= -nostdlib -Wl,--no-check-sections -Wl,-L$(BUILD_DIR)sdklib -Wl,-L$(ROOT)lib -u $(ENTRY_SYMBOL) -Wl,-static -Wl,-Map=build/${TARGET}.map

ifeq ($(FLAVOR),debug)
    CFLAGS += -g -O0
    LDFLAGS += -g -O0
else
    CFLAGS += -g -O2
    LDFLAGS += -g -O2
endif

GITSHORTREV=\"$(shell cd $(ROOT); git rev-parse --short -q HEAD)\"
CFLAGS += -DGITSHORTREV=$(GITSHORTREV)

LINKER_SCRIPTS  = ld/eagle.app.v6.ld ld/eagle.rom.addr.v6.ld

####
#### no user configurable options below here
####

# hacky way to get a single space value
empty :=
space := $(empty) $(empty)

# assume the target_dir is the directory the top-level makefile was run in
TARGET_DIR := $(dir $(firstword $(MAKEFILE_LIST)))

# assume the 'root' directory (ie top of the tree) is the directory common.mk is in
ROOT := $(dir $(lastword $(MAKEFILE_LIST)))

# derive various parts of compiler/linker arguments
SDK_LIB_ARGS         = $(addprefix -l,$(SDK_LIBS))
LIB_ARGS             = $(addprefix -l,$(LIBS))
TARGET_OUT   = $(BUILD_DIR)$(TARGET).out
LDFLAGS      += $(addprefix -T$(ROOT),$(LINKER_SCRIPTS))
FW_FILE_1    = $(addprefix $(FW_BASE)/,$(FW_1).bin)
FW_FILE_2    = $(addprefix $(FW_BASE)/,$(FW_2).bin)

# Common include directories, shared across all "components"
# components will add their include directories to this argument
#
# Placing $(TARGET_DIR) and $(TARGET_DIR)include first allows
# targets to have their own copies of header config files for components
# , which is useful for overriding things.
INC_DIRS      = $(TARGET_DIR) $(TARGET_DIR)include $(ROOT)include

ifeq ("$(V)","1")
Q :=
vecho := @true
else
Q := @
vecho := @echo
endif

.PHONY: all clean debug_print

all: $(TARGET_OUT) $(FW_FILE_1) $(FW_FILE_2)

# component_compile_rules: Produces compilation rules for a given
# component
#
# Call arguments are:
# $(1) - component name
#
# Expects that the following component-specific variables are defined:
#
# $(1)_ROOT    = Top-level dir containing component. Can be in-tree or out-of-tree.
# $(1)_SRC_DIR = List of source directories for the component. All must be under $(1)_ROOT
# $(1)_INC_DIR = List of include directories specific for the component
#
# Optional variables:
# $(1)_CFLAGS  = CFLAGS to override the default CFLAGS for this component only.
#
# Each call appends to COMPONENT_ARS which is a list of archive files for compiled components
COMPONENT_ARS =
define component_compile_rules
$(1)_OBJ_DIR   = $(BUILD_DIR)$(1)/
### determine source files and object files ###
$(1)_SRC_FILES = $$(foreach sdir,$$($(1)_SRC_DIR),$$(realpath $$(wildcard $$(sdir)/*.c)))
$(1)_REAL_ROOT = $$(realpath $$($(1)_ROOT))
# patsubst here substitutes real paths for the relative OBJ_DIR path, making things short again
$(1)_OBJ_FILES = $$(patsubst $$($(1)_REAL_ROOT)%.c,$$($(1)_OBJ_DIR)%.o,$$($(1)_SRC_FILES))

### determine compiler arguments ###
$(1)_CFLAGS ?= $(CFLAGS)
$(1)_CC_ARGS = $(Q) $(CC) $(addprefix -I,$(INC_DIRS)) $$(addprefix -I,$$($(1)_INC_DIR)) $$($(1)_CFLAGS)
$(1)_AR = $(BUILD_DIR)$(1).a

$$($(1)_OBJ_DIR)%.o: $$($(1)_REAL_ROOT)%.c
	$(vecho) "CC $$<"
	$(Q) mkdir -p $$(dir $$@)
	$$($(1)_CC_ARGS) -c $$< -o $$@
	$$($(1)_CC_ARGS) -MM -MT $$@ -MF $$(@:.o=.d) $$<
	$(Q) $(OBJCOPY) --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal $$@

$$($(1)_AR): $$($(1)_OBJ_FILES)
	$(vecho) "AR $$@"
	$(Q) $(AR) cru $$@ $$^

COMPONENT_ARS += $$($(1)_AR)

-include $$($(1)_OBJ_FILES:.o=.d)
endef

## Linking rules for SDK libraries
## SDK libraries are preprocessed to:
# - prefix all defined symbols with 'sdk_'
# - weaken all global symbols so they can be overriden from the open SDK side

# SDK binary libraries are preprocessed into build/lib
{'fraction_non_alphanumeric': 0.1451059713305778, 'fraction_numerical': 0.013367337964998682, 'mean_word_length': 4.451581975071908, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2420058', 'n_tokens_mistral': 4088, 'n_tokens_neox': 3865, 'n_words': 1291}