#!/usr/bin/env perl1#***************************************************************************2# _ _ ____ _3# Project ___| | | | _ \| |4# / __| | | | |_) | |5# | (__| |_| | _ <| |___6# \___|\___/|_| \_\_____|7#8# Copyright (C) Daniel Stenberg, <[email protected]>, et al.9#10# This software is licensed as described in the file COPYING, which11# you should have received as part of this distribution. The terms12# are also available at https://curl.se/docs/copyright.html.13#14# You may opt to use, copy, modify, merge, publish, distribute and/or sell15# copies of the Software, and permit persons to whom the Software is16# furnished to do so, under the terms of the COPYING file.17#18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY19# KIND, either express or implied.20#21# SPDX-License-Identifier: curl22#23###########################################################################24#25# This script grew out of help from Przemyslaw Iskra and Balint Szilakszi26# a late evening in the #curl IRC channel.27#2829use strict;30use warnings;31use vars qw($Cpreprocessor);3233#34# configurehelp perl module is generated by configure script35#36my $rc = eval {37require configurehelp;38configurehelp->import(qw(39$Cpreprocessor40));411;42};43# Set default values if configure has not generated a configurehelp.pm file.44# This is the case with cmake.45if (!$rc) {46$Cpreprocessor = 'cpp';47}4849# we may get the dir root pointed out50my $root=$ARGV[0] || ".";5152# need an include directory when building out-of-tree53my $i = ($ARGV[1]) ? "-I$ARGV[1] " : '';5455my $verbose=0;56my $summary=0;57my $misses=0;5859my @manrefs;60my @syms;61my %doc;62my %rem;6364# scanenum runs the preprocessor on curl.h so it will process all enums65# included by it, which *should* be all headers66sub scanenum {67my ($file) = @_;68open my $h_in, "-|", "$Cpreprocessor $i$file" || die "Cannot preprocess $file";69while ( <$h_in> ) {70if ( /enum\s+(\S+\s+)?{/ .. /}/ ) {71s/^\s+//;72next unless /^CURL/;73chomp;74s/[,\s].*//;75push @syms, $_;76}77}78close $h_in || die "Error preprocessing $file";79}8081sub scanheader {82my ($f)=@_;83open my $h, "<", "$f";84while(<$h>) {85if (/^#define ((LIB|)CURL[A-Za-z0-9_]*)/) {86push @syms, $1;87}88}89close $h;90}9192sub scanallheaders {93my $d = "$root/include/curl";94opendir(my $dh, $d) ||95die "Can't opendir: $!";96my @headers = grep { /.h\z/ } readdir($dh);97closedir $dh;98foreach my $h (@headers) {99scanenum("$d/$h");100scanheader("$d/$h");101}102}103104sub checkmanpage {105my ($m) = @_;106107open(my $mh, "<", "$m");108my $line = 1;109while(<$mh>) {110# strip off formatting111$_ =~ s/(^|[^A-Z0-9])[*_]+/ /;112# detect global-looking 'CURL[BLABLA]_*' symbols113while(s/\W(CURL(AUTH|E|H|MOPT|OPT|SHOPT|UE|M|SSH|SSLBACKEND|HEADER|FORM|FTP|PIPE|MIMEOPT|GSSAPI|ALTSVC|PROTO|PROXY|UPART|USESSL|_READFUNC|_WRITEFUNC|_CSELECT|_FORMADD|_IPRESOLVE|_REDIR|_RTSPREQ|_TIMECOND|_VERSION)_[a-zA-Z0-9_]+)//) {114my $s = $1;115# skip two "special" ones116if($s !~ /^(CURLE_OBSOLETE|CURLOPT_TEMPLATE)/) {117push @manrefs, "$1:$m:$line";118}119}120$line++;121}122close($mh);123}124125sub scanman_md_dir {126my ($d) = @_;127opendir(my $dh, $d) ||128die "Can't opendir: $!";129my @mans = grep { /.md\z/ } readdir($dh);130closedir $dh;131for my $m (@mans) {132checkmanpage("$d/$m");133}134}135136137scanallheaders();138scanman_md_dir("$root/docs/libcurl");139scanman_md_dir("$root/docs/libcurl/opts");140141open my $s, "<", "$root/docs/libcurl/symbols-in-versions";142while(<$s>) {143if(/(^[^ \n]+) +(.*)/) {144my ($sym, $rest)=($1, $2);145if($doc{$sym}) {146print "Detected duplicate symbol: $sym\n";147$misses++;148next;149}150$doc{$sym}=$sym;151my @a=split(/ +/, $rest);152if($a[2]) {153# this symbol is documented to have been present the last time154# in this release155$rem{$sym}=$a[2];156}157}158}159close $s;160161my $ignored=0;162for my $e (sort @syms) {163# OBSOLETE - names that are just placeholders for a position where we164# previously had a name, that is now removed. The OBSOLETE names should165# never be used for anything.166#167# CURL_EXTERN - is a define used for libcurl functions that are external,168# public. No app or other code should ever use it.169#170# CURLINC_ - defines for header dual-include prevention, ignore those.171#172# CURL_TEMP_ - are defined and *undefined* again within the file173#174# *_LAST and *_LASTENTRY are just prefix for the placeholders used for the175# last entry in many enum series.176#177178if($e =~ /(OBSOLETE|CURLE_RESERVED|^CURL_EXTERN|^CURLINC_|_LAST\z|_LASTENTRY\z|^CURL_TEMP_)/) {179$ignored++;180next;181}182if($doc{$e}) {183if($verbose) {184print $e."\n";185}186$doc{$e}="used";187next;188}189else {190print $e."\n";191$misses++;192}193}194195#196# now scan through all symbols that were present in the symbols-in-versions197# but not in the headers198#199# If the symbols were marked 'removed' in symbols-in-versions we don't output200# anything about it since that is perfectly fine.201#202203my $anyremoved;204205for my $e (sort keys %doc) {206if(($doc{$e} ne "used") && !$rem{$e}) {207208if(!$anyremoved++) {209print "Missing symbols mentioned in symbols-in-versions\n";210print "Add them to a header, or mark them as removed.\n";211}212213print "$e\n";214$misses++;215}216}217218my %warned;219for my $r (@manrefs) {220if($r =~ /^([^:]+):(.*)/) {221my ($sym, $file)=($1, $2);222if(!$doc{$sym} && !$warned{$sym, $file}) {223print "$file: $sym is not a public symbol\n";224$warned{$sym, $file} = 1;225}226}227}228229if($summary) {230print "Summary:\n";231printf "%d symbols in headers (out of which %d are ignored)\n", scalar(@syms),232$ignored;233printf "%d symbols in headers are interesting\n",234scalar(@syms)- $ignored;235printf "%d symbols are listed in symbols-in-versions\n (out of which %d are listed as removed)\n", scalar(keys %doc), scalar(keys %rem);236printf "%d symbols in symbols-in-versions should match the ones in headers\n", scalar(keys %doc) - scalar(keys %rem);237}238239if($misses) {240exit 0; # there are stuff to attend to!241}242else {243print "OK\n";244}245246247