Skip to content

Commit 375ae9e

Browse files
committed
Emscripten 3.1.73 update. CSP fixes. Working on htaccess stuff at the moment. Split template for style and script CSP requirements
1 parent 0e33e6b commit 375ae9e

File tree

7 files changed

+342
-283
lines changed

7 files changed

+342
-283
lines changed

libs/openFrameworksCompiled/project/emscripten/.htaccess

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
<IfModule mod_headers.c>
2-
Header set Cross-Origin-Embedder-Policy "require-corp"
3-
Header set Cross-Origin-Opener-Policy "same-origin"
2+
# Header set Cross-Origin-Embedder-Policy "require-corp"
3+
# Header set Cross-Origin-Opener-Policy "same-origin"
4+
Header unset Cross-Origin-Embedder-Policy
5+
Header unset Cross-Origin-Opener-Policy
6+
Header set Access-Control-Allow-Origin "*"
7+
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE"
8+
Header set Access-Control-Allow-Headers "Content-Type, Authorization"
9+
Header set Content-Security-Policy "script-src 'self' 'unsafe-inline';"
10+
Header set Content-Security-Policy "default-src 'self' 'unsafe-inline';"
11+
Header set Content-Security-Policy "img-src 'self' 'unsafe-inline';"
12+
Header set Content-Security-Policy "script-src 'self' 'unsafe-eval';"
13+
Header set Content-Security-Policy "default-src 'self' 'unsafe-eval';"
14+
#https://web.dev/articles/csp?utm_source=devtools#eval_too
15+
#https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
416
</IfModule>
517

618
# apache setting for WebAssembly COOP Requirement
@@ -13,4 +25,4 @@ AddEncoding x-gzip .gz
1325
AddType application/wasm .wasm
1426
AddOutputFilterByType DEFLATE application/wasm
1527

16-
# check content type of WASM using cURL: curl -I https://*.com/index.wasm | grep "content-type"
28+
# check content type of WASM using cURL: curl -I https://*.com/index.wasm | grep "content-type"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
document.addEventListener('DOMContentLoaded', () => {
3+
const canvas = document.getElementById('canvas');
4+
if (!canvas) {
5+
console.error("Canvas element not found!");
6+
return;
7+
}
8+
canvas.addEventListener('contextmenu', (event) => {
9+
event.preventDefault();
10+
});
11+
12+
function goEmscriptenFullscreen(resize) {
13+
Module.requestFullscreen(0, resize);
14+
}
15+
16+
function tryFullScreen(aspect, resize) {
17+
var canvas = document.getElementById("canvas");
18+
if (resize) {
19+
canvas.width = screen.width;
20+
canvas.height = screen.height;
21+
}
22+
if (canvas.requestFullScreen) {
23+
if (aspect) goEmscriptenFullscreen(resize);
24+
else canvas.requestFullScreen();
25+
} else if (canvas.webkitRequestFullScreen) {
26+
if (aspect) goEmscriptenFullscreen(resize);
27+
else canvas.webkitRequestFullScreen();
28+
} else if (canvas.mozRequestFullScreen) {
29+
if (aspect) goEmscriptenFullscreen(resize);
30+
else canvas.mozRequestFullScreen();
31+
} else {
32+
canvas.width = window.innerWidth;
33+
canvas.height = window.innerHeight;
34+
document.getElementById("header").style.display = "none";
35+
document.getElementById("output").style.display = "none";
36+
}
37+
}
38+
39+
var statusElement = document.getElementById("status");
40+
var progressElement = document.getElementById("progress");
41+
var spinnerElement = document.getElementById("spinner");
42+
43+
var Module = {
44+
print: (function () {
45+
var element = document.getElementById("output");
46+
if (element) element.value = ""; // clear browser cache
47+
return (...args) => {
48+
var text = args.join(" ");
49+
console.log(text);
50+
if (element) {
51+
element.value += text + "\n";
52+
element.scrollTop = element.scrollHeight; // focus on bottom
53+
}
54+
};
55+
})(),
56+
canvas: (() => {
57+
var canvas = document.getElementById("canvas");
58+
59+
canvas.addEventListener(
60+
"webglcontextlost",
61+
(e) => {
62+
alert("WebGL context lost. You will need to reload the page.");
63+
e.preventDefault();
64+
},
65+
false
66+
);
67+
68+
return canvas;
69+
})(),
70+
setStatus: (text) => {
71+
Module.setStatus.last ??= { time: Date.now(), text: "" };
72+
if (text === Module.setStatus.last.text) return;
73+
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
74+
var now = Date.now();
75+
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
76+
Module.setStatus.last.time = now;
77+
Module.setStatus.last.text = text;
78+
if (m) {
79+
text = m[1];
80+
progressElement.value = parseInt(m[2]) * 100;
81+
progressElement.max = parseInt(m[4]) * 100;
82+
progressElement.hidden = false;
83+
spinnerElement.hidden = false;
84+
} else {
85+
progressElement.value = null;
86+
progressElement.max = null;
87+
progressElement.hidden = true;
88+
if (!text) spinnerElement.style.display = "none";
89+
}
90+
statusElement.innerHTML = text;
91+
},
92+
totalDependencies: 0,
93+
monitorRunDependencies: (left) => {
94+
this.totalDependencies = Math.max(this.totalDependencies, left);
95+
Module.setStatus(
96+
left
97+
? `Preparing... (${this.totalDependencies - left}/${this.totalDependencies})`
98+
: "All downloads complete."
99+
);
100+
},
101+
};
102+
103+
Module.setStatus("Downloading...");
104+
window.onerror = () => {
105+
Module.setStatus("Exception thrown, see JavaScript console");
106+
spinnerElement.style.display = "none";
107+
Module.setStatus = (text) => {
108+
if (text) console.error(`[post-exception status] ${text}`);
109+
};
110+
};
111+
});
112+
113+
114+

libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk

+48-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
PLATFORM_PROJECT_RELEASE_TARGET = bin/em/$(BIN_NAME)/index.html
1818
PLATFORM_PROJECT_DEBUG_TARGET = bin/em/$(BIN_NAME)/index.html
19+
OUTPUT_DIR_RELEASE = $(dir $(PLATFORM_PROJECT_RELEASE_TARGET))
20+
OUTPUT_DIR_DEBUG = $(dir $(PLATFORM_PROJECT_DEBUG_TARGET))
1921
BYTECODECORE=1
2022
PLATFORM_CORELIB_DEBUG_TARGET = $(OF_CORE_LIB_PATH)/libopenFrameworksDebug.o
2123
PLATFORM_CORELIB_RELEASE_TARGET = $(OF_CORE_LIB_PATH)/libopenFrameworks.o
@@ -65,13 +67,15 @@ PLATFORM_REQUIRED_ADDONS = ofxEmscripten
6567

6668
ifdef EMSCRIPTEN_PTHREADS
6769
PLATFORM_PTHREAD = -s USE_PTHREADS=1
70+
CFLAG_PLATFORM_PTHREAD = -pthread -matomics -mbulk-memory
6871
else
6972
PLATFORM_PTHREAD = -s USE_PTHREADS=0
73+
CFLAG_PLATFORM_PTHREAD = -matomics -mbulk-memory
7074
endif
7175

7276
# Code Generation Option Flags (http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html)
73-
PLATFORM_CFLAGS = -std=c17 -fPIC $(PLATFORM_PTHREAD)
74-
PLATFORM_CXXFLAGS = -Wall -std=c++17 -fPIC -Wno-warn-absolute-paths $(PLATFORM_PTHREAD)
77+
PLATFORM_CFLAGS = -std=c17 -fPIC $(CFLAG_PLATFORM_PTHREAD)
78+
PLATFORM_CXXFLAGS = -Wall -std=c++17 -fPIC -Wno-warn-absolute-paths $(CFLAG_PLATFORM_PTHREAD)
7579

7680
################################################################################
7781
# PLATFORM LDFLAGS
@@ -99,25 +103,41 @@ ifdef USE_CCACHE
99103
endif
100104
endif
101105

102-
PLATFORM_LDFLAGS = --preload-file bin/data@data --emrun --bind --profiling-funcs -s USE_WEBGPU=1 -s NO_EXIT_RUNTIME=1
103-
PLATFORM_LDFLAGS += -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION=1 -s FULL_ES2 -s FULL_ES3=1
104-
PLATFORM_LDFLAGS += -s AUTO_NATIVE_LIBRARIES=1 -s AUTO_JS_LIBRARIES=1 -s EVAL_CTORS=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0
106+
PLATFORM_LDFLAGS += -s EMBIND_AOT=1
107+
PLATFORM_LDFLAGS = --preload-file bin/data@data --emrun --bind --profiling-funcs
108+
PLATFORM_LDFLAGS += -s USE_WEBGPU=1 -s NO_EXIT_RUNTIME=1
109+
PLATFORM_LDFLAGS += -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION=1 -s FULL_ES2
110+
PLATFORM_LDFLAGS += -s AUTO_NATIVE_LIBRARIES=1 -s AUTO_JS_LIBRARIES=1
111+
PLATFORM_LDFLAGS += -s EVAL_CTORS=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0
105112
PLATFORM_LDFLAGS += $(PLATFORM_PTHREAD)
106-
PLATFORM_LDFLAGS += -lGL -lhtml5
113+
PLATFORM_LDFLAGS += -lGL
114+
PLATFORM_LDFLAGS += -lhtml5
115+
PLATFORM_LDFLAGS += -s MINIFY_HTML=0
116+
PLATFORM_LDFLAGS += -s DYNAMIC_EXECUTION=0 -s NO_DYNAMIC_EXECUTION=1
117+
# PLATFORM_LDFLAGS += -s SINGLE_FILE=1
118+
PLATFORM_LDFLAGS += -s MODULARIZE=1
119+
107120
# PLATFORM_LDFLAGS += -s WASM_WORKERS=1 -s ENVIRONMENT="web,worker"
108121
# PLATFORM_LDFLAGS += -s USE_GLFW=3 -lglfw
109-
PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5video/lib/emscripten/library_html5video.js
110-
PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js
122+
# PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5video/lib/emscripten/library_html5video.js
123+
# PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js
111124

