aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 99a5d01154835d997f2ad03de8482085d1330fb4 (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
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
# Copyright (c) 2016-2017, Intel Corporation.

BOARD ?= arduino_101
RAM ?= 55
ROM ?= 256

# Dump memory information: on = print allocs, full = print allocs + dump pools
TRACE ?= off

# Generate and run snapshot as byte code instead of running JS directly
ifneq (,$(filter $(MAKECMDGOALS),ide ashell linux))
SNAPSHOT=off
# if the user passes in SNAPSHOT=on for ide, ashell, or linux give an error
ifeq ($(SNAPSHOT), on)
$(error ide, ashell, and linux do not support SNAPSHOT=$(SNAPSHOT))
endif
else
# snapshot is enabled by default
SNAPSHOT ?= on
ifeq ($(SNAPSHOT), on)
EXT_JERRY_FLAGS += -DFEATURE_JS_PARSER=OFF
endif
endif

ifndef ZJS_BASE
$(error ZJS_BASE not defined. You need to source zjs-env.sh)
endif

ifeq ($(BOARD), arduino_101)
# RAM can't be less than the 55KB normally allocated for x86
# NOTE: We could change this and allow it though, find a sane minimum
ifeq ($(shell test $(RAM) -lt 55; echo $$?), 0)
$(error RAM must be at least 55)
endif

# RAM can't be more than 79KB, the total size of RAM left for x86/arc
# NOTE: the first 1KB is reserved for ARC_READY bit
ifeq ($(shell test $(RAM) -gt 79; echo $$?), 0)
$(error RAM must be no higher than 79)
endif

# ROM can't be less than the 144KB physically allocated for x86
# NOTE: We could change this and allow it though, but splice images in reverse
ifeq ($(shell test $(ROM) -lt 144; echo $$?), 0)
$(error ROM must be at least 144)
endif

# ROM can't be more than 296KB, the total size of x86 + arc partitions combined
ifeq ($(shell test $(ROM) -gt 296; echo $$?), 0)
$(error ROM must be no higher than 296)
endif
endif  # BOARD = arduino_101

ifeq ($(filter $(MAKECMDGOALS),linux), linux)
$(error 'linux' make target is deprecated, use "make BOARD=linux")
endif

OCF_ROOT ?= deps/iotivity-constrained
JS ?= samples/HelloWorld.js
VARIANT ?= release
DEVICE_NAME ?= "Zephyr OCF Node"
BLE_ADDR ?= "none"
# JerryScript options
JERRY_BASE ?= $(ZJS_BASE)/deps/jerryscript
EXT_JERRY_FLAGS ?=	-DENABLE_ALL_IN_ONE=ON \
					-DFEATURE_PROFILE=$(ZJS_BASE)/jerry_feature.profile \
					-DFEATURE_ERROR_MESSAGES=OFF \
					-DJERRY_LIBM=OFF

# Settings for ashell builds
ifneq (,$(filter $(MAKECMDGOALS),ide ashell))
CONFIG ?= fragments/zjs.conf.dev
DEV=ashell
endif

ifeq ($(BOARD), arduino_101)
EXT_JERRY_FLAGS += -DENABLE_LTO=ON
$(info makecmd: $(MAKECMDGOALS))
ifeq ($(MAKECMDGOALS),)
TARGETS=all
else
TARGETS=$(MAKECMDGOALS)
endif

# if actually building for A101 (not clean), report RAM/ROM allocation
ifneq ($(filter all zephyr arc,$(TARGETS)),)
$(info Building for Arduino 101...)
$(info $() $() RAM allocation: $(RAM)KB for X86, $(shell echo $$((79 - $(RAM))))KB for ARC)
$(info $() $() ROM allocation: $(ROM)KB for X86, $(shell echo $$((296 - $(ROM))))KB for ARC)
endif
endif  # BOARD = arduino_101

# Print callback statistics during runtime
CB_STATS ?= off
# Print floats (uses -u _printf_float flag). This is a workaround on the A101
# otherwise floats will not print correctly. It does use ~11k extra ROM though
PRINT_FLOAT ?= off

ifeq ($(BOARD), linux)
	SNAPSHOT = off
endif

ifeq ($(BOARD), arduino_101)
ARC = arc
endif

.PHONY: all
ifeq ($(BOARD), linux)
all: linux
else
all: zephyr
endif

A101BIN = outdir/arduino_101/zephyr.bin
A101SSBIN = arc/outdir/arduino_101_sss/zephyr.bin

.PHONY: ram_report
ram_report: zephyr
	@make -f Makefile.zephyr	BOARD=$(BOARD) \
					VARIANT=$(VARIANT) \
					CB_STATS=$(CB_STATS) \
					PRINT_FLOAT=$(PRINT_FLOAT) \
					SNAPSHOT=$(SNAPSHOT) \
					ram_report

.PHONY: rom_report
rom_report: zephyr
	@make -f Makefile.zephyr	BOARD=$(BOARD) \
					VARIANT=$(VARIANT) \
					CB_STATS=$(CB_STATS) \
					PRINT_FLOAT=$(PRINT_FLOAT) \
					SNAPSHOT=$(SNAPSHOT) \
					rom_report

# Build for zephyr, default target
.PHONY: zephyr
zephyr: analyze generate jerryscript $(ARC)
	@make -f Makefile.zephyr	BOARD=$(BOARD) \
					VARIANT=$(VARIANT) \
					CB_STATS=$(CB_STATS) \
					PRINT_FLOAT=$(PRINT_FLOAT) \
					SNAPSHOT=$(SNAPSHOT) \
					BLE_ADDR=$(BLE_ADDR) \
					DEV=$(DEV)
ifeq ($(BOARD), arduino_101)
	@echo
	@echo -n Creating dfu images...
	@dd if=$(A101BIN) of=$(A101BIN).dfu bs=1024 count=144 2> /dev/null
	@dd if=$(A101BIN) of=$(A101SSBIN).dfu bs=1024 skip=144 2> /dev/null
	@dd if=$(A101SSBIN) of=$(A101SSBIN).dfu bs=1024 seek=$$(($(ROM) - 144)) 2> /dev/null
	@echo " done."
endif

.PHONY: ide
ide: zephyr

.PHONY: ashell
ashell: zephyr

# Flash Arduino 101 x86 and arc images
.PHONY: dfu
dfu:
	dfu-util -a x86_app -D $(A101BIN).dfu
	dfu-util -a sensor_core -D $(A101SSBIN).dfu

# Build JerryScript as a library (libjerry-core.a)
jerryscript:
	@echo "Building" $@
	$(MAKE) -C $(JERRY_BASE) -f targets/zephyr/Makefile.zephyr BOARD=$(BOARD) EXT_JERRY_FLAGS="$(EXT_JERRY_FLAGS)" jerry
	mkdir -p outdir/$(BOARD)/
	cp $(JERRY_BASE)/build/$(BOARD)/obj-$(BOARD)/lib/libjerry-core.a outdir/$(BOARD)/

# Give an error if we're asked to create the JS file
$(JS):
	$(error The file $(JS) does not exist.)

# Find the modules the JS file depends on
.PHONY: analyze
analyze: $(JS)
	@echo "% This is a generated file" > prj.mdef
	@echo "# This is a generated file" > src/Makefile
	@cat src/Makefile.base >> src/Makefile
	@echo "# This is a generated file" > arc/src/Makefile
	@cat arc/src/Makefile.base >> arc/src/Makefile
	@if [ "$(TRACE)" = "on" ] || [ "$(TRACE)" = "full" ]; then \
		echo "ccflags-y += -DZJS_TRACE_MALLOC" >> src/Makefile; \
	fi
	@if [ "$(SNAPSHOT)" = "on" ]; then \
		echo "ccflags-y += -DZJS_SNAPSHOT_BUILD" >> src/Makefile; \
	fi
	@echo "ccflags-y += $(shell ./scripts/analyze.sh $(BOARD) $(JS) $(CONFIG) $(DEV))" | tee -a src/Makefile arc/src/Makefile
	@sed -i '/This is a generated file/r./zjs.conf.tmp' src/Makefile
	@# Add the include for the OCF Makefile only if the script is using OCF
	@if grep BUILD_MODULE_OCF src/Makefile; then \
		echo "CONFIG_BLUETOOTH_DEVICE_NAME=\"$(DEVICE_NAME)\"" >> prj.conf.tmp; \
		echo "include \$$(ZJS_BASE)/Makefile.ocf_zephyr" >> src/Makefile; \
	fi

# Update dependency repos
.PHONY: update
update:
	@git submodule update --init
	@cd $(OCF_ROOT); git submodule update --init;

# set up prj.conf file
-.PHONY: setup
setup:
	@echo "# This is a generated file" > prj.conf
	@cat fragments/prj.conf.base >> prj.conf

ifeq ($(BOARD), qemu_x86)
	@cat fragments/prj.conf.qemu_x86 >> prj.conf
else
ifeq ($(DEV), ashell)
	@cat fragments/prj.conf.ashell >> prj.conf
	@cat fragments/prj.mdef.ashell >> prj.mdef
ifeq ($(filter ide,$(MAKECMDGOALS)),ide)
	@echo CONFIG_USB_CDC_ACM=n >> prj.conf
else
	@echo CONFIG_USB_CDC_ACM=y >> prj.conf
endif
endif
ifeq ($(BOARD), arduino_101)
	@cat fragments/prj.conf.arduino_101 >> prj.conf
	@printf "CONFIG_PHYS_RAM_ADDR=0xA800%x\n" $$(((80 - $(RAM)) * 1024)) >> prj.conf
	@echo "CONFIG_RAM_SIZE=$(RAM)" >> prj.conf
	@echo "CONFIG_ROM_SIZE=$(ROM)" >> prj.conf
	@printf "CONFIG_SS_RESET_VECTOR=0x400%x\n" $$((($(ROM) + 64) * 1024)) >> prj.conf
endif
endif
# Append script specific modules to prj.conf
	@if [ -e prj.conf.tmp ]; then \
		cat prj.conf.tmp >> prj.conf; \
	fi

.PHONY: cleanlocal
cleanlocal:
	@rm -f src/*.o
	@rm -f src/Makefile
	@rm -f arc/prj.conf
	@rm -f arc/prj.conf.tmp
	@rm -f arc/src/Makefile
	@rm -f arc/outdir/arduino_101_sss/zephyr.bin.dfu
	@rm -f outdir/arduino_101/zephyr.bin.dfu
	@rm -f outdir/jsgen.tmp
	@rm -f outdir/include/zjs_script_gen.h
	@rm -f outdir/include/zjs_snapshot_gen.h
	@rm -f prj.conf
	@rm -f prj.conf.tmp
	@rm -f prj.mdef
	@rm -f zjs.conf.tmp
	@rm -f js.tmp
	@rm -f .snapshot.last_build

# Explicit clean
.PHONY: clean
clean: cleanlocal
ifeq ($(BOARD), linux)
	@make -f Makefile.linux clean
else
	@rm -rf $(JERRY_BASE)/build/$(BOARD)/;
	@rm -f outdir/$(BOARD)/libjerry-core.a;
	@make -f Makefile.zephyr clean BOARD=$(BOARD);
	@cd arc/; make clean;
endif

.PHONY: pristine
pristine: cleanlocal
	@rm -rf $(JERRY_BASE)/build;
	@make -f Makefile.zephyr pristine;
	@cd arc; make pristine;

# Generate the script file from the JS variable
.PHONY: generate
generate: $(JS) setup
	@mkdir -p outdir/include/
ifeq ($(SNAPSHOT), on)
	@echo Building snapshot generator...
	@if ! [ -e outdir/snapshot/snapshot ]; then \
		make -f tools/Makefile.snapshot; \
	fi
	@echo Creating snapshot bytecode from JS application...
	@if [ -x /usr/bin/uglifyjs ]; then \
		uglifyjs js.tmp -nc -mt > outdir/jsgen.tmp; \
	else \
		cat js.tmp > outdir/jsgen.tmp; \
	fi
	@outdir/snapshot/snapshot outdir/jsgen.tmp > outdir/include/zjs_snapshot_gen.h
# SNAPSHOT=on, check if rebuilding JerryScript is needed
ifeq ("$(wildcard .snapshot.last_build)", "")
	@rm -rf $(JERRY_BASE)/build/$(BOARD)/
	@rm -f outdir/$(BOARD)/libjerry-core.a
endif
	echo "" > .snapshot.last_build
else
	@echo Creating C string from JS application...
ifeq ($(BOARD), linux)
	@./scripts/convert.sh $(JS) outdir/include/zjs_script_gen.h
else
	@./scripts/convert.sh js.tmp outdir/include/zjs_script_gen.h
endif
# SNAPSHOT=off, check if rebuilding JerryScript is needed
ifneq ("$(wildcard .snapshot.last_build)", "")
	@rm -rf $(JERRY_BASE)/build/$(BOARD)/
	@rm -f outdir/$(BOARD)/libjerry-core.a
endif
	@rm -f .snapshot.last_build
endif

NET_BUILD=$(shell grep -q -E "BUILD_MODULE_OCF|BUILD_MODULE_DGRAM" src/Makefile && echo y)

# Run QEMU target
.PHONY: qemu
qemu: zephyr
	make -f Makefile.zephyr qemu \
		CB_STATS=$(CB_STATS) \
		SNAPSHOT=$(SNAPSHOT) \
		NETWORK_BUILD=$(NET_BUILD)

# Builds ARC binary
.PHONY: arc
arc: analyze
	@echo "# This is a generated file" > arc/prj.conf
	@cat arc/fragments/prj.conf.base >> arc/prj.conf
	@if [ -e arc/prj.conf.tmp ]; then \
		cat arc/prj.conf.tmp >> arc/prj.conf; \
	fi
	@printf "CONFIG_SRAM_SIZE=%d\n" $$((79 - $(RAM))) >> arc/prj.conf
	@printf "CONFIG_FLASH_BASE_ADDRESS=0x400%x\n" $$((($(ROM) + 64) * 1024)) >> arc/prj.conf
	@cd arc; make BOARD=arduino_101_sss
ifeq ($(BOARD), arduino_101)
	@echo
	@if test $$(((296 - $(ROM)) * 1024)) -lt $$(stat --printf="%s" $(A101SSBIN)); then echo Error: ARC image \($$(stat --printf="%s" $(A101SSBIN)) bytes\) will not fit in available $$(((296 - $(ROM))))KB space. Try decreasing ROM.; return 1; fi
endif

# Run debug server over JTAG
.PHONY: debug
debug:
	make -f Makefile.zephyr BOARD=arduino_101 ARCH=x86 debugserver

# Run gdb to connect to debug server for x86
.PHONY: gdb
gdb:
	$$ZEPHYR_SDK_INSTALL_DIR/sysroots/i686-pokysdk-linux/usr/bin/iamcu-poky-elfiamcu/i586-poky-elfiamcu-gdb outdir/arduino_101/zephyr.elf -ex "target remote :3333"

# Run gdb to connect to debug server for ARC
.PHONY: arcgdb
arcgdb:
	$$ZEPHYR_SDK_INSTALL_DIR/sysroots/i686-pokysdk-linux/usr/bin/arc-poky-elf/arc-poky-elf-gdb arc/outdir/zephyr.elf -ex "target remote :3334"

# Linux target
.PHONY: linux
# Linux command line target, script can be specified on the command line
linux: generate
	make -f Makefile.linux JS=$(JS) VARIANT=$(VARIANT) CB_STATS=$(CB_STATS) V=$(V) SNAPSHOT=$(SNAPSHOT)

test:
	scripts/run_tests

.PHONY: help
help:
	@echo "JavaScript Runtime for Zephyr OS - Build System"
	@echo
	@echo "Build targets:"
	@echo "    all:       Build for either Zephyr or Linux depending on BOARD"
	@echo "    zephyr:    Build Zephyr for the given BOARD (A101 is default)"
	@echo "    arc:       Build just the ARC Zephyr target for Arduino 101"
	@echo "    linux:     Build the Linux target"
	@echo "    dfu:       Flash x86 and arc images to A101 with dfu-util"
	@echo "    debug:     Run debug server using JTAG"
	@echo "    gdb:       Run gdb to connect to debug server for x86"
	@echo "    arcgdb:    Run gdb to connect to debug server for ARC"
	@echo "    qemu:      Run QEMU after building"
	@echo "    clean:     Clean stale build objects for given BOARD"
	@echo "    pristine:  Completely remove all generated files"
	@echo
	@echo "Build options:"
	@echo "    BOARD=     Specify a Zephyr board to build for"
	@echo "    JS=        Specify a JS script to compile into the binary"
	@echo "    RAM=       Specify size in KB for RAM allocated to X86"
	@echo "    ROM=       Specify size in KB for X86 partition (144 - 296)"
	@echo "    SNAPSHOT=  Specify off to turn off snapshotting"
	@echo "    TRACE=     Specify 'on' for malloc tracing (off is default)"
	@echo "    VARIANT=   Specify 'debug' for extra serial output detail"
	@echo