Skip to content

Commit

Permalink
Add a Nix package
Browse files Browse the repository at this point in the history
Also adds options to the CMake build system to unvendor some
dependencies to make packaging easier, along with installing the final
executable.
  • Loading branch information
nadiaholmquist authored and skylersaleh committed Feb 7, 2025
1 parent 8abce81 commit 15cc273
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 28 deletions.
94 changes: 66 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(ENABLE_RETRO_ACHIEVEMENTS "Enable Retro Achievements" ON)

option(USE_SYSTEM_CURL "Use the system's libcurl package" OFF)
option(USE_SYSTEM_OPENSSL "Use the system's OpenSSL package" OFF)
option(USE_SYSTEM_SDL2 "Use the system's SDL2 package" OFF)

if (EMSCRIPTEN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ELIMINATE\_DUPLICATE\_FUNCTIONS=1 -s ENVIRONMENT=web -s ASSERTIONS=0 -s WASM=1 -DSE_PLATFORM_WEB --shell-file ${PROJECT_SOURCE_DIR}/src/shell.html -s USE_CLOSURE_COMPILER=0 ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ELIMINATE\_DUPLICATE\_FUNCTIONS=1 -s ENVIRONMENT=web -s ASSERTIONS=0 -s WASM=1 -DSE_PLATFORM_WEB --shell-file ${PROJECT_SOURCE_DIR}/src/shell.html -s USE_CLOSURE_COMPILER=0 ")
Expand Down Expand Up @@ -102,28 +106,43 @@ if (SE_PLATFORM_LINUX OR SE_PLATFORM_FREEBSD)
find_package(X11 REQUIRED)
endif()

if (USE_SYSTEM_CURL OR USE_SYSTEM_OPENSSL)
find_package(PkgConfig REQUIRED)
endif()

if(NOT EMSCRIPTEN)
set(WITH_APPS OFF CACHE BOOL "" FORCE) # don't build executable
set(CURL_USE_LIBSSH2 OFF)
set(CURL_USE_LIBPSL OFF)
set(CURL_USE_OPENSSL ON)
set(CURL_DISABLE_LDAP ON)
set(USE_LIBIDN2 OFF)
set(CURL_ENABLE_EXPORT_TARGET OFF)
set(BUILD_TESTING OFF)
set(BUILD_CURL_EXE OFF)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
if (WIN32)
set(CURL_STATIC_CRT ON)
if (USE_SYSTEM_CURL)
pkg_check_modules(Curl REQUIRED IMPORTED_TARGET libcurl)
else()
set(WITH_APPS OFF CACHE BOOL "" FORCE) # don't build executable
set(CURL_USE_LIBSSH2 OFF)
set(CURL_USE_LIBPSL OFF)
set(CURL_USE_OPENSSL ON)
set(CURL_DISABLE_LDAP ON)
set(USE_LIBIDN2 OFF)
set(CURL_ENABLE_EXPORT_TARGET OFF)
set(BUILD_TESTING OFF)
set(BUILD_CURL_EXE OFF)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
if (WIN32)
set(CURL_STATIC_CRT ON)
endif()
# see "Important static libcurl usage note" in INSTALL.md
add_definitions(-DCURL_STATICLIB)
add_subdirectory(src/curl)
set_property(TARGET libcurl_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()

if(USE_SYSTEM_OPENSSL)
pkg_check_modules(OpenSSL REQUIRED IMPORTED_TARGET openssl)
else()
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
add_subdirectory(src/openssl)
set_property(TARGET ssl PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET crypto PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()
# see "Important static libcurl usage note" in INSTALL.md
add_definitions(-DCURL_STATICLIB)
add_subdirectory(src/openssl)
add_subdirectory(src/curl)
set_property(TARGET ssl PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET crypto PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET libcurl_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()

# TinyFileDialogs
Expand Down Expand Up @@ -158,6 +177,9 @@ set_property(TARGET cimgui PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
target_include_directories(cimgui INTERFACE src/cimgui)

if(USE_SDL)
if (USE_SYSTEM_SDL2)
pkg_check_modules(SDL2 REQUIRED IMPORTED_TARGET sdl2)
else()
message("Build with SDL!")

#=== LIBRARY: SDL2
Expand All @@ -182,6 +204,7 @@ if(USE_SDL)
add_definitions(-DSDL_LEAN_AND_MEAN=1)
add_subdirectory(src/SDL2)
set_property(TARGET SDL2-static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()
endif()

if (EMSCRIPTEN)
Expand Down Expand Up @@ -301,7 +324,7 @@ if(UNICODE_GUI)
add_definitions(-DUTF8PROC_STATIC=1)
endif()

if(USE_SDL)
if(USE_SDL AND NOT USE_SYSTEM_SDL2)
include_directories(${SDL2_INCLUDE_DIRS})
endif()

Expand Down Expand Up @@ -397,11 +420,19 @@ endif ()
set(LINK_LIBS ${LINK_LIBS} sokol ${ALSA_LIBRARIES})

if(NOT EMSCRIPTEN)
target_include_directories(${PROJECT_NAME} PRIVATE src/openssl/include)
# OpenSSL autogenerates some header file such as openssl/opensslconf.h and we want to include them
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/src/openssl/include)
target_include_directories(${PROJECT_NAME} PRIVATE src/curl/include)
set(LINK_LIBS ${LINK_LIBS} libcurl_static ssl crypto)
if (USE_SYSTEM_OPENSSL)
set(LINK_LIBS ${LINK_LIBS} PkgConfig::OpenSSL)
else()
target_include_directories(${PROJECT_NAME} PRIVATE src/openssl/include)
# OpenSSL autogenerates some header file such as openssl/opensslconf.h and we want to include them
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/src/openssl/include)
endif()
if (USE_SYSTEM_CURL)
set(LINK_LIBS ${LINK_LIBS} PkgConfig::Curl)
else()
target_include_directories(${PROJECT_NAME} PRIVATE src/curl/include)
set(LINK_LIBS ${LINK_LIBS} libcurl_static ssl crypto)
endif()
endif()

set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "Build capstone tests" FORCE)
Expand All @@ -410,12 +441,16 @@ set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "Build capstone shared library" FORCE)
set(CAPSTONE_ARCHITECTURE_DEFAULT OFF CACHE BOOL "Include all CAPSTONE architectures" FORCE)
set(CAPSTONE_ARM_SUPPORT ON CACHE BOOL "ARM disasm support")
set(CAPSTONE_BUILD_STATIC_RUNTIME ON CACHE BOOL "Capstone static runtime" FORCE)
add_subdirectory(src/capstone)
add_subdirectory(src/capstone EXCLUDE_FROM_ALL)
set_property(TARGET capstone-static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")

set(LINK_LIBS ${LINK_LIBS} capstone-static ${ALSA_LIBRARIES})
if(USE_SDL)
set(LINK_LIBS ${LINK_LIBS} SDL2-static)
if(USE_SYSTEM_SDL2)
set(LINK_LIBS ${LINK_LIBS} PkgConfig::SDL2)
else()
set(LINK_LIBS ${LINK_LIBS} SDL2-static)
endif()
endif()

target_link_libraries(${PROJECT_NAME} ${LINK_LIBS})
Expand All @@ -434,3 +469,6 @@ elseif(EMSCRIPTEN)
${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin"
BUNDLE DESTINATION "${CMAKE_INSTALL_PREFIX}/Applications")
46 changes: 46 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
pkgs ? import <nixpkgs> {},
lib ? pkgs.lib
}:

pkgs.stdenv.mkDerivation {
pname = "skyemu";
version = "3-unstable-2025-01-25";

src = ./.;

postPatch = ''
# Nixpkgs does not support macOS universal builds
substituteInPlace CMakeLists.txt \
--replace-fail "set(CMAKE_OSX_ARCHITECTURES" "#"
'';

nativeBuildInputs = with pkgs; [
cmake
ninja
pkg-config
];

buildInputs = with pkgs; [
curl
openssl
SDL2
]
++ lib.optionals stdenv.hostPlatform.isLinux (with pkgs; [
alsa-lib
xorg.libXcursor
xorg.libXi
xorg.xinput
]);

cmakeFlags = [
(lib.cmakeBool "USE_SYSTEM_CURL" true)
(lib.cmakeBool "USE_SYSTEM_OPENSSL" true)
(lib.cmakeBool "USE_SYSTEM_SDL2" true)
];

meta = {
mainProgram = "SkyEmu";
license = lib.licenses.mit;
};
}
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
package = pkgs.callPackage ./default.nix {};
in {
packages.default = package;
apps.default = {
type = "app";
program =
if pkgs.stdenv.isDarwin then
"${package}/Applications/SkyEmu.app/Contents/MacOS/SkyEmu"
else "${package}/bin/SkyEmu";
};
});
}

0 comments on commit 15cc273

Please sign in to comment.