RC-5 for ATmega8
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

221 lines
6.5 KiB

  1. #### PROJECT SETTINGS ####
  2. # The name of the executable to be created
  3. BIN_NAME := RC-5
  4. # Compiler used
  5. CC = avr-gcc
  6. # Extension of source files used in the project
  7. SRC_EXT = c
  8. # Path to the source directory, relative to the makefile
  9. SRC_PATH = ./src
  10. # Space-separated pkg-config libraries used by this project
  11. LIBS =
  12. # General compiler flags
  13. COMPILE_FLAGS = -mmcu=atmega8a -Os
  14. # Additional release-specific flags
  15. RCOMPILE_FLAGS = -D NDEBUG -pedantic
  16. # Additional debug-specific flags
  17. DCOMPILE_FLAGS = -D DEBUG -g -Wextra -Wall -Wfatal-errors
  18. # Add additional include paths
  19. INCLUDES = -I ./include -I /usr/avr/include
  20. # General linker settings
  21. LINK_FLAGS = -mmcu=atmega8a -Os
  22. # Additional release-specific linker settings
  23. RLINK_FLAGS = -pedantic
  24. # Additional debug-specific linker settings
  25. DLINK_FLAGS =
  26. # Destination directory, like a jail or mounted system
  27. DESTDIR = /
  28. # Install path (bin/ is appended automatically)
  29. INSTALL_PREFIX = usr/local
  30. #### END PROJECT SETTINGS ####
  31. # Generally should not need to edit below this line
  32. # Obtains the OS type, either 'Darwin' (OS X) or 'Linux'
  33. UNAME_S:=$(shell uname -s)
  34. # Function used to check variables. Use on the command line:
  35. # make print-VARNAME
  36. # Useful for debugging and adding features
  37. print-%: ; @echo $*=$($*)
  38. # Shell used in this makefile
  39. # bash is used for 'echo -en'
  40. SHELL = /bin/bash
  41. # Clear built-in rules
  42. .SUFFIXES:
  43. # Programs for installation
  44. INSTALL = install
  45. INSTALL_PROGRAM = $(INSTALL)
  46. INSTALL_DATA = $(INSTALL) -m 644
  47. # Append pkg-config specific libraries if need be
  48. ifneq ($(LIBS),)
  49. COMPILE_FLAGS += $(shell pkg-config --cflags $(LIBS))
  50. LINK_FLAGS += $(shell pkg-config --libs $(LIBS))
  51. endif
  52. # Verbose option, to output compile and link commands
  53. export V := false
  54. export CMD_PREFIX := @
  55. ifeq ($(V),true)
  56. CMD_PREFIX :=
  57. endif
  58. # Combine compiler and linker flags
  59. release: export CFLAGS := $(CFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS)
  60. release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS)
  61. debug: export CFLAGS := $(CFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS)
  62. debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS)
  63. # Build and output paths
  64. release: export BUILD_PATH := build/release
  65. release: export BIN_PATH := bin/release
  66. debug: export BUILD_PATH := build/debug
  67. debug: export BIN_PATH := bin/debug
  68. install: export BIN_PATH := bin/release
  69. # Find all source files in the source directory, sorted by most
  70. # recently modified
  71. ifeq ($(UNAME_S),Darwin)
  72. SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-)
  73. else
  74. SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \
  75. | sort -k 1nr | cut -f2-)
  76. endif
  77. # fallback in case the above fails
  78. rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \
  79. $(filter $(subst *,%,$2), $d))
  80. ifeq ($(SOURCES),)
  81. SOURCES := $(call rwildcard, $(SRC_PATH), *.$(SRC_EXT))
  82. endif
  83. # Set the object file names, with the source directory stripped
  84. # from the path, and the build path prepended in its place
  85. OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o)
  86. # Set the dependency files that will be used to add header dependencies
  87. DEPS = $(OBJECTS:.o=.d)
  88. # Macros for timing compilation
  89. ifeq ($(UNAME_S),Darwin)
  90. CUR_TIME = awk 'BEGIN{srand(); print srand()}'
  91. TIME_FILE = $(dir $@).$(notdir $@)_time
  92. START_TIME = $(CUR_TIME) > $(TIME_FILE)
  93. END_TIME = read st < $(TIME_FILE) ; \
  94. $(RM) $(TIME_FILE) ; \
  95. st=$$((`$(CUR_TIME)` - $$st)) ; \
  96. echo $$st
  97. else
  98. TIME_FILE = $(dir $@).$(notdir $@)_time
  99. START_TIME = date '+%s' > $(TIME_FILE)
  100. END_TIME = read st < $(TIME_FILE) ; \
  101. $(RM) $(TIME_FILE) ; \
  102. st=$$((`date '+%s'` - $$st - 86400)) ; \
  103. echo `date -u -d @$$st '+%H:%M:%S'`
  104. endif
  105. # Version macros
  106. # Comment/remove this section to remove versioning
  107. USE_VERSION := false
  108. # If this isn't a git repo or the repo has no tags, git describe will return non-zero
  109. ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0)
  110. USE_VERSION := true
  111. VERSION := $(shell git describe --tags --long --dirty --always | \
  112. sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g')
  113. VERSION_MAJOR := $(word 1, $(VERSION))
  114. VERSION_MINOR := $(word 2, $(VERSION))
  115. VERSION_PATCH := $(word 3, $(VERSION))
  116. VERSION_REVISION := $(word 4, $(VERSION))
  117. VERSION_HASH := $(word 5, $(VERSION))
  118. VERSION_STRING := \
  119. "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)"
  120. override CFLAGS := $(CFLAGS) \
  121. -D VERSION_MAJOR=$(VERSION_MAJOR) \
  122. -D VERSION_MINOR=$(VERSION_MINOR) \
  123. -D VERSION_PATCH=$(VERSION_PATCH) \
  124. -D VERSION_REVISION=$(VERSION_REVISION) \
  125. -D VERSION_HASH=\"$(VERSION_HASH)\"
  126. endif
  127. # Standard, non-optimized release build
  128. .PHONY: release
  129. release: dirs
  130. ifeq ($(USE_VERSION), true)
  131. @echo "Beginning release build v$(VERSION_STRING)"
  132. else
  133. @echo "Beginning release build"
  134. endif
  135. @$(START_TIME)
  136. @$(MAKE) all --no-print-directory
  137. @echo -n "Total build time: "
  138. @$(END_TIME)
  139. # Debug build for gdb debugging
  140. .PHONY: debug
  141. debug: dirs
  142. ifeq ($(USE_VERSION), true)
  143. @echo "Beginning debug build v$(VERSION_STRING)"
  144. else
  145. @echo "Beginning debug build"
  146. endif
  147. @$(START_TIME)
  148. @$(MAKE) all --no-print-directory
  149. @echo -n "Total build time: "
  150. @$(END_TIME)
  151. # Create the directories used in the build
  152. .PHONY: dirs
  153. dirs:
  154. @echo "Creating directories"
  155. @mkdir -p $(dir $(OBJECTS))
  156. @mkdir -p $(BIN_PATH)
  157. # Installs to the set path
  158. .PHONY: install
  159. install:
  160. @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin"
  161. @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin
  162. # Uninstalls the program
  163. .PHONY: uninstall
  164. uninstall:
  165. @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)"
  166. @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)
  167. # Removes all build files
  168. .PHONY: clean
  169. clean:
  170. @echo "Deleting $(BIN_NAME) symlink"
  171. @$(RM) $(BIN_NAME)
  172. @echo "Deleting directories"
  173. @$(RM) -r build
  174. @$(RM) -r bin
  175. # Main rule, checks the executable and symlinks to the output
  176. all: $(BIN_PATH)/$(BIN_NAME)
  177. @echo "Making symlink: $(BIN_NAME) -> $<"
  178. @$(RM) $(BIN_NAME)
  179. @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME)
  180. # Link the executable
  181. $(BIN_PATH)/$(BIN_NAME): $(OBJECTS)
  182. @echo "Linking: $@"
  183. @$(START_TIME)
  184. $(CMD_PREFIX)$(CC) $(OBJECTS) $(LDFLAGS) -o $@
  185. @echo -en "\t Link time: "
  186. @$(END_TIME)
  187. # Add dependency files, if they exist
  188. -include $(DEPS)
  189. # Source file rules
  190. # After the first compilation they will be joined with the rules from the
  191. # dependency files to provide header dependencies
  192. $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT)
  193. @echo "Compiling: $< -> $@"
  194. @$(START_TIME)
  195. $(CMD_PREFIX)$(CC) $(CFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@
  196. @echo -en "\t Compile time: "
  197. @$(END_TIME)