Skip to content

Commit

Permalink
dmd: bootstrap from source
Browse files Browse the repository at this point in the history
  • Loading branch information
jtbx committed Jul 27, 2024
1 parent bc3208b commit d9e6661
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 31 deletions.
13 changes: 13 additions & 0 deletions pkgs/by-name/dm/dmd/bootstrap-sysconfdir.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- a/dmd/src/inifile.c
+++ b/dmd/src/inifile.c
@@ -106,9 +106,7 @@ const char *findConfFile(const char *argv0, const char *inifile)
}

// Search /etc/ for inifile
-#ifndef SYSCONFDIR
-# error SYSCONFDIR not defined
-#endif
+#define SYSCONFDIR "@out@/etc/"
assert(SYSCONFDIR != NULL && strlen(SYSCONFDIR));
filename = FileName::combine(SYSCONFDIR, inifile);
#endif // __linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun
14 changes: 6 additions & 8 deletions pkgs/by-name/dm/dmd/bootstrap.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{ callPackage }:
callPackage ./binary.nix {
version = "2.090.1";
hashes = {
# Get these from `nix-prefetch-url http://downloads.dlang.org/releases/2.x/2.090.1/dmd.2.090.1.linux.tar.xz` etc..
osx = "sha256-9HwGVO/8jfZ6aTiDIUi8w4C4Ukry0uUS8ACP3Ig8dmU=";
linux = "sha256-ByCrIA4Nt7i9YT0L19VXIL1IqIp+iObcZux407amZu4=";
};
import ./generic.nix {
version = "2.076.1";
dmdRevision = "54c21a585da7ee7a6af24963a7e0973ea5b4d99b";
dmdHash = "sha256-APagC4FykwYZMTpC7gMy9bz5K4BRkcBRz1g2GsKohB8=";
phobosHash = "sha256-sSxhATBYlJ0mSrDZzmM6UZU6t7SWTtNECFsvY3ds0Gw=";
bootstrap = true;
}
84 changes: 61 additions & 23 deletions pkgs/by-name/dm/dmd/generic.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{ version
, dmdRevision ? "v${version}"
, phobosRevision ? "v${version}"
, dmdHash
, phobosHash
, bootstrap ? false
}:

{ stdenv
Expand All @@ -21,8 +24,17 @@
, tzdata
, unzip
, which
, dmdBootstrap ? callPackage ./bootstrap.nix { }
, dmdBin ? "${dmdBootstrap}/bin"

, dmdBootstrap ?
if !bootstrap then
callPackage ./bootstrap.nix { }
else
null
, dmdBin ?
if !bootstrap then
lib.getExe dmdBootstrap
else
""
}:

let
Expand All @@ -34,19 +46,21 @@ let
};
});
};

# TODO: move dmd.conf for non-bootstrap builds to etc/
dmdConfPath = "$out/${if bootstrap then "etc" else "bin"}/dmd.conf";
druntimeImport = (lib.optionalString (!bootstrap) "dmd/") + "druntime/import/";
bits = builtins.toString stdenv.hostPlatform.parsed.cpu.bits;
osname =
if stdenv.isDarwin then
"osx"
else
stdenv.hostPlatform.parsed.kernel.name;

pathToDmd = "\${NIX_BUILD_TOP}/dmd/generated/${osname}/release/${bits}/dmd";
in

