Skip to content

Commit

Permalink
CMake: use TARGET_PLATFORM=<platform> instead of <platform>_BUILD=ON
Browse files Browse the repository at this point in the history
  • Loading branch information
crudelios authored and bvschaik committed May 23, 2021
1 parent bdd5bea commit 04c0271
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .ci_scripts/run_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

case "$BUILD_TARGET" in
"vita")
docker exec vitasdk /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DVITA_BUILD=ON .."
docker exec vitasdk /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DTARGET_PLATFORM=vita .."
;;
"switch")
docker exec switchdev /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DSWITCH_BUILD=ON .."
docker exec switchdev /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DTARGET_PLATFORM=switch .."
;;
"mac")
mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DSYSTEM_LIBS=OFF ..
Expand All @@ -25,7 +25,7 @@ case "$BUILD_TARGET" in
;;
"emscripten")
export EMSDK=${PWD}/emsdk
mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DSYSTEM_LIBS=OFF -DEMSCRIPTEN_BUILD=ON ..
mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DSYSTEM_LIBS=OFF -DTARGET_PLATFORM=emscripten ..
;;
*)
mkdir build && cd build && cmake ..
Expand Down
58 changes: 25 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
cmake_minimum_required(VERSION 3.1)
include(CMakeDependentOption)

if(VITA_BUILD AND SWITCH_BUILD)
message(FATAL_ERROR "Cannot build with -DVITA_BUILD=ON and -DSWITCH_BUILD=ON at the same time")
endif()
set(TARGET_PLATFORM "default" CACHE STRING "Platform to cross-compile for. Options: vita switch android emscripten. Leave blank for no cross compilation")
set_property(CACHE TARGET_PLATFORM PROPERTY STRINGS default vita switch android emscripten)

if((VITA_BUILD OR SWITCH_BUILD) AND ANDROID_BUILD)
message(FATAL_ERROR "Cannot build with -DVITA_BUILD=ON or -DSWITCH_BUILD=ON and -DANDROID_BUILD=ON at the same time")
endif()
string(TOLOWER ${TARGET_PLATFORM} TARGET_PLATFORM)

option(DRAW_FPS "Draw FPS on the top left corner of the window." OFF)
option(SYSTEM_LIBS "Use system libraries when available." ON)
option(EMSCRIPTEN_LOAD_SDL_PORTS "Load SDL and SDL_mixer emscripten ports instead of compiling them" OFF)
option(LINK_MPG123 "Link mpg123 statically to Julius instead of relying on a library." OFF)

cmake_dependent_option(VITA_BUILD "Build for the PlayStation Vita handheld game console." OFF "NOT MSVC" OFF)
cmake_dependent_option(SWITCH_BUILD "Build for the Nintendo Switch handheld game console." OFF "NOT MSVC; NOT VITA_BUILD" OFF)
cmake_dependent_option(ANDROID_BUILD "Build for the Android environment." OFF "NOT MSVC; NOT VITA_BUILD; NOT SWITCH_BUILD" OFF)
cmake_dependent_option(EMESCRIPTEN_BUILD "Build for the Emscripten environment." OFF "NOT VITA_BUILD; NOT SWITCH_BUILD; NOT ANDROID_BUILD" OFF)

