implement --hdrsrc substitution mechanism
[rofl0r-rcb.git] / rcb2make.pl
blob8963940be464379c7daf3d8dc427c03c314cac87
1 #!/usr/bin/env perl
3 # program to automatically generate an optimized Makefile from an rcb file
4 # it is optimized because it only compiles *used* stuff
5 # use like this: cat myprog.rcb | rcb2make myprog > Makefile
7 use strict;
8 use warnings;
10 my $progname = $ARGV[0] or
11 die ("pass name of executable the makefile has to build");
13 my @libs;
14 my @c;
16 while(<STDIN>) {
17 chomp;
18 if(/^DEP ([\w_\-\/\.]+)$/) {
19 push @c, $1;
20 } elsif (/^LINK ([\w_\-\/\.]+)$/) {
21 push @libs, $1;
25 sub make_list {
26 my @a = @_;
27 my $res = "";
28 for(@a) {
29 $res .= " \\\n" . $_;
31 return $res;
34 my $mak_template = << 'EOF';
35 prefix = /usr/local
36 bindir = $(prefix)/bin
38 PROG = #PROG#
39 SRCS = #SRCS#
40 LIBS = #LIBS#
41 OBJS = $(SRCS:.c=.o)
43 CFLAGS += -Wall -D_GNU_SOURCE
45 -include config.mak
47 all: $(PROG)
49 install: $(PROG)
50 install -d $(DESTDIR)/$(bindir)
51 install -D -m 755 $(PROG) $(DESTDIR)/$(bindir)/
53 clean:
54 rm -f $(PROG)
55 rm -f $(OBJS)
57 %.o: %.c
58 $(CC) $(CPPFLAGS) $(CFLAGS) $(INC) $(PIC) -c -o $@ $<
60 $(PROG): $(OBJS)
61 $(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
63 .PHONY: all clean install
65 EOF
67 $mak_template =~ s/#PROG#/$progname/;
68 my $lc = make_list(@c);
69 $mak_template =~ s/#SRCS#/$lc/;
70 my $ll = make_list(@libs);
71 $mak_template =~ s/#LIBS#/$ll/;
73 print $mak_template;