Skip to content

Commit

Permalink
improving build
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 11, 2024
1 parent 5b0e933 commit ec10ff9
Show file tree
Hide file tree
Showing 26 changed files with 231 additions and 399 deletions.
159 changes: 7 additions & 152 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,162 +8,17 @@ project(null0
)

include(FetchContent)
set(FETCHCONTENT_QUIET 0)
# set(FETCHCONTENT_QUIET OFF)

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
cmake_policy(SET CMP0077 NEW)
cmake_policy(SET CMP0135 NEW)

set(BUILD_SHARED_LIBS OFF)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")

# These options will change what is built, and what libraries are used

if(EMSCRIPTEN)
option(CARTS "Build Demo Null0 Carts" FALSE)
else()
option(SDL "Use SDL in hosts (Choose SDL or RAYLIB)" TRUE)
option(RAYLIB "Use Raylib in hosts (Choose SDL or RAYLIB)" FALSE)
option(CARTS "Build Demo Null0 Carts" TRUE)
endif()

option(TESTS "Unit tests" FALSE)
option(LIBRETRO "Build Null0 libretro Host" FALSE)
option(HOST "Build Null0 Host" TRUE)

# this is used to output options
macro(message_bool _NAME _VALUE)
if(${_VALUE})
message(STATUS " ${_NAME}: enabled")
else()
message(STATUS " ${_NAME}: disabled")
endif()
endmacro()

if (LIBRETRO)
set(PNTR_APP_LIBRETRO ON)
endif()

# wamr is used in libretro & native runtimes
if ((HOST OR LIBRETRO) AND NOT EMSCRIPTEN)
find_package(wamr REQUIRED)
endif()

# check some invalid combos

if(EMSCRIPTEN AND LIBRETRO)
message(FATAL_ERROR "Do not build libretro core in emscripten. It will not turn out right.")
endif()

if (NOT EMSCRIPTEN AND (NOT (SDL OR RAYLIB)))
message(FATAL_ERROR "You must set SDL or RAYLIB.")
endif()

if (SDL AND RAYLIB)
message(FATAL_ERROR "You must set SDL or RAYLIB, not both.")
endif()

# shared defines for the API
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/null0_api/src)

# core API will be built with any runtime
if (LIBRETRO OR HOST OR TESTS)
if (SDL)

# SDL
set(SDL_SHARED FALSE)
set(SDL2_SHARED FALSE)
set(SDL_STATIC TRUE)
set(SDL_TEST FALSE)
set(SDL_TESTS FALSE)
set(INTERFACE_SDL2_SHARED false)
set(SDL2_DISABLE_UNINSTALL TRUE)
set(SDL2_DISABLE_INSTALL TRUE)
set(SDL_INSTALL_TESTS FALSE)
set(SDL2_LIBRARIES SDL2::SDL2-static)
#set(SDL2_LIBRARIES SDL2::SDL2 SDL2::SDL2main)

# SDL_mixer
set(SDL2MIXER_VORBIS STB)
set(SDL2MIXER_INSTALL OFF)
set(SDL2MIXER_DEPS_SHARED OFF)
set(SDL2MIXER_VENDORED OFF)
set(SDL2MIXER_SAMPLES OFF)
set(SDL2MIXER_CMD OFF)
set(SDL2MIXER_FLAC OFF)
set(SDL2MIXER_MOD OFF)
set(SDL2MIXER_MP3 OFF)
set(SDL2MIXER_MIDI_NATIVE OFF)
set(SDL2MIXER_OPUS OFF)
set(SDL2MIXER_OPUS_SHARED OFF)
set(SDL2MIXER_MIDI_FLUIDSYNTH OFF)
set(SDL2MIXER_MP3_MPG123 OFF)
set(SDL2MIXER_MP3_DRMP3 OFF)
set(SDL2MIXER_MOD_XMP OFF)
set(SDL2MIXER_MOD_MODPLUG OFF)
set(SDL2MIXER_FLAC_DRFLAC OFF)
set(SDL2MIXER_FLAC_LIBFLAC OFF)
set(SDL2MIXER_VORBIS_VORBISFILE OFF)
set(SDL2MIXER_WAVPACK OFF)
set(SDL2MIXER_SAMPLES_INSTALL OFF)
set(SDL2MIXER_BUILD_SHARED_LIBS OFF)

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)

add_definitions(-DPNTR_APP_SDL -DPNTR_PIXELFORMAT_ARGB)
include_directories(${SDL2_INCLUDE_DIR}/..)
endif()

if (RAYLIB)
find_package(raylib REQUIRED)
add_definitions(-DPNTR_APP_RAYLIB -DPNTR_PIXELFORMAT_RGBA)
endif()

