Skip to content
Snippets Groups Projects
Makefile 10.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Marcus M. Darden's avatar
    Marcus M. Darden committed
    ## EECS 281 Advanced Makefile
    
    # How to use this Makefile...
    ###################
    ###################
    ##               ##
    ##  $ make help  ##
    ##               ##
    ###################
    ###################
    
    # IMPORTANT NOTES:
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #   1. Set EXECUTABLE to the command name from the project specification.
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #   2. To enable automatic creation of unit test rules, your program logic
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #      (where main() is) should be in a file named project*.cpp or
    #      specified in the PROJECTFILE variable.
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #   3. Files you want to include in your final submission cannot match the
    #      test*.cpp pattern.
    
    
    #######################
    # TODO (begin) #
    #######################
    
    # Change UNIQNAME to match your UM uniqname.
    UNIQNAME    = youruniqname
    
    
    # Change IDENTIFIER to match the project identifier given in the project spec.
    
    IDENTIFIER  = EEC50281EEC50281EEC50281EEC50281EEC50281
    
    # Change 'executable' to match the command name given in the project spec.
    
    # The following line looks for a project's main() in files named project*.cpp,
    
    # executable.cpp (substituted from EXECUTABLE above), or main.cpp
    
    PROJECTFILE = $(or $(wildcard project*.cpp $(EXECUTABLE).cpp), main.cpp)
    
    # If main() is in another file delete line above, edit and uncomment below
    #PROJECTFILE = mymainfile.cpp
    #######################
    # TODO (end) #
    #######################
    
    
    # enables c++17 on CAEN or 281 autograder
    
    paoletti's avatar
    paoletti committed
    PATH := /usr/um/gcc-6.2.0/bin:$(PATH)
    LD_LIBRARY_PATH := /usr/um/gcc-6.2.0/lib64
    LD_RUN_PATH := /usr/um/gcc-6.2.0/lib64
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # This is the path from the CAEN home folder to where projects will be
    # uploaded. (eg. /home/mmdarden/eecs281/project1)
    # Change this if you prefer a different path.
    
    # REMOTE_PATH := eecs281_$(EXECUTABLE)_sync  # /home/mmdarden/eecs281_proj0_sync
    REMOTE_PATH := eecs281_$(EXECUTABLE)_sync
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    # designate which compiler to use
    
    CXX         = g++
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    # list of test drivers (with main()) for development
    TESTSOURCES = $(wildcard test*.cpp)
    # names of test executables
    TESTS       = $(TESTSOURCES:%.cpp=%)
    
    # list of sources used in project
    
    SOURCES     = $(wildcard *.cpp)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    SOURCES     := $(filter-out $(TESTSOURCES), $(SOURCES))
    # list of objects used in project
    
    OBJECTS     = $(SOURCES:%.cpp=%.o)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # name of the tarball created for submission
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    FULL_SUBMITFILE = fullsubmit.tar.gz
    
    PARTIAL_SUBMITFILE = partialsubmit.tar.gz
    UNGRADED_SUBMITFILE = ungraded.tar.gz
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # name of the perf data file, only used by the clean target
    PERF_FILE = perf.data*
    
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #Default Flags (we prefer -std=c++17 but Mac/Xcode/Clang doesn't support)
    
    CXXFLAGS = -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic 
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    # make release - will compile "all" with $(CXXFLAGS) and the -O3 flag
    
    #                also defines NDEBUG so that asserts will not check
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    release: CXXFLAGS += -O3 -DNDEBUG
    
    release: $(EXECUTABLE)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # make debug - will compile sources with $(CXXFLAGS) and the -g3 flag
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #              also defines DEBUG, so "#ifdef DEBUG /*...*/ #endif" works
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    debug: CXXFLAGS += -g3 -DDEBUG
    
    debug:
    	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_debug
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # make profile - will compile "all" with $(CXXFLAGS) and the -g3 and -O3 flags
    profile: CXXFLAGS += -g3 -O3
    
    profile:
    	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_profile
    
    	
    # make gprof - will compile "all" with $(CXXFLAGS) and the -pg (for gprof)
    gprof: CXXFLAGS += -pg
    gprof:
    	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_profile
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # make static - will perform static analysis in the matter currently used
    #               on the autograder
    static:
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    	cppcheck --enable=all --suppress=missingIncludeSystem \
    
          $(SOURCES) *.h *.hpp
    
    # make identifier - will check to ensure that all source code and header files
    #                   include the project identifier; skip subdirectories;
    #                   also removes old submit tarballs, they are outdated
    
    	@if [ $$(grep --include=*.{h,hpp,c,cpp} --exclude=xcode_redirect.hpp --directories=skip -L $(IDENTIFIER) * | wc -l) -ne 0 ]; then \
    
    		printf "Missing project identifier in file(s): "; \
    
    		echo `grep --include=*.{h,hpp,c,cpp} --directories=skip -L $(IDENTIFIER) *`; \
    		rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE); \
    		exit 1; \
    	fi
    
    # Build all executables
    all: release debug profile
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    $(EXECUTABLE): $(OBJECTS)
    ifeq ($(EXECUTABLE), executable)
    	@echo Edit EXECUTABLE variable in Makefile.
    	@echo Using default a.out.
    	$(CXX) $(CXXFLAGS) $(OBJECTS)
    else
    	$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(EXECUTABLE)
    endif
    
    # Automatically generate any build rules for test*.cpp files
    define make_tests
        ifeq ($$(PROJECTFILE),)
    	    @echo Edit PROJECTFILE variable to .cpp file with main\(\)
    	    @exit 1
        endif
        SRCS = $$(filter-out $$(PROJECTFILE), $$(SOURCES))
        OBJS = $$(SRCS:%.cpp=%.o)
    
        HDRS = $$(wildcard *.h *.hpp)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
        $(1): CXXFLAGS += -g3 -DDEBUG
        $(1): $$(OBJS) $$(HDRS) $(1).cpp
    	$$(CXX) $$(CXXFLAGS) $$(OBJS) $(1).cpp -o $(1)
    endef
    $(foreach test, $(TESTS), $(eval $(call make_tests, $(test))))
    
    
    alltests: $(TESTS)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    # rule for creating objects
    %.o: %.cpp
    	$(CXX) $(CXXFLAGS) -c $*.cpp
    
    # make clean - remove .o files, executables, tarball
    clean:
    
    	rm -f $(OBJECTS) $(EXECUTABLE) $(EXECUTABLE)_debug $(EXECUTABLE)_profile \
    
          $(TESTS) $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(PERF_FILE) \
          $(UNGRADED_SUBMITFILE)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    	rm -Rf *.dSYM
    
    
    
    # get a list of all files that might be included in a submit
    # different submit types can do additional filtering to remove unwanted files
    FULL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
                       $(wildcard Makefile *.h *.hpp *.cpp test*.txt))
    
    # make fullsubmit.tar.gz - cleans, runs dos2unix, creates tarball
    # including test files
    $(FULL_SUBMITFILE): $(FULL_SUBMITFILES)
    	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
    	COPYFILE_DISABLE=true tar -vczf $(FULL_SUBMITFILE) $(FULL_SUBMITFILES)
    	@echo !!! Final submission prepared, test files included... READY FOR GRADING !!!
    
    
    # make partialsubmit.tar.gz - cleans, creates tarball
    
    # omitting test files
    
    PARTIAL_SUBMITFILES=$(filter-out $(wildcard test*.txt), $(FULL_SUBMITFILES))
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    $(PARTIAL_SUBMITFILE): $(PARTIAL_SUBMITFILES)
    
    	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    	COPYFILE_DISABLE=true tar -vczf $(PARTIAL_SUBMITFILE) \
    
          $(PARTIAL_SUBMITFILES)
    
    	@echo !!! WARNING: No test files included. Use 'make fullsubmit' to include test files. !!!
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # make ungraded.tar.gz - cleans, creates tarball omitting test files, Makefile
    UNGRADED_SUBMITFILES=$(filter-out Makefile, $(PARTIAL_SUBMITFILES))
    $(UNGRADED_SUBMITFILE): $(UNGRADED_SUBMITFILES)
    	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
    	@touch __ungraded
    	COPYFILE_DISABLE=true tar -vczf $(UNGRADED_SUBMITFILE) \
          $(UNGRADED_SUBMITFILES) __ungraded
    	@rm -f __ungraded
    	@echo !!! WARNING: This submission will not be graded. !!!
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    # shortcut for make submit tarballs
    
    fullsubmit: identifier $(FULL_SUBMITFILE)
    
    partialsubmit: identifier $(PARTIAL_SUBMITFILE)
    ungraded: identifier $(UNGRADED_SUBMITFILE)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    
    # REMOTE_PATH has default definition above
    
    	# Synchronize local files into target directory on CAEN
    	rsync \
    
          --delete \
          --exclude '*.o' \
          --exclude '$(EXECUTABLE)' \
          --exclude '$(EXECUTABLE)_debug' \
          --exclude '$(EXECUTABLE)_profile' \
    
          --exclude '.git*' \
    
          --exclude '.vs*' \
    
          --exclude '*.code-workspace' \
    
          --filter=":- .gitignore" \
    
          "$(UNIQNAME)@login.engin.umich.edu:'$(REMOTE_PATH)/'"
    
    	echo "Files synced to CAEN at ~/$(REMOTE_PATH)/"
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    define MAKEFILE_HELP
    EECS281 Advanced Makefile Help
    * This Makefile uses advanced techniques, for more information:
        $$ man make
    
    * General usage
        1. Follow directions at each "TODO" in this file.
    
           a. Set EXECUTABLE equal to the name from the project specification.
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
           b. Set PROJECTFILE equal to the name of the source file with main()
           c. Add any dependency rules specific to your files.
        2. Build, test, submit... repeat as necessary.
    
    * Preparing submissions
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
        A) To build 'partialsubmit.tar.gz', a tarball without tests used to
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
           find buggy solutions in the autograder.
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
               *** USE THIS ONLY FOR TESTING YOUR SOLUTION! ***
    
           This is useful for faster autograder runs during development and
           free submissions if the project does not build.
               $$ make partialsubmit
        B) Build 'fullsubmit.tar.gz' a tarball complete with autograder test
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
               *** ALWAYS USE THIS FOR FINAL GRADING! ***
    
           It is also useful when trying to find buggy solutions in the
           autograder.
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
               $$ make fullsubmit
    
    * Unit testing support
        A) Source files for unit testing should be named test*.cpp.  Examples
           include test_input.cpp or test3.cpp.
        B) Automatic build rules are generated to support the following:
               $$ make test_input
               $$ make test3
               $$ make alltests        (this builds all test drivers)
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
        C) If test drivers need special dependencies, they must be added
           manually.
    
        D) IMPORTANT: NO SOURCE FILES WITH NAMES THAT BEGIN WITH test WILL BE
           ADDED TO ANY SUBMISSION TARBALLS.
    
    
    * Static Analysis support
        A) Matches current autograder style grading tests
        B) Usage:
               $$ make static
    
    * Sync to CAEN support
    
        A) Requires an .ssh/config file with a login.engin.umich.edu host
           defined, SSH Multiplexing enabled, and an open SSH connection.
    
        B) Edit the REMOTE_BASEDIR variable if default is not preferred.
        C) Usage:
    
               $$ make sync2caen
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    endef
    export MAKEFILE_HELP
    
    help:
    	@echo "$$MAKEFILE_HELP"
    
    #######################
    # TODO (begin) #
    #######################
    # individual dependencies for objects
    # Examples:
    # "Add a header file dependency"
    # project2.o: project2.cpp project2.h
    #
    # "Add multiple headers and a separate class"
    # HEADERS = some.h special.h header.h files.h
    # myclass.o: myclass.cpp myclass.h $(HEADERS)
    # project5.o: project5.cpp myclass.o $(HEADERS)
    #
    
    # SOME EXAMPLES
    #
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    #test_thing: test_thing.cpp class.o functions.o
    #class.o: class.cpp class.h
    #functions.o: functions.cpp functions.h
    #project0.o: project0.cpp class.h functions.h
    
    #
    # THE COMPILER CAN GENERATE DEPENDENCIES FROM SOURCE CODE
    #
    
    #
    # ADD YOUR OWN DEPENDENCIES HERE
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    
    ######################
    # TODO (end) #
    ######################
    
    # these targets do not create any files
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    .PHONY: all release debug profile static clean alltests partialsubmit \
    
            fullsubmit ungraded sync2caen help identifier
    
    Marcus M. Darden's avatar
    Marcus M. Darden committed
    # disable built-in rules
    .SUFFIXES: