Skip to content

Commit bebdfce

Browse files
authored
Drop ability to generate executables from qjsc (#76)
It's too brittle, and compiling the result is one command away.
1 parent d6a3342 commit bebdfce

File tree

2 files changed

+12
-141
lines changed

2 files changed

+12
-141
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ target_link_libraries(qjsc ${qjs_libs})
158158

159159
add_custom_command(
160160
OUTPUT repl.c
161-
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/qjsc" -c -o ./repl.c -m ${CMAKE_CURRENT_SOURCE_DIR}/repl.js
161+
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/qjsc" -o ./repl.c -m ${CMAKE_CURRENT_SOURCE_DIR}/repl.js
162162
DEPENDS qjsc
163163
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
164164
COMMENT "Compile repl.js to bytecode"

qjsc.c

+11-140
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* QuickJS command line compiler
33
*
44
* Copyright (c) 2018-2021 Fabrice Bellard
5+
* Copyright (c) 2023 Ben Noordhuis
6+
* Copyright (c) 2023 Saúl Ibarra Corretgé
57
*
68
* Permission is hereby granted, free of charge, to any person obtaining a copy
79
* of this software and associated documentation files (the "Software"), to deal
@@ -29,9 +31,6 @@
2931
#include <assert.h>
3032
#include <unistd.h>
3133
#include <errno.h>
32-
#if !defined(_WIN32)
33-
#include <sys/wait.h>
34-
#endif
3534

3635
#include "cutils.h"
3736
#include "quickjs-libc.h"
@@ -59,7 +58,6 @@ static namelist_t init_module_list;
5958
static uint64_t feature_bitmap;
6059
static FILE *outfile;
6160
static BOOL byte_swap;
62-
static BOOL dynamic_export;
6361
static const char *c_ident_prefix = "qjsc_";
6462

6563
#define FE_ALL (-1)
@@ -240,12 +238,9 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
240238
/* create a dummy module */
241239
m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
242240
} else if (has_suffix(module_name, ".so")) {
243-
fprintf(stderr, "Warning: binary module '%s' will be dynamically loaded\n", module_name);
244-
/* create a dummy module */
245-
m = JS_NewCModule(ctx, module_name, js_module_dummy_init);
246-
/* the resulting executable will export its symbols for the
247-
dynamic library */
248-
dynamic_export = TRUE;
241+
JS_ThrowReferenceError(ctx, "%s: dynamically linking to shared libraries not supported",
242+
module_name);
243+
return NULL;
249244
} else {
250245
size_t buf_len;
251246
uint8_t *buf;
@@ -343,8 +338,7 @@ void help(void)
343338
"usage: " PROG_NAME " [options] [files]\n"
344339
"\n"
345340
"options are:\n"
346-
"-c only output bytecode in a C file\n"
347-
"-e output main() and bytecode in a C file (default = executable output)\n"
341+
"-e output main() and bytecode in a C file\n"
348342
"-o output set the output filename\n"
349343
"-N cname set the C name of the generated data\n"
350344
"-m compile as Javascript module (default=autodetect)\n"
@@ -372,111 +366,9 @@ void help(void)
372366
exit(1);
373367
}
374368

375-
#if defined(CONFIG_CC) && !defined(_WIN32)
376-
377-
int exec_cmd(char **argv)
378-
{
379-
int pid, status, ret;
380-
381-
pid = fork();
382-
if (pid == 0) {
383-
execvp(argv[0], argv);
384-
exit(1);
385-
}
386-
387-
for(;;) {
388-
ret = waitpid(pid, &status, 0);
389-
if (ret == pid && WIFEXITED(status))
390-
break;
391-
}
392-
return WEXITSTATUS(status);
393-
}
394-
395-
static int output_executable(const char *out_filename, const char *cfilename,
396-
BOOL use_lto, BOOL verbose, const char *exename)
397-
{
398-
const char *argv[64];
399-
const char **arg, *bn_suffix, *lto_suffix;
400-
char libjsname[1024];
401-
char exe_dir[1024], inc_dir[1024], lib_dir[1024], buf[1024], *p;
402-
int ret;
403-
404-
/* get the directory of the executable */
405-
pstrcpy(exe_dir, sizeof(exe_dir), exename);
406-
p = strrchr(exe_dir, '/');
407-
if (p) {
408-
*p = '\0';
409-
} else {
410-
pstrcpy(exe_dir, sizeof(exe_dir), ".");
411-
}
412-
413-
/* if 'quickjs.h' is present at the same path as the executable, we
414-
use it as include and lib directory */
415-
snprintf(buf, sizeof(buf), "%s/quickjs.h", exe_dir);
416-
if (access(buf, R_OK) == 0) {
417-
pstrcpy(inc_dir, sizeof(inc_dir), exe_dir);
418-
pstrcpy(lib_dir, sizeof(lib_dir), exe_dir);
419-
} else {
420-
snprintf(inc_dir, sizeof(inc_dir), "%s/include/quickjs", CONFIG_PREFIX);
421-
snprintf(lib_dir, sizeof(lib_dir), "%s/lib/quickjs", CONFIG_PREFIX);
422-
}
423-
424-
lto_suffix = "";
425-
bn_suffix = "";
426-
427-
arg = argv;
428-
*arg++ = CONFIG_CC;
429-
*arg++ = "-O2";
430-
#ifdef CONFIG_LTO
431-
if (use_lto) {
432-
*arg++ = "-flto";
433-
lto_suffix = ".lto";
434-
}
435-
#endif
436-
/* XXX: use the executable path to find the includes files and
437-
libraries */
438-
*arg++ = "-D";
439-
*arg++ = "_GNU_SOURCE";
440-
*arg++ = "-I";
441-
*arg++ = inc_dir;
442-
*arg++ = "-o";
443-
*arg++ = out_filename;
444-
if (dynamic_export)
445-
*arg++ = "-rdynamic";
446-
*arg++ = cfilename;
447-
snprintf(libjsname, sizeof(libjsname), "%s/libquickjs%s%s.a",
448-
lib_dir, bn_suffix, lto_suffix);
449-
*arg++ = libjsname;
450-
*arg++ = "-lm";
451-
*arg++ = "-ldl";
452-
*arg++ = "-lpthread";
453-
*arg = NULL;
454-
455-
if (verbose) {
456-
for(arg = argv; *arg != NULL; arg++)
457-
printf("%s ", *arg);
458-
printf("\n");
459-
}
460-
461-
ret = exec_cmd((char **)argv);
462-
unlink(cfilename);
463-
return ret;
464-
}
465-
#else
466-
static int output_executable(const char *out_filename, const char *cfilename,
467-
BOOL use_lto, BOOL verbose, const char *exename)
468-
{
469-
fprintf(stderr, "Executable output is not supported for this target\n");
470-
exit(1);
471-
return 0;
472-
}
473-
#endif
474-
475-
476369
typedef enum {
477370
OUTPUT_C,
478371
OUTPUT_C_MAIN,
479-
OUTPUT_EXECUTABLE,
480372
} OutputTypeEnum;
481373

482374
int main(int argc, char **argv)
@@ -494,7 +386,7 @@ int main(int argc, char **argv)
494386
namelist_t dynamic_module_list;
495387

496388
out_filename = NULL;
497-
output_type = OUTPUT_EXECUTABLE;
389+
output_type = OUTPUT_C;
498390
cname = NULL;
499391
feature_bitmap = FE_ALL;
500392
module = -1;
@@ -509,7 +401,7 @@ int main(int argc, char **argv)
509401
namelist_add(&cmodule_list, "os", "os", 0);
510402

511403
for(;;) {
512-
c = getopt(argc, argv, "ho:cN:f:mxevM:p:S:D:");
404+
c = getopt(argc, argv, "ho:N:f:mxevM:p:S:D:");
513405
if (c == -1)
514406
break;
515407
switch(c) {
@@ -518,9 +410,6 @@ int main(int argc, char **argv)
518410
case 'o':
519411
out_filename = optarg;
520412
break;
521-
case 'c':
522-
output_type = OUTPUT_C;
523-
break;
524413
case 'e':
525414
output_type = OUTPUT_C_MAIN;
526415
break;
@@ -592,24 +481,10 @@ int main(int argc, char **argv)
592481
if (optind >= argc)
593482
help();
594483

595-
if (!out_filename) {
596-
if (output_type == OUTPUT_EXECUTABLE) {
597-
out_filename = "a.out";
598-
} else {
599-
out_filename = "out.c";
600-
}
601-
}
484+
if (!out_filename)
485+
out_filename = "out.c";
602486

603-
if (output_type == OUTPUT_EXECUTABLE) {
604-
#if defined(_WIN32) || defined(__ANDROID__)
605-
/* XXX: find a /tmp directory ? */
606-
snprintf(cfilename, sizeof(cfilename), "out%d.c", getpid());
607-
#else
608-
snprintf(cfilename, sizeof(cfilename), "/tmp/out%d.c", getpid());
609-
#endif
610-
} else {
611-
pstrcpy(cfilename, sizeof(cfilename), out_filename);
612-
}
487+
pstrcpy(cfilename, sizeof(cfilename), out_filename);
613488

614489
fo = fopen(cfilename, "w");
615490
if (!fo) {
@@ -723,10 +598,6 @@ int main(int argc, char **argv)
723598

724599
fclose(fo);
725600

726-
if (output_type == OUTPUT_EXECUTABLE) {
727-
return output_executable(out_filename, cfilename, use_lto, verbose,
728-
argv[0]);
729-
}
730601
namelist_free(&cname_list);
731602
namelist_free(&cmodule_list);
732603
namelist_free(&init_module_list);

0 commit comments

Comments
 (0)