Make everything about make_makefile_src better

Fixes #626 #627
This commit is contained in:
Derek Bankieris 2018-06-05 13:48:40 -05:00 committed by dbankieris
parent ea0878af26
commit 334273fa81

View File

@ -123,7 +123,6 @@ my @all_cfly_files ;
my @all_read_only_libs ;
my @all_compile_libs ;
my %files_by_dir ;
my ( $sp_dir , $src_dir , $sp_file , $base_name , $suffix) ;
my @exclude_dirs ;
@exclude_dirs = split /:/ , "$ENV{TRICK_EXCLUDE}:$ENV{TRICK_EXT_LIB_DIRS}";
@ -148,43 +147,31 @@ if (scalar @exclude_dirs) {
# split off files by directory
foreach ( @all_cfly_files ) {
$sp_file = basename($_) ;
$_ = abs_path(dirname($_)) ;
( $sp_dir , $src_dir ) = /(.*?)(?:\/(src))?$/ ;
$src_dir .= "/" if ($src_dir ne "") ;
($base_name , $suffix) = $sp_file =~ /(.*?)([cfly]$|C$|cc$|cxx$|cpp$|c\+\+$)/ ;
$files_by_dir{$sp_dir}{src_dir} = $src_dir ;
push @{$files_by_dir{$sp_dir}{$suffix}} , $base_name ;
my ($name, $path, $extension) = fileparse($_, qr/\.[^.]*/) ;
$path = abs_path($path) ;
push @{$files_by_dir{$path}{$extension}} , $name ;
}
# get all of the files required by compiled libraries
# compile all files as normal files, we're not going to make a library anymore.
foreach $n ( @all_compile_libs ) {
my @local_files ;
$sp_file = basename($n) ;
$sp_dir = dirname($n) ;
$sp_dir =~ s/\/object_\$\{TRICK_HOST_CPU\}?$// ;
$sp_dir = abs_path($sp_dir) ;
$src_dir = ( -e "$sp_dir/src" ) ? "src/" : "" ;
$files_by_dir{$sp_dir}{src_dir} = $src_dir ;
opendir THISDIR, "$sp_dir/$src_dir" or die "Could not open the directory $sp_dir/$src_dir";
@local_files = grep !/^\.\.\./ , readdir THISDIR;
@local_files = grep /\.[cfly]$|C$|cc$|cxx$|cpp$|c\+\+$/ , @local_files;
foreach $k ( @local_files ) {
($base_name , $suffix) = $k =~ /(.*?)([cfly]$|C$|cc$|cxx$|cpp$|c\+\+$)/ ;
push @{$files_by_dir{$sp_dir}{$suffix}} , $base_name ;
foreach ( @all_compile_libs ) {
my $path = abs_path(dirname($_)) ;
opendir THISDIR, "$path" or die "Could not open $path";
my @files = grep !/^\.\.\./ , readdir THISDIR;
@files = grep /\.[cfly]$|C$|cc$|cxx$|cpp$|c\+\+$/ , @files;
foreach ( @files ) {
my ($name, $unused, $extension) = fileparse($_, qr/\.[^.]*/) ;
push @{$files_by_dir{$path}{$extension}} , $name ;
}
closedir THISDIR ;
}
# sort and weed out duplicate files
foreach $k ( keys %files_by_dir ) {
foreach my $directory ( keys %files_by_dir ) {
my %temp_hash ;
foreach $n ( qw{ c f l y h C cc cxx cpp c++} ) {
foreach my $extension ( keys $files_by_dir{$directory} ) {
undef %temp_hash ;
@{$files_by_dir{$k}{$n}} = sort grep ++$temp_hash{$_} < 2, @{$files_by_dir{$k}{$n}} ;
@{$files_by_dir{$directory}{$extension}} = sort grep ++$temp_hash{$_} < 2, @{$files_by_dir{$directory}{$extension}} ;
}
}
@ -249,26 +236,32 @@ build/S_source.d: ;
" ;
my %object_files_by_type ;
my %model_build_dirs ;
# List out all of the object files and put the list in a file that we can pass to the linker.
# Passing all of them directly to the linker in the command line can exceed the line limit.
open MODEL_LINK_LIST, ">build/model_link_list" or die "Could not open build/model_link_list" ;
foreach my $ext ( qw{ c C cc cxx cpp c++ } ) {
print MAKEFILE "MODEL_${ext}_OBJECTS :=" ;
foreach $k ( sort keys %files_by_dir ) {
foreach $f ( @{$files_by_dir{$k}{$ext}} ) {
print MAKEFILE " \\\n build$k/$files_by_dir{$k}{src_dir}$f" . "o" ;
print MODEL_LINK_LIST "build$k/$files_by_dir{$k}{src_dir}$f" . "o\n" ;
my %files_by_extension ;
foreach my $directory ( keys %files_by_dir ) {
foreach my $extension ( grep { /^\.(c|cc|C|cxx|cpp|c\+\+)$/ } keys $files_by_dir{$directory} ) {
foreach my $file ( @{$files_by_dir{$directory}{$extension}} ) {
push @{$files_by_extension{$extension}} , "build$directory/$file.o" ;
}
}
}
foreach my $extension ( keys %files_by_extension ) {
print MAKEFILE "MODEL_OBJECTS${extension} :=" ;
foreach my $file ( @{$files_by_extension{$extension}} ) {
print MAKEFILE " \\\n $file" ;
print MODEL_LINK_LIST "$file\n" ;
}
print MAKEFILE "\n\n"
}
close MODEL_LINK_LIST ;
print MAKEFILE "MODEL_OBJECTS :=" ;
foreach my $ext ( qw{ c C cc cxx cpp c++ } ) {
print MAKEFILE " \${MODEL_${ext}_OBJECTS}" ;
foreach my $extension ( keys %files_by_extension ) {
print MAKEFILE " \${MODEL_OBJECTS$extension}" ;
}
# Write out the compile rules for each type of file.
@ -277,46 +270,21 @@ print MAKEFILE "
# These targets should have an order-only dependency on their directories, which would require
# moving them to a .SECONDEXPANSION section and would complicate the rules. However, these
# directories appear to be created as part of the read_lib_deps function, so I'm leaving it out
# for now.
# for now." ;
\${MODEL_c_OBJECTS} : build/%.o : /%.c | build/%.d
foreach my $extension ( keys %files_by_extension ) {
my $compiler = "TRICK_" . ($extension eq ".c" ? "CC" : "CPPC") ;
my $flags = $extension eq ".c" ? "C" : "CXX" ;
my $command = "\$($compiler) \$(TRICK_${flags}FLAGS) \$(TRICK_SYSTEM_${flags}FLAGS) -I\$(<D)/../include -MMD -MP -c -o \$\@ \$<" ;
print MAKEFILE "
\${MODEL_OBJECTS$extension} : build/%.o : /%$extension | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CC) \$(TRICK_CFLAGS) \$(TRICK_SYSTEM_CFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CC) \$(TRICK_CFLAGS) \$(TRICK_SYSTEM_CFLAGS) -I\$(<D)/../include -MMD -MP -c -o \$\@ \$< 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
\t\@echo $command >> \$(MAKE_OUT)
\t\$(ECHO_CMD)$command 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}" ;
}
\${MODEL_cc_OBJECTS} : build/%.o : /%.cc | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
\${MODEL_cpp_OBJECTS} : build/%.o : /%.cpp | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
\${MODEL_C_OBJECTS} : build/%.o : /%.C | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
\${MODEL_cxx_OBJECTS} : build/%.o : /%.cxx | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
\${MODEL_c++_OBJECTS} : build/%.o : /%.c++ | build/%.d
\t\$(PRINT_COMPILE)
\t\@echo \$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ >> \$(MAKE_OUT)
\t\$(ECHO_CMD)\$(TRICK_CPPC) \$(TRICK_CXXFLAGS) \$(TRICK_SYSTEM_CXXFLAGS) -I\$(<D)/../include -MMD -MP -c \$< -o \$\@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
#define compile_rule
#build/%.o: /%.\$1
#\t\$\$(PRINT_COMPILE)
#\t\$\$(ECHO_CMD)\$\$(TRICK_CPPC) \$\$(TRICK_CXXFLAGS) \$\$(TRICK_SYSTEM_CXXFLAGS) -I\$\$(<D)/../include -MMD -MP -c \$\$< -o \$\$@ 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]}
#endef
#
#EXTENSIONS := C cc cpp cxx c++
#\$(foreach EXTENSION,\$(EXTENSIONS),\$(eval \$(call compile_rule,\$(EXTENSION))))
print MAKEFILE "
\$(MODEL_OBJECTS:.o=.d): ;
@ -366,18 +334,20 @@ foreach $k ( sort keys %files_by_dir ) {
while ( s,/[^/.]+/\.\.,, ) {}
s//$comment/ ;
if ( s/^objects\s*:\s*// ) {
foreach my $ext ( qw{c C cc cxx cpp CPLUSPLUS l y} ) {
foreach my $file (@{$files_by_dir{$k}{$ext}}) {
$files_by_dir{$k}{overrides} .= "build$k/${file}o \\\n" ;
foreach my $extension ( keys %files_by_extension ) {
foreach my $file (@{$files_by_dir{$k}{$extension}}) {
$files_by_dir{$k}{overrides} .= "build$k/${file}.o \\\n" ;
}
}
$files_by_dir{$k}{overrides} .= ": $_"
}
elsif ( s/([cfhy]|C|cc|cxx|cpp|CPLUSPLUS)_objects\s*:\s*// ) {
foreach my $file (@{$files_by_dir{$k}{$1}}) {
$files_by_dir{$k}{overrides} .= "build$k/${file}o \\\n" ;
elsif ( s/(.+)_objects\s*:\s*// ) {
if (scalar @{$files_by_dir{$k}{".$1"}}) {
foreach my $file (@{$files_by_dir{$k}{".$1"}}) {
$files_by_dir{$k}{overrides} .= "build$k/$file.o \\\n" ;
}
$files_by_dir{$k}{overrides} .= ": $_"
}
$files_by_dir{$k}{overrides} .= ": $_"
}
else {
$files_by_dir{$k}{overrides} .= $_ ;