1# Copyright 2015 Google Inc. All rights reserved
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Find source file location from path to this Makefile
16KATI_SRC_PATH := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
17
18# Set defaults if they weren't set by the including Makefile
19KATI_CXX ?= $(CXX)
20KATI_LD ?= $(CXX)
21KATI_INTERMEDIATES_PATH ?= .
22KATI_BIN_PATH ?= .
23
24KATI_CXX_SRCS := \
25	affinity.cc \
26	command.cc \
27	dep.cc \
28	eval.cc \
29	exec.cc \
30	expr.cc \
31	file.cc \
32	file_cache.cc \
33	fileutil.cc \
34	find.cc \
35	flags.cc \
36	func.cc \
37	io.cc \
38	log.cc \
39	main.cc \
40	ninja.cc \
41	parser.cc \
42	regen.cc \
43	rule.cc \
44	stats.cc \
45	stmt.cc \
46	string_piece.cc \
47	stringprintf.cc \
48	strutil.cc \
49	symtab.cc \
50	thread_pool.cc \
51	timeutil.cc \
52	var.cc
53
54KATI_CXX_GENERATED_SRCS := \
55	version.cc
56
57KATI_CXX_SRCS := $(addprefix $(KATI_SRC_PATH)/,$(KATI_CXX_SRCS))
58KATI_CXX_TEST_SRCS := \
59  $(wildcard $(KATI_SRC_PATH)/*_test.cc) \
60  $(wildcard $(KATI_SRC_PATH)/*_bench.cc)
61
62KATI_CXX_OBJS := $(patsubst $(KATI_SRC_PATH)/%.cc,$(KATI_INTERMEDIATES_PATH)/%.o,\
63	$(KATI_CXX_SRCS))
64KATI_CXX_GENERATED_OBJS := $(patsubst %.cc,$(KATI_INTERMEDIATES_PATH)/%.o,\
65	$(KATI_CXX_GENERATED_SRCS))
66KATI_CXX_TEST_OBJS := $(patsubst $(KATI_SRC_PATH)/%.cc,$(KATI_INTERMEDIATES_PATH)/%.o,\
67	$(KATI_CXX_TEST_SRCS))
68
69KATI_CXX_TEST_EXES := $(patsubst $(KATI_INTERMEDIATES_PATH)/%.o,$(KATI_BIN_PATH)/%,\
70	$(KATI_CXX_TEST_OBJS))
71
72KATI_CXXFLAGS := -g -W -Wall -MMD -MP
73KATI_CXXFLAGS += -O -DNOLOG
74KATI_CXXFLAGS += -march=native
75#KATI_CXXFLAGS += -pg
76
77ifeq ($(shell uname),Linux)
78KATI_LIBS := -lrt -lpthread
79endif
80
81# Rule to build ckati into KATI_BIN_PATH
82$(KATI_BIN_PATH)/ckati: $(KATI_CXX_OBJS) $(KATI_CXX_GENERATED_OBJS)
83	@mkdir -p $(dir $@)
84	$(KATI_LD) -std=c++11 $(KATI_CXXFLAGS) -o $@ $^ $(KATI_LIBS)
85
86# Rule to build normal source files into object files in KATI_INTERMEDIATES_PATH
87$(KATI_CXX_OBJS) $(KATI_CXX_TEST_OBJS): $(KATI_INTERMEDIATES_PATH)/%.o: $(KATI_SRC_PATH)/%.cc
88	@mkdir -p $(dir $@)
89	$(KATI_CXX) -c -std=c++11 $(KATI_CXXFLAGS) -o $@ $<
90
91# Rule to build generated source files into object files in KATI_INTERMEDIATES_PATH
92$(KATI_CXX_GENERATED_OBJS): $(KATI_INTERMEDIATES_PATH)/%.o: $(KATI_INTERMEDIATES_PATH)/%.cc
93	@mkdir -p $(dir $@)
94	$(KATI_CXX) -c -std=c++11 $(KATI_CXXFLAGS) -o $@ $<
95
96ckati_tests: $(KATI_CXX_TEST_EXES)
97
98# Rule to build tests using *_test.cc and all normal *.cc files except main.cc
99$(KATI_CXX_TEST_EXES): $(filter-out $(KATI_INTERMEDIATES_PATH)/main.o,$(KATI_CXX_OBJS)) $(KATI_CXX_GENERATED_OBJS)
100$(KATI_CXX_TEST_EXES): $(KATI_BIN_PATH)/%: $(KATI_INTERMEDIATES_PATH)/%.o
101	$(KATI_LD) $^ -o $@ $(KATI_LIBS)
102
103# Rule to generate version.cc
104KATI_GIT_DIR := $(shell git -C $(KATI_SRC_PATH) rev-parse --show-toplevel)
105ifneq ($(KATI_GIT_DIR),)
106KATI_VERSION_DEPS := $(KATI_GIT_DIR)/.git/HEAD $(KATI_GIT_DIR)/.git/index
107KATI_VERSION := $(shell git -C $(KATI_GIT_DIR) rev-parse HEAD)
108else
109KATI_VERSION_DEPS :=
110KATI_VERSION := unknown
111endif
112$(KATI_INTERMEDIATES_PATH)/version.cc: $(KATI_VERSION_DEPS)
113	@mkdir -p $(dir $@)
114	echo '// +build ignore' > $@
115	echo >> $@
116	echo 'const char* kGitVersion = "$(KATI_VERSION)";' >> $@
117
118tsan_ckati:
119	$(MAKE) clean
120	$(MAKE) all CXX='clang++-3.6 -fsanitize=thread'
121	cp ckati $@
122
123ckati_clean:
124	rm -rf $(KATI_INTERMEDIATES_PATH)/ckati
125	rm -rf $(KATI_INTERMEDIATES_PATH)/*.o
126	rm -rf $(KATI_INTERMEDIATES_PATH)/*.d
127	rm -rf $(KATI_INTERMEDIATES_PATH)/version.cc
128	rm -rf $(KATI_CXX_TEST_EXES)
129
130.PHONY: ckati_clean
131
132-include $(KATI_INTERMEDIATES_PATH)/*.d
133