Updated and Validated
[betaflight.git] / src / test / Makefile
blobe8e77ac86ac23074a0a4be7efa6751df89ce2384
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/printf.c \
110 $(USER_DIR)/config/feature.c \
111 $(USER_DIR)/pg/pg.c \
112 $(USER_DIR)/common/typeconversion.c
114 cli_unittest_DEFINES := \
115 USE_OSD= \
116 USE_CLI= \
117 SystemCoreClock=1000000
119 cms_unittest_SRC := \
120 $(USER_DIR)/cms/cms.c \
121 $(USER_DIR)/cms/cms_menu_saveexit.c \
122 $(USER_DIR)/common/typeconversion.c \
123 $(USER_DIR)/drivers/display.c
126 common_filter_unittest_SRC := \
127 $(USER_DIR)/common/filter.c \
128 $(USER_DIR)/common/maths.c
131 encoding_unittest_SRC := \
132 $(USER_DIR)/common/encoding.c
135 flight_failsafe_unittest_SRC := \
136 $(USER_DIR)/common/bitarray.c \
137 $(USER_DIR)/fc/rc_modes.c \
138 $(USER_DIR)/fc/runtime_config.c \
139 $(USER_DIR)/flight/failsafe.c
141 flight_failsafe_unittest_DEFINES := \
142 USE_GPS_RESCUE=
145 flight_imu_unittest_SRC := \
146 $(USER_DIR)/common/bitarray.c \
147 $(USER_DIR)/common/maths.c \
148 $(USER_DIR)/config/feature.c \
149 $(USER_DIR)/fc/rc_modes.c \
150 $(USER_DIR)/flight/position.c \
151 $(USER_DIR)/flight/imu.c
154 flight_mixer_unittest := \
155 $(USER_DIR)/flight/mixer.c \
156 $(USER_DIR)/flight/servos.c \
157 $(USER_DIR)/common/maths.c
160 gps_conversion_unittest_SRC := \
161 $(USER_DIR)/common/gps_conversion.c
164 io_serial_unittest_SRC := \
165 $(USER_DIR)/io/serial.c \
166 $(USER_DIR)/drivers/serial_pinconfig.c
169 ledstrip_unittest_SRC := \
170 $(USER_DIR)/common/bitarray.c \
171 $(USER_DIR)/fc/rc_modes.c \
172 $(USER_DIR)/io/ledstrip.c
174 ledstrip_unittest_DEFINES := \
175 USE_LED_STRIP=
178 maths_unittest_SRC := \
179 $(USER_DIR)/common/maths.c
182 motor_output_unittest_SRC := \
183 $(USER_DIR)/drivers/dshot.c
186 osd_unittest_SRC := \
187 $(USER_DIR)/osd/osd.c \
188 $(USER_DIR)/osd/osd_elements.c \
189 $(USER_DIR)/osd/osd_warnings.c \
190 $(USER_DIR)/common/typeconversion.c \
191 $(USER_DIR)/drivers/display.c \
192 $(USER_DIR)/common/maths.c \
193 $(USER_DIR)/common/printf.c \
194 $(USER_DIR)/common/time.c \
195 $(USER_DIR)/common/filter.c \
196 $(USER_DIR)/fc/runtime_config.c
198 osd_unittest_DEFINES := \
199 USE_OSD= \
200 USE_GPS= \
201 USE_RTC_TIME= \
202 USE_ADC_INTERNAL=
204 link_quality_unittest_SRC := \
205 $(USER_DIR)/osd/osd.c \
206 $(USER_DIR)/osd/osd_elements.c \
207 $(USER_DIR)/osd/osd_warnings.c \
208 $(USER_DIR)/common/typeconversion.c \
209 $(USER_DIR)/drivers/display.c \
210 $(USER_DIR)/drivers/serial.c \
211 $(USER_DIR)/common/crc.c \
212 $(USER_DIR)/common/maths.c \
213 $(USER_DIR)/common/printf.c \
214 $(USER_DIR)/common/streambuf.c \
215 $(USER_DIR)/common/time.c \
216 $(USER_DIR)/fc/runtime_config.c \
217 $(USER_DIR)/common/bitarray.c \
218 $(USER_DIR)/fc/rc_modes.c \
219 $(USER_DIR)/rx/rx.c \
220 $(USER_DIR)/pg/pg.c \
221 $(USER_DIR)/rx/crsf.c \
222 $(USER_DIR)/pg/rx.c
224 link_quality_unittest_DEFINES := \
225 USE_OSD= \
226 USE_CRSF_LINK_STATISTICS= \
227 USE_RX_LINK_QUALITY_INFO=
229 pg_unittest_SRC := \
230 $(USER_DIR)/pg/pg.c
233 rc_controls_unittest_SRC := \
234 $(USER_DIR)/fc/rc_controls.c \
235 $(USER_DIR)/pg/pg.c \
236 $(USER_DIR)/common/bitarray.c \
237 $(USER_DIR)/common/maths.c \
238 $(USER_DIR)/fc/rc_adjustments.c \
239 $(USER_DIR)/fc/rc_modes.c
242 rx_crsf_unittest_SRC := \
243 $(USER_DIR)/rx/crsf.c \
244 $(USER_DIR)/common/crc.c \
245 $(USER_DIR)/common/printf.c \
246 $(USER_DIR)/common/typeconversion.c \
247 $(USER_DIR)/common/streambuf.c \
248 $(USER_DIR)/drivers/serial.c
251 rx_ibus_unittest_SRC := \
252 $(USER_DIR)/rx/ibus.c
255 rx_ranges_unittest_SRC := \
256 $(USER_DIR)/common/bitarray.c \
257 $(USER_DIR)/common/maths.c \
258 $(USER_DIR)/fc/rc_modes.c \
259 $(USER_DIR)/rx/rx.c \
260 $(USER_DIR)/pg/pg.c \
261 $(USER_DIR)/pg/rx.c
264 rx_rx_unittest_SRC := \
265 $(USER_DIR)/rx/rx.c \
266 $(USER_DIR)/fc/rc_modes.c \
267 $(USER_DIR)/common/bitarray.c \
268 $(USER_DIR)/common/maths.c \
269 $(USER_DIR)/config/feature.c \
270 $(USER_DIR)/pg/rx.c
272 rx_sumd_unittest_SRC := \
273 $(USER_DIR)/common/crc.c \
274 $(USER_DIR)/common/streambuf.c \
275 $(USER_DIR)/rx/sumd.c
277 scheduler_unittest_SRC := \
278 $(USER_DIR)/scheduler/scheduler.c \
279 $(USER_DIR)/common/crc.c \
280 $(USER_DIR)/common/streambuf.c
282 scheduler_unittest_DEFINES := \
283 USE_OSD=
285 sensor_gyro_unittest_SRC := \
286 $(USER_DIR)/sensors/gyro.c \
287 $(USER_DIR)/sensors/gyro_init.c \
288 $(USER_DIR)/sensors/boardalignment.c \
289 $(USER_DIR)/common/filter.c \
290 $(USER_DIR)/common/maths.c \
291 $(USER_DIR)/common/sensor_alignment.c \
292 $(USER_DIR)/drivers/accgyro/accgyro_fake.c \
293 $(USER_DIR)/drivers/accgyro/gyro_sync.c \
294 $(USER_DIR)/pg/pg.c \
295 $(USER_DIR)/pg/gyrodev.c
297 telemetry_crsf_unittest_SRC := \
298 $(USER_DIR)/rx/crsf.c \
299 $(USER_DIR)/telemetry/crsf.c \
300 $(USER_DIR)/common/crc.c \
301 $(USER_DIR)/common/maths.c \
302 $(USER_DIR)/common/streambuf.c \
303 $(USER_DIR)/common/gps_conversion.c \
304 $(USER_DIR)/common/printf.c \
305 $(USER_DIR)/common/typeconversion.c \
306 $(USER_DIR)/fc/runtime_config.c
308 telemetry_crsf_unittest_DEFINES := \
309 FLASH_SIZE=128 \
310 STM32F10X_MD= \
311 __TARGET__="TEST" \
312 __REVISION__="revision"
315 telemetry_crsf_msp_unittest_SRC := \
316 $(USER_DIR)/rx/crsf.c \
317 $(USER_DIR)/build/atomic.c \
318 $(USER_DIR)/common/crc.c \
319 $(USER_DIR)/common/streambuf.c \
320 $(USER_DIR)/common/printf.c \
321 $(USER_DIR)/common/streambuf.c \
322 $(USER_DIR)/drivers/serial.c \
323 $(USER_DIR)/common/typeconversion.c \
324 $(USER_DIR)/telemetry/crsf.c \
325 $(USER_DIR)/common/gps_conversion.c \
326 $(USER_DIR)/telemetry/msp_shared.c \
327 $(USER_DIR)/fc/runtime_config.c
329 telemetry_crsf_msp_unittest_DEFINES := \
330 USE_MSP_OVER_TELEMETRY=
333 telemetry_hott_unittest_SRC := \
334 $(USER_DIR)/telemetry/hott.c \
335 $(USER_DIR)/common/gps_conversion.c
338 telemetry_ibus_unittest_SRC := \
339 $(USER_DIR)/telemetry/ibus_shared.c \
340 $(USER_DIR)/telemetry/ibus.c
342 timer_definition_unittest_EXPAND := yes
344 # SITL is a simulator with empty timerHardware and many hearders in target.c.
345 timer_definition_unittest_BLACKLIST := SITL
347 timer_definition_unittest_SRC = \
348 $(TARGET_DIR)/$(call get_base_target,$1)/target.c
350 timer_definition_unittest_DEFINES = \
351 TARGET=$1 \
352 BASE_TARGET=$(call get_base_target,$1)
354 timer_definition_unittest_INCLUDE_DIRS = \
355 $(TEST_DIR)/timer_definition_unittest.include \
356 $(TARGET_DIR)/$(call get_base_target,$1)
358 transponder_ir_unittest_SRC := \
359 $(USER_DIR)/drivers/transponder_ir_ilap.c \
360 $(USER_DIR)/drivers/transponder_ir_arcitimer.c
362 ws2811_unittest_SRC := \
363 $(USER_DIR)/drivers/light_ws2811strip.c
365 huffman_unittest_SRC := \
366 $(USER_DIR)/common/huffman.c \
367 $(USER_DIR)/common/huffman_table.c
369 huffman_unittest_DEFINES := \
370 USE_HUFFMAN=
372 rcdevice_unittest_SRC := \
373 $(USER_DIR)/common/crc.c \
374 $(USER_DIR)/common/bitarray.c \
375 $(USER_DIR)/fc/rc_modes.c \
376 $(USER_DIR)/io/rcdevice.c \
377 $(USER_DIR)/io/rcdevice_cam.c \
378 $(USER_DIR)/pg/pg.c \
380 pid_unittest_SRC := \
381 $(USER_DIR)/common/filter.c \
382 $(USER_DIR)/common/maths.c \
383 $(USER_DIR)/drivers/accgyro/gyro_sync.c \
384 $(USER_DIR)/fc/controlrate_profile.c \
385 $(USER_DIR)/fc/runtime_config.c \
386 $(USER_DIR)/flight/pid.c \
387 $(USER_DIR)/flight/pid_init.c \
388 $(USER_DIR)/pg/pg.c
390 pid_unittest_DEFINES := \
391 USE_ITERM_RELAX= \
392 USE_RC_SMOOTHING_FILTER= \
393 USE_ABSOLUTE_CONTROL= \
394 USE_LAUNCH_CONTROL= \
395 USE_FEEDFORWARD=
397 rcdevice_unittest_DEFINES := \
398 USE_RCDEVICE=
400 vtx_unittest_SRC := \
401 $(USER_DIR)/fc/core.c \
402 $(USER_DIR)/fc/dispatch.c \
403 $(USER_DIR)/fc/rc_controls.c \
404 $(USER_DIR)/fc/rc_modes.c \
405 $(USER_DIR)/fc/runtime_config.c \
406 $(USER_DIR)/drivers/vtx_common.c \
407 $(USER_DIR)/drivers/vtx_table.c \
408 $(USER_DIR)/io/vtx_control.c \
409 $(USER_DIR)/io/vtx.c \
410 $(USER_DIR)/common/bitarray.c
412 vtx_unittest_DEFINES := \
413 USE_VTX_COMMON= \
414 USE_VTX_CONTROL= \
415 USE_VTX_SMARTAUDIO=
417 rx_spi_spektrum_unittest_SRC := \
418 $(USER_DIR)/rx/cyrf6936_spektrum.c
420 rx_spi_spektrum_unittest_DEFINES := \
421 USE_RX_SPI= \
422 USE_RX_SPEKTRUM=
424 rx_spi_expresslrs_unittest_SRC := \
425 $(USER_DIR)/pg/rx_spi_expresslrs.c \
426 $(USER_DIR)/rx/expresslrs_common.c \
427 $(USER_DIR)/rx/expresslrs.c \
428 $(USER_DIR)/build/atomic.c \
430 rx_spi_expresslrs_unittest_DEFINES := \
431 USE_RX_SPI= \
432 USE_RX_EXPRESSLRS= \
433 USE_RX_SX1280= \
434 USE_RX_SX127X= \
436 rx_spi_expresslrs_telemetry_unittest_SRC := \
437 $(USER_DIR)/rx/crsf.c \
438 $(USER_DIR)/telemetry/crsf.c \
439 $(USER_DIR)/common/crc.c \
440 $(USER_DIR)/common/maths.c \
441 $(USER_DIR)/common/streambuf.c \
442 $(USER_DIR)/common/gps_conversion.c \
443 $(USER_DIR)/common/printf.c \
444 $(USER_DIR)/common/typeconversion.c \
445 $(USER_DIR)/rx/expresslrs_telemetry.c \
446 $(USER_DIR)/build/atomic.c \
447 $(USER_DIR)/telemetry/msp_shared.c \
449 rx_spi_expresslrs_telemetry_unittest_DEFINES := \
450 USE_RX_EXPRESSLRS= \
451 USE_GPS= \
452 USE_MSP_OVER_TELEMETRY= \
454 # Please tweak the following variable definitions as needed by your
455 # project, except GTEST_HEADERS, which you can use in your own targets
456 # but shouldn't modify.
458 # Points to the root of Google Test, relative to where this file is.
459 # Remember to tweak this if you move this file.
460 GTEST_DIR = ../../lib/test/gtest
462 # Use clang/clang++ by default
464 CC := clang-10
465 CXX := clang++-10
466 ifeq ($(shell which $(CC) 2>/dev/null),)
467 $(info Falling back to 'clang'.)
468 CC := clang
469 CXX := clang++
470 endif
472 #CC := gcc
473 #CXX := g++
475 # These flags are needed for clang 10 (linux / MacOS) to make test work:
476 # -Wno-c99-extensions
477 # -Wno-reorder
479 COMMON_FLAGS = \
480 -g \
481 -Wall \
482 -Wextra \
483 -Werror \
484 -Wno-error=unused-command-line-argument \
485 -ggdb3 \
486 -O0 \
487 -DUNIT_TEST \
488 -isystem $(GTEST_DIR)/inc \
489 -MMD -MP \
490 -Wno-c99-extensions \
491 -Wno-reorder
493 CC_VERSION = $(shell $(CC) -dumpversion)
494 CXX_VERSION = $(shell $(CXX) -dumpversion)
496 ifeq ($(shell $(CC) -v 2>&1 | grep -q "clang version" && echo "clang"),clang)
498 # Please revisit versions when new clang version arrive. Supported versions: { Linux / OSX: 7 - 13 }
499 # Travis reports CC_VERSION of 4.2.1
500 CC_VERSION_MAJOR := $(firstword $(subst ., ,$(CC_VERSION)))
501 CC_VERSION_CHECK_MIN := 7
502 CC_VERSION_CHECK_MAX := 13
504 # Added flags for clang 11 - 13 are not backwards compatible
505 ifeq ($(shell expr $(CC_VERSION_MAJOR) \> 10 \& $(CC_VERSION_MAJOR) \< 14), 1)
506 COMMON_FLAGS += \
507 -fcommon \
508 -Wno-void-pointer-to-int-cast
509 endif
511 ifeq ($(shell expr $(CC_VERSION_MAJOR) \< $(CC_VERSION_CHECK_MIN) \| $(CC_VERSION_MAJOR) \> $(CC_VERSION_CHECK_MAX)),1)
512 $(error $(CC) $(CC_VERSION) is not supported. The officially supported version of clang is 'clang-10'. 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).)
513 endif
515 COMMON_FLAGS += -fblocks
516 ifndef CYGWIN
517 ifneq ($(OSFAMILY), macosx)
518 LDFLAGS += -lBlocksRuntime
519 endif
520 endif
521 endif
523 $(info CC version: $(shell $(CC) --version))
524 $(info CXX version: $(shell $(CXX) --version))
526 USE_PTHREAD = YES
527 USE_COVERAGE = YES
528 ifeq ($(OSFAMILY), macosx)
529 USE_PTHREAD =
530 endif
531 ifdef CYGWIN
532 USE_PTHREAD =
533 USE_COVERAGE =
534 endif
536 ifdef USE_PTHREAD
537 COMMON_FLAGS += -pthread
538 endif
540 # Flags passed to the C compiler.
541 C_FLAGS = $(COMMON_FLAGS) \
542 -std=gnu99
544 # Flags passed to the C++ compiler.
545 CXX_FLAGS = $(COMMON_FLAGS) \
546 -std=gnu++11
548 # Compiler flags for coverage instrumentation
549 ifdef USE_COVERAGE
550 COVERAGE_FLAGS := --coverage
551 endif
553 C_FLAGS += $(COVERAGE_FLAGS)
554 CXX_FLAGS += $(COVERAGE_FLAGS)
556 C_FLAGS += -D_GNU_SOURCE
558 # Set up the parameter group linker flags according to OS
559 ifeq ($(OSFAMILY), macosx)
560 LDFLAGS += -Wl,-map,$(OBJECT_DIR)/$@.map
561 else
562 LDFLAGS += -Wl,-T,$(TEST_DIR)/pg.ld -Wl,-Map,$(OBJECT_DIR)/$@.map
563 endif
565 # Gather up all of the tests.
566 TEST_SRCS = $(sort $(wildcard $(TEST_DIR)/*.cc))
567 TEST_BASENAMES = $(TEST_SRCS:$(TEST_DIR)/%.cc=%)
568 TESTS_TARGET_SPECIFIC = $(foreach test,$(TEST_BASENAMES),$(if $($(test)_EXPAND),$(test)))
569 TESTS_TARGET_SPECIFIC_EXPANDED = $(foreach test,$(TESTS_TARGET_SPECIFIC),$(foreach \
570 target,$(filter-out $($(test)_BLACKLIST),$(VALID_TARGETS)),$(test).$(target)))
572 TESTS = $(foreach test,$(TEST_BASENAMES),$(if $($(test)_EXPAND),,$(test)))
573 TESTS_ALL = $(TESTS) $(TESTS_TARGET_SPECIFIC_EXPANDED)
574 TESTS_REPRESENTATIVE = $(TESTS) $(foreach test,$(TESTS_TARGET_SPECIFIC), \
575 $(test).$(word 1,$(filter-out $($(test)_BLACKLIST),$(VALID_TARGETS))))
577 # All Google Test headers. Usually you shouldn't change this
578 # definition.
579 GTEST_HEADERS = $(GTEST_DIR)/inc/gtest/*.h
581 ## V : Set verbosity level based on the V= parameter
582 ## V=0 Low
583 ## V=1 High
584 include ../../make/build_verbosity.mk
586 # House-keeping build targets.
588 ## test : Build and run the non target specific Unit Tests (default goal)
589 test: $(TESTS:%=test_%)
591 ## test-all : Build and run all Unit Tests
592 test-all: $(TESTS_ALL:%=test_%)
594 ## test-representative : Build and run a representative subset of the Unit Tests (i.e. run every expanded test only for the first target)
595 test-representative: $(TESTS_REPRESENTATIVE:%=test_%)
597 ## junittest : Build and run the Unit Tests, producing Junit XML result files."
598 junittest: EXEC_OPTS = "--gtest_output=xml:$<_results.xml"
599 junittest: $(TESTS:%=test_%)
603 ## help : print this help message and exit
604 ## what : print this help message and exit
605 ## usage : print this help message and exit
606 help what usage: Makefile
607 @echo ""
608 @echo "Makefile for Unit Tests"
609 @echo ""
610 @echo "Usage:"
611 @echo " make [goal] "
612 @echo ""
613 @echo "Valid goals are:"
614 @echo ""
615 @sed -n 's/^## //p' $<
616 @echo ""
617 @echo "Any of the Unit Test programs can be used as goals to build:"
618 @$(foreach test, $(TESTS), echo " $(OBJECT_DIR)/$(test)/$(test)";)
619 @echo ""
620 @echo "Any of the Unit Test programs (except for target specific unit tests) can be used as goals to build and run:"
621 @$(foreach test, $(TESTS), echo " test_$(test)";)
623 versions:
624 @echo "C compiler: $(CC): $(CC_VERSION)"
625 @echo "C++ compiler: $(CXX): $(CXX_VERSION)"
627 ## clean : Cleanup the UnitTest binaries.
628 clean :
629 rm -rf $(OBJECT_DIR)
632 # Builds gtest.a and gtest_main.a.
634 # Usually you shouldn't tweak such internal variables, indicated by a
635 # trailing _.
636 GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/inc/gtest/*.h $(GTEST_HEADERS)
638 # For simplicity and to avoid depending on Google Test's
639 # implementation details, the dependencies specified below are
640 # conservative and not optimized. This is fine as Google Test
641 # compiles fast and for ordinary users its source rarely changes.
642 $(OBJECT_DIR)/gtest-all.o : $(GTEST_SRCS_)
643 @echo "compiling $@" "$(STDOUT)"
644 @mkdir -p $(dir $@)
645 $(V1) $(CXX) $(CXX_FLAGS) -I$(GTEST_DIR) -Wno-missing-field-initializers -Wno-unused-const-variable -c \
646 $(GTEST_DIR)/src/gtest-all.cc -o $@
648 $(OBJECT_DIR)/gtest_main.o : $(GTEST_SRCS_)
649 @echo "compiling $@" "$(STDOUT)"
650 @mkdir -p $(dir $@)
651 $(V1) $(CXX) $(CXX_FLAGS) -I$(GTEST_DIR) -c \
652 $(GTEST_DIR)/src/gtest_main.cc -o $@
654 $(OBJECT_DIR)/gtest.a : $(OBJECT_DIR)/gtest-all.o
655 @echo "linking $@" "$(STDOUT)"
656 $(V1) $(AR) $(ARFLAGS) $@ $^
658 $(OBJECT_DIR)/gtest_main.a : $(OBJECT_DIR)/gtest-all.o $(OBJECT_DIR)/gtest_main.o
659 @echo "linking $@" "$(STDOUT)"
660 $(V1) $(AR) $(ARFLAGS) $@ $^
662 -include $(OBJECT_DIR)/gtest-all.d \
663 $(OBJECT_DIR)/gtest_main.d
666 # includes in test dir must override includes in user dir, unless the user
667 # specifies a list of endorsed directories in ${target}_INCLUDE_DIRS.
668 test_include_dirs = $1 $(TEST_DIR) $(USER_DIR)
669 test_cflags = $(addprefix -I,$(call test_include_dirs,$1))
672 # target name extractor
673 # param $1 = expanded test name in the form of test.target
674 target = $(1:$(basename $1).%=%)
676 # canned recipe for all test builds
678 # variable expansion rules of thumb (number of $'s):
679 # * parameters: one $, e.g. $1
680 # * statically accessed variables set elsewhere: one $, e.g. $(C_FLAGS)
681 # * dynamically accessed variables set elsewhere: one $, e.g. $($1_SRC)
682 # * make functions accessing only the above: one $, e.g. $(basename $1)
683 # * dynamically set and accessed variables: two $, e.g. $$($1_OBJS)
684 # * make functions accessing dynamically set variables: two $,
685 # e.g. $$(call test_cflags,$$($1_INCLUDE_DIRS))
687 # param $1 = plain test name for global tests, test.target for per-target tests
688 define test-specific-stuff
690 ifeq ($1,$(basename $1))
691 # standard global test
692 $1_OBJS = $(patsubst \
693 $(TEST_DIR)/%,$(OBJECT_DIR)/$1/%,$(patsubst \
694 $(USER_DIR)/%,$(OBJECT_DIR)/$1/%,$($1_SRC:=.o)))
695 else
696 # test executed for each target, $1 has the form of test.target
697 $1_SRC = $(addsuffix .o,$(call $(basename $1)_SRC,$(call target,$1)))
698 $1_OBJS = $$(patsubst \
699 $(TEST_DIR)/%,$(OBJECT_DIR)/$1/%,$$(patsubst \
700 $(USER_DIR)/%,$(OBJECT_DIR)/$1/%,$$(patsubst \
701 $(TARGET_DIR)/$(call get_base_target,$(call target,$1))/%,$(OBJECT_DIR)/$1/%,$$($1_SRC))))
702 $1_DEFINES = $(call $(basename $1)_DEFINES,$(call target,$1))
703 $1_INCLUDE_DIRS = $(call $(basename $1)_INCLUDE_DIRS,$(call target,$1))
704 endif
706 # $$(info $1 -v-v-------)
707 # $$(info $1_SRC: $$($1_SRC))
708 # $$(info $1_OBJS: $$($1_OBJS))
709 # $$(info $1 -^-^-------)
711 # include generated dependencies
712 -include $$($1_OBJS:.o=.d)
713 -include $(OBJECT_DIR)/$1/$(basename $1).d
715 $(OBJECT_DIR)/$1/%.c.o: $(USER_DIR)/%.c
716 @echo "compiling $$<" "$(STDOUT)"
717 $(V1) mkdir -p $$(dir $$@)
718 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
719 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
720 -c $$< -o $$@
722 $(OBJECT_DIR)/$1/%.c.o: $(TEST_DIR)/%.c
723 @echo "compiling test c file: $$<" "$(STDOUT)"
724 $(V1) mkdir -p $$(dir $$@)
725 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
726 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
727 -c $$< -o $$@
729 ifneq ($1,$(basename $1))
730 # per-target tests may compile files from the target directory
731 $(OBJECT_DIR)/$1/%.c.o: $(TARGET_DIR)/$(call get_base_target,$(call target,$1))/%.c
732 @echo "compiling target c file: $$<" "$(STDOUT)"
733 $(V1) mkdir -p $$(dir $$@)
734 $(V1) $(CC) $(C_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
735 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
736 -c $$< -o $$@
737 endif
739 $(OBJECT_DIR)/$1/$(basename $1).o: $(TEST_DIR)/$(basename $1).cc
740 @echo "compiling $$<" "$(STDOUT)"
741 $(V1) mkdir -p $$(dir $$@)
742 $(V1) $(CXX) $(CXX_FLAGS) $$(call test_cflags,$$($1_INCLUDE_DIRS)) \
743 $$(foreach def,$$($1_DEFINES),-D $$(def)) \
744 -c $$< -o $$@
746 $(OBJECT_DIR)/$1/$(basename $1): $$($1_OBJS) \
747 $(OBJECT_DIR)/$1/$(basename $1).o \
748 $(OBJECT_DIR)/gtest_main.a
750 @echo "linking $$@" "$(STDOUT)"
751 $(V1) mkdir -p $(dir $$@)
752 $(V1) $(CXX) $(CXX_FLAGS) $(LDFLAGS) $$^ -o $$@
754 test_$1: $(OBJECT_DIR)/$1/$(basename $1)
755 $(V1) $$< $$(EXEC_OPTS) "$(STDOUT)" && echo "running $$@: PASS"
757 endef
760 ifeq ($(MAKECMDGOALS),test-all)
761 $(eval $(foreach test,$(TESTS_ALL),$(call test-specific-stuff,$(test))))
762 else
763 ifeq ($(MAKECMDGOALS),test-representative)
764 $(eval $(foreach test,$(TESTS_REPRESENTATIVE),$(call test-specific-stuff,$(test))))
765 else
766 $(eval $(foreach test,$(TESTS),$(call test-specific-stuff,$(test))))
767 endif
768 endif
770 $(foreach test,$(TESTS_ALL),$(if $($(basename $(test))_SRC),,$(error \
771 Test 'unit/$(basename $(test)).cc' has no '$(basename $(test))_SRC' variable defined)))
772 $(foreach var,$(filter-out TARGET_SRC,$(filter %_SRC,$(.VARIABLES))),$(if $(filter $(var:_SRC=)%,$(TESTS_ALL)),,$(error \
773 Variable '$(var)' has no 'unit/$(var:_SRC=).cc' test)))
776 target_list:
777 @echo ========== BASE TARGETS ==========
778 @echo $(BASE_TARGETS)
779 @echo ========== ALT TARGETS ==========
780 @echo $(ALT_TARGETS)
781 @echo ========== VALID_TARGETS ==========
782 @echo $(VALID_TARGETS)
783 @echo ========== BASE/ALT PAIRS ==========
784 @echo $(BASE_ALT_PAIRS)
785 @echo ========== ALT/BASE MAPPING ==========
786 @echo $(foreach target,$(ALT_TARGETS),$(target)\>$(call get_base_target,$(target)))
787 @echo ========== ALT/BASE FULL MAPPING ==========
788 @echo $(foreach target,$(VALID_TARGETS),$(target)\>$(call get_base_target,$(target)))