]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - tools/make-functions
make.sh: Calculate MAKETUNING depending on available memory
[ipfire-2.x.git] / tools / make-functions
index b419253fa8cd9ea577d65c73e08f4cc811bec128..9cf2fcfb472f0f10da31c293e06fa1e99539924a 100644 (file)
@@ -60,6 +60,24 @@ WARN="\\033[1;35m"
 FAIL="\\033[1;31m"
 NORMAL="\\033[0;39m"
 
+system_processors() {
+       getconf _NPROCESSORS_ONLN 2>/dev/null || echo "1"
+}
+
+system_memory() {
+       local key val unit
+
+       while read -r key val unit; do
+               case "${key}" in
+                       MemTotal:*)
+                               # Convert to MB
+                               echo "$(( ${val} / 1024 ))"
+                               break
+                               ;;
+               esac
+       done < /proc/meminfo
+}
+
 configure_build() {
        local build_arch="${1}"
 
@@ -124,8 +142,34 @@ configure_build() {
        # Enables hardening
        HARDENING_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=4"
 
-       CFLAGS="-O2 -pipe -Wall -fexceptions -fPIC ${CFLAGS_ARCH}"
+       CFLAGS="-O2 -Wall -fexceptions -fPIC ${CFLAGS_ARCH}"
+
+       # Run compiler and assembler simultaneously on systems that have enough memory
+       if [ ${HOST_MEM} -ge 1024 ]; then
+               CFLAGS="${CFLAGS} -pipe"
+       fi
+
        CXXFLAGS="${CFLAGS}"
+
+       # Determine parallelism
+       if [ -z "${MAKETUNING}" ]; then
+               # We assume that each process consumes about
+               # 192MB of memory. Therefore we find out how
+               # many processes fit into memory.
+               local mem_max=$(( ${HOST_MEM} / 192 ))
+
+               local processors="$(system_processors)"
+               local cpu_max=$(( ${processors} * 2 ))
+
+               local parallelism
+               if [ ${mem_max} -lt ${cpu_max} ]; then
+                       parallelism=${mem_max}
+               else
+                       parallelism=${cpu_max}
+               fi
+
+               MAKETUNING="-j${parallelism}"
+       fi
 }
 
 configure_build_guess() {