Skip to content

Commit 16ede16

Browse files
authored
Set __progname global via static initializer (#16064)
Also sets the related globals `__progname_full`, `program_invocation_short_name`, and `program_invocation_name`.
1 parent ba84d40 commit 16ede16

File tree

7 files changed

+92
-1
lines changed

7 files changed

+92
-1
lines changed

src/library.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,19 @@ LibraryManager.library = {
34853485
err(UTF8ToString(str));
34863486
},
34873487

3488+
// Use program_invocation_short_name and program_invocation_name in compiled
3489+
// programs. This function is for implementing them.
3490+
_emscripten_get_progname__sig: 'vii',
3491+
_emscripten_get_progname: function(str, len) {
3492+
#if !MINIMAL_RUNTIME
3493+
#if ASSERTIONS
3494+
assert(typeof str === 'number');
3495+
assert(typeof len === 'number');
3496+
#endif
3497+
stringToUTF8(thisProgram, str, len);
3498+
#endif
3499+
},
3500+
34883501
emscripten_console_log__sig: 'vi',
34893502
emscripten_console_log: function(str) {
34903503
#if ASSERTIONS

system/lib/libc/musl/src/internal/libc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
struct __libc __libc;
44

55
size_t __hwcap;
6+
#ifndef __EMSCRIPTEN__
67
char *__progname=0, *__progname_full=0;
78

89
weak_alias(__progname, program_invocation_short_name);
910
weak_alias(__progname_full, program_invocation_name);
11+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#ifdef __EMSCRIPTEN__
9+
#include <limits.h>
10+
#include <string.h>
11+
12+
char *__progname=0, *__progname_full=0;
13+
14+
weak_alias(__progname, program_invocation_short_name);
15+
weak_alias(__progname_full, program_invocation_name);
16+
17+
/* See src/library.js for the implementation. */
18+
extern void _emscripten_get_progname(char*, int);
19+
20+
__attribute__((constructor))
21+
static void __progname_ctor(void)
22+
{
23+
static char full_path[PATH_MAX];
24+
char *basename;
25+
26+
_emscripten_get_progname(full_path, sizeof(full_path));
27+
28+
basename = strrchr(full_path, '/');
29+
if (basename == NULL) {
30+
basename = full_path;
31+
} else {
32+
basename++;
33+
}
34+
35+
__progname_full = full_path;
36+
__progname = basename;
37+
}
38+
#endif

tests/other/test_err.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(null): hello warning 42
1+
test_err.js: hello warning 42

tests/other/test_libc_progname.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2022 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <assert.h>
9+
#include <stdio.h>
10+
#include <string.h>
11+
12+
extern const char *__progname;
13+
extern const char *__progname_full;
14+
15+
int main(void) {
16+
printf("__progname: %s\n", __progname);
17+
18+
/* Ensure the pointers aren't NULL. */
19+
assert(__progname && __progname_full);
20+
21+
/* Ensure the basename is contained in the full path. */
22+
assert(strstr(__progname_full, __progname));
23+
24+
/* Ensure the basename contains no path separator. */
25+
assert(!strchr(__progname, '/'));
26+
27+
/* Ensure the full path starts with the root directory. */
28+
assert(*__progname_full == '/');
29+
30+
/* Ensure the full path is a file and not a directory. */
31+
assert(__progname_full[strlen(__progname_full)-1] != '/');
32+
33+
return 0;
34+
}

tests/other/test_libc_progname.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__progname: test_libc_progname.js

tests/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ def test_multiple_sources(self):
461461
def test_tsearch(self):
462462
self.do_other_test('test_tsearch.c')
463463

464+
def test_libc_progname(self):
465+
self.do_other_test('test_libc_progname.c')
466+
464467
def test_combining_object_files(self):
465468
# Compiling two files with -c will generate separate object files
466469
self.run_process([EMCC, test_file('twopart_main.cpp'), test_file('twopart_side.c'), '-c'])

0 commit comments

Comments
 (0)