ZerBea / hcxdumptool

Small tool to capture packets from wlan devices.
MIT License
1.85k stars 398 forks source link

Extend compiler version detection with clang #392

Closed gemesa closed 11 months ago

gemesa commented 11 months ago

Previously when compiled with clang the version check returned this misleading gcc version:

$ ./hcxdumptool -v
hcxdumptool 6.3.2 (C) 2023 ZeroBeat
...
compiled by gcc 4.2.1
...

I guess it is because clang defines the __GNUC__ macros for some compatibility reasons or something like that.

I think clang is becoming more widespread so this case should be handled. Now, after the changes the version check is fixed:

$ make CC=clang                                                  
fatal: No names found, cannot describe anything.
clang -O3 -Wall -Wextra -Wpedantic -std=gnu99   -o hcxdumptool hcxdumptool.c -DVERSION_TAG=\"6.3.2\" -DVERSION_YEAR=\"2023\" -DHCXSTATUSOUT -DHCXNMEAOUT
$ ./hcxdumptool -v
...
compiled by clang 17.0.6
...
$ make clean
...
$ make
fatal: No names found, cannot describe anything.
cc -O3 -Wall -Wextra -Wpedantic -std=gnu99   -o hcxdumptool hcxdumptool.c -DVERSION_TAG=\"6.3.2\" -DVERSION_YEAR=\"2023\" -DHCXSTATUSOUT -DHCXNMEAOUT
$ ./hcxdumptool -v
...
compiled by gcc 13.2.1
..
ZerBea commented 11 months ago

Thanks, merged but modified because gcc should be on first rank.

gemesa commented 11 months ago

That would have been my preference also but with this approach we are back to the original problem because __GNUC__ is defined both by gcc and clang so the branch #elif defined(__clang__) is never hit. If you want to check for __GNUC__ first, you could write something like this:

#if defined(__GNUC__) && !defined(__clang__)
fprintf(stdout, "compiled by gcc %d.%d.%d\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#elif defined(__clang__)
fprintf(stdout, "compiled by clang %d.%d.%d\n", __clang_major__, __clang_minor__, __clang_patchlevel__);
...
ZerBea commented 11 months ago

Thanks, that is better:

$ ./hcxdumptool -v
hcxdumptool 6.3.2-145-g4fd4dfb (C) 2023 ZeroBeat
running on Linux kernel 6.6.7-arch1-1
running GNU libc version 2.38
compiled by gcc 13.2.1
compiled with Linux API headers 6.4.0
compiled with GNU libc headers 2.38
enabled REALTIME DISPLAY
enabled GPS support
enabled BPF compiler

$ ./hcxdumptool -v
hcxdumptool 6.3.2-145-g4fd4dfb (C) 2023 ZeroBeat
running on Linux kernel 6.6.7-arch1-1
running GNU libc version 2.38
compiled by clang 16.0.6
compiled with Linux API headers 6.4.0
compiled with GNU libc headers 2.38
enabled REALTIME DISPLAY
enabled GPS support
enabled BPF compiler