cmake: Fix PLATFORM_WEB build

Did this ever work? Surely, doesn't look like it...
This commit is contained in:
Ahmad Fatoum
2018-04-12 19:31:53 +02:00
parent cea78e4fc1
commit e025e62445
9 changed files with 150 additions and 121 deletions

View File

@@ -1,5 +1,5 @@
# Setup the project and settings
project(raylib)
project(raylib C)
include(GNUInstallDirs)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
@@ -13,14 +13,6 @@ configure_file(config.h.in ${CMAKE_BINARY_DIR}/cmake/config.h)
include_directories(${CMAKE_BINARY_DIR})
include_directories(SYSTEM .)
if(MACOS_FATLIB)
if (CMAKE_OSX_ARCHITECTURES)
message(FATAL_ERROR "User supplied -DCMAKE_OSX_ARCHITECTURES overrides -DMACOS_FATLIB=ON")
else()
SET(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
endif()
endif()
# Get the sources together
file(GLOB raylib_sources *.c)
list(REMOVE_ITEM raylib_sources ${CMAKE_CURRENT_SOURCE_DIR}/rglfw.c)
@@ -72,9 +64,9 @@ if(${PLATFORM} MATCHES "Desktop")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
# Need to force OpenGL 3.3 on OS X
# See: https://github.com/raysan5/raylib/issues/341
if(APPLE)
# Need to force OpenGL 3.3 on OS X
# See: https://github.com/raysan5/raylib/issues/341
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
link_libraries("${LIBS_PRIVATE}")
elseif(WIN32)
@@ -85,12 +77,9 @@ elseif(${PLATFORM} MATCHES "Web")
set(PLATFORM "PLATFORM_WEB")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
# Need to use `emcc`
set(CMAKE_C_COMPILER "emcc")
set(CMAKE_CXX_COMPILER "em++")
set(CMAKE_C_FLAGS "-s USE_GLFW=3 -s ASSERTIONS=1 --profiling")
# Change the name of the output library
set(RAYLIB "libraylib.bc")
elseif(${PLATFORM} MATCHES "Android")
set(PLATFORM "PLATFORM_ANDROID")
@@ -110,89 +99,87 @@ elseif(${PLATFORM} MATCHES "Raspberry Pi")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
# Which platform?
if(${PLATFORM} MATCHES "PLATFORM_WEB")
# For the web.
add_executable(${RAYLIB} ${sources})
if(${SHARED})
add_library(${RAYLIB}_shared SHARED ${sources})
else()
if(${SHARED})
add_library(${RAYLIB}_shared SHARED ${sources})
target_compile_definitions(${RAYLIB}_shared
PUBLIC ${PLATFORM}
PUBLIC ${GRAPHICS}
)
target_compile_definitions(${RAYLIB}_shared
PUBLIC ${PLATFORM}
PUBLIC ${GRAPHICS}
)
set_property(TARGET ${RAYLIB}_shared PROPERTY POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH ON)
set_property(TARGET ${RAYLIB}_shared PROPERTY POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH ON)
target_link_libraries(${RAYLIB}_shared ${LIBS_PRIVATE})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(${RAYLIB}_shared glfw ${GLFW_LIBRARIES})
endif()
if (UNIX AND ${FILESYSTEM_LACKS_SYMLINKS})
MESSAGE(WARNING "Can't version UNIX shared library on file system without symlink support")
else()
set_target_properties(${RAYLIB}_shared PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${API_VERSION}
)
endif()
set_target_properties(${RAYLIB}_shared PROPERTIES
PUBLIC_HEADER "raylib.h"
)
if(WIN32)
install(
TARGETS ${RAYLIB}_shared
RUNTIME DESTINATION lib
PUBLIC_HEADER DESTINATION include
target_link_libraries(${RAYLIB}_shared ${LIBS_PRIVATE})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(${RAYLIB}_shared glfw ${GLFW_LIBRARIES})
endif()
if (UNIX AND ${FILESYSTEM_LACKS_SYMLINKS})
MESSAGE(WARNING "Can't version UNIX shared library on file system without symlink support")
else()
set_target_properties(${RAYLIB}_shared PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${API_VERSION}
)
else() # Keep lib*.(a|dll) name, but avoid *.lib files overwriting each other on Windows
set_target_properties(${RAYLIB}_shared PROPERTIES OUTPUT_NAME ${RAYLIB})
install(
TARGETS ${RAYLIB}_shared
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
endif()
endif(${SHARED})
if(${STATIC})
add_library(${RAYLIB} STATIC ${sources})
target_compile_definitions(${RAYLIB}
PUBLIC ${PLATFORM}
PUBLIC ${GRAPHICS}
endif()
set_target_properties(${RAYLIB}_shared PROPERTIES
PUBLIC_HEADER "raylib.h"
)
if(WIN32)
install(
TARGETS ${RAYLIB}_shared
RUNTIME DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
set(PKG_CONFIG_LIBS_PRIVATE ${__PKG_CONFIG_LIBS_PRIVATE})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(${RAYLIB} glfw ${GLFW_LIBRARIES})
endif()
if (WITH_PIC)
set_property(TARGET ${RAYLIB} PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
set_target_properties(${RAYLIB} PROPERTIES PUBLIC_HEADER "raylib.h")
install(TARGETS ${RAYLIB}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
else() # Keep lib*.(a|dll) name, but avoid *.lib files overwriting each other on Windows
set_target_properties(${RAYLIB}_shared PROPERTIES OUTPUT_NAME ${RAYLIB})
install(
TARGETS ${RAYLIB}_shared
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
endif(${STATIC})
endif()
endif(${SHARED})
configure_file(../raylib.pc.in raylib.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/release/raylib.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
if(${STATIC})
if(${PLATFORM} MATCHES "PLATFORM_WEB")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
endif()
add_library(${RAYLIB} STATIC ${sources})
set(PKG_CONFIG_LIBS_PRIVATE ${__PKG_CONFIG_LIBS_PRIVATE})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(${RAYLIB} glfw ${GLFW_LIBRARIES})
endif()
if (WITH_PIC)
set_property(TARGET ${RAYLIB} PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
set_target_properties(${RAYLIB} PROPERTIES PUBLIC_HEADER "raylib.h")
install(TARGETS ${RAYLIB}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
endif(${STATIC})
configure_file(../raylib.pc.in raylib.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/release/raylib.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
# Copy the header files to the build directory
file(COPY "raylib.h" DESTINATION ".")
file(COPY "rlgl.h" DESTINATION ".")
file(COPY "physac.h" DESTINATION ".")
file(COPY "raymath.h" DESTINATION ".")
file(COPY "audio.h" DESTINATION ".")
target_compile_definitions(${RAYLIB}
PUBLIC ${PLATFORM}
PUBLIC ${GRAPHICS}
)
# Copy the header files to the build directory
file(COPY "raylib.h" DESTINATION ".")
file(COPY "rlgl.h" DESTINATION ".")
file(COPY "physac.h" DESTINATION ".")
file(COPY "raymath.h" DESTINATION ".")
file(COPY "audio.h" DESTINATION ".")
endif()
# Print the flags for the user
message(STATUS "Compiling with the flags:")

View File

@@ -1,13 +1,24 @@
### Config options ###
include(CMakeDependentOption)
set(PLATFORM "Desktop" CACHE STRING "Platform to build for.")
set_property(CACHE PLATFORM PROPERTY STRINGS "Desktop" "Web" "Android" "Raspberry Pi")
set(OPENGL_VERSION "3.3" CACHE STRING "OpenGL Version to build raylib with")
set_property(CACHE OPENGL_VERSION PROPERTY STRINGS "3.3" "2.1" "1.1" "ES 2.0")
# Shared library is always PIC. Static library should be PIC too if linked into a shared library
option(WITH_PIC "Compile static library as position-independent code" OFF)
option(SHARED "Build raylib as a dynamic library" OFF)
option(STATIC "Build raylib as a static library" ON)
option(MACOS_FATLIB "Build fat library for both i386 and x86_64 on macOS" ON)
option(USE_AUDIO "Build raylib with audio module" ON)
cmake_dependent_option(USE_OPENAL_BACKEND "Link raylib with openAL instead of mini-al" OFF "USE_AUDIO" OFF)
if(${PLATFORM} MATCHES "Web")
cmake_dependent_option(USE_OPENAL_BACKEND "Link raylib with openAL instead of mini-al" ON "USE_AUDIO" OFF)
else()
cmake_dependent_option(USE_OPENAL_BACKEND "Link raylib with openAL instead of mini-al" OFF "USE_AUDIO" OFF)
endif()
set(USE_EXTERNAL_GLFW OFF CACHE STRING "Link raylib against system GLFW instead of embedded one")
set_property(CACHE USE_EXTERNAL_GLFW PROPERTY STRINGS ON OFF IF_POSSIBLE)
if(UNIX AND NOT APPLE)
@@ -15,12 +26,6 @@ if(UNIX AND NOT APPLE)
endif()
set(PLATFORM "Desktop" CACHE STRING "Platform to build for.")
set_property(CACHE PLATFORM PROPERTY STRINGS "Desktop" "Web" "Android" "Raspberry Pi")
set(OPENGL_VERSION "3.3" CACHE STRING "OpenGL Version to build raylib with")
set_property(CACHE OPENGL_VERSION PROPERTY STRINGS "3.3" "2.1" "1.1" "ES 2.0")
# core.c
option(SUPPORT_BUSY_WAIT_LOOP "Use busy wait loop for timing sync instead of a high-resolution timer" ON)
option(SUPPORT_CAMERA_SYSTEM "Provide camera module (camera.h) with multiple predefined cameras: free, 1st/3rd person, orbital" ON)
@@ -88,4 +93,14 @@ if(DEFINED STATIC_RAYLIB)
message(DEPRECATION "-DSTATIC_RAYLIB is deprecated. Please use -DSTATIC instead.")
endif()
if(${PLATFORM} MATCHES "Desktop" AND APPLE)
if(MACOS_FATLIB)
if (CMAKE_OSX_ARCHITECTURES)
message(FATAL_ERROR "User supplied -DCMAKE_OSX_ARCHITECTURES overrides -DMACOS_FATLIB=ON")
else()
set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
endif()
endif()
endif()
# vim: ft=cmake

View File

@@ -73,6 +73,7 @@
*
**********************************************************************************************/
#include "config.h"
#if !defined(USE_OPENAL_BACKEND)
#define USE_MINI_AL 1 // Set to 1 to use mini_al; 0 to use OpenAL.
#endif

View File

@@ -1,5 +1,7 @@
/* config.h.in */
#cmakedefine USE_OPENAL_BACKEND 1
// core.c
/* Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital */
#cmakedefine SUPPORT_CAMERA_SYSTEM 1

View File

@@ -568,6 +568,7 @@ void ExportImage(const char *fileName, Image image)
// NOTE: Getting Color array as RGBA unsigned char values
unsigned char *imgData = (unsigned char *)GetImageData(image);
SavePNG(fileName, imgData, image.width, image.height, 4);
// FIXME ^ this fails on PLATFORM_WEB, what should we do?
free(imgData);
}

View File

@@ -143,31 +143,27 @@ void TraceLog(int msgType, const char *text, ...)
va_end(args);
if (msgType == LOG_ERROR) exit(1); // If LOG_ERROR message, exit program
#endif // SUPPORT_TRACELOG
}
#if defined(SUPPORT_SAVE_BMP)
// Creates a BMP image file from an array of pixel data
void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
{
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
#if defined(SUPPORT_SAVE_BMP) && (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI))
stbi_write_bmp(fileName, width, height, compSize, imgData);
TraceLog(LOG_INFO, "BMP Image saved: %s", fileName);
#endif
}
#endif
#if defined(SUPPORT_SAVE_PNG)
// Creates a PNG image file from an array of pixel data
void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
{
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
#if defined(SUPPORT_SAVE_PNG) && (defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI))
stbi_write_png(fileName, width, height, compSize, imgData, width*compSize);
TraceLog(LOG_INFO, "PNG Image saved: %s", fileName);
#endif
}
#endif
// Keep track of memory allocated
// NOTE: mallocType defines the type of data allocated