Home | History | Annotate | Download | only in make
      1 # A sample Makefile for building both Google Mock and Google Test and
      2 # using them in user tests.  This file is self-contained, so you don't
      3 # need to use the Makefile in Google Test's source tree.  Please tweak
      4 # it to suit your environment and project.  You may want to move it to
      5 # your project's root directory.
      6 #
      7 # SYNOPSIS:
      8 #
      9 #   make [all]  - makes everything.
     10 #   make TARGET - makes the given target.
     11 #   make clean  - removes all files generated by make.
     12 
     13 # Please tweak the following variable definitions as needed by your
     14 # project, except GMOCK_HEADERS and GTEST_HEADERS, which you can use
     15 # in your own targets but shouldn't modify.
     16 
     17 # Points to the root of Google Test, relative to where this file is.
     18 # Remember to tweak this if you move this file, or if you want to use
     19 # a copy of Google Test at a different location.
     20 GTEST_DIR = ../../googletest
     21 
     22 # Points to the location of the Google Test libraries
     23 GTEST_LIB_DIR = .
     24 
     25 # Points to the root of Google Mock, relative to where this file is.
     26 # Remember to tweak this if you move this file.
     27 GMOCK_DIR = ..
     28 
     29 # Where to find user code.
     30 USER_DIR = ../test
     31 
     32 # Flags passed to the preprocessor.
     33 # Set Google Test and Google Mock's header directories as system
     34 # directories, such that the compiler doesn't generate warnings in
     35 # these headers.
     36 CPPFLAGS += -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)/include
     37 
     38 # Flags passed to the C++ compiler.
     39 CXXFLAGS += -g -Wall -Wextra -pthread -std=c++11
     40 
     41 # Google Test libraries
     42 GTEST_LIBS = libgtest.a libgtest_main.a libgmock.a libgmock_main.a
     43 
     44 # All tests produced by this Makefile.  Remember to add new tests you
     45 # created to the list.
     46 TESTS = gmock_test
     47 
     48 # All Google Test headers.  Usually you shouldn't change this
     49 # definition.
     50 GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
     51                 $(GTEST_DIR)/include/gtest/internal/*.h
     52 
     53 # All Google Mock headers. Note that all Google Test headers are
     54 # included here too, as they are #included by Google Mock headers.
     55 # Usually you shouldn't change this definition.	
     56 GMOCK_HEADERS = $(GMOCK_DIR)/include/gmock/*.h \
     57                 $(GMOCK_DIR)/include/gmock/internal/*.h \
     58                 $(GTEST_HEADERS)
     59 
     60 # House-keeping build targets.
     61 
     62 all : $(GTEST_LIBS) $(TESTS)
     63 
     64 clean :
     65 	rm -f $(GTEST_LIBS) $(TESTS) *.o
     66 
     67 # Builds gmock.a and gmock_main.a.  These libraries contain both
     68 # Google Mock and Google Test.  A test should link with either gmock.a
     69 # or gmock_main.a, depending on whether it defines its own main()
     70 # function.  It's fine if your test only uses features from Google
     71 # Test (and not Google Mock).
     72 
     73 # Usually you shouldn't tweak such internal variables, indicated by a
     74 # trailing _.
     75 GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
     76 GMOCK_SRCS_ = $(GMOCK_DIR)/src/*.cc $(GMOCK_HEADERS)
     77 
     78 # For simplicity and to avoid depending on implementation details of
     79 # Google Mock and Google Test, the dependencies specified below are
     80 # conservative and not optimized.  This is fine as Google Mock and
     81 # Google Test compile fast and for ordinary users their source rarely
     82 # changes.
     83 gtest-all.o : $(GTEST_SRCS_)
     84 	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
     85             -c $(GTEST_DIR)/src/gtest-all.cc
     86 
     87 gtest_main.o : $(GTEST_SRCS_)
     88 	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
     89             -c $(GTEST_DIR)/src/gtest_main.cc
     90 
     91 gmock-all.o : $(GMOCK_SRCS_)
     92 	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
     93             -c $(GMOCK_DIR)/src/gmock-all.cc
     94 
     95 gmock_main.o : $(GMOCK_SRCS_)
     96 	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
     97             -c $(GMOCK_DIR)/src/gmock_main.cc
     98 
     99 libgtest.a : gtest-all.o
    100 	$(AR) $(ARFLAGS) $@ $^
    101 
    102 libgtest_main.a : gtest_main.o
    103 	$(AR) $(ARFLAGS) $@ $^
    104 
    105 libgmock.a : gmock-all.o
    106 	$(AR) $(ARFLAGS) $@ $^
    107 
    108 libgmock_main.a : gmock_main.o
    109 	$(AR) $(ARFLAGS) $@ $^
    110 
    111 # Builds a sample test.
    112 
    113 gmock_test.o : $(USER_DIR)/gmock_test.cc $(GMOCK_HEADERS)
    114 	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/gmock_test.cc
    115 
    116 gmock_test : gmock_test.o $(GTEST_LIBS)
    117 	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -L$(GTEST_LIB_DIR) -lgmock -lpthread $^ -o $@
    118