stdenv.mkDerivation (finalAttrs: {
pname = "dmd";
pname = "dmd"
+ lib.optionalString bootstrap "-bootstrap";
inherit version;

enableParallelBuilding = true;
Expand All @@ -55,25 +69,35 @@ stdenv.mkDerivation (finalAttrs: {
(fetchFromGitHub {
owner = "dlang";
repo = "dmd";
rev = "v${finalAttrs.version}";
rev = dmdRevision;
hash = dmdHash;
name = "dmd";
})
(fetchFromGitHub {
owner = "dlang";
repo = "phobos";
rev = "v${finalAttrs.version}";
rev = phobosRevision;
hash = phobosHash;
name = "phobos";
})
] ++ lib.optionals bootstrap [
(fetchFromGitHub {
owner = "dlang";
repo = "druntime";
rev = "98c6ff0cf1241a0cfac196bf8a0523b1d4ecd3ac";
hash = "sha256-4xhPzyNeafuayy39wAlXiJ+eD0eXcpcgPPMxELGlcMk=";
name = "druntime";
})
];

sourceRoot = ".";

# https://issues.dlang.org/show_bug.cgi?id=19553
hardeningDisable = [ "fortify" ];

patches = lib.optionals (lib.versionOlder version "2.088.0") [
patches = lib.optionals bootstrap [
./bootstrap-sysconfdir.diff
] ++ lib.optionals (!bootstrap && lib.versionOlder version "2.088.0") [
# Migrates D1-style operator overloads in DMD source, to allow building with
# a newer DMD
(fetchpatch {
Expand All @@ -85,6 +109,10 @@ stdenv.mkDerivation (finalAttrs: {
];

postPatch = ''
'' + lib.optionalString bootstrap ''
substituteInPlace dmd/src/inifile.c \
--subst-var out
'' + lib.optionalString (!bootstrap) ''
patchShebangs dmd/compiler/test/{runnable,fail_compilation,compilable,tools}{,/extra-files}/*.sh
rm dmd/compiler/test/runnable/gdb1.d
Expand Down Expand Up @@ -116,6 +144,8 @@ stdenv.mkDerivation (finalAttrs: {
makeWrapper
which
installShellFiles
] ++ lib.optionals (!bootstrap) [
dmdBootstrap
] ++ lib.optionals (lib.versionOlder version "2.088.0") [
git
];
Expand All @@ -133,21 +163,28 @@ stdenv.mkDerivation (finalAttrs: {
unzip
];

buildFlags = [
"BUILD=release"
"ENABLE_RELEASE=1"
"PIC=1"
];
buildFlags =
if bootstrap then [
"-fposix.mak"
"CXXFLAGS+=-Wno-format-security"
"CXXFLAGS+=-DTARGET_LINUX"
] else [
"BUILD=release"
"ENABLE_RELEASE=1"
"PIC=1"
];

# Build and install are based on http://wiki.dlang.org/Building_DMD
buildPhase = ''
runHook preBuild
export buildJobs=$NIX_BUILD_CORES
[ -z "$enableParallelBuilding" ] && buildJobs=1
${dmdBin}/rdmd dmd/compiler/src/build.d -j$buildJobs $buildFlags \
HOST_DMD=${dmdBin}/dmd
'' + lib.optionalString bootstrap ''
make -C dmd -j$buildJobs $buildFlags
'' + lib.optionalString (!bootstrap) ''
${dmdBin} -run dmd/compiler/src/build.d -j$buildJobs $buildFlags \
HOST_DMD=${dmdBin}
make -C dmd/druntime -j$buildJobs DMD=${pathToDmd} $buildFlags
echo ${tzdata}/share/zoneinfo/ > TZDatabaseDirFile
echo ${lib.getLib curl}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary} \
Expand All @@ -158,7 +195,7 @@ stdenv.mkDerivation (finalAttrs: {
runHook postBuild
'';

doCheck = true;
doCheck = !bootstrap;

# many tests are disabled because they are failing

Expand All @@ -171,7 +208,8 @@ stdenv.mkDerivation (finalAttrs: {
[ -z "$enableParallelChecking" ] && checkJobs=1
CC=$CXX HOST_DMD=${pathToDmd} NIX_ENFORCE_PURITY= \
${dmdBin}/rdmd dmd/compiler/test/run.d -j$checkJobs
${dmdBin} -i -Idmd/compiler/test \
-run dmd/compiler/test/run.d -j$checkJobs
NIX_ENFORCE_PURITY= \
make -C phobos unittest -j$checkJobs $checkFlags \
Expand All @@ -188,7 +226,7 @@ stdenv.mkDerivation (finalAttrs: {
installManPage dmd/docs/man/man*/*
mkdir -p $out/include/dmd
cp -r {dmd/druntime/import/*,phobos/{std,etc}} $out/include/dmd/
cp -r {${druntimeImport}/*,phobos/{std,etc}} $out/include/dmd/
mkdir $out/lib
cp phobos/generated/${osname}/release/${bits}/libphobos2.* $out/lib/
Expand All @@ -197,19 +235,19 @@ stdenv.mkDerivation (finalAttrs: {
--prefix PATH : "${targetPackages.stdenv.cc}/bin" \
--set-default CC "${targetPackages.stdenv.cc}/bin/cc"
substitute ${dmdConfFile} "$out/bin/dmd.conf" --subst-var out
substitute ${dmdConfFile} ${dmdConfPath} --subst-var out
runHook postInstall
'';

preFixup = ''
find $out/bin -type f -exec ${removeReferencesTo}/bin/remove-references-to -t ${dmdBin}/dmd '{}' +
find $out/bin -type f -exec ${removeReferencesTo}/bin/remove-references-to -t ${dmdBin} '{}' +
'';

disallowedReferences = [ dmdBootstrap ];
disallowedReferences = lib.optional (!bootstrap) dmdBootstrap;

passthru = {
inherit dmdBootstrap;
bootstrap = dmdBootstrap;
};

meta = with lib; {
Expand Down

0 comments on commit d9e6661

Please sign in to comment.