Skip to content

Commit 9233826

Browse files
committed
Switch build system to CMake
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
1 parent 578f90b commit 9233826

File tree

14 files changed

+239
-899
lines changed

14 files changed

+239
-899
lines changed

.gitignore

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
.DS_Store
22
*.dSYM
33
/.vs/
4-
/packages/
54
/saves/
6-
*.o
7-
/sm
8-
/build/
5+
/build*/
96
/sm.smc
10-
SDL2.dll
11-
/sm.exe
12-
/glsl-shaders/
7+
/glsl-shaders/

BUILDING.md

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
11
# Requirements
2-
* A Super Metroid rom (Make sure to rename it to `sm.smc`)
2+
* cmake (and also ninja, for better compiling speed)
3+
* A Super Metroid rom (Make sure to rename it to `sm.smc`) which **must be in
4+
the same folder as `sm`/`sm.exe`**
35
* libsdl2-dev
46
* Super Metroid repo `git clone --recursive https://github.com/snesrev/sm`
57

68
For Linux/MacOS you must install these for your desired OS:
7-
* Ubuntu/Debian: `sudo apt install libsdl2-dev`
8-
* Fedora Linux: `sudo dnf in sdl2-devel`
9-
* Arch Linux: `sudo pacman -S sdl2`
10-
* macOS: `brew install sdl2`
9+
* Ubuntu/Debian: `sudo apt install libsdl2-dev cmake ninja-build`
10+
* Fedora Linux: `sudo dnf in sdl2-devel cmake ninja-build`
11+
* Arch Linux: `sudo pacman -S sdl2 cmake ninja`
12+
* macOS: `brew install sdl2 cmake ninja`
1113

1214
# Windows
1315

