Skip to content

Commit 1296638

Browse files
authored
Make __file__ work in config file (#23973)
As of today folks who want to find stuff relative to the config file itself need to do `os.getenv('EM_CONFIG')` which is pretty hacky.
1 parent 2c822cd commit 1296638

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ See docs/process.md for more on how version tagging works.
2424
source map : use `-sSOURCE_MAP_PREFIXES=["<old>=<new>"]` with `-gsource-map`.
2525
Alternatively, you can now embed the sources content into the source map file
2626
using `-gsource-map=inline`. (#23741)
27+
- The python `__file__` builtin now works in the emscripten config file.
28+
(#23973)
2729

2830
4.0.5 - 03/12/25
2931
----------------

test/test_other.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,17 @@ def test_em_config_missing_arg(self):
845845
out = self.expect_fail([EMCC, '--em-config'])
846846
self.assertContained('error: --em-config must be followed by a filename', out)
847847

848+
def test_em_config_filename(self):
849+
create_file('myconfig.py', f'''
850+
LLVM_ROOT = '{config.LLVM_ROOT}'
851+
BINARYEN_ROOT = '{config.BINARYEN_ROOT}'
852+
CACHE = '{os.path.abspath("cache")}'
853+
print("filename", __file__)
854+
''')
855+
proc = self.run_process([EMAR, '--em-config', 'myconfig.py', '--version'], stdout=PIPE, stderr=PIPE)
856+
self.assertContained('LLVM', proc.stdout)
857+
self.assertContained('filename myconfig.py', proc.stdout)
858+
848859
def test_emsize(self):
849860
# test binaryen generated by running:
850861
# emcc test/hello_world.c -Oz --closure 1 -o test/other/test_emsize.js

tools/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def parse_config_file():
9797
9898
Also check EM_<KEY> environment variables to override specific config keys.
9999
"""
100-
config = {}
100+
config = {'__file__': EM_CONFIG}
101101
config_text = utils.read_file(EM_CONFIG)
102102
try:
103103
exec(config_text, config)

0 commit comments

Comments
 (0)