2018-07-26 20:15:55 -04:00
|
|
|
#!/usr/bin/env python
|
2019-01-21 14:03:30 -05:00
|
|
|
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-07-26 20:15:55 -04:00
|
|
|
import third_party
|
2018-09-02 17:37:14 -04:00
|
|
|
from util import build_mode, build_path, enable_ansi_colors, root_path, run
|
2019-03-01 19:33:28 -05:00
|
|
|
from util import shell_quote, run_output
|
2018-07-26 20:15:55 -04:00
|
|
|
import os
|
2018-09-08 11:56:19 -04:00
|
|
|
import re
|
2018-08-17 01:05:50 -04:00
|
|
|
import sys
|
2018-08-25 07:30:58 -04:00
|
|
|
from distutils.spawn import find_executable
|
2018-12-18 21:09:30 -05:00
|
|
|
import prebuilt
|
2018-07-26 20:15:55 -04:00
|
|
|
|
2018-08-27 17:40:29 -04:00
|
|
|
|
|
|
|
def main():
|
2018-09-02 17:37:14 -04:00
|
|
|
enable_ansi_colors()
|
|
|
|
|
2018-08-27 17:40:29 -04:00
|
|
|
os.chdir(root_path)
|
|
|
|
|
|
|
|
third_party.download_gn()
|
|
|
|
third_party.download_clang_format()
|
|
|
|
third_party.download_clang()
|
|
|
|
third_party.maybe_download_sysroot()
|
|
|
|
write_lastchange()
|
|
|
|
|
|
|
|
mode = build_mode(default=None)
|
|
|
|
if mode is not None:
|
|
|
|
gn_gen(mode)
|
|
|
|
else:
|
|
|
|
gn_gen("release")
|
|
|
|
gn_gen("debug")
|
|
|
|
|
|
|
|
|
2018-08-29 00:45:10 -04:00
|
|
|
def write_if_not_exists(filename, contents):
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
with open(filename, "w+") as f:
|
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
|
2018-08-27 17:40:29 -04:00
|
|
|
def write_lastchange():
|
2018-08-29 00:45:10 -04:00
|
|
|
write_if_not_exists(
|
|
|
|
"build/util/LASTCHANGE",
|
|
|
|
"LASTCHANGE=c42e4ddbb7973bfb0c57a49ab6bf6dc432baad7e-\n")
|
|
|
|
write_if_not_exists("build/util/LASTCHANGE.committime", "1535518087")
|
|
|
|
# TODO Properly we should call the following script, but it seems to cause
|
|
|
|
# a rebuild on every commit.
|
|
|
|
# run([
|
|
|
|
# sys.executable, "build/util/lastchange.py", "-o",
|
|
|
|
# "build/util/LASTCHANGE", "--source-dir", root_path, "--filter="
|
|
|
|
# ])
|
2018-07-26 20:15:55 -04:00
|
|
|
|
|
|
|
|
2018-09-08 11:56:19 -04:00
|
|
|
# If this text is found in args.gn, we assume it hasn't been hand edited.
|
|
|
|
gn_args_header = [
|
|
|
|
"# This file is automatically generated by tools/setup.py.",
|
|
|
|
"# REMOVE THIS LINE to preserve any changes you make.", ""
|
|
|
|
]
|
|
|
|
|
2018-09-25 17:45:27 -04:00
|
|
|
|
|
|
|
def gn_string(s):
|
|
|
|
# In gn, strings are enclosed in double-quotes and use backslash as the
|
|
|
|
# escape character. The only escape sequences supported are:
|
|
|
|
# \" (for literal quote)
|
|
|
|
# \$ (for literal dollars sign)
|
|
|
|
# \\ (for literal backslash)
|
|
|
|
# Any other use of a backslash is treated as a literal backslash.
|
|
|
|
s = re.sub(r'("|\$|\\(?=["$\\]))', r'\\\1', s)
|
|
|
|
s = '"' + s + '"'
|
|
|
|
return s
|
|
|
|
|
2018-09-08 11:56:19 -04:00
|
|
|
|
|
|
|
def gn_args_are_generated(lines):
|
|
|
|
for line in lines:
|
|
|
|
if re.match("^\s*#.*REMOVE THIS LINE", line):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def read_gn_args(args_filename):
|
|
|
|
if not os.path.exists(args_filename):
|
|
|
|
return (None, False) # No content, not hand edited.
|
|
|
|
|
|
|
|
with open(args_filename) as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
args = [l.strip() for l in lines if not re.match("^\s*(#|$)", l)]
|
|
|
|
hand_edited = not gn_args_are_generated(lines)
|
|
|
|
return (args, hand_edited)
|
|
|
|
|
|
|
|
|
|
|
|
def write_gn_args(args_filename, args):
|
|
|
|
assert not gn_args_are_generated(args) # No header -> hand crafted.
|
|
|
|
lines = gn_args_header + args
|
|
|
|
assert gn_args_are_generated(lines) # With header -> generated.
|
|
|
|
|
|
|
|
# Ensure the directory where args.gn goes exists.
|
2018-11-30 03:27:41 -05:00
|
|
|
d = os.path.dirname(args_filename)
|
|
|
|
if not os.path.isdir(d):
|
|
|
|
os.makedirs(d)
|
2018-09-08 11:56:19 -04:00
|
|
|
|
|
|
|
with open(args_filename, "w") as f:
|
|
|
|
f.write("\n".join(lines) + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
def generate_gn_args(mode):
|
2018-07-26 20:15:55 -04:00
|
|
|
out = []
|
2018-09-08 11:56:19 -04:00
|
|
|
if mode == "release":
|
2018-07-26 20:15:55 -04:00
|
|
|
out += ["is_official_build=true"]
|
2018-09-08 11:56:19 -04:00
|
|
|
elif mode == "debug":
|
2018-12-18 09:16:49 -05:00
|
|
|
out += ["is_debug=true"]
|
2018-07-26 20:15:55 -04:00
|
|
|
else:
|
2018-09-08 11:56:19 -04:00
|
|
|
print "Bad mode {}. Use 'release' or 'debug' (default)" % mode
|
2018-07-26 20:15:55 -04:00
|
|
|
sys.exit(1)
|
2018-09-08 11:56:19 -04:00
|
|
|
|
2018-07-26 20:15:55 -04:00
|
|
|
if "DENO_BUILD_ARGS" in os.environ:
|
|
|
|
out += os.environ["DENO_BUILD_ARGS"].split()
|
|
|
|
|
2018-12-18 21:09:30 -05:00
|
|
|
cacher = prebuilt.load_sccache()
|
|
|
|
if not os.path.exists(cacher):
|
|
|
|
cacher = find_executable("sccache") or find_executable("ccache")
|
|
|
|
|
2018-08-25 07:30:58 -04:00
|
|
|
# Check if ccache or sccache are in the path, and if so we set cc_wrapper.
|
2018-12-18 21:09:30 -05:00
|
|
|
cc_wrapper = cacher
|
2018-08-25 07:30:58 -04:00
|
|
|
if cc_wrapper:
|
2018-09-25 17:48:07 -04:00
|
|
|
# The gn toolchain does not shell escape cc_wrapper, so do it here.
|
|
|
|
out += ['cc_wrapper=%s' % gn_string(shell_quote(cc_wrapper))]
|
2018-08-25 07:30:58 -04:00
|
|
|
if os.name == "nt":
|
2018-10-25 22:13:10 -04:00
|
|
|
# Disable treat_warnings_as_errors until this sccache bug is fixed:
|
|
|
|
# https://github.com/mozilla/sccache/issues/264
|
|
|
|
out += ["treat_warnings_as_errors=false"]
|
2018-07-26 20:15:55 -04:00
|
|
|
|
2018-09-25 21:46:26 -04:00
|
|
|
# Look for sccache; if found, set rustc_wrapper.
|
2018-12-18 21:09:30 -05:00
|
|
|
rustc_wrapper = cacher
|
2018-09-25 21:46:26 -04:00
|
|
|
if rustc_wrapper:
|
|
|
|
out += ['rustc_wrapper=%s' % gn_string(rustc_wrapper)]
|
|
|
|
|
2018-07-26 20:15:55 -04:00
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
# gn gen.
|
2018-07-31 05:19:02 -04:00
|
|
|
def gn_gen(mode):
|
2018-07-26 20:15:55 -04:00
|
|
|
os.environ["DENO_BUILD_MODE"] = mode
|
|
|
|
|
2018-09-08 11:56:19 -04:00
|
|
|
# Rather than using gn gen --args we write directly to the args.gn file.
|
2018-07-26 20:15:55 -04:00
|
|
|
# This is to avoid quoting/escaping complications when passing overrides as
|
|
|
|
# command-line arguments.
|
|
|
|
args_filename = os.path.join(build_path(), "args.gn")
|
2018-09-08 11:56:19 -04:00
|
|
|
|
|
|
|
# Check if args.gn exists, and if it was auto-generated or handcrafted.
|
|
|
|
existing_gn_args, hand_edited = read_gn_args(args_filename)
|
|
|
|
|
|
|
|
# If args.gn wasn't handcrafted, regenerate it.
|
|
|
|
if hand_edited:
|
|
|
|
print "%s: Using gn options from hand edited '%s'." % (mode,
|
|
|
|
args_filename)
|
|
|
|
gn_args = existing_gn_args
|
2018-08-27 20:01:12 -04:00
|
|
|
else:
|
2018-09-08 11:56:19 -04:00
|
|
|
print "%s: Writing gn options to '%s'." % (mode, args_filename)
|
|
|
|
gn_args = generate_gn_args(mode)
|
|
|
|
if gn_args != existing_gn_args:
|
|
|
|
write_gn_args(args_filename, gn_args)
|
|
|
|
|
|
|
|
for line in gn_args:
|
|
|
|
print " " + line
|
2018-07-26 20:15:55 -04:00
|
|
|
|
|
|
|
run([third_party.gn_path, "gen", build_path()],
|
|
|
|
env=third_party.google_env())
|
2018-07-31 05:19:02 -04:00
|
|
|
|
|
|
|
|
2018-08-27 17:40:29 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|