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 16MAKEPARALLEL_SRC_PATH := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST)))) 17ifndef MAKEPARALLEL_SRC_PATH 18 MAKEPARALLEL_SRC_PATH := . 19endif 20 21# Set defaults if they weren't set by the including Makefile 22MAKEPARALLEL_CXX ?= $(CXX) 23MAKEPARALLEL_LD ?= $(CXX) 24MAKEPARALLEL_INTERMEDIATES_PATH ?= . 25MAKEPARALLEL_BIN_PATH ?= . 26 27MAKEPARALLEL_CXX_SRCS := \ 28 makeparallel.cpp 29 30MAKEPARALLEL_CXXFLAGS := -Wall -Werror -MMD -MP 31 32MAKEPARALLEL_CXX_SRCS := $(addprefix $(MAKEPARALLEL_SRC_PATH)/,\ 33 $(MAKEPARALLEL_CXX_SRCS)) 34 35MAKEPARALLEL_CXX_OBJS := $(patsubst $(MAKEPARALLEL_SRC_PATH)/%.cpp,$(MAKEPARALLEL_INTERMEDIATES_PATH)/%.o,$(MAKEPARALLEL_CXX_SRCS)) 36 37MAKEPARALLEL := $(MAKEPARALLEL_BIN_PATH)/makeparallel 38 39ifeq ($(shell uname),Linux) 40MAKEPARALLEL_LIBS := -lrt -lpthread 41endif 42 43# Rule to build makeparallel into MAKEPARALLEL_BIN_PATH 44$(MAKEPARALLEL): $(MAKEPARALLEL_CXX_OBJS) 45 @mkdir -p $(dir $@) 46 $(MAKEPARALLEL_LD) -std=c++11 $(MAKEPARALLEL_CXXFLAGS) -o $@ $^ $(MAKEPARALLEL_LIBS) 47 48# Rule to build source files into object files in MAKEPARALLEL_INTERMEDIATES_PATH 49$(MAKEPARALLEL_CXX_OBJS): $(MAKEPARALLEL_INTERMEDIATES_PATH)/%.o: $(MAKEPARALLEL_SRC_PATH)/%.cpp 50 @mkdir -p $(dir $@) 51 $(MAKEPARALLEL_CXX) -c -std=c++11 $(MAKEPARALLEL_CXXFLAGS) -o $@ $< 52 53makeparallel_clean: 54 rm -rf $(MAKEPARALLEL) 55 rm -rf $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.o 56 rm -rf $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.d 57 58.PHONY: makeparallel_clean 59 60-include $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.d 61 62.PHONY: makeparallel_test 63MAKEPARALLEL_TEST := MAKEFLAGS= MAKELEVEL= MAKEPARALLEL=$(MAKEPARALLEL) $(MAKE) -f Makefile.test test 64MAKEPARALLEL_NINJA_TEST := MAKEFLAGS= MAKELEVEL= MAKEPARALLEL="$(MAKEPARALLEL) --ninja" $(MAKE) -f Makefile.test test 65makeparallel_test: $(MAKEPARALLEL) 66 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) -j1234 67 @EXPECTED="-j123" $(MAKEPARALLEL_TEST) -j123 68 @EXPECTED="" $(MAKEPARALLEL_TEST) -j1 69 @EXPECTED="-j$$(($$(nproc) + 2))" $(MAKEPARALLEL_TEST) -j 70 @EXPECTED="" $(MAKEPARALLEL_TEST) 71 72 @EXPECTED="-j1234" $(MAKEPARALLEL_NINJA_TEST) -j1234 73 @EXPECTED="-j123" $(MAKEPARALLEL_NINJA_TEST) -j123 74 @EXPECTED="-j1" $(MAKEPARALLEL_NINJA_TEST) -j1 75 @EXPECTED="-j1" $(MAKEPARALLEL_NINJA_TEST) 76 @EXPECTED="" $(MAKEPARALLEL_NINJA_TEST) -j 77 @EXPECTED="" $(MAKEPARALLEL_NINJA_TEST) -j -l 78 79 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) --no-print-directory -j1234 80 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) --no-print-directory -k -j1234 81 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) -k -j1234 82 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) -j1234 -k 83 @EXPECTED="-j1234" $(MAKEPARALLEL_TEST) -kt -j1234 84 85 @EXPECTED="-j1234" $(MAKEPARALLEL_NINJA_TEST) --no-print-directory -j1234 86 @EXPECTED="-j1234 -k0" $(MAKEPARALLEL_NINJA_TEST) --no-print-directory -k -j1234 87 @EXPECTED="-j1234 -k0" $(MAKEPARALLEL_NINJA_TEST) -k -j1234 88 @EXPECTED="-j1234 -k0" $(MAKEPARALLEL_NINJA_TEST) -j1234 -k 89 @EXPECTED="-j1234 -k0" $(MAKEPARALLEL_NINJA_TEST) -kt -j1234 90 91 @EXPECTED="" $(MAKEPARALLEL_TEST) A=-j1234 92 93 @EXPECTED="-j1234 args" ARGS="args" $(MAKEPARALLEL_TEST) -j1234 94