-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakefile.common
More file actions
401 lines (315 loc) · 15.3 KB
/
Makefile.common
File metadata and controls
401 lines (315 loc) · 15.3 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
### ==========================================================================
### $Id: Makefile.common 606 2007-11-14 22:02:48Z hoenicke $
### FILE: Makefile.common - definitions common to most brickOS Makefiles
### bibo - another LEGO MindStorms RCX OS
### --------------------------------------------------------------------------
### (this file is included by Makefiles that build programs)
# ----------------------------------------------------------------------------
# Initialization
# ----------------------------------------------------------------------------
# Using $(abspath x) here so that symlinks are NOT replaced
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(abspath $(dir $(MAKEFILE_PATH)))
MAKEFILE_NAME := $(notdir $(MAKEFILE_PATH))
HOST_PROPERTIES_FILE := host.properties
HOST_PROPERTIES_PATH := $(MAKEFILE_DIR)/$(HOST_PROPERTIES_FILE)
# Since we are not installed, we need to ensure we are properly configured
include $(HOST_PROPERTIES_PATH)
$(HOST_PROPERTIES_PATH):
$(MAKE) --makefile=$(MAKEFILE_DIR)/Makefile.configure
# ----------------------------------------------------------------------------
# Setup Default Values
# ----------------------------------------------------------------------------
# Default subdirectory and file names
BINSUBDIR ?= bin
SBINSUBDIR ?= sbin
LIBEXECSUBDIR ?= libexec
LIBSUBDIR ?= lib
SYSCONFSUBDIR ?= etc
SHAREDSTATESUBDIR ?= com
LOCALSTATESUBDIR ?= var
INCLUDESUBDIR ?= include
DATASUBDIR ?= share
INFOSUBDIR ?= info
LOCALESUBDIR ?= locale
MANSUBDIR ?= man
MAN1SUBDIR ?= man1
DOCSUBDIR ?= doc
HTMLSUBDIR ?= html
ARCHSUBDIR ?= $(BRICK_ARCH)
KERNELSUBDIR ?= boot
KCONFIGSUBDIR ?= kconfig
CONFIGDEFAULTSFILE ?= config.h
KCONFIGFILE ?= kernel-config.h
# Default subdirectory combinations
CONFIGDEFAULTSPATH ?= $(INCLUDEDIR)/$(CONFIGDEFAULTSFILE)
KCONFIGINSTANCESUBDIR ?= $(KCONFIGSUBDIR)/$(KERNEL)
KCONFIGINSTANCEPATH ?= $(KCONFIGINSTANCESUBDIR)/$(KCONFIGFILE)
# Default subdirectory combinations for the target arch
SYSCONF_PACKAGESUBDIR ?= $(SYSCONFSUBDIR)/$(PACKAGE)
INCLUDE_PACKAGESUBDIR ?= $(INCLUDESUBDIR)/$(PACKAGE)/$(VERSION_SERIES)
INCLUDE_KCONFIGSUBDIR ?= $(INCLUDESUBDIR)/$(KCONFIGSUBDIR)
INCLUDE_KCONFIG_INSTANCESUBDIR ?= $(INCLUDESUBDIR)/$(KCONFIGINSTANCESUBDIR)
KERNEL_INSTANCESUBDIR ?= $(KERNELSUBDIR)/$(KERNEL)
LIB_INSTANCESUBDIR ?= $(LIBSUBDIR)/$(KERNEL)
# ----------------------------------------------------------------------------
# Environment Differentiation: Source Tree vs. Installed
# ----------------------------------------------------------------------------
# Lookup available shared libraries
CROSSLIBS = $(patsubst $(LIBDIR)/lib%.a,-l%,$(wildcard $(LIBDIR)/lib*.a))
# "INSTALLED_ROOT" will only be defined if installed (and not if running from source tree)
ifeq ("$(wildcard $(MAKEFILE_DIR)/makelx.sh)","")
# Running as installed, so set the root path of the installation
RUNTIME_ROOT ?= $(abspath $(MAKEFILE_DIR)/../../..)
$(info Running as installed from $(RUNTIME_ROOT))
BINDIR ?= $(RUNTIME_ROOT)/$(BINSUBDIR)
LIBEXECPKGDIR ?= $(RUNTIME_ROOT)/$(LIBEXECSUBDIR)/$(PACKAGE)
ARCHDIR ?= $(RUNTIME_ROOT)/$(ARCHSUBDIR)
INCLUDEDIR ?= $(ARCHDIR)/$(INCLUDE_PACKAGESUBDIR)
KCONFIGDIR ?= $(ARCHDIR)/$(INCLUDE_KCONFIGSUBDIR)
KCONFIGINSTANCEDIR ?= $(ARCHDIR)/$(INCLUDE_KCONFIG_INSTANCESUBDIR)
KERNELDIR ?= $(ARCHDIR)/$(KERNELSUBDIR)
KERNELINSTANCEDIR ?= $(ARCHDIR)/$(KERNEL_INSTANCESUBDIR)
LIBDIR ?= $(ARCHDIR)/$(LIB_INSTANCESUBDIR)
MULTIKERNELSUBDIR_SEARCH_PATTERN ?= /*
else
# Running from the source tree
RUNTIME_ROOT ?= $(MAKEFILE_DIR)
$(info Running relative to the source tree at $(MAKEFILE_DIR))
BINDIR ?= util
LIBEXECPKGDIR ?= util
INCLUDEDIR ?= $(INCLUDESUBDIR)
KCONFIGDIR ?= $(KCONFIGSUBDIR)
KCONFIGINSTANCEDIR ?= $(KCONFIGINSTANCESUBDIR)
KERNELDIR ?= kernel
KERNELINSTANCEDIR ?= $(KERNELDIR)
LIBDIR ?= $(LIBSUBDIR)
ifeq ("$(CROSSLIBS)","")
# Lib check based on lib*.a files didn't find anything, so now check for source tree subfolder names
CROSSLIBS = $(patsubst $(LIBDIR)/%/Makefile.sub,-l%,$(wildcard $(LIBDIR)/*/Makefile.sub))
endif
MULTIKERNELSUBDIR_SEARCH_PATTERN ?=
endif
# ----------------------------------------------------------------------------
# Directory Path Variables Configuration
# ----------------------------------------------------------------------------
# PACKAGE MAINTAINERS: Adjust here the default paths of where files were installed
# if these paths differ from your packaging system defaults.
#
# Define default install paths (overridden by packaging systems)
#
DESTDIR ?=
prefix ?= /opt/stow/${PACKAGE}
exec_prefix ?= ${prefix}
bindir ?= ${exec_prefix}/${BINSUBDIR}
sbindir ?= ${exec_prefix}/${SBINSUBDIR}
libexecdir ?= ${exec_prefix}/${LIBEXECSUBDIR}
libdir ?= ${exec_prefix}/${LIBSUBDIR}
sysconfdir ?= ${prefix}/${SYSCONFSUBDIR}
sharedstatedir ?= ${prefix}/${SHAREDSTATESUBDIR}
localstatedir ?= ${prefix}/${LOCALSTATESUBDIR}
includedir ?= ${prefix}/${INCLUDESUBDIR}
datarootdir ?= ${prefix}/${DATASUBDIR}
datadir ?= ${datarootdir}
infodir ?= ${datadir}/${INFOSUBDIR}
localedir ?= ${datadir}/${LOCALESUBDIR}
docdir ?= ${datadir}/${DOCSUBDIR}
htmldir ?= ${docdir}/${HTMLSUBDIR}
mandir ?= ${datadir}/${MANSUBDIR}
man1dir ?= ${mandir}/${MAN1SUBDIR}
pkglibexecdir ?= ${libexecdir}/${PACKAGE}
pkgdatadir ?= ${datadir}/${PACKAGE}
pkgdocdir ?= ${docdir}/packages/${PACKAGE}
pkghtmldir ?= ${pkgdocdir}/${HTMLSUBDIR}
pkgexampledir ?= ${pkgdocdir}/${EXAMPLESSUBDIR}
pkgtargetdir ?= ${prefix}/${ARCHSUBDIR}
pkgtargetincludedir ?= ${pkgtargetdir}/${INCLUDE_PACKAGESUBDIR}
pkgtargetkconfigdir ?= ${pkgtargetdir}/${INCLUDE_KCONFIGSUBDIR}
pkgtargetkconfiginstancedir ?= ${pkgtargetdir}/${INCLUDE_KCONFIG_INSTANCESUBDIR}
pkgtargetkerneldir ?= ${pkgtargetdir}/${KERNELSUBDIR}
pkgtargetkernelinstancedir ?= ${pkgtargetdir}/${KERNEL_INSTANCESUBDIR}
pkgtargetlibinstancedir ?= ${pkgtargetdir}/${LIB_INSTANCESUBDIR}
pkgtargetsysconfdir ?= ${pkgtargetdir}/${SYSCONF_PACKAGESUBDIR}
# ----------------------------------------------------------------------------
# Kernel Name Check
# ----------------------------------------------------------------------------
# If building a kernel or *.lx program, a name (KERNEL) needs to be specified.
# If NOT specified, check if a single, distinct kernel name can be found.
ifndef KERNEL
# Search both the current directory (for if running from the source tree)
# and any immediate subdirectories (for if installed)
FOUND_LDS_FILES := $(wildcard $(KERNELDIR)$(MULTIKERNELSUBDIR_SEARCH_PATTERN)/*.lds)
ifeq ($(words $(FOUND_LDS_FILES)),1)
KERNEL := $(basename $(notdir $(FOUND_LDS_FILES)))
# Now that KERNEL has been found and defined, DYNAMIC_LDS should work
$(info A single LDS file "$(FOUND_LDS_FILES)" found, for kernel "$(KERNEL)")
else ifeq ($(words $(FOUND_LDS_FILES)),0)
$(error ERROR: No kernel specified, and no dynamic linker "*.lds" files found under "$(KERNELDIR)")
else
$(error ERROR: No kernel specified, but multiple dynamic linker "*.lds" files found under "$(KERNELDIR)")
endif
endif
# ----------------------------------------------------------------------------
# Build Variables Configuration
# ----------------------------------------------------------------------------
# Kernel libraries. Use second version to link all
# libraries statically to kernel. Change CROSSLIBS, too.
# - Version 1: Dynamically Linked
KLIBS=-Llib -lc -lmint
KLIBRARIES=$(LIBC) $(LIBMINT)
# - Version 2: Statically Linked
#KLIBS=--whole-archive -lc -lmint
#KLIBRARIES=$(LIBC) $(LIBMINT)
# User application libraries. Should be cleared if linking
# them statically to kernel in $(KLIBS).
CROSSLIBS := $(if $(filter --whole-archive,$(KLIBS)),,$(CROSSLIBS))
# Defines for build of both native and cross-compiled parts
#
COPT = -Os -fno-builtin -fomit-frame-pointer
CWARN = -Wall
# Defines for build of native parts: utils and other host-run programs
#
CC = gcc
CINC =
CDEF =
CFLAGS = $(COPT) $(CWARN) $(CINC) $(CDEF)
ifeq ("$(HOST_OS_TYPE)","Windows")
CDEF += -D_WIN32
else
COPT += -g
endif
# Defines for build of cross-compiled parts: firmware, demo programs
#
CROSSCOPT = $(COPT) -mquickcall
CROSSCWARN = $(CWARN)
CROSSCINC = -I$(INCLUDEDIR) -I$(KCONFIGINSTANCEDIR)
CROSSCFLAGS = $(CROSSCOPT) $(CROSSCWARN) $(CROSSCINC) $(CROSSCDEFINES)
CROSSCXXFLAGS = -DCXX -fno-rtti -fno-exceptions $(CROSSCFLAGS)
CROSSPFLAGS = $(CROSSCFLAGS) --extended-syntax --unit-path=$(BRICKOS_ROOT)/lib/p --automake
# Defines for internal programs and scripts
#
#MERGEMAP = $(LIBEXECPKGDIR)/merge-map$(HOST_EXEC_EXT)
#FIXDEPS = $(LIBEXECPKGDIR)/fixdeps$(HOST_EXEC_EXT)
GENLDS = $(LIBEXECPKGDIR)/genlds$(HOST_EXEC_EXT)
WRITELX = $(LIBEXECPKGDIR)/writelx$(HOST_EXEC_EXT)
# Linker command files dynamic executables (variable "$(KERNEL_FILES_BASE)" is without the extension).
#
KERNEL_FILES_BASE = $(KERNELINSTANCEDIR)/$(KERNEL)
KERNEL_METADATA_FILE = $(KERNEL_FILES_BASE).kmetadata
DYNAMIC_LDS = $(KERNEL_FILES_BASE).lds
DLDFLAGS = -T $(DYNAMIC_LDS) -relax -L$(LIBDIR)
# ----------------------------------------------------------------------------
# Header configuration lookups
# ----------------------------------------------------------------------------
GET_DEFINE_FROM_FILE = $(shell sed -n -E 's/^([[:blank:]]*#[[:blank:]]*define[[:blank:]]*$(1)[[:blank:]]*)([^[:blank:]]+([[:blank:]]*[^[:blank:]])*)([[:blank:]]*)$$/\2/p' $(2))
# Check the kerne-specific configuration and then the default configuration
PROG_MAX := $(or $(call GET_DEFINE_FROM_FILE,PROG_MAX,$(KCONFIGINSTANCEPATH)),$(call GET_DEFINE_FROM_FILE,PROG_MAX,$(CONFIGDEFAULTSPATH)),$(error ERROR: Unable to get the configuration for PROG_MAX))
# ----------------------------------------------------------------------------
# Base address determination for relocation info
# ----------------------------------------------------------------------------
BASE1_DEFAULT = 0xb000
BASE2_DEFAULT = 0xb210
GET_CONF_FROM_FILE = $(shell [ -e $(2) ] && cat $(2) | grep -E ^$(1)[[:space:]]*=[[:space:]]*0x[0-9A-Fa-f]{4}$ | sed s/$(1)[[:space:]]*=[[:space:]]*// )
GET_BASE_FROM_LDS = $(call GET_CONF_FROM_FILE,$(1),$(DYNAMIC_LDS))
# Tracking BASE1 and BASE2 in the Makefile provides fallback support if unable
# to read the dynamic LDS file, but such static tracking breaks support for
# dynamically installing and working with different kernels. Leaving support
# for static BASE tracking for if needed but disabling by default.
ifdef RECORD_BASE_IN_MAKEFILE
# Base addresses updated from $(DYNAMIC_LDS) in kernel/Makefile.sub, target BaseAddresses
# (Values should be checked as kernel changes but kept as backup if GET_BASE fails)
BASE1 = 0xac44
BASE2 = 0xae54
# Use precedence ordering for obtaining BASE1 and BASE2 values:
# $(BASEx) value from $(DYNAMIC_LDS) file, $(BASEx), and then $(BASEx)_DEFAULT
GET_BASE = $(or $(call GET_BASE_FROM_LDS,$(1)),$($(1)),$($(1)_DEFAULT))
# Empty if the base has not changed, new base value if it has changed
BASE_CHANGED = $(if $(findstring $(call GET_CONF_FROM_FILE,$(1),Makefile.common),$(call GET_BASE,$(1))),,$(call GET_BASE,$(1)))
# Update the BASE1 and BASE2 addresses in Makefile.common only if there has been a change
BaseAddresses: $(KERNEL_FILES_BASE).lds
[ -z '$(call BASE_CHANGED,BASE1)' ] || sed -i -r 's/^BASE1[[:space:]]*=[[:space:]]*0x[0-9A-Fa-f]{4}$$/BASE1 = $(call GET_BASE,BASE1)/' Makefile.common
[ -z '$(call BASE_CHANGED,BASE2)' ] || sed -i -r 's/^BASE2[[:space:]]*=[[:space:]]*0x[0-9A-Fa-f]{4}$$/BASE2 = $(call GET_BASE,BASE2)/' Makefile.common
kernel:: BaseAddresses
else
# If not already set, lookup BASE1 and BASE2 from the dynamic LDS file
GET_BASE = $(or $($(1)),$(call GET_BASE_FROM_LDS,$(1)),$(error ERROR: Unable to get $(1) from the "$(DYNAMIC_LDS)" dynamic LDS file))
endif
# ----------------------------------------------------------------------------
# Targets for building of cross-compiled parts: firmware, demo programs
# ----------------------------------------------------------------------------
############ new dynamic executables stuff
# How to build executables dynamically linked to the kernel
# Set DYNAMIC_TEXT, DOBJECTS, DYNAMIC_LDS appropriately.
%.dcoff: %.o %.a $(DYNAMIC_LDS)
$(CROSSLD) $(DLDFLAGS) $*.o $*.a $(CROSSLIBS) -o $@ -Ttext $(call GET_BASE,BASE1) --oformat coff-h8300
# How to make kernel lds files
%.lds: %.coff $(GENLDS)
$(CROSSNM) $< | sort -u | $(GENLDS) $* > $@
# How to make barebones dymanic executable map files
%.dmap: %.dcoff
$(CROSSNM) $< | sort > $@
# How to disassemble coff kernel
%.dis: %.dcoff
$(CROSSOBJDUMP) $(ODFLAGS) $*.dcoff > $@
# How to merge map files with symbols
%.dis2: %.dmap %.dis
$(MERGEMAP) $*.dmap $*.dis > $@
# How to assemble
%.o: %.s
$(CROSSAS) $< -o $@
# How to compile assembler source for the Hitachi H8/300
%.o: %.S
$(CROSSCC) -MD $(CROSSCFLAGS) -c $< -o $@
# How to compile C source for the Hitachi H8/300
%.o: %.c
$(CROSSCC) -MD $(CROSSCFLAGS) -c $< -o $@
# How to generate an assembly listing of C source
%.s: %.c
$(CROSSCC) $(CROSSCFLAGS) -S $< -o $@
# How to compile C++ source for the Hitachi H8/300
%.o: %.cpp
$(CROSSCXX) $(CROSSCXXFLAGS) -c $< -o $@
# How to compile C++ source for the Hitachi H8/300
%.o: %.C
$(CROSSCXX) $(CROSSCXXFLAGS) -c $< -o $@
# How to compile pas source
%.o: %.pas
$(CROSSGPC) $(CROSSPFLAGS) -c $< -o $@
# How to generate an assembly listing of Pascal source
%.s: %.pas
$(CROSSGPC) $(CROSSPFLAGS) -c $< -S
# How to compile XS:Lisp source for the Hitachi H8/300
# *.o: %.lsp
# TODO: c.f. demo/xs-lisp/Makefile.sub
# How to compile Esterel source for the Hitachi H8/300 (use Automaton mode by default [smallest generated code footprint])
%.c: %.strl
esterel -D $(@D) -ALego $<
# How to make archive for extra object files
#$(PROGRAMS:%.lx=%.a): %.a: $(filter %.o,$(%_SRC:%.c=%.o) $(%_SRC:%.cpp=%.o) $(%_SRC:%.s=%.o) $(%_SRC:%.S=%.o))
#.SECONDEXPANSION:
#%.a: %.o $$($$(notdir $$*)_OBJS)
%.a: _SRC_OBJS = $(filter %.o, $($(notdir $*)_SRC:%.c=%.o) \
$($(notdir $*)_SRC:%.cpp=%.o) $($(notdir $*)_SRC:%.s=%.o) $($(notdir $*)_SRC:%.S=%.o))
%.a: %.o
rm -f $@
$(CROSSAR) -sq $@ $^ $(_SRC_OBJS)
# How to make coff files of dynamic executables (twice, for writelx)
%.dc1: %.o %.a $(CROSSLIBS:-l%=$(LIBDIR)/lib%.a) $(DYNAMIC_LDS)
$(CROSSLD) $(DLDFLAGS) $*.o $*.a $(CROSSLIBS) -o $@ --oformat coff-h8300 -Ttext $(call GET_BASE,BASE1)
%.dc2: %.o %.a $(CROSSLIBS:-l%=$(LIBDIR)/lib%.a) $(DYNAMIC_LDS)
$(CROSSLD) $(DLDFLAGS) $*.o $*.a $(CROSSLIBS) -o $@ --oformat coff-h8300 -Ttext $(call GET_BASE,BASE2)
# How to make srec files from coff files (twice, for writelx)
%.ds2: %.dc2
$(CROSSOBJCOPY) -I coff-h8300 -O symbolsrec $< $@
%.ds1: %.dc1
$(CROSSOBJCOPY) -I coff-h8300 -O symbolsrec $< $@
%.lx: %.ds1 %.ds2 $(WRITELX)
$(WRITELX) $*.ds1 $*.ds2 $@
# How to make srec and bin from coff (for download)
%.srec: %.coff
$(CROSSOBJCOPY) -I coff-h8300 -O srec $< $@
%.bin: %.coff
$(CROSSOBJCOPY) -I coff-h8300 -O binary $< $@
### --------------------------------------------------------------------------
### End of FILE: Makefile.common
### ==========================================================================