Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4ce3cea7 authored by Colin Cross's avatar Colin Cross Committed by Gerrit Code Review
Browse files

Merge changes I6e4f957c,Id41a2d81

* changes:
  Use makeparallel to propagate -j flag to ninja or kati
  Implement makeparallel
parents da196abf f7ca64fa
Loading
Loading
Loading
Loading
+22 −4
Original line number Original line Diff line number Diff line
KATI ?= $(HOST_OUT_EXECUTABLES)/ckati
KATI ?= $(HOST_OUT_EXECUTABLES)/ckati
MAKEPARALLEL ?= $(HOST_OUT_EXECUTABLES)/makeparallel


KATI_OUTPUT_PATTERNS := $(PRODUCT_OUT)/build%.ninja $(PRODUCT_OUT)/ninja%.sh
KATI_OUTPUT_PATTERNS := $(PRODUCT_OUT)/build%.ninja $(PRODUCT_OUT)/ninja%.sh
NINJA_GOALS := fastincremental generateonly droid showcommands
NINJA_GOALS := fastincremental generateonly droid showcommands
@@ -85,14 +86,20 @@ PHONY: showcommands
showcommands: droid
showcommands: droid
endif
endif


ifdef KATI_REMOTE_NUM_JOBS_FLAG
KATI_MAKEPARALLEL := $(MAKEPARALLEL)
else
NINJA_MAKEPARALLEL := $(MAKEPARALLEL)
endif

ifeq (,$(filter generateonly,$(ORIGINAL_MAKECMDGOALS)))
ifeq (,$(filter generateonly,$(ORIGINAL_MAKECMDGOALS)))
fastincremental droid $(ANDROID_TARGETS): ninja.intermediate
fastincremental droid $(ANDROID_TARGETS): ninja.intermediate
	@#empty
	@#empty


.INTERMEDIATE: ninja.intermediate
.INTERMEDIATE: ninja.intermediate
ninja.intermediate: $(KATI_OUTPUTS)
ninja.intermediate: $(KATI_OUTPUTS) $(MAKEPARALLEL)
	@echo Starting build with ninja
	@echo Starting build with ninja
	$(hide) PATH=prebuilts/ninja/$(HOST_PREBUILT_TAG)/:$$PATH NINJA_STATUS="$(NINJA_STATUS)" $(KATI_NINJA_SH) -C $(TOP) $(NINJA_ARGS) $(ANDROID_TARGETS)
	+$(hide) PATH=prebuilts/ninja/$(HOST_PREBUILT_TAG)/:$$PATH NINJA_STATUS="$(NINJA_STATUS)" $(NINJA_MAKEPARALLEL) $(KATI_NINJA_SH) -C $(TOP) $(NINJA_ARGS) $(ANDROID_TARGETS)
else
else
generateonly droid $(ANDROID_TARGETS): $(KATI_OUTPUTS)
generateonly droid $(ANDROID_TARGETS): $(KATI_OUTPUTS)
	@#empty
	@#empty
@@ -105,10 +112,10 @@ endif
$(KATI_OUTPUTS): kati.intermediate $(KATI_FORCE)
$(KATI_OUTPUTS): kati.intermediate $(KATI_FORCE)


.INTERMEDIATE: kati.intermediate
.INTERMEDIATE: kati.intermediate
kati.intermediate: $(KATI)
kati.intermediate: $(KATI) $(MAKEPARALLEL)
	@echo Running kati to generate build$(KATI_NINJA_SUFFIX).ninja...
	@echo Running kati to generate build$(KATI_NINJA_SUFFIX).ninja...
	@#TODO: use separate ninja file for mm or single target build
	@#TODO: use separate ninja file for mm or single target build
	$(hide) $(KATI) --ninja --ninja_dir=$(PRODUCT_OUT) --ninja_suffix=$(KATI_NINJA_SUFFIX) --regen --ignore_dirty=$(OUT_DIR)/% --ignore_optional_include=$(OUT_DIR)/%.P --detect_android_echo --use_find_emulator $(KATI_REMOTE_NUM_JOBS_FLAG) -f build/core/main.mk $(or $(KATI_TARGETS),--gen_all_phony_targets) USE_NINJA=false
	+$(hide) $(KATI_MAKEPARALLEL) $(KATI) --ninja --ninja_dir=$(PRODUCT_OUT) --ninja_suffix=$(KATI_NINJA_SUFFIX) --regen --ignore_dirty=$(OUT_DIR)/% --ignore_optional_include=$(OUT_DIR)/%.P --detect_android_echo --use_find_emulator $(KATI_REMOTE_NUM_JOBS_FLAG) -f build/core/main.mk $(or $(KATI_TARGETS),--gen_all_phony_targets) USE_NINJA=false


KATI_CXX := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_CPPFLAGS)
KATI_CXX := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_CPPFLAGS)
KATI_LD := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_LDFLAGS)
KATI_LD := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_LDFLAGS)
@@ -121,5 +128,16 @@ KATI_INTERMEDIATES_PATH := $(HOST_OUT_INTERMEDIATES)/EXECUTABLES/ckati_intermedi
KATI_BIN_PATH := $(HOST_OUT_EXECUTABLES)
KATI_BIN_PATH := $(HOST_OUT_EXECUTABLES)
include build/kati/Makefile.ckati
include build/kati/Makefile.ckati


