2
2
* QuickJS command line compiler
3
3
*
4
4
* Copyright (c) 2018-2021 Fabrice Bellard
5
+ * Copyright (c) 2023 Ben Noordhuis
6
+ * Copyright (c) 2023 Saúl Ibarra Corretgé
5
7
*
6
8
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
9
* of this software and associated documentation files (the "Software"), to deal
29
31
#include <assert.h>
30
32
#include <unistd.h>
31
33
#include <errno.h>
32
- #if !defined(_WIN32 )
33
- #include <sys/wait.h>
34
- #endif
35
34
36
35
#include "cutils.h"
37
36
#include "quickjs-libc.h"
@@ -59,7 +58,6 @@ static namelist_t init_module_list;
59
58
static uint64_t feature_bitmap ;
60
59
static FILE * outfile ;
61
60
static BOOL byte_swap ;
62
- static BOOL dynamic_export ;
63
61
static const char * c_ident_prefix = "qjsc_" ;
64
62
65
63
#define FE_ALL (-1)
@@ -240,12 +238,9 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
240
238
/* create a dummy module */
241
239
m = JS_NewCModule (ctx , module_name , js_module_dummy_init );
242
240
} 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 ;
249
244
} else {
250
245
size_t buf_len ;
251
246
uint8_t * buf ;
@@ -343,8 +338,7 @@ void help(void)
343
338
"usage: " PROG_NAME " [options] [files]\n"
344
339
"\n"
345
340
"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"
348
342
"-o output set the output filename\n"
349
343
"-N cname set the C name of the generated data\n"
350
344
"-m compile as Javascript module (default=autodetect)\n"
@@ -372,111 +366,9 @@ void help(void)
372
366
exit (1 );
373
367
}
374
368
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
-
476
369
typedef enum {
477
370
OUTPUT_C ,
478
371
OUTPUT_C_MAIN ,
479
- OUTPUT_EXECUTABLE ,
480
372
} OutputTypeEnum ;
481
373
482
374
int main (int argc , char * * argv )
@@ -494,7 +386,7 @@ int main(int argc, char **argv)
494
386
namelist_t dynamic_module_list ;
495
387
496
388
out_filename = NULL ;
497
- output_type = OUTPUT_EXECUTABLE ;
389
+ output_type = OUTPUT_C ;
498
390
cname = NULL ;
499
391
feature_bitmap = FE_ALL ;
500
392
module = -1 ;
@@ -509,7 +401,7 @@ int main(int argc, char **argv)
509
401
namelist_add (& cmodule_list , "os" , "os" , 0 );
510
402
511
403
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:" );
513
405
if (c == -1 )
514
406
break ;
515
407
switch (c ) {
@@ -518,9 +410,6 @@ int main(int argc, char **argv)
518
410
case 'o' :
519
411
out_filename = optarg ;
520
412
break ;
521
- case 'c' :
522
- output_type = OUTPUT_C ;
523
- break ;
524
413
case 'e' :
525
414
output_type = OUTPUT_C_MAIN ;
526
415
break ;
@@ -592,24 +481,10 @@ int main(int argc, char **argv)
592
481
if (optind >= argc )
593
482
help ();
594
483
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" ;
602
486
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 );
613
488
614
489
fo = fopen (cfilename , "w" );
615
490
if (!fo ) {
@@ -723,10 +598,6 @@ int main(int argc, char **argv)
723
598
724
599
fclose (fo );
725
600
726
- if (output_type == OUTPUT_EXECUTABLE ) {
727
- return output_executable (out_filename , cfilename , use_lto , verbose ,
728
- argv [0 ]);
729
- }
730
601
namelist_free (& cname_list );
731
602
namelist_free (& cmodule_list );
732
603
namelist_free (& init_module_list );
0 commit comments