Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use emscripten ports (SDL2 and Freetype) with cmake

I'm trying to compile a C++ project using CMake to webassembly. I'm using emscripten and I want to use the emscripten ports for SDL2 and Freetype. Normally, when compiling with emcc, you would use the flags: -USE_SDL=2 and -USE_FREETYPE=1, in order to include these ports. However I don't know how to achieve this using CMake.

This is my CMakeList file:

cmake_minimum_required(VERSION 3.15)
project(project)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

find_package(SDL2 REQUIRED)
find_package(Freetype REQUIRED)
include_directories(${CMAKE_SOURCE_DIR}/include ${SDL2_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS})

add_executable(project src/main.cpp src/glad.c src/Game.cpp src/Block.cpp include/jumpyblock/Block.h)
target_link_libraries(project ${SDL2_LIBRARIES} ${FREETYPE_LIBRARIES})

It compiles and runs successfully using regular cmake.

So far I have tried compiling with emcmake cmake . && make, which gave me an error saying that it couldn't find a package configuration file for SDL2.

And I've tried modifying the CMakeList file to use the emcc flags for the ports:

cmake_minimum_required(VERSION 3.15)
project(project)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -USE_SDL=2 -USE_FREETYPE=1")

include_directories(${CMAKE_SOURCE_DIR}/include ${SDL2_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS})

add_executable(project src/main.cpp src/glad.c src/Game.cpp src/Block.cpp include/jumpyblock/Block.h )
target_link_libraries(project ${SDL2_LIBRARIES} ${FREETYPE_LIBRARIES})

With this cmake file, emcmake cmake . runs successfully, but then make complains that it can't find SDL2/SDL.h.

like image 865
Astrejoe Avatar asked May 04 '20 10:05

Astrejoe


2 Answers

I fixed the issue based on this page, this page, and this page:

cmake_minimum_required(VERSION 3.15)
project(project)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")

if( ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
    set(USE_FLAGS "-s USE_SDL=2 -s USE_FREETYPE=1")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS}")
    set(CMAKE_EXECUTABLE_SUFFIX .html)
else()
    find_package(SDL2 REQUIRED)
    find_package(Freetype REQUIRED)
endif()
include_directories(${CMAKE_SOURCE_DIR}/include ${SDL2_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS})

add_executable(project src/main.cpp src/glad.c src/Game.cpp src/Block.cpp include/jumpyblock/Block.h)
target_link_libraries(project ${SDL2_LIBRARIES} ${FREETYPE_LIBRARIES})

This compiles with emscripten using emcmake cmake . && make and regularly with cmake . && make.

like image 109
Astrejoe Avatar answered Nov 15 '22 03:11

Astrejoe


I tricked emcc into installing zlib port with this:

emcc -s USE_ZLIB=1 $(mktemp)

and emcmake cmake .. started working for me. I bet you should be able to do the same with SDL.

like image 35
futpib Avatar answered Nov 15 '22 05:11

futpib