From 46a8b40b6f71abc696bf460f74890fe6dfb0fb23 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Thu, 20 Oct 2022 19:29:09 +0700 Subject: [PATCH 1/2] convert main executable to .dll and add .exe and .com launchers (#772) * convert main executable to .dll and add .exe and .com launchers --- .github/workflows/release.yml | 1 + src/CMakeLists.txt | 82 ++++++++++++++++------------- src/i_main.c | 8 +-- src/i_system.c | 37 ------------- win32/win_console_wrapper.c | 98 ----------------------------------- win32/win_launcher.c | 22 ++++++++ 6 files changed, 73 insertions(+), 175 deletions(-) delete mode 100644 win32/win_console_wrapper.c create mode 100644 win32/win_launcher.c diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4b66ed84..1d78f2fc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,6 +40,7 @@ jobs: mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_net + - uses: actions/checkout@v2 - name: Configure diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 819d3952..697bda70 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -139,19 +139,55 @@ if (WIN32) list(APPEND WOOF_LIBRARIES winmm) endif() -# Some platforms have SDL2main, others don't. -if(SDL2_MAIN_LIBRARY) - list(APPEND WOOF_LIBRARIES SDL2::SDL2main) -endif() - +# Standard target definition if(WIN32) + add_library(woof SHARED EXCLUDE_FROM_ALL ${WOOF_SOURCES}) + target_compile_definitions(woof PRIVATE WIN_LAUNCHER) + # Some platforms have SDL2main, others don't. + if(SDL2_MAIN_LIBRARY) + list(APPEND WIN_LAUNCHER_LIBS SDL2::SDL2main) + endif() + list(APPEND WIN_LAUNCHER_LIBS SDL2::SDL2 woof) + + add_executable(woof-com WIN32 ../win32/win_launcher.c) + target_woof_settings(woof-com) + target_link_libraries(woof-com PRIVATE ${WIN_LAUNCHER_LIBS}) + if(MSVC) + # MSVC tries to supply a default manifest and complains when it finds + # ours unless we specifically tell it not to. + target_link_options(woof-com PRIVATE "/MANIFEST:NO" "/SUBSYSTEM:CONSOLE") + else() + target_link_options(woof-com PRIVATE "-mconsole") + endif() + set_target_properties(woof-com PROPERTIES + RUNTIME_OUTPUT_NAME "woof" + SUFFIX ".com" + ) + # Stamp out and compile resource file on Windows. configure_file(resource.rc.in "${CMAKE_CURRENT_BINARY_DIR}/resource.rc") - list(APPEND WOOF_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/resource.rc") + add_executable(woof-exe WIN32 + ../win32/win_launcher.c + "${CMAKE_CURRENT_BINARY_DIR}/resource.rc" + ) + target_woof_settings(woof-exe) + target_link_libraries(woof-exe PRIVATE ${WIN_LAUNCHER_LIBS}) + if(MSVC) + # MSVC tries to supply a default manifest and complains when it finds + # ours unless we specifically tell it not to. + target_link_options(woof-exe PRIVATE "/MANIFEST:NO") + endif() + set_target_properties(woof-exe PROPERTIES + RUNTIME_OUTPUT_NAME "woof" + SUFFIX ".exe" + ) +else() + # Some platforms have SDL2main, others don't. + if(SDL2_MAIN_LIBRARY) + list(APPEND WOOF_LIBRARIES SDL2::SDL2main) + endif() + add_executable(woof ${WOOF_SOURCES}) endif() - -# Standard target definition -add_executable(woof WIN32 ${WOOF_SOURCES}) target_woof_settings(woof) target_include_directories(woof PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../") @@ -168,14 +204,6 @@ endif() target_link_libraries(woof PRIVATE ${WOOF_LIBRARIES} SDL2::SDL2 SDL2::mixer SDL2::net opl textscreen) -if(MSVC) - # MSVC tries to supply a default manifest and complains when it finds ours - # unless we specifically tell it not to. - set_target_properties(woof PROPERTIES LINK_FLAGS "/MANIFEST:NO") - # Enable console for debug build. - set_target_properties(woof PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") -endif() - # Optional features. # # Our defines are not namespaced, so we pass them at compile-time instead of @@ -224,23 +252,6 @@ target_link_libraries(woof-setup PRIVATE ${SETUP_LIBRARIES} if(MSVC) set_target_properties(woof-setup PROPERTIES LINK_FLAGS "/MANIFEST:NO") - set_target_properties(woof-setup PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") -endif() - -if(WIN32) - add_executable(conredir WIN32 "../win32/win_console_wrapper.c") - target_woof_settings(conredir) - if(MSVC) - set_target_properties(conredir PROPERTIES - LINK_FLAGS "/MANIFEST:NO /SUBSYSTEM:CONSOLE") - else() - set_target_properties(conredir PROPERTIES LINK_FLAGS "-municode -mconsole") - endif() - - add_custom_command(TARGET conredir POST_BUILD - COMMAND "${CMAKE_COMMAND}" -E copy - "$/conredir.exe" - "$/woof.com" VERBATIM) endif() # Install @@ -275,8 +286,7 @@ endforeach() # Files to package in our distribution. if(WIN32) - install(TARGETS woof woof-setup RUNTIME DESTINATION .) - install(FILES "$/woof.com" DESTINATION .) + install(TARGETS woof woof-exe woof-com woof-setup RUNTIME DESTINATION .) else() install(TARGETS woof woof-setup RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() diff --git a/src/i_main.c b/src/i_main.c index 6c5364d8..64cff76d 100644 --- a/src/i_main.c +++ b/src/i_main.c @@ -103,15 +103,15 @@ static void I_Signal(void) void D_DoomMain(void); +#if defined(WIN_LAUNCHER) +__declspec(dllexport) int Woof_Main(int argc, char **argv) +#else int main(int argc, char **argv) +#endif { myargc = argc; myargv = argv; -#ifdef _WIN32 - I_WinConsole(); -#endif - //! // // Print the program version and exit. diff --git a/src/i_system.c b/src/i_system.c index 96598bfc..9e9cdbc4 100644 --- a/src/i_system.c +++ b/src/i_system.c @@ -121,43 +121,6 @@ static boolean I_ConsoleStdout(void) #endif } -#ifdef _WIN32 -static void ReopenConsoleHandle(DWORD std, FILE *stream) -{ - HANDLE handle = GetStdHandle(std); - DWORD lpmode = 0; - - if (GetConsoleMode(handle, &lpmode)) - { - freopen("CONOUT$", "wt", stream); - } -} - -boolean I_WinConsole(void) -{ - wchar_t console_env[4] = {0}; - - if (!GetEnvironmentVariableW(L"_started_from_console", console_env, 4)) - return false; - - if (wcsncmp(console_env, L"yes", 4)) - return false; - - SetEnvironmentVariableW(L"_started_from_console", NULL); - - if (!AttachConsole(ATTACH_PARENT_PROCESS)) - return false; - - // We have a console window. Redirect input/output streams to that console's - // low-level handles, so things that use stdio work later on. - - ReopenConsoleHandle(STD_OUTPUT_HANDLE, stdout); - ReopenConsoleHandle(STD_ERROR_HANDLE, stderr); - - return true; -} -#endif - // // I_Error // diff --git a/win32/win_console_wrapper.c b/win32/win_console_wrapper.c deleted file mode 100644 index e0c0b66d..00000000 --- a/win32/win_console_wrapper.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * conredir, a hack to get working console IO with Windows GUI applications - * - * Copyright (c) 2013, Martin Herkt - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include - -int wmain(int argc, wchar_t **argv, wchar_t **envp); - -static void cr_perror(const wchar_t *prefix) -{ - wchar_t *error; - - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&error, 0, NULL); - - fwprintf(stderr, L"%s: %s", prefix, error); - LocalFree(error); -} - -static int cr_runproc(wchar_t *name, wchar_t *cmdline) -{ - STARTUPINFOW si; - STARTUPINFOW our_si; - PROCESS_INFORMATION pi; - DWORD retval = 1; - - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); - si.dwFlags |= STARTF_USESTDHANDLES; - - // Copy the list of inherited CRT file descriptors to the new process - our_si.cb = sizeof(our_si); - GetStartupInfoW(&our_si); - si.lpReserved2 = our_si.lpReserved2; - si.cbReserved2 = our_si.cbReserved2; - - ZeroMemory(&pi, sizeof(pi)); - - if (!CreateProcessW(name, cmdline, NULL, NULL, TRUE, 0, - NULL, NULL, &si, &pi)) { - - cr_perror(L"CreateProcess"); - } else { - WaitForSingleObject(pi.hProcess, INFINITE); - GetExitCodeProcess(pi.hProcess, &retval); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - } - - return (int)retval; -} - -int wmain(int argc, wchar_t **argv, wchar_t **envp) -{ - wchar_t *cmd; - wchar_t exe[MAX_PATH]; - UINT orig_code_page; - int ret; - - cmd = GetCommandLineW(); - GetModuleFileNameW(NULL, exe, MAX_PATH); - wcscpy(wcsrchr(exe, '.') + 1, L"exe"); - - // Set an environment variable so the child process can tell whether it - // was started from this wrapper and attach to the console accordingly - SetEnvironmentVariableW(L"_started_from_console", L"yes"); - - orig_code_page = GetConsoleOutputCP(); - SetConsoleOutputCP(CP_UTF8); - - ret = cr_runproc(exe, cmd); - - SetConsoleOutputCP(orig_code_page); - - return ret; -} diff --git a/win32/win_launcher.c b/win32/win_launcher.c new file mode 100644 index 00000000..bb679d4d --- /dev/null +++ b/win32/win_launcher.c @@ -0,0 +1,22 @@ +// +// Copyright(C) 2022 Roman Fomin +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// + +#include "SDL.h" + +__declspec(dllexport) extern int Woof_Main(int argc, char** argv); + +int main(int argc, char** argv) +{ + return Woof_Main(argc, argv); +} From 810d0d9ebc009061eb93b2057a3abe7ba120d4f5 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Fri, 21 Oct 2022 02:08:41 +0700 Subject: [PATCH 2/2] update workflow files (deprecation warnings) (#774) --- .github/workflows/main.yml | 14 ++++++++------ .github/workflows/release.yml | 9 ++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a24e9c94..f4f7ea72 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,6 +3,8 @@ name: build on: push: branches: [ master ] + tags-ignore: ['*'] + paths-ignore: ['**.md'] pull_request: branches: [ master ] @@ -18,7 +20,7 @@ jobs: - name: Install dependencies run: sudo apt-get update && sudo apt-get install libfluidsynth-dev libsdl2-dev libsdl2-mixer-dev libsdl2-net-dev ninja-build - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Configure env: @@ -40,7 +42,7 @@ jobs: - name: Install dependencies run: sudo apt-get update && sudo apt-get install cppcheck - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Run cppcheck shell: bash @@ -68,7 +70,7 @@ jobs: mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_net - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Configure run: cmake -B build -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DENABLE_WERROR=ON @@ -82,7 +84,7 @@ jobs: cpack -G ZIP - name: Upload artifacts - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: Win64 path: build/*.zip @@ -91,7 +93,7 @@ jobs: runs-on: windows-2019 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Configure run: cmake -B build -T v141_xp -A Win32 -DENABLE_WERROR=ON @@ -109,7 +111,7 @@ jobs: cpack -G ZIP - name: Upload artifacts - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: WinXP path: build/Woof*.zip diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1d78f2fc..9de423a7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,14 +11,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Version - id: get_version - run: echo ::set-output name=VERSION::${GITHUB_REF##*_} + run: echo "VERSION=${GITHUB_REF##*_}" >> $GITHUB_ENV - name: Create Release id: create_release uses: softprops/action-gh-release@v1 with: - name: Woof! ${{ steps.get_version.outputs.VERSION }} + name: Woof! ${{ env.VERSION }} win64_msys2: runs-on: windows-latest @@ -41,7 +40,7 @@ jobs: mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_net - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Configure run: cmake -B build -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DENABLE_WERROR=ON @@ -65,7 +64,7 @@ jobs: runs-on: windows-2019 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Configure run: cmake -B build -T v141_xp -A Win32 -DENABLE_WERROR=ON