1# Copyright (c) 2023-2024 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14add_custom_target(declgen_ets2ts_test) 15add_custom_target(declgen_ets2ts_test_diff) 16add_dependencies(ets_interop_js_tests declgen_ets2ts_test_diff declgen_ets2ts_test) 17 18# Create create_declgen_ets2ts_test test 19# 20# Declgen test consists of files: 21# ${ROOT_DIR}/*.sts 22# ${ROOT_DIR}/*.ts 23# ${ROOT_DIR}/*.js 24# ${ROOT_DIR}/*.expected 25# This function: 26# 1. Compiles 'ets' files with es2panda into 'abc' and decl 'ts': 27# ${ROOT_DIR}/*.sts -> ${BINARY_ROOT_DIR}/*.abc ${BINARY_ROOT_DIR}/*.ts 28# 2. Copies ${ROOT_DIR}/*.ts into ${BINARY_ROOT_DIR}/*.ts 29# 3. Compiles 'ts' project with tsc: 30# ${BINARY_ROOT_DIR}/*.ts -> ${BINARY_ROOT_DIR}/*.js 31# 4. Links all 'abc' files into single file: 32# ${BINARY_ROOT_DIR}/*.abc -> ${BINARY_ROOT_DIR}/lib_linked.abc 33# 5. Creates panda_ets_interop_js_test from ${BINARY_ROOT_DIR}/*.js and ${BINARY_ROOT_DIR}/lib_linked.abc 34# 6. Tests difference between expected and actual declaration files: 35# ${BINARY_ROOT_DIR}/*.ts <-> ${ROOT_DIR}/*.expected 36# 37# Example usage: 38# create_declgen_ets2ts_test(common 39# ROOT_DIR 40# ${CMAKE_CURRENT_SOURCE_DIR} 41# BINARY_ROOT_DIR 42# ${CMAKE_CURRENT_BINARY_DIR} 43# TS_MAIN 44# ${CMAKE_CURRENT_SOURCE_DIR}/main.ts 45# TS_SOURCES 46# ${CMAKE_CURRENT_SOURCE_DIR}/test.ts 47# JS_SOURCES 48# ${CMAKE_CURRENT_SOURCE_DIR}/test_dyn.js 49# ETS_SOURCES 50# ${CMAKE_CURRENT_SOURCE_DIR}/lib1.sts 51# ${CMAKE_CURRENT_SOURCE_DIR}/lib2.sts 52# DECLS_EXPECTED 53# ${CMAKE_CURRENT_SOURCE_DIR}/lib1.expected 54# ${CMAKE_CURRENT_SOURCE_DIR}/lib2.expected 55# ) 56function(create_declgen_ets2ts_test TARGET) 57 cmake_parse_arguments( 58 ARG 59 "" 60 "ROOT_DIR;BINARY_ROOT_DIR;TS_MAIN" 61 "TS_SOURCES;ETS_SOURCES;JS_SOURCES;DECLS_EXPECTED" 62 ${ARGN} 63 ) 64 65 # Replaces path prefix from ARG_ROOT_DIR to ARG_BINARY_ROOT_DIR and extension 66 function(rebase_path SRC DST) 67 string(REGEX REPLACE "^${ARG_ROOT_DIR}" "${ARG_BINARY_ROOT_DIR}/" RES ${SRC}) 68 if(ARGC GREATER 2) 69 set(NEW_EXT ${ARGV2}) 70 string(REGEX REPLACE "\.[a-zA-Z0-9]+$" ".${NEW_EXT}" RES ${RES}) 71 endif() 72 set(${DST} ${RES} PARENT_SCOPE) 73 endfunction() 74 75 set(TARGET "declgen_ets2ts_test_${TARGET}") 76 77 # Create arktsconfig.json with "dynamicPaths" for js modules 78 set(ARKTSCONFIG_CONTENT 79 " }\n" 80 " }\n" 81 "}\n" 82 ) 83 set(COMMA "") 84 foreach(JS_SOURCE ${ARG_JS_SOURCES}) 85 set(ARKTSCONFIG_CONTENT 86 " \"${JS_SOURCE}\": {\"language\": \"js\", \"hasDecl\": false}${COMMA}\n" 87 "${ARKTSCONFIG_CONTENT}" 88 ) 89 if(COMMA STREQUAL "") 90 set(COMMA ",") 91 endif() 92 endforeach() 93 set(ARKTSCONFIG_CONTENT 94 "{\n" 95 " \"compilerOptions\": {\n" 96 " \"baseUrl\": \"${PANDA_ROOT}\",\n" 97 " \"paths\": {\n" 98 " \"std\": [\"${PANDA_ROOT}/plugins/ets/stdlib/std\"],\n" 99 " \"escompat\": [\"${PANDA_ROOT}/plugins/ets/stdlib/escompat\"]\n" 100 " },\n" 101 " \"dynamicPaths\": {\n" 102 "${ARKTSCONFIG_CONTENT}" 103 ) 104 set(ARKTSCONFIG ${CMAKE_CURRENT_BINARY_DIR}/arktsconfig.json) 105 file(WRITE ${ARKTSCONFIG} ${ARKTSCONFIG_CONTENT}) 106 107 # NOTE (kkonsw): temporary disabling all other checks 108 set(ETS_VERIFICATOR_ERRORS "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll,IdentifierHasVariableForAll,ReferenceTypeAnnotationIsNullForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll,ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll,ImportExportAccessValid") 109 110 # Compile source .sts files into .abc and declaration .ts files 111 set(ABC_FILES "") 112 set(DECL_FILES "") 113 set(ABC_TARGETS "") 114 set(ETS_MODULE_KEY "") 115 foreach(ETS_SOURCE ${ARG_ETS_SOURCES}) 116 rebase_path(${ETS_SOURCE} ABC_PATH "abc") 117 list(APPEND ABC_FILES ${ABC_PATH}) 118 rebase_path(${ETS_SOURCE} DECL_PATH "ts") 119 list(APPEND DECL_FILES ${DECL_PATH}) 120 add_custom_command( 121 OUTPUT ${ABC_PATH} ${DECL_PATH} 122 COMMENT "Compiling: ${ETS_SOURCE}" 123 COMMAND $<TARGET_FILE:es2panda> ${ETS_MODULE_KEY} --arktsconfig ${ARKTSCONFIG} --output ${ABC_PATH} --extension=sts --verifier-errors=${ETS_VERIFICATOR_ERRORS} ${ETS_SOURCE} 124 COMMAND $<TARGET_FILE:declgen_ets2ts> ${ETS_MODULE_KEY} --arktsconfig ${ARKTSCONFIG} --output ${DECL_PATH} --extension=sts ${ETS_SOURCE} 125 DEPENDS es2panda declgen_ets2ts ${ETS_SOURCE} ${ARKTSCONFIG} 126 ) 127 128 get_filename_component(ABC_NAME ${ABC_PATH} NAME) 129 set(ABC_TARGET ${TARGET}_es2panda_${ABC_NAME}) 130 add_custom_target(${ABC_TARGET} 131 DEPENDS ${ABC_PATH} ${DECL_PATH} 132 ) 133 list(APPEND ABC_TARGETS ${ABC_TARGET}) 134 if(ETS_MODULE_KEY STREQUAL "") 135 set(ETS_MODULE_KEY "--ets-module") 136 endif() 137 endforeach() 138 139 set(COPIED_TS_SOURCES "") 140 set(JS_FILES "") 141 # Copy TS files into build folder and 142 # Compute destination JS file paths for these TS files 143 foreach(TS_SOURCE ${ARG_TS_SOURCES}) 144 rebase_path(${TS_SOURCE} COPIED_TS_SOURCE) 145 configure_file(${TS_SOURCE} ${COPIED_TS_SOURCE} COPYONLY) 146 list(APPEND COPIED_TS_SOURCES ${COPIED_TS_SOURCE}) 147 rebase_path(${TS_SOURCE} JS_SOURCE "js") 148 list(APPEND JS_FILES ${JS_SOURCE}) 149 endforeach() 150 151 # Compute destination JS file paths for generated TS declarations 152 foreach(TS_SOURCE ${DECL_FILES}) 153 rebase_path(${TS_SOURCE} JS_SOURCE "js") 154 set(JS_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${JS_SOURCE}") 155 list(APPEND JS_FILES ${JS_SOURCE}) 156 endforeach() 157 158 # Copy main.ts into build folder and compute main.js path 159 rebase_path(${ARG_TS_MAIN} COPIED_TS_MAIN) 160 configure_file(${ARG_TS_MAIN} ${COPIED_TS_MAIN} COPYONLY) 161 list(APPEND COPIED_TS_SOURCES ${COPIED_TS_MAIN}) 162 rebase_path(${COPIED_TS_MAIN} JS_MAIN "js") 163 list(APPEND JS_FILES ${JS_MAIN}) 164 165 # Compile source .ts and declaratin .ts files into .js 166 add_custom_command( 167 OUTPUT ${JS_FILES} 168 COMMENT "Compile ts files: ${COPIED_TS_SOURCES} ${DECL_FILES}" 169 COMMAND ${PANDA_THIRD_PARTY_SOURCES_DIR}/typescript/bin/tsc --target es5 --strict --outDir ${CMAKE_CURRENT_BINARY_DIR} ${COPIED_TS_SOURCES} ${DECL_FILES} 170 DEPENDS es2panda ark_link ${ABC_TARGETS} ${ARKTSCONFIG} ${COPIED_TS_SOURCES} ${DECL_FILES} 171 ) 172 add_custom_target(${TARGET}_tsc 173 DEPENDS ${JS_FILES} 174 ) 175 176 # Link .abc files into single .abc file 177 set(LIB_LINKED_ABC ${CMAKE_CURRENT_BINARY_DIR}/lib_linked.abc) 178 add_custom_command( 179 OUTPUT ${LIB_LINKED_ABC} 180 COMMENT "Linking ABC files: ${ABC_FILES}" 181 COMMAND $<TARGET_FILE:ark_link> --output ${LIB_LINKED_ABC} -- ${ABC_FILES} 182 DEPENDS ark_link ${ABC_TARGETS} ${ABC_FILES} 183 ) 184 add_custom_target(${TARGET}_link 185 DEPENDS ${LIB_LINKED_ABC} 186 ) 187 188 # Execute compiled interop project 189 panda_ets_interop_js_test(${TARGET} 190 ABC_FILE ${LIB_LINKED_ABC} 191 JS_LAUNCHER ${JS_MAIN} 192 ) 193 add_dependencies(${TARGET} ${TARGET}_tsc ${TARGET}_link) 194 add_dependencies(declgen_ets2ts_test ${TARGET}) 195 196 # Test difference between actual and expected declarations 197 list(LENGTH DECL_FILES DECL_LEN) 198 list(LENGTH ARG_DECLS_EXPECTED DECL_EXPECTED_LEN) 199 if(NOT DECL_LEN EQUAL DECL_EXPECTED_LEN) 200 message(FATAL_ERROR "create_declgen_ets2ts_test: number of source ets files is not equal to number of expected declarations") 201 endif() 202 math(EXPR DECL_LEN "${DECL_LEN} - 1") 203 foreach(IDX RANGE ${DECL_LEN}) 204 list(GET ARG_DECLS_EXPECTED ${IDX} DECL_EXPECTED) 205 list(GET DECL_FILES ${IDX} DECL_ACTUAL) 206 207 get_filename_component(DECL_NAME ${DECL_EXPECTED} NAME) 208 set(DIFF_TARGET ${TARGET}_diff_${DECL_NAME}) 209 210 add_custom_target(${DIFF_TARGET} 211 COMMENT "Test declgen ets2ts diff between ${DECL_ACTUAL} and ${DECL_EXPECTED}" 212 COMMAND ${CMAKE_COMMAND} -E compare_files ${DECL_ACTUAL} ${DECL_EXPECTED} 213 DEPENDS ${DECL_ACTUAL} ${DECL_EXPECTED} 214 ) 215 add_dependencies(declgen_ets2ts_test_diff ${DIFF_TARGET}) 216 endforeach() 217endfunction() 218 219add_subdirectory(async) 220add_subdirectory(classes) 221add_subdirectory(enums) 222add_subdirectory(fields) 223add_subdirectory(functions) 224add_subdirectory(generics) 225add_subdirectory(getters_setters) 226add_subdirectory(imports) 227add_subdirectory(methods) 228add_subdirectory(unions) 229add_subdirectory(properties) 230# NOTE(aakmaev): need proper comparing. Issue #17097 231# add_subdirectory(global_vars) 232