Skip to content

Commit 6e79476

Browse files
authored
Fixed the build for web using CMake. (raysan5#1452)
* Fixed the build for web using CMake. I found that the build for me was failing and I added some if defined checks in the core.c file where the glfwSetWindowAttrib was used. (error: implicit declaration of function 'glfwSetWindowAttrib' is invalid in C99 [-Werror,-Wimplicit-function-declaration]) I also changed some values in the toolchain file so that it correctly uses the .bat files when on windows. * Cleaned up the additional variables (they are not important) * Added more improvements to cmakelists Added the option to use the system provided Emscripten toolchain to be more uniform with other libraries. Fixed and issue which prevented example being built from cmake and also building with html extensions properly. * Fixed ENUM to STRING because of a missed warning
1 parent 11ebb54 commit 6e79476

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ option(ENABLE_ASAN "Enable AddressSanitizer (ASAN) for debugging (degrades perf
77
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan) for debugging" OFF)
88
option(ENABLE_MSAN "Enable MemorySanitizer (MSan) for debugging (not recommended to run with ASAN)" OFF)
99

10+
# This helps support the case where emsdk toolchain file is used
11+
# either by setting it with -DCMAKE_TOOLCHAIN_FILE=<path_to_emsdk>/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
12+
# or by using "emcmake cmake -B build -S ." as described in https://emscripten.org/docs/compiling/Building-Projects.html
13+
if(EMSCRIPTEN)
14+
SET(PLATFORM Web CACHE STRING "Forcing PLATFORM_WEB because EMSCRIPTEN was detected")
15+
endif()
16+
1017
if(CMAKE_VERSION VERSION_LESS "3.1")
1118
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
1219
set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")

cmake/emscripten.cmake

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
SET(CMAKE_SYSTEM_NAME Linux)
2+
SET(CMAKE_SYSTEM_PROCESSOR x86)
3+
4+
if (CMAKE_HOST_WIN32)
5+
SET(EMSCRIPTEN_EXTENSION ".bat")
6+
else ()
7+
SET(EMSCRIPTEN_EXTENSION "")
8+
endif()
9+
10+
SET(CMAKE_C_COMPILER emcc${EMSCRIPTEN_EXTENSION})
11+
SET(CMAKE_CXX_COMPILER em++${EMSCRIPTEN_EXTENSION})
212

3-
SET(CMAKE_C_COMPILER emcc)
4-
SET(CMAKE_CXX_COMPILER em++)
513
if(NOT DEFINED CMAKE_AR)
6-
find_program(CMAKE_AR NAMES emar)
14+
find_program(CMAKE_AR NAMES emar${EMSCRIPTEN_EXTENSION})
715
endif()
816
if(NOT DEFINED CMAKE_RANLIB)
9-
find_program(CMAKE_RANLIB NAMES emranlib)
17+
find_program(CMAKE_RANLIB NAMES emranlib${EMSCRIPTEN_EXTENSION})
1018
endif()
1119

1220
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

examples/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ if (APPLE AND NOT CMAKE_SYSTEM STRLESS "Darwin-18.0.0")
2828
add_definitions(-DGL_SILENCE_DEPRECATION)
2929
MESSAGE(AUTHOR_WARNING "OpenGL is deprecated starting with macOS 10.14 (Mojave)!")
3030
endif()
31-
set(OUTPUT_EXT)
3231
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/rlgl_standalone.c)
3332

3433
include(CheckIncludeFile)
@@ -85,12 +84,12 @@ elseif(${PLATFORM} MATCHES "Web")
8584
# Since WASM is used, ALLOW_MEMORY_GROWTH has no extra overheads
8685
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s ALLOW_MEMORY_GROWTH=1 --no-heap-copy")
8786
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html")
88-
set(OUTPUT_EXT ".html")
87+
set(CMAKE_EXECUTABLE_SUFFIX ".html")
8988

9089
# Remove the -rdynamic flag because otherwise emscripten
9190
# does not generate HTML+JS+WASM files, only a non-working
9291
# and fat HTML
93-
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
92+
string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS}")
9493
endif()
9594

9695
include_directories(BEFORE SYSTEM others/external/include)
@@ -103,7 +102,7 @@ endif()
103102
foreach(example_source ${example_sources})
104103
# Create the basename for the example
105104
get_filename_component(example_name ${example_source} NAME)
106-
string(REPLACE ".c" "${OUTPUT_EXT}" example_name ${example_name})
105+
string(REPLACE ".c" "" example_name ${example_name})
107106

108107
# Setup the example
109108
add_executable(${example_name} ${example_source})
@@ -125,7 +124,7 @@ if (${PLATFORM} MATCHES "Desktop")
125124
foreach (example_source "others/rlgl_standalone.c")
126125
# Create the basename for the example
127126
get_filename_component(example_name ${example_source} NAME)
128-
string(REPLACE ".c" "${OUTPUT_EXT}" example_name ${example_name})
127+
string(REPLACE ".c" "" example_name ${example_name})
129128
add_executable(${example_name} ${example_source})
130129
add_dependencies(${example_name} raylib)
131130
target_link_libraries(${example_name} ${raylib_LDFLAGS})

