Skip to content

Commit

Permalink
Switch build system to CMake
Browse files Browse the repository at this point in the history
This unifies the build system across platforms, including doing
development inside Visual Studio. No change to other code.

Some exceptions:
 - Switch port is still a Makefile
 - run_with_tcc.bat still exists
  • Loading branch information
LaserEyess committed Jul 3, 2023
1 parent 578f90b commit 9233826
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 899 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
.DS_Store
*.dSYM
/.vs/
/packages/
/saves/
*.o
/sm
/build/
/build*/
/sm.smc
SDL2.dll
/sm.exe
/glsl-shaders/
/glsl-shaders/
84 changes: 35 additions & 49 deletions BUILDING.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
# Requirements
* A Super Metroid rom (Make sure to rename it to `sm.smc`)
* cmake (and also ninja, for better compiling speed)
* A Super Metroid rom (Make sure to rename it to `sm.smc`) which **must be in
the same folder as `sm`/`sm.exe`**
* libsdl2-dev
* Super Metroid repo `git clone --recursive https://github.com/snesrev/sm`

For Linux/MacOS you must install these for your desired OS:
* Ubuntu/Debian: `sudo apt install libsdl2-dev`
* Fedora Linux: `sudo dnf in sdl2-devel`
* Arch Linux: `sudo pacman -S sdl2`
* macOS: `brew install sdl2`
* Ubuntu/Debian: `sudo apt install libsdl2-dev cmake ninja-build`
* Fedora Linux: `sudo dnf in sdl2-devel cmake ninja-build`
* Arch Linux: `sudo pacman -S sdl2 cmake ninja`
* macOS: `brew install sdl2 cmake ninja`

# Windows

## Building with MSYS2

Dependencies and requirements:

