Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
cmake_minimum_required(VERSION 3.5)

project (PyStand)
project (PyStand LANGUAGES CXX RC)

option(PYSTAND_CONSOLE "Build PyStand as a console application." OFF)

# sources
add_executable(PyStand
WIN32
set(sources
PyStand.cpp
resource.rc
)

if (PYSTAND_CONSOLE)
add_executable(PyStand ${sources})
target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE)
else()
add_executable(PyStand WIN32 ${sources})
endif()

# static link
if (CMAKE_GENERATOR MATCHES "Visual Studio")
Expand Down
9 changes: 9 additions & 0 deletions PyStand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//---------------------------------------------------------------------
PyStand::~PyStand()
{
FreeLibrary(_hDLL);
}


Expand Down Expand Up @@ -308,13 +309,15 @@ const char *init_script =
" ctypes.windll.user32.MessageBoxW(None, str(msg), str(info), 0)\n"
" return 0\n"
"os.MessageBox = MessageBox\n"
#ifndef PYSTAND_CONSOLE
"try:\n"
" fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY)\n"
" fp = os.fdopen(fd, 'w')\n"
" sys.stdout = fp\n"
" sys.stderr = fp\n"
"except Exception as e:\n"
" pass\n"
#endif
"for n in ['lib', 'site-packages']:\n"
" test = os.path.join(PYSTAND_HOME, n)\n"
" if os.path.exists(test): sys.path.append(test)\n"
Expand All @@ -332,17 +335,22 @@ const char *init_script =
// main
//---------------------------------------------------------------------

#ifdef PYSTAND_CONSOLE
int main()
#else
//! flag: -static
//! src:
//! mode: win
//! int: objs
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
#endif
{
PyStand ps("runtime");
if (ps.DetectScript() != 0) {
return 3;
}
#ifndef PYSTAND_CONSOLE
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
Expand All @@ -357,6 +365,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
SetEnvironmentVariableA("PYSTAND_STDIN", fn.c_str());
}
}
#endif
int hr = ps.RunString(init_script);
// printf("finalize\n");
return hr;
Expand Down