-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllbuild.cpp
1193 lines (1132 loc) · 45 KB
/
dllbuild.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2019 by NEC Corporation
* This file is part of ve-jit */
#include "dllbuild.hpp"
//#include "cblock.hpp" // prefix_lines (debug output)
#include "throw.hpp"
#include "jitpage.h" // low level 'C' utilities
#include <fstream>
#include <cstring>
#include <assert.h>
#include <unistd.h> // getcwd, sysconf, pathconf, access
#include <sys/stat.h> // stat (possibly faster than 'access') and I check size
#define PSTREAMS 1
#if PSTREAMS
#include "pstreams-1.0.1/pstream.h"
#endif
/** need a better way to [easily] disable unrolled compiles */
#define UNROLLS 1
using namespace std;
/** 0 ~ we are linked with bin.mk object file,
* 1 ~ read bin.mk (from current dir, fragile?). */
#define BIN_MK_FROM_FILE 0
#if ! BIN_MK_FROM_FILE
extern "C" {
#if 0
// using objcopy to create a .rodata section works ONLY if you make a static lib
extern unsigned char _binary_bin_mk_start;
extern unsigned char _binary_bin_mk_end;
extern unsigned char _binary_bin_mk_size;
#else
// it's always ok to convert to C_string and "compile" bin.mk file into a string
// see ftostring.cpp
extern "C" {
// generated file: bin_mk.c
extern char const bin_mk[];
extern int bin_mk_size;
}//extern "C"
#endif
}//extern "C"
static std::string bin_mk_file_to_string(){
//system("ls -l bin.mk");
//cout<<" binary_bin_mk_start @ "<<(void*)&_binary_bin_mk_start<<endl;
//cout<<" binary_bin_mk_end @ "<<(void*)&_binary_bin_mk_end<<endl;
//cout<<" binary_bin_mk_size "<<(size_t)&_binary_bin_mk_size<<endl;
//return std::string((char*)&_binary_bin_mk_start, (size_t)&_binary_bin_mk_size);
#if 0
cout<<" bin_mk @ "<<(void*)&bin_mk[0]<<endl;
cout<<" bin_mk_size "<<bin_mk_size<<endl;
cout.flush();
#endif
return std::string(&bin_mk[0], (size_t)bin_mk_size);
}
#else
static std::string bin_mk_file_to_string(){
std::ostringstream oss;
try{
if(access("bin.mk",R_OK))
THROW("No read access to template file bin.mk");
ifstream ifs("bin.mk");
if(ifs){
oss << ifs.rdbuf();
ifs.close();
}
}catch(...){
cout<<" Trouble appending bin.mk template to makefile string"<<endl;
throw;
}
return oss.str();
}
#endif
static std::string getPath();
enum FileCmp { FILECMP_ABSENT, FILECMP_SAMESIZE, FILECMP_DIFFSIZE };
/** check if file is absent, samesize, or diffsize */
static enum FileCmp filecmp(std::string abspath, size_t const match_size){
enum FileCmp ret = FILECMP_ABSENT;
struct stat st = {0}; // XXX initialize ONLY to appease x86 valgrind XXX ?
//cout<<" stat('"<<abspath<<"')... "; cout.flush();
if(stat(abspath.c_str(), &st)){
if((size_t)st.st_size == match_size){ // file size matches => "same" (hack)
ret = FILECMP_SAMESIZE;
}else{
ret = FILECMP_DIFFSIZE;
}
}
return ret;
}
#if 0
class FileLocn {
std::string subdir;
std::string basename;
std::string suffix;
// calculated:
std::string abspath;
std::string fullpath;
};
#endif
SubDir::SubDir(std::string subdir)
: subdir(subdir), abspath(subdir)
{
if(createDirectoryAnyDepth(subdir.c_str())){
THROW("Write access denied to "+subdir);
}
if(subdir[0] != '/'){
abspath = getPath() + '/' + subdir;
}
}
std::string DllFile::short_descr() const {
std::ostringstream oss;
oss <<basename<<suffix
<<" "<<code.size()<<" code bytes, "
<<syms.size()<<" symbols";
return oss.str();
}
/** Unfortunately, multiple compilations rely on bin.mk file rule details,
* so now we return a vector of objects. */
std::vector<std::string> DllFile::obj(std::string fname, int const v/*=0,quiet*/){
std::vector<std::string> ret;
size_t p, pp=0;
std::vector<char const*> alts;
if(v){cout<<" Dllfile::obj(\""<<fname<<"\")..."<<endl; cout.flush();}
if((p=fname.rfind('-'))!= std::string::npos){
char const* obj_suffix = "-ve.o"; // handles VE common cases
//cout<<" (found a '-' in fname)"<<endl;
if(0){ ;
}else if((p=fname.rfind("-x86.c"))==fname.size()-6){ pp=p; obj_suffix="-x86.o";
}else if((p=fname.rfind("-x86.cpp"))==fname.size()-8){ pp=p; obj_suffix="-x86.o";
}else if((p=fname.rfind("-vi.c"))==fname.size()-5){
pp=p;
// need a better way to disable unrolled compiles XXX
#if UNROLLS
alts.push_back("_unroll-ve.o");
#endif
}else if((p=fname.rfind("-vi.cpp"))==fname.size()-7){ pp=p;
}else if((p=fname.rfind("-ncc.c"))==fname.size()-6){ pp=p;
}else if((p=fname.rfind("-ncc.cpp"))==fname.size()-8){ pp=p;
}else if((p=fname.rfind("-clang.c"))==fname.size()-8){ pp=p;
}else if((p=fname.rfind("-clang.cpp"))==fname.size()-10){ pp=p;
}
if(pp){ // all make fname[0:pp)+"-ve.o", and perhaps some alts
ret.push_back(fname.substr(0,pp).append(obj_suffix)); // usually -ve.o
//cout<<" "<<fname<<"-->"<<ret.back()<<endl;
for(auto const& suffix: alts){
ret.push_back(fname.substr(0,pp).append(suffix));
//cout<<" "<<fname<<"-->"<<ret.back()<<endl;
}
}//else{
// cout<<" (unrecognized '-' suffix)"<<endl;
//}
}
if(ret.empty()){
auto last_dot = fname.find_last_of('.');
if(last_dot != std::string::npos){
std::string ftype = fname.substr(last_dot+1);
if( ftype == "c" || ftype == "cpp" ){
ret.push_back(fname.substr(0,last_dot) + "-ve.o");
}else if( ftype == "s" || ftype == "S" ){
ret.push_back(fname.substr(0,last_dot) + ".bin");
}
}
}
if(ret.empty()) THROW("DllFile::obj("<<fname
<<") must match %[-vi|-ncc|-clang].{c|cpp} or %.{s|S} (see bin.mk rules)");
return ret;
}
/** \b new: if file exists and "same", don't rewrite it */
std::string DllFile::write(SubDir const& subdir, int const v/*=0,quiet*/){
string myfile; // entire generated source file
{
std::ostringstream oss;
oss <<"//Dllfile: basename = "<<basename
<<"\n//Dllfile: suffix = "<<suffix
<<"\n//Dllfile: abspath = "<<abspath;
if(v>1){cout<<" generating comment: "<<comment<<endl; cout.flush();}
oss <<"\n"<<comment;
if(v>1){cout<<" copying code: "<<code<<endl; cout.flush();}
oss <<"\n"<<code
<<endl;
myfile = oss.str();
}
// nitpick: '/' -> os path separator?
this->abspath = subdir.abspath + "/" + this->basename + this->suffix;
//cout<<" this->abspath="<<this->abspath<<endl; cout.flush();
// check file existence, and whether we can skip the rewrite
// quick'n'dirty check for file edit via size mismatch
#if 1 // XXX readable helper fn (perhaps extend with hash of file content?)
auto const fcmp = filecmp(abspath, myfile.size());
bool const writeit = (fcmp != FILECMP_SAMESIZE);
if (v>1){
if (fcmp == FILECMP_SAMESIZE) cout<<
" file samesize as code string, so NOT overwriting";
else if (fcmp == FILECMP_DIFFSIZE) cout<<
" file differs from code string size "<<myfile.size();
else cout << // FILECMP_ABSENT
" target file does not exist";
}
#else // original
bool writeit = true;
{
struct stat st;
if(stat(abspath.c_str(), &st)){
cout<<" my size "<<myfile.size();
if((size_t)st.st_size == myfile.size()){ // file size matches => "same" (hack)
writeit = false;
if(v>1)
cout<<" matches existing file, so NOT overwriting";
}else{
if(v>1) cout<<" differs from existing file size "<<st.st_size;
}
}else{
if(v>1) cout<<" and target file does not exist";
}
}
#endif
if(writeit){
try{
std::ofstream ofs(abspath);
if(v>1){cout<<" ofstream ofs("<<abspath<<") CREATED"<<endl; cout.flush();}
ofs<<myfile;
if(v>1){cout<<" and an extra ofs.flush() !!!"<<endl; cout.flush();}
ofs.flush();
ofs.close();
}catch(...){
cout<<" Trouble writing file "<<abspath<<endl;
throw;
}
if(v>0){cout<<" Wrote file "<<abspath<<endl;}
}
return this->abspath;
}
std::string getPath() {
long const sz = pathconf(".",_PC_PATH_MAX); // assume we are interested cwd
if(sz<=0) THROW("Invalid max path length?");
char* const temp=(char*)malloc((size_t)sz);
if(temp==nullptr) THROW("Out of memory");
if ( getcwd(temp, sz) != 0) {
std::string ret{temp};
free(temp);
return ret;
}
int error = errno;
switch ( error ) {
// sz>0 alreay checked (no EINVAL)
// PATH_MAX includes the terminating nul (no ERANGE)
case EACCES: THROW("Access denied");
case ENOMEM: THROW("Insufficient storage"); // is this possible?
default: THROW("Unrecognised errno="<<error);
}
return std::string{};
}
#if 0
/** create a tmp file in some [plain] subdirectory */
FileLocn writeFile(std::string const& code, std::string const& basename,
std::string suffix, std::string subdir){
// other inputs: this->basename, this->outDir, suffix
// output: this->ccode_tmpfile
std::string base = basename;
std::string suffix(".c");
if( code.empty() )
THROW(" Error: mkTmpfile with no 'C' code string? ");
if( !ccode_tmpfile.empty() ){
std::cerr<<" Did you forget to 'run' the DllPipe on temporary file "<<ccode_tmpfile<<" ?"<<std::endl;
}
// Write, then set this->ccode_tmpfile
{
//std::string tmpfile(std::tmpnam(nullptr));
//
// warning: the use of `tempnam' is dangerous, better use `mkstemp'
// ... but not sure about portability of mkstemp ...
//
std::string tmpfile(tempnam(outDir.c_str(),"tmp")+std::string("_")+base+suffix);
std::cout<<" DllPipe writing code to "<<tmpfile<<" ..."<<std::endl;
try{
std::ofstream ofs(tmpfile);
ofs<<code<<std::endl;
ofs.close();
}catch(...){
THROW("Problem creating "<<tmpfile<<" 'C'-code file)");
}
this->ccode_tmpfile = tmpfile;
}
}
#endif
std::string const & DllBuild::getLibName() const {
if(!prepped) THROW("getLibName requires DllBuild::prep(basename,dir)");
return this->libname;
}
/** Generating the Makefile
*
* - adds a prefix to canned rules of \e bin.mk Makefile template
* - notably, \e all: target is a shared library
* - bin.mk now can produce multiple clang versions.
* - also creates basename.OBJECTS so Makefile can circumvent command line limits
*
* \c skip added so existing Makefile or source files would not get
* rewritten.
*/
void DllBuild::prep(string basename, string subdir/*="."*/){
if(empty()){
if(verbose) cout<<" Nothing to do for dll "<<basename<<endl;
return;
}
this->dir = SubDir(subdir);
this->basename = basename;
this->libname = "lib"+this->basename+".so";
string archive = "lib"+this->basename+".a"; // NEW
this->mkfname = this->basename+".mk";
this->fullpath = dir.abspath+"/"+libname;
ostringstream mkfile;
// at_file is so mkfile can use @FILE way to avoid command line length limits
string at_filename(this->basename+".OBJECTS");
ostringstream at_file;
at_file<<"\n";
mkfile<<"# Auto-generated Makefile for "<<libname;
mkfile<<"\nLIBNAME:="<<libname
<<"\nARCHIVE:="<<archive
//<<"\nLDFLAGS:=$(LDFLAGS) -shared -fPIC -Wl,-rpath="<<dir.abspath<<" -L"<<dir.abspath
<<"\nLDFLAGS:=-shared -fPIC -Wl,-rpath="<<dir.abspath<<" -L"<<dir.abspath<<" $(LDFLAGS)";
mkfile<<"\n.PHONY: hello goodbye all\n"
<<"all: hello $(ARCHIVE) $(LIBNAME) goodbye\n";
// Let's be even more careful about duplicates (multiply-defined-symbols if identical code)
for(size_t i=0U; i<size(); ++i){
auto& df_i = (*this)[i];
if( df_i.basename.empty() ){
cout<<" DllFile "<<i<<" had no basename (removing)"<<endl;
continue;
}
for(size_t j=0U; j<i; ++j){
auto& df_j = (*this)[j];
if( df_i.basename == df_j.basename ){
if( df_i.suffix == df_j.suffix){
if( df_i.code == df_j.code && df_i.syms.size() == df_j.syms.size() ){
cout<<" Duplicate DllFile "<<i<<" matches "<<j<<", IGNORED "<<i
<<"\n prev: "<<df_j.short_descr()
<<"\n skip: "<<df_i.short_descr()
<<endl;
}else{
cout<<" Duplicate DllFile "<<i<<" vs "<<j<<"! code/syms do not match "
<<"\n prev: "<<df_j.short_descr()
<<"\n skip: "<<df_i.short_descr();
THROW(" Duplicate DllFile "<<i<<" vs "<<j<<"! code/syms do not match "
<<"\n prev: "<<df_j.short_descr()
<<"\n skip: "<<df_i.short_descr());
}
}else{
cout<<" Duplicate DllFile "<<i<<" w/ different suffix from "<<j<<", IGNORED "<<i
<<"\n prev: "<<df_j.short_descr()
<<"\n skip: "<<df_i.short_descr()
<<endl;
}
#define DLLBUILD_ERASE_SUSPICIOUS 1
#if DLLBUILD_ERASE_SUSPICIOUS
df_i.basename.clear(); // mark for full erasure from build
break;
#else // keep it around, but disable its syms and library inclusion (for debug?)
// UNTESTED CODE
std::ostringstream oss;
oss<<df_i.suffix<<".dup_"<<i<<'_'<<j;
df_i.suffix = oss.str(); // remove by change suffix and clearing syms
cout<<" changed DllFile "<<i<<" suffix to "<<df_i.suffix<<endl;
df_i.comment = "REMOVED "+df_i.comment;
df_i.syms.clear();
cout<<" and clearing its symbols"<<endl;
// keep the tag? or punt it to the old one? (not sure)
if(0){ // with changed suffix and empty syms, we can still write a file
// in case it helps debug...
oss.str("");
oss<<"#if 0 /* Test file "<<i<<" removed, similar to prev "<<j
<<"\n prev: "<<(*this)[ j ].short_descr()
<<"\n skip: "<<(*this)[ i ].short_descr()
<<df_i.code
<<"\n#endif /* test removed! */";
df_i.code = newcode.str();
}
#endif
break; // important!
}
}
}
if(DLLBUILD_ERASE_SUSPICIOUS) { // fully remove the DllFile?
for(auto it=begin(); it!=end(); ){
if(verbose) cout<<(it->basename.empty()? " Removing: ":" Keeping: ")
<<it->basename<<""<<it->suffix<<endl;
if( it->basename.empty() ){
it = erase(it);
}else{
++it;
}
}
}
{
ostringstream sources; sources<<"\nSOURCES:=";
ostringstream objects; objects<<"\nOBJECTS_FILE:="<<at_filename<<"\nOBJECTS:=";
ostringstream deps; deps <<"\n";
ostringstream hello; hello <<"\n";
ostringstream goodbye; goodbye<<"\ngoodbye:\n"
<<"\t@echo 'Goodbye, "<<mkfname<<" ending'\n";
#define DLLBUILD_SIMPLE_RENAMES 0
#if DLLBUILD_SIMPLE_RENAMES
//
// To think about...
//
// By creating rename files for every possible target, we will always generate
// an alternate unroll_FUNC call for every FUNC in FUNC-vi.c.
// Downside:
// 1. you only want some unroll_FUNCs
// 2. FUNC names are not obtained from the sourcefile stem
//
// current method allows client-specified symbols to be renamed, but can
// still run into problems with multiply defined symbols for the *other* FUNCs
//
// XXX Eventually, will want also to objcopy -N <name> to --strip-symbol
// the alternate-compile symbols that we want to remove
//
hello<<
"\n$(patsubst %-vi.c,%_unroll-ve.o.rename,$(SOURCES)): %_unroll-ve.o.rename:: %-vi.c"
"\n\t@# assume stem exactly matches the function name!"
"\n\techo '$* unroll_$*' > $@"
"\nhello: | $(patsubst %-vi.c,%_unroll-ve.o.rename,$(SOURCES))"
"\n\techo 'Hello, cjitConv.mk begins'"
#endif
for(size_t i=0U; i<size(); ++i){
DllFile& df = (*this)[i];
if(1){ // handle absent fields in DllFile
if(df.basename.empty()){
ostringstream oss;
oss<<"lib"<<basename<<"_file"<<i;
df.basename=oss.str();
cout<<" Warning: auto-suppying source file name "<<oss.str()<<endl;
}
if(df.suffix.empty()){
df.suffix = "-ncc.c";
cout<<" Warning: auto-suppying source suffix "<<df.suffix
<<" for basename "<<df.basename<<endl;
}
}
string dfSourceFile = df.basename+df.suffix; // no "." because suffix could be "-vi.c"
sources<<" \\\n\t"<<dfSourceFile;
df.objects = DllFile::obj(dfSourceFile); // checks name correctness
// A source file might produce several objects by different compile options
// The symbols should be renamed to coexist in a single dll
//
// objcopy with a rename file seems best way to rename
// compile-option-differentiated symbols.
//
vector<SymbolDecl> altsyms;
for(auto const& object: df.objects){
objects<<" \\\n\t"<<object; // add to makefile OBJECTS:=
at_file<<object<<"\n"; // also add to @FILE to circumvent command line limits
deps<<"\n"<<object<<": "<<dfSourceFile;
// What object file types do we recognize?
if(object.rfind("_unroll-ve.o")==object.size()-12){
ostringstream rename;
for(auto const& sd: df.syms){ // SymbolDecl
string altname = "unroll_"+sd.symbol;
string altfwd = sd.fwddecl;
size_t fnLoc = altfwd.find(sd.symbol);
if(fnLoc != string::npos)
altfwd.replace(fnLoc, sd.symbol.length(), altname);
altsyms.emplace_back(altname,"unrolled version",altfwd);
rename<<"\techo '"<<sd.symbol<<" "<<altname<<"' >> $@\n";
}
string renames = rename.str();
//std::cout<<" XXX renames = <"<<renames<<">\n";
if(!renames.empty()){
string renameFile(object);
renameFile.append(".rename");
mkfile<<"\n"<<renameFile<<":"
<<"\n\t@rm -f "<<renameFile<<"\n"
<<renames;
hello<<"\nhello: |"<<renameFile; // create this FIRST (only if not present)
}
}
}
// append all altsyms, from any alternate object files
if(!altsyms.empty()){
df.syms.reserve(df.syms.size() + altsyms.size());
for(auto const &s: altsyms)
df.syms.push_back(s);
}
df.abspath = dir.abspath+'/'+dfSourceFile;
df.write(this->dir); // source file input (throw if err)
}
mkfile<<"\n#sources\n"<<sources.str()<<endl;
mkfile<<"\n#deps \n"<<deps .str()<<endl;
mkfile<<"\n#objects\n"<<objects.str()<<endl;
mkfile<<hello.str();
mkfile<<"\nhello:\n\techo 'Hello, "<<mkfname<<" begins'\n";
mkfile<<"\n"<<goodbye.str()<<endl;
mkfile<<endl;
}
mkfile<<"\n# end of customized prologue. Follow by standard build recipes from bin.mk\n";
mkfile << bin_mk_file_to_string() << "\n#";
{ // write mkfile to <dir.abspath>/<mkfname>
std::string absmkfile;
try{
absmkfile = dir.abspath+"/"+mkfname;
ofstream ofs(absmkfile);
if(ofs){
//ofs << mkfile.rdbuf();
ofs << mkfile.str();
ofs.flush();
ofs.close();
}else{
THROW(" Trouble constructing ofs("<<absmkfile<<")");
}
}catch(...){
cout<<" Trouble writing file "<<absmkfile<<endl;
throw;
}
if(verbose) cout<<" makefile written: "<<absmkfile<<endl;
}
// NOTE alternate is to write the @FILE as a series of 'echo' commands, right
// into the makefile itself.
{ // write at_file.str() to <dir.abspath>/<at_filename>
std::string absatfile;
try{
absatfile = dir.abspath+"/"+at_filename;
ofstream ofs(absatfile);
if(ofs){
//ofs << mkfile.rdbuf();
ofs << at_file.str();
ofs.flush();
ofs.close();
}else{
THROW(" Trouble constructing ofs("<<absatfile<<")");
}
}catch(...){
cout<<" Trouble writing file "<<absatfile<<endl;
throw;
}
if(verbose>0) cout<<" @FILE list of object files written: "<<absatfile<<endl;
}
// sometimes the 'make' does nothing ??? check that files are really there.
if(verbose>0) {
// dir.abspath might have literally thousands of files (too much output)
//auto ls = "ls -l "+dir.abspath+" *.mk lib*";
cout<<" *.mk and lib* in directory "<<dir.abspath
<<" as DllBuild::prep finishes:"<<endl;
auto ls = "cd "+dir.abspath+" && ls -l *.mk lib*";
if(system(ls.c_str())!=0) cout<<"Issues with command `"<<ls<<"`"<<endl;
}
prepped = true;
}
void DllBuild::skip_prep(string basename, string subdir/*="."*/){
if(empty()){
if(verbose>0) cout<<" Nothing to do for dll "<<basename<<endl;
return;
}
this->dir = SubDir(subdir);
this->basename = basename;
this->libname = "lib"+this->basename+".so";
//string archive = "lib"+this->basename+".a"; // NEW
this->mkfname = this->basename+".mk";
this->fullpath = dir.abspath+"/"+libname;
std::string absmkfile = dir.abspath+"/"+mkfname;
if(access(absmkfile.c_str(),R_OK)){
if(verbose) cout<<" Oh. Cannot skip generating "<<mkfname;
this->prep(basename,subdir);
}else{
if(verbose) cout<<" Re-using "<<absmkfile<<endl;
prepped = true;
}
}
static int try_make( std::string mk, std::string mklog, int const v/*verbose*/ ){
string mk_cmd = mk;
if( PSTREAMS==0 || mklog.empty() || v>1 )
{
auto ret = system(mk_cmd.c_str());
if(ret){
//THROW(" Build error: "+mk);
cout<<" Build error: "<<mk_cmd<<endl;
cout<<" Build ret : "<<ret<<endl;
}
return ret;
}
#if !PSTREAMS
if(!mklog.empty()){
mk_cmd.append(" >& ").append(mklog);
}
// XXX
if(system(mk_cmd)!=0){
return 1; // error
}
#else
// else pstreams to capture stdout, stderr into log files etc.
if(v) cout<<" Build command: "<<mk_cmd<<endl;
using namespace redi;
redi::pstream mkstream(mk_cmd,
pstreams::pstdin | pstreams::pstdout | pstreams::pstderr);
mkstream << peof;
string err;
string out;
int status;
int error;
{
mkstream.err();
std::stringstream ss_err;
ss_err << mkstream.rdbuf();
err = ss_err.str(); // stderr --> std::string
}
{
mkstream.out();
std::stringstream ss_out;
ss_out << mkstream.rdbuf();
out = ss_out.str(); // stdout --> std::string
}
mkstream.close(); // retrieve exit status and errno
status = mkstream.rdbuf()->status();
error = mkstream.rdbuf()->error();
if(error || status){
cout<<" exit status or error! Please check build log in "<<mklog<<endl;
}
#if 0
{ // even in quiet mode mode: write 'make' output into a file:
std::ofstream ofs(mklog, ios_base::app);
ofs<<string(40,'-')<<" stdout:"<<endl<<out<<endl;
ofs<<string(40,'-')<<" stderr:"<<endl<<err<<endl;
ofs.flush();
ofs.close();
}
#endif
if(error || status){
cout<<"\n\n WARNING: see build log "<<mklog<<" for more info"
<<"\n We will try to continue anyways.\n"<<endl;
cout<<" Build command: "<<mk_cmd<<endl;
cout<<" Make error: "<<error <<endl;
cout<<" Make status: "<<status<<endl;
if(v>1) cout<<string(40,'-')<<" stdout:"<<endl<<out<<endl;
if(v>0) cout<<string(40,'-')<<" stderr:"<<endl<<err<<endl;
}
return status | error;
#endif
}
void DllBuild::make(std::string env){
int const v = this->verbose;
if(!prepped)
THROW("Please prep(basename,dir) before make()");
std::string mklog = dir.abspath+"/"+mkfname+".log";
string mk = env+" make VERBOSE=1 -C "+dir.abspath+" -f "+mkfname;
if(v>0){cout<<" Make command: "<<mk<<endl; cout.flush();}
//system(("ls -l "+dir.abspath).c_str()); // <-- unsafe c_str usage
int bad = try_make(mk,mklog,v);
if(bad){
mklog.append("2");
if(v>0) cout<<"Trying make once again... "<<endl;
bad = try_make(mk, mklog, this->verbose);
if(bad){
cout<<"BUILD ERROR! see "<<mklog<<endl;
THROW("Build error");
}
}
if(v>0){cout<<" 'make' ran in: "<<dir.abspath<<endl;}
made = true;
}
void DllBuild::skip_make(std::string env){
if(!prepped)
THROW("Please prep(basename,dir), or at least skip_prep(), before make()");
//string mk = env+" make VERBOSE=1 -C "+dir.abspath+" -f "+mkfname;
//if(verbose) {cout<<" Make command: "<<mk<<endl; cout.flush();}
if(access(this->fullpath.c_str(),R_OK)){
if(verbose) {
cout<<" Oh. library "<<fullpath<<" is not there."
" Attempting rebuild"<<endl;
cout.flush();
}
this->make(env);
}else{
if(verbose) cout<<" Re-using existing library "<<fullpath<<endl;
}
made = true;
}
DllOpen::DllOpen() : basename(), libHandle(nullptr), dlsyms(), libname(), files() {
#if 0
cout<<" +DllOpen"; cout.flush();
#endif
}
DllOpen::~DllOpen() {
int const v = 0;
if(v){cout<<" -DllOpen"; cout.flush();}
if(libHandle) dlclose(libHandle);
libHandle = nullptr;
if(v){cout<<" back from dlclose\n"; cout.flush();}
//files.clear();
}
// When inlined from cblock.hpp I got linker errors (std::string::size multiple defn of Cblock::append or so !!!!!)
static std::ostream& prefix_lines(std::ostream& os, std::string code,
std::string prefix, std::string sep=std::string("\n")){
if( prefix.empty() ){
os << code;
}else if( !code.empty()){
size_t nLoc = 0, nLocEnd;
while ((nLocEnd = code.find_first_of(sep, nLoc)) != std::string::npos) {
//std::cout<<" line["<<nLoc<<"..."<<nLocEnd<<"] = <"<<code.substr(nLoc,nLocEnd)<<">\n";
// line is nLoc..nLocEnd, including the last sep char
auto const first_nb = code.find_first_not_of(" \r\n\t",nLoc); // first non blank
if( first_nb < nLocEnd ){ // if not a blank line
if( code[first_nb] != '#' ) os << prefix; // never indent cpp directives
os << code.substr(nLoc,nLocEnd-nLoc) << "\n"; // code string + newline)
}
nLoc = nLocEnd+1;
}
//std::cout<<" nLoc="<<nLoc<<" code.size()="<<code.size();
if(nLoc < code.size()){
//std::cout<<" line["<<nLoc<<"...end] = <"<<code.substr(nLoc)<<">\n";
// line is nLoc..nLocEnd, including the last sep char
auto const first_nb = code.find_first_not_of(" \r\n\t",nLoc);
if( first_nb < nLocEnd ){
if( code[first_nb] != '#' ) os << prefix;
os << code.substr(nLoc,nLocEnd-nLoc);
//os << "\n"; // NO newline
}
}
}
return os;
}
std::unique_ptr<DllOpen> DllBuild::dllopen(){
int const v = this->verbose;
//using cprog::prefix_lines;
if(v)cout<<"*** DllBuild::dllopen() BEGINS"<<endl;
if(!(prepped and made))
THROW("Please prep(basename,dir) and make() before you dllopen()");
std::unique_ptr<DllOpen> pRet( new DllOpen );
DllOpen& ret = *pRet;
ret.libname = this->libname;
if(v>1){
cout<<"DllBuild::dllopen() calling dlopen... libname="<<libname<<endl;
cout<<"DllBuild::dllopen() calling dlopen... fullpath="<<fullpath<<endl;
cout.flush();
}
if(access(fullpath.c_str(),R_OK))
THROW(" Cannot read file "<<fullpath);
else
if(v>1){cout<<"Good, have read access to fullpath"<<endl; cout.flush();}
#if JIT_DLFUNCS // but means libjit1 has a dependency on libdl
if(0 && v>1){
//
// Check right away that dlopen *can* succeed [debug]
//
if(v>1){cout<<" debug... dlopen_rel + dl_dump..."<<endl;}
//void * dbg = dlopen_rel( fullpath.c_str(), RTLD_LAZY );
void * dbg = dlopen( fullpath.c_str(), RTLD_LAZY | RTLD_GLOBAL );
if(!dbg) {
cout<<" dlopen(\""<<fullpath.c_str()<<"\", RTLD_LAZY | RTLD_GLOBAL) failed!"<<endl;
#if !defined(__ve)
cout<<" (note: x86 program won't be able to open a VE library)"<<endl;
#endif
cout.flush();
THROW(" dlopen failed!");
}
if(v>1){cout<<" debug... dlopen succeeded"<<endl; cout.flush();}
if(v>2){
cout<<" debug... calling dl_dump ..."<<endl; cout.flush();
dl_dump(dbg);
cout<<" debug... back from dl_dump..."<<endl;
dlclose(dbg);
}
}
#endif
// originally targeted VE, but now accept -x86.c and -x86.cpp sources
#if 0 // !defined(__ve)
cout<<" DllBuild::dllopen cut short -- not running on VE "<<endl;
pRet.reset(nullptr);
return pRet;
#endif
if(v>1){cout<<"now dlopen..."<<endl; cout.flush();}
ret.libHandle = dlopen(fullpath.c_str(), RTLD_LAZY);
if(v>0){cout<<"DllBuild::dllopen() dlopen(fullpath, RTLD_LAZY) OK"<<endl; cout.flush();}
if(!ret.libHandle){ std::ostringstream oss; oss<<"failed dlopen("<<fullpath<<") dlerror "<<dlerror(); cout<<oss.str()<<endl; cout.flush(); THROW(oss.str()); }
//
// Now go through all our expected symbols and actually
// obtain their address, before we return
//
int nerr=0; // symbol load error count
if(v>0){cout<<"Library: "<<this->libname<<endl; cout.flush();}
ret.dlsyms_num.resize(this->size()); // create 'i'-->vector<symbolName> entries
for(size_t i=0U; i<this->size(); ++i) // for each source file
{
auto const& df = (*this)[i];
auto const filepath = df.getFilePath();
if(v>1){cout<<" File: "<<df.basename<<df.suffix
<<"\n : "<<filepath<<endl;
cout.flush();
}
ret.files.push_back(filepath);
ret.tag.push_back(df.tag);
for(auto const &sym: df.syms){
dlerror(); // clear previous error [if any]
void* addr = dlsym(ret.libHandle, sym.symbol.c_str());
if(v>0){
cout<<" Symbol: "<<sym.symbol<<" @ "<<addr<<"\n";
if(!sym.comment.empty())
prefix_lines(cout,sym.comment," // ")<<"\n";
if(!sym.fwddecl.empty())
prefix_lines(cout,sym.fwddecl," ")<<"\n";
cout.flush();
}
char const* dlerr=dlerror();
if(dlerr){
cout<<"**Error: could not load symbol "<<sym.symbol<<endl;
++nerr;
}
if(ret.dlsyms.find(sym.symbol)!=ret.dlsyms.end()){
cout<<"**Error: duplicate symbol "<<sym.symbol<<endl;
++nerr;
}
ret.dlsyms.insert(make_pair(sym.symbol,addr));
ret.dlsyms_num[i].push_back(sym.symbol);
}
}
if(v){cout<<"*** DllBuild::dllopen() DONE : nerr="<<nerr<<endl; cout.flush();}
if(nerr) THROW(nerr<<" symbol load errors from "<<libname);
return pRet;
}
std::unique_ptr<DllOpen> DllBuild::safe_create(
std::string basename,
std::string dir /*="."*/ ,
std::string env /*=""*/ ){
std::unique_ptr<DllOpen> ret;
int err = 0;
if(this->size()==0){
cout<<" warning: no files in DllBuild?"<<endl; cout.flush();
++err;
} else try {
ret = this->create(basename, dir, env);
if (!ret) ++err;
}
catch(...) {
cout<<" warning: DllBuild::create failed!"<<endl; cout.flush();
++err;
}
if(err){
this->dump(cout);
throw(runtime_error("Error: DllBuild::safe_create"));
}
return ret;
}
void DllBuild::dump(std::ostream& os){
os<<"\nDllBuild::dump"<<endl;
for(auto const& dllfile: *this){
os<<" "<<dllfile.short_descr()<<endl;
for(auto const& sym: dllfile.syms){
os<<" sym "<<sym.symbol<<endl;
}
for(auto const& obj: dllfile.objects){
os<<" obj "<<obj<<endl;
}
}
os<<"\n DllBuild::basename ="<<basename;
os<<"\n DllBuild::dir ="<<dir.subdir; // also avail: dir.abspath
os<<"\n DllBuild::prepped ="<<prepped;
os<<"\n DllBuild::made ="<<made;
os.flush();
}
#if 0
enum DllPipe::CC jithow = DllPipe::CC::same;
// different compilations will generate things like:
//
// libgcc_lucky.so this code and jit code runs on host
// libncc_lucky.so this code and jit code runs on VE
// libclang_lucky.so this code(can be gcc) and jit code from clang run on host
// libnclang_lucky.so this code(can be ncc) and jist code from clang -target ve-linux on VE
//
// Passing this test requires that this executable and the .so agree that they
// are running on HOST or VE, but test and lib compilers don't have to match exactly.
//
// Ex. this test can compile under nc++,
// but the JIT ccode string **requires** vector builtins,
// so using nc++ (or DllPipe::CC::ncc) as the 'C' compiler would fail.
//
// The DllPipe constructor should be given the DllPipe::CC::nclang
// override (opt 3rd arg) to force the proper cross-compiler to run.
//
#if defined(JIT_CLANG)
//std::cout<<" !!clang!!override "<<std::endl;
jithow = DllPipe::CC::clang;
#if defined(__ve)
#error "Wrong compiler! -DJIT_CLANG test wants this test to run on HOST"
#endif
#elif defined(JIT_NCLANG)
jithow = DllPipe::CC::nclang;
//std::cout<<" !!nclang!!override="<<(int)jithow<<std::endl;
//#if !defined(__ve)
//#error "Wrong compiler! -DJIT_NCLANG test wants this test to run on VE"
//#endif
#elif defined(JIT_GCC) // force JIT via gcc (only native .so)
//std::cout<<" !!gcc!!override "<<std::endl;
jithow = DllPipe::CC::gcc;
#if defined(__ve)
#error "Wrong compiler! -DJIT_GCC requires this compiler to output a native executable"
#endif
#elif defined(JIT_NCC) // force JIT via ncc (only cross-compiling to VE .so)
//std::cout<<" !!gcc!!override "<<std::endl;
jithow = DllPipe::CC::ncc;
#if !defined(__ve)
#error "Wrong compiler! -DJIT_NCLANG requires this compiler to predefine '__ve'"
#endif
#endif // JIT-compiler overrides (not nec. same as current compiler)
// compile the jit 'C' code into a .so library
DllPipe dll("lucky",ccode,jithow); // this actually creates the .so
// if all went well, we have libgcc_lucky.so
// or libncc_lucky.so
cout<<" DllPipe::lib() = "<<dll.lib() <<endl;
cout<<" DllPipe::libFullPath() = "<<dll.libFullPath()<<endl;
void *jitLibHandle;
pfx_where(); // print where we're running
//jitLibHandle = dlopen(dll.libFullPath().c_str(), RTLD_LAZY);
//
// There is only one function, and I want to call it for sure,
// so let's ask to perform all relocations immediately.
//
jitLibHandle = dlopen(dll.libFullPath().c_str(), RTLD_NOW);
if(!jitLibHandle){
cout<<"OHOH, dlopen(\""<<dll.libFullPath()<<"\",RTLD_NOW) failed"<<endl;
return 1;
}
cout<<"EXCELLENT, dlopen(\""<<dll.libFullPath()<<"\",RTLD_NOW)"
"\n returned library handle "<<(void*)jitLibHandle<<endl;
typedef int (*JitFunc)();
JitFunc ohoh = (JitFunc)dlsym(jitLibHandle, "myUnluckyNumber");
if( ohoh == nullptr ){
cout<<"OF COURSE, cannot find 'myUnluckyNumber' in library"
"\n dlerror() says "<<dlerror()<<endl;
}else{
THROW("Found a nonexistent symbol in the jit library?");
}
JitFunc jitLuckyNumber = (JitFunc)dlsym(jitLibHandle, "myLuckyNumber");
if( jitLuckyNumber == nullptr ){
cout<<"GOLLY GEE, my jit symbol myUnluckyNumber was not in the jit library"<<endl;
THROW(dlerror());
}
cout<<"EXCELLENT, dlsym(jitLibHandle,\"myLuckyNumber\") has my jit function"
"\n loaded at "<<(void*)jitLuckyNumber<<endl;
#endif
//----------------------------------------------------------------//
#ifdef DLLBUILD_MAIN
using namespace std;
#include "stringutil.hpp" // multiReplace
#if 0
/** print a prefix stating where we think we're running */
static void pfx_where(){
#if defined(__ve)
cout<<"Running on VE ... ";
#else
cout<<"Running on HOST ... ";
#endif
}
#endif
std::string program_myLuckyNumber( int const runtime_lucky_number ){
std::ostringstream oss;
// Best practice, since 'c' code might be compiled via C++ compiler:
oss<<"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n";
oss<<"int myLuckyNumber(){\n"
" /* This optimized 'C' code returns runtime lucky number "<<runtime_lucky_number<<" */\n";