Skip to content

Commit

Permalink
Merge pull request #2039 from mingwandroid/2.1.x-experimental-cli
Browse files Browse the repository at this point in the history
Fix {cli,gui}-{32,64}.exe for spaces in the final exe path
  • Loading branch information
msarahan authored May 18, 2017
2 parents b9b3fad + b700b8b commit 77cbb74
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 19 deletions.
Binary file removed conda_build/a.exe
Binary file not shown.
Binary file modified conda_build/cli-32.exe
Binary file not shown.
Binary file modified conda_build/cli-64.exe
Binary file not shown.
Binary file modified conda_build/gui-32.exe
Binary file not shown.
Binary file modified conda_build/gui-64.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion conda_build/launcher_sources/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for _BITS in 64 32; do
# You *could* use MSVC 2008 here, but you'd end up with much larger (~230k) executables.
# cl.exe -opt:nowin98 -D NDEBUG -D "GUI=0" -D "WIN32_LEAN_AND_MEAN" -ZI -Gy -MT -MERGE launcher.c -Os -link -MACHINE:x64 -SUBSYSTEM:CONSOLE version.lib advapi32.lib shell32.lib
PATH=/mingw${_BITS}/bin:$PATH gcc -O2 -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -DMINGW_HAS_SECURE_API ${CPPFLAGS} launcher.c -c -o ${_TYPE}-${_BITS}.o
PATH=/mingw${_BITS}/bin:$PATH gcc -Wl,-s --static -static-libgcc -municode ${LDFLAGS} ${_TYPE}-${_BITS}.o resources-${_BITS}.res -lversion -o ${_TYPE}-${_BITS}.exe
PATH=/mingw${_BITS}/bin:$PATH gcc -Wl,-s --static -static-libgcc -municode ${LDFLAGS} ${_TYPE}-${_BITS}.o resources-${_BITS}.res -o ${_TYPE}-${_BITS}.exe
done
done
ls -l *.exe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- launcher.c.orig 2017-05-15 20:33:34.000000000 -0500
+++ launcher.c 2017-05-17 10:58:18.524041300 -0500
@@ -8,11 +8,43 @@
--- launcher.c.orig 2017-05-18 05:43:28.000000000 -0500
+++ launcher.c 2017-05-18 05:42:37.000000000 -0500
@@ -8,11 +8,72 @@
* Curt Hagenlocher (job management)
*/

Expand All @@ -24,16 +24,45 @@
#include <stdio.h>
#include <tchar.h>

+/* Replacement for setuptools {cli,gui}-{32,64}.exe programs with support
+ * for (16-bit) Unicode and spaces in paths. A minor modification of:
+ * https://raw.githubusercontent.com/python/cpython/3.6/PC/launcher.c
+ * To build with MSYS2/mingw-w64, from the MSYS2 shell:
+ * PATH=/mingw64/bin:$PATH gcc -mconsole -O2 -Wl,-s -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -DMINGW_HAS_SECURE_API launcher.c -lversion --static -static-libgcc -o cli-64.exe
+ * PATH=/mingw32/bin:$PATH gcc -mconsole -O2 -Wl,-s -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -DMINGW_HAS_SECURE_API launcher.c -lversion --static -static-libgcc -o cli-32.exe
+ * PATH=/mingw64/bin:$PATH gcc -mwindows -municode -O2 -Wl,-s -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -D_WINDOWS -DMINGW_HAS_SECURE_API launcher.c -lversion --static -static-libgcc -o gui-64.exe
+ * PATH=/mingw32/bin:$PATH gcc -mwindows -municode -O2 -Wl,-s -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -D_WINDOWS -DMINGW_HAS_SECURE_API launcher.c -lversion --static -static-libgcc -o gui-32.exe
+/* To build on MSYS2 use the following script:
+#!/usr/bin/env bash
+
+wget -c https://raw.githubusercontent.com/python/cpython/3.6/PC/launcher.c -O launcher.c
+patch -p0 < $(dirname ${BASH_SOURCE[0]})/cpython-launcher-c-mods-for-setuptools.patch
+RCFILE=$(dirname ${BASH_SOURCE[0]})/resources.rc
+[[ -f ${RCFILE} ]] && rm -f ${RCFILE}
+echo "#include \"winuser.h\"" > ${RCFILE}
+echo "1 RT_MANIFEST manifest.xml" >> ${RCFILE}
+for _BITS in 64 32; do
+ [[ -f resources-${_BITS}.res ]] && rm -f resources-${_BITS}.res
+ PATH=/mingw${_BITS}/bin:$PATH windres --input ${RCFILE} --output resources-${_BITS}.res --output-format=coff
+ for _TYPE in cli gui; do
+ if [[ ${_TYPE} == cli ]]; then
+ CPPFLAGS=
+ LDFLAGS=
+ else
+ CPPFLAGS="-D_WINDOWS -mwindows"
+ LDFLAGS="-mwindows"
+ fi
+ # You *could* use MSVC 2008 here, but you'd end up with much larger (~230k) executables.
+ # cl.exe -opt:nowin98 -D NDEBUG -D "GUI=0" -D "WIN32_LEAN_AND_MEAN" -ZI -Gy -MT -MERGE launcher.c -Os -link -MACHINE:x64 -SUBSYSTEM:CONSOLE version.lib advapi32.lib shell32.lib
+ PATH=/mingw${_BITS}/bin:$PATH gcc -O2 -DSCRIPT_WRAPPER -DUNICODE -D_UNICODE -DMINGW_HAS_SECURE_API ${CPPFLAGS} launcher.c -c -o ${_TYPE}-${_BITS}.o
+ PATH=/mingw${_BITS}/bin:$PATH gcc -Wl,-s --static -static-libgcc -municode ${LDFLAGS} ${_TYPE}-${_BITS}.o resources-${_BITS}.res -o ${_TYPE}-${_BITS}.exe
+ done
+done
+ls -l *.exe
+echo "Debug this from cmd.exe via:"
+echo "set PYLAUNCH_DEBUG=1"
+ */
+
+/* Previosuly GetFileVersionInfoW was used here, but I would rather save
+ * the 1.5k that costs. If we bring this back, add -lversion to the link
+ * command line.
+ */
+
+#define VERSION_HIGH 0
+#define VERSION_LOW 1
+
+/* Previously BUFSIZE was used for this but 256 is not enough for even 260
+ * ASCII characters and far too little for unicode (it is a char array, not
+ * a wchar_t array, though it needs to be even bigger than 260 wchar_ts as
Expand All @@ -44,7 +73,7 @@
#define BUFSIZE 256
#define MSGSIZE 1024

@@ -765,6 +797,25 @@
@@ -765,6 +826,25 @@

static COMMAND path_command;

Expand All @@ -70,7 +99,7 @@
static COMMAND * find_on_path(wchar_t * name)
{
wchar_t * pathext;
@@ -888,7 +939,7 @@
@@ -888,7 +968,7 @@

static BOOL
parse_shebang(wchar_t * shebang_line, int nchars, wchar_t ** command,
Expand All @@ -79,7 +108,7 @@
{
BOOL rc = FALSE;
SHEBANG * vpp;
@@ -898,11 +949,40 @@
@@ -898,11 +978,40 @@
wchar_t * endp = shebang_line + nchars - 1;
COMMAND * cp;
wchar_t * skipped;
Expand Down Expand Up @@ -120,7 +149,7 @@
if ((*shebang_line++ == L'#') && (*shebang_line++ == L'!')) {
shebang_line = skip_whitespace(shebang_line);
if (*shebang_line) {
@@ -1121,7 +1201,7 @@
@@ -1121,7 +1230,7 @@
*/
FILE * fp;
errno_t rc = _wfopen_s(&fp, *argv, L"rb");
Expand All @@ -129,7 +158,7 @@
wchar_t shebang_line[BUFSIZE + 1];
size_t read;
char *p;
@@ -1138,7 +1218,7 @@
@@ -1138,7 +1247,7 @@
INSTALLED_PYTHON * ip;

