-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
910 lines (799 loc) · 25.1 KB
/
main.go
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
// dcc - dependency-driven C/C++ compiler front end
//
// Copyright © A.Newman 2015.
//
// This source code is released under version 2 of the GNU Public License.
// See the file LICENSE for details.
//
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
)
const (
// The default "hidden" directory where options files reside.
//
DefaultDccDir = ".dcc"
// The default "hidden" directory where compiler-generated
// dependency files reside.
//
DefaultDepsDir = ".dcc.d"
)
var (
// Myname is the program's invocation name. We use it to
// prefix log messages and check it to see if we should
// work as a C++ compiler driver.
//
Myname string
// DccCurrentDirectory is the path of the process working directory at startup
//
DccCurrentDirectory = MustGetwd()
// IgnoreDependencies has dcc not perform any dependency checking
// and makes it assume everything is out of date, forcing everything
// to be rebuilt.
//
// This is set by the --force command line option.
//
IgnoreDependencies bool
// ActualCompiler is the Compiler (compiler.go) for the real
// compiler executable. The Compiler type abstracts the
// functions of the underlying compiler and lets dcc work with
// different compilers, i.e. Microsoft's cl.exe requires
// different handling to the gcc-style compilers.
//
ActualCompiler Compiler
// DefaultNumJobs is the number of concurrent compilations
// dcc will perform by default. The "2 + numcpu" is the
// same value used by the ninja build tool.
//
DefaultNumJobs = 2 + runtime.NumCPU()
// NumJobs is the number of concurrent compilations dcc will
// perform. By default this is two times the number of CPUs.
// Why? Well basically because that's what works well for me.
// The old rule-of-thumb used to be 1+NCPU but we do more I/O
// in our builds these days (C++ especially) so there's more
// I/O to overlap. Ninja uses 2+NCPU by default and dcc
// used to do that but 2*NCPU works better for what I build
// on my machines (not so modern C++, SSDs, 4/8 cores).
//
NumJobs = GetenvInt("NUMJOBS", DefaultNumJobs)
// DccDir is the name of the directory where dcc-related files
// are stored. If this directory does not exist the current
// directory is used. DccDir defaults to ".dcc" allowing users
// to "hide" the various build files by putting them in a "dot"
// directory (which makes for cleaner source directories).
//
DccDir = Getenv("DCCDIR", DefaultDccDir)
// DepsDir is the name of the directory where the per-object
// file dependency files (.d files) are stored. This is usually
// ".dcc.d" except on windows.
//
DepsDir = Getenv("DEPSDIR", DefaultDepsDir)
// ObjsDir is the name of the directory where object files
// are written. It is usually a relative path, relative to
// the source file.
//
ObjsDir = "."
// Quiet disables most messages when true. Usually dcc will
// print, to stderr, the commands being executed.
//
Quiet = false
// Verbose, when true, enables various messages from dcc.
//
Verbose = false
// Debug, when true, enables debug output.
//
Debug = false
// Debug "FindFile" operations when true.
//
DebugFind = false
// CCFILE is the name of the "options file" that defines
// the name of the C compiler.
//
CCFILE = Getenv("CCFILE", "CC")
// CXXFILE is the name of the "options file" that defines
// the name of the C++ compiler.
//
CXXFILE = Getenv("CXXFILE", "CXX")
// CFLAGSFILE is the name of the "options file" that
// defines the options passed to the C compiler.
//
CFLAGSFILE = Getenv("CFLAGSFILE", "CFLAGS")
// CXXFLAGSFILE is the name of the "options file" that
// defines the options passed to the C++ compiler.
//
CXXFLAGSFILE = Getenv("CXXFLAGSFILE", "CXXFLAGS")
// LDFLAGSFILE is the name of the "options file" that defines
// the options passed to the linker (aka "loader").
//
LDFLAGSFILE = Getenv("LDFLAGSFILE", "LDFLAGS")
// LIBSFILE is the name of the "options file" that defines
// the names of the libraries used when linking.
//
LIBSFILE = Getenv("LIBSFILE", "LIBS")
)
func main() {
Myname = GetProgramName()
ConfigureLogger(Myname)
// Enable debug as early as possible.
if s := os.Getenv("DCCDEBUG"); s != "" {
if s == "find" {
DebugFind = true
} else if s == "all" {
Debug = true
DebugFind = true
} else {
Debug = true
}
} else {
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--debug" {
Debug = true
}
if os.Args[i] == "--debug-find" {
DebugFind = true
}
}
}
if !Debug {
defer CatchPanics()
}
runningMode := ModeNotSpecified
outputPathname := ""
dasho := ""
dashm := ""
writeCompileCommands := false
appendCompileCommands := false
cCompiler := makeCompilerOption(CCFILE, platform.DefaultCC)
cppCompiler := makeCompilerOption(CXXFILE, platform.DefaultCXX)
// We assume we're compiling C and define a function to switch
// things to C++. We'll check for that next.
//
underlyingCompiler, optionsFilename := cCompiler, CFLAGSFILE
cplusplus := func() {
underlyingCompiler, optionsFilename = cppCompiler, CXXFLAGSFILE
}
// Rules for deciding if we're need to compile with C++:
//
// - our invocation name ends in "++"
// - the --cpp option was supplied
// - any of the input files are C++ source files
//
if strings.HasSuffix(Myname, "++") {
cplusplus()
} else {
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--cpp" {
cplusplus()
break
}
if os.Args[i][0] != '-' && IsCPlusPlusFile(os.Args[i]) {
cplusplus()
break
}
}
}
// The state...
//
// Options can be read from files, other state variables are
// set by interpreting command line options. The
// sourceFileIndex is explained below, basically it defines
// which files in inputFilenames are the names of source files.
//
var (
err error
compilerOptions = new(Options)
linkerOptions = new(Options)
libraryFiles = new(Options)
otherFiles = new(Options)
libraryDirs = make([]string, 0)
inputFilenames = make([]string, 0)
sourceFilenames = make([]string, 0)
sourceFileIndex = make([]int, 0) // position of sourceFilenames[x] within inputFilenames
frameworks = make([]string, 0)
)
// The default set of libraryDirs comes from the platform's standard directories.
//
libraryDirs = append(libraryDirs, platform.LibraryPaths...)
// Get compiler options from the options file.
//
if path, found := FindFile(optionsFilename); found {
_, err := compilerOptions.ReadFromFile(path, nil)
if err != nil {
log.Fatal(err)
}
}
// A -o in an options file is problematic, we keep that information elsewhere.
// So we have to look for a -o and transfer it's value to our variable and
// remove it from the options.
//
if index := compilerOptions.OptionIndex("-o"); index != -1 {
if index == len(compilerOptions.Values)-1 {
log.Fatalf("invalid -o option in compiler options file %q", optionsFilename)
}
dasho = compilerOptions.Values[index+1]
compilerOptions.Values = append(compilerOptions.Values[0:index], compilerOptions.Values[index+2:]...)
}
// We use the compilerOptions modtime as a dependency but in
// the unlikely case the compiler's modtime is newer we'll use
// that instead - automatic rebuilds when the compiler
// changes (v.unsafe - ref. Thompson's Reflections on Trust).
//
compilerOptions.SetModTime(MostRecentModTime(compilerOptions, underlyingCompiler))
// Now we do the same for the linker options. We use the "LDFLAGS" file.
//
if path, found := FindFile(LDFLAGSFILE); found {
_, err = linkerOptions.ReadFromFile(path, func(s string) string {
// Collect directories named via -L in libraryDirs
if strings.HasPrefix(s, "-L") {
libraryDirs = append(libraryDirs, s[2:])
}
return s
})
}
setLinkOpts := false
if os.IsNotExist(err) {
setLinkOpts = true
// remember that we need to set the linker options
// from the compiler options ... but only when
// linking an executable.
// linkerOptions.SetFrom(compilerOptions)
} else if err != nil {
log.Fatal(err)
}
// Libraries
//
if err = readLibs(LIBSFILE, libraryFiles, &libraryDirs, &frameworks); err != nil {
log.Fatal(err)
}
if Debug {
log.Printf("LIBS: %d libraries, %d dirs, %d frameworks", libraryFiles.Len(), len(libraryDirs), len(frameworks))
log.Println("LIBS:", libraryFiles.String())
log.Printf("LIBS: paths %v", libraryDirs)
log.Printf("LIBS: frameworks %v", frameworks)
}
// Helper to set our running mode, once and once only.
//
setMode := func(arg string, mode RunningMode) {
if runningMode != ModeNotSpecified {
fmt.Fprintf(os.Stderr, "%s: a running mode has already been defined\n\n", arg)
UsageError(os.Stderr, 1)
}
runningMode = mode
}
// Helper to take a filename supplied on the command line
// and incorporate it in the appropriate place according
// to its type.
//
collectInputFile := func(path string) {
inputFilenames = append(inputFilenames, path)
switch {
case FileWillBeCompiled(path):
sourceFilenames = append(sourceFilenames, path)
sourceFileIndex = append(sourceFileIndex, len(inputFilenames)-1)
case IsLibraryFile(path):
libraryFiles.Append(path)
default:
otherFiles.Append(path)
}
}
// Helper to issue a warning message re. the -o option being set more
// that once and that we accept the first -o to set an output path
// and ignore subsequent ones.
//
warnOutputPathAlreadySet := func(ignoredPath string) {
log.Printf("warning: output pathname has already been set, ignoring this -o (%s)", ignoredPath)
}
// Windows is special... When using Microsoft's "cl.exe"
// as the underlying compiler, on Windows, we need to support
// its command line option syntax which allows for '/' as an
// option specifier.
//
// Likewise darwin (MacOS) is special in that it has the
// concept of frameworks.
//
windows := runtime.GOOS == "windows"
macos := runtime.GOOS == "darwin"
// Go over the command line and collect option and the names of
// source files to be compiled and/or files to pass to the linker.
//
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
switch {
case arg == "--help":
UsageError(os.Stdout, 0)
case windows && arg[0] == '/':
// Compiler option for cl.exe
//
switch {
case arg == "/c":
setMode(arg, CompileSourceFiles)
case strings.HasPrefix(arg, "/Fo"):
dasho = arg[3:]
case strings.HasPrefix(arg, "/Fe"):
outputPathname = arg[3:]
default:
if _, err := Stat(arg); err != nil {
compilerOptions.Append(arg)
compilerOptions.Changed()
} else {
collectInputFile(arg)
}
}
case arg[0] != '-':
// Non-option arguments are filenames, possibly sources to compile or
// files to pass to the linker - classical cc(1) behaviour.
//
collectInputFile(arg)
case arg == "--write-compile-commands":
writeCompileCommands = true
case arg == "--append-compile-commands":
appendCompileCommands = true
case arg == "--cpp":
break // ignore, handled above
case arg == "--force":
IgnoreDependencies = true
case arg == "--quiet":
Quiet = true
case arg == "--verbose":
Verbose = true
case arg == "--debug":
break // also handled above
case arg == "--version":
fmt.Print(versionNumber)
os.Exit(0)
case strings.HasPrefix(arg, "-j"):
if arg == "-j" {
NumJobs = runtime.NumCPU()
} else if n, err := strconv.Atoi(arg[2:]); err != nil {
log.Fatalf("%s: %s", arg, err)
} else if n < 1 {
log.Fatalf("%s: bad number of jobs", arg)
} else {
NumJobs = n
}
case arg == "--objdir":
if i++; i < len(os.Args) {
ObjsDir = os.Args[i]
} else {
log.Fatalf("%s: directory name required", arg)
}
case arg == "--exe":
if i++; i < len(os.Args) {
outputPathname = os.Args[i]
} else {
log.Fatalf("%s: program filename required", arg)
}
setMode(arg, CompileAndLink)
case arg == "--lib":
if i++; i < len(os.Args) {
outputPathname = os.Args[i]
} else {
log.Fatalf("%s: library filename required", arg)
}
setMode(arg, CompileAndMakeLib)
case arg == "--dll":
if i++; i < len(os.Args) {
outputPathname = os.Args[i]
} else {
log.Fatalf("%s: library filename required", arg)
}
setMode(arg, CompileAndMakeDLL)
case arg == "--plugin":
if i++; i < len(os.Args) {
outputPathname = os.Args[i]
} else {
log.Fatalf("%s: library filename required", arg)
}
setMode(arg, CompileAndMakePlugin)
case macos && arg == "-framework":
libraryFiles.Append(arg)
if i++; i < len(os.Args) {
libraryFiles.Append(os.Args[i])
}
libraryFiles.Changed()
case macos && (arg == "-macosx_version_min" || arg == "-macosx_version_max"):
linkerOptions.Append(arg)
if i++; i < len(os.Args) {
linkerOptions.Append(os.Args[i])
}
linkerOptions.Changed()
case macos && arg == "-bundle_loader":
linkerOptions.Append(arg)
if i++; i < len(os.Args) {
linkerOptions.Append(os.Args[i])
}
linkerOptions.Changed()
case arg == "-o":
if i++; i < len(os.Args) {
if outputPathname != "" {
warnOutputPathAlreadySet(outputPathname)
} else {
dasho = os.Args[i]
}
} else {
log.Fatal(arg + " pathname parameter required")
}
case strings.HasPrefix(arg, "-o"):
if outputPathname != "" {
warnOutputPathAlreadySet(outputPathname)
} else {
dasho = arg[2:]
}
case strings.HasPrefix(arg, "-L"):
linkerOptions.Append(arg)
linkerOptions.Changed()
libraryDirs = append(libraryDirs, arg[2:])
case strings.HasPrefix(arg, "-l"):
libraryFiles.Prepend(arg)
case arg == "-c":
setMode(arg, CompileSourceFiles)
case arg == "-m32":
if dashm != "" && dashm != arg {
log.Fatalf("conflicting options %s and %s", dashm, arg)
}
dashm = arg
case arg == "-m64":
if dashm != "" && dashm != arg {
log.Fatalf("conflicting options %s and %s", dashm, arg)
}
dashm = arg
default:
compilerOptions.Append(arg)
compilerOptions.Changed()
}
}
// We have to at least have one filename to process. It doesn't
// need to be a source file but we need something.
//
if len(inputFilenames) == 0 {
UsageError(os.Stderr, 1)
}
// ----------------------------------------------------------------
// If no mode was explicitly specified compile and link like cc(1).
//
if runningMode == ModeNotSpecified {
runningMode = CompileAndLink
}
// Deal with any -m32/-m64 option
//
if dashm != "" {
if platform.SelectTarget != nil {
err = platform.SelectTarget(&platform, dashm)
}
if err != nil {
log.Fatal(err)
}
}
// Deal with any -o option.
//
// The cc(1) behaviour is:
//
// - without -c the -o's parameter names the executable
// - -c -o<path> names the object file but is only permitted when compiling a single file
//
// Dcc should add:
//
// -c -o <path> permitted with multiple files if <path> names
// a directory.
//
if dasho != "" && runningMode == CompileSourceFiles && len(sourceFilenames) > 1 {
log.Fatal("-o <file> may not be supplied with -c and more one input file")
}
// outputPathname will be empty if no dcc-specific option that sets it
// was used. If a -o was supplied we propogate its value to outputPathname.
//
if outputPathname == "" && dasho != "" {
outputPathname = dasho
dasho = ""
}
// objdir is the object file directory.
//
objdir := dasho
if objdir == "" {
objdir = ObjsDir
}
// Next, replace any source file names with their object file
// name in the inputFilenams slice. This is then the list of
// files given to the linker, or librarian. During this
// replacement we replace any header file names with empty
// strings as they are not used as inputs to the linker or
// librarian. We do this so we don't invalidate the indices
// stored in the sourceFileIndex slice during the loop. The
// empty strings are removed once we've done this pass over
// the names. And we only do all this if we need to since
// pre-compiling headers is relatively rare.
//
removeEmptyNames := false
for index, filename := range sourceFilenames {
if IsSourceFile(filename) {
inputFilenames[sourceFileIndex[index]] = ObjectFilename(filename, objdir)
} else if IsHeaderFile(filename) {
inputFilenames[sourceFileIndex[index]] = ""
removeEmptyNames = true
}
}
// NB. sourceFileIndex is no longer used/required and we're
// possibly about to re-write th inputFilenames slice which
// would invalidate the sourceFileIndex anyway so we nil it
// to help detect now invalid accesses.
//
sourceFileIndex = nil
// We typically don't do this. Empty strings only end up in
// inputFilenames if a header file is being pre-compiled.
//
if removeEmptyNames {
tmp := make([]string, 0, len(inputFilenames))
for _, filename := range inputFilenames {
if filename != "" {
tmp = append(tmp, filename)
}
}
inputFilenames = tmp
// If there are no inputs remaining then we do not need to
// link (or create a library).
//
if len(inputFilenames) == 0 {
runningMode = CompileSourceFiles
}
}
// Update any -l<name> library references in LibraryFiles with
// the actual file path. We want to "stat" these files to determine
// if they're newer than the executable/DLL that depends on them.
//
for index, name := range libraryFiles.Values {
if strings.HasPrefix(name, "-l") {
path, _, found, err := FindLibrary(libraryDirs, name[2:])
switch {
case err != nil:
log.Fatal(err)
case !found:
log.Printf("warning: %q library not found on path %v", name, libraryDirs)
// ... and we let the linker deal with it
default:
libraryFiles.Values[index] = path
if Debug {
log.Print("DEBUG LIB: '", name[2:], "' -> ", path)
}
}
}
}
// Set the actual compiler we'll be using.
//
ActualCompiler = GetCompiler(underlyingCompiler.String())
// Generate a compile_commands.json if requested.
//
if appendCompileCommands {
if err := AppendCompileCommandsDotJson(filepath.Join(objdir, CompileCommandsFilename), sourceFilenames, compilerOptions, objdir); err != nil {
log.Fatal(err)
}
} else if writeCompileCommands {
if err := WriteCompileCommandsDotJson(filepath.Join(objdir, CompileCommandsFilename), sourceFilenames, compilerOptions, objdir); err != nil {
log.Fatal(err)
}
}
// And now we're ready to compile everything.
//
if !CompileAll(sourceFilenames, compilerOptions, objdir) {
os.Exit(1)
}
// Then, if required, link an executable or DLL, or create a
// static library.
//
switch runningMode {
case CompileAndLink:
if setLinkOpts && linkerOptions.Empty() {
linkerOptions.SetFrom(compilerOptions)
}
err = Link(outputPathname, inputFilenames, libraryFiles, linkerOptions, otherFiles, frameworks)
case CompileAndMakeDLL:
err = Dll(outputPathname, inputFilenames, libraryFiles, linkerOptions, otherFiles, frameworks)
case CompileAndMakePlugin:
err = Plugin(outputPathname, inputFilenames, libraryFiles, linkerOptions, otherFiles, frameworks)
case CompileAndMakeLib:
err = Lib(outputPathname, inputFilenames)
}
// And that's it. Report any final error and exit.
//
if err != nil {
log.Print(err)
os.Exit(1)
}
os.Exit(0)
}
// Helper function to create an Options (see options.go) that
// defines the underlying compiler, either CC and CXX depending
// upon mode.
//
// This function is given the name of the file and the default
// command name. If an environment variable with the same name
// as the file is set we use its value, or a default. Then we
// search for a, possibly platform and/or architecture
// specific, file with that name. If that file exists we use
// its contents.
//
// We need to do the above twice which is why its a function.
//
func makeCompilerOption(name, defcmd string) *Options {
cmd := Getenv(name, defcmd)
opts := new(Options)
path, fileExists := FindFile(name)
if fileExists {
_, err := opts.ReadFromFile(path, nil)
if err != nil {
log.Fatal(err)
}
} else {
opts.Append(cmd)
}
if opts.Empty() {
opts.Append(defcmd)
}
return opts
}
// UsageError outputs a program usage message to the given
// writer and exits the process with the given status.
//
func UsageError(w io.Writer, status int) {
fmt.Fprintf(w, `Usage: %s [options] [compiler-options] filename...
Options, other than those listed below, are passed to the underlying
compiler. Any -c or -o and similar options are noted and used to control
linking and object file locations. Compilation is done in parallel using,
by default, as many jobs as there are CPUs and dependency files written
to a directory alongside the object files.
Options:
--exe path Create executable program 'path'.
--lib path Create static library 'path'.
--dll path Create shared/dynamic library 'path'.
--objdir path Create object files in 'path'.
-j[N] Use 'N' compile jobs (note single dash, default is one per CPU).
--cpp Compile source files as C++.
--force Ignore dependencies, always compile/link/lib.
--clean Remove dcc-maintained files.
--quiet Disable non-error messages.
--verbose Show more output.
--debug Enable debug messages.
--version Report dcc version and exit.
--write-compile-commands
Output a compile_commands.json file.
--append-compile-commands
Append to an existng compile_commands.json file
if it exists.
With anything else is passed to the underlying compiler.
Environment
CC C compiler (%s).
CXX C++ compiler (%s).
DEPSDIR Name of .d file directory (%s).
OBJDIR Name of .o file directory (%s).
DCCDIR Name of the dcc-options directory (%s).
NJOBS Number of compile jobs (%d).
The following variables define the actual names used for
the options files (see "Files" below).
CCFILE Name of the CC options file.
CXXFILE Name of the CXX options files.
CFLAGSFILE Name of the CFLAGS options file.
CXXFLAGSFILE Name of the CXXFLAGS options file.
LDFLAGSFILE Name of the LDFLAGS options file.
LIBSFILE Name of the LIBS options file.
Files
CC C compiler name.
CXX C++ compiler name.
CFLAGS Compiler options for C.
CXXFLAGS Compiler options for C++.
LDFLAGS Linker options.
LIBS Libraries and library paths.
Options files may be put in a directory named by the DCCDIR
environment variable, ".dcc" by default, or if that directory
does not exist, the current directory.
Options files are text files storing compiler and linker options.
The contents are turned into program options by removing #-style
line comments and using the whitespace separated words as individual
program arguments. Options files act as dependencies and invoke
rebuilds when changed.
Platform-specific file naming may be used to have different options
files for different platforms and/or architectures. When opening a
file dcc appends extensions formed from the host OS and archtecture
to the file name and tries to use those files. The first extension
tried is of the form ".<os>_<arch>", followed by ".<os>" and finally
no extension.
<os> is something like "windows", "linux", "darwin", "freebsd".
<arch> is one of "amd64", "386", "arm8le" , "s390x", etc...
The actual names come from the Go runtime. This host is "%s_%s".
Environment variables may be used to define the names of the
different options files and select specific options.
`,
Myname,
platform.DefaultCC,
platform.DefaultCXX,
DefaultDepsDir,
ObjsDir,
DccDir,
DefaultNumJobs,
runtime.GOOS,
runtime.GOARCH,
)
os.Exit(status)
}
// CatchPanics catches and reports panics. It is intended to be used
// at the top level of main to avoid printing unsightly stack traces.
//
func CatchPanics() {
if x := recover(); x != nil {
if err, ok := x.(error); ok {
fmt.Fprintf(os.Stderr, "UNHANDLED ERROR: %s\n", err.Error())
} else {
fmt.Fprintf(os.Stderr, "PANIC: %v\n", x)
}
os.Exit(1)
}
}
func readLibs(libsFile string, libraryFiles *Options, libraryDirs *[]string, frameworks *[]string) error {
var frameworkDirs []string
captureNext := false
prevs := ""
path, found := FindFile(libsFile)
if !found {
return nil
}
_, err := libraryFiles.ReadFromFile(path, func(s string) string {
if Debug {
log.Printf("LIBS: filter %q", s)
}
if captureNext {
if prevs == "-L" {
*libraryDirs = append(*libraryDirs, s)
} else if prevs == "-F" {
frameworkDirs = append(frameworkDirs, s)
} else {
*frameworks = append(*frameworks, s)
}
captureNext = false
prevs = s
return ""
}
if s == "-framework" {
*frameworks = append(*frameworks, s)
captureNext = true
prevs = s
return ""
}
if s == "-F" || s == "-L" {
captureNext = true
prevs = s
return ""
}
if strings.HasPrefix(s, "-F") {
frameworkDirs = append(frameworkDirs, s[2:])
prevs = s
return ""
}
if strings.HasPrefix(s, "-L") {
*libraryDirs = append(*libraryDirs, s[2:])
prevs = s
return ""
}
if strings.HasPrefix(s, "-l") {
if path, _, found, err := FindLibrary(*libraryDirs, s[2:]); err != nil {
log.Printf("warning: failed to find %s: %s", *libraryDirs, err)
} else if found {
return path
}
prevs = s
}
return s
})
if err != nil {
return err
}
for index := range frameworkDirs {
frameworkDirs[index] = "-F" + frameworkDirs[index]
}
*frameworks = append(frameworkDirs, (*frameworks)...)
return nil
}