MAKEPARALLEL_CXX := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_CPPFLAGS)
MAKEPARALLEL_LD := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_LDFLAGS)
# Build static makeparallel. Unfortunately Mac OS X doesn't officially support static exectuables.
ifeq ($(BUILD_OS),linux)
MAKEPARALLEL_LD += -static
endif

MAKEPARALLEL_INTERMEDIATES_PATH := $(HOST_OUT_INTERMEDIATES)/EXECUTABLES/makeparallel_intermediates
MAKEPARALLEL_BIN_PATH := $(HOST_OUT_EXECUTABLES)
include build/tools/makeparallel/Makefile

.PHONY: FORCE
.PHONY: FORCE
FORCE:
FORCE:
+4 −0
Original line number Original line Diff line number Diff line
makeparallel
*.o
*.d
test.out
+64 −0
Original line number Original line Diff line number Diff line
# Copyright 2015 Google Inc. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Find source file location from path to this Makefile
MAKEPARALLEL_SRC_PATH := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
ifndef MAKEPARALLEL_SRC_PATH
  MAKEPARALLEL_SRC_PATH := .
endif

# Set defaults if they weren't set by the including Makefile
MAKEPARALLEL_CXX ?= $(CXX)
MAKEPARALLEL_LD ?= $(CXX)
MAKEPARALLEL_INTERMEDIATES_PATH ?= .
MAKEPARALLEL_BIN_PATH ?= .

MAKEPARALLEL_CXX_SRCS := \
	makeparallel.cpp

MAKEPARALLEL_CXXFLAGS := -Wall -Werror -MMD -MP

MAKEPARALLEL_CXX_SRCS := $(addprefix $(MAKEPARALLEL_SRC_PATH)/,\
	$(MAKEPARALLEL_CXX_SRCS))

MAKEPARALLEL_CXX_OBJS := $(patsubst $(MAKEPARALLEL_SRC_PATH)/%.cpp,$(MAKEPARALLEL_INTERMEDIATES_PATH)/%.o,$(MAKEPARALLEL_CXX_SRCS))

MAKEPARALLEL := $(MAKEPARALLEL_BIN_PATH)/makeparallel

ifeq ($(shell uname),Linux)
MAKEPARALLEL_LIBS := -lrt -lpthread
endif

# Rule to build makeparallel into MAKEPARALLEL_BIN_PATH
$(MAKEPARALLEL): $(MAKEPARALLEL_CXX_OBJS)
	@mkdir -p $(dir $@)
	$(MAKEPARALLEL_LD) -std=c++11 $(MAKEPARALLEL_CXXFLAGS) -o $@ $^ $(MAKEPARALLEL_LIBS)

# Rule to build source files into object files in MAKEPARALLEL_INTERMEDIATES_PATH
$(MAKEPARALLEL_CXX_OBJS): $(MAKEPARALLEL_INTERMEDIATES_PATH)/%.o: $(MAKEPARALLEL_SRC_PATH)/%.cpp
	@mkdir -p $(dir $@)
	$(MAKEPARALLEL_CXX) -c -std=c++11 $(MAKEPARALLEL_CXXFLAGS) -o $@ $<

makeparallel_clean:
	rm -rf $(MAKEPARALLEL)
	rm -rf $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.o
	rm -rf $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.d

.PHONY: makeparallel_clean

-include $(MAKEPARALLEL_INTERMEDIATES_PATH)/*.d

.PHONY: test
test: $(MAKEPARALLEL)
	MAKEFLAGS= $(MAKE) -j1234 -C $(MAKEPARALLEL_SRC_PATH) -f Makefile.test MAKEPARALLEL=$(MAKEPARALLEL) test
+5 −0
Original line number Original line Diff line number Diff line
MAKEPARALLEL ?= ./makeparallel

.PHONY: test
test:
	+if [ "$$($(MAKEPARALLEL) echo)" = "-j1234" ]; then echo SUCCESS; else echo FAILED; fi
+54 −0
Original line number Original line Diff line number Diff line
<!---
Copyright (C) 2015 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

makeparallel
============
makeparallel communicates with the [GNU make jobserver](http://make.mad-scientist.net/papers/jobserver-implementation/)
in order claim all available jobs, and then passes the number of jobs
claimed to a subprocess with `-j<jobs>`.

The number of available jobs is determined by reading tokens from the jobserver
until a read would block.  If the makeparallel rule is the only one running the
number of jobs will be the total size of the jobserver pool, i.e. the value
passed to make with `-j`.  Any jobs running in parallel with with the
makeparellel rule will reduce the measured value, and thus reduce the
parallelism available to the subprocess.

To run a multi-thread or multi-process binary inside GNU make using
makeparallel, add
```Makefile
	+makeparallel subprocess arguments
```
to a rule.  For example, to wrap ninja in make, use something like:
```Makefile
	+makeparallel ninja -f build.ninja
```

To determine the size of the jobserver pool, add
```Makefile
	+makeparallel echo > make.jobs
```
to a rule that is guarantee to run alone (i.e. all other rules are either
dependencies of the makeparallel rule, or the depend on the makeparallel
rule.  The output file will contain the `-j<num>` flag passed to the parent
make process, or `-j1` if no flag was found.  Since GNU make will run
makeparallel during the execution phase, after all variables have been
set and evaluated, it is not possible to get the output of makeparallel
into a make variable.  Instead, use a shell substitution to read the output
file directly in a recipe.  For example:
```Makefile
	echo Make was started with $$(cat make.jobs)
```
Loading