Permalink
Browse files

Replace Makefile with build.pl

  • Loading branch information...
1 parent 6b0123c commit 549dc3869dbd1a3fdfab9192c5a06a4c3ea010e1 @RaimoNiskanen RaimoNiskanen committed Apr 7, 2010
Showing with 207 additions and 44 deletions.
  1. +0 −38 Makefile
  2. +19 −6 README.md
  3. +188 −0 build.pl
View
@@ -1,38 +0,0 @@
-#
-## Copyright
-##
-## This document has been placed in the public domain.
-##
-## Author: Erlang/OTP, Raimo Niskanen
-#
-
-PERL = perl -w
-EEPS_DIR = eeps
-MD = md/Markdown.pl
-MK = Makefile
-IX = eep-index.pl
-PRE = eep-pre.pl
-
-
-
-all: README.html
- @$(MAKE) `$(PERL) -e '\
- $$d = shift; \
- opendir(D, $$d) || die; \
- while($$_ = readdir(D)) { \
- print "$$d/$$_\n" if s/[.]md$$/.html/; \
- }' $(EEPS_DIR)`
-
-README.html: README.md $(MK) $(MD)
- $(PERL) $(MD) README.md > $@
-
-eeps/eep-0000.html: eeps/eep-*.md $(MK) $(IX) $(PRE) $(MD)
- $(PERL) -CSD $(IX) eeps/eep-0000.md | $(PERL) -CSD $(PRE) | \
- $(PERL) $(MD) > $@
-
-eeps/eep-*.html: $(MK) $(PRE) $(MD)
-
-.SUFFIXES:
-.SUFFIXES: .html .md
-.md.html:
- $(PERL) -CSD $(PRE) $< | $(PERL) $(MD) > $@
View
@@ -6,11 +6,13 @@ Extension Proposals), in [Markdown][MD] (*.md) format produced in the
[Erlang Enhancement Process][EEP].
It also contains a version of a [Markdown.pl][] script in subdirectory
-md/ that can be used, for example with the also contained [Makefile][]
-and some Perl build scripts, to produce HTML versions of the *.md EEPs.
+md/ that can be used, for example with the also contained Perl [build
+script][build.pl] and some helper scripts, to produce HTML versions of
+the *.md EEPs.
-Just typing `make` in a posix-compatible shell with any old 'make'
-in the path plus a reasonable 'perl' (5.8) should build all HTML.
+Type `perl build.pl` or `./build.pl` depending on your environment to
+rebuild all HTML that needs to be rebuilt. A reasonable 'perl' (5.8) is all
+that is needed.
@@ -23,8 +25,9 @@ in the path plus a reasonable 'perl' (5.8) should build all HTML.
[EEP]: http://demo.erlang.org/static/dev/eep.html
"Erlang Enhancement Process"
-[Makefile]: Makefile
- "Makefile"
+[build.pl]: build.pl
+ "Perl build script to overcome Makefile inportability"
+
Copyright
@@ -34,3 +37,13 @@ This document has been placed in the public domain.
### Author
Erlang/OTP, Raimo Niskanen, 2010
+
+
+
+[EmacsVar]: <> "Local Variables:"
+[EmacsVar]: <> "mode: indented-text"
+[EmacsVar]: <> "indent-tabs-mode: nil"
+[EmacsVar]: <> "sentence-end-double-space: t"
+[EmacsVar]: <> "fill-column: 70"
+[EmacsVar]: <> "coding: utf-8"
+[EmacsVar]: <> "End:"
View
188 build.pl
@@ -0,0 +1,188 @@
+#! /usr/bin/perl
+##
+## Build script, since make is sooo unportable.
+##
+## Copyright 2010: Erlang/OTP, Raimo Niskanen
+## This document has been placed in the public domain.
+#
+#
+# $0 Build all outdated targets
+# $0 -a | --all Force build of all targets
+# $0 -c | --clean Remove all targets
+# $0 [--] Target [Target2 ...] Force build of only target(s)
+
+use strict;
+
+my $mk = $0;
+my @perl = ("perl", "-w");
+my $utf8 = '-CSD'; # Perl UTF-8 command line switch
+my $md = "md/Markdown.pl";
+my $ix = "eep-index.pl";
+my $pre = "eep-pre.pl";
+
+my $eeps_dir = 'eeps/';
+my $src_ext = '.md';
+my $dst_ext = '.html';
+my @basenames =
+ &dir_files($eeps_dir, sub {s/^(eep-\d+)$src_ext$/$1/});
+my $eep_0 = $eeps_dir.'eep-0000';
+
+my %rules =
+ (
+
+ "README.html" => #target
+ [[\&redirect, "README.html", @perl, $md, "README.md"], #build
+ "README.md", $mk, $md], #deps
+
+ $eep_0.$dst_ext => #target
+ [[\&pipe, @perl, $utf8, $ix, $eep_0.$src_ext,
+ \&pipe, @perl, $utf8, $pre,
+ \&redirect, $eep_0.$dst_ext, @perl, $md], #build
+ $eep_0.$src_ext, $mk, $ix, $pre, $md], #deps
+
+ );
+# Add rules for wildcard targets
+foreach (@basenames) {
+ my $src = $_.$src_ext;
+ my $dst = $_.$dst_ext;
+ unless (defined $rules{$dst}) {
+ $rules{$dst} = #target
+ [[\&pipe, @perl, $utf8, $pre, $src,
+ \&redirect, $dst, @perl, $md], #build
+ $src, $mk, $pre, $md]; #deps
+ }
+}
+
+
+
+my %mtime;
+my %targets;
+if (defined ($_ = $ARGV[0])) {
+ if (/^(-a|--all)$/) {
+ foreach (keys %rules) { # force build all
+ $targets{$_} = 1;
+ }
+ } elsif (/^(-c|--clean)$/) {
+ my @files = keys %rules;
+ print "rm @files\n";
+ unlink @files;
+ exit 0;
+ } else {
+ shift if /^--$/; # only targets after this
+ foreach (@ARGV) {
+ $targets{$_} = 1; # force build
+ }
+ foreach (keys %rules) { # build only forced
+ delete $rules{$_} unless $targets{$_};
+ }
+ }
+} else {
+ # Look up modification time for all files
+ &foreach_rules(sub {
+ shift;
+ foreach (@_) {
+ if (!(defined $mtime{$_}) && -f) {
+ $mtime{$_} = (stat _)[9];
+ }
+ }
+ });
+}
+
+# Call build function for all that need rebuild
+&foreach_rules(sub {
+ my ($build, $target, @deps) = @_;
+ my @build = @{$build};
+ if ($targets{$target} || ! -f $target) {
+ #print "Target $target does not exist\n";
+ &build(@build);
+ return;
+ }
+ my $target_mtime = $mtime{$target};
+ foreach my $d (@deps) {
+ -f $d or die "Missing dependency: $d";
+ if ($mtime{$d} >= $target_mtime) {
+ #print "Target $target outdated vs $d\n";
+ &build(@build);
+ return;
+ }
+ }
+});
+
+exit 0;
+
+
+
+# Toplevel build, wait for last pid in pipe
+sub build {
+ my $func = shift;
+ open SAVEOUT, ">&STDOUT" or die "Can't dup STDOUT: $!";
+ my $last_pid = &{$func};
+ close STDOUT;
+ waitpid $last_pid, 0;
+ open STDOUT, ">&SAVEOUT" or die "Can't dup SAVEOUT: $!";
+}
+
+# Make a pipe of the arguments to the next subroutine
+sub pipe {
+ my @call;
+ my $func;
+ while (@_) {
+ my $x = shift;
+ if (ref $x) {
+ # next subroutine found
+ $func = $x;
+ last;
+ } else {
+ push @call, $x;
+ }
+ }
+ print "@call | ";
+ my $last_pid;
+ if (defined $func) {
+ print "@call | ";
+ $last_pid = &{$func};
+ } else {
+ print "@call\n";
+ }
+ my $pid;
+ unless ($pid = open STDOUT, '|-') {
+ defined $pid or die "can't fork: $!";
+ exec {$call[0]} @call or die "can't exec @call: $!";
+ }
+ return $last_pid if defined $last_pid;
+ return $pid;
+}
+
+# Redirect command to destination file
+sub redirect {
+ my $dst = shift;
+ print "@_ > $dst\n";
+ my $pid;
+ unless ($pid = open STDOUT, '|-') {
+ defined $pid or die "can't fork: $!";
+ open STDOUT, ">$dst" or die "can't open > $dst: $!";
+ exec {$_[0]} @_ or die "can't exec @_: $!";
+ }
+ return $pid;
+}
+
+# Helper loop subroutine over %rules
+sub foreach_rules {
+ my ($func) = @_;
+ foreach my $target (sort (keys %rules)) {
+ my ($build, @files) = @{$rules{$target}};
+ &{$func}($build, $target, @files);
+ }
+}
+
+# Filename wildcard
+sub dir_files {
+ my ($dir, $subst) = @_;
+ my @names;
+ opendir D, $dir || die "Can't opendir $dir: $!";
+ while ($_ = readdir(D)) {
+ push @names, $dir.$_ if &{$subst};
+ }
+ closedir D;
+ return @names;
+}

0 comments on commit 549dc38

Please sign in to comment.