src/core.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,15 +1133,19 @@ void SetWindowState(unsigned int flags)
11331133
// State change: FLAG_WINDOW_RESIZABLE
11341134
if (((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) != (flags & FLAG_WINDOW_RESIZABLE)) && ((flags & FLAG_WINDOW_RESIZABLE) > 0))
11351135
{
1136+
#if defined(PLATFORM_DESKTOP)
11361137
glfwSetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE, GLFW_TRUE);
11371138
CORE.Window.flags |= FLAG_WINDOW_RESIZABLE;
1139+
#endif
11381140
}
11391141

11401142
// State change: FLAG_WINDOW_UNDECORATED
11411143
if (((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) != (flags & FLAG_WINDOW_UNDECORATED)) && (flags & FLAG_WINDOW_UNDECORATED))
11421144
{
1145+
#if defined(PLATFORM_DESKTOP)
11431146
glfwSetWindowAttrib(CORE.Window.handle, GLFW_DECORATED, GLFW_FALSE);
11441147
CORE.Window.flags |= FLAG_WINDOW_UNDECORATED;
1148+
#endif
11451149
}
11461150

11471151
// State change: FLAG_WINDOW_HIDDEN
@@ -1168,15 +1172,19 @@ void SetWindowState(unsigned int flags)
11681172
// State change: FLAG_WINDOW_UNFOCUSED
11691173
if (((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) != (flags & FLAG_WINDOW_UNFOCUSED)) && ((flags & FLAG_WINDOW_UNFOCUSED) > 0))
11701174
{
1175+
#if defined(PLATFORM_DESKTOP)
11711176
glfwSetWindowAttrib(CORE.Window.handle, GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
11721177
CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED;
1178+
#endif
11731179
}
11741180

11751181
// State change: FLAG_WINDOW_TOPMOST
11761182
if (((CORE.Window.flags & FLAG_WINDOW_TOPMOST) != (flags & FLAG_WINDOW_TOPMOST)) && ((flags & FLAG_WINDOW_TOPMOST) > 0))
11771183
{
1184+
#if defined(PLATFORM_DESKTOP)
11781185
glfwSetWindowAttrib(CORE.Window.handle, GLFW_FLOATING, GLFW_TRUE);
11791186
CORE.Window.flags |= FLAG_WINDOW_TOPMOST;
1187+
#endif
11801188
}
11811189

11821190
// State change: FLAG_WINDOW_ALWAYS_RUN
@@ -1234,15 +1242,19 @@ void ClearWindowState(unsigned int flags)
12341242
// State change: FLAG_WINDOW_RESIZABLE
12351243
if (((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) > 0) && ((flags & FLAG_WINDOW_RESIZABLE) > 0))
12361244
{
1245+
#if defined(PLATFORM_DESKTOP)
12371246
glfwSetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE, GLFW_FALSE);
12381247
CORE.Window.flags &= ~FLAG_WINDOW_RESIZABLE;
1248+
#endif
12391249
}
12401250

12411251
// State change: FLAG_WINDOW_UNDECORATED
12421252
if (((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) > 0) && ((flags & FLAG_WINDOW_UNDECORATED) > 0))
12431253
{
1254+
#if defined(PLATFORM_DESKTOP)
12441255
glfwSetWindowAttrib(CORE.Window.handle, GLFW_DECORATED, GLFW_TRUE);
12451256
CORE.Window.flags &= ~FLAG_WINDOW_UNDECORATED;
1257+
#endif
12461258
}
12471259

12481260
// State change: FLAG_WINDOW_HIDDEN
@@ -1267,15 +1279,19 @@ void ClearWindowState(unsigned int flags)
12671279
// State change: FLAG_WINDOW_UNFOCUSED
12681280
if (((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) > 0) && ((flags & FLAG_WINDOW_UNFOCUSED) > 0))
12691281
{
1282+
#if defined(PLATFORM_DESKTOP)
12701283
glfwSetWindowAttrib(CORE.Window.handle, GLFW_FOCUS_ON_SHOW, GLFW_TRUE);
12711284
CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED;
1285+
#endif
12721286
}
12731287

12741288
// State change: FLAG_WINDOW_TOPMOST
12751289
if (((CORE.Window.flags & FLAG_WINDOW_TOPMOST) > 0) && ((flags & FLAG_WINDOW_TOPMOST) > 0))
12761290
{
1291+
#if defined(PLATFORM_DESKTOP)
12771292
glfwSetWindowAttrib(CORE.Window.handle, GLFW_FLOATING, GLFW_FALSE);
12781293
CORE.Window.flags &= ~FLAG_WINDOW_TOPMOST;
1294+
#endif
12791295
}
12801296

12811297
// State change: FLAG_WINDOW_ALWAYS_RUN

0 commit comments

Comments
 (0)