blob: 0cdeaa26efc01091881d4c0de797a6cff3341e34 [file] [log] [blame]
Ben Claytonaffb7a32021-09-28 11:14:42 +00001# 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
15set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen")
Ben Clayton8bbcd802021-09-30 15:23:53 +000016set(IDLGEN_TOOL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tools/src/cmd/idlgen")
Ben Claytonaffb7a32021-09-28 11:14:42 +000017
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.
27function(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 Eng4948c812021-10-15 14:28:32 +000044 COMMAND ${GO_EXECUTABLE} "run" "main.go"
Ben Claytonaffb7a32021-09-28 11:14:42 +000045 "--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 )
56endfunction()
57
Ben Claytonaf48bbc2021-09-28 11:59:10 +000058add_subdirectory(binding)
Ben Claytonaffb7a32021-09-28 11:14:42 +000059add_subdirectory(interop)
Ben Claytond6ecf832021-09-29 09:51:21 +000060
Ben Clayton72e3ba62021-09-30 18:04:01 +000061add_library(dawn_node SHARED
62 "Module.cpp"
Ben Clayton72e3ba62021-09-30 18:04:01 +000063)
Ben Claytond6ecf832021-09-29 09:51:21 +000064set_target_properties(dawn_node PROPERTIES
65 PREFIX ""
66 OUTPUT_NAME "dawn"
67 SUFFIX ".node"
Ben Clayton75911ca2021-09-30 18:51:40 +000068 RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
Ben Claytond6ecf832021-09-29 09:51:21 +000069 LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
Ben Clayton75911ca2021-09-30 18:51:40 +000070 CXX_STANDARD 17
Ben Claytond6ecf832021-09-29 09:51:21 +000071)
72target_link_libraries(dawn_node dawn_node_binding dawn_node_interop dawn_native dawncpp dawn_proc)
73target_include_directories(dawn_node PRIVATE
Ben Clayton72e3ba62021-09-30 18:04:01 +000074 "${CMAKE_SOURCE_DIR}"
75 "${NODE_API_HEADERS_DIR}/include"
76 "${NODE_ADDON_API_DIR}"
77 "${GEN_DIR}"
Ben Claytond6ecf832021-09-29 09:51:21 +000078)
79
Ben Clayton75911ca2021-09-30 18:51:40 +000080# 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
89file(READ "${NODE_API_HEADERS_DIR}/symbols.js" NAPI_SYMBOLS_JS_CONTENT)
90string(REGEX MATCHALL "napi_[a-z0-9_]*" NAPI_SYMBOLS "${NAPI_SYMBOLS_JS_CONTENT}")
91
92if (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}")
116else()
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")
124endif()