📄 makefile
字号:
# file: Makefile
#
# Example Makefile for a
# typical Nios2 program:
#
# peripheral_test.c
#
# ex:set noexpandtab:
#
# +---------------------------
# | How To Use This Makefile
# |
# | 1. Make a new directory with
# | your project's .c and .h
# | (and maybe .s) files
# |
# | 2. Copy this Makefile to it,
# | and change the following
# | variables as needed:
# | PROGRAM_NAME -- base name for your program
# | OBJECTS -- list of .o files, which implies
# | the .c files and .s source files
# | CINCLUDES -- all your .h files
# | AINCLUDES -- all your .s include files
# |
# | 3. Type "make all" to build the project,
# | or "make run" to make it and nios-run it.
# |
# +--------------------------
# | Project Files
# +--------------------------
# |
# | Program name -- the base for all
# | the output files including .srec and .elf.
# |
PROGRAM_NAME = peripheral_test
# |
# | Assembly Include Files
# |
# | In this makefile, every C (.c) file
# | depends on every header file (.h)
# | in the CINCLUDES section, and every
# | assembly file (.s) depends on
# | each and every assembly-header (also .s)
# | in the AINCLUDES section
AINCLUDES = \
$(SDK_ROOT)/inc/excalibur.s \
$(SDK_ROOT)/inc/nios_macros.s
CINCLUDES = \
$(SDK_ROOT)/inc/excalibur.h
# |
# | Object files
# |
# | The rules below will find the .c or .s files needed
# | to make each of these .o object files.
# |
# | This list will all go to the linker to make the
# | final binary.
# |
# | (This makefile follows the unusual convention
# | of appending .o after the original file name,
# | rather than replacing the file-type extension.
# | This makes it a)easier to identify a file's
# | history from its name, and b)easier to set
# | up implicit rules to generate them.
# |
OBJECTS = \
$(OBJ)/peripheral_test_uart.c.o \
$(OBJ)/peripheral_test_timer.c.o \
$(OBJ)/peripheral_test_memory.c.o \
$(OBJ)/peripheral_test.c.o \
# +----------------------
# | Anchor point:
# | the SOPC Builder sdk directory
# | generated for this project
# |
# | This example makefile happens to
# | be located at
# |
# | <your quartus project><your nios cpu>_sdk/src/makefile_example
# |
# | so the sdk root is at ../..
SDK_ROOT = ../..
#
#
#
# +--------------------------------
# | First, some traditional defines
# | for our particular tool chain
# |
GNU_TOOLS_PREFIX = nios2-elf
AS = $(GNU_TOOLS_PREFIX)-as
CC = $(GNU_TOOLS_PREFIX)-gcc
AR = $(GNU_TOOLS_PREFIX)-ar
LD = $(GNU_TOOLS_PREFIX)-g++
OC = $(GNU_TOOLS_PREFIX)-objcopy
NM = $(GNU_TOOLS_PREFIX)-nm
OD = $(GNU_TOOLS_PREFIX)-objdump
NR = nios-run
# | A special echo that prints prettily
E = @echo \\\# `date +%Y.%m.%d.%H:%M:%S` ---
# | Silence is golden... put "S=" (s equals nothing) on
# | the command line to get verbose output.
S = @
# |
# | To keep things tidy, we stash all
# | object (.o) files into a directory
# | named "obj"
# |
OBJ = ./obj
# |
# | And the source directory? Is right here.
# |
SRC = .
# +-----------------------------------
# | Include paths and such
# | If you have more directories of .h files,
# | add them here.
# |
# | We put in ../inc, ../../inc, and so on so
# | that it will look "up and over" to the sopc_builder
# | generated files, or other nearby inc dirs.
# |
# | Note that it uses "-I <dir>" format, which
# | is legal for both as and gcc.
# |
INCLUDE_PATHS = \
-I $(SDK_ROOT)/inc \
-I ./inc \
-I ../inc \
-I ../../inc \
-I ../../../inc \
# +------------------------------------
# | Switches for the compiler, the assembler,
# | and the linker
# |
ASFlags = \
$(INCLUDE_PATHS)
# +--------------------------------------
# | We conservatively assume that there's
# | no hw mul instruction, nor div instruction.
# |
# | Of course, you should modify these settings
# | for your particular hardware to get better
# | performance. If your system utilizes these,
# | the flags to replace the above with are:
# |
# | -mhw-mul: Support for "mul" instruction & 32-bit
# | hardware multiplies (result = 32 bits).
# | -mhw-mulx: Support for "mulx" instructions
# | (32x32=>64 bits).
# | -mhw-div: Support for the "div" instruction.
# |
CCFlags = \
$(INCLUDE_PATHS) \
-mno-hw-mul \
-mno-hw-mulx \
-mno-hw-div \
-mno-cache-volatile \
-W -g -c -O2
# +----------------------------------------
# | Rules
# |
# | These implicit rules treat all your .s
# | and all your .c files the same.
# |
# | "default" comes first so that if you just
# | type "make" it is the default target.
# |
default : srec
$(E) .
$(E) . Built $(PROGRAM_NAME).srec
$(E) . try "make help" for more information
$(E) .
$(OBJ)/%.s.o : $(SRC)/%.s $(AINCLUDES)
$(E) Assembling $<
$(S)$(AS) $(ASFlags) $< -o $@
$(OBJ)/%.c.o : $(SRC)/%.c $(CINCLUDES)
$(E) Compiling $<
$(S)$(CC) $(CCFlags) $< -o $@
$(OBJ) :
$(E) Making $@/ directory
$(S)mkdir $@
clean : $(OBJ)
$(E) Removing objects
$(S)rm -rf $(OBJ)/*
# +-------------------------------------
# | Linking
# |
# | This is the most involved command line.
# | It was taken from the output of "nios2-build"
# | and splayed out into an easier-to-read form.
# |
# | It references a linker script in your include
# | directory, and also makes sure that the first thing
# | in the file is a branch to "_start". (Usually
# | the routine named _start does a bunch of useful
# | initialization before your main() routine is called.)
# |
# | Explanation of switches to the linker:
# |
# | -g
# | include debug info in .elf file. Does NOT
# | increase the size of your S-Record.
# |
# | -T (path to ld script)
# | A "linker script" which knows about your memory
# | map, by using symbols like nasys_program_mem from
# | excalibur.s.
# |
# | -msys-crt0=$(SDK_ROOT)/lib/obj/nios2_crt0.s.o
# | Uses the appropriate crt0 object for this
# | SDK's library.
# |
# | -mno-hw-<mul|mulx|div>
# | As in compilation, the linker must know about
# | hardware-specific options to know which libraries
# | to link against. We conservatively assume no
# | hardware multiplication or division. If your system
# | utilizes these, the flags to replace the above with
# | are:
# | -mhw-mul: Support for "mul" instruction & 32-bit
# | hardware multiplies (result = 32 bits).
# | -mhw-mulx: Support for "mulx" instructions
# | (32x32=>64 bits).
# | -mhw-div: Support for the "div" instruction.
# |
# | -msys-lib=nios
# | We direct the linker to libnios.a in the sdk/lib
# | folder to provide its "system". The sys-lib provides
# | basic C "stub" routines such as open/close/sbrk, etc.
# | Without this the linker would link in the GNU "nosys"
# | implementations of such routines. If you want to
# | debug, you should probably change this to
# | -msys-lib=nios_debug (libnios_debug.a). Using -msys-lib
# | also directs the linker to grab common libraries so that
# | they need not be explicitly referenced (e.g. c, m,
# | stdc++, etc.).
# |
# | -l (any specific libs)
# | Place any specific libraries you require here, just
# | before the -L (big 'L') to paths below). For example,
# | if I have a file named "libfoo.a", "-l foo" would
# | include it.
# |
# | -L(various paths)
# | Places the linker might look for libraries. The -msys-lib
# | (in sdk/lib) is the one we care about telling the linker
# | to find.
# |
LFLAGS = \
-g \
-T$(SDK_ROOT)/inc/nios_build.ld \
-msys-crt0=$(SDK_ROOT)/lib/obj/nios2_crt0.s.o \
-mno-hw-mul \
-mno-hw-mulx \
-mno-hw-div \
-msys-lib=nios \
-L$(SDK_ROOT)/lib \
# |
# | Rule for making .elf file from
# | the objects. The OBJECTS variable
# | must come before the LFLAGS, because
# | the LFLAGS has the libraries. The linker
# | needs to know which library routines you've
# | referenced from your code before scanning
# | them and deciding which parts to use.
# |
$(OBJ)/$(PROGRAM_NAME).elf : $(OBJ) $(OBJECTS)
$(E) Linking $@
$(S)$(LD) $(OBJECTS) $(LFLAGS) -o $(OBJ)/$(PROGRAM_NAME).elf
# |
# | S-Record
# |
# | We like Nios programs to be in an S-Record
# | for use by nios-run or SOPC Builder memory
# | contents
# |
# | The S-Record is the only output file that
# | we dont stash tidily into the obj folder
# |
$(PROGRAM_NAME).srec : $(OBJ)/$(PROGRAM_NAME).elf
$(E) Converting $(PROGRAM_NAME) to S-Record
$(S)$(OC) -O srec $(OBJ)/$(PROGRAM_NAME).elf $(PROGRAM_NAME).srec
# |
# | Handy auxilliary files
# |
$(OBJ)/$(PROGRAM_NAME).nm : $(OBJ)/$(PROGRAM_NAME).elf
$(E) Making $(PROGRAM_NAME).nm
$(S)$(NM) $(OBJ)/$(PROGRAM_NAME).elf | sort > $(OBJ)/$(PROGRAM_NAME).nm
$(OBJ)/$(PROGRAM_NAME).objdump : $(OBJ)/$(PROGRAM_NAME).elf
$(E) Making $(PROGRAM_NAME).objdump
$(S)$(OD) $(OBJ)/$(PROGRAM_NAME).elf -d --source > $(OBJ)/$(PROGRAM_NAME).objdump
# +-------------------------------------
# | Shortcut Targets
# |
srec : $(PROGRAM_NAME).srec
elf : $(OBJ)/$(PROGRAM_NAME).elf
run : $(PROGRAM_NAME).srec
$(E) Running $(PROGRAM_NAME)
$(S)$(NR) $(PROGRAM_NAME).srec
$(E) Done running $(PROGRAM_NAME)
aux : $(OBJ)/$(PROGRAM_NAME).nm $(OBJ)/$(PROGRAM_NAME).objdump
all : srec aux
help :
@echo
@echo Program name: $(PROGRAM_NAME)
@echo
@echo Available makefile targets:
@echo
@echo " make clean -- erase intermediate files"
@echo " make srec -- compile, link, and convert to S-Record"
@echo " make run -- make the S-Record, and then nios-run, too"
@echo
@echo " make elf -- only make the .elf file"
@echo " make aux -- generate .nm and .objdump files"
@echo
@echo Add the option "S=" for a more verbose output
@echo
# end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -