Skip to content

Commit

Permalink
Generate PowerShell wrappers (emscripten-core#20416)
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser authored Nov 1, 2023
1 parent 4de9942 commit a7d1f86
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ coverage.xml

# Test output
/out/

# All the ps1 files are generated by bootstrap...
*.ps1
# ...except the templates.
!/tools/run_python.ps1
!/tools/run_python_compiler.ps1
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ See docs/process.md for more on how version tagging works.
- Default `MIN_CHROME_VERSION` was increased from 75 to 85 and default
`MIN_FIREFOX_VERSION` was increased from 68 to 79 to allow Emscripten
to use some ES2021 features for smaller JavaScript code size. (#20549)
- Emscripten now generates PowerShell wrappers for its CLIs. This allows to
sidestep some of the issues with legacy cmd.exe, but developers must
explicitly opt-in to running PowerShell scripts in system settings or
via the `Set-ExecutionPolicy` command. (#20416)

3.1.47 - 10/09/23
-----------------
Expand Down
8 changes: 8 additions & 0 deletions tools/maint/create_entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,22 @@ def main():
def generate_entry_points(cmd, path):
sh_file = path + '.sh'
bat_file = path + '.bat'
ps1_file = path + '.ps1'
with open(sh_file) as f:
sh_file = f.read()
with open(bat_file) as f:
bat_file = f.read()
with open(ps1_file) as f:
ps1_file = f.read()

for entry_point in cmd:
sh_data = sh_file
bat_data = bat_file
ps1_data = ps1_file
if entry_point in entry_remap:
sh_data = sh_data.replace('$0', '$(dirname $0)/' + entry_remap[entry_point])
bat_data = bat_data.replace('%~n0', entry_remap[entry_point].replace('/', '\\'))
ps1_data = ps1_data.replace(r"$MyInvocation.MyCommand.Path -replace '\.ps1$', '.py'", fr'"$PSScriptRoot/{entry_remap[entry_point]}.py"')

out_sh_file = os.path.join(root_dir, entry_point)
with open(out_sh_file, 'w') as f:
Expand All @@ -86,6 +91,9 @@ def generate_entry_points(cmd, path):
with open(os.path.join(root_dir, entry_point + '.bat'), 'w') as f:
f.write(bat_data)

with open(os.path.join(root_dir, entry_point + '.ps1'), 'w') as f:
f.write(ps1_data)

generate_entry_points(entry_points, os.path.join(tools_dir, 'run_python'))
generate_entry_points(compiler_entry_points, os.path.join(tools_dir, 'run_python_compiler'))

Expand Down
32 changes: 32 additions & 0 deletions tools/run_python.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
#
# Entry point for running python scripts on Windows systems via PowerShell.
#
# Automatically generated by `create_entry_points.py`; DO NOT EDIT.
#
# To make modifications to this file, edit `tools/run_python.ps1` and then run
# `tools/maint/create_entry_points.py`

$ErrorActionPreference = 'Stop'

$launcher = $env:EMSDK_PYTHON
$launcherArgs = @()
if (!$launcher) {
# Use the global Python launcher that manages all Python installations.
$launcher = Get-Command py.exe
# ...but ask for Python 3
$launcherArgs += '-3'
}

# Ignore PYTHON* environment variables.
$launcherArgs += '-E'
# -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
# of cpython used in cross compilation via setup.py, so remove it explicitly.
Remove-Item -ErrorAction Ignore Env:\_PYTHON_SYSCONFIGDATA_NAME

$pythonScript = $MyInvocation.MyCommand.Path -replace '\.ps1$', '.py'

& $launcher $launcherArgs $pythonScript $MyInvocation.UnboundArguments
38 changes: 38 additions & 0 deletions tools/run_python_compiler.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
#
# Entry point for running python scripts on Windows systems via PowerShell.
#
# Automatically generated by `create_entry_points.py`; DO NOT EDIT.
#
# To make modifications to this file, edit `tools/run_python.ps1` and then run
# `tools/maint/create_entry_points.py`

$ErrorActionPreference = 'Stop'

$launcher = $env:EMSDK_PYTHON
$launcherArgs = @()
if (!$launcher) {
# Use the global Python launcher that manages all Python installations.
$launcher = Get-Command py.exe
# ...but ask for Python 3
$launcherArgs += '-3'
}

# Ignore PYTHON* environment variables.
$launcherArgs += '-E'
# -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
# of cpython used in cross compilation via setup.py, so remove it explicitly.
Remove-Item -ErrorAction Ignore Env:\_PYTHON_SYSCONFIGDATA_NAME

$pythonScript = $MyInvocation.MyCommand.Path -replace '\.ps1$', '.py'

if ($env:_EMCC_CCACHE) {
# prepend current launcher to launcherArgs and use ccache as the new launcher
$launcherArgs = @($launcher) + $launcherArgs
$launcher = Get-Command ccache.exe
}

& $launcher $launcherArgs $pythonScript $MyInvocation.UnboundArguments

0 comments on commit a7d1f86

Please sign in to comment.