-
Notifications
You must be signed in to change notification settings - Fork 7
/
BISMON-config.cc
1833 lines (1747 loc) · 73.5 KB
/
BISMON-config.cc
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
// file BISMON-config.cc
// SPDX-License-Identifier: GPL-3.0-or-later
// see https://github.com/bstarynk/bismon/
/***
BISMON
Copyright © 2020 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
with help from Franck Védrine.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif /*_GNU_SOURCE*/
#ifndef BISMON_SHORTGIT
#error BISMON_SHORTGIT should be defined in compilation command
#endif
//// Please use astyle (see http://astyle.sourceforge.net/ ...) to format this C++ file
//// using astyle --style=gnu -s2 BISMON-config.cc ....
// Linux headers
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
// C++ headers
#include <vector>
#include <string>
#include <map>
#include <set>
#include <fstream>
#include <string>
#include <cassert>
#include <iostream>
#include <sstream>
extern "C" bool bmc_debug_flag;
extern "C" bool bmc_batch_flag;
extern "C" bool bmc_dryrun_flag;
std::string bmc_start_str_ctime;
time_t bmc_start_time;
std::string bmc_make;
std::string bmc_packages;
std::string bmc_target_gcc;
std::string bmc_target_gxx;
std::string bmc_host_cc;
std::string bmc_host_cxx;
std::string bmc_host_compflags;
std::string bmc_out_directory;
std::string bmc_emit_constdep_path;
std::string bmc_ninja_file;
std::vector<std::string> bmc_source_files;
std::vector<std::string> bmc_constdep_files;
// from generated __timestamp.c - see timestamp-emit.sh script
extern "C" const char bismon_timestamp[];
extern "C" const unsigned long bismon_timelong;
extern "C" const char bismon_lastgitcommit[];
extern "C" const char bismon_lastgittag[];
extern "C" const char bismon_checksum[];
extern "C" const char bismon_directory[];
extern "C" const char bismon_gnumakefile[];
extern "C" const char bismon_gitid[];
extern "C" const char bismon_shortgitid[];
extern "C" const char* bismon_make;
extern "C" const char* bismon_packages;
extern "C" const char* bismon_target_gcc;
extern "C" const char* bismon_target_gxx;
extern "C" const char* const bismon_sources[];
extern "C" const int bismon_source_number;
bool bmc_batch_flag;
bool bmc_debug_flag;
bool bmc_dryrun_flag;
bool bmc_constdepend_flag;
bool bmc_silent_flag;
bool bmc_force_flag;
char bmc_hostname[64];
enum bmc_longopt_en
{
BMCOPT__longoptstart=2048,
BMCOPT_batch,
BMCOPT_ninja,
BMCOPT_target_gcc,
BMCOPT_target_gxx,
BMCOPT_host_cc,
BMCOPT_host_cxx,
BMCOPT_host_compflags,
BMCOPT_emit_const_depend,
BMCOPT_dry_run,
BMCOPT_skiplabel,
BMCOPT_output_directory,
};
//see https://en.wikipedia.org/wiki/ANSI_escape_code
#define BMC_BOLD_ESCAPE "\033[1m"
#define BMC_PLAIN_ESCAPE "\033[0m"
static const struct option
bmc_long_options[] =
{
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"debug", no_argument, 0, 'D'},
{"silent", no_argument, 0, 'S'},
{"force", no_argument, 0, 'f'},
{"dry-run", no_argument, 0, BMCOPT_dry_run},
{"batch", no_argument, 0, BMCOPT_batch},
{"emit-const-dep", required_argument, 0, BMCOPT_emit_const_depend},
{"ninja", required_argument, 0, BMCOPT_ninja},
{"target-gcc", required_argument, 0, BMCOPT_target_gcc},
{"target-gxx", required_argument, 0, BMCOPT_target_gxx},
{"host-cc", required_argument, 0, BMCOPT_host_cc},
{"host-cxx", required_argument, 0, BMCOPT_host_cxx},
{"host-compflags", required_argument, 0, BMCOPT_host_compflags},
{"skip", required_argument, 0, BMCOPT_skiplabel},
{"label", required_argument, 0, BMCOPT_skiplabel},
{"output-directory", required_argument, 0, BMCOPT_output_directory},
{0,0,0,0}
};
extern "C" bool bmc_debug_flag;
extern "C" bool bmc_batch_flag;
#ifndef BISMON_CONFIG_LABEL
#error BISMON_CONFIG_LABEL should be defined in compilation command
#endif /*BISMON_CONFIG_LABEL*/
extern "C" const char bismon_config_label[];
const char bismon_config_label[] =
BISMON_CONFIG_LABEL ":" __FILE__ " compiled at " __DATE__
" git " BISMON_SHORTGIT;
/********************* debugging facilities *********************/
////////////////////////////////////////////////////////////////
// typical usage could be BMC_DEBUG("something bad x=" << x)
#define BMC_DEBUG_AT_BIS(Fil,Lin,...) do { \
if (bmc_debug_flag) { \
std::clog << "¿" /* U+00BF INVERTED QUESTION MARK */ \
<< (Fil) << ":" << Lin << ":: " \
<< __VA_ARGS__ << std::endl; } } while(0)
#define BMC_DEBUG_AT(Fil,Lin,...) BMC_DEBUG_AT_BIS(Fil,Lin,##__VA_ARGS__)
// typical usage would be BMC_DEBUG("annoying x=" << x)
#define BMC_DEBUG(...) BMC_DEBUG_AT(__FILE__,__LINE__,##__VA_ARGS__)
////////////////////////////////////////////////////////////////
//// debug output with prior newline
// typical usage could be BMC_NLDEBUG("something bad x=" << x)
#define BMC_NLDEBUG_AT_BIS(Fil,Lin,...) do { \
if (bmc_debug_flag) { \
std::clog << std::endl \
<< "¿" /* U+00BF INVERTED QUESTION MARK */ \
<< (Fil) << ":" << Lin << ":: " \
<< __VA_ARGS__ << std::endl; } } while(0)
#define BMC_NLDEBUG_AT(Fil,Lin,...) BMC_NLDEBUG_AT_BIS(Fil,Lin,##__VA_ARGS__)
// typical usage would be BMC_NLDEBUG("annoying x=" << x)
#define BMC_NLDEBUG(...) BMC_NLDEBUG_AT(__FILE__,__LINE__,##__VA_ARGS__)
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
static char**bmc_main_argv;
static int bmc_main_argc;
void bmc_show_usage(const char*progname);
void bmc_show_version(const char*progname);
extern "C" void bmc_failure_at (const char*reason, int lineno);
#define BMC_FAILURE(Reason) bmc_failure_at((Reason),__LINE__)
void bmc_set_readline_buffer(const std::string& str);
void
bmc_failure_at(const char*reason, int lineno)
{
std::cerr << bmc_main_argv[0] << " failure at " << __FILE__ << ":" << lineno << std::endl;
std::cerr << " ***** " << reason << " ****" << std::endl;
std::cerr << "(git " << BISMON_SHORTGIT << " built " << __DATE__ "@" __TIME__ " from " __FILE__ ")" << std::endl;
std::cerr << "\t invoked as: ";
for (int ix=0; ix<bmc_main_argc; ix++)
std::cerr << ' ' << bmc_main_argv[ix];
std::cerr << std::endl;
std::cerr << "((pid " << getpid() << ", parentpid " << getppid() << ", host " << bmc_hostname << "))" << std::endl;
std::cerr << std::endl;
exit(EXIT_FAILURE);
} // end bmc_failure_at
void
bmc_parse_options(int& argc, char**argv)
{
int constdepix= -1;
for (;;)
{
int optix= -1;
int optres = getopt_long(argc, argv, "DVSh", bmc_long_options, &optix);
if (optres < 0)
break;
switch (optres)
{
case 'h': // --help
bmc_show_usage(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'V': // --version
bmc_show_version(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'D': // --debug
bmc_debug_flag = true;
break;
case 'f': // --force
bmc_force_flag = true;
break;
case BMCOPT_batch: // --batch
bmc_batch_flag = true;
break;
case BMCOPT_emit_const_depend: // --const-depend
constdepix= optix;
bmc_constdepend_flag = true;
bmc_emit_constdep_path = optarg;
break;
case BMCOPT_dry_run: // --dry-run
bmc_dryrun_flag = true;
BMC_DEBUG("dry run - won't fork compilation commands");
break;
case BMCOPT_ninja: // --ninja=PATH
bmc_ninja_file = optarg;
BMC_DEBUG("ninja file:" << bmc_ninja_file);
break;
case BMCOPT_target_gcc: // --target-gcc=PATH
bmc_target_gcc = optarg;
BMC_DEBUG("target GCC:" << bmc_target_gcc);
break;
case BMCOPT_target_gxx: // --target-gxx=PATH
bmc_target_gxx = optarg;
BMC_DEBUG("target GXX:" << bmc_target_gxx);
break;
case BMCOPT_host_cc: // --host_cc=PATH
bmc_host_cc = optarg;
BMC_DEBUG("host C compiler:" << bmc_host_cc);
if (!bmc_dryrun_flag)
{
if (access(bmc_host_cc.c_str(), X_OK))
{
int erc = errno;
std::string errstr = "given host C compiler ";
errstr += bmc_host_cc;
errstr += " is not executable: ";
errstr += strerror(erc);
BMC_FAILURE(errstr.c_str());
}
}
break;
case BMCOPT_host_cxx: // --host_cxx=PATH
bmc_host_cxx = optarg;
BMC_DEBUG("host C++ compiler:" << bmc_host_cxx);
if (!bmc_dryrun_flag)
{
if (access(bmc_host_cxx.c_str(), X_OK))
{
int erc = errno;
std::string errstr = "given host C++ compiler ";
errstr += bmc_host_cxx;
errstr += " is not executable: ";
errstr += strerror(erc);
BMC_FAILURE(errstr.c_str());
}
}
break;
case BMCOPT_output_directory: // --output-directory=DIR
bmc_out_directory = optarg;
BMC_DEBUG("output directory :" << bmc_out_directory);
break;
case BMCOPT_skiplabel: // --skip=XXXX or --label=XXX
BMC_DEBUG("skipping label:" << optarg);
break;
}
}
BMC_DEBUG("constdepix=" << constdepix);
if (constdepix>0 && constdepix<argc)
{
for (int kix=constdepix; kix<argc; kix++)
{
BMC_DEBUG("constdep kix=" << kix << ": " << argv[kix]);
bmc_constdep_files.push_back(std::string{argv[kix]});
}
}
} // end of bmc_parse_options
void
bmc_show_usage(const char*progname)
{
std::cerr << "###### see github.com/bstarynk/bismon and the DRAFT report on starynkevitch.net/Basile/bismon-doc.pdf"
<< std::endl;
std::cerr << progname << " usage:" << std::endl;
std::cerr << " --version | -V # give version information - git " BISMON_SHORTGIT << std::endl;
std::cerr << " --help | -h # give this help message" << std::endl;
std::cerr << " --debug | -D # debug this configurator program " << progname << std::endl;
std::cerr << " --force | -f # force overwriting of existing generated files." << std::endl;
std::cerr << " --batch # dont ask for missing arguments even in terminal" << std::endl;
std::cerr << " --dry-run # wont fork target compilation commands" << std::endl;
std::cerr << " --const-depend *_BM.c # output the dependencies on #include-s *.const.h files" << std::endl;
std::cerr << " --skip=IGNORED # ignored argument, would appear on failure message" << std::endl;
std::cerr << " --label=IGNORED # ignored argument, would appear on failure message" << std::endl;
std::cerr << " # for example: --skip=that_thing or --label=first_run" << std::endl;
std::cerr << "# the target cross-compilers below should be some GCC 10 to 12 with plugins enabled. See gcc.gnu.org for more." << std::endl;
std::cerr << " --target-gcc=PATH # set to PATH the target GCC cross-compiler executable for C code" << std::endl;
std::cerr << " # for example: --target-gcc=/usr/local/bin/arm-lnux-gnueabi-gcc-12" << std::endl;
std::cerr << " --target-gxx=PATH # set to PATH the target GCC cross-compiler for C++ code" << std::endl;
std::cerr << " # for example: --target-gxx=/usr/local/bin/mipsel-linux-gnu-g++-12" << std::endl;
std::cerr << "# the host compilers below would be used to compile BISMON and generated GCC plugin code (C++)." << std::endl;
std::cerr << " --host-cc=PATH # set to PATH the host C compiler executable for C code" << std::endl;
std::cerr << " # for example: --host-cc=/usr/bin/gcc" << std::endl;
std::cerr << " --host-cxx=PATH # set to PATH the host C++ compiler for C++ code" << std::endl;
std::cerr << " # for example: --host-cxx=/usr/bin/clang++" << std::endl;
std::cerr << " --host-cflags=FLAGS # set common compilation flags for both host C and C++ compilers" << std::endl;
std::cerr << " # for example: --host-cflags='-O2 -g -flto -I /usr/local/include'" << std::endl;
std::cerr << " --ninja=PATH # generate a PATH for ninja builder - see ninja-build.org" << std::endl;
std::cerr << " # usually --ninja=build.ninja" << std::endl;
std::cerr << " --output-directory=DIR # set the output directory to DIR - default is " << bismon_directory << std::endl;
std::cerr << "#####################################" << std::endl;
std::cerr << "##-- This executable " << progname << " was built " << __DATE__ "@" __TIME__ " from " __FILE__ " git " << BISMON_SHORTGIT << std::endl;
std::cerr << std::endl;
std::cerr << "######## See github.com/bstarynk/bismon/ for more about BISMON." << std::endl;
std::cerr << "## BISMON, so " << progname << " is free software, GPLv3+ licensed, WITHOUT WARRANTY." << std::endl;
std::cerr << "## See www.gnu.org/licenses/gpl-3.0.en.html " << std::endl;
std::cerr << std::endl;
} // end bmc_show_usage
void
bmc_show_version(const char*progname)
{
std::cout << progname
<< " version " BISMON_SHORTGIT
<< " built on " << __DATE__ "@" << __TIME__ << " from " << __FILE__
<< std::endl
<< "... for last git commit " << bismon_lastgitcommit
<< std::endl
<< "... in directory " << bismon_directory
<< " timestamp " << bismon_timestamp << std::endl
<< "... checksum " << bismon_checksum
<< std::endl;
} // end bmc_show_version
static void
bmc_check_output_directory(const char*progname)
{
BMC_DEBUG("bmc_check_output_directory progname=" << progname);
struct stat st;
memset (&st, 0, sizeof(st));
if (stat(bmc_out_directory.c_str(), &st))
{
std::cerr << progname << " failed to stat output directory:"
<< strerror(errno) << std::endl;
BMC_FAILURE ("bad output directory (unstated)");
}
if (!S_ISDIR(st.st_mode))
{
std::cerr << progname << " has bad output directory " << bmc_out_directory
<< std::endl;
BMC_FAILURE ("output directory is not one");
}
if ((st.st_mode & S_IRWXU) != S_IRWXU)
{
std::cerr << progname << ": output directory " << bmc_out_directory
<< " is not rwx for user." << std::endl;
BMC_FAILURE ("bad permission on output directory");
}
}
// end bmc_check_output_directory
static void
bmc_check_target_compiler(const char*progname, bool forcplusplus)
{
std::string compiler = forcplusplus ? bmc_target_gxx : bmc_target_gcc;
BMC_DEBUG("bmc_check_target_compiler compiler=" << compiler << " progname=" << progname);
if (compiler.empty())
{
std::cerr << progname << ": no cross-compiler given for " << (forcplusplus?"C++":"C") << std::endl;
BMC_FAILURE (forcplusplus?"missing C++ cross-compiler":"missing C cross-compiler");
}
for (char c: compiler)
{
if (!isalnum(c) && c != '_' && c != '+' && c != '-' && c != '.' && c != '/')
{
std::cerr << progname << ": invalid " << (forcplusplus?"C++":"C")
<< " compiler '" << compiler << "'" << std::endl;
std::cerr << "(only letters, digits, plus, minus, underscore, dot and slash are allowed)" << std::endl;
BMC_FAILURE ("invalid cross-compiler");
}
}
if (bmc_dryrun_flag)
{
std::cout << progname << " should check the target GCC compiler " << compiler << std::endl;
std::cout << "See also http://gcc.gnu.org/ and notice that a GCC 12 compiler" << std::endl;
std::cout << "... is required for BISMON, with plugins enabled." << std::endl;
std::cout << "Try to run your GCC [cross-]compiler with just the -v program option." << std::endl;
}
else
{
std::string compcmd = compiler + " -v 2>&1";
BMC_DEBUG("bmc_check_target_compiler compcmd: " << compcmd);
FILE* compilepipe = popen(compcmd.c_str(), "r");
if (!compilepipe)
{
std::cerr << progname << " failed to popen " << compcmd << " : " << strerror(errno) << std::endl;
BMC_FAILURE ("popen of cross-compiler failed");
}
BMC_DEBUG("bmc_check_target_compiler compilepipe fd#" << fileno(compilepipe) << " pid#" << (int)getpid());
std::string cmdstr;
int gccversion_major=0, gccversion_minor=0;
while (cmdstr.size() < 2048)
{
char linbuf[128];
memset (linbuf, 0, sizeof(linbuf));
errno = 0;
if (!fgets(linbuf, sizeof(linbuf), compilepipe))
{
if (feof(compilepipe))
break;
std::cerr << progname << ": fgets failed on popen " << compcmd << " : " << strerror(errno) << std::endl;
BMC_FAILURE ("fgets failure on cross-compiler");
}
cmdstr.append(linbuf);
if (gccversion_major==0)
sscanf(linbuf, "gcc version %d.%d", &gccversion_major, &gccversion_minor);
}
BMC_DEBUG("bmc_check_target_compiler compcmd: " << compcmd << " got cmdstr " << std::endl
<< "####++++++++++++++++++++++++++++++++++++++++ " << std::endl
<< cmdstr << std::endl
<< "####---------------------------------------- " << compcmd <<std::endl);
BMC_DEBUG("bmc_check_target_compiler gccversion_major=" << gccversion_major << ", gccversion_minor=" << gccversion_minor);
if (gccversion_major != 12 && gccversion_major != 13)
{
std::cerr << progname << ": BISMON requires a GCC 12 or 13 compiler." << std::endl ;
if (gccversion_major > 0)
std::cerr << "But " << compiler << " is a GCC " << gccversion_major << "." << gccversion_minor
<< " compiler." << std::endl;
std::cerr << "See http://gcc.gnu.org/ for more about GCC, and github.com/bstarynk/bismon for more about BISMON." << std::endl;
std::cerr << "However " << compcmd << " gave:" << std::endl
<< cmdstr << std::endl;
BMC_FAILURE ("invalid GCC version");
}
int clcod = pclose(compilepipe);
if (clcod != 0)
{
std::cerr << progname << ": compiler command " << compcmd << " pclose failure (code " << clcod << ")" << std::endl;
BMC_FAILURE ("cross-compiler pclose failure");
}
compilepipe = nullptr;
}
} // end bmc_check_target_compiler
void
bmc_print_config_header(const char*progname)
{
assert (!bmc_out_directory.empty() && bmc_out_directory[bmc_out_directory.size()-1] == '/');
std::string headerpath = bmc_out_directory + "_bm_config.h";
BMC_DEBUG("bmc_print_config_header: headerpath=" << headerpath);
if (!access(headerpath.c_str(), F_OK))
{
if (bmc_force_flag)
{
std::string backup = headerpath + "~";
rename (headerpath.c_str(), backup.c_str());
}
else
{
std::cerr << progname << " did already generate header '" << headerpath
<< "' [" __FILE__ ":" << __LINE__ << "]" << std::endl
<< " (use --force option to overwrite, or maybe run ./distclean-script.bash to clean everything)" << std::endl;
return;
}
}
std::ofstream headoutf (headerpath);
if (!headoutf.good())
{
std::cerr << progname << " failed to open generated header " << headerpath << " : " << strerror(errno) << std::endl;
BMC_FAILURE ("failed to open generated header");
}
headoutf << "/// ¤" "GENERATED¤ Bismon HEADER FILE " << headerpath << " - DO NOT EDIT" << std::endl;
headoutf << "/// See http://github.com/bstarynk/bismon/" << std::endl;
headoutf << "/// generated at " << bmc_start_str_ctime << " on " << bmc_hostname << std::endl;
headoutf << "/// ... using " << __FILE__ "@" << __LINE__ << std::endl
<< "///... compiled on " __DATE__
<< " git " << BISMON_SHORTGIT << std::endl
<< std::endl;
#ifdef BISMON_MAKELEVEL
headoutf << "/// with BISMON_MAKELEVEL=" << BISMON_MAKELEVEL << std::endl;
#endif
headoutf << "#ifndef BISMON_CONFIG" << std::endl;
headoutf << std::endl;
headoutf << "#define BISMON_TIMESTAMP \"" << bismon_timestamp << "\"" << std::endl;
headoutf << "#define BISMON_BUILDTIME " << bismon_timelong << std::endl;
headoutf << "#define BISMON_DIRECTORY \"" << bismon_directory << "\"" << std::endl;
headoutf << "#define BISMON_CHECKSUM \"" << bismon_checksum << "\"" << std::endl;
headoutf << "#define BISMON_GNUMAKEFILE \"" << bismon_gnumakefile << "\"" << std::endl;
headoutf << "#define BISMON_GIT_ID \"" << bismon_gitid << "\"" << std::endl;
headoutf << "#define BISMON_SHORT_GIT_ID \"" << bismon_shortgitid << "\"" << std::endl;
if (!bmc_target_gcc.empty())
headoutf << "#define BISMON_TARGET_GCC \"" << bmc_target_gcc << "\"" << std::endl;
if (!bmc_target_gxx.empty())
headoutf << "#define BISMON_TARGET_GXX \"" << bmc_target_gxx << "\"" << std::endl;
if (!bmc_host_cc.empty())
headoutf << "#define BISMON_HOST_CC \"" << bmc_host_cc << "\"" << std::endl;
if (!bmc_host_cxx.empty())
headoutf << "#define BISMON_HOST_CXX \"" << bmc_host_cxx << "\"" << std::endl;
if (!bmc_out_directory.empty())
headoutf << "#define BISMON_OUT_DIRECTORY \"" << bmc_out_directory << "\"" << std::endl;
if (bmc_debug_flag)
headoutf << "#define BISMON_DEBUG 1" << std::endl;
else
headoutf << "#undef BISMON_DEBUG" << std::endl;
headoutf << std::endl;
headoutf << "#define BISMON_CONFIG \"" << BISMON_SHORTGIT << "\"" << std::endl;
headoutf << "#endif /*BISMON_CONFIG*/" << std::endl;
headoutf << "// end of Bismon generated configuration header file " << headerpath
<< std::endl << std::flush;
sync();
usleep (1300);
BMC_DEBUG("bmc_print_config_header ending headerpath=" << headerpath);
if (!bmc_batch_flag && !bmc_silent_flag)
{
std::cout << "#: ¤" "GENERATED¤ Bismon configuration header file " << headerpath << std::endl;
std::cout << "#: generated at " << bmc_start_str_ctime << " on " << bmc_hostname << std::endl;
std::cout << "#: ... using " << __FILE__ "@" << __LINE__ << std::endl
<< "#: ... compiled on " __DATE__
<< " git " << BISMON_SHORTGIT << std::endl
<< std::endl;
#ifdef BISMON_MAKELEVEL
std::cout << "#:: with BISMON_MAKELEVEL=" << BISMON_MAKELEVEL << std::endl;
#endif
struct stat headerstat;
memset (&headerstat, 0, sizeof(headerstat));
if (stat(headerpath.c_str(), &headerstat))
BMC_FAILURE((std::string("failed to stat the generated header file ") + headerpath + ":" + strerror(errno)).c_str());
std::cout << "#:" << progname
<< " [" __FILE__ ":" << __LINE__ << "°" BISMON_SHORTGIT "] "
<< " did generate ..." << std::endl
<< "#: the header file " << headerpath
<< " with " << headerstat.st_size << " bytes."
<< std::endl << std::endl;
}
} // end bmc_print_config_header
void
bmc_print_config_data(const char*progname)
{
assert (!bmc_out_directory.empty() && bmc_out_directory[bmc_out_directory.size()-1] == '/');
#define BMC_GITLS_COMMAND "git ls-files"
std::string datapath = bmc_out_directory + "_bm_config.c";
BMC_DEBUG("bmc_print_config_data: datapath=" << datapath);
if (!access(datapath.c_str(), F_OK))
{
if (bmc_force_flag)
{
std::string backup = datapath + "~";
rename (datapath.c_str(), backup.c_str());
}
else
{
std::cerr << progname << " did already generate configured data '" << datapath
<< "' [" __FILE__ ":" << __LINE__ << "]" << std::endl
<< " (use --force option to overwrite, or maybe run ./distclean-script.bash to clean everything)" << std::endl;
return;
}
}
std::ofstream dataoutf (datapath);
if (!dataoutf.good())
{
std::cerr << progname << " failed to open generated data " << datapath << " : " << strerror(errno) << std::endl;
BMC_FAILURE ("failed to open generated data");
}
dataoutf << "/// *** ¤" "GENERATED¤ Bismon DATA FILE " << datapath << " - DO NOT EDIT ***" << std::endl;
dataoutf << "/// See github.com/bstarynk/bismon for more about Bismon." << std::endl << std::endl;
dataoutf << "/// This file '" << basename(datapath.c_str())
<< "' was generated by " __FILE__ "@" << __LINE__ << std::endl
<< "///... compiled on " __DATE__
<< " git " BISMON_SHORTGIT << std::endl;
dataoutf << "const char bismon_confdata_gitid[]=\"" << bismon_gitid << "\";" << std::endl;
#ifdef BISMON_MAKELEVEL
dataoutf << "/// with BISMON_MAKELEVEL=" << BISMON_MAKELEVEL << std::endl;
#endif
FILE*gitpipe = popen(BMC_GITLS_COMMAND, "r");
if (!gitpipe)
{
std::cerr << progname << " failed to popen " BMC_GITLS_COMMAND << ":" << strerror(errno) << std::endl;
BMC_FAILURE (BMC_GITLS_COMMAND " failed");
}
std::vector<std::string> vecgitfilepath;
std::vector<std::string> vecgitlinkpath;
std::vector<std::string> vecgitdirpath;
int linenopip = 0;
do
{
char gitlinbuf[128];
memset (gitlinbuf, 0, sizeof(gitlinbuf));
if (!fgets(gitlinbuf, sizeof(gitlinbuf), gitpipe))
break;
int linl = (int)strlen(gitlinbuf);
if (linl > 0 && gitlinbuf[linl-1] == '\n')
gitlinbuf[linl-1] = (char)0;
std::string gitlinstr;
linenopip ++;
BMC_DEBUG("bmc_print_config_data gitls #" << linenopip << ":" << gitlinbuf);
/// skip files like .gdbinit or .indent.pro, etc..
if (gitlinbuf[0] == '.' && isalpha(gitlinbuf[1]))
continue;
if (isalnum(gitlinbuf[0]))
gitlinstr.assign(gitlinbuf);
{
int rk= -1, endpos= -1;
if (linl>12 && sscanf(gitlinbuf, "store%d-BISMON.bmon%n", &rk, &endpos) > 1
&& rk >0 && endpos >= linl-1)
{
BMC_DEBUG("bmc_print_config_data gitls store file rk=" << rk
<< " endpos=" << endpos);
gitlinstr.assign(gitlinbuf+1, endpos-2);
}
}
BMC_DEBUG("bmc_print_config_data gitls #" << linenopip << ", gitlinstr='" << gitlinstr << "'");
if (gitlinstr.empty())
continue;
if (access(gitlinstr.c_str(), R_OK))
{
int accerr= errno;
std::ostringstream errout;
errout << "bmc_print_config_data cannot access git versioned file '" << gitlinstr << "' : " << strerror(accerr) << std::endl;
errout << "... from line#" << linenopip << " given by command "
<< BMC_GITLS_COMMAND << std::flush;
BMC_FAILURE(errout.str().c_str());
};
for (int cix=0; cix<linl && gitlinbuf[cix]; cix++)
{
if (!isalnum(gitlinbuf[cix])
&& gitlinbuf[cix] != '_' && gitlinbuf[cix] != '/'
&& gitlinbuf[cix] != '+' && gitlinbuf[cix] != '-'
&& gitlinbuf[cix] != '.'
&& !strstr(gitlinbuf, "README"))
{
BMC_DEBUG("bmc_print_config_data bad gitlinbuf='" << gitlinbuf << "' cix=" << cix << " linl=" << linl);
char cwdbuf[256];
memset(cwdbuf, 0, sizeof(cwdbuf));
if (!getcwd(cwdbuf, sizeof(cwdbuf)-1))
cwdbuf[0] = '.';
std::cerr << progname << " pipe " << BMC_GITLS_COMMAND << " output line#" << linenopip << ":" << gitlinbuf
<< " - unexpected file name "
<< gitlinbuf
<< " in directory " << cwdbuf << std::endl;
std::cerr << "Expecting letters, digits, or one of '_/+-.§' characters."
<< " [" __FILE__ ":" << __LINE__ << "]"
<< std::endl;
BMC_FAILURE ("bad file name in directory");
}
}
struct stat curgitst;
memset(&curgitst, 0, sizeof(curgitst));
if (stat(gitlinbuf, &curgitst))
{
std::cerr << progname << " pipe " << BMC_GITLS_COMMAND << " output line#" << linenopip << ":" << gitlinbuf
<< " - stat(2) failed:" << strerror(errno) << std::endl;
BMC_FAILURE ("stat failed for " BMC_GITLS_COMMAND);
}
switch(curgitst.st_mode & S_IFMT)
{
case S_IFREG:
vecgitfilepath.push_back(std::string{gitlinbuf});
BMC_DEBUG("bmc_print_config_data git file " << gitlinbuf);
if (linl>5 && !strcmp(gitlinbuf+linl-4, "BM.c"))
{
BMC_DEBUG("bmc_print_config_data git source " << gitlinbuf);
bmc_source_files.push_back(std::string{gitlinbuf});
}
break;
case S_IFLNK:
vecgitlinkpath.push_back(std::string{gitlinbuf});
BMC_DEBUG("bmc_print_config_data git symlink " << gitlinbuf);
break;
case S_IFDIR:
vecgitdirpath.push_back(std::string{gitlinbuf});
BMC_DEBUG("bmc_print_config_data git directory " << gitlinbuf);
break;
default:
std::cerr << progname << ": unexpected git-listed file " << gitlinbuf << " (not a plain file, or symlink, or directory)" << std::endl;
BMC_FAILURE ("unexpected git-ed file");
} // end switch ...
}
while (!feof(gitpipe));
int pclocod = pclose(gitpipe);
if (pclocod != 0)
{
std::cerr << progname << " pipe " << BMC_GITLS_COMMAND << " pclose failed (code " << pclocod << ")" << std::endl;
BMC_FAILURE (BMC_GITLS_COMMAND " failed to pclose");
}
gitpipe = nullptr;
dataoutf << "const char*const bismonconf_git_files[] = {" << std::endl;
for (auto filepath: vecgitfilepath)
dataoutf << " \"" << filepath << "\"," << std::endl;
dataoutf << " (const char*)0 }; // end bismonconf_git_files" << std::endl;
dataoutf << "const char*const bismonconf_git_symlinks[] = {" << std::endl;
for (auto linkpath: vecgitlinkpath)
dataoutf << " \"" << linkpath << "\"," << std::endl;
dataoutf << " (const char*)0 }; // end bismonconf_git_symlinks" << std::endl;
dataoutf << "const char*const bismonconf_git_dirs[] = {" << std::endl;
for (auto dirpath: vecgitdirpath)
dataoutf << " \"" << dirpath << "/\"," << std::endl;
dataoutf << " (const char*)0 }; // end bismonconf_git_dirs" << std::endl;
dataoutf << "const char*const bismonconf_git_sources[] = {" << std::endl;
for (auto srcpath: bmc_source_files)
dataoutf << " \"" << srcpath << "\"," << std::endl;
dataoutf << " (const char*)0 }; // end bismonconf_git_sources" << std::endl;
//
dataoutf << std::endl << "/// end of Bismon generated data " << datapath << std::endl;
sync ();
usleep (1300);
BMC_DEBUG("bmc_print_config_data ending datapath=" << datapath);
if (!bmc_batch_flag && !bmc_silent_flag)
{
std::cout << "#: generated Bismon configuration data file " << datapath << std::endl;
struct stat datastat;
memset (&datastat, 0, sizeof(datastat));
if (stat(datapath.c_str(), &datastat))
BMC_FAILURE((std::string("failed to stat the generated data file ") + datapath + ":" + strerror(errno)).c_str());
std::cout << "#:" << progname
<< " [" __FILE__ ":" << __LINE__ << "°" BISMON_SHORTGIT "] "
<< " did generate" << std::endl
<< "#: ... the data file " << datapath << " with "
<< datastat.st_size << " bytes." << std::endl << std::endl;
}
} // end bmc_print_config_data
void
bmc_print_config_make(const char*progname)
{
assert (!bmc_out_directory.empty() && bmc_out_directory[bmc_out_directory.size()-1] == '/');
std::string makepath = bmc_out_directory + "_bismon-config.mk";
BMC_DEBUG("bmc_print_config_make: makepath=" << makepath);
if (!access(makepath.c_str(), F_OK))
{
if (bmc_force_flag)
{
std::string backup = makepath + "~";
rename (makepath.c_str(), backup.c_str());
}
else
{
std::cerr << progname << " did already generate makefile '" << makepath
<< "' [" __FILE__ ":" << __LINE__ << "]" << std::endl
<< " (use --force option to overwrite, or maybe run ./distclean-script.bash to clean everything)" << std::endl;
return;
}
}
std::ofstream makeoutf (makepath);
if (!makeoutf.good())
{
std::cerr << progname << " failed to open generated file " << makepath << " for GNU make : " << strerror(errno) << std::endl;
BMC_FAILURE ("failed to output make configuration file");
}
/// make prologue
makeoutf << "### ¤" "GENERATED¤ Bismon GNU-MAKE CONFIGURATION FILE " <<
makepath << " - DO NOT EDIT" << std::endl;
makeoutf << "### This file '" << basename(makepath.c_str())
<< "' was generated by " __FILE__ "@" << __LINE__ << std::endl
<< "### ... compiled on " __DATE__
<< " git " BISMON_SHORTGIT << std::endl;
makeoutf << "### this is for inclusion thru GNU make." << std::endl;
makeoutf << "### See github.com/bstarynk/bismon/ for more about Bismon." << std::endl;
#ifdef BISMON_MAKELEVEL
makeoutf << "### with BISMON_MAKELEVEL=" << BISMON_MAKELEVEL << std::endl;
#endif
makeoutf << "BISMONMK_TIMESTAMP=" << bismon_timestamp << std::endl;
makeoutf << "BISMONMK_BUILDTIME=" << bismon_timelong << std::endl;
makeoutf << "BISMONMK_DIRECTORY=" << bismon_directory << std::endl;
makeoutf << "BISMONMK_CHECKSUM=" << bismon_checksum << std::endl;
makeoutf << "BISMONMK_MAKEFILE=" << bismon_gnumakefile << std::endl;
makeoutf << "BISMONMK_GITID=" << bismon_gitid << std::endl;
makeoutf << "BISMONMK_SHORTGITID=" << bismon_shortgitid << std::endl;
makeoutf << "#without BISMONMK_gtk" << std::endl;
makeoutf << "BISMONMK_OBJECTS= $(BM_OBJECTS)" << std::endl;
errno = 0;
makeoutf << std::endl
<< "##: BISMONMK_HOST_CC from " << __FILE__ ":" << __LINE__ << std::endl;
if (!bmc_host_cc.empty() && !access(bmc_host_cc.c_str(), X_OK))
makeoutf << "BISMONMK_HOST_CC=" << bmc_host_cc << std::endl;
else if (!access("/usr/bin/gcc", X_OK))
{
makeoutf << "#guessed BISMONMK_HOST_CC - GCC - from " __FILE__ ":" << __LINE__ << std::endl
<< "BISMONMK_HOST_CC=/usr/bin/gcc" << std::endl;
}
else if (!access("/usr/bin/clang", X_OK))
{
makeoutf << "#guessed BISMONMK_HOST_CC - Clang - from " __FILE__ ":" << __LINE__ << std::endl
<< "BISMONMK_HOST_CC=/usr/bin/clang" << std::endl;
}
else
{
int er = errno;
makeoutf << "#without BISMONMK_HOST_CC";
if (!bmc_host_cc.empty() && er != 0)
makeoutf << " - bad " << bmc_host_cc << " : " << strerror(er);
makeoutf << std::endl;
}
errno = 0;
makeoutf << std::endl
<< "##: BISMONMK_HOST_CXX from " << __FILE__ ":" << __LINE__ << std::endl;
if (!bmc_host_cxx.empty() && !access(bmc_host_cxx.c_str(), X_OK))
makeoutf << "BISMONMK_HOST_CXX=" << bmc_host_cxx << std::endl;
else if (!access("/usr/bin/g++", X_OK))
{
makeoutf << "#guessed BISMONMK_HOST_CXX - GCC - from " __FILE__ ":" << __LINE__ << std::endl
<< "BISMONMK_HOST_CXX=/usr/bin/g++" << std::endl;
}
else if (!access("/usr/bin/clang++", X_OK))
{
makeoutf << "#guessed BISMONMK_HOST_CXX - Clang - from " __FILE__ ":" << __LINE__ << std::endl
<< "BISMONMK_HOST_CXX=/usr/bin/clang++" << std::endl;
}
else
{
int er = errno;
makeoutf << "#without BISMONMK_HOST_CXX" ;
if (!bmc_host_cxx.empty() && er != 0)
makeoutf << " - bad " << bmc_host_cxx << " : " << strerror(er);
makeoutf << std::endl;
}
makeoutf << "BISMONMK_PACKAGES= glib-2.0 gobject-2.0 jansson readline" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_EXECUTABLE stuff from " << __FILE__ ":" << __LINE__ << std::endl;
makeoutf << "BISMONMK_EXECUTABLE= bismon" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_TARGET_GCC from " << __FILE__ ":" << __LINE__ << std::endl;
if (!bmc_target_gcc.empty())
makeoutf << "BISMONMK_TARGET_GCC=" << bmc_target_gcc << std::endl;
else
makeoutf << "#no BISMONMK_TARGET_GCC" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_TARGET_GXX from " << __FILE__ ":" << __LINE__ << std::endl;
if (!bmc_target_gxx.empty())
makeoutf << "BISMONMK_TARGET_GXX=" << bmc_target_gxx << std::endl;
else
makeoutf << "#no BISMONMK_TARGET_GXX" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_OUT_DIRECTORY from " << __FILE__ ":" << __LINE__ << std::endl;
if (!bmc_out_directory.empty())
makeoutf << "BISMONMK_OUT_DIRECTORY=" << bmc_out_directory << std::endl;
else
makeoutf << "#no BISMONMK_OUT_DIRECTORY" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_DEBUG from " << __FILE__ ":" << __LINE__ << std::endl;
if (bmc_debug_flag)
makeoutf << "BISMONMK_DEBUG=1" << std::endl;
else
makeoutf << "#no BISMONMK_DEBUG" << std::endl;
makeoutf << std::endl
<< "##: BISMONMK_CONFIGPATH from " << __FILE__ ":" << __LINE__ << std::endl;
makeoutf << "BISMONMK_CONFIGPATH=" << makepath << std::endl;
makeoutf << std::endl;
makeoutf << "### end of Bismon generated file for GNU make " << makepath << " (by " << __FILE__ << ")"
<< std::endl << std::flush;
BMC_DEBUG("bmc_print_config_make ending makepath=" << makepath);
sync();
usleep (1300);
if (!bmc_batch_flag && !bmc_silent_flag)
{
std::cout << "#: generated Bismon configuration GNU make file " << makepath << std::endl;
struct stat makestat;
memset (&makestat, 0, sizeof(makestat));
if (stat(makepath.c_str(), &makestat))
BMC_FAILURE((std::string("failed to stat the generated GNU make file ") + makepath + ":" + strerror(errno)).c_str());
std::cout << "#:" << progname
<< " [" __FILE__ ":" << __LINE__ << "°" BISMON_SHORTGIT "] "
<< " did generate" << std::endl
<< "#:... GNU make file " << makepath << " with " << makestat.st_size << " bytes." << std::endl << std::endl;
}
} // end bmc_print_config_make
const int bmc_ninja_rules_lineno = __LINE__+2;
const char bmc_ninja_rules[] =
R"NinjaRules(
# our hardcoded rules for ninja - conventionally ended by _rlBM
# see https://ninja-build.org/manual.html
###########
# compilation of _BM.c files
rule CC_rlBM
description = CC_rlBM $out < $in (handwritten C code) ° $depfile
command = $NJBM_host_cc -c $NJBM_host_warn_flags $NJBM_host_cwarn_flags $
$NJBM_host_optim_flags $NJBM_host_debug_flags $
$NJBM_host_prepro_flags $NJBM_pkgconfig_cflags $
-MD -MF $depfile $
$in -o $out
# compilation of _BM.cc files - handwritten C++ code
rule CXX_rlBM
description = CXX_rlBM $out < $in (handwritten C++ code) ° $depfile
command = $NJBM_host_cxx -c $NJBM_host_warn_flags $
$NJBM_host_optim_flags $NJBM_host_debug_flags $
$NJBM_host_prepro_flags $NJBM_pkgconfig_cflags $
-MD -MF $depfile $
$in -o $out
# compilation of generated modules/modbm_.c into a modubin/*.so shared object
rule MODCC_rlBM
description = MODCC_rlBM $out < $in (generated C module) ° $depfile
command = $NJBM_host_cxx -fPIC -shared $NJBM_host_warn_flags $
$NJBM_host_optim_flags $NJBM_host_debug_flags $
$NJBM_host_prepro_flags $NJBM_pkgconfig_cflags $
-MD -MF $depfile $
$in -o $out
# linking of all Bismon
rule LINKALLBISMON_rlBM
description = LINKALLBISMON_rlBM (link everything into $out)
command = $NJBM_host_cxx $NJBM_host_warn_flags $
$NJBM_host_optim_flags $NJBM_host_debug_flags $
$in $NJBM_pkgconfig_libs $
-o $out
################### end of NinjaRules ###################
)NinjaRules";
// see file readline.c near line 250 of GNU readline 8.1
extern "C" char* rl_line_buffer;
void
bmc_set_readline_buffer(const std::string& str)
{
if (str.empty())
return;
unsigned slen = str.size();
unsigned roundlen = ((slen + 3) | 0xff) + 1;
rl_extend_line_buffer (roundlen);
memcpy (rl_line_buffer, str.c_str(), slen);
rl_point = 0;
add_history(str.c_str());
} // end bmc_set_readline_buffer
void
bmc_print_config_ninja(const char*progname)
{
assert (!bmc_out_directory.empty() && bmc_out_directory[bmc_out_directory.size()-1] == '/');
std::string ninjapath = bmc_out_directory + "/" + bmc_ninja_file;
BMC_DEBUG("bmc_print_config_make ninjapath=" << ninjapath);
if (!access(ninjapath.c_str(), F_OK))
{
if (bmc_force_flag)
{
std::string backup = ninjapath + "~";
rename (ninjapath.c_str(), backup.c_str());
}
else
{