summaryrefslogtreecommitdiff
path: root/kconfig/kconfig.mk
blob: 2be71410b7aa7daac6b7d908c3f82be8e63cfff1 (plain)
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
275
276
277
278
279
280
281
282
# ===========================================================================
# crosstool-NG configuration targets
# These targets are used from top-level makefile

# Derive the project version from, well, the project version:
export PROJECTVERSION=$(CT_VERSION)

#-----------------------------------------------------------
# Some static /configuration/

KCONFIG_TOP = config/config.in
obj = kconfig
PHONY += clean help oldconfig menuconfig defoldconfig

# Darwin (MacOS-X) does not have proper libintl support
ifeq ($(shell uname -s),Darwin)
KBUILD_NO_NLS:=1
endif

ifneq ($(KBUILD_NO_NLS),)
CFLAGS += -DKBUILD_NO_NLS
endif

#-----------------------------------------------------------
# List all config files, source and generated

# Build the list of all source config files
STATIC_CONFIG_FILES = $(patsubst $(CT_LIB_DIR)/%,%,$(shell find $(CT_LIB_DIR)/config -type f -name '*.in' 2>/dev/null))
# ... and how to access them:
$(STATIC_CONFIG_FILES): config

# Build a list of per-component-type source config files
ARCH_CONFIG_FILES   = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/arch/*.in))
KERNEL_CONFIG_FILES = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/kernel/*.in))
CC_CONFIG_FILES     = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/cc/*.in))
LIBC_CONFIG_FILES   = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/libc/*.in))
DEBUG_CONFIG_FILES  = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/debug/*.in))
TOOL_CONFIG_FILES   = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/config/tools/*.in))

# Build the list of generated config files
GEN_CONFIG_FILES = config.gen/arch.in     \
                   config.gen/kernel.in   \
                   config.gen/cc.in       \
                   config.gen/libc.in     \
                   config.gen/tools.in    \
                   config.gen/debug.in
# ... and how to access them:
# Generated files depends on kconfig.mk (this file) because it has the
# functions needed to build the genrated files, and thus they might
# need re-generation if kconfig.mk changes
$(GEN_CONFIG_FILES): config.gen                         \
                     $(CT_LIB_DIR)/kconfig/kconfig.mk

# KCONFIG_TOP should already be in STATIC_CONFIG_FILES, anyway...
CONFIG_FILES = $(sort $(KCONFIG_TOP) $(STATIC_CONFIG_FILES) $(GEN_CONFIG_FILES))

# Where to access to the source config files from
config:
	@$(ECHO) "  LN    config"
	$(SILENT)ln -s $(CT_LIB_DIR)/config config

# Where to store the generated config files into
config.gen:
	@$(ECHO) "  MKDIR config.gen"
	$(SILENT)mkdir -p config.gen

#-----------------------------------------------------------
# Build list of per-component-type items to easily build generated files

ARCHS   = $(patsubst config/arch/%.in,%,$(ARCH_CONFIG_FILES))
KERNELS = $(patsubst config/kernel/%.in,%,$(KERNEL_CONFIG_FILES))
CCS     = $(patsubst config/cc/%.in,%,$(CC_CONFIG_FILES))
LIBCS   = $(patsubst config/libc/%.in,%,$(LIBC_CONFIG_FILES))
DEBUGS  = $(patsubst config/debug/%.in,%,$(DEBUG_CONFIG_FILES))
TOOLS   = $(patsubst config/tools/%.in,%,$(TOOL_CONFIG_FILES))

#-----------------------------------------------------------
# Helper functions to ease building generated config files

# The function 'build_gen_choice_in' builds a choice-menu of a list of
# components in the given list, also adding source-ing of associazted
# config files:
# $1 : destination file
# $2 : name for the entries family (eg. Architecture, kernel...)
# $3 : prefix for the choice entries (eg. ARCH, KERNEL...)
# $4 : base directory containing config files
# $5 : list of config entries (eg. for architectures: "alpha arm ia64"...,
#      and for kernels: "bare-metal linux"...)
# Example to build the kernels generated config file:
# $(call build_gen_choice_in,config.gen/kernel.in,Target OS,KERNEL,config/kernel,$(KERNELS))
define build_gen_choice_in
	@$(ECHO) '  IN    $(1)'
	$(SILENT)(echo "# $(2) menu";                                       \
	  echo "# Generated file, do not edit!!!";                          \
	  echo "";                                                          \
	  echo "choice";                                                    \
	  echo "    bool";                                                  \
	  echo "    prompt \"$(2)\"";                                       \
	  echo "";                                                          \
	  for entry in $(5); do                                             \
	    file="$(4)/$${entry}.in";                                       \
	    _entry=$$(echo "$${entry}" |sed -r -s -e 's/[-.+]/_/g;');       \
	    echo "config $(3)_$${_entry}";                                  \
	    echo "    bool";                                                \
	    printf "    prompt \"$${entry}";                                \
	    if grep -E '^# +EXPERIMENTAL$$' $${file} >/dev/null 2>&1; then  \
	      echo " (EXPERIMENTAL)\"";                                     \
	      echo "    depends on EXPERIMENTAL";                           \
	    else                                                            \
	      echo "\"";                                                    \
	    fi;                                                             \
	  done;                                                             \
	  echo "";                                                          \
	  echo "endchoice";                                                 \
	  for entry in $(5); do                                             \
	    file="$(4)/$${entry}.in";                                       \
	    _entry=$$(echo "$${entry}" |sed -r -s -e 's/[-.+]/_/g;');       \
	    echo "";                                                        \
	    echo "if $(3)_$${_entry}";                                      \
	    echo "config $(3)";                                             \
	    echo "    default \"$${entry}\" if $(3)_$${_entry}";            \
	    echo "source $${file}";                                         \
	    echo "endif";                                                   \
	  done;                                                             \
	 ) >$(1)
endef

# The function 'build_gen_menu_in' builds a menuconfig for each component in
# the given list, source-ing the associated files conditionnaly:
# $1 : destination file
# $2 : name of entries family (eg. Tools, Debug...)
# $3 : prefix for the menu entries (eg. TOOL, DEBUG)
# $4 : base directory containing config files
# $5 : list of config entries (eg. for tools: "libelf sstrip"..., and for
#      debug: "dmalloc duma gdb"...)
# Example to build the tools generated config file:
# $(call build_gen_menu_in,config.gen/tools.in,Tools,TOOL,config/tools,$(TOOLS))
define build_gen_menu_in
	@$(ECHO) '  IN    $(1)'
	$(SILENT)(echo "# $(2) facilities menu";                            \
	  echo "# Generated file, do not edit!!!";                          \
	  echo "";                                                          \
	  for entry in $(5); do                                             \
	    file="$(4)/$${entry}.in";                                       \
	    _entry=$$(echo "$${entry}" |sed -r -s -e 's/[-.+]/_/g;');       \
	    echo "menuconfig $(3)_$${_entry}";                              \
	    echo "    bool";                                                \
	    printf "    prompt \"$${entry}";                                \
	    if grep -E '^# +EXPERIMENTAL$$' $${file} >/dev/null 2>&1; then  \
	      echo " (EXPERIMENTAL)\"";                                     \
	      echo "    depends on EXPERIMENTAL";                           \
	    else                                                            \
	      echo "\"";                                                    \
	    fi;                                                             \
	    echo "if $(3)_$${_entry}";                                      \
	    echo "source $${file}";                                         \
	    echo "endif";                                                   \
	    echo "";                                                        \
	  done;                                                             \
	 ) >$(1)
endef

#-----------------------------------------------------------
# The rules for the generated config files

config.gen/arch.in: $(ARCH_CONFIG_FILES)
	$(call build_gen_choice_in,$@,Target Architecture,ARCH,config/arch,$(ARCHS))

config.gen/kernel.in: $(KERNEL_CONFIG_FILES)
	$(call build_gen_choice_in,$@,Target OS,KERNEL,config/kernel,$(KERNELS))

config.gen/cc.in: $(CC_CONFIG_FILES)
	$(call build_gen_choice_in,$@,C compiler,CC,config/cc,$(CCS))

config.gen/libc.in: $(LIBC_CONFIG_FILES)
	$(call build_gen_choice_in,$@,C library,LIBC,config/libc,$(LIBCS))

config.gen/tools.in: $(TOOL_CONFIG_FILES)
	$(call build_gen_menu_in,$@,Tools,TOOL,config/tools,$(TOOLS))

config.gen/debug.in: $(DEBUG_CONFIG_FILES)
	$(call build_gen_menu_in,$@,Debug,DEBUG,config/debug,$(DEBUGS))

#-----------------------------------------------------------
# The configurators rules

menuconfig: $(obj)/mconf $(CONFIG_FILES)
	$(SILENT)$< $(KCONFIG_TOP)

oldconfig: .config $(obj)/conf $(CONFIG_FILES)
	$(SILENT)$< -s $(KCONFIG_TOP)

defoldconfig: .config $(obj)/conf $(CONFIG_FILES)
	$(SILENT)yes "" |$< -s $(KCONFIG_TOP)

#-----------------------------------------------------------
# Help text used by make help

help-config::
	@echo  '  menuconfig         - Update current config using a menu based program'
	@echo  '  oldconfig          - Update current config using a provided .config as base'
	@echo  '  defoldconfig       - As oldconfig, above, but using defaults for new options'

#-----------------------------------------------------------
# Hmmm! Cheesy build!
# Or: where I can unveil my make-fu... :-]

# Oh! Files not here are there, in fact! :-)
vpath %.c $(CT_LIB_DIR)
vpath %.h $(CT_LIB_DIR)

# What is the compiler?
HOST_CC ?= gcc -funsigned-char

# Compiler and linker flags to use ncurses
CFLAGS += $(shell $(CT_LIB_DIR)/kconfig/lxdialog/check-lxdialog.sh -ccflags)
LDFLAGS += $(shell $(CT_LIB_DIR)/kconfig/lxdialog/check-lxdialog.sh -ldflags $(HOST_CC))

# Common source files, and lxdialog source files
SRC = kconfig/zconf.tab.c
LXSRC = $(patsubst $(CT_LIB_DIR)/%,%,$(wildcard $(CT_LIB_DIR)/kconfig/lxdialog/*.c))

# What's needed to build 'conf'
conf_SRC  = $(SRC) kconfig/conf.c
conf_OBJ  = $(patsubst %.c,%.o,$(conf_SRC))

# What's needed to build 'mconf'
mconf_SRC = $(SRC) $(LXSRC) kconfig/mconf.c
mconf_OBJ = $(patsubst %.c,%.o,$(mconf_SRC))

# Cheesy auto-dependencies
DEPS = $(patsubst %.c,%.d,$(sort $(conf_SRC) $(mconf_SRC)))

# This is not very nice, as they will get rebuild even if (dist)cleaning... :-(
# Should look into the Linux kernel Kbuild to see how they do that...
# To really make me look into this, keep the annoying "DEP xxx" messages.
# Also see the comment for the "%.o: %c" rule below
%.d: %.c $(CT_LIB_DIR)/kconfig/kconfig.mk
	$(SILENT)if [ ! -d $(obj)/lxdialog ]; then  \
	   $(ECHO) "  MKDIR $(obj)";           \
	   mkdir -p $(obj)/lxdialog;        \
	 fi
	@$(ECHO) "  DEP   $@"
	$(SILENT)$(HOST_CC) $(CFLAGS) -MM $< |sed -r -e 's|([^:]+.o)( *:+)|$(<:.c=.o) $@\2|;' >$@
-include $(DEPS)

# Each .o must depend on the corresponding .c (obvious, isn't it?),
# but *can not* depend on kconfig/, because kconfig can be touched
# during the build (who's touching it, btw?) so each .o would be
# re-built when they sould not be.
# So manually check for presence of $(obj) (ie. kconfig), and only mkdir
# if needed. After all, that's not so bad...
# mkdir $(obj)/lxdialog, because we need it, and incidentally, that
# also creates $(obj).
# Also rebuild the object files is the makefile is changed
%.o: %.c $(CT_LIB_DIR)/kconfig/kconfig.mk
	$(SILENT)if [ ! -d $(obj)/lxdialog ]; then  \
	   $(ECHO) "  MKDIR $(obj)";           \
	   mkdir -p $(obj)/lxdialog;        \
	 fi
	@$(ECHO) "  CC    $@"
	$(SILENT)$(HOST_CC) $(CFLAGS) -o $@ -c $<

$(obj)/mconf: $(mconf_OBJ)
	@$(ECHO) '  LD    $@'
	$(SILENT)$(HOST_CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

$(obj)/conf: $(conf_OBJ)
	@$(ECHO) '  LD    $@'
	$(SILENT)$(HOST_CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

#-----------------------------------------------------------
# Cleaning up the mess...

clean::
	@$(ECHO) "  CLEAN kconfig"
	$(SILENT)rm -f kconfig/{,m}conf $(conf_OBJ) $(mconf_OBJ) $(DEPS)
	$(SILENT)rmdir --ignore-fail-on-non-empty kconfig{/lxdialog,} 2>/dev/null || true
	@$(ECHO) "  CLEAN config"
	$(SILENT)rm -f config 2>/dev/null || true
	@$(ECHO) "  CLEAN config.gen"
	$(SILENT)rm -rf config.gen