1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6CXX ?= g++
7
8BASE_VER ?= 369476
9PKG_CONFIG ?= pkg-config
10PC_DEPS = openssl
11PC_CFLAGS := $(shell $(PKG_CONFIG) --cflags $(PC_DEPS))
12PC_LIBS := $(shell $(PKG_CONFIG) --libs $(PC_DEPS))
13
14PROTOBUF = ../../third_party/protobuf
15GOOGLETEST = ../../third_party/googletest
16
17CXXFLAGS += -std=c++11 -g -Wall -Werror -Wall -Wno-error
18CPPFLAGS += -Imybase \
19            -Icompat/non_cros \
20            -I../../third_party \
21            -I. -I.. $(PC_CFLAGS) $(PROTOBUF_CFLAGS) $(GTEST_INCLUDES)
22LDLIBS += -lpthread -lgcov -lelf $(PC_LIBS) $(PROTOBUF_LIBS)
23
24MAIN_SOURCES = quipper.cc perf_converter.cc
25PROGRAMS = $(MAIN_SOURCES:.cc=)
26
27LIBRARY_SOURCES = \
28	address_mapper.cc binary_data_utils.cc buffer_reader.cc buffer_writer.cc \
29	conversion_utils.cc compat/log_level.cc data_reader.cc \
30	data_writer.cc dso.cc file_reader.cc file_utils.cc \
31	mybase/base/logging.cc perf_option_parser.cc perf_data_utils.cc \
32	perf_parser.cc perf_protobuf_io.cc perf_reader.cc perf_recorder.cc \
33	perf_serializer.cc perf_stat_parser.cc run_command.cc \
34	sample_info_reader.cc scoped_temp_path.cc string_utils.cc \
35  huge_page_deducer.cc
36GENERATED_SOURCES = perf_data.pb.cc perf_stat.pb.cc
37GENERATED_HEADERS = $(GENERATED_SOURCES:.pb.cc=.pb.h)
38
39COMMON_SOURCES = $(LIBRARY_SOURCES) $(GENERATED_SOURCES)
40COMMON_OBJECTS = $(COMMON_SOURCES:.cc=.o)
41TEST_COMMON_SOURCES = \
42	dso_test_utils.cc perf_test_files.cc test_perf_data.cc test_utils.cc
43TEST_COMMON_OBJECTS = $(TEST_COMMON_SOURCES:.cc=.o)
44
45TESTDATA_DIR = testdata
46INTEGRATION_TEST_SOURCES = conversion_utils_test.cc
47PERF_RECORDER_TEST_SOURCES = perf_recorder_test.cc
48UNIT_TEST_SOURCES = \
49	address_mapper_test.cc binary_data_utils_test.cc buffer_reader_test.cc \
50	buffer_writer_test.cc file_reader_test.cc \
51	perf_data_utils_test.cc \
52	perf_option_parser_test.cc perf_parser_test.cc perf_reader_test.cc \
53	perf_serializer_test.cc perf_stat_parser_test.cc run_command_test.cc \
54	sample_info_reader_test.cc scoped_temp_path_test.cc
55TEST_SOURCES = $(INTEGRATION_TEST_SOURCES) $(PERF_RECORDER_TEST_SOURCES) \
56	$(UNIT_TEST_SOURCES) $(TEST_COMMON_SOURCES) test_runner.cc
57TEST_OBJECTS = $(TEST_SOURCES:.cc=.o)
58TESTS = integration_tests perf_recorder_test unit_tests
59
60ALL_SOURCES = $(MAIN_SOURCES) $(COMMON_SOURCES) $(TEST_SOURCES)
61
62all: $(PROGRAMS) $(TESTS)
63	@echo Sources compiled!
64
65# Protobuf dependence configuration
66ifeq ($(wildcard ${PROTOBUF}/src/google/protobuf/descriptor.pb.h),)
67# Protobuf module hasn't been populated, attempt using local installation.
68PROTOC = protoc
69PROTOBUF_DEP =
70PROTOBUF_CFLAGS := $(shell $(PKG_CONFIG) --cflags protobuf)
71PROTOBUF_LIBS := $(shell $(PKG_CONFIG) --libs protobuf)
72else
73# Use protobuf compiler and libraries from submodule.
74PROTOC = ${PROTOBUF}/src/protoc
75PROTOBUF_CFLAGS := -I${PROTOBUF}/src
76PROTOBUF_LIBS := ${PROTOBUF}/src/.libs/libprotobuf.a  -lz
77PROTOBUF_DEP := ${PROTOBUF}/src/.libs/libprotobuf.a
78endif
79
80${PROTOBUF}/configure:
81	echo "[AUTOGEN] Preparing protobuf"
82	(cd ${PROTOBUF} ; autoreconf -f -i -Wall,no-obsolete)
83
84${PROTOBUF}/src/.libs/libprotobuf.a: ${PROTOBUF}/configure
85	echo "[MAKE]    Building protobuf"
86	(cd ${PROTOBUF} ; CC="$(CC)" CXX="$(CXX)" LDFLAGS="$(LDFLAGS_$(CONFIG)) -g \
87  $(PROTOBUF_LDFLAGS_EXTRA)" CPPFLAGS="$(PIC_CPPFLAGS) $(CPPFLAGS_$(CONFIG)) \
88  -g $(PROTOBUF_CPPFLAGS_EXTRA)" ./configure --disable-shared --enable-static \
89  $(PROTOBUF_CONFIG_OPTS))
90	$(MAKE) -C ${PROTOBUF} clean
91	$(MAKE) -C ${PROTOBUF}
92
93# Googletest dependence configuration
94ifeq ($(wildcard ${GOOGLETEST}/googletest/include/gtest/gtest.h),)
95# Use local gtest includes, already on the system path
96GTEST_INCLUDES =
97GTEST_LIBS = -lgtest
98else
99# Pick up gtest includes from submodule.
100GTEST_INCLUDES = -I${GOOGLETEST}/googletest/include
101GTEST_LIBS = -I${GOOGLETEST}/googletest ${GOOGLETEST}/googletest/src/gtest-all.cc
102endif
103
104ifneq ($(MAKECMDGOALS),clean)
105  -include $(ALL_SOURCES:.cc=.d)
106endif
107
108# Taken from:
109# http://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites
110%.d: %.cc $(GENERATED_HEADERS)
111	@set -e; rm -f $@; \
112	$(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< > $@.$$$$; \
113	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
114	rm -f $@.$$$$
115
116# Rule for compiling protobufs.
117%.pb.h %.pb.cc: %.proto $(PROTOBUF_DEP)
118	$(PROTOC) --cpp_out=. -I`dirname $<` $<
119
120# Do not remove protobuf headers that were generated as dependencies of other
121# modules.
122.SECONDARY: $(GENERATED_HEADERS)
123
124$(PROGRAMS): %: %.o $(COMMON_OBJECTS)
125	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
126
127INTEGRATION_TEST_OBJECTS = $(INTEGRATION_TEST_SOURCES:.cc=.o) test_runner.o
128integration_tests: LDLIBS += $(GTEST_LIBS)
129integration_tests: %: $(COMMON_OBJECTS) $(TEST_COMMON_OBJECTS) \
130		      $(INTEGRATION_TEST_OBJECTS) | $(TESTDATA_DIR)
131	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
132
133PERF_RECORDER_TEST_OBJECTS = $(PERF_RECORDER_TEST_SOURCES:.cc=.o)
134perf_recorder_test: LDLIBS += $(GTEST_LIBS)
135perf_recorder_test: %: $(COMMON_OBJECTS) $(TEST_COMMON_OBJECTS) \
136		       $(PERF_RECORDER_TEST_OBJECTS)
137	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
138
139UNIT_TEST_OBJECTS = $(UNIT_TEST_SOURCES:.cc=.o) test_runner.o
140unit_tests: LDLIBS += $(GTEST_LIBS) -lcap
141unit_tests: %: $(COMMON_OBJECTS) $(TEST_COMMON_OBJECTS) \
142		      $(UNIT_TEST_OBJECTS) | $(TESTDATA_DIR)
143	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
144
145# With large perf.data files, the tests will spew a lot of logging.  The
146# following calls will pipe the logging to a log file.  To see test logging,
147# build with 'FEATURES="test noclean"' and go to the build directory.
148check: $(TESTS)
149	for test in $(TESTS); do \
150			./$$test 2> ./$$test.stderr.log && continue; \
151			echo "See $$test.stderr.log for errors"; \
152		exit 1; \
153	done
154
155clean:
156	rm -f *.o *.d *.d.* *.a $(PROGRAMS) $(TESTS) $(GENERATED_SOURCES) \
157		$(GENERATED_HEADERS);
158