-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.cpphash
150 lines (109 loc) · 4.96 KB
/
Makefile.cpphash
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
# Directory of this Makefile
MY_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
# This Makefile is from https://github.com/maartenSXM/cpphash
# Copy it to any project directory to enable source files with hash-style
# comments in that directory to be processed by the C pre-processor.
# It can be run using make -f Makefile.cpphash. Alternatively, it can
# be renamed Makefile to avoid having to specify the the -f option.
# There are some pre-defined C pre-processor defines that your
# esphome yaml can use with #ifdef etc. See CH_EXTRA_DEFS below.
# Refer to https://github.com/maartenSXM/cpphash/blob/main/README.md
# for more details.
# To setup cpphash for one project, clone it into the that project
# directory and install it with:
# cpphash/install.sh -y -s
# To share cpphash across multiple esphome project directories, clone
# it in a common location and install it from that directory with
# ./install.sh -y -s
# Then, in each each project directory, copy Makefile.cpphash there and
# individually set CH_HOME to the common location where the shared
# cpphash is installed by uncommenting the following line:
# CH_HOME:=/my/shared/cpphash
CH_HOME ?= ./cpphash
ifeq (,$(wildcard $(CH_HOME)/install.sh))
$(info $(CH_HOME) not installed. To install it, do this:)
$(info git clone https://github.com/maartenSXM/cpphash.git)
$(info ./cpphash/install.sh -y)
$(info source cpphash/Bashrc)
$(info and try again)
$(error Or, alternatively, set a CH_HOME environment or make variable)
endif
ifeq (,$(VIRTUAL_ENV))
$(error $$VIRTUAL_ENV not set. Did you source Bashrc?)
endif
MAKECMDGOALS ?= all
MAKE := $(MAKE) --no-print-directory
MAKEFILE := $(lastword $(MAKEFILE_LIST))
include $(CH_HOME)/make/prjsave.mk
MY_PRJ_PATH = $(PRJ)
MY_PRJ_DIR = $(patsubst %/,%,$(dir $(PRJ)))
MY_PROJECT = $(basename $(notdir $(MY_PRJ_PATH)))
MY_GEN_FILES ?= $(PRJ)
CH_BUILD ?= build
ifeq (,$(wildcard $(CH_BUILD)/$(MY_PROJECT)))
$(shell mkdir -p $(CH_BUILD)/$(MY_PROJECT))
endif
# CH_BUILD_DIR is where projects are built. If changed, review
# the definition of MY_TOP below. It declares the relative path to
# the esphome project directory from CH_BUILD_DIR. This is done manually
# because realpath does not support the --relative-to option on many
# non-Linux OSs.
CH_BUILD_DIR = $(CH_BUILD)/$(MY_PROJECT)
include $(CH_HOME)/make/log.mk
# CH_GEN is the set of files that cpphash runs the C preprocessor on.
# They can include files from CH_SRCS (defined below) since the cpphash
# tool arranges that de-commented copies are included, not the originals.
CH_GEN ?= $(MY_GEN_FILES)
# Use this to list subdirectories to #include yaml files from.
MY_SRC_DIRS ?=
# CH_SRCS is the set of files that cpphash will remove hash-style
# comments from while leaving any C preprocessor directives so that
# the file can subsequently be used as a #include by one of the
# CH_GEN files.
# Builds the list of CH_SRCS by looking for .yaml files in $(MY_DIRS).
CH_SRCS += $(sort $(foreach d,$(MY_SRC_DIRS),$(wildcard $(d)/*.yaml)) $(CH_GEN))
# In addition to updates to $(CH_SRCS) triggering a rebuild of espmake.yaml,
# updates to source files in $(MY_DEPS) are also triggers.
MY_DEP_DIRS ?=
ESP_DEPS += $(foreach d,$(MY_DEP_DIRSS),$(wildcard $(d)/*.c) \
$(wildcard $(d)/*.cpp) $(wildcard $(d)/*.h))
# If there is a secrets.h file in ./ or ../, use it
ifneq (,$(wildcard ./secrets.h))
CH_EXTRA_FLAGS += -include ./secrets.h
else
ifneq (,$(wildcard ../secrets.h))
CH_EXTRA_FLAGS += -include ../secrets.h
endif
endif
# Use this to list additional #include directories
# Allow #include from all source directories. Note these
# directories come after -I $(CH_BUILD_DIR)/dehashed so
# includes of yaml files are taken from there.
CH_EXTRA_INCS += $(foreach d,$(MY_DIRS),-I $(d))
# These #defines are for project adapation.
# MY_TOP is how to get from the build directory back to
# the esphome project directory. It is relative from CH_BUILD_DIR.
CH_EXTRA_DEFS += -D MY_TOP=../.. \
-D MY_BUILD_PATH=$(CH_BUILD_DIR) \
-D MY_PRJ_DIR=$(MY_PRJ_DIR) \
-D MY_PROJECT_NAME=$(MY_PROJECT) \
-D MY_PROJECT_$(MY_PROJECT) \
-D MY_USER_NAME=$(USER) \
-D MY_USER_$(USER)
# This includes the cpphash Makefile fragment that will dehash yamls files.
# In turn, it will include cpphash/esphome.mk which handles the esphome
# file generation and platformio build steps.
include $(CH_HOME)/make/cpphash.mk
print-config:: $(MY_INIT)
@printf "Makefile variables as processed by cpphash:\n"
@printf " MY_PROJECT: $(MY_PROJECT)\n"
@printf " CH_GEN: $(CH_GEN)\n"
@printf " CH_SRCS:\n"
@$(foreach f,$(CH_INFILES),printf " $(f)\n";)
@printf " MY_DEPS:\n"
@$(foreach f,$(MY_DEPS),printf " $(f)\n";)
@printf "Makefile #defines available to yaml files:"
@printf " $(subst -, ,$(subst -D,#define,$(CH_EXTRA_DEFS)))\n" | sed -e 's/ #/\n #/g' -e 's/=/ /g'
@printf "For #defaults available to yaml files, "
@printf "use: make print-defaults\n"
.PHONY: print-config