Generator,BUILD.gn: add optional python path, use it for Jinja2

This makes the Jinja2 (and MarkupSafe) installation hermetic by
adding it to the DEPS and making the code generator add them in the
first spot of the python path.
This commit is contained in:
Corentin Wallez 2018-08-13 17:47:44 +02:00 committed by Corentin Wallez
parent 682c44af92
commit 3bb0bb940e
5 changed files with 33 additions and 11 deletions

View File

@ -21,6 +21,8 @@ import("//testing/test.gni")
# Template to wrap the Dawn code generator
###############################################################################
jinja2_python_path = rebase_path("${dawn_jinja2_dir}/..")
template("dawn_generator") {
generator = "generator/main.py"
json = "dawn.json"
@ -30,11 +32,13 @@ template("dawn_generator") {
common_args = [
rebase_path(json, root_build_dir),
"-t",
"--template-dir",
rebase_path(template_dir, root_build_dir),
"-o",
"--output-dir",
rebase_path(target_gen_dir, root_build_dir),
"-T",
"--extra-python-path",
jinja2_python_path,
"--targets",
target,
]

10
DEPS
View File

@ -31,6 +31,16 @@ deps = {
'condition': 'dawn_standalone',
},
# Jinja2 and MarkupSafe for the code generator
'{dawn_root}/third_party/jinja2': {
'url': '{chromium_git}/chromium/src/third_party/jinja2@b41863e42637544c2941b574c7877d3e1f663e25',
'condition': 'dawn_standalone',
},
'{dawn_root}/third_party/markupsafe': {
'url': '{chromium_git}/chromium/src/third_party/markupsafe@8f45f5cfa0009d2a70589bcda0349b8cb2b72783',
'condition': 'dawn_standalone',
},
# SPIRV-Cross
'{dawn_root}/third_party/spirv-cross': {
'url': '{github_git}/Kangz/SPIRV-Cross.git@694cad533296df02b4562f4a5a20cba1d1a9dbaf',

View File

@ -30,3 +30,4 @@ dawn_glslang_dir = "//third_party/glslang"
dawn_spirv_tools_dir = "//third_party/SPIRV-Tools"
dawn_shaderc_dir = "//third_party/shaderc"
dawn_glfw_dir = "//third_party/glfw"
dawn_jinja2_dir = "//third_party/jinja2"

View File

@ -212,14 +212,16 @@ def parse_json(json):
import re, os, sys
from collections import OrderedDict
try:
import jinja2
except ImportError:
# Try using Chromium's Jinja2
dir, _ = os.path.split(os.path.realpath(__file__))
third_party_dir = os.path.normpath(dir + (os.path.sep + os.path.pardir) * 2)
sys.path.insert(1, third_party_dir)
import jinja2
kExtraPythonPath = '--extra-python-path'
# Try using an additional python path from the arguments if present. This
# isn't done through the regular argparse because PreprocessingLoader uses
# jinja2 in the global scope before "main" gets to run.
if kExtraPythonPath in sys.argv:
path = sys.argv[sys.argv.index(kExtraPythonPath) + 1]
sys.path.insert(1, path)
import jinja2
# A custom Jinja2 template loader that removes the extra indentation
# of the template blocks so that the output is correctly indented
@ -398,6 +400,7 @@ def main():
parser.add_argument('-t', '--template-dir', default='templates', type=str, help='Directory with template files.')
parser.add_argument('-o', '--output-dir', default=None, type=str, help='Output directory for the generated source files.')
parser.add_argument('-T', '--targets', default=None, type=str, help='Comma-separated subset of targets to output. Available targets: ' + ', '.join(targets))
parser.add_argument(kExtraPythonPath, default=None, type=str, help='Additional python path to set before loading Jinja2')
parser.add_argument('--print-dependencies', action='store_true', help='Prints a space separated list of file dependencies, used for CMake integration')
parser.add_argument('--print-outputs', action='store_true', help='Prints a space separated list of file outputs, used for CMake integration')
parser.add_argument('--gn', action='store_true', help='Make the printing of dependencies/outputs GN-friendly')

View File

@ -43,3 +43,7 @@ if (!defined(dawn_shaderc_dir)) {
if (!defined(dawn_glfw_dir)) {
dawn_glfw_dir = "//third_party/glfw"
}
if (!defined(dawn_jinja2_dir)) {
dawn_jinja2_dir = "//third_party/jinja2"
}