@@ -16,13 +16,13 @@ import { dumpModules, getAllModules, gatherAllModules } from './lib/passes/gathe
16
16
17
17
import { bold , reset , genFreshFileName } from './lib/echo-util' ;
18
18
19
- // if we're running under coffee/node, argv will be ["coffee", ".../ejs", ...]
20
- // if we're running the compiled ejs.exe, argv will be [".../ejs.js.exe", ...]
21
- let slice_count = typeof ( __ejs ) === "undefined" ? 2 : 1 ;
22
- let argv = process . argv . slice ( slice_count ) ;
19
+ // argv is [".../ejs", ...], so get rid of the ejs
20
+ let argv = process . argv . slice ( 1 ) ;
23
21
24
22
let temp_files = [ ] ;
25
23
24
+ let running_from_source_dir = path . basename ( ejs_exe_dirname ( ) ) !== "bin" ;
25
+
26
26
let host_arch = os . arch ( ) ;
27
27
if ( host_arch === "x64" )
28
28
host_arch = "x86-64" ; // why didn't we just standardize on 'amd64'? sigh
@@ -316,27 +316,37 @@ function target_libraries(platform, arch) {
316
316
317
317
318
318
function target_libecho ( platform , arch ) {
319
- if ( platform === "darwin" ) {
320
- if ( arch === "x86-64" ) return "runtime/libecho.a" ;
319
+ if ( running_from_source_dir ) {
320
+ if ( platform === "darwin" ) {
321
+ if ( arch === "x86-64" ) return "runtime/libecho.a" ;
321
322
322
- return "runtime/libecho.a.ios" ;
323
- }
323
+ return "runtime/libecho.a.ios" ;
324
+ }
324
325
325
- return "runtime/libecho.a" ;
326
+ return "runtime/libecho.a" ;
327
+ }
328
+ else {
329
+ return path . join ( relative_to_ejs_exe ( `../lib/${ platform } -${ arch } ` ) , 'libecho.a' ) ;
330
+ }
326
331
}
327
332
328
333
329
334
function target_extra_libs ( platform , arch ) {
330
- if ( platform === "linux" ) return "external-deps/pcre-linux/.libs/libpcre16.a" ;
335
+ if ( running_from_source_dir ) {
336
+ if ( platform === "linux" ) return "external-deps/pcre-linux/.libs/libpcre16.a" ;
337
+
338
+ if ( platform === "darwin" ) {
339
+ if ( arch === "x86-64" ) return "external-deps/pcre-osx/.libs/libpcre16.a" ;
340
+ if ( arch === "x86" ) return "external-deps/pcre-iossim/.libs/libpcre16.a" ;
341
+ if ( arch === "arm" ) return "external-deps/pcre-iosdev/.libs/libpcre16.a" ;
342
+ if ( arch === "aarch64" ) return "external-deps/pcre-iosdevaarch64/.libs/libpcre16.a" ;
343
+ }
331
344
332
- if ( platform === "darwin" ) {
333
- if ( arch === "x86-64" ) return "external-deps/pcre-osx/.libs/libpcre16.a" ;
334
- if ( arch === "x86" ) return "external-deps/pcre-iossim/.libs/libpcre16.a" ;
335
- if ( arch === "arm" ) return "external-deps/pcre-iosdev/.libs/libpcre16.a" ;
336
- if ( arch === "aarch64" ) return "external-deps/pcre-iosdevaarch64/.libs/libpcre16.a" ;
345
+ throw new Error ( "no pcre for this platform" ) ;
346
+ }
347
+ else {
348
+ return path . join ( relative_to_ejs_exe ( `../lib/${ platform } -${ arch } ` ) , 'libpcre16.a' ) ;
337
349
}
338
-
339
- throw new Error ( "no pcre for this platform" ) ;
340
350
}
341
351
342
352
function target_path_prepend ( platform , arch ) {
@@ -360,19 +370,22 @@ function compileFile(filename, parse_tree, modules) {
360
370
}
361
371
362
372
let compiled_module ;
363
- // try {
373
+ try {
364
374
compiled_module = compile ( parse_tree , base_filename , filename , modules , options ) ;
365
- // }
366
- // catch (e) {
367
- // console.warn(`${e}`);
368
- // if (options.debug_level == 0) process.exit(-1);
369
- // throw e;
370
- // }
371
-
372
- let ll_filename = `${ os . tmpdir ( ) } /${ base_filename } -${ options . target_platform } -${ options . target_arch } .ll` ;
373
- let bc_filename = `${ os . tmpdir ( ) } /${ base_filename } -${ options . target_platform } -${ options . target_arch } .bc` ;
374
- let ll_opt_filename = `${ os . tmpdir ( ) } /${ base_filename } -${ options . target_platform } -${ options . target_arch } .ll.opt` ;
375
- let o_filename = `${ os . tmpdir ( ) } /${ base_filename } -${ options . target_platform } -${ options . target_arch } .o` ;
375
+ }
376
+ catch ( e ) {
377
+ console . warn ( `${ e } ` ) ;
378
+ if ( options . debug_level == 0 ) process . exit ( - 1 ) ;
379
+ throw e ;
380
+ }
381
+
382
+ function tmpfile ( suffix ) {
383
+ return `${ os . tmpdir ( ) } /${ base_filename } -${ options . target_arch } -${ options . target_platform } .${ suffix } ` ;
384
+ }
385
+ let ll_filename = tmpfile ( ".ll" ) ;
386
+ let bc_filename = tmpfile ( ".bc" ) ;
387
+ let ll_opt_filename = tmpfile ( ".ll.opt" ) ;
388
+ let o_filename = tmpfile ( ".o" ) ;
376
389
377
390
temp_files . push ( ll_filename , bc_filename , ll_opt_filename , o_filename ) ;
378
391
@@ -393,8 +406,12 @@ function compileFile(filename, parse_tree, modules) {
393
406
o_filenames . push ( o_filename ) ;
394
407
}
395
408
409
+ function ejs_exe_dirname ( ) {
410
+ return path . dirname ( path . resolve ( process . argv [ 0 ] ) ) ;
411
+ }
412
+
396
413
function relative_to_ejs_exe ( n ) {
397
- return path . resolve ( path . dirname ( process . argv [ typeof ( __ejs ) === 'undefined' ? 1 : 0 ] ) , n ) ;
414
+ return path . resolve ( ejs_exe_dirname ( ) , n ) ;
398
415
}
399
416
400
417
@@ -407,7 +424,7 @@ function generate_import_map (js_modules, native_modules) {
407
424
} ;
408
425
let map_path = `${ os . tmpdir ( ) } /${ genFreshFileName ( path . basename ( main_file ) ) } -import-map.cpp` ;
409
426
let map = fs . createWriteStream ( map_path ) ;
410
- map . write ( `#include "${ relative_to_ejs_exe ( ' runtime/ejs-module.h' ) } "\n` ) ;
427
+ map . write ( `#include "runtime/ejs-module.h"\n` ) ;
411
428
map . write ( 'extern "C" {\n' ) ;
412
429
413
430
js_modules . forEach ( ( module ) => {
@@ -471,9 +488,8 @@ function do_final_link(main_file, modules) {
471
488
let clang_args = target_link_args ( options . target_platform , options . target_arch ) . concat ( [ `-DEJS_BITS_PER_WORD=${ options . target_pointer_size } ` , "-o" , output_filename ] . concat ( o_filenames ) ) ;
472
489
if ( arch_info [ options . target_arch ] . little_endian )
473
490
clang_args . unshift ( "-DIS_LITTLE_ENDIAN=1" ) ;
474
-
475
- // XXX we shouldn't need this, but build is failing while compiling the require map
476
- clang_args . push ( "-I." ) ;
491
+
492
+ clang_args . push ( `-I${ relative_to_ejs_exe ( running_from_source_dir ? '.' : '../include' ) } ` ) ;
477
493
478
494
clang_args . push ( map_filename ) ;
479
495
@@ -487,11 +503,12 @@ function do_final_link(main_file, modules) {
487
503
if ( seen_native_modules . has ( mf ) ) return ;
488
504
489
505
seen_native_modules . add ( mf ) ;
490
-
491
- clang_args . push ( mf ) ;
506
+
507
+ clang_args . push ( path . resolve ( module . ejs_dir ,
508
+ running_from_source_dir ? '.' : `${ options . target_platform } -${ options . target_arch } ` ,
509
+ mf ) ) ;
492
510
} ) ;
493
511
494
- // very strange, not sure why we need this \n
495
512
clang_args = clang_args . concat ( module . link_flags . replace ( '\n' , ' ' ) . split ( " " ) ) ;
496
513
} ) ;
497
514
@@ -501,22 +518,9 @@ function do_final_link(main_file, modules) {
501
518
502
519
debug . log ( 1 , `executing '${ target_linker } ${ clang_args . join ( ' ' ) } '` ) ;
503
520
504
- if ( typeof ( __ejs ) != "undefined" ) {
505
- spawn ( target_linker , clang_args ) ;
506
- // we ignore leave_tmp_files here
507
- if ( ! options . quiet ) console . warn ( `${ bold ( ) } done.${ reset ( ) } ` ) ;
508
- }
509
- else {
510
- let clang = spawn ( target_linker , clang_args ) ;
511
- clang . stderr . on ( "data" , ( data ) => { console . warn ( `${ data } ` ) ; } ) ;
512
- clang . on ( "exit" , ( code ) => {
513
- if ( ! options . leave_temp_files ) {
514
- cleanup ( ( ) => {
515
- if ( ! options . quiet ) console . warn ( `${ bold ( ) } done.${ reset ( ) } ` ) ;
516
- } ) ;
517
- }
518
- } ) ;
519
- }
521
+ spawn ( target_linker , clang_args ) ;
522
+ // we ignore leave_tmp_files here
523
+ if ( ! options . quiet ) console . warn ( `${ bold ( ) } done.${ reset ( ) } ` ) ;
520
524
}
521
525
522
526
function cleanup ( done ) {
@@ -531,6 +535,8 @@ function cleanup(done) {
531
535
532
536
let main_file = file_args [ 0 ] ;
533
537
538
+ if ( ! running_from_source_dir )
539
+ options . native_module_dirs . push ( relative_to_ejs_exe ( "../lib" ) ) ;
534
540
let files = gatherAllModules ( file_args , options ) ;
535
541
debug . log ( 1 , ( ) => dumpModules ( ) ) ;
536
542
let allModules = getAllModules ( ) ;
0 commit comments