if (rc == 0) {
Expand All @@ -138,7 +167,7 @@
debug(L"maybe_handle_shebang: read %d bytes\n", read);
fclose(fp);

@@ -1163,7 +1243,7 @@
@@ -1163,7 +1272,7 @@
bom->code_page);
start = &buffer[bom->length];
}
Expand All @@ -147,7 +176,7 @@
/*
* If no CR or LF was found in the heading,
* we assume it's not a shebang file.
@@ -1247,7 +1327,7 @@
@@ -1247,7 +1356,7 @@
if (nchars > 0) {
shebang_line[--nchars] = L'\0';
is_virt = parse_shebang(shebang_line, nchars, &command,
Expand All @@ -156,3 +185,64 @@
if (command != NULL) {
debug(L"parse_shebang: found command: %ls\n", command);
if (!is_virt) {
@@ -1368,6 +1477,7 @@
int newlen;
wchar_t * newcommand;
wchar_t * av[2];
+ int quoted = 0;
#endif

setvbuf(stderr, (char *)NULL, _IONBF, 0);
@@ -1405,6 +1515,7 @@
}
}
plen = GetModuleFileNameW(NULL, launcher_ini_path, MAX_PATH);
+/*
size = GetFileVersionInfoSizeW(launcher_ini_path, &size);
if (size == 0) {
winerror(GetLastError(), message, MSGSIZE);
@@ -1430,6 +1541,9 @@
free(version_data);
}
}
+*/
+ version_high = VERSION_HIGH;
+ version_low = VERSION_LOW;
p = wcsrchr(launcher_ini_path, L'\\');
if (p == NULL) {
debug(L"GetModuleFileNameW returned value has no backslash: %ls\n",
@@ -1464,13 +1578,23 @@
locate_wrapped_script();

/* Add the wrapped script to the start of command */
- newlen = wcslen(wrapped_script_path) + wcslen(command) + 2; /* ' ' + NUL */
+ p = wcsrchr(wrapped_script_path, L' ');
+ if (p != NULL)
+ quoted = 1;
+ newlen = wcslen(wrapped_script_path) + wcslen(command) + 2 + (2 * quoted); /* ' ' + NUL */
newcommand = malloc(sizeof(wchar_t) * newlen);
if (!newcommand) {
error(RC_NO_MEMORY, L"Could not allocate new command line");
}
else {
- wcscpy_s(newcommand, newlen, wrapped_script_path);
+ newcommand[0] = L'\0';
+ if (quoted) {
+ wcscpy_s(newcommand, newlen, L"\"");
+ }
+ wcscat_s(newcommand, newlen, wrapped_script_path);
+ if (quoted) {
+ wcscat_s(newcommand, newlen, L"\"");
+ }
wcscat_s(newcommand, newlen, L" ");
wcscat_s(newcommand, newlen, command);
debug(L"Running wrapped script with command line '%ls'\n", newcommand);
@@ -1535,7 +1659,7 @@

get_version_info(version_text, MAX_PATH);
fwprintf(stdout, L"\
-Python Launcher for Windows Version %ls\n\n", version_text);
+Python Launcher for Windows (Anaconda/Setuptools variant) Version %ls\n\n", version_text);
fwprintf(stdout, L"\
usage: %ls [ launcher-arguments ] [ python-arguments ] script [ script-arguments ]\n\n", argv[0]);
fputws(L"\

0 comments on commit 77cbb74

Please sign in to comment.