if (EMSCRIPTEN)
add_definitions(-DPNTR_APP_WEB -DPNTR_PIXELFORMAT_RGBA)
endif()

set(PHYSFS_ARCHIVE_7Z FALSE)
set(PHYSFS_ARCHIVE_GRP FALSE)
set(PHYSFS_ARCHIVE_HOG FALSE)
set(PHYSFS_ARCHIVE_ISO9660 FALSE)
set(PHYSFS_ARCHIVE_MVL FALSE)
set(PHYSFS_ARCHIVE_QPAK FALSE)
set(PHYSFS_ARCHIVE_SLB FALSE)
set(PHYSFS_ARCHIVE_VDF FALSE)
set(PHYSFS_ARCHIVE_CSM FALSE)
set(PHYSFS_ARCHIVE_WAD FALSE)
set(PHYSFS_BUILD_STATIC TRUE)
set(PHYSFS_BUILD_SHARED FALSE)
set(PHYSFS_BUILD_TEST FALSE)
set(PHYSFS_BUILD_DOCS FALSE)
set(PHYSFS_DISABLE_INSTALL TRUE)
set(PHYSFS_TARGETNAME_UNINSTALL "phys-uninstall")

find_package(pntr REQUIRED)
find_package(pntr_app REQUIRED)
find_package(physfs REQUIRED)
add_subdirectory(null0_api)
endif()

if (HOST)
add_subdirectory(host)
endif()

if (CARTS)
add_subdirectory(cart/c)
endif()

if (TESTS)
add_subdirectory(test)
endif()

# CARTS=ON/OFF
# HOST_TYPE=OFF/WEB/RAYLIB/SDL/RETRO

message(STATUS "Null0 will built with the following options:")
message_bool("Web host" HOST AND EMSCRIPTEN)
message_bool("Native host" HOST AND (NOT EMSCRIPTEN))
message_bool("Libretro host" LIBRETRO)
message_bool("Example carts" CARTS)
message_bool("Unit tests" TESTS)
message_bool("Hosts use Raylib" RAYLIB)
message_bool("Hosts use SDL" SDL)
find_package(null0 REQUIRED)
8 changes: 0 additions & 8 deletions DEPS.md

This file was deleted.

68 changes: 23 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,66 +66,44 @@ You will need cmake, ninja & emscripten installed.

### more on cmake

You can tune how you build things by using cmake directly:
I like ninja, in the tasks above, because it builds a bit faster, but it's optional, if yuou use cmake directly.

```
# config
cmake -B build
# build runtime
cmake --build build
Essentially, there are 2 options:

# run with cart
./build/host/null0 ./build/cart/c/input.null0
- `CARTS` - Should carts be built?
- `HOST_TYPE` - What sort of host are you building (OFF/WEB/RAYLIB/SDL/RETRO)

# configure for web
emcmake cmake -B wbuild
You can only build 1 `HOST_TYPE` at a time. `CARTS` can be paired with any `HOST_TYPE`, but not `WEB`, and `WEB` requires emscripten (and should probably be built in a seperate root.)

# build web runtime
emmake make -C wbuild
```

These cmake-flags will effect the build:

```
// Build Null0 libretro Host (don't do this in web-build)
LIBRETRO:BOOL=OFF
#### examples

// Build Null0 Host (web or native)
HOST:BOOL=ON
There are 2 steps: configure & build.

// Use SDL in hosts
SDL:BOOL=ON
// Build Null0 raylib Host
RAYLIB:BOOL=OFF
```sh
# configure without ninja (plain make) build only carts
cmake -B build -DCARTS=ON -DHOST_TYPE=OFF

// Build Demo Null0 Carts
CARTS:BOOL=ON
# configure for ninja, build only carts
cmake -GNinja -B build -DCARTS=ON -DHOST_TYPE=OFF

// Unit tests
TESTS:BOOL=OFF
```
# configure for ninja, build only raylib host
cmake -GNinja -B build -DCARTS=OFF -DHOST_TYPE=RAYLIB

You can set them like this:
# configure for ninja, build only SDL host
cmake -GNinja -B build -DCARTS=OFF -DHOST_TYPE=SDL

```
# don't build demo carts, use defaults
cmake -B build -DCARTS=0
# configure for ninja, build only libretro-core
cmake -GNinja -B build -DCARTS=OFF -DHOST_TYPE=RETRO

# don't buid anything but the carts (since LIBRETRO and TESTS is off by default)
cmake -B build -DHOST=0
# configure for ninja, build only web-host (in wbuild/)
emcmake cmake -GNinja -B wbuild

# just build unit-tests
cmake -B build -DHOST=0 -DTESTS=1 -DCARTS=0
# after configure, build build
cmake --build build
./build/test/test_colors

# after configure, build wbuild (for web)
cmake --build wbuild

