Skip to content

Instantly share code, notes, and snippets.

@meowstr
Created May 30, 2024 18:59
Show Gist options
  • Save meowstr/c52f03310822153a8a03aa9844b5d277 to your computer and use it in GitHub Desktop.
Save meowstr/c52f03310822153a8a03aa9844b5d277 to your computer and use it in GitHub Desktop.
crossplatform glfw/openal/opengl setup (windows, linux, web)
cmake_minimum_required( VERSION 3.28 )
project( newproject )
set( COMMON_SOURCES
# includes
# ...
# sources
# ...
src/main.cpp
)
# common libs
add_library( cglm INTERFACE )
target_include_directories( cglm INTERFACE libs/cglm/include )
add_library( stb INTERFACE )
target_include_directories( stb INTERFACE libs/stb/include )
add_library( glad libs/glad/src/glad.c )
target_include_directories( glad PUBLIC libs/glad/include )
#
# linux build
#
if ( (NOT DEFINED EMSCRIPTEN) AND UNIX )
# pull libraries from the system
find_package( PkgConfig REQUIRED )
pkg_check_modules( GLFW REQUIRED IMPORTED_TARGET glfw3 )
pkg_check_modules( OPENAL REQUIRED IMPORTED_TARGET openal )
add_executable( app ${COMMON_SOURCES} src/platform/desktop.cpp )
target_link_libraries( app PRIVATE glad cglm stb PkgConfig::GLFW PkgConfig::OPENAL )
add_custom_target( run COMMAND app DEPENDS app WORKING_DIRECTORY ${CMAKE_PROJECT_DIR} )
add_executable( bake tools/bake.c )
endif()
#
# windows build
#
if ( (NOT DEFINED EMSCRIPTEN) AND WIN32 )
# pull prebuilt libraries
add_library( glfw STATIC IMPORTED )
set_target_properties(glfw PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glfw/lib-vc2019/glfw3.lib)
target_include_directories(glfw INTERFACE libs/glfw/include)
add_library( openal SHARED IMPORTED )
set_target_properties(openal PROPERTIES IMPORTED_IMPLIB ${PROJECT_SOURCE_DIR}/libs/openal/lib/OpenAL32.lib)
target_include_directories(openal INTERFACE libs/openal/include)
add_executable( app ${COMMON_SOURCES} src/platform/desktop.cpp )
set_target_properties(app PROPERTIES WIN32_EXECUTABLE $<CONFIG:Release>)
target_link_libraries( app PRIVATE glad cglm stb glfw openal )
add_executable( bake tools/bake.c )
endif()
#
# emscripten build
#
if ( DEFINED EMSCRIPTEN )
add_executable( app ${COMMON_SOURCES} src/platform/web.cpp )
target_link_libraries( app PRIVATE cglm stb )
set_target_properties( app PROPERTIES LINK_FLAGS "-s USE_GLFW=3 --shell-file ${PROJECT_SOURCE_DIR}/shell.html" )
set(CMAKE_EXECUTABLE_SUFFIX ".html")
endif()
# common build flags
target_include_directories( app PRIVATE src )
target_compile_features( app PRIVATE cxx_std_20 )
target_compile_definitions( app PRIVATE "RELEASE=$<CONFIG:Release>" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment