Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rakefile for building dmd, druntime and phobos from trunk
# -*- coding: utf-8 -*-
#
# Building dmd trunk
#
require 'rake'
OS = 'osx'
DMD_DIR = 'trunk'
BIN_DIR = File.join(DMD_DIR, OS, 'bin')
LIB_DIR = File.join(DMD_DIR, OS, 'lib')
SRC_DIR = File.join(DMD_DIR, 'src')
PROJECTS = ['dmd', 'druntime', 'phobos']
MAKE = ENV['MAKE'] || 'make'
MODEL = ENV['MODEL'] || '64'
PARALLEL = ENV['PARALLEL'] || '4'
desc 'Run dmd build and replace by default'
task :default => [:build]
task :all => [:clean, :build] do
puts "All task finished"
end
task :build => [:prepare, :run, :move] do
puts "Complete building!"
if ENV.has_key?('TO')
path = ENV['TO']
FileUtils.rm_r(path) if FileTest.exist?(path)
FileUtils.mv(DMD_DIR, path)
end
end
desc 'Move important files'
task :move do
dmd_path = File.join('dmd', 'src')
FileUtils.mv(File.join(dmd_path, 'dmd'), BIN_DIR)
FileUtils.mv(File.join('phobos', 'generated', OS, 'release', MODEL, 'libphobos2.a'), LIB_DIR)
# Fixup source directories
FileUtils.mkdir_p(SRC_DIR)
FileUtils.cp_r(dmd_path, SRC_DIR)
FileUtils.cp_r(File.join('druntime'), SRC_DIR)
FileUtils.cp_r(File.join('phobos'), SRC_DIR)
FileUtils.mv(File.join(SRC_DIR, 'src'), File.join(SRC_DIR, 'dmd'))
end
desc 'Build projects'
task :run do |t|
builder = '__build.sh'
File.open(builder, 'w') { |f| f.write(build_file) }
FileUtils.chmod(0755, builder)
sh("./#{builder}")
FileUtils.rm(builder)
end
desc 'Prepare environemnt'
task :prepare do
FileUtils.rm_r(DMD_DIR) if FileTest.exist?(DMD_DIR)
FileUtils.mkdir_p(LIB_DIR)
FileUtils.mkdir_p(BIN_DIR)
File.open(File.join(BIN_DIR, 'dmd.conf'), 'w') { |f| f.write(conf_file) }
File.open(File.join(BIN_DIR, 'dmdx.conf'), 'w') { |f| f.write(xconf_file) }
end
desc 'Clean up previous garbage'
task :clean do
FileUtils.rm_r(PROJECTS)
end
def conf_file
<<EOS
[Environment]
DFLAGS=-I%@P%/../../src/phobos -I%@P%/../../src/druntime/import -L-L%@P%/../lib
EOS
end
def xconf_file
<<EOS
[Environment]
DFLAGS=-I~/dmd2/src/phobos -I~/dmd2/src/druntime/import
EOS
end
def build_file
# from https://github.com/D-Programming-Language/tools/blob/master/update.sh
<<EOS
#!/usr/bin/env zsh
setopt err_exit
local projects
typeset -a projects
projects=(#{PROJECTS.join(' ')})
# Working directory
local wd=$(pwd)
# Configuration
local makecmd=#{MAKE}
local parallel=#{PARALLEL}
local model=#{MODEL}
# List of projects to install vs. update. Their disjoint union is
# $projects.
local toInstall toUpdate
typeset -a toInstall toUpdate
# Mess to go here
local tempdir=$(mktemp -d /tmp/dmd-update.XXX)
#
# Take care of the command line arguments
#
function handleCmdLine() {
local arg
for arg in $*; do
case $arg in
(--tag=*)
tag="`echo $arg | sed 's/[-a-zA-Z0-9]*=//'`"
;;
(*)
echo "Error: $arg not recognized." >&2exit 1
;;
esac
done
if [[ ! -z $tag ]]; then
wd+="/$tag"
mkdir -p "$wd"
fi
}
#
# Confirm correct choices
#
function confirmChoices() {
function joinWithWorkingDir() {
for i in $*; do
echo "$wd/$i"
done
}
for project in $projects; do
if [ -e "$wd/$project" ]; then
toUpdate=($toUpdate "$project")
else
toInstall=($toInstall "$project")
fi
done
if [[ ! -z $toInstall ]]; then
echo "*** The following projects will be INSTALLED:"
joinWithWorkingDir ${toInstall}
echo "*** Note: this script assumes you have a github account set up."
fi
if [[ ! -z $toUpdate ]]; then
echo "*** The following projects will be UPDATED:"
joinWithWorkingDir ${toUpdate}
fi
}
#
# Install from scratch
#
function installAnew() {
local projects
projects=($*)
for project in $projects; do
(
cd $wd &&
git clone --quiet -o upstream git://github.com/D-Programming-Language/$project.git &&
touch $tempdir/$project
) &
done
wait
for project in $projects; do
if [ ! -f $tempdir/$project ]; then
echo "Getting $project failed." >&2
rm -rf $tempdir
exit 1
fi
if [[ ! -z $tag &&
($project = dmd || $project = druntime || $project = phobos ||
$project = d-programming-language.org) ]]; then
( cd $wd/$project && git checkout v$tag )
fi
done
}
#
# Freshen existing stuff
#
function update() {
echo "Updating projects in $wd..."
function update_project() {
local project=$1
if ! ( cd "$wd/$project" && \
git checkout master && \
git pull upstream master && \
git pull upstream master --tags && \
git fetch && \
git fetch --tags) 2>$tempdir/$project.log
then
echo "Failure updating $wd/$project." >>$tempdir/errors
exit 1
fi
}
for project in $toUpdate; do
update_project $project &
done
wait
if [ -f $tempdir/errors ]; then
cat $tempdir/*.log >&2
exit 1
fi
}
function makeWorld() {
# First make dmd
(
cd "$wd/dmd/src" &&
$makecmd -f posix.mak clean MODEL=$model &&
$makecmd -f posix.mak -j $parallel MODEL=$model
)
# Then make druntime
(
cd "$wd/druntime" &&
$makecmd -f posix.mak -j $parallel DMD="$wd/dmd/src/dmd" MODEL=$model
)
# Then make phobos
(
cd "$wd/phobos" &&
$makecmd -f posix.mak -j $parallel DMD="$wd/dmd/src/dmd" MODEL=$model
)
}
# main
handleCmdLine
confirmChoices
installAnew $toInstall
update $toUpdate
makeWorld
EOS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment