forked from SPRESENSE/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.c
1625 lines (1336 loc) · 45.4 KB
/
configure.c
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
/****************************************************************************
* tools/configure.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <libgen.h>
#include <errno.h>
#include "cfgparser.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BUFFER_SIZE 1024
#ifdef WIN32
# define strndup(x, y) strdup(x)
#endif
#define HOST_NOCHANGE 0
#define HOST_LINUX 1
#define HOST_MACOS 2
#define HOST_WINDOWS 3
#define HOST_BSD 4
#define WINDOWS_NATIVE 1
#define WINDOWS_CYGWIN 2
#define WINDOWS_MSYS 3
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void show_usage(const char *progname, int exitcode);
static void dumpcfgs(void);
static void debug(const char *fmt, ...);
static void parse_args(int argc, char **argv);
static int run_make(const char *arg);
static bool filecmp(const char *f1, const char *f2);
static bool check_directory(const char *directory);
static void verify_directory(const char *directory);
static bool verify_optiondir(const char *directory);
static bool verify_file(const char *path);
static void find_topdir(void);
typedef void (*config_callback)(const char *boarddir, const char *archname,
const char *chipname, const char *boardname,
const char *configname, void *data);
static void config_search(const char *boarddir,
config_callback callback, void *data);
static void archname_callback(const char *boarddir, const char *archname,
const char *chipname, const char *boardname,
const char *configname, void *data);
static void find_archname(void);
static void enumerate_callback(const char *boarddir, const char *archname,
const char *chipname, const char *boardname,
const char *configname, void *data);
static void enumerate_configs(void);
static void check_configdir(void);
static void check_configured(void);
static void read_configfile(void);
static void read_versionfile(void);
static void get_verstring(void);
static bool verify_appdir(const char *appdir);
static void check_appdir(void);
static void check_configuration(void);
static void copy_file(const char *srcpath,
const char *destpath, mode_t mode);
static void substitute(char *str, int ch1, int ch2);
static char *double_appdir_backslashes(char *old_appdir);
static void copy_optional(void);
static void enable_feature(const char *destconfig, const char *varname);
static void disable_feature(const char *destconfig, const char *varname);
static void set_host(const char *destconfig);
static void configure(void);
static void refresh(void);
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_WINDOWS_NATIVE
static char g_delim = '\\'; /* Delimiter to use when forming paths */
static bool g_winpaths = true; /* True: Windows style paths */
#else
static char g_delim = '/'; /* Delimiter to use when forming paths */
static bool g_winpaths = false; /* False: POSIX style paths */
#endif
static bool g_debug = false; /* Enable debug output */
static bool g_enforce = false; /* Enfore distclean */
static bool g_distclean = false; /* Distclean if configured */
static const char *g_appdir = NULL; /* Relative path to the application directory */
static const char *g_archdir = NULL; /* Name of architecture subdirectory */
static const char *g_chipdir = NULL; /* Name of chip subdirectory */
static const char *g_boarddir = NULL; /* Name of board subdirectory */
static char *g_configdir = NULL; /* Name of configuration subdirectory */
static char *g_topdir = NULL; /* Full path to top-level NuttX build directory */
static char *g_apppath = NULL; /* Full path to the application directory */
static char *g_configtop = NULL; /* Full path to the top-level configuration directory */
static char *g_configpath = NULL; /* Full path to the configuration sub-directory */
static char *g_scriptspath = NULL; /* Full path to the scripts sub-directory */
static char *g_verstring = "0.0"; /* Version String */
static char *g_srcdefconfig = NULL; /* Source defconfig file */
static char *g_srcmakedefs = NULL; /* Source Make.defs file */
static char **g_makeargv = NULL; /* Arguments pass to make */
static bool g_winnative = false; /* True: Windows native configuration */
static bool g_oldnative = false; /* True: Was Windows native configuration */
static bool g_needapppath = true; /* Need to add app path to the .config file */
static uint8_t g_host = HOST_NOCHANGE;
static uint8_t g_windows = WINDOWS_CYGWIN;
static char g_buffer[BUFFER_SIZE]; /* Scratch buffer for forming full paths */
static struct variable_s *g_configvars = NULL;
static struct variable_s *g_versionvars = NULL;
/* Optional configuration files */
static const char *g_optfiles[] =
{
".gdbinit",
".cproject",
".project"
};
#define N_OPTFILES (sizeof(g_optfiles) / sizeof(const char *))
/****************************************************************************
* Private Functions
****************************************************************************/
static void show_usage(const char *progname, int exitcode)
{
fprintf(stderr, "\nUSAGE: %s [-d] [-E] [-e] [-b|f] [-L] [-l|m|c|g|n|B] "
"[-a <app-dir>] <board-name>:<config-name> [make-opts]\n",
progname);
fprintf(stderr, "\nUSAGE: %s [-h]\n", progname);
fprintf(stderr, "\nWhere:\n");
fprintf(stderr, " -d:\n");
fprintf(stderr, " Enables debug output\n");
fprintf(stderr, " -E:\n");
fprintf(stderr, " Enforce distclean if already configured\n");
fprintf(stderr, " -e:\n");
fprintf(stderr, " Performs distclean if configuration changed\n");
fprintf(stderr, " -b:\n");
#ifdef CONFIG_WINDOWS_NATIVE
fprintf(stderr, " Informs the tool that it should use Windows style\n");
fprintf(stderr, " paths like C:\\Program Files instead of POSIX\n");
fprintf(stderr, " style paths are used like /usr/local/bin. Windows\n");
fprintf(stderr, " style paths are used by default.\n");
#else
fprintf(stderr, " Informs the tool that it should use Windows style\n");
fprintf(stderr, " paths like C:\\Program Files. By default, POSIX\n");
fprintf(stderr, " style paths like /usr/local/bin are used.\n");
#endif
fprintf(stderr, " -f:\n");
#ifdef CONFIG_WINDOWS_NATIVE
fprintf(stderr, " Informs the tool that it should use POSIX style\n");
fprintf(stderr, " paths like /usr/local/bin. By default, Windows\n");
fprintf(stderr, " style paths like C:\\Program Files are used.\n");
#else
fprintf(stderr, " Informs the tool that it should use POSIX style\n");
fprintf(stderr, " paths like /usr/local/bin instead of Windows\n");
fprintf(stderr, " style paths like C:\\Program Files are used.\n");
fprintf(stderr, " POSIX style paths are used by default.\n");
#endif
fprintf(stderr, " [-l|m|c|g|n]\n");
fprintf(stderr, " Selects the host environment.\n");
fprintf(stderr, " -l Selects the Linux (l) host environment.\n");
fprintf(stderr, " -m Selects the macOS (m) host environment.\n");
fprintf(stderr, " -B Selects the *BSD (B) host environment.\n");
fprintf(stderr, " -c Selects the Windows Cygwin (c) environment.\n");
fprintf(stderr, " -g Selects the Windows MinGW/MSYS environment.\n");
fprintf(stderr, " -n Selects the Windows native (n) environment.\n");
fprintf(stderr, " Default: Use host setup in the defconfig file.\n");
fprintf(stderr, " Default Windows: Cygwin.\n");
fprintf(stderr, " -L:\n");
fprintf(stderr, " Lists all available configurations.\n");
fprintf(stderr, " -a <app-dir>:\n");
fprintf(stderr, " Informs the configuration tool where the\n");
fprintf(stderr, " application build directory. This is a relative\n");
fprintf(stderr, " path from the top-level NuttX build directory.\n");
fprintf(stderr, " But default, this tool will look in the usual\n");
fprintf(stderr, " places to locate the application directory:\n");
fprintf(stderr, " ..%capps or\n", g_delim);
fprintf(stderr, " ..%capps-xx.yy where xx.yy is the version number.\n",
g_delim);
fprintf(stderr, " <board-name>:\n");
fprintf(stderr, " Identifies the board. This must correspond to a\n");
fprintf(stderr, " board directory under nuttx%cboards%c.\n",
g_delim, g_delim);
fprintf(stderr, " <config-name>:\n");
fprintf(stderr, " Identifies the specific configuration for the\n");
fprintf(stderr, " selected <board-name>. This must correspond to\n");
fprintf(stderr, " a sub-directory under the board directory at\n");
fprintf(stderr, " under nuttx%cboards%c<board-name>%cconfigs%c.\n",
g_delim, g_delim, g_delim, g_delim);
fprintf(stderr, " [make-opts]:\n");
fprintf(stderr, " Options directly pass to make\n");
fprintf(stderr, " -h:\n");
fprintf(stderr, " Prints this message and exits.\n");
exit(exitcode);
}
static void dumpcfgs(void)
{
find_topdir();
snprintf(g_buffer, BUFFER_SIZE, "%s%cboards", g_topdir, g_delim);
verify_directory(g_buffer);
g_configtop = strdup(g_buffer);
enumerate_configs();
free(g_configtop);
exit(EXIT_SUCCESS);
}
static void debug(const char *fmt, ...)
{
va_list ap;
if (g_debug)
{
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
}
static void parse_args(int argc, char **argv)
{
char *ptr;
int ch;
/* Parse command line options */
while ((ch = getopt(argc, argv, "a:bcdEefghLlmBnu")) > 0)
{
switch (ch)
{
case 'a' :
g_appdir = optarg;
break;
case 'b' :
g_delim = '\\';
g_winpaths = false;
break;
case 'c' :
g_host = HOST_WINDOWS;
g_windows = WINDOWS_CYGWIN;
break;
case 'd' :
g_debug = true;
break;
case 'E' :
g_enforce = true;
break;
case 'e' :
g_distclean = true;
break;
case 'f' :
g_delim = '/';
g_winpaths = true;
break;
case 'g' :
g_host = HOST_WINDOWS;
g_windows = WINDOWS_MSYS;
break;
case 'h' :
show_usage(argv[0], EXIT_SUCCESS);
case 'L' :
dumpcfgs();
case 'l' :
g_host = HOST_LINUX;
break;
case 'm' :
g_host = HOST_MACOS;
break;
case 'B' :
g_host = HOST_BSD;
break;
case 'n' :
g_host = HOST_WINDOWS;
g_windows = WINDOWS_NATIVE;
break;
case '?' :
fprintf(stderr, "ERROR: Unrecognized option: %c\n", optopt);
show_usage(argv[0], EXIT_FAILURE);
case ':' :
fprintf(stderr, "ERROR: Missing option argument, option: %c\n",
optopt);
show_usage(argv[0], EXIT_FAILURE);
default:
fprintf(stderr, "ERROR: Unexpected option: %c\n", ch);
show_usage(argv[0], EXIT_FAILURE);
}
}
/* There should be exactly one argument following the options */
if (optind >= argc)
{
fprintf(stderr, "ERROR: Missing <board-name>:<config-name>\n");
show_usage(argv[0], EXIT_FAILURE);
}
/* The required option should be the board directory name and the
* configuration directory name separated by ':', '/' or '\'. Any are
* acceptable in this context. Or using the custom board relative or
* absolute path directly here.
*/
g_boarddir = argv[optind];
optind++;
if (!verify_optiondir(g_boarddir))
{
ptr = strchr(g_boarddir, ':');
if (ptr == NULL)
{
ptr = strchr(g_boarddir, '/');
if (!ptr)
{
ptr = strchr(g_boarddir, '\\');
}
}
if (ptr == NULL)
{
fprintf(stderr, "ERROR: Invalid <board-name>:<config-name>\n");
show_usage(argv[0], EXIT_FAILURE);
}
*ptr++ = '\0';
g_configdir = ptr;
}
else
{
/* custom board case with relative or absolute path */
g_configpath = strdup(g_boarddir);
}
/* The left arguments will pass to make */
g_makeargv = &argv[optind];
}
static int run_make(const char *arg)
{
char **argv;
snprintf(g_buffer, BUFFER_SIZE, "make %s", arg);
for (argv = g_makeargv; *argv; argv++)
{
strncat(g_buffer, " ", BUFFER_SIZE - 1);
strncat(g_buffer, *argv, BUFFER_SIZE - 1);
}
return system(g_buffer);
}
static bool filecmp(const char *f1, const char *f2)
{
FILE *stream1;
FILE *stream2;
char ch1;
char ch2;
stream1 = fopen(f1, "r");
stream2 = fopen(f2, "r");
if (stream1 == NULL || stream2 == NULL)
{
return false;
}
do
{
ch1 = fgetc(stream1);
ch2 = fgetc(stream2);
if (ch1 != ch2)
{
return false;
}
}
while (ch1 != EOF && ch2 != EOF);
fclose(stream1);
fclose(stream2);
return true;
}
static bool check_directory(const char *directory)
{
struct stat buf;
if (stat(directory, &buf) < 0)
{
debug("stat of %s failed: %s\n", directory, strerror(errno));
return false;
}
if (!S_ISDIR(buf.st_mode))
{
debug("%s exists but is not a directory\n", directory);
return false;
}
return true;
}
static void verify_directory(const char *directory)
{
struct stat buf;
if (stat(directory, &buf) < 0)
{
fprintf(stderr, "ERROR: stat of %s failed: %s\n",
directory, strerror(errno));
exit(EXIT_FAILURE);
}
if (!S_ISDIR(buf.st_mode))
{
fprintf(stderr, "ERROR: %s exists but is not a directory\n",
directory);
exit(EXIT_FAILURE);
}
}
static bool verify_optiondir(const char *directory)
{
struct stat buf;
if (stat(directory, &buf) < 0)
{
/* It may be okay if the directory does not exist */
/* It may be okay if the file does not exist */
int errcode = errno;
if (errcode == ENOENT)
{
debug("verify_optiondir: stat of %s failed: %s\n",
directory, strerror(errno));
return false;
}
else
{
fprintf(stderr, "ERROR: stat of %s failed: %s\n",
directory, strerror(errno));
exit(EXIT_FAILURE);
}
}
if (!S_ISDIR(buf.st_mode))
{
fprintf(stderr, "ERROR: %s exists but is not a directory\n",
directory);
exit(EXIT_FAILURE);
}
return true;
}
static bool verify_file(const char *path)
{
struct stat buf;
if (stat(path, &buf) < 0)
{
/* It may be okay if the file does not exist */
int errcode = errno;
if (errcode == ENOENT)
{
debug("verify_file: stat of %s failed: %s\n",
path, strerror(errno));
return false;
}
else
{
fprintf(stderr, "ERROR: stat of %s failed: %s\n",
path, strerror(errno));
exit(EXIT_FAILURE);
}
}
if (!S_ISREG(buf.st_mode))
{
fprintf(stderr, "ERROR: %s exists but is not a regular file\n", path);
exit(EXIT_FAILURE);
}
return true;
}
static void find_topdir(void)
{
char *currdir;
/* Get and verify the top-level NuttX directory */
/* First get the current directory. We expect this to be either
* the nuttx root directory or the tools subdirectory.
*/
if (getcwd(g_buffer, BUFFER_SIZE) == NULL)
{
fprintf(stderr, "ERROR: getcwd failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* Assume that we are in the tools sub-directory and the directory above
* is the nuttx root directory.
*/
currdir = strdup(g_buffer);
g_topdir = strdup(dirname(g_buffer));
debug("get_topdir: Checking parent directory: %s\n", g_topdir);
verify_directory(g_topdir);
/* Check if the current directory is the nuttx root directory.
* If so, then the tools directory should be a sub-directory.
*/
snprintf(g_buffer, BUFFER_SIZE, "%s%ctools", currdir, g_delim);
debug("get_topdir: Checking topdir/tools=%s\n", g_buffer);
if (check_directory(g_buffer))
{
/* There is a tools sub-directory under the current directory.
* We must have already been in the nuttx root directory. We
* will find out for sure in later tests.
*/
free(g_topdir);
g_topdir = currdir;
}
else
{
/* Yes, we are probably in the tools/ sub-directory */
free(currdir);
if (chdir(g_topdir) < 0)
{
fprintf(stderr, "ERROR: Failed to ch to %s\n", g_topdir);
exit(EXIT_FAILURE);
}
}
}
static void config_search(const char *boarddir,
config_callback callback, void *data)
{
DIR *dir;
struct dirent *dp;
struct stat buf;
char *parent;
char *child;
/* Skip over any leading '/' or '\\'. This happens on the first second
* call because the starting boarddir is ""
*/
if (boarddir[0] == g_delim)
{
boarddir++;
}
/* Get the full directory path and open it */
snprintf(g_buffer, BUFFER_SIZE, "%s%c%s", g_configtop, g_delim, boarddir);
dir = opendir(g_buffer);
if (!dir)
{
fprintf(stderr, "ERROR: Could not open %s: %s\n",
g_buffer, strerror(errno));
return;
}
/* Make a copy of the path to the directory */
parent = strdup(g_buffer);
/* Visit each entry in the directory */
while ((dp = readdir(dir)) != NULL)
{
/* Ignore directory entries that start with '.' */
if (dp->d_name[0] == '.')
{
continue;
}
/* Get a properly terminated copy of d_name (if d_name is long it may
* not include a NUL terminator.
*/
child = strndup(dp->d_name, NAME_MAX);
/* Get the full path to d_name and stat the file/directory */
snprintf(g_buffer, BUFFER_SIZE, "%s%c%s", parent, g_delim, child);
if (stat(g_buffer, &buf) < 0)
{
fprintf(stderr, "ERROR: stat of %s failed: %s\n",
g_buffer, strerror(errno));
free(child);
continue;
}
/* If it is a directory, then recurse */
if (S_ISDIR(buf.st_mode))
{
char *tmppath;
snprintf(g_buffer, BUFFER_SIZE, "%s%c%s",
boarddir, g_delim, child);
tmppath = strdup(g_buffer);
config_search(tmppath, callback, data);
free(tmppath);
}
/* If it is a regular file named 'defconfig' then we have found a
* configuration directory. We could terminate the search in this case
* because we do not expect sub-directories within configuration
* directories.
*/
else if (S_ISREG(buf.st_mode) && strcmp("defconfig", child) == 0)
{
char *archname;
char *chipname;
char *boardname;
char *configname;
char *delim;
/* Get the board directory near the beginning of the 'boarddir':
* <archdir>/<chipdir>/<boarddir>/configs/<configdir>
*/
/* Make a modifiable copy */
strncpy(g_buffer, boarddir, BUFFER_SIZE - 1);
/* Save the <archdir> */
archname = g_buffer;
delim = strchr(g_buffer, g_delim);
if (delim == NULL)
{
debug("ERROR: delimiter not found in path: %s\n", boarddir);
}
else
{
/* Save the <chipdir> */
*delim = '\0';
chipname = delim + 1;
delim = strchr(chipname, g_delim);
if (delim == NULL)
{
debug("ERROR: delimiter not found in path: %s\n",
chipname);
}
else
{
/* Save the <boardir> */
*delim = '\0';
boardname = delim + 1;
delim = strchr(boardname, g_delim);
if (delim == NULL)
{
debug("ERROR: delimiter not found in path: %s\n",
boardname);
}
else
{
/* Save the <configdir> */
*delim = '\0';
delim = strrchr(delim + 1, g_delim);
if (delim == NULL)
{
debug("ERROR: directory not found in path: %s\n",
boardname);
}
else
{
configname = delim + 1;
callback(boarddir, archname, chipname,
boardname, configname, data);
}
}
}
}
}
free(child);
}
free(parent);
closedir(dir);
}
static void archname_callback(const char *boarddir, const char *archname,
const char *chipname, const char *boardname,
const char *configname, void *data)
{
if (strcmp(g_boarddir, boardname) == 0 &&
strcmp(g_configdir, configname) == 0)
{
g_archdir = strdup(archname);
g_chipdir = strdup(chipname);
}
}
static void find_archname(void)
{
config_search("", archname_callback, NULL);
if (g_archdir == NULL || g_chipdir == NULL)
{
g_archdir = "unknown";
g_chipdir = "unknown";
}
}
static void enumerate_callback(const char *boarddir, const char *archname,
const char *chipname, const char *boardname,
const char *configname, void *data)
{
fprintf(stderr, " %s:%s\n", boardname, configname);
}
static void enumerate_configs(void)
{
fprintf(stderr, "Options for <board-name>:<config-name> include:\n\n");
config_search("", enumerate_callback, NULL);
}
static void check_configdir(void)
{
if (verify_optiondir(g_configpath))
{
/* Get the path to the custom board scripts directory */
snprintf(g_buffer, BUFFER_SIZE, "%s%c..%c..%cscripts",
g_configpath, g_delim, g_delim, g_delim);
if (verify_optiondir(g_buffer))
{
g_scriptspath = strdup(g_buffer);
}
}
else
{
/* Get the path to the top level configuration directory: boards/ */
snprintf(g_buffer, BUFFER_SIZE, "%s%cboards", g_topdir, g_delim);
debug("check_configdir: Checking configtop=%s\n", g_buffer);
verify_directory(g_buffer);
g_configtop = strdup(g_buffer);
/* Get and verify the path to the selected configuration:
* boards/<archdir>/<chipdir>/<boarddir>/configs/<configdir>
*/
find_archname();
snprintf(g_buffer, BUFFER_SIZE, "%s%cboards%c%s%c%s%c%s%cconfigs%c%s",
g_topdir, g_delim, g_delim, g_archdir, g_delim, g_chipdir,
g_delim, g_boarddir, g_delim, g_delim, g_configdir);
debug("check_configdir: Checking configpath=%s\n", g_buffer);
if (!verify_optiondir(g_buffer))
{
fprintf(stderr, "ERROR: No configuration at %s\n", g_buffer);
fprintf(stderr, "Run tools/configure -L"
" to list available configurations.\n");
exit(EXIT_FAILURE);
}
g_configpath = strdup(g_buffer);
/* Get and verify the path to the scripts directory:
* boards/<archdir>/<chipdir>/<boarddir>/scripts
*/
snprintf(g_buffer, BUFFER_SIZE, "%s%cboards%c%s%c%s%c%s%cscripts",
g_topdir, g_delim, g_delim, g_archdir, g_delim,
g_chipdir, g_delim, g_boarddir, g_delim);
debug("check_configdir: Checking scriptspath=%s\n", g_buffer);
g_scriptspath = NULL;
if (verify_optiondir(g_buffer))
{
g_scriptspath = strdup(g_buffer);
}
}
}
static void check_configured(void)
{
/* If we are already configured then there will be a .config and
* a Make.defs file in the top-level directory.
*/
snprintf(g_buffer, BUFFER_SIZE, "%s%c.config", g_topdir, g_delim);
debug("check_configured: Checking %s\n", g_buffer);
if (!verify_file(g_buffer))
{
return;
}
if (g_enforce)
{
run_make("distclean");
}
else
{
char *defcfgpath = NULL;
snprintf(g_buffer, BUFFER_SIZE, "%s%cdefconfig",
g_configpath, g_delim);
defcfgpath = strdup(g_buffer);
snprintf(g_buffer, BUFFER_SIZE, "%s%cdefconfig",
g_topdir, g_delim);
if (filecmp(g_buffer, defcfgpath))
{
fprintf(stderr, "No configuration change.\n");
free(defcfgpath);
exit(EXIT_SUCCESS);
}
else
{
free(defcfgpath);
if (g_distclean)
{
run_make("distclean");
}
else
{
fprintf(stderr, "Already configured!\n");
fprintf(stderr, "Please 'make distclean' and try again.\n");
exit(EXIT_FAILURE);
}
}
}
}
static void read_configfile(void)
{
FILE *stream;
snprintf(g_buffer, BUFFER_SIZE, "%s%cdefconfig", g_configpath, g_delim);
stream = fopen(g_buffer, "r");
if (!stream)
{
fprintf(stderr, "ERROR: failed to open %s for reading: %s\n",
g_buffer, strerror(errno));
exit(EXIT_FAILURE);
}
parse_file(stream, &g_configvars);
fclose(stream);
}
static void read_versionfile(void)
{
FILE *stream;
snprintf(g_buffer, BUFFER_SIZE, "%s%c.version", g_topdir, g_delim);
stream = fopen(g_buffer, "r");
if (!stream)
{
/* It may not be an error if there is no .version file */
debug("Failed to open %s for reading: %s\n",
g_buffer, strerror(errno));
}
else
{
parse_file(stream, &g_versionvars);
fclose(stream);
}
}
static void get_verstring(void)
{
struct variable_s *var;
if (g_versionvars)
{
var = find_variable("CONFIG_VERSION_STRING", g_versionvars);
if (var && var->val)
{
g_verstring = strdup(var->val);
}
}
debug("get_verstring: Version string=%s\n", g_verstring);
}
static bool verify_appdir(const char *appdir)
{
/* Does this directory exist? */
snprintf(g_buffer, BUFFER_SIZE, "%s%c%s", g_topdir, g_delim, appdir);
debug("verify_appdir: Checking apppath=%s\n", g_buffer);
if (verify_optiondir(g_buffer))
{
/* Yes.. Use this application directory path */
g_appdir = strdup(appdir);
g_apppath = strdup(g_buffer);
return true;
}
debug("verify_appdir: apppath=%s does not exist\n", g_buffer);
return false;
}
static void check_appdir(void)
{
char tmp[16];
/* Get and verify the full path to the application directory */
/* Was the appdir provided on the command line? */
debug("check_appdir: Command line appdir=%s\n",
g_appdir ? g_appdir : "<null>");
if (!g_appdir)
{
/* If no application directory was provided on the command line and we
* are switching between a windows native host and some other host then