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 122af6a4c17SAlex Richardson 123af6a4c17SAlex Richardsondef default_cross_toolchain(): 124af6a4c17SAlex Richardson # default to homebrew-installed clang on MacOS if available 125af6a4c17SAlex Richardson if sys.platform.startswith("darwin"): 126af6a4c17SAlex Richardson if shutil.which("brew"): 127af6a4c17SAlex Richardson llvm_dir = subprocess.getoutput("brew --prefix llvm") 128af6a4c17SAlex Richardson if llvm_dir and Path(llvm_dir, "bin").exists(): 129af6a4c17SAlex Richardson return str(Path(llvm_dir, "bin")) 130af6a4c17SAlex Richardson return None 131af6a4c17SAlex Richardson 132af6a4c17SAlex Richardson 133af6a4c17SAlex Richardsonif __name__ == "__main__": 134af6a4c17SAlex Richardson parser = argparse.ArgumentParser( 135af6a4c17SAlex Richardson formatter_class=argparse.ArgumentDefaultsHelpFormatter) 136af6a4c17SAlex Richardson parser.add_argument("--host-bindir", 137af6a4c17SAlex Richardson help="Directory to look for cc/c++/cpp/ld to build " 138af6a4c17SAlex Richardson "host (" + sys.platform + ") binaries", 139af6a4c17SAlex Richardson default="/usr/bin") 140af6a4c17SAlex Richardson parser.add_argument("--cross-bindir", default=default_cross_toolchain(), 141af6a4c17SAlex Richardson help="Directory to look for cc/c++/cpp/ld to build " 142af6a4c17SAlex Richardson "target binaries (only needed if XCC/XCPP/XLD " 143af6a4c17SAlex Richardson "are not set)") 144af6a4c17SAlex Richardson parser.add_argument("--cross-compiler-type", choices=("clang", "gcc"), 145af6a4c17SAlex Richardson default="clang", 146af6a4c17SAlex Richardson help="Compiler type to find in --cross-bindir (only " 147af6a4c17SAlex Richardson "needed if XCC/XCPP/XLD are not set)" 148af6a4c17SAlex Richardson "Note: using CC is currently highly experimental") 149af6a4c17SAlex Richardson parser.add_argument("--host-compiler-type", choices=("cc", "clang", "gcc"), 150af6a4c17SAlex Richardson default="cc", 151af6a4c17SAlex Richardson help="Compiler type to find in --host-bindir (only " 152af6a4c17SAlex Richardson "needed if CC/CPP/CXX are not set). ") 153af6a4c17SAlex Richardson parser.add_argument("--debug", action="store_true", 154af6a4c17SAlex Richardson help="Print information on inferred env vars") 155af6a4c17SAlex Richardson parser.add_argument("--clean", action="store_true", 156af6a4c17SAlex Richardson help="Do a clean rebuild instead of building with " 157af6a4c17SAlex Richardson "-DNO_CLEAN") 158af6a4c17SAlex Richardson parser.add_argument("--no-clean", action="store_false", dest="clean", 159af6a4c17SAlex Richardson help="Do a clean rebuild instead of building with " 160af6a4c17SAlex Richardson "-DNO_CLEAN") 161af6a4c17SAlex Richardson try: 162af6a4c17SAlex Richardson import argcomplete # bash completion: 163af6a4c17SAlex Richardson 164af6a4c17SAlex Richardson argcomplete.autocomplete(parser) 165af6a4c17SAlex Richardson except ImportError: 166af6a4c17SAlex Richardson pass 167af6a4c17SAlex Richardson parsed_args, bmake_args = parser.parse_known_args() 168af6a4c17SAlex Richardson 169af6a4c17SAlex Richardson MAKEOBJDIRPREFIX = os.getenv("MAKEOBJDIRPREFIX") 170af6a4c17SAlex Richardson if not MAKEOBJDIRPREFIX: 171af6a4c17SAlex Richardson sys.exit("MAKEOBJDIRPREFIX is not set, cannot continue!") 172af6a4c17SAlex Richardson if not Path(MAKEOBJDIRPREFIX).is_dir(): 173af6a4c17SAlex Richardson sys.exit( 174af6a4c17SAlex Richardson "Chosen MAKEOBJDIRPREFIX=" + MAKEOBJDIRPREFIX + " doesn't exit!") 175af6a4c17SAlex Richardson objdir_prefix = Path(MAKEOBJDIRPREFIX).absolute() 176af6a4c17SAlex Richardson source_root = Path(__file__).absolute().parent.parent.parent 177af6a4c17SAlex Richardson 178af6a4c17SAlex Richardson new_env_vars = {} 179af6a4c17SAlex Richardson if not sys.platform.startswith("freebsd"): 180af6a4c17SAlex Richardson if not is_make_var_set("TARGET") or not is_make_var_set("TARGET_ARCH"): 181af6a4c17SAlex Richardson if "universe" not in sys.argv and "tinderbox" not in sys.argv: 182af6a4c17SAlex Richardson sys.exit("TARGET= and TARGET_ARCH= must be set explicitly " 183af6a4c17SAlex Richardson "when building on non-FreeBSD") 184af6a4c17SAlex Richardson # infer values for CC/CXX/CPP 185af6a4c17SAlex Richardson 186af6a4c17SAlex Richardson if sys.platform.startswith( 187af6a4c17SAlex Richardson "linux") and parsed_args.host_compiler_type == "cc": 188af6a4c17SAlex Richardson # FIXME: bsd.compiler.mk doesn't handle the output of GCC if it 189af6a4c17SAlex Richardson # is /usr/bin/cc on Ubuntu since it doesn't contain the GCC string. 190af6a4c17SAlex Richardson parsed_args.host_compiler_type = "gcc" 191af6a4c17SAlex Richardson 192af6a4c17SAlex Richardson if parsed_args.host_compiler_type == "gcc": 193af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ("gcc", "g++", "cpp") 194accf9611SUlrich Spörlein # FIXME: this should take values like `clang-9` and then look for 195accf9611SUlrich Spörlein # clang-cpp-9, etc. Would alleviate the need to set the bindir on 196accf9611SUlrich Spörlein # ubuntu/debian at least. 197af6a4c17SAlex Richardson elif parsed_args.host_compiler_type == "clang": 198af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ( 199af6a4c17SAlex Richardson "clang", "clang++", "clang-cpp") 200af6a4c17SAlex Richardson else: 201af6a4c17SAlex Richardson default_cc, default_cxx, default_cpp = ("cc", "c++", "cpp") 202af6a4c17SAlex Richardson 203af6a4c17SAlex Richardson check_required_make_env_var("CC", default_cc, parsed_args.host_bindir) 204af6a4c17SAlex Richardson check_required_make_env_var("CXX", default_cxx, 205af6a4c17SAlex Richardson parsed_args.host_bindir) 206af6a4c17SAlex Richardson check_required_make_env_var("CPP", default_cpp, 207af6a4c17SAlex Richardson parsed_args.host_bindir) 208af6a4c17SAlex Richardson # Using the default value for LD is fine (but not for XLD!) 209af6a4c17SAlex Richardson 210af6a4c17SAlex Richardson # On non-FreeBSD we need to explicitly pass XCC/XLD/X_COMPILER_TYPE 211*d037edf8SAlex Richardson use_cross_gcc = parsed_args.cross_compiler_type == "gcc" 212af6a4c17SAlex Richardson check_required_make_env_var("XCC", "gcc" if use_cross_gcc else "clang", 213af6a4c17SAlex Richardson parsed_args.cross_bindir) 214af6a4c17SAlex Richardson check_required_make_env_var("XCXX", 215af6a4c17SAlex Richardson "g++" if use_cross_gcc else "clang++", 216af6a4c17SAlex Richardson parsed_args.cross_bindir) 217af6a4c17SAlex Richardson check_required_make_env_var("XCPP", 218af6a4c17SAlex Richardson "cpp" if use_cross_gcc else "clang-cpp", 219af6a4c17SAlex Richardson parsed_args.cross_bindir) 220af6a4c17SAlex Richardson check_required_make_env_var("XLD", "ld" if use_cross_gcc else "ld.lld", 221af6a4c17SAlex Richardson parsed_args.cross_bindir) 222*d037edf8SAlex Richardson 223*d037edf8SAlex Richardson # We also need to set STRIPBIN if there is no working strip binary 224*d037edf8SAlex Richardson # in $PATH. 225*d037edf8SAlex Richardson if not shutil.which("strip"): 226*d037edf8SAlex Richardson if sys.platform.startswith("darwin"): 227*d037edf8SAlex Richardson # On macOS systems we have to use /usr/bin/strip. 228*d037edf8SAlex Richardson sys.exit("Cannot find required tool 'strip'. Please install the" 229*d037edf8SAlex Richardson " host compiler and command line tools.") 230*d037edf8SAlex Richardson if parsed_args.host_compiler_type == "clang": 231*d037edf8SAlex Richardson strip_binary = "llvm-strip" 232*d037edf8SAlex Richardson else: 233*d037edf8SAlex Richardson strip_binary = "strip" 234*d037edf8SAlex Richardson check_required_make_env_var("STRIPBIN", strip_binary, 235a920b981SAlex Richardson parsed_args.cross_bindir) 236*d037edf8SAlex Richardson if os.getenv("STRIPBIN") or "STRIPBIN" in new_env_vars: 237*d037edf8SAlex Richardson # If we are setting STRIPBIN, we have to set XSTRIPBIN to the 238*d037edf8SAlex Richardson # default if it is not set otherwise already. 239*d037edf8SAlex Richardson if not os.getenv("XSTRIPBIN") and not is_make_var_set("XSTRIPBIN"): 240*d037edf8SAlex Richardson # Use the bootstrapped elftoolchain strip: 241*d037edf8SAlex Richardson new_env_vars["XSTRIPBIN"] = "strip" 242af6a4c17SAlex Richardson 243af6a4c17SAlex Richardson bmake_binary = bootstrap_bmake(source_root, objdir_prefix) 244af6a4c17SAlex Richardson # at -j1 cleandir+obj is unbearably slow. AUTO_OBJ helps a lot 245af6a4c17SAlex Richardson debug("Adding -DWITH_AUTO_OBJ") 246af6a4c17SAlex Richardson bmake_args.append("-DWITH_AUTO_OBJ") 247af6a4c17SAlex Richardson if parsed_args.clean is False: 248af6a4c17SAlex Richardson bmake_args.append("-DWITHOUT_CLEAN") 249af6a4c17SAlex Richardson if (parsed_args.clean is None and not is_make_var_set("NO_CLEAN") 250af6a4c17SAlex Richardson and not is_make_var_set("WITHOUT_CLEAN")): 251af6a4c17SAlex Richardson # Avoid accidentally deleting all of the build tree and wasting lots of 252af6a4c17SAlex Richardson # time cleaning directories instead of just doing a rm -rf ${.OBJDIR} 253af6a4c17SAlex Richardson want_clean = input("You did not set -DNO_CLEAN/--clean/--no-clean." 254af6a4c17SAlex Richardson " Did you really mean to do a clean build? y/[N] ") 255af6a4c17SAlex Richardson if not want_clean.lower().startswith("y"): 256af6a4c17SAlex Richardson bmake_args.append("-DNO_CLEAN") 257af6a4c17SAlex Richardson 258af6a4c17SAlex Richardson env_cmd_str = " ".join( 259af6a4c17SAlex Richardson shlex.quote(k + "=" + v) for k, v in new_env_vars.items()) 260af6a4c17SAlex Richardson make_cmd_str = " ".join( 261af6a4c17SAlex Richardson shlex.quote(s) for s in [str(bmake_binary)] + bmake_args) 262af6a4c17SAlex Richardson debug("Running `env ", env_cmd_str, " ", make_cmd_str, "`", sep="") 263af6a4c17SAlex Richardson os.environ.update(new_env_vars) 264af6a4c17SAlex Richardson os.chdir(str(source_root)) 265af6a4c17SAlex Richardson os.execv(str(bmake_binary), [str(bmake_binary)] + bmake_args) 266