1af6a4c17SAlex Richardson#!/usr/bin/env python3 2af6a4c17SAlex Richardson# PYTHON_ARGCOMPLETE_OKAY 3af6a4c17SAlex Richardson# - 4af6a4c17SAlex Richardson# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5af6a4c17SAlex Richardson# 6af6a4c17SAlex Richardson# Copyright (c) 2018 Alex Richardson <arichardson@FreeBSD.org> 7af6a4c17SAlex Richardson# 8af6a4c17SAlex Richardson# Redistribution and use in source and binary forms, with or without 9af6a4c17SAlex Richardson# modification, are permitted provided that the following conditions 10af6a4c17SAlex Richardson# are met: 11af6a4c17SAlex Richardson# 1. Redistributions of source code must retain the above copyright 12af6a4c17SAlex Richardson# notice, this list of conditions and the following disclaimer. 13af6a4c17SAlex Richardson# 2. Redistributions in binary form must reproduce the above copyright 14af6a4c17SAlex Richardson# notice, this list of conditions and the following disclaimer in the 15af6a4c17SAlex Richardson# documentation and/or other materials provided with the distribution. 16af6a4c17SAlex Richardson# 17af6a4c17SAlex Richardson# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18af6a4c17SAlex Richardson# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19af6a4c17SAlex Richardson# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20af6a4c17SAlex Richardson# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21af6a4c17SAlex Richardson# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22af6a4c17SAlex Richardson# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23af6a4c17SAlex Richardson# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24af6a4c17SAlex Richardson# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25af6a4c17SAlex Richardson# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26af6a4c17SAlex Richardson# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27af6a4c17SAlex Richardson# SUCH DAMAGE. 28af6a4c17SAlex Richardson# 29af6a4c17SAlex Richardson# $FreeBSD$ 30af6a4c17SAlex Richardson# 31af6a4c17SAlex Richardson 32af6a4c17SAlex Richardson# This script makes it easier to build on non-FreeBSD systems by bootstrapping 33af6a4c17SAlex Richardson# bmake and inferring required compiler variables. 34af6a4c17SAlex Richardson# 35af6a4c17SAlex Richardson# On FreeBSD you can use it the same way as just calling make: 36af6a4c17SAlex Richardson# `MAKEOBJDIRPREFIX=~/obj ./tools/build/make.py buildworld -DWITH_FOO` 37af6a4c17SAlex Richardson# 38af6a4c17SAlex Richardson# On Linux and MacOS you will either need to set XCC/XCXX/XLD/XCPP or pass 39af6a4c17SAlex Richardson# --cross-bindir to specify the path to the cross-compiler bindir: 40af6a4c17SAlex Richardson# `MAKEOBJDIRPREFIX=~/obj ./tools/build/make.py 41af6a4c17SAlex Richardson# --cross-bindir=/path/to/cross/compiler buildworld -DWITH_FOO TARGET=foo 42af6a4c17SAlex Richardson# TARGET_ARCH=bar` 43af6a4c17SAlex Richardsonimport argparse 44af6a4c17SAlex Richardsonimport os 45af6a4c17SAlex Richardsonimport shlex 46af6a4c17SAlex Richardsonimport shutil 47af6a4c17SAlex Richardsonimport subprocess 48af6a4c17SAlex Richardsonimport sys 49af6a4c17SAlex Richardsonfrom pathlib import Path 50af6a4c17SAlex Richardson 51af6a4c17SAlex Richardson 52af6a4c17SAlex Richardsondef run(cmd, **kwargs): 53af6a4c17SAlex Richardson cmd = list(map(str, cmd)) # convert all Path objects to str 54af6a4c17SAlex Richardson debug("Running", cmd) 55af6a4c17SAlex Richardson subprocess.check_call(cmd, **kwargs) 56af6a4c17SAlex Richardson 57af6a4c17SAlex Richardson 58af6a4c17SAlex Richardsondef bootstrap_bmake(source_root, objdir_prefix): 59af6a4c17SAlex Richardson bmake_source_dir = source_root / "contrib/bmake" 60af6a4c17SAlex Richardson bmake_build_dir = objdir_prefix / "bmake-build" 61af6a4c17SAlex Richardson bmake_install_dir = objdir_prefix / "bmake-install" 62af6a4c17SAlex Richardson bmake_binary = bmake_install_dir / "bin/bmake" 63af6a4c17SAlex Richardson 64af6a4c17SAlex Richardson if (bmake_install_dir / "bin/bmake").exists(): 65af6a4c17SAlex Richardson return bmake_binary 66af6a4c17SAlex Richardson print("Bootstrapping bmake...") 67af6a4c17SAlex Richardson # TODO: check if the host system bmake is new enough and use that instead 68af6a4c17SAlex Richardson if not bmake_build_dir.exists(): 69af6a4c17SAlex Richardson os.makedirs(str(bmake_build_dir)) 70af6a4c17SAlex Richardson env = os.environ.copy() 71af6a4c17SAlex Richardson global new_env_vars 72af6a4c17SAlex Richardson env.update(new_env_vars) 73af6a4c17SAlex Richardson 74af6a4c17SAlex Richardson if sys.platform.startswith("linux"): 75af6a4c17SAlex Richardson # Work around the deleted file bmake/missing/sys/cdefs.h 76af6a4c17SAlex Richardson # TODO: bmake should keep the compat sys/cdefs.h 77af6a4c17SAlex Richardson env["CFLAGS"] = "-I{src}/tools/build/cross-build/include/common " \ 78af6a4c17SAlex Richardson "-I{src}/tools/build/cross-build/include/linux " \ 79af6a4c17SAlex Richardson "-D_GNU_SOURCE=1".format(src=source_root) 80af6a4c17SAlex Richardson configure_args = [ 81af6a4c17SAlex Richardson "--with-default-sys-path=" + str(bmake_install_dir / "share/mk"), 82af6a4c17SAlex Richardson "--with-machine=amd64", # TODO? "--with-machine-arch=amd64", 83af6a4c17SAlex Richardson "--without-filemon", "--prefix=" + str(bmake_install_dir)] 84af6a4c17SAlex Richardson run(["sh", bmake_source_dir / "boot-strap"] + configure_args, 85af6a4c17SAlex Richardson cwd=str(bmake_build_dir), env=env) 86af6a4c17SAlex Richardson 87af6a4c17SAlex Richardson run(["sh", bmake_source_dir / "boot-strap", "op=install"] + configure_args, 88af6a4c17SAlex Richardson cwd=str(bmake_build_dir)) 89af6a4c17SAlex Richardson print("Finished bootstrapping bmake...") 90af6a4c17SAlex Richardson return bmake_binary 91af6a4c17SAlex Richardson 92af6a4c17SAlex Richardson 93af6a4c17SAlex Richardsondef debug(*args, **kwargs): 94af6a4c17SAlex Richardson global parsed_args 95af6a4c17SAlex Richardson if parsed_args.debug: 96af6a4c17SAlex Richardson print(*args, **kwargs) 97af6a4c17SAlex Richardson 98af6a4c17SAlex Richardson 99af6a4c17SAlex Richardsondef is_make_var_set(var): 100af6a4c17SAlex Richardson return any( 101af6a4c17SAlex Richardson x.startswith(var + "=") or x == ("-D" + var) for x in sys.argv[1:]) 102af6a4c17SAlex Richardson 103af6a4c17SAlex Richardson 104af6a4c17SAlex Richardsondef check_required_make_env_var(varname, binary_name, bindir): 105af6a4c17SAlex Richardson global new_env_vars 106af6a4c17SAlex Richardson if os.getenv(varname): 107af6a4c17SAlex Richardson return 108af6a4c17SAlex Richardson if not bindir: 109af6a4c17SAlex Richardson sys.exit("Could not infer value for $" + varname + ". Either set $" + 110af6a4c17SAlex Richardson varname + " or pass --cross-bindir=/cross/compiler/dir/bin") 111af6a4c17SAlex Richardson # try to infer the path to the tool 112af6a4c17SAlex Richardson guess = os.path.join(bindir, binary_name) 113af6a4c17SAlex Richardson if not os.path.isfile(guess): 114af6a4c17SAlex Richardson sys.exit("Could not infer value for $" + varname + ": " + guess + 115af6a4c17SAlex Richardson " does not exist") 116af6a4c17SAlex Richardson new_env_vars[varname] = guess 117af6a4c17SAlex Richardson debug("Inferred", varname, "as", guess) 118accf9611SUlrich Spörlein global parsed_args 119accf9611SUlrich Spörlein if parsed_args.debug: 120accf9611SUlrich Spörlein run([guess, "--version"]) 121af6a4c17SAlex Richardson 1222b181156SAlex Richardsondef check_xtool_make_env_var(varname, binary_name): 1232b181156SAlex Richardson # Avoid calling brew --prefix on macOS if all variables are already set: 1242b181156SAlex Richardson if os.getenv(varname): 1252b181156SAlex Richardson return 1262b181156SAlex Richardson global parsed_args 1272b181156SAlex Richardson if parsed_args.cross_bindir is None: 1282b181156SAlex Richardson parsed_args.cross_bindir = default_cross_toolchain() 1292b181156SAlex Richardson return check_required_make_env_var(varname, binary_name, 1302b181156SAlex Richardson parsed_args.cross_bindir) 131af6a4c17SAlex Richardson 132af6a4c17SAlex Richardsondef default_cross_toolchain(): 133af6a4c17SAlex Richardson # default to homebrew-installed clang on MacOS if available 134af6a4c17SAlex Richardson if sys.platform.startswith("darwin"): 135af6a4c17SAlex Richardson if shutil.which("brew"): 136a26ace4dSAlex Richardson llvm_dir = subprocess.run(["brew", "--prefix", "llvm"], 137a26ace4dSAlex Richardson capture_output=True).stdout.strip() 138a26ace4dSAlex Richardson debug("Inferred LLVM dir as", llvm_dir) 139a26ace4dSAlex Richardson try: 140a26ace4dSAlex Richardson if llvm_dir and Path(llvm_dir.decode("utf-8"), "bin").exists(): 141a26ace4dSAlex Richardson return str(Path(llvm_dir.decode("utf-8"), "bin")) 142a26ace4dSAlex Richardson except OSError: 143a26ace4dSAlex Richardson return None 144af6a4c17SAlex Richardson return None 145af6a4c17SAlex Richardson 146af6a4c17SAlex Richardson 147af6a4c17SAlex Richardsonif __name__ == "__main__": 148af6a4c17SAlex Richardson parser = argparse.ArgumentParser( 149af6a4c17SAlex Richardson formatter_class=argparse.ArgumentDefaultsHelpFormatter) 150af6a4c17SAlex Richardson parser.add_argument("--host-bindir", 151af6a4c17SAlex Richardson help="Directory to look for cc/c++/cpp/ld to build " 152af6a4c17SAlex Richardson "host (" + sys.platform + ") binaries", 153af6a4c17SAlex Richardson default="/usr/bin") 154a26ace4dSAlex Richardson parser.add_argument("--cross-bindir", default=None, 155af6a4c17SAlex Richardson help="Directory to look for cc/c++/cpp/ld to build " 156af6a4c17SAlex Richardson "target binaries (only needed if XCC/XCPP/XLD " 157af6a4c17SAlex Richardson "are not set)") 158af6a4c17SAlex Richardson parser.add_argument("--cross-compiler-type", choices=("clang", "gcc"), 159af6a4c17SAlex Richardson default="clang", 160af6a4c17SAlex Richardson help="Compiler type to find in --cross-bindir (only " 161af6a4c17SAlex Richardson "needed if XCC/XCPP/XLD are not set)" 162af6a4c17SAlex Richardson "Note: using CC is currently highly experimental") 163af6a4c17SAlex Richardson parser.add_argument("--host-compiler-type", choices=("cc", "clang", "gcc"), 164af6a4c17SAlex Richardson default="cc", 165af6a4c17SAlex Richardson help="Compiler type to find in --host-bindir (only " 166af6a4c17SAlex Richardson "needed if CC/CPP/CXX are not set). ") 167af6a4c17SAlex Richardson parser.add_argument("--debug", action="store_true", 168af6a4c17SAlex Richardson help="Print information on inferred env vars") 16931ba4ce8SAlex Richardson parser.add_argument("--bootstrap-toolchain", action="store_true", 17031ba4ce8SAlex Richardson help="Bootstrap the toolchain instead of using an " 17131ba4ce8SAlex Richardson "external one (experimental and not recommended)") 172af6a4c17SAlex Richardson parser.add_argument("--clean", action="store_true", 173af6a4c17SAlex Richardson help="Do a clean rebuild instead of building with " 17443e083beSAlex Richardson "-DWITHOUT_CLEAN") 175af6a4c17SAlex Richardson parser.add_argument("--no-clean", action="store_false", dest="clean", 176af6a4c17SAlex Richardson help="Do a clean rebuild instead of building with " 17743e083beSAlex Richardson "-DWITHOUT_CLEAN") 178af6a4c17SAlex Richardson try: 179af6a4c17SAlex Richardson import argcomplete # bash completion: 180af6a4c17SAlex Richardson 181af6a4c17SAlex Richardson argcomplete.autocomplete(parser) 182af6a4c17SAlex Richardson except ImportError: 183af6a4c17SAlex Richardson pass 184af6a4c17SAlex Richardson parsed_args, bmake_args = parser.parse_known_args() 185af6a4c17SAlex Richardson 186af6a4c17SAlex Richardson MAKEOBJDIRPREFIX = os.getenv("MAKEOBJDIRPREFIX") 187af6a4c17SAlex Richardson if not MAKEOBJDIRPREFIX: 188af6a4c17SAlex Richardson sys.exit("MAKEOBJDIRPREFIX is not set, cannot continue!") 189af6a4c17SAlex Richardson if not Path(MAKEOBJDIRPREFIX).is_dir(): 190af6a4c17SAlex Richardson sys.exit( 191af6a4c17SAlex Richardson "Chosen MAKEOBJDIRPREFIX=" + MAKEOBJDIRPREFIX + " doesn't exit!") 192af6a4c17SAlex Richardson objdir_prefix = Path(MAKEOBJDIRPREFIX).absolute() 193af6a4c17SAlex Richardson source_root = Path(__file__).absolute().parent.parent.parent 194af6a4c17SAlex Richardson 195af6a4c17SAlex Richardson new_env_vars = {} 196af6a4c17SAlex Richardson if not sys.platform.startswith("freebsd"): 197af6a4c17SAlex Richardson if not is_make_var_set("TARGET") or not is_make_var_set("TARGET_ARCH"): 198af6a4c17SAlex Richardson if "universe" not in sys.argv and "tinderbox" not in sys.argv: 199af6a4c17SAlex Richardson sys.exit("TARGET= and TARGET_ARCH= must be set explicitly " 200af6a4c17SAlex Richardson "when building on non-FreeBSD") 20131ba4ce8SAlex Richardson if not parsed_args.bootstrap_toolchain: 202af6a4c17SAlex Richardson # infer values for CC/CXX/CPP 203af6a4c17SAlex Richardson if parsed_args.host_compiler_type == "gcc": 204af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ("gcc", "g++", "cpp") 205accf9611SUlrich Spörlein # FIXME: this should take values like `clang-9` and then look for 206accf9611SUlrich Spörlein # clang-cpp-9, etc. Would alleviate the need to set the bindir on 207accf9611SUlrich Spörlein # ubuntu/debian at least. 208af6a4c17SAlex Richardson elif parsed_args.host_compiler_type == "clang": 209af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ( 210af6a4c17SAlex Richardson "clang", "clang++", "clang-cpp") 211af6a4c17SAlex Richardson else: 212af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ("cc", "c++", "cpp") 213af6a4c17SAlex Richardson 214af6a4c17SAlex Richardson check_required_make_env_var("CC", default_cc, parsed_args.host_bindir) 215af6a4c17SAlex Richardson check_required_make_env_var("CXX", default_cxx, 216af6a4c17SAlex Richardson parsed_args.host_bindir) 217af6a4c17SAlex Richardson check_required_make_env_var("CPP", default_cpp, 218af6a4c17SAlex Richardson parsed_args.host_bindir) 219af6a4c17SAlex Richardson # Using the default value for LD is fine (but not for XLD!) 220af6a4c17SAlex Richardson 221af6a4c17SAlex Richardson # On non-FreeBSD we need to explicitly pass XCC/XLD/X_COMPILER_TYPE 222d037edf8SAlex Richardson use_cross_gcc = parsed_args.cross_compiler_type == "gcc" 2232b181156SAlex Richardson check_xtool_make_env_var("XCC", "gcc" if use_cross_gcc else "clang") 2242b181156SAlex Richardson check_xtool_make_env_var("XCXX", "g++" if use_cross_gcc else "clang++") 2252b181156SAlex Richardson check_xtool_make_env_var("XCPP", 2262b181156SAlex Richardson "cpp" if use_cross_gcc else "clang-cpp") 2272b181156SAlex Richardson check_xtool_make_env_var("XLD", "ld" if use_cross_gcc else "ld.lld") 228d037edf8SAlex Richardson 229d037edf8SAlex Richardson # We also need to set STRIPBIN if there is no working strip binary 230d037edf8SAlex Richardson # in $PATH. 231d037edf8SAlex Richardson if not shutil.which("strip"): 232d037edf8SAlex Richardson if sys.platform.startswith("darwin"): 233d037edf8SAlex Richardson # On macOS systems we have to use /usr/bin/strip. 23488db1cc9SAlex Richardson sys.exit("Cannot find required tool 'strip'. Please install " 23588db1cc9SAlex Richardson "the host compiler and command line tools.") 236d037edf8SAlex Richardson if parsed_args.host_compiler_type == "clang": 237d037edf8SAlex Richardson strip_binary = "llvm-strip" 238d037edf8SAlex Richardson else: 239d037edf8SAlex Richardson strip_binary = "strip" 240d037edf8SAlex Richardson check_required_make_env_var("STRIPBIN", strip_binary, 2412b181156SAlex Richardson parsed_args.host_bindir) 242d037edf8SAlex Richardson if os.getenv("STRIPBIN") or "STRIPBIN" in new_env_vars: 243d037edf8SAlex Richardson # If we are setting STRIPBIN, we have to set XSTRIPBIN to the 244d037edf8SAlex Richardson # default if it is not set otherwise already. 245d037edf8SAlex Richardson if not os.getenv("XSTRIPBIN") and not is_make_var_set("XSTRIPBIN"): 246d037edf8SAlex Richardson # Use the bootstrapped elftoolchain strip: 247d037edf8SAlex Richardson new_env_vars["XSTRIPBIN"] = "strip" 248af6a4c17SAlex Richardson 249af6a4c17SAlex Richardson bmake_binary = bootstrap_bmake(source_root, objdir_prefix) 250af6a4c17SAlex Richardson # at -j1 cleandir+obj is unbearably slow. AUTO_OBJ helps a lot 251af6a4c17SAlex Richardson debug("Adding -DWITH_AUTO_OBJ") 252af6a4c17SAlex Richardson bmake_args.append("-DWITH_AUTO_OBJ") 253af6a4c17SAlex Richardson if parsed_args.clean is False: 254af6a4c17SAlex Richardson bmake_args.append("-DWITHOUT_CLEAN") 255af6a4c17SAlex Richardson if (parsed_args.clean is None and not is_make_var_set("NO_CLEAN") 256af6a4c17SAlex Richardson and not is_make_var_set("WITHOUT_CLEAN")): 257af6a4c17SAlex Richardson # Avoid accidentally deleting all of the build tree and wasting lots of 258af6a4c17SAlex Richardson # time cleaning directories instead of just doing a rm -rf ${.OBJDIR} 25943e083beSAlex Richardson want_clean = input("You did not set -DWITHOUT_CLEAN/--clean/--no-clean." 260af6a4c17SAlex Richardson " Did you really mean to do a clean build? y/[N] ") 261af6a4c17SAlex Richardson if not want_clean.lower().startswith("y"): 26243e083beSAlex Richardson bmake_args.append("-DWITHOUT_CLEAN") 263af6a4c17SAlex Richardson 264af6a4c17SAlex Richardson env_cmd_str = " ".join( 265af6a4c17SAlex Richardson shlex.quote(k + "=" + v) for k, v in new_env_vars.items()) 266af6a4c17SAlex Richardson make_cmd_str = " ".join( 267af6a4c17SAlex Richardson shlex.quote(s) for s in [str(bmake_binary)] + bmake_args) 268af6a4c17SAlex Richardson debug("Running `env ", env_cmd_str, " ", make_cmd_str, "`", sep="") 269af6a4c17SAlex Richardson os.environ.update(new_env_vars) 270*b7ac17b4SAlfredo Dal'Ava Junior 271*b7ac17b4SAlfredo Dal'Ava Junior # Fedora defines bash function wrapper for some shell commands and this 272*b7ac17b4SAlfredo Dal'Ava Junior # makes 'which <command>' return the function's source code instead of 273*b7ac17b4SAlfredo Dal'Ava Junior # the binary path. Undefine it to restore the original behavior. 274*b7ac17b4SAlfredo Dal'Ava Junior os.unsetenv("BASH_FUNC_which%%") 275*b7ac17b4SAlfredo Dal'Ava Junior os.unsetenv("BASH_FUNC_ml%%") 276*b7ac17b4SAlfredo Dal'Ava Junior os.unsetenv("BASH_FUNC_module%%") 277*b7ac17b4SAlfredo Dal'Ava Junior 278af6a4c17SAlex Richardson os.chdir(str(source_root)) 279af6a4c17SAlex Richardson os.execv(str(bmake_binary), [str(bmake_binary)] + bmake_args) 280