patrickjp93
patrickjp93

Reputation: 65

How do you use a single makefile in multiple projects (and one version of cpplint.py)?

I've really tried to look through the GNU manuals for LISP, but I just don't seem to get how the variable system works or how to solve this one annoying problem. Every time I want to compile a C++ project for this course, I have to reference the Makefile that was provided, but that means I need to copy the Makefile into every single project... I'd really like a way to just have the Makefile and the cpplint.py file (a style checker which I must fulfill) in one directory I can reference anywhere instead of having to have copies of both in every project folder. Someone please, please help me. My poor SSD doesn't deserve this ;(

I've provided the makefile code below.

have_lint=$(wildcard cpplint.py)

The line above is the one I know I need to change to be able to at least put the cpplint file into one directory, but how do I change this? I've tried wildcard../maker_directory/cpplint.py but that didn't work...

# Makefile for use with emacs and emacs's flymake mode
# Copyright (C) [email protected]
#
# From emacs menu select Tools->compile
# To compile one file use the syntax:
#     make SRC=hello.cpp
# The compile many files use the syntax (change my_exe_name below):
#     make many EXE=my_exe_name
#

.PHONY: check-syntax all clean style many style-many

CXX=icpc
CXXFLAGS=-Wall -g -Wextra -std=c++11
LIBS=
DEF_LIBS=-lm -lpthread

# Target exectuable name if SRC is defined
ifdef SRC
OBJ=$(patsubst %.cpp, %.o, $(SRC))
EXE=$(patsubst %.cpp, %,   $(SRC))
endif

# Variables to conditionally download cpplint.py
have_lint=$(wildcard cpplint.py)
ifneq ('$(have_lint)', 'cpplint.py')
WGET=get-lint
endif

all: build style

build: $(SRC)
ifeq (,$(findstring .h, $(SRC)))
	$(CXX) $(CXXFLAGS) $(SRC) -o $(EXE) $(LIBS) $(DEF_LIBS)
endif

compile: $(SRC)
ifeq (,$(findstring .h, $(SRC)))
	$(CXX) -c $(CXXFLAGS) $(SRC) -o $(OBJ) $(LIBS) $(DEF_LIBS)
endif

check-syntax:
	$(CXX) $(CXXFLAGS) -fsyntax-only $(CHK_SOURCES)

style: $(WGET)
	./cpplint.py $(SRC)

get-lint:
	wget -q http://pc2lab.cec.miamiOH.edu/documents/cpplint.py
	chmod +x cpplint.py

many: compile-many style-many

compile-many:
ifndef EXE
	@echo Specify target executable name via command-line EXE=your_exe_name
	@exit 2
endif
	$(CXX) $(CXXFLAGS) *.cpp -o $(EXE) $(LIBS) $(DEF_LIBS)

style-many: $(WGET)
	$(eval SRCS:=$(wildcard *.h *.cpp))
	./cpplint.py $(SRCS) || echo done

Upvotes: 1

Views: 893

Answers (1)

Scooter
Scooter

Reputation: 7061

You can add a makefile variable to hold the directory where the Python cpplint.py is stored and also put the full path to the file in a variable:

lint_dir=$(HOME)/code/cpp/CppLint
cpplint=$(lint_dir)/cpplint.py

Then add that directory prefix to the places you use cpplint.py and also the download of it:

# Variables to conditionally download cpplint.py
have_lint=$(wildcard $(cpplint) )
ifneq ('$(have_lint)', '$(cpplint)')
WGET=get-lint
endif

style: $(WGET)
    $(cpplint) $(SRC)

get-lint:
    wget -q --directory-prefix=$(lint_dir) http://pc2lab.cec.miamiOH.edu/documents/cpplint.py
    chmod +x $(cpplint)

style-many: $(WGET)
    $(eval SRCS:=$(wildcard *.h *.cpp))
    $(cpplint) $(SRCS) || echo done

You can use the -f option to use a Makefile in a different directory

make -f ${makefile-directory}/my_makefile ...

Upvotes: 1

Related Questions