|
| 1 | +# This makefile creates a target to build and test each exercise using the provided example |
| 2 | +# implementation. The exercise target depends on a list of other targets that |
| 3 | +# 1) Copy the main test file and adjust it to include all tests, |
| 4 | +# 2) Copy the example implementation so that it is used, |
| 5 | +# 3) Copy the makefile and unittest framework, |
| 6 | +# 4) Build and test the exercise. |
| 7 | +# |
| 8 | +# Use `make <slug>` to build and test a specific exercise. Simply running `make` builds and |
| 9 | +# tests all available exercises. |
| 10 | + |
| 11 | + |
| 12 | +# Macro to create the rules for one exercise. |
| 13 | +# Arguments: |
| 14 | +# $(1) - slug |
| 15 | +# $(2) - slug with dashes replaced by underscores |
| 16 | +# $(3) - type of exercise: 'practice' or 'concept' |
| 17 | +# $(4) - name of test implementation: 'example' or 'exemplar' |
| 18 | +define setup_exercise |
| 19 | + |
| 20 | +# Copy the test file and removes TEST_IGNORE |
| 21 | +build/exercises/$(3)/$(1)/test_$(2).c: exercises/$(3)/$(1)/test_$(2).c |
| 22 | + @mkdir -p $$(dir $$@) |
| 23 | + @sed 's#TEST_IGNORE();#// &#' $$< > $$@ |
| 24 | + |
| 25 | +# Copy example/exemplar implementation |
| 26 | +build/exercises/$(3)/$(1)/$(2).c: exercises/$(3)/$(1)/.meta/$(4).c |
| 27 | + @mkdir -p $$(dir $$@) |
| 28 | + @cp $$< $$@ |
| 29 | + |
| 30 | +build/exercises/$(3)/$(1)/$(2).h: $$(wildcard exercises/$(3)/$(1)/.meta/$(4).h exercises/$(3)/$(1)/*.h) |
| 31 | + @# Copy all .h files in the exercises directory |
| 32 | + @cp exercises/$(3)/$(1)/*.h build/exercises/$(3)/$(1) || true |
| 33 | + @# If an example.h/exemplar.h file exists in .meta, replace slug.h with that one |
| 34 | + @if [ -e exercises/$(3)/$(1)/.meta/$(4).h ]; then \ |
| 35 | + cp exercises/$(3)/$(1)/.meta/$(4).h build/exercises/$(3)/$(1)/$(2).h; \ |
| 36 | + fi |
| 37 | + |
| 38 | +# Copy Makefile |
| 39 | +build/exercises/$(3)/$(1)/makefile: exercises/$(3)/$(1)/makefile |
| 40 | + @mkdir -p $$(dir $$@) |
| 41 | + @cp $$< $$@ |
| 42 | + |
| 43 | +# Copy the test framework |
| 44 | +build/exercises/$(3)/$(1)/test-framework: $$(wildcard exercises/$(3)/$(1)/test-framework/*) |
| 45 | + @mkdir -p $$@ |
| 46 | + @cp exercises/$(3)/$(1)/test-framework/* build/exercises/$(3)/$(1)/test-framework/ |
| 47 | + |
| 48 | +INPUT_FILE_TARGETS = \ |
| 49 | + build/exercises/$(3)/$(1)/test_$(2).c \ |
| 50 | + build/exercises/$(3)/$(1)/$(2).c \ |
| 51 | + build/exercises/$(3)/$(1)/$(2).h \ |
| 52 | + build/exercises/$(3)/$(1)/makefile \ |
| 53 | + build/exercises/$(3)/$(1)/test-framework |
| 54 | + |
| 55 | +# Build the exercise. |
| 56 | +build/exercises/$(3)/$(1)/tests.out: $$(INPUT_FILE_TARGETS) |
| 57 | + $$(MAKE) -C build/exercises/$(3)/$(1) tests.out |
| 58 | + |
| 59 | +# Run the exercise's test binary and create a stamp file if all tests pass |
| 60 | +build/exercises/$(3)/$(1)/tests-passed.stamp: build/exercises/$(3)/$(1)/tests.out |
| 61 | + @rm -f build/exercises/$(3)/$(1)/tests-passed.stamp |
| 62 | + @build/exercises/$(3)/$(1)/tests.out && touch build/exercises/$(3)/$(1)/tests-passed.stamp |
| 63 | + |
| 64 | +# Build and run the memcheck variant |
| 65 | +# This is only a single target, since an exercise's makefile builds and runs memcheck as a single target |
| 66 | +build/exercises/$(3)/$(1)/memcheck-passed.stamp: $$(INPUT_FILE_TARGETS) |
| 67 | + @rm -f build/exercises/$(3)/$(1)/memcheck-passed.stamp |
| 68 | + $$(MAKE) -C build/exercises/$(3)/$(1) memcheck && touch build/exercises/$(3)/$(1)/memcheck-passed.stamp |
| 69 | + |
| 70 | +# Top-level target for an exercise. It depends on the stamp files for the actual tests and the memcheck |
| 71 | +# run. Only when both stamp files are present will this target be considered "done", so as long as there |
| 72 | +# are some errors, building that target will trigger a re-run of the tests or memcheck binary. |
| 73 | +.PHONY: $(1) |
| 74 | +$(1): build/exercises/$(3)/$(1)/tests-passed.stamp build/exercises/$(3)/$(1)/memcheck-passed.stamp |
| 75 | + |
| 76 | +# Run `make clean` for this exercise. |
| 77 | +.PHONY: $(1)-clean |
| 78 | +$(1)-clean: $$(INPUT_FILE_TARGETS) |
| 79 | + $$(MAKE) -C build/exercises/$(3)/$(1) clean |
| 80 | + |
| 81 | +endef |
| 82 | + |
| 83 | +PRACTICE_EXERCISES := $(notdir $(wildcard exercises/practice/*)) |
| 84 | +CONCEPT_EXERCISES := $(notdir $(wildcard exercises/concept/*)) |
| 85 | + |
| 86 | +all: practice concept |
| 87 | + |
| 88 | +.PHONY: practice |
| 89 | +practice: $(PRACTICE_EXERCISES) |
| 90 | + @if [ -z "$(PRACTICE_EXERCISES)" ]; then \ |
| 91 | + echo "No practice exercises found."; \ |
| 92 | + fi |
| 93 | + |
| 94 | +.PHONY: concept |
| 95 | +concept: $(CONCEPT_EXERCISES) |
| 96 | + @if [ -z "$(CONCEPT_EXERCISES)" ]; then \ |
| 97 | + echo "No concept exercises found."; \ |
| 98 | + fi |
| 99 | + |
| 100 | +# Instantiate the macro for each practice exercise to create targets for each exercise. |
| 101 | +$(foreach exercise,$(PRACTICE_EXERCISES),$(eval $(call setup_exercise,$(exercise),$(subst -,_,$(exercise)),practice,example))) |
| 102 | +$(foreach exercise,$(CONCEPT_EXERCISES),$(eval $(call setup_exercise,$(exercise),$(subst -,_,$(exercise)),concept,exemplar))) |
| 103 | + |
| 104 | +.PHONY: list-practice |
| 105 | +list-practice: |
| 106 | + @for exercise in $(PRACTICE_EXERCISES); do \ |
| 107 | + echo "$$exercise"; \ |
| 108 | + done |
| 109 | + |
| 110 | +.PHONY: list-concept |
| 111 | +list-concept: |
| 112 | + @for exercise in $(CONCEPT_EXERCISES); do \ |
| 113 | + echo "$$exercise"; \ |
| 114 | + done |
| 115 | + |
| 116 | +.PHONY: clean |
| 117 | +clean: |
| 118 | + rm -rf build |
| 119 | + |
| 120 | +.PHONY: help |
| 121 | +help: |
| 122 | + @echo "Available targets:" |
| 123 | + @echo " all - Build and test all practice exercises (default)" |
| 124 | + @echo " <slug> - Build and test a specific exercise given by its slug" |
| 125 | + @echo " clean - Remove all build artifacts" |
| 126 | + @echo " <slug>-clean - Remove build artifacts of a specific exercise given by its slug" |
| 127 | + @echo " list-practice - List all practice exercises" |
| 128 | + @echo " list-concept - List all concept exercises" |
| 129 | + @echo " help - Show this help message" |
0 commit comments