aboutsummaryrefslogtreecommitdiff
path: root/system-clock/genmake.pl
blob: 80026b1615530607b60a7a240893497871071e9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl

# This script is designed to introspect C files and generate a makefile to use.

sub header_deps {
  my $file = @_[0];
  my @headers;

  if (open(my $fh, '<:encoding(UTF-8)', $file)) {
    print STDERR "\x1b[35m[Trace] - Reading file $file\x1b[00m\n";
    push(@headers, $file);

    while (<$fh>) {
      /#include\s+"(.*)"\s*$/ && push(@headers, header_deps($1));
    }
  }

  return @headers;
}

my @files = glob('*.c');
my @obj_files;

open(my $fh, '<:encoding(UTF-8)', "Makefile.preamble")
    or die "Missing Makefile.preamble";

while (<$fh>) {
  print "$_";
}

# Emit a rule that will rerun genmake if the c files do not match.
my $idempotency_cmd="ls *.c *.h| sha1sum | awk '{print \$ 1}'";
my $idempotency_cmd_make="ls *.c *.h | sha1sum | awk '{print \$\$1}'";
print "IDEMPOTENCY_HASH=" . `$idempotency_cmd` . "\n";

my $arch_obs_dir = "_\$(PREFIX)_obs";
print "$arch_obs_dir:\n\t";
print "mkdir $arch_obs_dir\n";

foreach $file (@files) {
  (my $file_no_ext = $file) =~ s/\.c$//g;

  my $obj_file = "$arch_obs_dir/${file_no_ext}.o";
  my $c_file = "${file_no_ext}.c";
  my $s_file = "${file_no_ext}.s";

  push(@obj_files, $obj_file);
  my @deps = header_deps($c_file);

  my $deps_as_join = join(" ", @deps);

  # Emit the rule to make the object file.
  print "$obj_file: $arch_obs_dir $deps_as_join\n\t";
  print '$(CC) -c ' . $c_file . ' -o ' . $obj_file . ' $(CFLAGS)' . "\n\n";

  # Emit the rule to make the assembly file.
  print "$s_file: $deps_as_join\n\t";
  print '$(CC) -S ' . $c_file . ' -o ' . $s_file . ' $(CFLAGS)' . "\n\n";
}

my $obj_files_deps = join(' ', @obj_files);
print "FORCE:\n\t\n\n";
print "$arch_obs_dir/main.elf: FORCE $obj_files_deps linker_script.ld\n\t";
print "([ \"\$\$($idempotency_cmd_make)\" != \"\$(IDEMPOTENCY_HASH)\" ] "
        . "&& ./genmake.pl > Makefile && make main.elf ) "
        . "|| "
        . "\$(LD) -o $arch_obs_dir/main.elf \$(LD_FLAGS) $obj_files_deps\n\n";