Skip to content
/ openwrt Public
forked from openwrt/openwrt

Commit

Permalink
build: add ninja build tool and make it available for cmake
Browse files Browse the repository at this point in the history
ninja is faster at building cmake packages than make, and according to reports
also more reliable at handling parallel builds
This commit includes a patch that adds GNU make jobserver support, in order to
allow more precise control over the number of parallel tasks

Enable parallel build by default for packages using ninja

Signed-off-by: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
nbd168 committed Jun 12, 2021
1 parent a85aaa1 commit 97258f5
Show file tree
Hide file tree
Showing 5 changed files with 2,359 additions and 1 deletion.
34 changes: 34 additions & 0 deletions include/cmake.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
cmake_bool = $(patsubst %,-D%:BOOL=$(if $($(1)),ON,OFF),$(2))

ifeq ($(PKG_USE_NINJA),1)
PKG_BUILD_PARALLEL ?= 1
endif
ifeq ($(HOST_USE_NINJA),1)
HOST_BUILD_PARALLEL ?= 1
endif
PKG_INSTALL:=1

ifneq ($(findstring c,$(OPENWRT_VERBOSE)),)
Expand Down Expand Up @@ -44,6 +50,34 @@ CMAKE_FIND_ROOT_PATH:=$(STAGING_DIR)/usr;$(TOOLCHAIN_DIR)$(if $(CONFIG_EXTERNAL_
CMAKE_HOST_FIND_ROOT_PATH:=$(STAGING_DIR)/host;$(STAGING_DIR_HOSTPKG);$(STAGING_DIR_HOST)
CMAKE_SHARED_LDFLAGS:=-Wl,-Bsymbolic-functions

ifeq ($(HOST_USE_NINJA),1)
CMAKE_HOST_OPTIONS += -DCMAKE_GENERATOR="Ninja"

define Host/Compile/Default
+$(NINJA) -C $(HOST_BUILD_DIR) $(1)
endef

define Host/Install/Default
+DESTDIR="$(HOST_INSTALL_DIR)" $(NINJA) -C $(HOST_BUILD_DIR) install
endef

define Host/Uninstall/Default
+DESTDIR="$(HOST_INSTALL_DIR)" $(NINJA) -C $(HOST_BUILD_DIR) uninstall
endef
endif

ifeq ($(PKG_USE_NINJA),1)
CMAKE_OPTIONS += -DCMAKE_GENERATOR="Ninja"

define Build/Compile/Default
+$(NINJA) -C $(CMAKE_BINARY_DIR) $(1)
endef

define Build/Install/Default
+DESTDIR="$(PKG_INSTALL_DIR)" $(NINJA) -C $(CMAKE_BINARY_DIR) install
endef
endif

define Build/Configure/Default
mkdir -p $(CMAKE_BINARY_DIR)
(cd $(CMAKE_BINARY_DIR); \
Expand Down
6 changes: 6 additions & 0 deletions rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ else
$(SCRIPT_DIR)/rstrip.sh
endif

NINJA = \
MAKEFLAGS="$(MAKE_JOBSERVER)" \
$(STAGING_DIR_HOST)/bin/ninja \
$(if $(findstring c,$(OPENWRT_VERBOSE)),-v) \
$(if $(MAKE_JOBSERVER),,-j1)

ifeq ($(CONFIG_IPV6),y)
DISABLE_IPV6:=
else
Expand Down
2 changes: 1 addition & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ endif
tools-y += autoconf autoconf-archive automake bc bison cmake cpio dosfstools
tools-y += e2fsprogs fakeroot findutils firmware-utils flex gengetopt
tools-y += libressl libtool lzma m4 make-ext4fs missing-macros mkimage
tools-y += mklibs mm-macros mtd-utils mtools padjffs2 patch-image
tools-y += mklibs mm-macros mtd-utils mtools ninja padjffs2 patch-image
tools-y += patchelf pkgconf quilt squashfskit4 sstrip xxd zip zlib zstd
tools-$(BUILD_B43_TOOLS) += b43-tools
tools-$(BUILD_ISL) += isl
Expand Down
39 changes: 39 additions & 0 deletions tools/ninja/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=ninja
PKG_VERSION:=1.10.2
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/ninja-build/ninja/tar.gz/v$(PKG_VERSION)?
PKG_HASH:=ce35865411f0490368a8fc383f29071de6690cbadc27704734978221f25e2bed

include $(INCLUDE_DIR)/host-build.mk

CONFIGURE_ARGS:=
ifneq ($(findstring c,$(OPENWRT_VERBOSE)),)
CONFIGURE_ARGS+=--verbose
endif

define Host/Configure
endef

define Host/Compile
cd $(HOST_BUILD_DIR) && \
CXX="$(HOSTCXX)" \
CXXFLAGS="$(HOST_CXXFLAGS) $(HOST_CPPFLAGS)" \
LDFLAGS="$(HOST_LDFLAGS)" \
$(STAGING_DIR_HOST)/bin/$(PYTHON) configure.py --bootstrap $(CONFIGURE_ARGS)
endef

define Host/Install
$(INSTALL_DIR) $(STAGING_DIR_HOST)/bin
$(INSTALL_BIN) $(HOST_BUILD_DIR)/ninja $(STAGING_DIR_HOST)/bin/
endef

define Host/Clean
$(call Host/Clean/Default)
rm -f $(STAGING_DIR_HOST)/bin/ninja
endef

$(eval $(call HostBuild))
Loading

0 comments on commit 97258f5

Please sign in to comment.