Skip to content

Commit

Permalink
refactor: move C++ source to src
Browse files Browse the repository at this point in the history
This commit moves the C++ build files to the top level directory `src`
inside the repo, to enable some differentiation between source code
and other repo artifacts/files.

A few changes are included to refactor the codebase

- update cmake file references
- rename justfile to Justfile
- fix componentize.sh references in cmake & elsewhere
- wpt builtin location

Signed-off-by: Victor Adossi <[email protected]>
  • Loading branch information
vados-cosmonic committed Jan 9, 2025
1 parent fb85134 commit 74bab02
Show file tree
Hide file tree
Showing 139 changed files with 71 additions and 66 deletions.
31 changes: 17 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ if (DEFINED ENV{HOST_API})
if (EXISTS ${HOST_API})
cmake_path(ABSOLUTE_PATH HOST_API)
else ()
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/host-apis/$ENV{HOST_API})
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/src/host-apis/$ENV{HOST_API})
endif()
if (NOT EXISTS ${HOST_API})
message(FATAL_ERROR "Host API `$ENV{HOST_API}` not found. The HOST_API environment \
variable must be the name of a host API implementation provided in the `host-apis` \
variable must be the name of a host API implementation provided in the `src/host-apis` \
folder of StarlingMonkey, or be an absolute path pointing to custom host API implementation.")
endif()
else()
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/host-apis/wasi-0.2.0)
set(HOST_API ${CMAKE_CURRENT_SOURCE_DIR}/src/host-apis/wasi-0.2.0)
endif()
message(STATUS "Using host API: ${HOST_API}")

Expand All @@ -37,9 +37,9 @@ include("openssl")
include("${HOST_API}/host_api.cmake")
include("build-crates")

add_library(extension_api INTERFACE include/extension-api.h runtime/encode.h runtime/decode.h)
add_library(extension_api INTERFACE src/include/extension-api.h src/runtime/encode.h src/runtime/decode.h)
target_link_libraries(extension_api INTERFACE rust-url spidermonkey)
target_include_directories(extension_api INTERFACE include deps/include runtime)
target_include_directories(extension_api INTERFACE src/include deps/include src/runtime)

include("builtins")

Expand All @@ -49,14 +49,14 @@ if (ENABLE_WPT)
endif()

add_executable(starling-raw.wasm
runtime/js.cpp
runtime/allocator.cpp
runtime/encode.cpp
runtime/decode.cpp
runtime/engine.cpp
runtime/event_loop.cpp
runtime/builtin.cpp
runtime/script_loader.cpp
src/runtime/js.cpp
src/runtime/allocator.cpp
src/runtime/encode.cpp
src/runtime/decode.cpp
src/runtime/engine.cpp
src/runtime/event_loop.cpp
src/runtime/builtin.cpp
src/runtime/script_loader.cpp
)

option(USE_WASM_OPT "use wasm-opt to optimize the StarlingMonkey binary" ON)
Expand Down Expand Up @@ -112,10 +112,13 @@ endif()

set(RUNTIME_FILE "starling-raw.wasm")
set(ADAPTER_FILE "preview1-adapter.wasm")
configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)

