Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove python_selector.py in favor of run_python.sh #10729

Merged
merged 1 commit into from
Mar 20, 2020
Merged

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Mar 19, 2020

Ubuntu 20.04 dropped the /usr/bin/python symlink so it is no longer
possible use /usr/bin/env python in launcher scripts and then
rely on python_selector.py to select a different python version.

Instead we go back to age-old method of using shell wrapper script.

Here I create a single wrapper script called run_python.sh which
runs a commmand under the best version of python it can find. Each
unsiffixed command is then a symbol link to this shell script. Since
windows users use the .bat files it shouldn't matter whether or not
these symlinks work on windows.

Fixes #10726

@sbc100 sbc100 requested a review from kripken March 19, 2020 16:20
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm, while we assume most windows users use the bat files, they should still be able to do python on the python files?

@@ -2464,12 +2464,12 @@ def test_mmap_file(self):
def test_emrun_info(self):
if not has_browser():
self.skipTest('need a browser')
result = run_process([PYTHON, path_from_root('emrun'), '--system_info', '--browser_info'], stdout=PIPE).stdout
result = run_process([PYTHON, path_from_root('emrun.py'), '--system_info', '--browser_info'], stdout=PIPE).stdout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change necessary? if it is that seems like a breaking change. if it isn't then why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this change is a slightly breaking change. You can no longer run python emcc (because emcc is now itself a shell script that runs python). You can still run ./emcc.py directly or python emcc.py if you like.

I think this is the best approach. The python_selector method where python re-invokes itself is kind of nasty. If I run python2 emcc I kind of expect it to run under python2 not invoke a python3 subprocess to run itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm. is there some way we can show a clear error on python emcc / emranlib / etc., like keeping the files around but only containing a print of an error? or if that's not possible, maybe a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not deleting those files. They are now symbolic links to run_python.sh. So they exist by they are shell scripts.

I'm not sure we can have python <some_shell_script> do anything useful... but let me see...

@@ -1,19 +1,17 @@
#!/usr/bin/env python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is em++.py special among the tools? seems like it's getting different treatment in this PR than the others and it's not obvious why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because em++ was using the python_selector in a slightly different way. It was using it to redirect to emcc.py as well as to choose the python version.

With this change, once python is running we assume its the correct version, we never try to re-invoke python. So this change means that em++ now runs emcc directly, without re-invoking python.

@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 19, 2020

To confirm, while we assume most windows users use the bat files, they should still be able to do python on the python files?

Yes. One can always run python on the .py files. This change just means you can't run python on the non-py files (just like you can't run python on the .bat files).

@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 19, 2020

The essential problem here is that there is no #! line that we can use to correctly run any version of python that works on all systems. So keeping these files in python format is not an option (as far as I can tell).

@@ -2464,12 +2464,12 @@ def test_mmap_file(self):
def test_emrun_info(self):
if not has_browser():
self.skipTest('need a browser')
result = run_process([PYTHON, path_from_root('emrun'), '--system_info', '--browser_info'], stdout=PIPE).stdout
result = run_process([PYTHON, path_from_root('emrun.py'), '--system_info', '--browser_info'], stdout=PIPE).stdout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm. is there some way we can show a clear error on python emcc / emranlib / etc., like keeping the files around but only containing a print of an error? or if that's not possible, maybe a comment?

Ubuntu 20.04 dropped the /usr/bin/python symlink so it is no longer
possible use `/usr/bin/env python` in launcher scripts and then
rely on python_selector.py to select a different python version.

Instead we go back to age-old method of using shell wrapper script.

Here I create a single wrapper script called run_python.sh which
runs a commmand under the best version of python it can find.  Each
unsiffixed command is then a symbol link to this shell script.  Since
windows users use the .bat files it shouldn't matter whether or not
these symlinks work on windows.

