+run;
+exit 0;
+
+sub run() {
+ mkdir BUILDPATH;
+ chdir BUILDPATH or die "Could not open build directory: $!";
+ unlink 'include';
+ symlink "${\SOURCEPATH}/include", 'include';
+ mkdir $_ for qw/bin modules obj/;
+
+ open MAKE, '>real.mk' or die "Could not write real.mk: $!";
+ chdir "${\SOURCEPATH}/src";
+
+ run_dynamic();
+ close MAKE;
+}
+
+sub run_dynamic() {
+ print MAKE <<END;
+# DO NOT EDIT THIS FILE
+# It is autogenerated by make/calcdep.pl, and will be overwritten
+# every time you rerun make in the main directory
+VPATH = \$(SOURCEPATH)/src
+
+bad-target:
+ \@echo "This Makefile must be run by a sub-make from the source"
+ \@echo "in order to set the correct environment variables"
+ \@exit 1
+
+all: inspircd modules
+
+END
+ my(@core_deps, @modlist);
+ for my $file (<*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
+ my $out = find_output $file;
+ dep_cpp $file, $out, 'gen-o';
+ next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
+ # Having a module in the src directory is a bad idea because it will be linked to the core binary
+ if ($file =~ /^(m|core)_.*\.cpp/) {
+ my $correctsubdir = ($file =~ /^m_/ ? "modules" : "coremods");
+ print "Error: module $file is in the src directory, put it in src/$correctsubdir instead!\n";
+ exit 1;
+ }
+ push @core_deps, $out;
+ }
+
+ foreach my $directory (qw(coremods modules)) {
+ opendir(my $moddir, $directory);
+ for my $file (sort readdir $moddir) {
+ next if $file =~ /^\./;
+ if ($directory eq 'modules' && -e "modules/extra/$file" && !-l "modules/$file") {
+ # Incorrect symlink?
+ print "Replacing symlink for $file found in modules/extra\n";
+ rename "modules/$file", "modules/$file~";
+ symlink "extra/$file", "modules/$file";
+ }
+ if ($file =~ /^(?:core|m)_/ && -d "$directory/$file" && dep_dir "$directory/$file", "modules/$file") {
+ mkdir "${\BUILDPATH}/obj/$file";
+ push @modlist, "modules/$file.so";
+ }
+ if ($file =~ /^.*\.cpp$/) {
+ my $out = dep_so "$directory/$file";
+ push @modlist, $out;
+ }
+ }
+ }
+
+ my $core_mk = join ' ', @core_deps;
+ my $mods = join ' ', @modlist;
+ print MAKE <<END;
+
+bin/inspircd: $core_mk
+ @\$(SOURCEPATH)/make/unit-cc.pl core-ld \$\@ \$^ \$>
+
+inspircd: bin/inspircd
+
+modules: $mods
+
+.PHONY: all bad-target inspircd modules
+
+END
+}
+
+sub find_output {
+ my $file = shift;
+ my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
+ if ($path eq 'modules/' || $path eq 'coremods/') {
+ return "modules/$base.so";
+ } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
+ return "obj/$base.o";
+ } elsif ($path =~ m#modules/(m_.*)/# || $path =~ m#coremods/(core_.*)/#) {
+ return "obj/$1/$base.o";
+ } else {
+ die "Can't determine output for $file";
+ }
+}
+
+sub gendep($) {