configure_file("scripts/componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)

if(EXISTS ${ADAPTER})
configure_file(${ADAPTER} "${CMAKE_CURRENT_BINARY_DIR}/${ADAPTER_FILE}" COPYONLY)
endif()

configure_file(spin.toml spin.toml COPYONLY)

function(componentize OUTPUT)
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug

3. Build the runtime

Building the runtime is done in two phases: first, cmake is used to build a raw version as a WebAssembly core module. Then, that module is turned into a [WebAssembly Component](https://component-model.bytecodealliance.org/) using the `componentize.sh` script.
Building the runtime is done in two phases: first, cmake is used to build a raw version as a WebAssembly core module. Then, that module is turned into a [WebAssembly Component](https://component-model.bytecodealliance.org/) using `scripts/componentize.sh`.

The following command will build the `starling-raw.wasm` runtime module in the `cmake-build-release` directory:
```bash
Expand All @@ -71,7 +71,7 @@ Then, the `starling-raw.wasm` module can be turned into a component with the fol

```bash
cd cmake-build-release
./componentize.sh -o starling.wasm
./scripts/componentize.sh -o starling.wasm
```

The resulting runtime can be used to load and evaluate JS code dynamically:
Expand All @@ -86,7 +86,7 @@ Alternatively, a JS file can be provided during componentization:

```bash
cd cmake-build-release
./componentize.sh index.js -o starling.wasm
./scripts/componentize.sh index.js -o starling.wasm
```

This way, the JS file will be loaded during componentization, and the top-level code will be executed, and can e.g. register a handler for the `fetch` event to serve HTTP requests.
Expand Down Expand Up @@ -115,13 +115,13 @@ Then visit http://0.0.0.0:8080/timers, or any test name and filter of the form `

5. Using the runtime with other JS applications

The build directory contains a shell script `componentize.sh` that can be used to create components from JS applications. `componentize.sh` takes a single argument, the path to the JS application, and creates a component with a name of the form `[input-file-name].wasm` in the current working directory.
The build directory contains a shell script `scripts/componentize.sh` that can be used to create components from JS applications. `componentize.sh` takes a single argument, the path to the JS application, and creates a component with a name of the form `[input-file-name].wasm` in the current working directory.

For example, the following command is equivalent to the `cmake` invocation from step 5, and will create the component `cmake-build-release/smoke.wasm`:

```bash
cd cmake-build-release
./componentize.sh ../tests/smoke.js
./scripts/componentize.sh ../tests/smoke.js
```

### Web Platform Tests
Expand Down
3 changes: 2 additions & 1 deletion cmake/add_builtin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function(add_builtin)
list(GET ARGN 0 SRC)
cmake_path(GET SRC STEM NAME)
cmake_path(GET SRC PARENT_PATH DIR)
string(REPLACE "/" "::" NS ${DIR})
string(REPLACE "src/" "" NS ${DIR})
string(REPLACE "/" "::" NS ${NS})
set(NS ${NS}::${NAME})
set(DEFAULT_ENABLE ON)
else()
Expand Down
81 changes: 41 additions & 40 deletions cmake/builtins.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(CMAKE_SCRIPT_MODE_FILE)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
else()
add_library(builtins STATIC builtins/install_builtins.cpp)
add_library(builtins STATIC src/builtins/install_builtins.cpp)
target_include_directories(builtins PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(builtins PRIVATE extension_api)
endif()
Expand All @@ -13,86 +13,87 @@ file(WRITE ${INSTALL_BUILTINS} "// This file is generated by CMake\n")


# These builtins are always enabled.
add_builtin(builtins/web/global_self.cpp)
add_builtin(builtins/web/queue-microtask.cpp)
add_builtin(builtins/web/structured-clone.cpp)
add_builtin(builtins/web/base64.cpp)
add_builtin(builtins/web/blob.cpp)
add_builtin(src/builtins/web/global_self.cpp)
add_builtin(src/builtins/web/queue-microtask.cpp)
add_builtin(src/builtins/web/structured-clone.cpp)
add_builtin(src/builtins/web/base64.cpp)
add_builtin(src/builtins/web/blob.cpp)

add_builtin(
builtins::web::dom_exception
SRC
builtins/web/dom-exception.cpp
src/builtins/web/dom-exception.cpp
INCLUDE_DIRS
runtime)
src/runtime)

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

add_builtin(builtins/web/console.cpp)
add_builtin(src/builtins/web/console.cpp)

add_builtin(builtins/web/performance.cpp)
add_builtin(src/builtins/web/performance.cpp)

add_builtin(
builtins::web::timers
SRC
builtins/web/timers.cpp
src/builtins/web/timers.cpp
INCLUDE_DIRS
runtime)
src/runtime)

add_builtin(builtins/web/worker-location.cpp)
add_builtin(src/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
src/builtins/web/text-codec/text-codec.cpp
src/builtins/web/text-codec/text-decoder.cpp
src/builtins/web/text-codec/text-encoder.cpp
INCLUDE_DIRS
runtime)
src/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
src/builtins/web/streams/compression-stream.cpp
src/builtins/web/streams/decompression-stream.cpp
src/builtins/web/streams/native-stream-sink.cpp
src/builtins/web/streams/native-stream-source.cpp
src/builtins/web/streams/streams.cpp
src/builtins/web/streams/transform-stream.cpp
src/builtins/web/streams/transform-stream-default-controller.cpp
INCLUDE_DIRS
runtime)
src/runtime)

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

