Skip to content

Commit

Permalink
(compiler) Detect stdlib bundled with Perlang binaries
Browse files Browse the repository at this point in the history
This provides some of the groundwork for this, mentioned in
#406:

> Distribute the (compiled) stdlib along with snapshot builds

The changes to the `Makefile` means that running `make install` will now
install the `stdlib` into the expected location. The next step is to get
the `stdlib` bundled with releases and release snapshots as well.
  • Loading branch information
perlun committed Mar 26, 2024
1 parent a6f5870 commit 4fe53e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -61,7 +61,7 @@ docfx/docfx.exe:
darkerfx-push:
rsync -av docs/templates/darkerfx/ ../darkerfx/darkerfx/

install: auto-generated
install: auto-generated stdlib
./scripts/local_install_linux.sh

# Downloads and installs the latest snapshot from https://builds.perlang.org
Expand Down
2 changes: 2 additions & 0 deletions release-notes/v0.4.0.md
Expand Up @@ -16,6 +16,7 @@
- Run more tests in compiled mode [[#427][427]]
- Implement modulo operator for `BigInt` [[#432][432]]
- Support `<<` and `>>` for bigint in compiled mode [[#440][440]]
- Detect stdlib bundled with Perlang binaries [[#444][444]]

### Changed
#### Data types
Expand Down Expand Up @@ -79,3 +80,4 @@
[440]: https://github.com/perlang-org/perlang/pull/440
[442]: https://github.com/perlang-org/perlang/pull/442
[443]: https://github.com/perlang-org/perlang/pull/443
[444]: https://github.com/perlang-org/perlang/pull/444
3 changes: 3 additions & 0 deletions scripts/local_install_linux.sh
Expand Up @@ -11,3 +11,6 @@ mkdir -p $HOME/.perlang/nightly/bin

dotnet publish src/Perlang.ConsoleApp/Perlang.ConsoleApp.csproj -c Release -r linux-x64 --self-contained true /p:PublishReadyToRun=true /p:SolutionDir=$(pwd)/
cp -r src/Perlang.ConsoleApp/bin/Release/net7.0/linux-x64/publish/* $HOME/.perlang/nightly/bin

# Copy the precompiled stdlib binaries as well, so that experimental compiled mode can find them
cp -r lib/ $HOME/.perlang/nightly/bin
43 changes: 32 additions & 11 deletions src/Perlang.Interpreter/Compiler/PerlangCompiler.cs
Expand Up @@ -490,28 +490,49 @@ private void RegisterGlobalClasses()
}
}

// TODO: Try picking this up from the location of the `perlang` executable first, if possible. We might even
// TODO: be able to get rid of this environment variable completely.
string? perlangRoot = Environment.GetEnvironmentVariable("PERLANG_ROOT");
// We attempt to detect the stdlib location by looking in the path to the `perlang` executable first. If it
// doesn't exist there, the PERLANG_ROOT environment variable is checked next.
string? executablePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

if (perlangRoot == null)
string? stdlibPath;

string perlangExecutableStdlibPath = Path.Join(executablePath, "lib", "stdlib");

if (Directory.Exists(perlangExecutableStdlibPath))
{
throw new PerlangCompilerException(
"The PERLANG_ROOT environment variable must be set for experimental compilation to succeed. " +
"It should point to a directory where the lib/stdlib directory contains a compiled version " +
"of the Perlang standard library.");
// This seems to be a Perlang distribution, either a nightly snapshot or a release version. Use the
// stdlib provided with it.
stdlibPath = perlangExecutableStdlibPath;
}
else
{
string? perlangRoot = Environment.GetEnvironmentVariable("PERLANG_ROOT");

if (perlangRoot == null)
{
throw new PerlangCompilerException(
$"The {perlangExecutableStdlibPath} directory does not exist, and PERLANG_ROOT is not set. One " +
"of these conditions must be met for experimental compilation to succeed. If setting " +
"PERLANG_ROOT, it should point to a directory where the lib/stdlib directory contains a " +
"compiled version of the Perlang standard library."
);
}

stdlibPath = Path.Join(perlangRoot, "lib", "stdlib");
}

var processStartInfo = new ProcessStartInfo
{
FileName = "clang++",
// Make this explicit, since we have only tested this with a very specific clang version. Anything else is
// completely untested and not expected to work at the moment.
FileName = "clang++-14",

ArgumentList =
{
// Required for nested namespaces
"--std=c++17",

"-I", Path.Combine(perlangRoot, "lib/stdlib/include"),
"-I", Path.Combine(stdlibPath, "include"),
"-o", targetExecutable,

// Useful while debugging
Expand Down Expand Up @@ -572,7 +593,7 @@ private void RegisterGlobalClasses()
targetCppFile,

// TODO: Support Windows static libraries as well
Path.Combine(perlangRoot, "lib/stdlib/lib/libstdlib.a"),
Path.Combine(stdlibPath, "lib/libstdlib.a"),

// Needed by the Perlang stdlib
"-lm"
Expand Down

0 comments on commit 4fe53e5

Please sign in to comment.