Skip to content

Commit

Permalink
Support configuring enabled builtins using CMake options (#30)
Browse files Browse the repository at this point in the history
This modifies the `add_builtin` CMake function to derive a CMake option from the name of the builtin, and only to only install the builtin if that option is set.

The README.md is also modified to document how to get a list of all available builtins, and whether they're enabled by default.

Note that this mechanism will automatically apply to CMake projects using StarlingMonkey, too.
  • Loading branch information
tschneidereit authored Apr 18, 2024
1 parent 073e7bb commit e15b39f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 43 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ cd cmake-build-debug
ctest --verbose # Note: some of the tests run fairly slowly in debug builds, so be patient
```

## Configuring available builtins
StarlingMonkey supports enabling/disabling bundled builtins using CMake options. You can get a full list of bundled builtins by running the following shell command:
```shell
cmake -P [PATH_TO_STARLING_MONKEY]/cmake/builtins.cmake
```

Note that it's required to include builtins defining all exports defined by the used host API. Using the default WASI 0.2.0 host API, that means including the `fetch_event` builtin.


## Using StarlingMonkey as a CMake sub-project

Expand Down
39 changes: 36 additions & 3 deletions cmake/add_builtin.cmake
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
if(CMAKE_SCRIPT_MODE_FILE)
message(STATUS "Available builtins:")
endif()

function(add_builtin)
if (ARGC EQUAL 1)
list(GET ARGN 0 SRC)
cmake_path(GET SRC STEM NAME)
cmake_path(GET SRC PARENT_PATH DIR)
string(REPLACE "/" "::" NS ${DIR})
set(NS ${NS}::${NAME})
set(DEFAULT_ENABLE ON)
else()
cmake_parse_arguments(PARSE_ARGV 1 "" "" "" "SRC")
cmake_parse_arguments(PARSE_ARGV 1 "" "DISABLED_BY_DEFAULT" "" "SRC;LINK_LIBS;INCLUDE_DIRS")
list(GET ARGN 0 NS)
set(SRC ${_SRC})
set(LINK_LIBS ${_LINK_LIBS})
set(INCLUDE_DIRS ${_INCLUDE_DIRS})
if (_DISABLED_BY_DEFAULT)
set(DEFAULT_ENABLE OFF)
else()
set(DEFAULT_ENABLE ON)
endif()
endif()
string(REPLACE "-" "_" NS ${NS})
string(REPLACE "::" "_" LIB_NAME ${NS})
message(STATUS "Adding builtin ${LIB_NAME}")
string(REGEX REPLACE "^builtins_" "" LIB_NAME ${LIB_NAME})
set(LIB_NAME_NO_PREFIX ${LIB_NAME})
string(PREPEND LIB_NAME "builtin_")
string(TOUPPER ${LIB_NAME} LIB_NAME_UPPER)
set(OPT_NAME ENABLE_${LIB_NAME_UPPER})
set(DESCRIPTION "${LIB_NAME_NO_PREFIX} (option: ${OPT_NAME}, default: ${DEFAULT_ENABLE})")

# In script-mode, just show the available builtins.
if(CMAKE_SCRIPT_MODE_FILE)
message(STATUS " ${DESCRIPTION}")
return()
endif()

option(${OPT_NAME} "Enable ${LIB_NAME}" ${DEFAULT_ENABLE})
if (${${OPT_NAME}})
else()
message(STATUS "Skipping builtin ${DESCRIPTION}")
return()
endif()

message(STATUS "Adding builtin ${DESCRIPTION}")

add_library(${LIB_NAME} STATIC ${SRC})
target_link_libraries(${LIB_NAME} PRIVATE spidermonkey extension_api)
target_link_libraries(${LIB_NAME} PRIVATE spidermonkey extension_api ${LINK_LIBS})
target_link_libraries(builtins PRIVATE ${LIB_NAME})
target_include_directories(${LIB_NAME} PRIVATE ${INCLUDE_DIRS})
file(APPEND $CACHE{INSTALL_BUILTINS} "NS_DEF(${NS})\n")
return(PROPAGATE LIB_NAME)
endfunction()
107 changes: 67 additions & 40 deletions cmake/builtins.cmake
Original file line number Diff line number Diff line change
@@ -1,73 +1,100 @@
if(CMAKE_SCRIPT_MODE_FILE)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
else()
add_library(builtins STATIC builtins/install_builtins.cpp)
target_include_directories(builtins PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(builtins PRIVATE extension_api)
endif()

include("add_builtin")

set(INSTALL_BUILTINS ${CMAKE_CURRENT_BINARY_DIR}/builtins.incl CACHE INTERNAL "Path to the builtins.incl file" FORCE)
file(WRITE ${INSTALL_BUILTINS} "// This file is generated by CMake\n")

add_library(builtins STATIC builtins/install_builtins.cpp)
target_include_directories(builtins PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(builtins PRIVATE extension_api)

# These builtins are always enabled.
add_builtin(builtins/web/global_self.cpp)

add_builtin(builtins/web/url.cpp)
target_include_directories(builtins_web_url PRIVATE runtime)

add_builtin(builtins/web/queue-microtask.cpp)
add_builtin(builtins/web/structured-clone.cpp)
add_builtin(builtins/web/base64.cpp)
add_builtin(
builtins::web::dom_exception
SRC
builtins/web/dom-exception.cpp
INCLUDE_DIRS
runtime)

add_builtin(builtins/web/console.cpp)
add_builtin(
builtins::web::url
SRC
builtins/web/url.cpp
INCLUDE_DIRS
runtime)

add_builtin(builtins/web/dom-exception.cpp)
target_include_directories(builtins_web_dom_exception PRIVATE runtime)
add_builtin(builtins/web/console.cpp)

add_builtin(builtins/web/performance.cpp)
add_builtin(builtins/web/queue-microtask.cpp)
add_builtin(builtins/web/structured-clone.cpp)

add_builtin(builtins/web/timers.cpp)
target_include_directories(builtins_web_timers PRIVATE runtime)
add_builtin(
builtins::web::timers
SRC
builtins/web/timers.cpp
INCLUDE_DIRS
runtime)

add_builtin(builtins/web/worker-location.cpp)

add_builtin(
builtins::web::text-codec
SRC
builtins/web/text-codec/text-codec.cpp
builtins/web/text-codec/text-decoder.cpp
builtins/web/text-codec/text-encoder.cpp)
target_include_directories(builtins_web_text_codec PRIVATE runtime)
builtins::web::text-codec
SRC
builtins/web/text-codec/text-codec.cpp
builtins/web/text-codec/text-decoder.cpp
builtins/web/text-codec/text-encoder.cpp
INCLUDE_DIRS
runtime)

add_builtin(
builtins::web::streams
SRC
builtins/web/streams/compression-stream.cpp
builtins/web/streams/decompression-stream.cpp
builtins/web/streams/native-stream-sink.cpp
builtins/web/streams/native-stream-source.cpp
builtins/web/streams/streams.cpp
builtins/web/streams/transform-stream.cpp
builtins/web/streams/transform-stream-default-controller.cpp)
target_include_directories(builtins_web_streams PRIVATE runtime)
builtins::web::streams
SRC
builtins/web/streams/compression-stream.cpp
builtins/web/streams/decompression-stream.cpp
builtins/web/streams/native-stream-sink.cpp
builtins/web/streams/native-stream-source.cpp
builtins/web/streams/streams.cpp
builtins/web/streams/transform-stream.cpp
builtins/web/streams/transform-stream-default-controller.cpp
INCLUDE_DIRS
runtime)

add_builtin(
builtins::web::fetch
SRC
builtins::web::fetch
SRC
builtins/web/fetch/fetch-api.cpp
builtins/web/fetch/headers.cpp
builtins/web/fetch/request-response.cpp)

add_builtin(builtins/web/fetch/fetch_event.cpp)
target_link_libraries(builtins_web_fetch_fetch_event PRIVATE host_api)
target_include_directories(builtins_web_fetch_fetch_event PRIVATE runtime ${HOST_API}/bindings/)
add_builtin(
builtins::web::fetch::fetch_event
SRC
builtins/web/fetch/fetch_event.cpp
LINK_LIBS
host_api
INCLUDE_DIRS
runtime
${HOST_API}/bindings/)

add_builtin(
builtins::web::crypto
SRC
builtins::web::crypto
SRC
builtins/web/crypto/crypto.cpp
builtins/web/crypto/crypto-algorithm.cpp
builtins/web/crypto/crypto-key.cpp
builtins/web/crypto/crypto-key-ec-components.cpp
builtins/web/crypto/crypto-key-rsa-components.cpp
builtins/web/crypto/json-web-key.cpp
builtins/web/crypto/subtle-crypto.cpp)
target_link_libraries(builtins_web_crypto PRIVATE OpenSSL::Crypto fmt)
target_include_directories(builtins_web_crypto PRIVATE runtime)
builtins/web/crypto/subtle-crypto.cpp
LINK_LIBS
OpenSSL::Crypto
fmt
INCLUDE_DIRS
runtime)

0 comments on commit e15b39f

Please sign in to comment.