Fixes #10726
@sbc100 sbc100 merged commit d5c77b0 into master Mar 20, 2020
@sbc100 sbc100 deleted the sh_entry_points branch March 20, 2020 18:23
sbc100 added a commit that referenced this pull request Mar 21, 2020
The non-py version of these scripts were converted to shell scripts
in #10729.  The .bat files run python explicitly so can directly
reference .py versions of these scripts.

Also, move em-config to em-config.py to match all the others.
sbc100 added a commit that referenced this pull request Mar 21, 2020
The non-py version of these scripts were converted to shell scripts
in #10729.  The .bat files run python explicitly so can directly
reference .py versions of these scripts.

Also, move em-config to em-config.py to match all the others.
@diegocr
Copy link
Contributor

diegocr commented Apr 12, 2020

Have just upgraded to 1.39.12 and found this PR broke running the compiler on my Windows/MinGW environment :-(

$ emcc --version
/c/emsdk/upstream/emscripten/emcc: line 1: tools/run_python.sh: No such file or directory

@diegocr
Copy link
Contributor

diegocr commented Apr 12, 2020

Just realized emcc etc should be symbolic links now.. so is there some post-process that is failing here? :-/

Anyway, i made this script to workaround it...

#!/bin/sh

base_dir=$(dirname "$0")

find $base_dir -type f -name "*.orig" -o -name "*.rej" | xargs -l rm

emsdk update
emsdk install latest

for p in `ls $base_dir/*.diff`; do cd $EMSCRIPTEN && patch -p1 < $p; done

files=("em++" "emar" "emcc" "emcmake" "emconfigure" "emmake" "emranlib" "emrun" "emscons")
for f in "${files[@]}"
do
    rm -f "$EMSCRIPTEN/$f"
    ln -s "$EMSCRIPTEN/tools/run_python.sh" "$EMSCRIPTEN/$f"
done

sed -i -e 's-which-& 2>/dev/null-g' "$EMSCRIPTEN/tools/run_python.sh"

Later on, the first time i tried to compile something i got this error:

$ ./build.sh
patching file dcraw.tmp.c
emcc: error: 'C:/emsdk/upstream/bin\clang++.exe --version' failed: [Error 5] Acceso denegado

On the next attempt though, it did worked fine

$ ./build.sh
patching file dcraw.tmp.c
shared:INFO: (Emscripten: system change: 1.39.12|C:/emsdk/upstream/bin|11.0_wasm vs 1.39.11|C:/emsdk/upstream/bin|11.0_wasm, clearing cache)
shared:INFO: (Emscripten: Running sanity checks)
emcc:WARNING: --llvm-lto ignored when using llvm backend
....[snip]
cache:INFO: generating system asset: is_vanilla.txt... (this will be cached in "C:\msys\1.0\home\dc\.emscripten_cache\is_vanilla.txt" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libc.a... (this will be cached in "C:\msys\1.0\home\dc\.emscripten_cache\wasm-lto\libc.a" for subsequent builds)
cache:INFO:  - ok
cache:INFO: generating system library: libcompiler_rt.a... (this will be cached in "C:\msys\1.0\home\dc\.emscripten_cache\wasm-lto\libcompiler_rt.a" for subsequent builds)
cache:INFO:  - ok
...[snip]

If you guys want me to do some further troubleshooting, a PR for my env, etc lmk.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 12, 2020

Sadly we don't really have the time or resources to test in MINGW.

I think the problem here is that you are installing the windows SDK, but then using the compiler not via the .bat file launchers but via the shell script symbolic links.

If you want to fix this we might need your help. I can think of possible options:

  1. Try to fix the windows archive extraction in them emsdk to do the same thing as what ln -s is doing. BTW do you know what you ln -s command is doing on the windows filesystem? Do some windows filesystems have actual symbolic link support now?

  2. Don't include the shell scripts at all in the windows installer. Can you run emcc.bat from the bash shell? Will your bash shell automatically add the .bat if you omit it? I'm tempted to choose this option because I don't really want to mess with symbolic links or bash shell scripts on windows machines. (Conversely we should remove the .bat file launchers from the linux and mac SDKs).

@parasti
Copy link
Contributor

parasti commented Apr 13, 2020

Also just found out that emcc now bails with an error on MSYS. Worked fine prior to this.

emcc isn't a symlink for me. It is a small text file that literally contains the string "tools/run_python.sh". This is how Git handles symlinks on Windows by default. https://github.com/git-for-windows/git/wiki/Symbolic-Links

In my opinion it is always a mistake use symlinks in distribution. Just don't do it. A small shell script that finds the run_python.sh script from anywhere will work just fine.

@parasti
Copy link
Contributor

parasti commented Apr 13, 2020

Here's an idea with a hypothetical tools/find_python.sh script:

#!/bin/sh
abspath="$(type -p $0)"
emroot=$(dirname "$abspath")
python=$("$emroot"/tools/find_python.sh)
exec $python "$abspath.py" "$@"

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 13, 2020

Does simply removing emcc work for you ? (i.e. using emcc.bat instead?)

I think it makes sense to include .bat files in the windows SDK and shell scripts in the linux/mac SDK but including both seems unnecessary/wrong.

I agree symbol links are bad idea on windows and we should not be using them. But on linux/mac there is nothing wrong with symbolic links (clang is symlink to clang-11, for example).

@parasti
Copy link
Contributor

parasti commented Apr 13, 2020

No, .bat files do not work in bash.

You're making a mistaken assumption that Windows users want to use cmd.exe. I used Linux for a decade. I want to (and can) use bash. MSYS2 and Git Bash bundled with Git for Windows provides a POSIX compatibility layer over the C runtime and filesystem (similar to Emscripten). I can even configure VS Code to run tasks and launch terminals with bash, because Microsoft supports that out of the box. It's mind blowing the first time you try it, but it's a standard thing these days. Just don't use symlinks.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 13, 2020

I'm curious how other software interacts with MSYS. For example windows python. If you type python in bash does is know to looks for python.exe? Or do you explicitly need to type the `.exe on the end?

Also, if you can't run .bat files doesn't what about software which use .bat files as the entry points? Does that just not work with MSYS?

I'm also curious how @diegocr fix works, given that is does use symlinks (it seems to replace the fake symlinks with real ones) and also seems to solve the problem. What is the ln -s command actually doing in your script @diegocr ?

@parasti
Copy link
Contributor

parasti commented Apr 13, 2020

I'm curious how other software interacts with MSYS. For example windows python. If you type python in bash does is know to looks for python.exe? Or do you explicitly need to type the `.exe on the end?

I just type python like I would on Linux.

Also, if you can't run .bat files doesn't what about software which use .bat files as the entry points? Does that just not work with MSYS?

It doesn't. If I come across software like that, which is actually rare, I make a launcher shell script for it. I have no use for .bat files except as reference for the shell script implementation.

I'm also curious how @diegocr fix works, given that is does use symlinks (it seems to replace the fake symlinks with real ones) and also seems to solve the problem.

ln -s on MSYS does a copy by default.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 13, 2020

Ok, sounds like its worth fixing the shell launchers to work under MSYS.

Are you mostly concerned about the emsdk use case? Or do you want git clone of emscripten to work out-of-the-box too?

@parasti
Copy link
Contributor

parasti commented Apr 14, 2020

Ths way it's set up is perfect. Symlinks are the only problem, really.

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 14, 2020

Would you be OK with the emsdk image containing the fix to goes do you think it needs to go all the way back the git content?

@sbc100
Copy link
Collaborator Author

sbc100 commented Apr 14, 2020

OK, lets try this: #10921

@diegocr
Copy link
Contributor

diegocr commented Apr 18, 2020

Hi guys, sorry for the late reply - I've just installed 1.39.13 and all is good now, Thanks!

PYTHON=$(which python3)
fi

if [ -z $PYTHON ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's possible for Python to be installed in a path with spaces, which breaks this line: #11005

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot run on Ubuntu 20.04 (devel) due to python2 interpreter rename
5 participants