#!/usr/bin/env perl
#
# Copyright (C) 2001-2022 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# make2cdb
#
# Parses automake files throughout the source tree and generates
# a "compilation database" file, "compile_commands.json".
#
# See also: https://clang.llvm.org/docs/JSONCompilationDatabase.html
#
# Usage: make2cdb [--list] [<src-dir> [<top-dir> [<src-to-top>]]]
#
# Eg:
#  $ cd src
#  $ ../bin/make2cdb > compile_commands.json
#  $ ../bin/make2cdb --list | xargs clang-tidy -p .
#

use strict ;
use Cwd ;
use FileHandle ;
use File::Find ;
use File::Basename ;
use File::Spec ;
use File::Copy ;
use Getopt::Long ;
use lib dirname($0) ;
use ConfigStatus ;
use CompilationDatabase ;

my %opt = () ;
GetOptions( \%opt , "list" , "test" , "debug" ) or die "$0: usage error\n" ;
my $cfg_show_list = exists( $opt{list} ) ;
my $cfg_test_mode = exists( $opt{test} ) ;
my $cfg_debug = exists( $opt{debug} ) ;
$CompilationDatabase::debug = $cfg_debug ;
$AutoMakeParser::debug = $cfg_debug ;

my $src_dir = @ARGV ? $ARGV[0] : dirname($0)."/../src" ;
my $top_dir = @ARGV >= 1 ? $ARGV[1] : "$src_dir/.." ;
my $src_to_top = @ARGV >= 2 ? $ARGV[2] : ".." ;

my $cs = new ConfigStatus( "$top_dir/config.status" ) ;
my %switches = $cs->switches() ;
my %vars = $cs->vars() ;
$vars{top_srcdir} = $src_to_top ;
$vars{top_builddir} = "." ;

my $cdb = new CompilationDatabase( $src_dir , \%switches , \%vars , {test_mode=>$cfg_test_mode,full_paths=>1} ) ;
if( $cfg_show_list )
{
	print join( "\n" , $cdb->list() , "" ) ;
}
elsif( $cfg_test_mode )
{
	print join( "" , $cdb->stanzas() ) ;
}
else
{
	print "[\n" ;
	print join( ",\n" , $cdb->stanzas() ) ;
	print "]\n" ;
}

