Makefile

The Makefile for this part of the project looks like this:

GPP = g++
GCC = gcc
MAKEDIR = mkdir -p
RM = rm
AR = ar
ROOT = test/node
OUTPUT = bmac_esp8266
OUTLIB = lib$(OUTPUT).a
INCLUDE = -I $(ROOT)/
-I $(ROOT)/SmingCore/
-I $(ROOT)/SmingCore/network
-I $(ROOT)/SmingCore/network/Http
-I $(ROOT)/SmingCore/network/Http/Websocket
-I $(ROOT)/SmingCore/network/libmosquitto
-I $(ROOT)/SmingCore/network/libmosquitto/cpp
-I $(ROOT)/SmingCore/wiring
-I $(ROOT)/Libraries/BME280
-I $(ROOT)/esp8266/app
FLAGS := $(INCLUDE) -g3 -U__STRICT_ANSI__
LIB := -L$(ROOT)/lib -l$(OUTPUT) -lmosquittopp -lmosquitto -lnymphrpc
-lPocoNet -lPocoUtil -lPocoFoundation -lPocoJSON -lstdc++fs
-lssl -lcrypto
LIB_WIN := -lws2_32
ifeq ($(OS),Windows_NT)
LIB := $(LIB) $(LIB_WIN)
endif
include ./esp8266/version
include ./Makefile-user.mk
CPPFLAGS := $(FLAGS) -DVERSION=""$(VERSION)"" $(USER_CFLAGS) -std=c++17 -Wl,--gc-sections
CFLAGS := -g3
CPP_SOURCES := $(wildcard $(ROOT)/SmingCore/*.cpp)
$(wildcard $(ROOT)/SmingCore/network/*.cpp)
$(wildcard $(ROOT)/SmingCore/network/Http/*.cpp)
$(wildcard $(ROOT)/SmingCore/wiring/*.cpp)
$(wildcard $(ROOT)/Libraries/BME280/*.cpp)
FW_SOURCES := $(wildcard esp8266/app/*.cpp)
CPP_OBJECTS := $(addprefix $(ROOT)/obj/,$(notdir) $(CPP_SOURCES:.cpp=.o))
FW_OBJECTS := $(addprefix $(ROOT)/obj/,$(notdir) $(FW_SOURCES:.cpp=.o))
all: makedir $(FW_OBJECTS) $(CPP_OBJECTS) $(ROOT)/lib/$(OUTLIB) $(ROOT)/bin/$(OUTPUT)
$(ROOT)/obj/%.o: %.cpp
$(GPP) -c -o $@ $< $(CPPFLAGS)
$(ROOT)/obj/%.o: %.c
$(GCC) -c -o $@ $< $(CFLAGS)
$(ROOT)/lib/$(OUTLIB): $(CPP_OBJECTS)
-rm -f $@
$(AR) rcs $@ $^
$(ROOT)/bin/$(OUTPUT):
-rm -f $@
$(GPP) -o $@ $(CPPFLAGS) $(FW_SOURCES) $(LIB)
makedir:
$(MAKEDIR) $(ROOT)/bin
$(MAKEDIR) $(ROOT)/lib
$(MAKEDIR) $(ROOT)/obj
$(MAKEDIR) $(ROOT)/obj/$(ROOT)/SmingCore/network
$(MAKEDIR) $(ROOT)/obj/$(ROOT)/SmingCore/wiring
$(MAKEDIR) $(ROOT)/obj/$(ROOT)/Libraries/BME280
$(MAKEDIR) $(ROOT)/obj/esp8266/app
clean:
$(RM) $(CPP_OBJECTS) $(FW_OBJECTS)

The main thing to note about this Makefile is that it gathers source files from two different source folders, both for the test API and for the firmware source. The former source files are first compiled to object files, which are assembled into an archive. The firmware source is used directly along with this test framework library, though we also have the firmware object files available if we need them.

The reason for creating an archive of the test API before linking it has to do with the way that the linker finds symbols. By using the AR tool, it will create an index of all symbols in the object files inside the archive, ensuring that we will not get any linker errors. Especially for large projects this is often a requirement to have the object files successfully link into a binary.

Compiling to object files first is also helpful with larger projects, as Make will ensure that only files that have actually changed will be recompiled, which can really speed up development time. Since the target firmware source for this project is fairly minimal, we can compile directly from the source files here.

We also include two more Makefiles from this one. The first includes the version number of the firmware source we are compiling with, which is useful since it'll ensure that the produced node binary will report the exact same version as the version installed on an ESP8266 module would. This making validation of a specific firmware version much easier.

The second is the Makefile with user-definable settings, copied verbatim from the firmware project Makefile, but with just the variables we need for the firmware source to compile and work, as shown in the following code:

WIFI_SSID = MyWi-FiNetwork
WIFI_PWD = MyWi-FiPassword

MQTT_HOST = localhost
# For SSL support, uncomment the following line or compile with this parameter.
#ENABLE_SSL=1
# MQTT SSL port (for example):
ifdef ENABLE_SSL
MQTT_PORT = 8883
else
MQTT_PORT = 1883
endif

# Uncomment if password authentication is used.
# USE_MQTT_PASSWORD=1
# MQTT username & password (if needed):
# MQTT_USERNAME = esp8266
# MQTT_PWD = ESPassword

# MQTT topic prefix: added to all MQTT subscriptions and publications.
# Can be left empty, but must be defined.
# If not left empty, should end with a '/' to avoid merging with topic names.
MQTT_PREFIX =

# OTA (update) URL. Only change the host name (and port).
OTA_URL = http://ota.host.net/ota.php?uid=

USER_CFLAGS := $(USER_CFLAGS) -DWIFI_SSID=""$(WIFI_SSID)""
USER_CFLAGS := $(USER_CFLAGS) -DWIFI_PWD=""$(WIFI_PWD)""
USER_CFLAGS := $(USER_CFLAGS) -DMQTT_HOST=""$(MQTT_HOST)""
USER_CFLAGS := $(USER_CFLAGS) -DMQTT_PORT="$(MQTT_PORT)"
USER_CFLAGS := $(USER_CFLAGS) -DMQTT_USERNAME=""$(MQTT_USERNAME)""
USER_CFLAGS := $(USER_CFLAGS) -DOTA_URL=""$(OTA_URL)""
USER_CFLAGS := $(USER_CFLAGS) -DMQTT_PWD=""$(MQTT_PWD)""
ifdef USE_MQTT_PASSWORD
USER_CFLAGS := $(USER_CFLAGS) -DUSE_MQTT_PASSWORD=""$(USE_MQTT_PASSWORD)""
endif
SER_CFLAGS := $(USER_CFLAGS) -DMQTT_PREFIX=""$(MQTT_PREFIX)""

Including this Makefile sets all of these defines to be passed to the compiler. These are all preprocessor statements that are used to set strings or to change which parts of the code will be compiled, such as the SSL code.

However, for simplicity's sake, we aren't implementing SSL functionality for this example project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.5.68