Skip to content

Commit ca3527a

Browse files
committed
Moved preprocessor definition into GN file instead of chip config.
1 parent da758f2 commit ca3527a

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

src/lib/core/CHIPConfig.h

-15
Original file line numberDiff line numberDiff line change
@@ -712,21 +712,6 @@
712712
#define CHIP_CONFIG_ENABLE_ARG_PARSER_VALIDITY_CHECKS 1
713713
#endif
714714

715-
/**
716-
* @def CHIP_CONFIG_NON_POSIX_LONG_OPT
717-
*
718-
* @brief Some platforms have a version of getopt_long that behaves differently from the
719-
* POSIX version. Set CHIP_CONFIG_NON_POSIX_LONG_OPT to 1 if the platform's implementation of
720-
* getopt_long differs from POSIX in the following ways (as is the case for ESP32 and OpenIoT):
721-
* (a) Sets optopt to '?' when encountering an unknown short option, instead of setting it
722-
* to the actual value of the character it encountered.
723-
* (b) Treats an unknown long option like a series of short options.
724-
* (c) Does not always permute argv to put the nonoptions at the end.
725-
*/
726-
#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT
727-
#define CHIP_CONFIG_NON_POSIX_LONG_OPT 0
728-
#endif
729-
730715
/**
731716
* @def CHIP_CONFIG_UNAUTHENTICATED_CONNECTION_POOL_SIZE
732717
*

src/lib/support/BUILD.gn

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ static_library("support") {
333333
if (chip_config_memory_debug_dmalloc) {
334334
libs += [ "dmallocthcxx" ]
335335
}
336+
337+
if (chip_device_platform == "esp32" || chip_device_platform == "openiotsdk") {
338+
defines = [ "CONFIG_NON_POSIX_GETOPT_LONG" ]
339+
}
336340
}
337341

338342
source_set("test_utils") {

src/lib/support/CHIPArgParser.cpp

+29-18
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static inline bool IsShortOptionChar(int ch)
7878
return isgraph(ch);
7979
}
8080

81-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
81+
#if CONFIG_NON_POSIX_GETOPT_LONG
8282
static inline bool IsNotAnOption(char * const argv[], int element, int character)
8383
{
8484
char c = argv[element][character];
@@ -102,7 +102,7 @@ static inline int FirstCharacter(char * const argv[], int element)
102102
return 0;
103103
return argv[element][1] == '-' ? 2 : 1;
104104
}
105-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
105+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
106106

107107
/**
108108
* @brief
@@ -302,6 +302,17 @@ void (*PrintArgError)(const char * msg, ...) = DefaultPrintArgError;
302302
* PrintOptionHelp() will print the option help for the different OptionSets together under
303303
* a common section title.
304304
*
305+
*
306+
* ## NON-POSIX IMPLEMENTATIONS OF getopt_long
307+
*
308+
* Some platforms have a version of getopt_long that behaves differently from the POSIX
309+
* version. Set CONFIG_NON_POSIX_LONG_OPT to 1 if the platform's implementation of getopt_long
310+
* differs from POSIX in the following ways (as is the case for ESP32 and OpenIoT):
311+
* (a) Sets optopt to '?' when encountering an unknown short option, instead of setting it
312+
* to the actual value of the character it encountered.
313+
* (b) Treats an unknown long option like a series of short options.
314+
* (c) Does not always permute argv to put the nonoptions at the end.
315+
*
305316
*/
306317
bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet * optSets[],
307318
NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown)
@@ -315,13 +326,13 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
315326
OptionDef * curOpt;
316327
bool handlerRes;
317328
int optIndex = -1;
318-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
319-
// The element and character that is being looked at during the current iteration's call to getopt_long.
329+
#if CONFIG_NON_POSIX_GETOPT_LONG
330+
// The element and character being looked at during the current iteration's call to getopt_long.
320331
int currentElement = 0;
321332
int currentCharacter;
322333
// The value of optind after the last time getopt_long was called.
323334
int prevOptind = 1;
324-
// All the nonoptions that we have found so far will be in range [firstNonoption, lastNonoptionPlus1).
335+
// All the nonoptions we have found so far will be in range [firstNonoption, lastNonoptionPlus1).
325336
int firstNonoption = 1;
326337
int lastNonoptionPlus1 = 1;
327338
// Temporary variables used When finding a new set of nonoptions.
@@ -336,7 +347,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
336347
char * tempSwap;
337348
// Permutable version of argv. Needed to move the nonoptions to the end.
338349
char ** permutableArgv = const_cast<char **>(argv);
339-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
350+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
340351

341352
// The getopt() functions do not support recursion, so exit immediately with an
342353
// error if called recursively.
@@ -378,7 +389,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
378389
goto done;
379390
}
380391