* The `libsdl2-dev` library
* [MSYS2](https://www.msys2.org)
First, install [MSYS2](https://www.msys2.org/). After following the instructions,
install the following packages.

Note: *Make sure you're using MINGW64 and you're in `sm` folder in the terminal.*

1. Install MSYS2 on your machine.
2. Place the copy of your rom in the main directory.
3. Install the necessary dependencies by inputting this command in the terminal.

```sh
pacman -S mingw-w64-x86_64-SDL2 && pacman -S make && pacman -S mingw-w64-x86_64-gcc
```
4. Type `sdl2-config --cflags`, it should output:
```sh
-IC:/msys64/mingw64/include/SDL2 -Dmain=SDL_main
```
5. After that type `sdl2-config --libs`, should output:
```sh
-LC:/msys64/mingw64/lib -lmingw32 -mwindows -lSDL2main -lSDL2
pacman -S mingw-w64-x86_64-{gcc,cmake,SDL2,ninja}
```

After you've done installing everything, cd to `sm` folder. Type `make`
In order to speed up the compilation, type `make -j16`

## Building with Visual Studio

Dependencies and requirements:
* The `libsdl2-dev` library, which is automatically installed with NuGet.
* [Visual Studio Community 2022](https://visualstudio.microsoft.com)

Download VS installer. On installer prompt, make sure you're on "Workloads" and check `Desktop Development with C++` this will install the necessary deps for compilation.
Download VS installer. On installer prompt, make sure you're on "Workloads"
and check **both** `Desktop Development with C++` **and** `C++ CMake Tools for Windows`.
This will install the necessary deps for compilation.

1. Open `sm.sln` solution.
2. Change the build target from `Debug` to `Release`
3. Build the solution.
Visual Studio should automatically detect the CMake project and let you configure and build it. If not,
follow the [general instructions](https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170)
from Microsoft.

## Building with Tiny C Compiler

Expand All @@ -64,34 +50,34 @@ Download VS installer. On installer prompt, make sure you're on "Workloads" and

CD to your SM root folder and open the terminal and type:
```sh
make
cmake -B build && cmake --build build --parallel
```

For more advanced usage:
```sh
make -j$(nproc) # run on all core
make clean all # clear gen+obj and rebuild
CC=clang make # specify compiler
```
The resulting binary will be `build/sm`.

# Nintendo Switch

Dependencies and requirements:
## Getting Dependencies

* The `switch-sdl2` library
You will need:
* [DevKitPro](https://github.com/devkitPro/installer)
* [Atmosphere](https://github.com/Atmosphere-NX/Atmosphere)

1. Make sure you've installed Atmosphere on your Switch.
2. Please download the DevKitPro version of MSYS2 through their installer, as the default MSYS2 causes issues with windows compiling.
3. Now that you've installed DevKitPro, open up the location you've installed DevKitPro to, then find `mingw64.exe` inside `msys2` located in `devkitPro` folder.
4. Type `pacman -S git switch-dev switch-sdl2 switch-tools` in the terminal to install the `switch-sdl2` library.
5. CD to `switch` folder by typing `cd src/platfrom/switch` in the terminal on the `sm` root folder.
6. type `make` to compile the Switch Port.
7. Transfer the `.ini`, `nro`, `ncap` and your rom file to the Switch.
First, follow the [installation instructions on devkitPro's website](https://devkitpro.org/wiki/Getting_Started).

**OPTIONAL STEP**
Second, once you have pacman set up and synced, do:
```shell
pacman -S switch-dev switch-tools switch-sdl2
```

```sh
make -j$(nproc) # To build using all cores
## Building
In the top level directory, you can use the cmake preset for running a build for the switch:
```shell
cmake --preset nintendo-switch
cmake --build build-switch --parallel
```

## Getting SM on to your Switch
First, make sure you've installed [Atmosphere](https://github.com/Atmosphere-NX/Atmosphere) on your Switch.
Next, go into the `build-switch` directory and copy `sm.ini`, `sm.nro`, and `sm.ncap` and the sm rom
file to your Switch.
84 changes: 84 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)

project(sm
VERSION 0.0.1
DESCRIPTION "Super Metroid PC Port"
HOMEPAGE_URL "https://github.com/snesrev/sm"
LANGUAGES C)

if (CMAKE_BINARY_DIR EQUAL CMAKE_SOURCE_DIR)
message(WARNING "You are compiling in the source tree, you probably don't want to do that. Use -B to set the build directory")
endif()

include(GNUInstallDirs)

# Compiler Warnings
list(APPEND c_warnings
"-Wall"
"-fno-strict-aliasing")

list(APPEND msvc_warnings
"/W3"
"/wd4996")

# For installation later
set(ini_name "${PROJECT_SOURCE_DIR}/sm.ini")

# Dependencies
# ------------
# SDL
if (NINTENDO_SWITCH)
# Force static on Switch, just because it's easier
find_package(SDL2 REQUIRED COMPONENTS SDL2-static)
else()
find_package(SDL2 REQUIRED COMPONENTS SDL2)
endif()

# OpenGL
# TODO: This should likely be removed
add_library(gl STATIC "third_party/gl_core/gl_core_3_1.c")
target_include_directories(gl PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

# Main Executable
# ---------------
add_executable(sm)
add_subdirectory(src)
target_link_libraries(sm PRIVATE SDL2::SDL2 gl)
set_target_properties(sm PROPERTIES C_STANDARD 11)

if (MSVC)
target_compile_options(sm PRIVATE ${msvc_warnings})
else()
target_link_libraries(sm PRIVATE m)
target_compile_definitions(sm PRIVATE SYSTEM_VOLUME_MIXER_AVAILABLE=0)
target_compile_options(sm PRIVATE ${c_warnings})
endif()

# Nintendo Switch extra setup
if (NINTENDO_SWITCH)
target_compile_definitions(sm PUBLIC __SWITCH__)

# needs to be linked with g++ for C++ stdlib
enable_language(CXX)
set_target_properties(sm PROPERTIES
CXX_STANDARD 11
LINKER_LANGUAGE CXX)

nx_generate_nacp(sm.nacp
NAME "Super Metroid"
AUTHOR "snesrev & Lywx"
VERSION "${PROJECT_VERSION}")

nx_create_nro(sm
NACP sm.nacp
ICON "${PROJECT_SOURCE_DIR}/src/platform/switch/icon.jpg")

set(ini_name "${PROJECT_SOURCE_DIR}/src/platform/switch/sm.ini")
endif()

# Installation
# ------------
install(TARGETS sm RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")

# TODO: should be in some config dir
install(FILES "${ini_name}" DESTINATION "${CMAKE_INSTALL_BINDIR}")
20 changes: 20 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "nintendo-switch",
"binaryDir": "${sourceDir}/build-switch",
"toolchainFile": "$env{DEVKITPRO}/cmake/Switch.cmake",
"condition": {
"type": "notEquals",
"lhs": "$env{DEVKITPRO}",
"rhs": ""
}
}
]
}
33 changes: 0 additions & 33 deletions Makefile

This file was deleted.

31 changes: 0 additions & 31 deletions sm.sln

This file was deleted.

1 change: 0 additions & 1 deletion src/.gitignore

This file was deleted.

Loading

0 comments on commit 9233826

Please sign in to comment.