Skip to content

Commit c35dc2e

Browse files
brccabralpsxdev
authored andcommitted
[build][cmake] Review web compilation system (raysan5#5181)
* [cmake] export automatically raylib definitions and compile/link options * [cmake] pass emscripten options to consumer project * [web] compile for web * [web] canvas width 100%
1 parent 791cf7c commit c35dc2e

File tree

6 files changed

+34
-94
lines changed

6 files changed

+34
-94
lines changed

examples/CMakeLists.txt

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,9 @@ if (${PLATFORM} MATCHES "Android")
9797
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_basic_lighting.c)
9898

9999
elseif (${PLATFORM} MATCHES "Web")
100-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os")
101-
# Since WASM is used, ALLOW_MEMORY_GROWTH has no extra overheads
102-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s WASM=1 -s ASYNCIFY -s ALLOW_MEMORY_GROWTH=1 --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html")
103-
set(CMAKE_EXECUTABLE_SUFFIX ".html")
104-
105-
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/raylib_opengl_interop.c)
106-
107-
# Remove the -rdynamic flag because otherwise emscripten
108-
# does not generate HTML+JS+WASM files, only a non-working
109-
# and fat HTML
110-
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS}")
100+
set(example_sources) # clear example_sources
101+
list(APPEND example_sources others/web_basic_window.c)
102+
list(APPEND example_sources core/core_input_gestures_testbed.c)
111103

112104
elseif ("${PLATFORM}" STREQUAL "DRM")
113105
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/rlgl_standalone.c)
@@ -165,10 +157,34 @@ foreach (example_source ${example_sources})
165157
string(REGEX MATCH ".*/.*/" resources_dir ${example_source})
166158
string(APPEND resources_dir "resources")
167159

168-
if (${PLATFORM} MATCHES "Web" AND EXISTS ${resources_dir})
169-
# The local resources path needs to be mapped to /resources virtual path
170-
string(APPEND resources_dir "@resources")
171-
set_target_properties(${example_name} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}")
160+
if (${PLATFORM} MATCHES "Web")
161+
target_compile_options(${example_name} PRIVATE -Os)
162+
target_link_options(${example_name} PRIVATE
163+
-sALLOW_MEMORY_GROWTH=1
164+
-sEXPORTED_RUNTIME_METHODS=[requestFullscreen]
165+
-sUSE_GLFW=3
166+
--shell-file "${CMAKE_SOURCE_DIR}/src/shell.html"
167+
)
168+
set_target_properties(${example_name} PROPERTIES SUFFIX ".html")
169+
170+
if (EXISTS ${resources_dir})
171+
# The local resources path needs to be mapped to /resources virtual path
172+
string(APPEND resources_dir "@resources")
173+
set_target_properties(${example_name} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}")
174+
endif ()
175+
176+
if(${GRAPHICS} MATCHES "GRAPHICS_API_OPENGL_ES3")
177+
target_link_options(${example_name} PUBLIC "-sMIN_WEBGL_VERSION=2")
178+
target_link_options(${example_name} PUBLIC "-sMAX_WEBGL_VERSION=2")
179+
endif()
180+
181+
# Checks if OSX and links appropriate frameworks (Only required on MacOS)
182+
if (APPLE)
183+
target_link_libraries(${example_name} "-framework IOKit")
184+
target_link_libraries(${example_name} "-framework Cocoa")
185+
target_link_libraries(${example_name} "-framework OpenGL")
186+
endif()
187+
172188
endif ()
173189
endforeach ()
174190

projects/CMake/core_basic_window.c renamed to examples/others/web_basic_window.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************************
22
*
3-
* raylib [core] example - Basic window (adapted for HTML5 platform)
3+
* raylib [others] example - Basic window (adapted for HTML5 platform)
44
*
55
* This example is prepared to compile for PLATFORM_WEB and PLATFORM_DESKTOP
66
* As you will notice, code structure is slightly different to the other examples...
@@ -37,7 +37,7 @@ int main()
3737
{
3838
// Initialization
3939
//--------------------------------------------------------------------------------------
40-
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
40+
InitWindow(screenWidth, screenHeight, "raylib [others] example - web basic window");
4141

4242
#if defined(PLATFORM_WEB)
4343
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);

projects/CMake/CMakeLists.txt

Lines changed: 0 additions & 41 deletions
This file was deleted.

projects/CMake/README.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ else()
6868
)
6969
endif()
7070

71-
if (${PLATFORM} MATCHES "Web")
72-
target_link_options(raylib PUBLIC "-sUSE_GLFW=3" -sEXPORTED_RUNTIME_METHODS=ccall -sASYNCIFY)
73-
if(${GRAPHICS} MATCHES "GRAPHICS_API_OPENGL_ES3")
74-
target_link_options(raylib PUBLIC "-sMIN_WEBGL_VERSION=2")
75-
target_link_options(raylib PUBLIC "-sMAX_WEBGL_VERSION=2")
76-
endif()
77-
endif()
78-
7971
set_target_properties(raylib PROPERTIES
8072
PUBLIC_HEADER "${raylib_public_headers}"
8173
VERSION ${PROJECT_VERSION}

src/shell.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<link rel="shortcut icon" href="https://www.raylib.com/favicon.ico">
3535

3636
<style>
37-
body { font-family: arial; margin: 0; padding: none; }
37+
body { font-family: arial; margin: 0; padding: unset; }
3838

3939
#header {
4040
width: 100%;

0 commit comments

Comments
 (0)