summaryrefslogtreecommitdiff
path: root/tools/setup.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/setup.py')
-rwxr-xr-xtools/setup.py78
1 files changed, 62 insertions, 16 deletions
diff --git a/tools/setup.py b/tools/setup.py
index 737f80017..77aaf95d6 100755
--- a/tools/setup.py
+++ b/tools/setup.py
@@ -2,6 +2,7 @@
import third_party
from util import run, root_path, build_path, build_mode
import os
+import re
import sys
from distutils.spawn import find_executable
@@ -44,15 +45,55 @@ def write_lastchange():
# ])
-def get_gn_args():
+# 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.", ""
+]
+
+
+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.
+ dir = os.path.dirname(args_filename)
+ if not os.path.isdir(dir):
+ os.makedirs(dir)
+
+ with open(args_filename, "w") as f:
+ f.write("\n".join(lines) + "\n")
+
+
+def generate_gn_args(mode):
out = []
- if build_mode() == "release":
+ if mode == "release":
out += ["is_official_build=true"]
- elif build_mode() == "debug":
+ elif mode == "debug":
pass
else:
- print "Bad mode {}. Use 'release' or 'debug' (default)" % build_mode()
+ print "Bad mode {}. Use 'release' or 'debug' (default)" % mode
sys.exit(1)
+
if "DENO_BUILD_ARGS" in os.environ:
out += os.environ["DENO_BUILD_ARGS"].split()
@@ -67,8 +108,6 @@ def get_gn_args():
tc = "//build_extra/toolchain/win:win_clang_x64"
out += ['custom_toolchain="%s"' % tc, 'host_toolchain="%s"' % tc]
- print "DENO_BUILD_ARGS:", out
-
return out
@@ -76,20 +115,27 @@ def get_gn_args():
def gn_gen(mode):
os.environ["DENO_BUILD_MODE"] = mode
- # mkdir $build_path(). We do this so we can write args.gn before running gn gen.
- if not os.path.isdir(build_path()):
- os.makedirs(build_path())
-
- # Rather than using gn gen --args we manually write the args.gn override file.
+ # Rather than using gn gen --args we write directly to the args.gn file.
# This is to avoid quoting/escaping complications when passing overrides as
# command-line arguments.
args_filename = os.path.join(build_path(), "args.gn")
- if os.path.exists(args_filename):
- print "Skip writing args file: '%s' already exists." % args_filename
+
+ # 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
else:
- gn_args = get_gn_args()
- with open(args_filename, "w+") as f:
- f.write("\n".join(gn_args) + "\n")
+ 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
run([third_party.gn_path, "gen", build_path()],
env=third_party.google_env())