1416
## Building with MSYS2
15-
16-
Dependencies and requirements:
17-
18-
* The `libsdl2-dev` library
19-
* [MSYS2](https://www.msys2.org)
17+
First, install [MSYS2](https://www.msys2.org/). After following the instructions,
18+
install the following packages.
2019

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

23-
1. Install MSYS2 on your machine.
24-
2. Place the copy of your rom in the main directory.
25-
3. Install the necessary dependencies by inputting this command in the terminal.
26-
27-
```sh
28-
pacman -S mingw-w64-x86_64-SDL2 && pacman -S make && pacman -S mingw-w64-x86_64-gcc
29-
```
30-
4. Type `sdl2-config --cflags`, it should output:
31-
```sh
32-
-IC:/msys64/mingw64/include/SDL2 -Dmain=SDL_main
33-
```
34-
5. After that type `sdl2-config --libs`, should output:
3522
```sh
36-
-LC:/msys64/mingw64/lib -lmingw32 -mwindows -lSDL2main -lSDL2
23+
pacman -S mingw-w64-x86_64-{gcc,cmake,SDL2,ninja}
3724
```
3825

39-
After you've done installing everything, cd to `sm` folder. Type `make`
40-
In order to speed up the compilation, type `make -j16`
41-
4226
## Building with Visual Studio
4327

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

48-
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.
32+
Download VS installer. On installer prompt, make sure you're on "Workloads"
33+
and check **both** `Desktop Development with C++` **and** `C++ CMake Tools for Windows`.
34+
This will install the necessary deps for compilation.
4935

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

5440
## Building with Tiny C Compiler
5541

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

6551
CD to your SM root folder and open the terminal and type:
6652
```sh
67-
make
53+
cmake -B build && cmake --build build --parallel
6854
```
6955

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

7758
# Nintendo Switch
7859

79-
Dependencies and requirements:
60+
## Getting Dependencies
8061

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

85-
1. Make sure you've installed Atmosphere on your Switch.
86-
2. Please download the DevKitPro version of MSYS2 through their installer, as the default MSYS2 causes issues with windows compiling.
87-
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.
88-
4. Type `pacman -S git switch-dev switch-sdl2 switch-tools` in the terminal to install the `switch-sdl2` library.
89-
5. CD to `switch` folder by typing `cd src/platfrom/switch` in the terminal on the `sm` root folder.
90-
6. type `make` to compile the Switch Port.
91-
7. Transfer the `.ini`, `nro`, `ncap` and your rom file to the Switch.
66+
First, follow the [installation instructions on devkitPro's website](https://devkitpro.org/wiki/Getting_Started).
9267

93-
**OPTIONAL STEP**
68+
Second, once you have pacman set up and synced, do:
69+
```shell
70+
pacman -S switch-dev switch-tools switch-sdl2
71+
```
9472

95-
```sh
96-
make -j$(nproc) # To build using all cores
73+
## Building
74+
In the top level directory, you can use the cmake preset for running a build for the switch:
75+
```shell
76+
cmake --preset nintendo-switch
77+
cmake --build build-switch --parallel
9778
```
79+
80+
## Getting SM on to your Switch
81+
First, make sure you've installed [Atmosphere](https://github.com/Atmosphere-NX/Atmosphere) on your Switch.
82+
Next, go into the `build-switch` directory and copy `sm.ini`, `sm.nro`, and `sm.ncap` and the sm rom
83+
file to your Switch.

CMakeLists.txt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
2+
3+
project(sm
4+
VERSION 0.0.1
5+
DESCRIPTION "Super Metroid PC Port"
6+
HOMEPAGE_URL "https://github.com/snesrev/sm"
7+
LANGUAGES C)
8+
9+
if (CMAKE_BINARY_DIR EQUAL CMAKE_SOURCE_DIR)
10+
message(WARNING "You are compiling in the source tree, you probably don't want to do that. Use -B to set the build directory")
11+
endif()
12+
13+
include(GNUInstallDirs)
14+
15+
# Compiler Warnings
16+
list(APPEND c_warnings
17+
"-Wall"
18+
"-fno-strict-aliasing")
19+
20+
list(APPEND msvc_warnings
21+
"/W3"
22+
"/wd4996")
23+
24+
# For installation later
25+
set(ini_name "${PROJECT_SOURCE_DIR}/sm.ini")
26+
27+
# Dependencies
28+
# ------------
29+
# SDL
30+
if (NINTENDO_SWITCH)
31+
# Force static on Switch, just because it's easier
32+
find_package(SDL2 REQUIRED COMPONENTS SDL2-static)
33+
else()
34+
find_package(SDL2 REQUIRED COMPONENTS SDL2)
35+
endif()
36+
37+
# OpenGL
38+
# TODO: This should likely be removed
39+
add_library(gl STATIC "third_party/gl_core/gl_core_3_1.c")
40+
target_include_directories(gl PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
41+
42+
# Main Executable
43+
# ---------------
44+
add_executable(sm)
45+
add_subdirectory(src)
46+
target_link_libraries(sm PRIVATE SDL2::SDL2 gl)
47+
set_target_properties(sm PROPERTIES C_STANDARD 11)
48+
49+
if (MSVC)
50+
target_compile_options(sm PRIVATE ${msvc_warnings})
51+
else()
52+
target_link_libraries(sm PRIVATE m)
53+
target_compile_definitions(sm PRIVATE SYSTEM_VOLUME_MIXER_AVAILABLE=0)
54+
target_compile_options(sm PRIVATE ${c_warnings})
55+
endif()
56+
57+
# Nintendo Switch extra setup
58+
if (NINTENDO_SWITCH)
59+
target_compile_definitions(sm PUBLIC __SWITCH__)
60+
61+
# needs to be linked with g++ for C++ stdlib
62+
enable_language(CXX)
63+
set_target_properties(sm PROPERTIES
64+
CXX_STANDARD 11
65+
LINKER_LANGUAGE CXX)
66+
67+
nx_generate_nacp(sm.nacp
68+
NAME "Super Metroid"
69+
AUTHOR "snesrev & Lywx"
70+
VERSION "${PROJECT_VERSION}")
71+
72+
nx_create_nro(sm
73+
NACP sm.nacp
74+
ICON "${PROJECT_SOURCE_DIR}/src/platform/switch/icon.jpg")
75+
76+
set(ini_name "${PROJECT_SOURCE_DIR}/src/platform/switch/sm.ini")
77+
endif()
78+
79+
# Installation
80+
# ------------
81+
install(TARGETS sm RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
82+
83+
# TODO: should be in some config dir
84+
install(FILES "${ini_name}" DESTINATION "${CMAKE_INSTALL_BINDIR}")

CMakePresets.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 21,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "nintendo-switch",
11+
"binaryDir": "${sourceDir}/build-switch",
12+
"toolchainFile": "$env{DEVKITPRO}/cmake/Switch.cmake",
13+
"condition": {
14+
"type": "notEquals",
15+
"lhs": "$env{DEVKITPRO}",
16+
"rhs": ""
17+
}
18+
}
19+
]
20+
}

Makefile

Lines changed: 0 additions & 33 deletions
This file was deleted.

sm.sln

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)