diff --git a/CMakeLists.txt b/CMakeLists.txt index c01c04782..081c7dd1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,13 +24,50 @@ project(86Box HOMEPAGE_URL "https://86box.net" LANGUAGES C CXX) -if(WIN32 AND VCPKG_TOOLCHAIN) - if(VCPKG_TARGET_TRIPLET MATCHES "-windows-static$") - # `-static` triplet, use the statically linked CRT - set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +include(CPack) +include(CMakeDependentOption) + +# Basic build options +if(WIN32) + if(VCPKG_TOOLCHAIN) + # For vcpkg builds we have to respect the linking method used by the + # specified triplet. + set(NO_STATIC_OPTION ON) + if(VCPKG_TARGET_TRIPLET MATCHES "-windows-static$") + # `-static` triplet, use static linking + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + set(STATIC_BUILD ON) + elseif(VCPKG_TARGET_TRIPLET MATCHES "-windows-static-md$") + # `-static-md` triplet, use static linking with dynamic CRT + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") + set(STATIC_BUILD ON) + elseif() + # Regular triplet, use dynamic linking + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") + set(STATIC_BUILD OFF) + endif() + endif() + + # Prefer static builds on Windows + set(PREFER_STATIC ON) + + # Default value for the `WIN32` target property, which specifies whether + # to build the application for the Windows GUI or console subsystem + option(CMAKE_WIN32_EXECUTABLE "Build a Windows GUI executable" ON) +else() + # Prefer dynamic builds everywhere else + set(PREFER_STATIC OFF) +endif() + +if(APPLE) + option(CMAKE_MACOSX_BUNDLE "Build a macOS bundle (.app)" ON) +endif() + +if(NOT NO_STATIC_OPTION) + if(PREFER_STATIC) + option(STATIC_BUILD "Static build" ON) else() - # Regular triplet (or `-static-md`), use the dynamically linked CRT - set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") + option(STATIC_BUILD "Static build" OFF) endif() endif() @@ -42,10 +79,6 @@ if (NOT ARCH) set(ARCH unknown) endif() -include(CPack) - -include(CMakeDependentOption) - add_compile_definitions(CMAKE) add_compile_definitions("$<$:DEBUG>") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ee6f316b7..c3ed435d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,9 +64,11 @@ if(WIN32 AND ARCH STREQUAL "i386") endif() endif() -if(MINGW) - target_link_options(86Box PRIVATE "-static") - set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".dll.a") +if(STATIC_BUILD) + if(MINGW OR UNIX) + target_link_options(86Box PRIVATE "-static") + set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") + endif() endif() if(APPLE) @@ -91,7 +93,7 @@ target_link_libraries(86Box ${OPENAL_LIBRARY}) find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) -if(WIN32 AND TARGET SDL2::SDL2-static) +if(STATIC_BUILD AND TARGET SDL2::SDL2-static) target_link_libraries(86Box SDL2::SDL2-static) elseif(TARGET SDL2::SDL2) target_link_libraries(86Box SDL2::SDL2) @@ -143,7 +145,7 @@ if(MINITRACE) target_link_libraries(86Box minitrace) endif() -if(WIN32 OR APPLE) +if(WIN32 OR (APPLE AND CMAKE_MACOSX_BUNDLE)) # Copy the binary to the root of the install prefix on Windows and macOS install(TARGETS 86Box DESTINATION ".") else()