add_builtin(
builtins::web::fetch::fetch_event
SRC
builtins/web/fetch/fetch_event.cpp
src/builtins/web/fetch/fetch_event.cpp
DEPENDENCIES
host_api)
host_api)

add_builtin(
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
src/builtins/web/crypto/crypto.cpp
src/builtins/web/crypto/crypto-algorithm.cpp
src/builtins/web/crypto/crypto-key.cpp
src/builtins/web/crypto/crypto-key-ec-components.cpp
src/builtins/web/crypto/crypto-key-rsa-components.cpp
src/builtins/web/crypto/json-web-key.cpp
src/builtins/web/crypto/subtle-crypto.cpp
DEPENDENCIES
OpenSSL::Crypto
fmt
INCLUDE_DIRS
runtime)
src/runtime)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ add_library(host_api STATIC
${HOST_API}/host_call.cpp
${HOST_API}/bindings/bindings.c
${HOST_API}/bindings/bindings_component_type.o
${CMAKE_CURRENT_SOURCE_DIR}/include/host_api.h
${CMAKE_CURRENT_SOURCE_DIR}/src/include/host_api.h
)

target_link_libraries(host_api PRIVATE spidermonkey)
target_include_directories(host_api PRIVATE include)
target_include_directories(host_api PRIVATE src/include)
target_include_directories(host_api PRIVATE ${HOST_API})
target_include_directories(host_api PUBLIC ${HOST_API}/include)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/host_api.h → src/include/host_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <variant>
#include <vector>

#include "../crates/rust-url/rust-url.h"
#include "../../crates/rust-url/rust-url.h"
#include "extension-api.h"
#include "js/TypeDecls.h"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/wpt-harness/build-wpt-runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ inputs=(
)

cat "${inputs[@]}" > wpt-test-runner.js
./componentize.sh $componentize_flags --verbose --legacy-script wpt-test-runner.js wpt-runtime.wasm
./scripts/componentize.sh $componentize_flags --verbose --legacy-script wpt-test-runner.js wpt-runtime.wasm
4 changes: 2 additions & 2 deletions tests/wpt-harness/wpt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ endif()

add_builtin(wpt_support
SRC "${CMAKE_CURRENT_LIST_DIR}/wpt_builtins.cpp"
INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/builtins/web/")
INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src/builtins/web/")

add_custom_command(
OUTPUT wpt-runtime.wasm
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E env PATH=${WASM_TOOLS_DIR}:${WIZER_DIR}:$ENV{PATH} env "COMPONENTIZE_FLAGS=${COMPONENTIZE_FLAGS}" WPT_ROOT=${WPT_ROOT} ${CMAKE_CURRENT_SOURCE_DIR}/tests/wpt-harness/build-wpt-runtime.sh
DEPENDS starling-raw.wasm componentize.sh tests/wpt-harness/build-wpt-runtime.sh tests/wpt-harness/pre-harness.js tests/wpt-harness/post-harness.js
DEPENDS starling-raw.wasm scripts/componentize.sh tests/wpt-harness/build-wpt-runtime.sh tests/wpt-harness/pre-harness.js tests/wpt-harness/post-harness.js
VERBATIM
)

Expand Down

0 comments on commit 74bab02

Please sign in to comment.