<pre style='margin:0'>
Chris Jones (cjones051073) pushed a commit to branch master
in repository macports-ports.
</pre>
<p><a href="https://github.com/macports/macports-ports/commit/5d9765ddd7d0c8f16d25beeca7be37fd74c42f1d">https://github.com/macports/macports-ports/commit/5d9765ddd7d0c8f16d25beeca7be37fd74c42f1d</a></p>
<pre style="white-space: pre; background: #F8F8F8"><span style='display:block; white-space:pre;color:#808000;'>commit 5d9765ddd7d0c8f16d25beeca7be37fd74c42f1d
</span>Author: Chris Jones <jonesc@macports.org>
AuthorDate: Sun May 2 13:12:14 2021 +0100
<span style='display:block; white-space:pre;color:#404040;'> bazel PG: Support ccache and better compiler selection
</span>---
_resources/port1.0/group/bazel-1.0.tcl | 123 +++++++++++++++++++++++----------
1 file changed, 86 insertions(+), 37 deletions(-)
<span style='display:block; white-space:pre;color:#808080;'>diff --git a/_resources/port1.0/group/bazel-1.0.tcl b/_resources/port1.0/group/bazel-1.0.tcl
</span><span style='display:block; white-space:pre;color:#808080;'>index 3a2524777b1..9ec6be519c9 100644
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>--- a/_resources/port1.0/group/bazel-1.0.tcl
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>+++ b/_resources/port1.0/group/bazel-1.0.tcl
</span><span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -30,7 +30,7 @@ options bazel.post_build_cmd
</span> default bazel.post_build_cmd ""
options bazel.max_idle_secs
<span style='display:block; white-space:pre;background:#ffe0e0;'>-default bazel.max_idle_secs 30
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+default bazel.max_idle_secs 10
</span>
options bazel.max_cpu_fraction
default bazel.max_cpu_fraction 0.5
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -70,8 +70,6 @@ license_noconflict ${java.fallback}
</span> configure.env-append JAVA_HOME=${java.home}
build.env-append JAVA_HOME=${java.home}
destroot.env-append JAVA_HOME=${java.home}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-# Bazel PG plays badily with ccache
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-configure.ccache no
</span>
# Require c++ standard
proc bazel::set_standards {} {
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -102,6 +100,11 @@ proc bazel::get_bazel_executable_name { } {
</span> }
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+proc bazel::get_bazel_build_area { } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ global workpath
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return ${workpath}/bazel_build
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> proc bazel::set_dep { } {
set bz_dep [bazel::get_bazel_executable_name]
ui_debug "Defining bazel dependency port:${bz_dep}"
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -123,6 +126,11 @@ proc bazel::get_base_arch {} {
</span> return ""
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+proc bazel::get_ccache_dir {} {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ global portdbpath
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return [file join $portdbpath build .ccache]
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> supported_archs x86_64
if {![variant_isset native]} {
set base_march [bazel::get_base_arch]
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -137,21 +145,24 @@ if {![variant_isset native]} {
</span> proc bazel::set_env {} {
global prefix env
ui_debug "Setting Bazel Env"
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if { [bazel::use_mp_clang] } {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- configure.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- build.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- destroot.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # if { [bazel::use_mp_clang] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # configure.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # build.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # destroot.env-append BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ proc add_to_envs { var } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ foreach phase {configure build destroot} {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ ${phase}.env-append ${var}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span> }
# https://github.com/bazelbuild/bazel/issues/2852
<span style='display:block; white-space:pre;background:#ffe0e0;'>- configure.env-append BAZEL_SH=/bin/bash
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- build.env-append BAZEL_SH=/bin/bash
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- destroot.env-append BAZEL_SH=/bin/bash
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ add_to_envs BAZEL_SH=/bin/bash
</span> # patch PATH to find correct 'bazel' version
<span style='display:block; white-space:pre;background:#ffe0e0;'>- set newpath "PATH=${prefix}/libexec/[bazel::get_bazel_executable_name]/bin:$env(PATH)"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- configure.env-append ${newpath}
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- build.env-append ${newpath}
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- destroot.env-append ${newpath}
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- ui_debug "Prepended ${newpath}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ add_to_envs "PATH=${prefix}/libexec/[bazel::get_bazel_executable_name]/bin:$env(PATH)"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # ccache dir
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { [option configure.ccache] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ add_to_envs CCACHE_DIR=[bazel::get_ccache_dir]
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span> }
port::register_callback bazel::set_env
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -176,6 +187,8 @@ pre-configure {
</span> reinplace -q "s|/usr/bin/${cmd}|${prefix}/bin/${cmd}|g" ${f}
}
reinplace -q "s|/usr/bin/clang|\"${configure.cc}\"|g" ${f}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ reinplace -q "s|\"clang++\"|\"${configure.cxx}\"|g" ${f}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ reinplace -q "s| clang++ | ${configure.cxx} |g" ${f}
</span> reinplace -q "s|\"clang\"|\"${configure.cc}\"|g" ${f}
reinplace -q "s| clang | ${configure.cc} |g" ${f}
reinplace -q "s|/usr/local/include|${prefix}/include|g" ${f}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -205,12 +218,37 @@ pre-build {
</span> ui_error "Once the build is complete, you can safely re-select your preferred gcc."
return -code error "build error"
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { [option bazel.build_cmd] ne "" && [file exists ${worksrcpath}] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # Create ccache wrapper with CCACHE_DIR enforced...
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set wrapdir ${workpath}/bazelwrap
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ xinstall -m 755 -d ${wrapdir}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set f [ open ${wrapdir}/ccache w 0755 ]
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ puts ${f} "#!/bin/bash"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ puts ${f} "export CCACHE_DIR=[bazel::get_ccache_dir]"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ puts ${f} "exec ${prefix}/bin/ccache \"\$\{\@\}\""
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ close ${f}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # Run fetch
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ system -W ${worksrcpath} "[bazel::get_build_env] [option bazel.build_cmd] [option bazel.build_cmd_opts] fetch [option bazel.build_target]"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # Patch the bazel clang wrapper script for use MacPorts selection and support ccache
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ foreach f [ exec find [bazel::get_bazel_build_area] -name "wrapped_clang.cc" ] {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # Switch to selected compiler
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ reinplace -q "s|\"clang++\"|\"${configure.cxx}\"|g" ${f}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ reinplace -q "s|\"clang\"|\"${configure.cc}\"|g" ${f}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # If required use ccache
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { [option configure.ccache] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ reinplace -q "s|\"/usr/bin/xcrun\"\, tool_name|\"/usr/bin/xcrun\"\, \"${wrapdir}/ccache\"\, tool_name|g" ${f}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # Bazel **really** doesn't want you changing stuff ;)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # https://stackoverflow.com/questions/47775668/bazel-how-to-skip-corrupt-installation-on-centos6
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ system "touch -m -t 210012120101 ${f}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span> }
proc bazel::get_cmd_opts {} {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- global bazel.max_idle_secs workpath
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ global bazel.max_idle_secs
</span> # Generate the bazel build command
<span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_cmd_opts "--max_idle_secs=${bazel.max_idle_secs} --output_user_root=${workpath}/bazel_build"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_cmd_opts "--max_idle_secs=${bazel.max_idle_secs} --output_user_root=[bazel::get_bazel_build_area]"
</span> # Extra user defined options
set bazel_cmd_opts "${bazel_cmd_opts} [option bazel.extra_build_cmd_opts]"
ui_debug "Defined Bazel build command options ${bazel_cmd_opts}"
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -226,14 +264,15 @@ proc bazel::get_build_opts {} {
</span> # Extra user defined build options
set bazel_build_opts "${bazel_build_opts} [option bazel.extra_build_opts]"
# Always disable as bazel sets build jobs differently
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ set cache_n_jobs ${build.jobs}
</span> use_parallel_build no
# Limit bazel resource utilisation
if { [option bazel.limit_build_jobs] } {
# Limit the number of parallel jobs to the number of physical, not logical, cpus.
# First current setting to ensure we would be reducing the current setting.
if { ![catch {sysctl hw.physicalcpu} physicalcpus] } {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if { ${build.jobs} > ${physicalcpus} } {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- build.jobs ${physicalcpus}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { ${cache_n_jobs} > ${physicalcpus} } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set cache_n_jobs ${physicalcpus}
</span> }
}
set bazel_build_opts "${bazel_build_opts} --jobs ${build.jobs}"
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -244,6 +283,7 @@ proc bazel::get_build_opts {} {
</span> set bazel_build_opts "${bazel_build_opts} --local_cpu_resources=HOST_CPUS*[option bazel.max_cpu_fraction]"
}
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ build.jobs ${cache_n_jobs}
</span> # hack to try and transfer MP c, c++ and ld options to bazel...
foreach opt [list {*}${configure.cflags} ] {
set bazel_build_opts "${bazel_build_opts} --conlyopt \"${opt}\""
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -268,38 +308,47 @@ proc bazel::get_build_opts {} {
</span> } else {
set bazel_build_opts "${bazel_build_opts} --copt=-march=native"
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { [option configure.ccache] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_opts "${bazel_build_opts} --action_env CCACHE_DIR=[bazel::get_ccache_dir]"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span> ui_debug "Defined Bazel build options ${bazel_build_opts}"
return ${bazel_build_opts}
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+proc bazel::get_build_env { } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env ""
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ #if { [bazel::use_mp_clang] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # set bazel_build_env "BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ #} else {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ # #set bazel_build_env "SDKROOT=${configure.sdkroot} ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ #}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if {![variant_isset native]} {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set base_march [bazel::get_base_arch]
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env "CC_OPT_FLAGS=${base_march} ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ } else {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env "CC_OPT_FLAGS=-march=native ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env "BAZEL_SH=/bin/bash ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if { [option configure.ccache] } {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env "CCACHE_DIR=[bazel::get_ccache_dir] ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return ${bazel_build_env}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+}
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> proc bazel::configure_build {} {
if { [option bazel.build_cmd] ne "" } {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- global configure.sdkroot
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ global configure.sdkroot worksrcpath
</span> global bazel.build_cmd bazel.build_opts bazel.build_target
global build.jobs build.cmd build.args build.post_args
<span style='display:block; white-space:pre;background:#ffe0e0;'>- ui_debug "Configuring bazel build command and arguments"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_build_env ""
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if { [bazel::use_mp_clang] } {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_build_env "BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- #set bazel_build_env "SDKROOT=${configure.sdkroot} ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if {![variant_isset native]} {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set base_march [bazel::get_base_arch]
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_build_env "CC_OPT_FLAGS=${base_march} ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- } else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_build_env "CC_OPT_FLAGS=-march=native ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- set bazel_build_env "BAZEL_SH=/bin/bash ${bazel_build_env}"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ set bazel_build_env [bazel::get_build_env]
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> build.cmd "${bazel_build_env} [option bazel.build_cmd] [option bazel.build_cmd_opts]"
build.args "[option bazel.build_opts]"
build.post_args "[option bazel.build_target]"
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ ui_debug "Bazel build environ : ${bazel_build_env}"
</span> ui_debug "Bazel build command : [option bazel.build_cmd]"
ui_debug "Bazel build options : [option bazel.build_cmd_opts] [option bazel.build_opts]"
ui_debug "Bazel build target : [option bazel.build_target]"
</pre><pre style='margin:0'>
</pre>