1# Copyright 2020 The Dawn Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15find_package(PythonInterp REQUIRED) 16message(STATUS "Dawn: using python at ${PYTHON_EXECUTABLE}") 17 18# Check for Jinja2 19if (NOT DAWN_JINJA2_DIR) 20 message(STATUS "Dawn: Using system jinja2") 21 execute_process( 22 COMMAND ${PYTHON_EXECUTABLE} -c "import jinja2" 23 RESULT_VARIABLE RET 24 ) 25 if (NOT RET EQUAL 0) 26 message(FATAL_ERROR "Dawn: Missing dependencies for code generation, please ensure you have python-jinja2 installed.") 27 endif() 28else() 29 message(STATUS "Dawn: Using jinja2 at ${DAWN_JINJA2_DIR}") 30endif() 31 32# Function to invoke a generator_lib.py generator. 33# - SCRIPT is the name of the script to call 34# - ARGS are the extra arguments to pass to the script in addition to the base generator_lib.py arguments 35# - PRINT_NAME is the name to use when outputting status or errors 36# - RESULT_VARIABLE will be modified to contain the list of files generated by this generator 37function(DawnGenerator) 38 set(oneValueArgs SCRIPT RESULT_VARIABLE PRINT_NAME) 39 set(multiValueArgs ARGS) 40 cmake_parse_arguments(G "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 41 42 # Build the set of args common to all invocation of that generator. 43 set(BASE_ARGS 44 ${PYTHON_EXECUTABLE} 45 ${G_SCRIPT} 46 --template-dir 47 "${DAWN_TEMPLATE_DIR}" 48 --root-dir 49 "${Dawn_SOURCE_DIR}" 50 --output-dir 51 "${DAWN_BUILD_GEN_DIR}" 52 ${G_ARGS} 53 ) 54 if (DAWN_JINJA2_DIR) 55 list(APPEND BASE_ARGS --jinja2-path ${DAWN_JINJA2_DIR}) 56 endif() 57 58 # Call the generator to get the list of its dependencies. 59 execute_process( 60 COMMAND ${BASE_ARGS} --print-cmake-dependencies 61 OUTPUT_VARIABLE DEPENDENCIES 62 RESULT_VARIABLE RET 63 ) 64 if (NOT RET EQUAL 0) 65 message(FATAL_ERROR "Dawn: Failed to get the dependencies for ${G_PRINT_NAME}. Base args are '${BASE_ARGS}'.") 66 endif() 67 68 # Ask CMake to re-run if any of the dependencies changed as it might modify the build graph. 69 if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0") 70 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${DEPENDENCIES}) 71 endif() 72 73 # Call the generator to get the list of its outputs. 74 execute_process( 75 COMMAND ${BASE_ARGS} --print-cmake-outputs 76 OUTPUT_VARIABLE OUTPUTS 77 RESULT_VARIABLE RET 78 ) 79 if (NOT RET EQUAL 0) 80 message(FATAL_ERROR "Dawn: Failed to get the outputs for ${G_PRINT_NAME}. Base args are '${BASE_ARGS}'.") 81 endif() 82 83 # Add the custom command that calls the generator. 84 add_custom_command( 85 COMMAND ${BASE_ARGS} 86 DEPENDS ${DEPENDENCIES} 87 OUTPUT ${OUTPUTS} 88 COMMENT "Dawn: Generating files for ${G_PRINT_NAME}." 89 ) 90 91 # Return the list of outputs. 92 set(${G_RESULT_VARIABLE} ${OUTPUTS} PARENT_SCOPE) 93endfunction() 94 95# Helper function to call dawn_generator.py: 96# - TARGET is the generator target to build 97# - PRINT_NAME and RESULT_VARIABLE are like for DawnGenerator 98function(DawnJSONGenerator) 99 set(oneValueArgs TARGET RESULT_VARIABLE) 100 cmake_parse_arguments(G "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 101 102 DawnGenerator( 103 SCRIPT "${Dawn_SOURCE_DIR}/generator/dawn_json_generator.py" 104 ARGS --dawn-json 105 "${Dawn_SOURCE_DIR}/dawn.json" 106 --wire-json 107 "${Dawn_SOURCE_DIR}/dawn_wire.json" 108 --targets 109 ${G_TARGET} 110 RESULT_VARIABLE RET 111 ${G_UNPARSED_ARGUMENTS} 112 ) 113 114 # Forward the result up one more scope 115 set(${G_RESULT_VARIABLE} ${RET} PARENT_SCOPE) 116endfunction() 117