Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / mojo / tools / mopy / gn.py
blob69ddff4f743e198a21dc7455041e5803ac6c056d
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """
6 GN-related configuration functions, e.g., to produce a Config object from a GN
7 args.gn file).
8 """
11 import ast
12 import os.path
13 import re
15 from .config import Config
18 def BuildDirectoryForConfig(config, src_root):
19 """
20 Returns the build directory for the given configuration.
21 """
22 subdir = ""
23 if config.target_os == Config.OS_ANDROID:
24 subdir += "android_"
25 if config.target_cpu != Config.ARCH_ARM:
26 subdir += config.target_cpu + "_"
27 elif config.target_os == Config.OS_CHROMEOS:
28 subdir += "chromeos_"
29 subdir += "Debug" if config.is_debug else "Release"
30 if config.sanitizer == Config.SANITIZER_ASAN:
31 subdir += "_asan"
32 if not(config.is_debug) and config.dcheck_always_on:
33 subdir += "_dcheck"
34 return os.path.join(src_root, "out", subdir)
37 def GNArgsForConfig(config):
38 """
39 Return the arguments for gn for the given configuration. This function returns
40 a dictionary with boolean values as boolean.
41 """
42 gn_args = {}
44 gn_args["is_debug"] = bool(config.is_debug)
45 gn_args["is_asan"] = config.sanitizer == Config.SANITIZER_ASAN
47 if config.is_clang is not None:
48 gn_args["is_clang"] = bool(config.is_clang)
49 else:
50 gn_args["is_clang"] = config.target_os not in (Config.OS_ANDROID,
51 Config.OS_WINDOWS)
53 if config.values.get("use_goma"):
54 gn_args["use_goma"] = True
55 gn_args["goma_dir"] = config.values["goma_dir"]
56 else:
57 gn_args["use_goma"] = False
59 gn_args["dcheck_always_on"] = config.dcheck_always_on
61 gn_args["mojo_use_nacl"] = config.values.get("use_nacl", False)
63 if config.target_os == Config.OS_ANDROID:
64 gn_args["os"] = "android"
65 elif config.target_os == Config.OS_CHROMEOS:
66 gn_args["os"] = "chromeos"
67 gn_args["use_glib"] = False
68 gn_args["use_system_harfbuzz"] = False
69 elif config.target_os == Config.OS_LINUX:
70 gn_args["use_aura"] = False
71 gn_args["use_glib"] = False
72 gn_args["use_system_harfbuzz"] = False
74 gn_args["target_cpu"] = config.target_cpu
76 extra_args = config.values.get("gn_args")
77 if extra_args:
78 for arg in extra_args.split():
79 (name, val) = arg.split('=')
80 gn_args[name] = val
82 return gn_args
85 def CommandLineForGNArgs(gn_args):
86 """
87 Returns the list of gn arguments to use with the gn command line.
88 """
89 def _ToCommandLine(key, value):
90 if type(value) is bool:
91 return "%s=%s" % (key, "true" if value else "false")
92 return "%s=\"%s\"" % (key, value)
93 return [_ToCommandLine(x, y) for x, y in gn_args.iteritems()]
96 def ConfigForGNArgs(args):
97 """
98 Return the Config object for the given gn arguments. This function takes a
99 dictionary with boolean values as boolean.
101 config_args = {}
102 config_args["is_debug"] = args.get("is_debug", False)
103 config_args["sanitizer"] = (
104 Config.SANITIZER_ASAN if args.get("is_asan") else None)
105 config_args["is_clang"] = args.get("is_clang", False)
106 config_args["use_goma"] = args.get("use_goma", False)
107 if config_args["use_goma"]:
108 config_args["goma_dir"] = args.get("goma_dir")
109 config_args["use_nacl"] = args.get("mojo_use_nacl", False)
110 config_args["target_os"] = args.get("os")
111 config_args["target_cpu"] = args.get("target_cpu")
112 config_args["dcheck_always_on"] = args.get("dcheck_always_on")
113 return Config(**config_args)
116 def ParseGNConfig(build_dir):
118 Parse the gn config file present in |build_dir|. This function returns a
119 dictionary with boolean values as boolean.
121 TRANSLATIONS = {
122 "true": "True",
123 "false": "False",
125 gn_file = os.path.join(build_dir, "args.gn")
126 values = {}
127 with open(gn_file, "r") as f:
128 for line in f.readlines():
129 line = re.sub("\s*#.*", "", line)
130 result = re.match("^\s*(\w+)\s*=\s*(.*)\s*$", line)
131 if result:
132 key = result.group(1)
133 value = result.group(2)
134 values[key] = ast.literal_eval(TRANSLATIONS.get(value, value))
135 return values