112125
ifdef PROJECT_EMSCRIPTEN_TEMPLATE
113-
PLATFORM_LDFLAGS += --shell-file $(PROJECT_EMSCRIPTEN_TEMPLATE)
114-
# else
115-
# PLATFORM_LDFLAGS += --shell-file $(OF_LIBS_PATH)/openFrameworksCompiled/project/emscripten/template.html
126+
PLATFORM_LDFLAGS += --shell-file $(PROJECT_EMSCRIPTEN_TEMPLATE)
127+
else
128+
PLATFORM_LDFLAGS += --shell-file $(OF_LIBS_PATH)/openFrameworksCompiled/project/emscripten/template.html
116129
endif
117130

118-
PLATFORM_OPTIMIZATION_LDFLAGS_RELEASE = -O3 -s TOTAL_MEMORY=$(PLATFORM_EMSCRIPTEN_TOTAL_MEMORY) -s WASM=1 -fPIC
131+
EMSCRIPTEN_JS = $(OF_LIBS_PATH)/openFrameworksCompiled/project/emscripten/app.js
132+
EMSCRIPTEN_CSS = $(OF_LIBS_PATH)/openFrameworksCompiled/project/emscripten/style.css
133+
OUTPUT_DIR = output
134+
135+
119136

120-
PLATFORM_OPTIMIZATION_LDFLAGS_DEBUG = -O1 -g -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=134217728 -s WASM=1 -fPIC -s VERBOSE=1 -s GL_ASSERTIONS=1
137+
138+
PLATFORM_OPTIMIZATION_LDFLAGS_RELEASE = -O3 -s TOTAL_MEMORY=$(PLATFORM_EMSCRIPTEN_TOTAL_MEMORY) -s WASM=1 -fPIC -gsource-map
139+
140+
PLATFORM_OPTIMIZATION_LDFLAGS_DEBUG = -O1 -g -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=134217728 -s WASM=1 -fPIC -s VERBOSE=1 -s GL_ASSERTIONS=1 -gsource-map
121141

122142
################################################################################
123143
# PLATFORM OPTIMIZATION CFLAGS
@@ -296,8 +316,23 @@ PLATFORM_LIBRARY_SEARCH_PATHS =
296316
################################################################################
297317
#PLATFORM_CXX=
298318

319+
BUILD_TYPE ?= release
320+
299321
afterplatform: $(TARGET_NAME)
300322
@echo
323+
@echo "Copying assets based on build type ($(BUILD_TYPE))..."
324+
@if [ "$(BUILD_TYPE)" = "debug" ]; then \
325+
echo "Copying app.js and style.css to debug directory..."; \
326+
mkdir -p bin/em/$(BIN_NAME)/debug; \
327+
cp $(EMSCRIPTEN_JS) $(OUTPUT_DIR_DEBUG) || echo "Failed to copy app.js"; \
328+
cp $(EMSCRIPTEN_CSS) $(OUTPUT_DIR_DEBUG) || echo "Failed to copy style.css"; \
329+
else \
330+
echo "Copying app.js and style.css to release directory..."; \
331+
mkdir -p bin/em/$(BIN_NAME)/release; \
332+
cp $(EMSCRIPTEN_JS) $(OUTPUT_DIR_RELEASE) || echo "Failed to copy app.js"; \
333+
cp $(EMSCRIPTEN_CSS) $(OUTPUT_DIR_RELEASE) || echo "Failed to copy style.css"; \
334+
fi
335+
@echo "Assets copied successfully."
301336
@echo " compiling done"
302337
@echo " to launch the application on the default browser, run:"
303338
@echo

0 commit comments

Comments
 (0)