if(VITA_BUILD AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(${TARGET_PLATFORM} STREQUAL "vita" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{VITASDK})
set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
else()
message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
endif()
endif()

if(SWITCH_BUILD AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(${TARGET_PLATFORM} STREQUAL "switch" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{DEVKITPRO})
set(DEVKITPRO $ENV{DEVKITPRO})
set(CMAKE_TOOLCHAIN_FILE "./cmake/switch.cmake" CACHE PATH "toolchain file")
Expand All @@ -36,7 +28,7 @@ if(SWITCH_BUILD AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
endif()
endif()

if(EMSCRIPTEN_BUILD AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(${TARGET_PLATFORM} STREQUAL "emscripten" AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{EMSDK})
set(EMSDK $ENV{EMSDK})
set(CMAKE_TOOLCHAIN_FILE "$ENV{EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" CACHE PATH "toolchain file")
Expand All @@ -49,7 +41,7 @@ set(SHORT_NAME julius)
set(USER_FRIENDLY_NAME Julius)
project(${SHORT_NAME} C)

if (VITA_BUILD)
if (${TARGET_PLATFORM} STREQUAL "vita")
include("${VITASDK}/share/vita.cmake" REQUIRED)
set(VITA_TITLEID "JULIUS001")
endif()
Expand Down Expand Up @@ -123,7 +115,7 @@ else()
set(VERSION_REVISION " unknown development version")
endif()

if(VITA_BUILD)
if(${TARGET_PLATFORM} STREQUAL "vita")
string(LENGTH "${PROJECT_VERSION_MAJOR}" NUM_DIGITS)
if(NUM_DIGITS EQUAL 1)
set(VITA_VERSION_MAJOR "0${PROJECT_VERSION_MAJOR}")
Expand All @@ -145,15 +137,15 @@ if(VITA_BUILD)
set(VITA_VERSION "${VITA_VERSION_MAJOR}.${VITA_VERSION_MINOR}")
endif()

if (SWITCH_BUILD)
if (${TARGET_PLATFORM} STREQUAL "switch")
string(SUBSTRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}${VERSION_REVISION}" 0 15 SWITCH_VERSION)
endif()

configure_file(${PROJECT_SOURCE_DIR}/gen/version.c.in ${PROJECT_SOURCE_DIR}/src/platform/version.c)
configure_file(${PROJECT_SOURCE_DIR}/gen/version.rc.in ${PROJECT_SOURCE_DIR}/res/version.rc)
configure_file(${PROJECT_SOURCE_DIR}/gen/version.txt.in ${PROJECT_SOURCE_DIR}/res/version.txt)

if(EMSCRIPTEN_BUILD)
if(${TARGET_PLATFORM} STREQUAL "emscripten")
configure_file(${PROJECT_SOURCE_DIR}/gen/shell.html.in ${PROJECT_SOURCE_DIR}/res/shell.html)
endif()

Expand Down Expand Up @@ -202,18 +194,18 @@ set(PLATFORM_FILES
${PROJECT_SOURCE_DIR}/src/platform/virtual_keyboard.c
)

if (VITA_BUILD)
if (${TARGET_PLATFORM} STREQUAL "vita")
set(PLATFORM_FILES
${PLATFORM_FILES}
${PROJECT_SOURCE_DIR}/src/platform/vita/vita.c
${PROJECT_SOURCE_DIR}/src/platform/vita/vita_keyboard.c
)
elseif (SWITCH_BUILD)
elseif (${TARGET_PLATFORM} STREQUAL "switch")
set(PLATFORM_FILES
${PLATFORM_FILES}
${PROJECT_SOURCE_DIR}/src/platform/switch/switch.c
)
elseif (ANDROID_BUILD)
elseif (${TARGET_PLATFORM} STREQUAL "android")
set(PLATFORM_FILES
${PLATFORM_FILES}
${PROJECT_SOURCE_DIR}/src/platform/android/android.c
Expand Down Expand Up @@ -594,7 +586,7 @@ if(APPLE)
endif()

set(EMSCRIPTEN_FILES "")
if(EMSCRIPTEN_BUILD)
if(${TARGET_PLATFORM} STREQUAL "emscripten")
set(EMSCRIPTEN_FILES ${PROJECT_SOURCE_DIR}/res/shell.html)
endif()

Expand Down Expand Up @@ -622,7 +614,7 @@ set(SOURCE_FILES
${EMSCRIPTEN_FILES}
)

if(EMSCRIPTEN_BUILD AND EMSCRIPTEN_LOAD_SDL_PORTS)
if(${TARGET_PLATFORM} STREQUAL "emscripten" AND EMSCRIPTEN_LOAD_SDL_PORTS)
set(USE_FLAGS "-s USE_SDL=2 -s USE_SDL_MIXER=2")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${USE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${USE_FLAGS}")
Expand Down Expand Up @@ -650,7 +642,7 @@ else()

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)
if(EMSCRIPTEN_BUILD)
if(${TARGET_PLATFORM} STREQUAL "emscripten")
set(LINK_MPG123 true)
endif()
endif()
Expand All @@ -659,13 +651,13 @@ if(LINK_MPG123)
find_package(MPG123 REQUIRED)
endif()

if(ANDROID_BUILD)
if(${TARGET_PLATFORM} STREQUAL "android")
add_library(${SHORT_NAME} SHARED ${SDL2_ANDROID_HOOK} ${SOURCE_FILES})
else()
add_executable(${SHORT_NAME} WIN32 ${SOURCE_FILES})
endif()

if(EMSCRIPTEN_BUILD)
if(${TARGET_PLATFORM} STREQUAL "emscripten")
set(CMAKE_EXECUTABLE_SUFFIX .html)
set_target_properties(${SHORT_NAME} PROPERTIES LINK_FLAGS "-lidbfs.js -s SINGLE_FILE=1 -s DEMANGLE_SUPPORT=1 -s DYNCALLS=1 --shell-file ${PROJECT_SOURCE_DIR}/res/shell.html -s INITIAL_MEMORY=268435456 -s ALLOW_MEMORY_GROWTH=1 -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 -s SAFE_HEAP=1 -s INVOKE_RUN=0 -s 'EXPORTED_FUNCTIONS=[\"_main\"]' -s 'EXPORTED_RUNTIME_METHODS=[\"callMain\",\"FS\"]' --bind")
endif()
Expand Down Expand Up @@ -753,12 +745,12 @@ else()
target_sources(${SHORT_NAME} PRIVATE "${PNG_FILES}" "${ZLIB_FILES}")
endif()

if(VITA_BUILD)
if(${TARGET_PLATFORM} STREQUAL "vita")
include_directories(
$ENV{VITASDK}/arm-vita-eabi/include
${CMAKE_CURRENT_SOURCE_DIR}
)
elseif (SWITCH_BUILD)
elseif (${TARGET_PLATFORM} STREQUAL "switch")
include_directories(
${DEVKITPRO}/portlibs/switch/include/SDL2
${DEVKITPRO}/libnx/include
Expand All @@ -773,7 +765,7 @@ if(MSVC)
include_directories(ext/dirent)
endif()

if (VITA_BUILD)
if (${TARGET_PLATFORM} STREQUAL "vita")
target_link_libraries (${SHORT_NAME}
${SDL2_MIXER_LIBRARY}
${SDL2_LIBRARY}
Expand Down Expand Up @@ -813,7 +805,7 @@ if (VITA_BUILD)
FILE res/vita/startup.png sce_sys/livearea/contents/startup.png
FILE res/vita/template.xml sce_sys/livearea/contents/template.xml
)
elseif(SWITCH_BUILD)
elseif(${TARGET_PLATFORM} STREQUAL "switch")
target_link_libraries (${SHORT_NAME}
SDL2_mixer
SDL2
Expand Down Expand Up @@ -856,15 +848,15 @@ else()
if (UNIX AND NOT APPLE AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID STREQUAL "Clang"))
target_link_libraries(${SHORT_NAME} m)
endif()
if(ANDROID_BUILD)
if(${TARGET_PLATFORM} STREQUAL "android")
find_library(LOG_LIB log)
target_link_libraries(${SHORT_NAME} ${LOG_LIB})
endif()
if(LINK_MPG123)
target_link_libraries(${SHORT_NAME} ${MPG123_LIBRARY})
endif()
target_link_libraries(${SHORT_NAME} ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY})
if(NOT APPLE AND NOT ANDROID_BUILD)
if(NOT APPLE AND NOT ${TARGET_PLATFORM} STREQUAL "android")
install(TARGETS ${SHORT_NAME} RUNTIME DESTINATION bin)
endif()

Expand All @@ -878,7 +870,7 @@ else()
endif()

# Unit tests
if(NOT VITA_BUILD AND NOT SWITCH_BUILD AND NOT ANDROID_BUILD AND NOT EMSCRIPTEN_BUILD)
if(${TARGET_PLATFORM} STREQUAL "default")
enable_testing()
add_subdirectory(test)
endif()
Expand Down
2 changes: 1 addition & 1 deletion android/julius/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ android {

externalNativeBuild {
cmake {
arguments "-DANDROID_BUILD=ON", "-DSYSTEM_LIBS=OFF"
arguments "-DTARGET_PLATFORM=android", "-DSYSTEM_LIBS=OFF"
}
}
ndk {
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindSDL2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

GET_SDL_EXT_DIR(SDL_EXT_DIR "")

IF(ANDROID_BUILD)
IF(${TARGET_PLATFORM} STREQUAL "android")
STRING(TOLOWER ${CMAKE_BUILD_TYPE} ANDROID_BUILD_DIR)
SET(SDL2_LIBRARY SDL2)
SET(SDL2_ANDROID_HOOK ${SDL_EXT_DIR}/src/main/android/SDL_android_main.c)
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindSDL2_mixer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

GET_SDL_EXT_DIR(SDL_MIXER_EXT_DIR "mixer")

IF(ANDROID_BUILD)
IF(${TARGET_PLATFORM} STREQUAL "android")
STRING(TOLOWER ${CMAKE_BUILD_TYPE} ANDROID_BUILD_DIR)
SET(SDL2_MIXER_LIBRARY SDL2_mixer)
link_directories(${PROJECT_SOURCE_DIR}/android/SDL2/build/intermediates/ndkBuild/${ANDROID_BUILD_DIR}/obj/local/${ANDROID_ABI})
Expand Down

0 comments on commit 04c0271

Please sign in to comment.