Add initial CMakeLists.txt files

Adds CMake support for:
 - Generating the Dawn headers and C++ wrappers
 - libdawn_wire
 - libdawn_native with the Metal backend for now
 - All the examples.

Bug: dawn:333

Change-Id: I6ffbe090b0bd21d6a805c03a507ad51fda0275ca
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/15720
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
Corentin Wallez
2020-02-05 17:16:05 +00:00
committed by Commit Bot service account
parent 47b15048a1
commit 7fe6efba4a
11 changed files with 928 additions and 10 deletions

116
generator/CMakeLists.txt Normal file
View File

@@ -0,0 +1,116 @@
# Copyright 2020 The Dawn Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
find_package(PythonInterp REQUIRED)
message(STATUS "Dawn: using python at ${PYTHON_EXECUTABLE}")
# Check for Jinja2
if (NOT DAWN_JINJA2_DIR)
message(STATUS "Dawn: Using system jinja2")
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "import jinja2"
RESULT_VARIABLE RET
)
if (NOT RET EQUAL 0)
message(FATAL_ERROR "Dawn: Missing dependencies for code generation, please ensure you have python-jinja2 installed.")
endif()
else()
message(STATUS "Dawn: Using jinja2 at ${DAWN_JINJA2_DIR}")
endif()
# Function to invoke a generator_lib.py generator.
# - SCRIPT is the name of the script to call
# - ARGS are the extra arguments to pass to the script in addition to the base generator_lib.py arguments
# - PRINT_NAME is the name to use when outputting status or errors
# - RESULT_VARIABLE will be modified to contain the list of files generated by this generator
function(DawnGenerator)
set(oneValueArgs SCRIPT RESULT_VARIABLE PRINT_NAME)
set(multiValueArgs ARGS)
cmake_parse_arguments(G "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# Build the set of args common to all invocation of that generator.
set(BASE_ARGS
${PYTHON_EXECUTABLE}
${G_SCRIPT}
--template-dir
"${DAWN_TEMPLATE_DIR}"
--root-dir
"${Dawn_SOURCE_DIR}"
--output-dir
"${DAWN_BUILD_GEN_DIR}"
${G_ARGS}
)
if (DAWN_JINJA2_DIR)
list(APPEND BASE_ARGS --jinja2-path ${DAWN_JINJA2_DIR})
endif()
# Call the generator to get the list of its dependencies.
execute_process(
COMMAND ${BASE_ARGS} --print-cmake-dependencies
OUTPUT_VARIABLE DEPENDENCIES
RESULT_VARIABLE RET
)
if (NOT RET EQUAL 0)
message(FATAL_ERROR "Dawn: Failed to get the dependencies for ${G_PRINT_NAME}.")
endif()
# Ask CMake to re-run if any of the dependencies changed as it might modify the build graph.
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${DEPENDENCIES})
endif()
# Call the generator to get the list of its outputs.
execute_process(
COMMAND ${BASE_ARGS} --print-cmake-outputs
OUTPUT_VARIABLE OUTPUTS
RESULT_VARIABLE RET
)
if (NOT RET EQUAL 0)
message(FATAL_ERROR "Dawn: Failed to get the outputs for ${G_PRINT_NAME}.")
endif()
# Add the custom command that calls the generator.
add_custom_command(
COMMAND ${BASE_ARGS}
DEPENDS ${DEPENDENCIES}
OUTPUT ${OUTPUTS}
COMMENT "Dawn: Generating files for ${G_PRINT_NAME}."
)
# Return the list of outputs.
set(${G_RESULT_VARIABLE} ${OUTPUTS} PARENT_SCOPE)
endfunction()
# Helper function to call dawn_generator.py:
# - TARGET is the generator target to build
# - PRINT_NAME and RESULT_VARIABLE are like for DawnGenerator
function(DawnJSONGenerator)
set(oneValueArgs TARGET RESULT_VARIABLE)
cmake_parse_arguments(G "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
DawnGenerator(
SCRIPT "${Dawn_SOURCE_DIR}/generator/dawn_json_generator.py"
ARGS --dawn-json
"${Dawn_SOURCE_DIR}/dawn.json"
--wire-json
"${Dawn_SOURCE_DIR}/dawn_wire.json"
--targets
${G_TARGET}
RESULT_VARIABLE RET
${G_UNPARSED_ARGUMENTS}
)
# Forward the result up one more scope
set(${G_RESULT_VARIABLE} ${RET} PARENT_SCOPE)
endfunction()

View File

@@ -206,22 +206,39 @@ def _compute_python_dependencies(root_dir = None):
def run_generator(generator):
parser = argparse.ArgumentParser(
description = generator.get_description(),
formatter_class = argparse.ArgumentDefaultsHelpFormatter
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
)
generator.add_commandline_arguments(parser);
parser.add_argument('-t', '--template-dir', default='templates', type=str, help='Directory with template files.')
parser.add_argument('--template-dir', default='templates', type=str, help='Directory with template files.')
parser.add_argument(kJinja2Path, default=None, type=str, help='Additional python path to set before loading Jinja2')
parser.add_argument('--output-json-tarball', default=None, type=str, help='Name of the "JSON tarball" to create (tar is too annoying to use in python).')
parser.add_argument('--depfile', default=None, type=str, help='Name of the Ninja depfile to create for the JSON tarball')
parser.add_argument('--expected-outputs-file', default=None, type=str, help="File to compare outputs with and fail if it doesn't match")
parser.add_argument('--root-dir', default=None, type=str, help='Optional source root directory for Python dependency computations')
parser.add_argument('--allowed-output-dirs-file', default=None, type=str, help="File containing a list of allowed directories where files can be output.")
parser.add_argument('--print-cmake-dependencies', default=False, action="store_true", help="Prints a semi-colon separated list of dependencies to stdout and exits.")
parser.add_argument('--print-cmake-outputs', default=False, action="store_true", help="Prints a semi-colon separated list of outputs to stdout and exits.")
parser.add_argument('--output-dir', default=None, type=str, help='Directory where to output generate files.')
args = parser.parse_args()
renders = generator.get_file_renders(args);
# Output a list of all dependencies for CMake or the tarball for GN/Ninja.
if args.depfile != None or args.print_cmake_dependencies:
dependencies = generator.get_dependencies(args)
dependencies += [args.template_dir + os.path.sep + render.template for render in renders]
dependencies += _compute_python_dependencies(args.root_dir)
if args.depfile != None:
with open(args.depfile, 'w') as f:
f.write(args.output_json_tarball + ": " + " ".join(dependencies))
if args.print_cmake_dependencies:
sys.stdout.write(";".join(dependencies))
return 0
# The caller wants to assert that the outputs are what it expects.
# Load the file and compare with our renders.
if args.expected_outputs_file != None:
@@ -235,6 +252,11 @@ def run_generator(generator):
print("Actual output:\n " + repr(sorted(actual)))
return 1
# Print the list of all the outputs for cmake.
if args.print_cmake_outputs:
sys.stdout.write(";".join([os.path.join(args.output_dir, render.output) for render in renders]))
return 0
outputs = _do_renders(renders, args.template_dir)
# The caller wants to assert that the outputs are only in specific directories.
@@ -257,7 +279,7 @@ def run_generator(generator):
print(' "{}"'.format(directory))
return 1
# Output the tarball and its depfile
# Output the JSON tarball
if args.output_json_tarball != None:
json_root = {}
for output in outputs:
@@ -266,11 +288,14 @@ def run_generator(generator):
with open(args.output_json_tarball, 'w') as f:
f.write(json.dumps(json_root))
# Output a list of all dependencies for the tarball for Ninja.
if args.depfile != None:
dependencies = generator.get_dependencies(args)
dependencies += [args.template_dir + os.path.sep + render.template for render in renders]
dependencies += _compute_python_dependencies(args.root_dir)
# Output the files directly.
if args.output_dir != None:
for output in outputs:
output_path = os.path.join(args.output_dir, output.name)
with open(args.depfile, 'w') as f:
f.write(args.output_json_tarball + ": " + " ".join(dependencies))
directory = os.path.dirname(output_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(output_path, 'w') as outfile:
outfile.write(output.content)