build: Detect number of CPUs for build processes on FreeBSD
[vpp.git] / build-root / Makefile
index d810abb..d69a94c 100644 (file)
@@ -39,7 +39,7 @@
 ######################################################################
 
 # Scripts require non-POSIX parts of bash
-SHELL := /bin/bash
+SHELL := $(shell which bash)
 
 # Where this makefile lives
 MU_BUILD_ROOT_DIR = $(shell pwd)
@@ -190,7 +190,7 @@ ARCH_TARGET_tmp = $(call ifdef_fn,$(ARCH)_target,$(ARCH)-$(OS))
 TARGET = $(call ifdef_fn,$(PLATFORM)_target,$(ARCH_TARGET_tmp))
 TARGET_PREFIX = $(if $(not_native),$(TARGET)-,)
 
-# CPU microarchitecture detection. 
+# CPU microarchitecture detection.
 # Either set <platform>_march in build-data/platforms/<platform>.mk,
 # or detect and use the build-host instruction set
 
@@ -236,18 +236,18 @@ PACKAGE = $*
 # Build/install tags.  This lets you have different CFLAGS/CPPFLAGS/LDFLAGS
 # for e.g. debug versus optimized compiles.  Each tag has its own set of build/install
 # areas.
-TAG = 
+TAG =
 TAG_PREFIX = $(if $(TAG),$(TAG)-)
 
 # yes you need the space
 tag_var_with_added_space_fn = $(if $($(TAG)_TAG_$(1)),$($(TAG)_TAG_$(1)) )
 
 # TAG=debug for debugging
-debug_TAG_CFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
+debug_TAG_CFLAGS = -g -O0 -DCLIB_DEBUG -march=$(MARCH) \
                   -fstack-protector-all -fPIC
-debug_TAG_CXXFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
+debug_TAG_CXXFLAGS = -g -O0 -DCLIB_DEBUG -march=$(MARCH) \
                   -fstack-protector-all -fPIC
-debug_TAG_LDFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
+debug_TAG_LDFLAGS = -g -O0 -DCLIB_DEBUG -march=$(MARCH) \
                   -fstack-protector-all -fPIC
 
 BUILD_PREFIX_package = build-$(TAG_PREFIX)
@@ -309,8 +309,8 @@ endif
 
 # Always prefer our own tools to those installed on system.
 # Note: ccache-bin must be before tool bin.
-# 
-# Removed LD_LIBRARY_PATH from BUILD_ENV (drb, 10/31/17):  
+#
+# Removed LD_LIBRARY_PATH from BUILD_ENV (drb, 10/31/17):
 # export LD_LIBRARY_PATH=$(TOOL_INSTALL_DIR)/lib64:$(TOOL_INSTALL_DIR)/lib
 #   Reported to cause trouble. Only of historical interest, since we no longer
 #   build a full tool chain from source.
@@ -365,7 +365,7 @@ NATIVE_TOOLS_$(IS_LINUX) += $(NATIVE_TOOLS_LINUX)
 CROSS_TOOLS_$(IS_LINUX) += glibc gcc
 
 # must be first for bootstrapping
-NATIVE_TOOLS = findutils make
+NATIVE_TOOLS = findutils $(MAKE)
 
 # basic tools needed for build system
 NATIVE_TOOLS += git automake autoconf libtool texinfo tar
