Merge branch 'master' into bump_clang_version
[betaflight.git] / src / test / Makefile
blobec024bce4bc208c14d5904b991830aeccdb8fb17
1 # A sample Makefile for building Google Test and using it in user
2 # A sample Makefile for building Google Test and using it in user
3 # tests. Please tweak it to suit your environment and project. You
4 # may want to move it to your project's root directory.
6 # SYNOPSIS:
8 # make [all] - makes everything.
9 # make TARGET - makes the given target.
10 # make clean - removes all files generated by make.
13 # Where to find user code.
14 USER_DIR = ../main
15 TEST_DIR = unit
16 ROOT = ../..
17 OBJECT_DIR = ../../obj/test
18 TARGET_DIR = $(USER_DIR)/target
20 include $(ROOT)/make/system-id.mk
21 include $(ROOT)/make/targets_list.mk
23 VPATH := $(VPATH):$(USER_DIR):$(TEST_DIR)
25 # specify which files that are included in the test in addition to the unittest file.
26 # variables available:
27 # <test_name>_SRC
28 # <test_name>_DEFINES
29 # <test_name>_INCLUDE_DIRS
30 # <test_name>_EXPAND (run for each target, call the above with target as $1)
31 # <test_name>_BLACKLIST (targets to exclude from an expanded test's run)
33 alignsensor_unittest_SRC := \
34 $(USER_DIR)/sensors/boardalignment.c \
35 $(USER_DIR)/common/sensor_alignment.c \
36 $(USER_DIR)/common/maths.c
38 arming_prevention_unittest_SRC := \
39 $(USER_DIR)/fc/core.c \
40 $(USER_DIR)/fc/dispatch.c \
41 $(USER_DIR)/fc/rc_controls.c \
42 $(USER_DIR)/fc/rc_modes.c \
43 $(USER_DIR)/fc/runtime_config.c \
44 $(USER_DIR)/flight/gps_rescue.c \
45 $(USER_DIR)/common/bitarray.c
47 arming_prevention_unittest_DEFINES := \
48 USE_GPS_RESCUE=
50 atomic_unittest_SRC := \
51 $(USER_DIR)/build/atomic.c \
52 $(TEST_DIR)/atomic_unittest_c.c
54 baro_bmp085_unittest_SRC := \
55 $(USER_DIR)/drivers/barometer/barometer_bmp085.c
57 baro_bmp085_unittest_DEFINES := \
58 USE_BARO_BMP085=
60 baro_bmp280_unittest_SRC := \
61 $(USER_DIR)/drivers/barometer/barometer_bmp280.c
63 baro_bmp280_unittest_DEFINES := \
64 USE_BARO_BMP280= \
65 USE_BARO_SPI_BMP280=
67 baro_bmp388_unittest_SRC := \
68 $(USER_DIR)/common/maths.c \
69 $(USER_DIR)/drivers/barometer/barometer_bmp388.c
71 baro_bmp388_unittest_DEFINES := \
72 USE_EXTI= \
73 USE_BARO_BMP388= \
74 USE_BARO_SPI_BMP388=
76 baro_ms5611_unittest_SRC := \
77 $(USER_DIR)/drivers/barometer/barometer_ms5611.c
79 baro_ms5611_unittest_DEFINES := \
80 USE_BARO_MS5611= \
81 USE_BARO_SPI_MS5611=
83 # This test is disabled due to build errors.
84 # Its source code is archived in unit/battery_unittest.cc.txt
86 #battery_unittest_SRC := \
87 # $(USER_DIR)/sensors/battery.c \
88 # $(USER_DIR)/common/maths.c
91 blackbox_unittest_SRC := \
92 $(USER_DIR)/blackbox/blackbox.c \
93 $(USER_DIR)/blackbox/blackbox_encoding.c \
94 $(USER_DIR)/blackbox/blackbox_io.c \
95 $(USER_DIR)/common/encoding.c \
96 $(USER_DIR)/common/printf.c \
97 $(USER_DIR)/common/maths.c \
98 $(USER_DIR)/common/typeconversion.c \
99 $(USER_DIR)/drivers/accgyro/gyro_sync.c
101 blackbox_encoding_unittest_SRC := \
102 $(USER_DIR)/blackbox/blackbox_encoding.c \
103 $(USER_DIR)/common/encoding.c \
104 $(USER_DIR)/common/printf.c \
105 $(USER_DIR)/common/typeconversion.c
107 cli_unittest_SRC := \
108 $(USER_DIR)/cli/cli.c \
109 $(USER_DIR)/common/crc.c \
110 $(USER_DIR)/common/printf.c \
111 $(USER_DIR)/common/streambuf.c \
112 $(USER_DIR)/config/feature.c \
113 $(USER_DIR)/pg/pg.c \
114 $(USER_DIR)/common/typeconversion.c
116 cli_unittest_DEFINES := \
117 USE_OSD= \
118 USE_CLI= \
119 SystemCoreClock=1000000
121 cms_unittest_SRC := \
122 $(USER_DIR)/cms/cms.c \
123 $(USER_DIR)/cms/cms_menu_saveexit.c \
124 $(USER_DIR)/common/typeconversion.c \
125 $(USER_DIR)/drivers/display.c
128 common_filter_unittest_SRC := \
129 $(USER_DIR)/common/filter.c \
130 $(USER_DIR)/common/maths.c
133 encoding_unittest_SRC := \
134 $(USER_DIR)/common/encoding.c
137 flight_failsafe_unittest_SRC := \
138 $(USER_DIR)/common/bitarray.c \
139 $(USER_DIR)/fc/rc_modes.c \
140 $(USER_DIR)/fc/runtime_config.c \
141 $(USER_DIR)/flight/failsafe.c
143 flight_failsafe_unittest_DEFINES := \
144 USE_GPS_RESCUE=
147 flight_imu_unittest_SRC := \
148 $(USER_DIR)/common/bitarray.c \
149 $(USER_DIR)/common/maths.c \
150 $(USER_DIR)/config/feature.c \
151 $(USER_DIR)/fc/rc_modes.c \
152 $(USER_DIR)/flight/position.c \
153 $(USER_DIR)/flight/imu.c
156 flight_mixer_unittest := \
157 $(USER_DIR)/flight/mixer.c \
158 $(USER_DIR)/flight/servos.c \
159 $(USER_DIR)/common/maths.c
162 gps_conversion_unittest_SRC := \
163 $(USER_DIR)/common/gps_conversion.c
166 io_serial_unittest_SRC := \
167 $(USER_DIR)/io/serial.c \
168 $(USER_DIR)/drivers/serial_pinconfig.c
171 ledstrip_unittest_SRC := \
172 $(USER_DIR)/common/bitarray.c \
173 $(USER_DIR)/fc/rc_modes.c \
174 $(USER_DIR)/io/ledstrip.c
176 ledstrip_unittest_DEFINES := \
177 USE_LED_STRIP=
180 maths_unittest_SRC := \
181 $(USER_DIR)/common/maths.c
184 motor_output_unittest_SRC := \
185 $(USER_DIR)/drivers/dshot.c
188 osd_unittest_SRC := \
189 $(USER_DIR)/osd/osd.c \
190 $(USER_DIR)/osd/osd_elements.c \
191 $(USER_DIR)/osd/osd_warnings.c \
192 $(USER_DIR)/common/typeconversion.c \
193 $(USER_DIR)/drivers/display.c \
194 $(USER_DIR)/common/maths.c \
195 $(USER_DIR)/common/printf.c \
196 $(USER_DIR)/common/time.c \
197 $(USER_DIR)/common/filter.c \
198 $(USER_DIR)/fc/runtime_config.c
200 osd_unittest_DEFINES := \
201 USE_OSD= \
202 USE_GPS= \
203 USE_RTC_TIME= \
204 USE_ADC_INTERNAL=
206 link_quality_unittest_SRC := \
207 $(USER_DIR)/osd/osd.c \
208 $(USER_DIR)/osd/osd_elements.c \
209 $(USER_DIR)/osd/osd_warnings.c \
210 $(USER_DIR)/common/typeconversion.c \
211 $(USER_DIR)/drivers/display.c \
212 $(USER_DIR)/drivers/serial.c \
213 $(USER_DIR)/common/crc.c \
214 $(USER_DIR)/common/maths.c \
215 $(USER_DIR)/common/printf.c \
216 $(USER_DIR)/common/streambuf.c \
217 $(USER_DIR)/common/time.c \
218 $(USER_DIR)/fc/runtime_config.c \
219 $(USER_DIR)/common/bitarray.c \
220 $(USER_DIR)/fc/rc_modes.c \
221 $(USER_DIR)/rx/rx.c \
222 $(USER_DIR)/pg/pg.c \
223 $(USER_DIR)/rx/crsf.c \
224 $(USER_DIR)/pg/rx.c
226 link_quality_unittest_DEFINES := \
227 USE_OSD= \
228 USE_CRSF_LINK_STATISTICS= \
229 USE_RX_LINK_QUALITY_INFO=
231 pg_unittest_SRC := \
232 $(USER_DIR)/common/crc.c \
233 $(USER_DIR)/common/streambuf.c \
234 $(USER_DIR)/pg/pg.c
237 rc_controls_unittest_SRC := \
238 $(USER_DIR)/fc/rc_controls.c \
239 $(USER_DIR)/pg/pg.c \
240 $(USER_DIR)/common/bitarray.c \
241 $(USER_DIR)/common/crc.c \
242 $(USER_DIR)/common/maths.c \
243 $(USER_DIR)/common/streambuf.c \
244 $(USER_DIR)/fc/rc_adjustments.c \
245 $(USER_DIR)/fc/rc_modes.c
248 rx_crsf_unittest_SRC := \
249 $(USER_DIR)/rx/crsf.c \
250 $(USER_DIR)/common/crc.c \
251 $(USER_DIR)/common/printf.c \
252 $(USER_DIR)/common/typeconversion.c \
253 $(USER_DIR)/common/streambuf.c \
254 $(USER_DIR)/drivers/serial.c
257 rx_ibus_unittest_SRC := \
258 $(USER_DIR)/rx/ibus.c
261 rx_ranges_unittest_SRC := \
262 $(USER_DIR)/common/bitarray.c \
263 $(USER_DIR)/common/crc.c \
264 $(USER_DIR)/common/maths.c \
265 $(USER_DIR)/common/streambuf.c \
266 $(USER_DIR)/fc/rc_modes.c \
267 $(USER_DIR)/rx/rx.c \
268 $(USER_DIR)/pg/pg.c \
269 $(USER_DIR)/pg/rx.c
272 rx_rx_unittest_SRC := \
273 $(USER_DIR)/rx/rx.c \
274 $(USER_DIR)/fc/rc_modes.c \
275 $(USER_DIR)/common/bitarray.c \
276 $(USER_DIR)/common/maths.c \
277 $(USER_DIR)/config/feature.c \
278 $(USER_DIR)/pg/rx.c
280 rx_sumd_unittest_SRC := \
281 $(USER_DIR)/common/crc.c \
282 $(USER_DIR)/common/streambuf.c \
283 $(USER_DIR)/rx/sumd.c
285 scheduler_unittest_SRC := \
286 $(USER_DIR)/scheduler/scheduler.c \
287 $(USER_DIR)/common/crc.c \
288 $(USER_DIR)/common/streambuf.c
290 scheduler_unittest_DEFINES := \
291 USE_OSD=
293 sensor_gyro_unittest_SRC := \
294 $(USER_DIR)/sensors/gyro.c \
295 $(USER_DIR)/sensors/gyro_init.c \
296 $(USER_DIR)/sensors/boardalignment.c \
297 $(USER_DIR)/common/filter.c \
298 $(USER_DIR)/common/crc.c \
299 $(USER_DIR)/common/maths.c \
300 $(USER_DIR)/common/streambuf.c \
301 $(USER_DIR)/common/sensor_alignment.c \
302 $(USER_DIR)/drivers/accgyro/accgyro_fake.c \
303 $(USER_DIR)/drivers/accgyro/gyro_sync.c \
304 $(USER_DIR)/pg/pg.c \
305 $(USER_DIR)/pg/gyrodev.c
307 telemetry_crsf_unittest_SRC := \
308 $(USER_DIR)/rx/crsf.c \
309 $(USER_DIR)/telemetry/crsf.c \
310 $(USER_DIR)/common/crc.c \
311 $(USER_DIR)/common/maths.c \
312 $(USER_DIR)/common/streambuf.c \
313 $(USER_DIR)/common/gps_conversion.c \
314 $(USER_DIR)/common/printf.c \
315 $(USER_DIR)/common/typeconversion.c \
316 $(USER_DIR)/fc/runtime_config.c
318 telemetry_crsf_unittest_DEFINES := \
319 FLASH_SIZE=128 \
320 __TARGET__="TEST" \
321 __REVISION__="revision"
324 telemetry_crsf_msp_unittest_SRC := \
325 $(USER_DIR)/rx/crsf.c \
326 $(USER_DIR)/build/atomic.c \
327 $(USER_DIR)/common/crc.c \
328 $(USER_DIR)/common/streambuf.c \
329 $(USER_DIR)/common/printf.c \
330 $(USER_DIR)/common/streambuf.c \
331 $(USER_DIR)/drivers/serial.c \
332 $(USER_DIR)/common/typeconversion.c \
333 $(USER_DIR)/telemetry/crsf.c \
334 $(USER_DIR)/common/gps_conversion.c \
335 $(USER_DIR)/telemetry/msp_shared.c \
336 $(USER_DIR)/fc/runtime_config.c
338 telemetry_crsf_msp_unittest_DEFINES := \
339 USE_MSP_OVER_TELEMETRY=
342 telemetry_hott_unittest_SRC := \
343 $(USER_DIR)/telemetry/hott.c \
344 $(USER_DIR)/common/gps_conversion.c
347 telemetry_ibus_unittest_SRC := \
348 $(USER_DIR)/telemetry/ibus_shared.c \
349 $(USER_DIR)/telemetry/ibus.c
351 timer_definition_unittest_EXPAND := yes
353 # SITL is a simulator with empty timerHardware and many hearders in target.c.
354 timer_definition_unittest_BLACKLIST := SITL
356 timer_definition_unittest_SRC = \
357 $(TARGET_DIR)/$(call get_base_target,$1)/target.c
359 timer_definition_unittest_DEFINES = \
360 TARGET=$1 \
361 BASE_TARGET=$(call get_base_target,$1)
363 timer_definition_unittest_INCLUDE_DIRS = \
364 $(TEST_DIR)/timer_definition_unittest.include \
365 $(TARGET_DIR)/$(call get_base_target,$1)
367 transponder_ir_unittest_SRC := \
368 $(USER_DIR)/drivers/transponder_ir_ilap.c \
369 $(USER_DIR)/drivers/transponder_ir_arcitimer.c
371 ws2811_unittest_SRC := \
372 $(USER_DIR)/drivers/light_ws2811strip.c
374 huffman_unittest_SRC := \
375 $(USER_DIR)/common/huffman.c \
376 $(USER_DIR)/common/huffman_table.c
378 huffman_unittest_DEFINES := \
379 USE_HUFFMAN=
381 rcdevice_unittest_SRC := \
382 $(USER_DIR)/common/crc.c \
383 $(USER_DIR)/common/bitarray.c \
384 $(USER_DIR)/fc/rc_modes.c \
385 $(USER_DIR)/io/rcdevice.c \
386 $(USER_DIR)/io/rcdevice_cam.c \
387 $(USER_DIR)/pg/pg.c \
389 pid_unittest_SRC := \
390 $(USER_DIR)/common/crc.c \
391 $(USER_DIR)/common/filter.c \
392 $(USER_DIR)/common/maths.c \
393 $(USER_DIR)/common/streambuf.c \
394 $(USER_DIR)/drivers/accgyro/gyro_sync.c \
395 $(USER_DIR)/fc/controlrate_profile.c \
396 $(USER_DIR)/fc/runtime_config.c \
397 $(USER_DIR)/flight/pid.c \
398 $(USER_DIR)/flight/pid_init.c \
399 $(USER_DIR)/pg/pg.c
401 pid_unittest_DEFINES := \
402 USE_ITERM_RELAX= \
403 USE_RC_SMOOTHING_FILTER= \
404 USE_ABSOLUTE_CONTROL= \
405 USE_LAUNCH_CONTROL= \
406 USE_FEEDFORWARD=
408 rcdevice_unittest_DEFINES := \
409 USE_RCDEVICE=
411 vtx_unittest_SRC := \
412 $(USER_DIR)/fc/core.c \
413 $(USER_DIR)/fc/dispatch.c \
414 $(USER_DIR)/fc/rc_controls.c \
415 $(USER_DIR)/fc/rc_modes.c \
416 $(USER_DIR)/fc/runtime_config.c \
417 $(USER_DIR)/drivers/vtx_common.c \
418 $(USER_DIR)/drivers/vtx_table.c \
419 $(USER_DIR)/io/vtx_control.c \
420 $(USER_DIR)/io/vtx.c \
421 $(USER_DIR)/common/bitarray.c
423 vtx_unittest_DEFINES := \
424 USE_VTX_COMMON= \
425 USE_VTX_CONTROL= \
426 USE_VTX_SMARTAUDIO=
428 rx_spi_spektrum_unittest_SRC := \
429 $(USER_DIR)/rx/cyrf6936_spektrum.c
431 rx_spi_spektrum_unittest_DEFINES := \
432 USE_RX_SPI= \
433 USE_RX_SPEKTRUM=
435 rx_spi_expresslrs_unittest_SRC := \
436 $(USER_DIR)/pg/rx_spi_expresslrs.c \
437 $(USER_DIR)/rx/expresslrs_common.c \
438 $(USER_DIR)/rx/expresslrs.c \
439 $(USER_DIR)/build/atomic.c \
441 rx_spi_expresslrs_unittest_DEFINES := \
442 USE_RX_SPI= \
443 USE_RX_EXPRESSLRS= \
444 USE_RX_SX1280= \
445 USE_RX_SX127X= \
447 rx_spi_expresslrs_telemetry_unittest_SRC := \
448 $(USER_DIR)/rx/crsf.c \
449 $(USER_DIR)/telemetry/crsf.c \
450 $(USER_DIR)/common/crc.c \
451 $(USER_DIR)/common/maths.c \
452 $(USER_DIR)/common/streambuf.c \
453 $(USER_DIR)/common/gps_conversion.c \
454 $(USER_DIR)/common/printf.c \
455 $(USER_DIR)/common/typeconversion.c \
456 $(USER_DIR)/rx/expresslrs_telemetry.c \
457 $(USER_DIR)/build/atomic.c \
458 $(USER_DIR)/telemetry/msp_shared.c \
460 rx_spi_expresslrs_telemetry_unittest_DEFINES := \
461 USE_RX_EXPRESSLRS= \
462 USE_GPS= \
463 USE_MSP_OVER_TELEMETRY= \
465 # Please tweak the following variable definitions as needed by your
466 # project, except GTEST_HEADERS, which you can use in your own targets
467 # but shouldn't modify.
469 # Points to the root of Google Test, relative to where this file is.
470 # Remember to tweak this if you move this file.
471 GTEST_DIR = ../../lib/test/gtest
473 # Use clang/clang++ by default
475 CC := clang-12
476 CXX := clang++-12
477 ifeq ($(shell which $(CC) 2>/dev/null),)
478 $(info Falling back to 'clang'.)
479 CC := clang
480 CXX := clang++
481 endif
483 #CC := gcc
484 #CXX := g++
486 # These flags are needed for clang > 10 (linux / MacOS) to make test work:
487 # -Wno-c99-extensions
488 # -Wno-reorder
490 COMMON_FLAGS = \
491 -g \
492 -Wall \
493 -Wextra \
494 -Werror \
495 -Wno-error=unused-command-line-argument \
496 -ggdb3 \
497 -O0 \
498 -DUNIT_TEST \
499 -isystem $(GTEST_DIR)/inc \
500 -MMD -MP \
501 -Wno-c99-extensions \
502 -Wno-reorder \
503 -pipe
505 CC_VERSION = $(shell $(CC) -dumpversion)
506 CXX_VERSION = $(shell $(CXX) -dumpversion)
508 ifeq ($(shell $(CC) -v 2>&1 | grep -q "clang version" && echo "clang"),clang)
510 # Please revisit versions when new clang version arrive. Supported versions: { Linux / OSX: 7 - 14 }
511 # Travis reports CC_VERSION of 4.2.1
512 CC_VERSION_MAJOR := $(firstword $(subst ., ,$(CC_VERSION)))
513 CC_VERSION_CHECK_MIN := 7
514 CC_VERSION_CHECK_MAX := 14
516 # Added flags for clang 11 - 13 are not backwards compatible
517 ifeq ($(shell expr $(CC_VERSION_MAJOR) \> 10 \& $(CC_VERSION_MAJOR) \< 14), 1)
518 COMMON_FLAGS += \
519 -fcommon \
520 -Wno-void-pointer-to-int-cast
521 endif
523 ifeq ($(shell expr $(CC_VERSION_MAJOR) \< $(CC_VERSION_CHECK_MIN) \| $(CC_VERSION_MAJOR) \> $(CC_VERSION_CHECK_MAX)),1)
524 $(error $(CC) $(CC_VERSION) is not supported. The officially supported version of clang is 'clang-12'. If this is not found, 'clang' is used as a fallback. The version of the compiler must be between $(CC_VERSION_CHECK_MIN) and $(CC_VERSION_CHECK_MAX).)
525 endif
527 COMMON_FLAGS += -fblocks
528 ifndef CYGWIN
529 ifneq ($(OSFAMILY), macosx)
530 LDFLAGS += -lBlocksRuntime
531 endif
532 endif
533 endif
535 $(info CC version: $(shell $(CC) --version))
536 $(info CXX version: $(shell $(CXX) --version))
538 USE_PTHREAD = YES
539 USE_COVERAGE = YES
540 ifeq ($(OSFAMILY), macosx)
541 USE_PTHREAD =
542 endif
543 ifdef CYGWIN
544 USE_PTHREAD =
545 USE_COVERAGE =
546 endif
548 ifdef USE_PTHREAD
549 COMMON_FLAGS += -pthread
550 endif
552 # Flags passed to the C compiler.
553 C_FLAGS = $(COMMON_FLAGS) \
554 -std=gnu99
556 # Flags passed to the C++ compiler.
557 CXX_FLAGS = $(COMMON_FLAGS) \
558 -std=gnu++11
560 # Compiler flags for coverage instrumentation
561 ifdef USE_COVERAGE
562 COVERAGE_FLAGS := --coverage
563 endif
565 C_FLAGS += $(COVERAGE_FLAGS)
566 CXX_FLAGS += $(COVERAGE_FLAGS)
568 C_FLAGS += -D_GNU_SOURCE
570 # Set up the parameter group linker flags according to OS
571 ifeq ($(OSFAMILY), macosx)
572 LDFLAGS += -Wl,-map,$(OBJECT_DIR)/$@.map
573 else
574 LDFLAGS += -Wl,-T,$(TEST_DIR)/pg.ld -Wl,-Map,$(OBJECT_DIR)/$@.map
575 endif
577 # Gather up all of the tests.
578 TEST_SRCS = $(sort $(wildcard $(TEST_DIR)/*.cc))
579 TEST_BASENAMES = $(TEST_SRCS:$(TEST_DIR)/%.cc=%)
580 TESTS_TARGET_SPECIFIC = $(foreach test,$(TEST_BASENAMES),$(if $($(test)_EXPAND),$(test)))
581 TESTS_TARGET_SPECIFIC_EXPANDED = $(foreach test,$(TESTS_TARGET_SPECIFIC),$(foreach \
582 target,$(filter-out $($(test)_BLACKLIST),$(VALID_TARGETS)),$(test).$(target)))
584 TESTS = $(foreach test,$(TEST_BASENAMES),$(if $($(test)_EXPAND),,$(test)))
585 TESTS_ALL = $(TESTS) $(TESTS_TARGET_SPECIFIC_EXPANDED)
586 TESTS_REPRESENTATIVE = $(TESTS) $(foreach test,$(TESTS_TARGET_SPECIFIC), \
587 $(test).$(word 1,$(filter-out $($(test)_BLACKLIST),$(VALID_TARGETS))))
589 # All Google Test headers. Usually you shouldn't change this
590 # definition.
591 GTEST_HEADERS = $(GTEST_DIR)/inc/gtest/*.h
593 ## V : Set verbosity level based on the V= parameter
594 ## V=0 Low
595 ## V=1 High
596 include ../../make/build_verbosity.mk
598 # House-keeping build targets.
600 ## test : Build and run the non target specific Unit Tests (default goal)
601 test: $(TESTS:%=test_%)
603 ## test-all : Build and run all Unit Tests
604 test-all: $(TESTS_ALL:%=test_%)
606 ## test-representative : Build and run a representative subset of the Unit Tests (i.e. run every expanded test only for the first target)
607 test-representative: $(TESTS_REPRESENTATIVE:%=test_%)
609 ## junittest : Build and run the Unit Tests, producing Junit XML result files."
610 junittest: EXEC_OPTS = "--gtest_output=xml:$<_results.xml"
611 junittest: $(TESTS:%=test_%)
615 ## help : print this help message and exit
616 ## what : print this help message and exit
617 ## usage : print this help message and exit
618 help what usage: Makefile
619 @echo ""
620 @echo "Makefile for Unit Tests"
621 @echo ""
622 @echo "Usage:"
623 @echo " make [goal] "
624 @echo ""
625 @echo "Valid goals are:"
626 @echo ""
627 @sed -n 's/^## //p' $<
628 @echo ""
629 @echo "Any of the Unit Test programs can be used as goals to build:"
630 @$(foreach test, $(TESTS), echo " $(OBJECT_DIR)/$(test)/$(test)";)
631 @echo ""
632 @echo "Any of the Unit Test programs (except for target specific unit tests) can be used as goals to build and run:"
633 @$(foreach test, $(TESTS), echo " test_$(test)";)
635 versions:
636 @echo "C compiler: $(CC): $(CC_VERSION)"
637 @echo "C++ compiler: $(CXX): $(CXX_VERSION)"
639 ## clean : Cleanup the UnitTest binaries.
640 clean :
641 rm -rf $(OBJECT_DIR)
644 # Builds gtest.a and gtest_main.a.
646 # Usually you shouldn't tweak such internal variables, indicated by a
647 # trailing _.
648 GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/inc/gtest/*.h $(GTEST_HEADERS)
650 # For simplicity and to avoid depending on Google Test's
651 # implementation details, the dependencies specified below are
652 # conservative and not optimized. This is fine as Google Test
653 # compiles fast and for ordinary users its source rarely changes.
654 $(OBJECT_DIR)/gtest-all.o : $(GTEST_SRCS_)
655 @echo "compiling $@" "$(STDOUT)"
656 @mkdir -p $(dir $@)
657 $(V1) $(CXX) $(CXX_FLAGS) -I$(GTEST_DIR) -Wno-missing-field-initializers -Wno-unused-const-variable -c \
658 $(GTEST_DIR)/src/gtest-all.cc -o $@
660 $(OBJECT_DIR)/gtest_main.o : $(GTEST_SRCS_)
661 @echo "compiling $@" "$(STDOUT)"
662 @mkdir -p $(dir $@)
663 $(V1) $(CXX) $(CXX_FLAGS) -I$(GTEST_DIR) -c \
664 $(GTEST_DIR)/src/gtest_main.cc -o $@
666 $(OBJECT_DIR)/gtest.a : $(OBJECT_DIR)/gtest-all.o
667 @echo "linking $@" "$(STDOUT)"
668 $(V1) $(AR) $(ARFLAGS) $@ $^
670 $(OBJECT_DIR)/gtest_main.a : $(OBJECT_DIR)/gtest-all.o $(OBJECT_DIR)/gtest_main.o
671 @echo "linking $@" "$(STDOUT)"
672 $(V1) $(AR) $(ARFLAGS) $@ $^
674 -include $(OBJECT_DIR)/gtest-all.d \
675 $(OBJECT_DIR)/gtest_main.d
678 # includes in test dir must override includes in user dir, unless the user
679 # specifies a list of endorsed directories in ${target}_INCLUDE_DIRS.
680 test_include_dirs = $1 $(TEST_DIR) $(USER_DIR)
681 test_cflags = $(addprefix -I,$(call test_include_dirs,$1))
684 # target name extractor
685 # param $1 = expanded test name in the form of test.target
686 target = $(1:$(basename $1).%=%)
688 # canned recipe for all test builds
690 # variable expansion rules of thumb (number of $'s):
691 # * parameters: one $, e.g. $1
692 # * statically accessed variables set elsewhere: one $, e.g. $(C_FLAGS)
693 # * dynamically accessed variables set elsewhere: one $, e.g. $($1_SRC)
694 # * make functions accessing only the above: one $, e.g. $(basename $1)
695 # * dynamically set and accessed variables: two $, e.g. $$($1_OBJS)
696 # * make functions accessing dynamically set variables: two $,
697 # e.g. $$(call test_cflags,$$($1_INCLUDE_DIRS))
699 # param $1 = plain test name for global tests, test.target for per-target tests
700 define test-specific-stuff
702 ifeq ($1,$(basename $1))
703 # standard global test
704 $1_OBJS = $(patsubst \
705 $(TEST_DIR)/%,$(OBJECT_DIR)/$1/%,$(patsubst \
706 $(USER_DIR)/%,$(OBJECT_DIR)/$1/%,$($1_SRC:=.o)))
707 else
708 # test executed for each target, $1 has the form of test.target
709 $1_SRC = $(addsuffix .o,$(call $(basename $1)_SRC,$(call target,$1)))
710 $1_OBJS = $$(patsubst \
711 $(TEST_DIR)/%,$(OBJECT_DIR)/$1/%,$$(patsubst \
712 $(USER_DIR)/%,$(OBJECT_DIR)/$1/%,$$(patsubst \
713 $(TARGET_DIR)/$(call get_base_target,$(call target,$1))/%,$(OBJECT_DIR)/$1/%,$$($1_SRC))))
714 $1_DEFINES = $(call $(basename $1)_DEFINES,$(call target,$1))
715 $1_INCLUDE_DIRS = $(call $(basename $1)_INCLUDE_DIRS,$(call target,$1))
716 endif
718 # $$(info $1 -v-v-------)
719 # $$(info $1_SRC: $$($1_SRC))
720 # $$(info $1_OBJS: $$($1_OBJS))
721 # $$(info $1 -^-^-------)
723 # include generated dependencies
724 -include $$($1_OBJS:.o=.d)
725 -include $(OBJECT_DIR)/$1/$(basename $1).d
727 $(OBJECT_DIR)/$1/%.c.o: $(USER_DIR)/%.c
728 @echo "compiling $$<" "$(STDOUT)"
729 $(V1) mkdir -p $$(dir $$@)
730 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
731 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
732 -c $$< -o $$@
734 $(OBJECT_DIR)/$1/%.c.o: $(TEST_DIR)/%.c
735 @echo "compiling test c file: $$<" "$(STDOUT)"
736 $(V1) mkdir -p $$(dir $$@)
737 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
738 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
739 -c $$< -o $$@
741 ifneq ($1,$(basename $1))
742 # per-target tests may compile files from the target directory
743 $(OBJECT_DIR)/$1/%.c.o: $(TARGET_DIR)/$(call get_base_target,$(call target,$1))/%.c
744 @echo "compiling target c file: $$<" "$(STDOUT)"
745 $(V1) mkdir -p $$(dir $$@)
746 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
747 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
748 -c $$< -o $$@
749 endif
751 $(OBJECT_DIR)/$1/$(basename $1).o: $(TEST_DIR)/$(basename $1).cc
752 @echo "compiling $$<" "$(STDOUT)"
753 $(V1) mkdir -p $$(dir $$@)
754 $(V1) $(CXX) $(CXX_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
755 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
756 -c $$< -o $$@
758 $(OBJECT_DIR)/$1/$(basename $1): $$($1_OBJS) \
759 $(OBJECT_DIR)/$1/$(basename $1).o \
760 $(OBJECT_DIR)/gtest_main.a
762 @echo "linking $$@" "$(STDOUT)"
763 $(V1) mkdir -p $(dir $$@)
764 $(V1) $(CXX) $(CXX_FLAGS) $(LDFLAGS) $$^ -o $$@
766 test_$1: $(OBJECT_DIR)/$1/$(basename $1)
767 $(V1) $$< $$(EXEC_OPTS) "$(STDOUT)" && echo "running $$@: PASS"
769 endef
772 ifeq ($(MAKECMDGOALS),test-all)
773 $(eval $(foreach test,$(TESTS_ALL),$(call test-specific-stuff,$(test))))
774 else
775 ifeq ($(MAKECMDGOALS),test-representative)
776 $(eval $(foreach test,$(TESTS_REPRESENTATIVE),$(call test-specific-stuff,$(test))))
777 else
778 $(eval $(foreach test,$(TESTS),$(call test-specific-stuff,$(test))))
779 endif
780 endif
782 $(foreach test,$(TESTS_ALL),$(if $($(basename $(test))_SRC),,$(error \
783 Test 'unit/$(basename $(test)).cc' has no '$(basename $(test))_SRC' variable defined)))
784 $(foreach var,$(filter-out TARGET_SRC,$(filter %_SRC,$(.VARIABLES))),$(if $(filter $(var:_SRC=)%,$(TESTS_ALL)),,$(error \
785 Variable '$(var)' has no 'unit/$(var:_SRC=).cc' test)))
788 target_list:
789 @echo ========== BASE TARGETS ==========
790 @echo $(BASE_TARGETS)
791 @echo ========== ALT TARGETS ==========
792 @echo $(ALT_TARGETS)
793 @echo ========== VALID_TARGETS ==========
794 @echo $(VALID_TARGETS)
795 @echo ========== BASE/ALT PAIRS ==========
796 @echo $(BASE_ALT_PAIRS)
797 @echo ========== ALT/BASE MAPPING ==========
798 @echo $(foreach target,$(ALT_TARGETS),$(target)\>$(call get_base_target,$(target)))
799 @echo ========== ALT/BASE FULL MAPPING ==========
800 @echo $(foreach target,$(VALID_TARGETS),$(target)\>$(call get_base_target,$(target)))