Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 1 | # Copyright 2021 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 | |
| 15 | set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen") |
Ben Clayton | 8bbcd80 | 2021-09-30 15:23:53 +0000 | [diff] [blame] | 16 | set(IDLGEN_TOOL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tools/src/cmd/idlgen") |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 17 | |
| 18 | # idlgen() is a function that uses the tools/cmd/idlgen/main.go tool to generate |
| 19 | # code from an IDL file and template. |
| 20 | # idlgen() accepts the following named arguments: |
| 21 | # TEMPLATE <path> - (required) the path to the root .tmpl file. If the |
| 22 | # template imports other templates, then these should be |
| 23 | # added to the DEPENDS argument list. |
| 24 | # OUTPUT <path> - (required) the output file path. |
| 25 | # IDLS <paths> - (at least one required) the list of input WebIDL files. |
| 26 | # DEPENDS <paths> - an optional list of additional file dependencies used. |
| 27 | function(idlgen) |
| 28 | cmake_parse_arguments(IDLGEN |
| 29 | "" # options |
| 30 | "TEMPLATE;OUTPUT" # one_value_keywords |
| 31 | "IDLS;DEPENDS" # multi_value_keywords |
| 32 | ${ARGN}) |
| 33 | |
| 34 | if(NOT IDLGEN_TEMPLATE) |
| 35 | message(FATAL_ERROR "idlgen() missing TEMPLATE argument") |
| 36 | endif() |
| 37 | if(NOT IDLGEN_OUTPUT) |
| 38 | message(FATAL_ERROR "idlgen() missing OUTPUT argument") |
| 39 | endif() |
| 40 | if(NOT IDLGEN_IDLS) |
| 41 | message(FATAL_ERROR "idlgen() missing IDLS argument(s)") |
| 42 | endif() |
| 43 | add_custom_command( |
Austin Eng | 4948c81 | 2021-10-15 14:28:32 +0000 | [diff] [blame] | 44 | COMMAND ${GO_EXECUTABLE} "run" "main.go" |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 45 | "--template" "${IDLGEN_TEMPLATE}" |
| 46 | "--output" "${IDLGEN_OUTPUT}" |
| 47 | ${IDLGEN_IDLS} |
| 48 | DEPENDS "${IDLGEN_TOOL_DIR}/main.go" |
| 49 | ${IDLGEN_TEMPLATE} |
| 50 | ${IDLGEN_DEPENDS} |
| 51 | ${IDLGEN_IDLS} |
| 52 | OUTPUT ${IDLGEN_OUTPUT} |
| 53 | WORKING_DIRECTORY ${IDLGEN_TOOL_DIR} |
| 54 | COMMENT "Generating ${IDLGEN_OUTPUT}" |
| 55 | ) |
| 56 | endfunction() |
| 57 | |
Ben Clayton | af48bbc | 2021-09-28 11:59:10 +0000 | [diff] [blame] | 58 | add_subdirectory(binding) |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 59 | add_subdirectory(interop) |
Ben Clayton | d6ecf83 | 2021-09-29 09:51:21 +0000 | [diff] [blame] | 60 | |
Ben Clayton | 72e3ba6 | 2021-09-30 18:04:01 +0000 | [diff] [blame] | 61 | add_library(dawn_node SHARED |
| 62 | "Module.cpp" |
Ben Clayton | 72e3ba6 | 2021-09-30 18:04:01 +0000 | [diff] [blame] | 63 | ) |
Ben Clayton | d6ecf83 | 2021-09-29 09:51:21 +0000 | [diff] [blame] | 64 | set_target_properties(dawn_node PROPERTIES |
| 65 | PREFIX "" |
| 66 | OUTPUT_NAME "dawn" |
| 67 | SUFFIX ".node" |
Ben Clayton | 75911ca | 2021-09-30 18:51:40 +0000 | [diff] [blame] | 68 | RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" |
Ben Clayton | d6ecf83 | 2021-09-29 09:51:21 +0000 | [diff] [blame] | 69 | LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" |
Ben Clayton | 75911ca | 2021-09-30 18:51:40 +0000 | [diff] [blame] | 70 | CXX_STANDARD 17 |
Ben Clayton | d6ecf83 | 2021-09-29 09:51:21 +0000 | [diff] [blame] | 71 | ) |
| 72 | target_link_libraries(dawn_node dawn_node_binding dawn_node_interop dawn_native dawncpp dawn_proc) |
| 73 | target_include_directories(dawn_node PRIVATE |
Ben Clayton | 72e3ba6 | 2021-09-30 18:04:01 +0000 | [diff] [blame] | 74 | "${CMAKE_SOURCE_DIR}" |
| 75 | "${NODE_API_HEADERS_DIR}/include" |
| 76 | "${NODE_ADDON_API_DIR}" |
| 77 | "${GEN_DIR}" |
Ben Clayton | d6ecf83 | 2021-09-29 09:51:21 +0000 | [diff] [blame] | 78 | ) |
| 79 | |
Ben Clayton | 75911ca | 2021-09-30 18:51:40 +0000 | [diff] [blame] | 80 | # To reduce the build dependencies for compiling the dawn.node targets, we do |
| 81 | # not use cmake-js for building, but instead just depend on node_api_headers. |
| 82 | # As the name suggests, node_api_headers contains just the *headers* of Napi, |
| 83 | # and does not provide a library to link against. |
| 84 | # Fortunately node_api_headers provides a list of Napi symbols exported by Node, |
| 85 | # which we can use to either produce weak-symbol stubs (unix) or generate a .lib |
| 86 | # (Windows). |
| 87 | |
| 88 | # Parse the Napi symbols from ${NODE_API_HEADERS_DIR}/symbols.js |
| 89 | file(READ "${NODE_API_HEADERS_DIR}/symbols.js" NAPI_SYMBOLS_JS_CONTENT) |
| 90 | string(REGEX MATCHALL "napi_[a-z0-9_]*" NAPI_SYMBOLS "${NAPI_SYMBOLS_JS_CONTENT}") |
| 91 | |
| 92 | if (WIN32) |
| 93 | # Generate the NapiSymbols.def file from the Napi symbol list |
| 94 | set(NAPI_SYMBOLS_DEF "${GEN_DIR}/NapiSymbols.def") |
| 95 | list(TRANSFORM NAPI_SYMBOLS PREPEND " ") |
| 96 | list(TRANSFORM NAPI_SYMBOLS APPEND "\n") |
| 97 | string(REPLACE ";" "" NAPI_SYMBOLS "${NAPI_SYMBOLS}") |
| 98 | string(PREPEND NAPI_SYMBOLS "LIBRARY node.exe\nEXPORTS\n") |
| 99 | file(GENERATE OUTPUT "${NAPI_SYMBOLS_DEF}" CONTENT "${NAPI_SYMBOLS}") |
| 100 | # Generate the NapiSymbols.lib from the NapiSymbols.def file |
| 101 | set(NAPI_SYMBOLS_LIB "${GEN_DIR}/NapiSymbols.lib") |
| 102 | # Resolve path to lib.exe |
| 103 | get_filename_component(VS_BIN_DIR "${CMAKE_LINKER}" DIRECTORY) |
| 104 | set(LIB_EXE "${VS_BIN_DIR}/lib.exe") |
| 105 | add_custom_command( |
| 106 | COMMAND "${LIB_EXE}" |
| 107 | "/DEF:${NAPI_SYMBOLS_DEF}" |
| 108 | "/OUT:${NAPI_SYMBOLS_LIB}" |
| 109 | DEPENDS "${NAPI_SYMBOLS_DEF}" |
| 110 | OUTPUT "${NAPI_SYMBOLS_LIB}" |
| 111 | COMMENT "Generating ${NAPI_SYMBOLS_LIB}" |
| 112 | ) |
| 113 | add_custom_target(napi-symbols DEPENDS "${NAPI_SYMBOLS_LIB}") |
| 114 | add_dependencies(dawn_node napi-symbols) |
| 115 | target_link_libraries(dawn_node "${NAPI_SYMBOLS_LIB}") |
| 116 | else() |
| 117 | # Generate the NapiSymbols.h file from the Napi symbol list |
| 118 | set(NAPI_SYMBOLS_H "${GEN_DIR}/NapiSymbols.h") |
| 119 | list(TRANSFORM NAPI_SYMBOLS PREPEND "NAPI_SYMBOL(") |
| 120 | list(TRANSFORM NAPI_SYMBOLS APPEND ")\n") |
| 121 | string(REPLACE ";" "" NAPI_SYMBOLS "${NAPI_SYMBOLS}") |
| 122 | file(GENERATE OUTPUT "${NAPI_SYMBOLS_H}" CONTENT "${NAPI_SYMBOLS}") |
| 123 | target_sources(dawn_node PRIVATE "NapiSymbols.cpp") |
| 124 | endif() |