381-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
392+
#if CONFIG_NON_POSIX_GETOPT_LONG
382393
// This non-POSIX getopt_long will sometimes permute argv, but not in all the cases that the POSIX version would and not always
383394
// at the same times. This makes it hard to predict when it will permute, which can sometimes break the manual permutation that
384395
// we're trying to do in here. To avoid this we do an initial pass through the full argv and let getopt_long get all its
@@ -387,7 +398,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
387398
while (getopt_long(argc, argv, shortOpts, longOpts, &optIndex) != -1)
388399
{
389400
}
390-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
401+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
391402

392403
// Force getopt() to reset its internal state.
393404
optind = 0;
@@ -402,7 +413,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
402413
optarg = nullptr;
403414
optopt = 0;
404415

405-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
416+
#if CONFIG_NON_POSIX_GETOPT_LONG
406417
// Advance to the next element/character that getopt_long will be processing.
407418

408419
// If currentElement has been initialized and currentElement is currently a short option (or short option group).
@@ -426,11 +437,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
426437
// Set currentCharacter to the first character after the hyphens.
427438
currentCharacter = FirstCharacter(argv, currentElement);
428439
}
429-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
440+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
430441

431442
id = getopt_long(argc, argv, shortOpts, longOpts, &optIndex);
432443

433-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
444+
#if CONFIG_NON_POSIX_GETOPT_LONG
434445
// The POSIX implementation does the sorting as it goes, gradually permuting argv to put the nonoptions towards the end.
435446
// This code attempts to simulate that outcome, the one difference being that the POSIX version will move nonoptions after
436447
// the next time getopt_long is called, whereas this code will only move nonoptions when it either encounters additional
@@ -486,7 +497,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
486497
// Store the value of optind so we'll know which elements getopt_long processes the next time it is called.
487498
prevOptind = optind;
488499

489-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
500+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
490501

491502
// Stop if there are no more options.
492503
if (id == -1)
@@ -499,7 +510,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
499510
continue;
500511
if (optopt != 0)
501512
{
502-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
513+
#if CONFIG_NON_POSIX_GETOPT_LONG
503514
if (IsShortOption(argv, currentElement))
504515
{
505516
// On this platform an unknown short option sets optopt to '?' instead of the actual option character, so fetch
@@ -519,7 +530,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
519530
}
520531
#else
521532
PrintArgError("%s: Unknown option: -%c\n", progName, optopt);
522-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
533+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
523534
}
524535
else // optopt == 0 only happens for long options, so optind has already advanced.
525536
PrintArgError("%s: Unknown option: %s\n", progName, argv[optind - 1]);
@@ -532,11 +543,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
532543
{
533544
// NOTE: with the way getopt_long() works, it is impossible to tell whether the option that
534545
// was missing an argument was a long option or a short option.
535-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
546+
#if CONFIG_NON_POSIX_GETOPT_LONG
536547
PrintArgError("%s: Missing argument for %s option\n", progName, argv[currentElement]);
537548
#else
538549
PrintArgError("%s: Missing argument for %s option\n", progName, argv[optind - 1]);
539-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
550+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
540551
goto done;
541552
}
542553

@@ -576,13 +587,13 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
576587
// If supplied, call the non-option argument handler with the remaining arguments (if any).
577588
if (nonOptArgHandler != nullptr)
578589
{
579-
#if CHIP_CONFIG_NON_POSIX_LONG_OPT
590+
#if CONFIG_NON_POSIX_GETOPT_LONG
580591
// On a POSIX implementation, on the final interation when getopt_long returns -1 indicating it has nothing left to do,
581592
// optind would be set to the location of the first nonoption (all of which by now would have been moved to the end of
582593
// argv). On some non-POSIX implementations this is not true -- it simply sets optind to the location of argv's terminal
583594
// NULL (i.e. optind == argc). So we have to alter optind here to simulate the POSIX behavior.
584595
optind = firstNonoption;
585-
#endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
596+
#endif // CONFIG_NON_POSIX_GETOPT_LONG
586597

587598
if (!nonOptArgHandler(progName, argc - optind, argv + optind))
588599
goto done;

src/test_driver/esp32/main/include/CHIPProjectConfig.h

-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929

3030
// Enable support functions for parsing command-line arguments
3131
#define CHIP_CONFIG_ENABLE_ARG_PARSER 1
32-
#define CHIP_CONFIG_NON_POSIX_LONG_OPT 1
3332

3433
#endif // CHIP_PROJECT_CONFIG_H

src/test_driver/openiotsdk/unit-tests/main/include/CHIPProjectConfig.h

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
// Enable support functions for parsing command-line arguments
3131
#define CHIP_CONFIG_ENABLE_ARG_PARSER 1
32-
#define CHIP_CONFIG_NON_POSIX_LONG_OPT 1
3332

3433
// Enable building for unit testing
3534
#define CONFIG_BUILD_FOR_HOST_UNIT_TEST 1

0 commit comments

Comments
 (0)