Skip to content

Commit

Permalink
Merge pull request #267 from sliemeobn/embedded/build-flags
Browse files Browse the repository at this point in the history
improved embedded support for building with SwiftPM
  • Loading branch information
kateinoigakukun authored Oct 31, 2024
2 parents 43607e5 + dcdb1c5 commit 64e2e11
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
7 changes: 3 additions & 4 deletions Examples/Embedded/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ let package = Package(
"JavaScriptKit",
.product(name: "dlmalloc", package: "swift-dlmalloc")
],
cSettings: [
.unsafeFlags(["-fdeclspec"])
],
cSettings: [.unsafeFlags(["-fdeclspec"])],
swiftSettings: [
.enableExperimentalFeature("Embedded"),
.enableExperimentalFeature("Extern"),
Expand All @@ -29,7 +27,8 @@ let package = Package(
linkerSettings: [
.unsafeFlags([
"-Xclang-linker", "-nostdlib",
"-Xlinker", "--no-entry"
"-Xlinker", "--no-entry",
"-Xlinker", "--export-if-defined=__main_argc_argv"
])
]
)
Expand Down
11 changes: 3 additions & 8 deletions Examples/Embedded/build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/bin/bash
package_dir="$(cd "$(dirname "$0")" && pwd)"
JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM=true swift build --package-path "$package_dir" -c release --product EmbeddedApp \
--triple wasm32-unknown-none-wasm \
-Xswiftc -enable-experimental-feature -Xswiftc Embedded \
-Xswiftc -enable-experimental-feature -Xswiftc Extern \
-Xcc -D__Embedded -Xcc -fdeclspec \
-Xlinker --export-if-defined=__main_argc_argv \
-Xlinker --export-if-defined=swjs_call_host_function \
-Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor
JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM=true \
swift build --package-path "$package_dir" --product EmbeddedApp \
-c release --triple wasm32-unknown-none-wasm
15 changes: 10 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// swift-tools-version:5.7
// swift-tools-version:5.8

import PackageDescription
import Foundation

// NOTE: needed for embedded customizations, ideally this will not be necessary at all in the future, or can be replaced with traits
let shouldBuildForEmbedded = ProcessInfo.processInfo.environment["JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM"].flatMap(Bool.init) ?? false
let shouldBuildForEmbedded = Context.environment["JAVASCRIPTKIT_EXPERIMENTAL_EMBEDDED_WASM"].flatMap(Bool.init) ?? false

let package = Package(
name: "JavaScriptKit",
Expand All @@ -19,9 +18,15 @@ let package = Package(
name: "JavaScriptKit",
dependencies: ["_CJavaScriptKit"],
resources: shouldBuildForEmbedded ? [] : [.copy("Runtime")],
cSettings: shouldBuildForEmbedded ? [
.unsafeFlags(["-fdeclspec"])
] : nil,
swiftSettings: shouldBuildForEmbedded
? [.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"])]
: []
? [
.enableExperimentalFeature("Embedded"),
.enableExperimentalFeature("Extern"),
.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"])
] : nil
),
.target(name: "_CJavaScriptKit"),
.target(
Expand Down
8 changes: 5 additions & 3 deletions Sources/_CJavaScriptKit/_CJavaScriptKit.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "_CJavaScriptKit.h"
#if __wasm32__
#if __Embedded
#ifndef __wasi__
#if __has_include("malloc.h")
#include <malloc.h>
#endif
Expand Down Expand Up @@ -31,8 +31,10 @@ void swjs_cleanup_host_function_call(void *argv_buffer) {
free(argv_buffer);
}

#ifndef __Embedded
// cdecls don't work in Embedded, also @_expose(wasm) can be used with Swift >=6.0
// NOTE: This __wasi__ check is a hack for Embedded compatibility (assuming that if __wasi__ is defined, we are not building for Embedded)
// cdecls don't work in Embedded, but @_expose(wasm) can be used with Swift >=6.0
// the previously used `#if __Embedded` did not play well with SwiftPM (defines needed to be on every target up the chain)
#ifdef __wasi__
bool _call_host_function_impl(const JavaScriptHostFuncRef host_func_ref,
const RawJSValue *argv, const int argc,
const JavaScriptObjectRef callback_func);
Expand Down
6 changes: 3 additions & 3 deletions Sources/_CJavaScriptKit/include/_CJavaScriptKit.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef _CJavaScriptKit_h
#define _CJavaScriptKit_h

#if __Embedded
#include <stddef.h>
#else
#if __has_include("stdlib.h")
#include <stdlib.h>
#else
#include <stddef.h>
#endif
#include <stdbool.h>
#include <stdint.h>
Expand Down

0 comments on commit 64e2e11

Please sign in to comment.