@@ -416,7 +416,7 @@ find_filter += -and -not -path '*/.mu_build_*'
 find_newer_filtered_fn =                       \
   (! -f $(1)                                   \
     || -n $(call find_newer_files_fn,$(1),$(3))        \
-    || -n "`find -H $(2)                       \
+    || -n "`find -L $(2)                       \
              -type f                           \
               -and -newer $(1)                 \
              -and \( $(4) \)                   \
@@ -614,10 +614,10 @@ check_platform =                                                          \
        -a ! -x "`which 2> /dev/null $${target_gcc}`" ] ; then                  \
     $(call build_msg_fn,                                                       \
           No cross-compiler found for platform $(PLATFORM) target $(TARGET);   \
-            try make PLATFORM=$(PLATFORM) install-tools) ;                     \
+            try $(MAKE) PLATFORM=$(PLATFORM) install-tools) ;                  \
     exit 1 ;                                                                   \
   fi
-    
+
 configure_check_timestamp =                                            \
   @$(BUILD_ENV) ;                                                      \
   $(check_platform) ;                                                  \
@@ -650,15 +650,20 @@ configure_check_timestamp =                                               \
 # /proc/cpuinfo does not exist on platforms without a /proc and on some
 # platforms, notably inside containers, it has no content. In those cases
 # we assume there's 1 processor; we use 2*ncpu for the -j option.
+#
+# On FreeBSD we can call nproc to get the number of processors.
+#
 # NB: GNU Make 4.2 will let us use '$(file </proc/cpuinfo)' to both test
 # for file presence and content; for now this will have to do.
 ifndef MAKE_PARALLEL_JOBS
-MAKE_PARALLEL_JOBS = -j $(if $(shell [ -f /proc/cpuinfo ] && head /proc/cpuinfo), \
-       $(shell grep -c ^processor /proc/cpuinfo), 2)
+ifeq ($(shell uname), FreeBSD)
+MAKE_PARALLEL_JOBS = $(shell nproc)
 else
-MAKE_PARALLEL_JOBS := -j $(MAKE_PARALLEL_JOBS)
+MAKE_PARALLEL_JOBS = $(if $(shell [ -f /proc/cpuinfo ] && head /proc/cpuinfo), \
+       $(shell grep -c ^processor /proc/cpuinfo), 2)
+endif  # FreeBSD
 endif
-MAKE_PARALLEL_FLAGS ?= $(if $($(PACKAGE)_make_parallel_fails),,$(MAKE_PARALLEL_JOBS))
+MAKE_PARALLEL_FLAGS ?= $(if $($(PACKAGE)_make_parallel_fails),,-j $(MAKE_PARALLEL_JOBS))
 
 # Make command shorthand for packages & tools.
 PACKAGE_MAKE =                                 \
@@ -767,7 +772,7 @@ find_source_for_package =                                                                   \
       exit 1;                                                                                  \
     fi ;                                                                                       \
     $(call build_msg_fn,Fix file dates in $${g}/$(PACKAGE_SOURCE)) ;                                   \
-    (cd $${s} ; $(MU_BUILD_ROOT_DIR)/autowank --touch) ;                                       \
+    : the timestamp-adjustment script used to be invoked at this point ;                       \
   fi ;                                                                                         \
   s=`cd $${s} && pwd` ;                                                                                \
   $(call build_msg_fn,Source found in $${s})
@@ -802,10 +807,10 @@ pull-all:
        $(call build_msg_fn,Git pull build tools) ;                             \
        $(call tool_make_target_fn,pull-all) ;                                  \
        $(call build_msg_fn,Git pull packages for platform $(PLATFORM)) ;       \
-       make PLATFORM=$(PLATFORM) $(patsubst %,%-pull-all,$(ROOT_PACKAGES))
+       $(MAKE) PLATFORM=$(PLATFORM) $(patsubst %,%-pull-all,$(ROOT_PACKAGES))
 
 .PHONY: %-diff
-%-diff:                                                                        
+%-diff:
        @$(BUILD_ENV) ;                                                 \
        d=$(call find_source_fn,$(PACKAGE_SOURCE)) ;                    \
        $(call build_msg_fn,Git diff $(PACKAGE)) ;                      \
@@ -814,12 +819,12 @@ pull-all:
         else                                                           \
         $(call build_msg_fn, $(PACKAGE) not a git directory) ;         \
        fi
-            
+
 
 
 # generate diffs for everything in source path
 .PHONY: diff-all
-diff-all:                                                              
+diff-all:
        @$(BUILD_ENV) ;                                                 \
        $(call build_msg_fn,Generate diffs) ;                           \
        for r in $(ABSOLUTE_SOURCE_PATH); do                            \
@@ -917,7 +922,7 @@ basic_system_image_install =                                \
   mkdir -p bin lib mnt proc root sbin sys tmp etc ;    \
   mkdir -p usr usr/{bin,sbin} usr/lib ;                        \
   mkdir -p var var/{lib,lock,log,run,tmp} ;            \
-  mkdir -p var/lock/subsys var/lib/urandom 
+  mkdir -p var/lock/subsys var/lib/urandom
 
 .PHONY: basic_system-image_install
 basic_system-image_install: # linuxrc-install
@@ -927,7 +932,7 @@ basic_system-image_install: # linuxrc-install
 ROOT_PACKAGES = $(if $($(PLATFORM)_root_packages),$($(PLATFORM)_root_packages),$(default_root_packages))
 
 .PHONY: install-packages
-install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES)) 
+install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES))
        @$(BUILD_ENV) ;                                                         \
        set -eu$(BUILD_DEBUG) ;                                                 \
        d=$(MU_BUILD_ROOT_DIR)/packages-$(PLATFORM) ;                           \
@@ -949,7 +954,7 @@ install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES))
                    >& /dev/null ;                                              \
        else                                                                    \
            $(call build_msg_fn, NOT stripping symbols) ;                       \
-       fi 
+       fi
 
 # readonly root squashfs image
 # Note: $(call build_msg_fn) does not seem to work inside of fakeroot so we use echo
@@ -964,13 +969,13 @@ $(PLATFORM_IMAGE_DIR)/ro.img ro-image: $(patsubst %,%-find-source,$(ROOT_PACKAGE
        chmod 0755 $${tmp_dir} ;                                        \
        cd $${tmp_dir} ;                                                \
        trap "rm -rf $${tmp_dir}" err ;                                 \
-       fakeroot /bin/bash -c "{                                        \
+       fakeroot $(SHELL) -c "{                                 \
          set -eu$(BUILD_DEBUG) ;                                       \
          $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${tmp_dir} \
            $(patsubst %,%-image_install,                               \
              basic_system                                              \
              $(ROOT_PACKAGES)) ;                                       \
-         : make dev directory ;                                        \
+         : $(MAKE) dev directory ;                                     \
          $(linuxrc_makedev) ;                                          \
          echo @@@@ Relocating ELF executables to run in / @@@@ ;       \
          scripts/set-rpath /$(arch_lib_dir):/lib ;                     \
@@ -1056,7 +1061,7 @@ rw-image: rw-image-check-type ro-image
        chmod 0755 $${tmp_dir} ;                                \
        cd $${tmp_dir} ;                                        \
        trap "rm -rf $${tmp_dir}" err ;                         \
-       fakeroot /bin/bash -c "{                                \
+       fakeroot $(SHELL) -c "{                         \
          set -eu$(BUILD_DEBUG) ;                               \
          $(linuxrc_makedev) ;                                  \
          $(call rw_image_embed_ro_image_fn,$${ro_image}) ;     \
@@ -1089,7 +1094,7 @@ ccache-install:
        $(MAKE) -C $(MU_BUILD_ROOT_DIR) ccache-build
        mkdir -p $(TOOL_INSTALL_DIR)/ccache-bin
        ln -sf $(MU_BUILD_ROOT_DIR)/build-tool-native/ccache/ccache \
-               $(TOOL_INSTALL_DIR)/ccache-bin/$(TARGET_PREFIX)gcc 
+               $(TOOL_INSTALL_DIR)/ccache-bin/$(TARGET_PREFIX)gcc
 
 TOOL_MAKE = $(MAKE) is_build_tool=yes
 
@@ -1153,7 +1158,4 @@ distclean:
        rm -rf $(MU_BUILD_ROOT_DIR)/*.deb
        rm -rf $(MU_BUILD_ROOT_DIR)/*.rpm
        rm -rf $(MU_BUILD_ROOT_DIR)/*.changes
-       rm -rf $(MU_BUILD_ROOT_DIR)/python
-       if [ -e /usr/bin/dh ];then (cd $(MU_BUILD_ROOT_DIR)/deb/;debian/rules clean); fi
-       rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/*.install
-       rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/changelog
+       rm -rf $(MU_BUILD_ROOT_DIR)/*.buildinfo