# just build carts
cmake -B build -DHOST=0 -DTESTS=0 -DCARTS=1
cmake --build build
```

## todo

Expand Down
Binary file modified docs/cart/colorbars.null0
Binary file not shown.
Binary file modified docs/cart/draw.null0
Binary file not shown.
Binary file modified docs/cart/filesystem.null0
Binary file not shown.
Binary file modified docs/cart/flappybird.null0
Binary file not shown.
Binary file modified docs/cart/hello.null0
Binary file not shown.
Binary file modified docs/cart/input.null0
Binary file not shown.
Binary file modified docs/cart/justlog.null0
Binary file not shown.
Binary file modified docs/cart/sound.null0
Binary file not shown.
Binary file modified docs/cart/tracker.null0
Binary file not shown.
26 changes: 14 additions & 12 deletions host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
add_executable(${PROJECT_NAME} src/main.c)

if(NOT EMSCRIPTEN)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
target_link_libraries(${PROJECT_NAME} null0_api vmlib)
if (NULL0_HOST_TYPE_RETRO)
add_library(${PROJECT_NAME}_libretro src/main.c)
target_link_libraries(${PROJECT_NAME}_libretro null0_api vmlib)
else()
if (NULL0_HOST_TYPE_SDL OR NULL0_HOST_TYPE_RAYLIB OR NULL0_HOST_TYPE_WEB)
add_executable(${PROJECT_NAME} src/main.c)
if(NULL0_HOST_TYPE_WEB)
set(CMAKE_EXECUTABLE_SUFFIX ".mjs")
target_link_options(${PROJECT_NAME} PRIVATE -sFORCE_FILESYSTEM -sEXPORTED_RUNTIME_METHODS=FS,stringToUTF8 -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/functions.txt)
target_link_libraries(${PROJECT_NAME} null0_api)
else()
target_link_libraries(${PROJECT_NAME} null0_api vmlib)
endif()
endif()
endif()

if(EMSCRIPTEN)
set(CMAKE_EXECUTABLE_SUFFIX ".mjs")
target_link_options(${PROJECT_NAME} PRIVATE -sFORCE_FILESYSTEM -sEXPORTED_RUNTIME_METHODS=FS,stringToUTF8 -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/functions.txt)
endif()

target_link_libraries(${PROJECT_NAME} null0_api)
9 changes: 3 additions & 6 deletions null0_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
add_library(null0_api src/main.c src/sfx_gen.c)

# TODO: check for emscripten

if (RAYLIB)
if (NULL0_HOST_TYPE_RAYLIB)
target_link_libraries(null0_api raylib pntr pntr_app physfs-static)
endif()

if (SDL)
message("SDL libs: ${SDL2_LIBRARIES} ${SDL2_MIXER_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}")
if (NULL0_HOST_TYPE_SDL)
target_link_libraries(null0_api ${SDL2_LIBRARIES} pntr pntr_app physfs-static)
endif()

if (EMSCRIPTEN)
if (NULL0_HOST_TYPE_WEB)
target_link_libraries(null0_api pntr pntr_app physfs-static)
endif()
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"scripts": {
"clean": "rimraf build wbuild docs/cart docs/wasm",
"start": "npm run build:site && live-server docs",
"build:retro": "cmake -GNinja -B build -DLIBRETRO=1 -DHOST=0 -DTESTS=0 -DCARTS=0 && cmake --build build",
"build:carts": "cmake -GNinja -B build -DLIBRETRO=0 -DHOST=0 -DTESTS=0 -DCARTS=1 && cmake --build build",
"build:host": "cmake -GNinja -B build -DLIBRETRO=0 -DHOST=1 -DTESTS=0 -DCARTS=0 && cmake --build build",
"build:web": "emcmake cmake -DLIBRETRO=0 -DHOST=1 -DTESTS=0 -DCARTS=0 -GNinja -B wbuild && cmake --build wbuild",
"build:retro": "cmake -GNinja -B build -DCARTS=OFF -DHOST_TYPE=RETRO && cmake --build build",
"build:carts": "cmake -GNinja -B build -DCARTS=ON -DHOST_TYPE=OFF && cmake --build build",
"build:host": "cmake -GNinja -B build -DCARTS=OFF -DHOST_TYPE=RAYLIB && cmake --build build",
"build:web": "emcmake cmake -GNinja -B wbuild && cmake --build wbuild",
"build:site": "npm run build:carts && npm run build:web && mkdir -p docs/wasm docs/cart && cp wbuild/host/null0.* docs/wasm && cp build/cart/c/*.null0 docs/cart"
},
"keywords": [],
Expand Down
15 changes: 0 additions & 15 deletions test/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit ec10ff9

Please sign in to comment.