DE1-SoC: A better makefile

I created a new Makefile from scratch for compiling the Linux programs for the Cyclone V SoC. The Makefile is able to handle both C and C++ files.

The Makefile will create a dedicated build directory where all object and binary files are stored in order to keep the project directory clean. Furthermore, there is a target called upload which will upload the compiled binary to the DE1-SoC board using SCP. The corresponding settings (IP, user, …) can be set at the top section of the Makefile.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
################################################################################
# Makefile for the DE1-SoC.                                                    #
# Tested with Quartus 15 / EDS SoC 15 on Windows 10 64-Bit                     #
#                                                                              #
# Copyright (C) 2015 by Joel Bodenmann <joel@embedded.pro>                     #
#                                                                              #
# Feel free to use this makefile without any warranty on your own risk.        #
################################################################################
 
# This is the name of the binaries that will be generated
TARGET                  = de1_soc_hps_binary
 
# The device family (soc_cv_av = Cyclone V   ;   soc_a10 = Aria 10)
ALT_DEVICE_FAMILY       = soc_cv_av
 
# Some paths (Default should work)
HWLIBS_ROOT             = ${SOCEDS_DEST_ROOT}/ip/altera/hps/altera_hps/hwlib
 
# Here we add all *.c files that we want to compile
CSRCS                   =
 
# Here we add all *.cpp files that we want to compile
CPPSRCS                 =
 
# Here we add the paths to all include directories
INCS                    =
 
# Parameters for SCP upload. Set up SSH keys to bypass password prompt
SCP_TARGET_IP           = 192.168.1.100
SCP_USER                = root
SCP_TARGET_PATH         = /home/root
SCP                     = SCP
SCP_FLAGS               =
 
# Compiler settings
ARCH                    = arm-linux-gnueabihf
LD                      = $(ARCH)-g++
CC                      = $(ARCH)-gcc
CPPC                    = $(ARCH)-g++
SIZE                    = $(ARCH)-size
CFLAGS                  = -g -std=gnu99 -Wall
CPPFLAGS                = -g -std=c++11 -Wall
LDFLAGS                 = -g -Wall
 
################################################################################
# Don't change anything below this line                                        #
################################################################################
 
# Some directory and file magic
BUILDDIR                = build
OBJDIR                  = $(BUILDDIR)/obj
 
# Generate the object names
OBJS                    = $(addprefix $(OBJDIR)/,$(addsuffix .o,$(basename $(CSRCS:%.c=%.o))))
OBJS                    += $(addprefix $(OBJDIR)/,$(addsuffix .o,$(basename $(CPPSRCS:%.cpp=%.o))))
 
# Add some paths
CFLAGS                  += $(INCS:%=-I%) -I$(HWLIBS_ROOT)/include -I$(HWLIBS_ROOT)/include/$(ALT_DEVICE_FAMILY) -D$(ALT_DEVICE_FAMILY)
CPPFLAGS                += $(INCS:%=-I%) -I$(HWLIBS_ROOT)/include -I$(HWLIBS_ROOT)/include/$(ALT_DEVICE_FAMILY) -D$(ALT_DEVICE_FAMILY)
LDFLAGS                 += $(INCS:%=-I%)
 
# This is the default target if the user does just calls 'make'
all: build size
 
# Build all the files
build: builddirs $(BUILDDIR)/$(TARGET)
 
# Create the required directories (if not already existing)
builddirs:
    @mkdir -p $(BUILDDIR)
    @mkdir -p $(OBJDIR)
 
# Link everything together
$(BUILDDIR)/$(TARGET): $(OBJS)
    @echo Linking $@
    @$(LD) $(LDFLAGS) -o $(BUILDDIR)/$(TARGET) $(OBJS)
 
# Compile c files
$(OBJDIR)/%.o: %.c
    @mkdir -p $(dir $@)
    @echo Compiling $^
    @$(CC) $(CFLAGS) -c -o $@ $^
 
# Compile cpp files
$(OBJDIR)/%.o: %.cpp
    @mkdir -p $(dir $@)
    @echo Compiling $^
    @$(CPPC) $(CPPFLAGS) -c -o $@ $^
 
# Print size information
size: $(BUILDDIR)/$(TARGET)
    @echo
    @echo
    $(SIZE) $^
 
# Clean up
clean:
    @rm -rf $(BUILDDIR) $(OBJS) $(TARGET) $(TARGET).* *.a *.o *~
    @echo Done
 
# Clean must be a phony target so make knows this never exists as a file
.PHONY: clean
 
# Upload to target
upload:
    $(SCP) $(SCP_FLAGS) $(BUILDDIR)/$(TARGET) $(SCP_USER)@$(SCP_TARGET_IP):$(SCP_TARGET_PATH)
comments powered by Disqus