Skip to content

Commit

Permalink
Merge pull request #152 from Perigord-Kleisli/dev
Browse files Browse the repository at this point in the history
Make repo a flake and make ld-linux be dynamically located
  • Loading branch information
mauro-balades authored Dec 12, 2023
2 parents 24c72f7 + 89d916a commit a93550e
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 11 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ install_manifest.txt
compile_commands.json
callgrind.out.*

install_deps_temp/
install_deps_temp/

.direnv
result
.cmake
9 changes: 2 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,7 @@ if (DEFINED ENV{BUILD_FOR_CE})
add_compile_definitions(_SNOWBALL_BUILD_FOR_CE="1")
endif()

if (${BUILD_FOR_WIN})
add_compile_definitions(LD_PATH="ld")
else()
add_compile_definitions(LD_PATH="/usr/bin/ld")
endif()

add_compile_definitions(LD_PATH="ld")

add_compile_definitions(STATICLIB_DIR="${STATICLIB_DIR}")

Expand Down Expand Up @@ -353,4 +348,4 @@ install(TARGETS snowballexe EXPORT MyLibConfig
# TODO: as a feature:
# install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

#install(FILES ${std_sn_files} DESTINATION ${HOME_DIR}/${STATICLIB_DIR}/${_SNOWBALL_LIBRARY_DIR})
#install(FILES ${std_sn_files} DESTINATION ${HOME_DIR}/${STATICLIB_DIR}/${_SNOWBALL_LIBRARY_DIR})
59 changes: 59 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
description = "Snowball is a low-weight, statically typed, object oriented programming language. ";

inputs = {
flake-utils.url = "github:numtide/flake-utils";
};

outputs = {
self,
flake-utils,
nixpkgs,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {inherit system;};
in {
packages.snowball = pkgs.stdenv.mkDerivation {
name = "snowball";
version = "0.0.8";

src = ./.;

nativeBuildInputs = with pkgs; [cmake pkg-config makeWrapper];
buildInputs = with pkgs; [zstd libxml2 libsigsegv glib pcre2 libllvm libbacktrace];

patchPhase = ''
sed -i -e '/CPMAddPackage(/,/)/d' CMakeLists.txt
sed -i -e 's/app\///' app/*.cc
sed -i -e 's/app\///' app/commands/*.h
'';

buildPhase = ''
runHook preBuild
mkdir -p bin/Release
#HACK: uses RelWithDebInfo since normal realease causes a segfault
cmake \
-DLLVM_ENABLE_BACKTRACES=OFF \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DLLVM_ENABLE_TERMINFO=OFF \
-DCMAKE_OSX_ARCHITECTURES="${system}" \
-DLLVM_ENABLE_ZLIB=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DEXECUTABLE_OUTPUT_PATH="bin/Release" $@ .
cmake --build . --config RelWithDebInfo -- -j $NIX_BUILD_CORES
runHook postBuild
'';
installPhase = let
snowball = ''
'';
in ''
runHook preInstall
install --mode +x -D ./bin/Release/snowball $out/bin/unwrapped/snowball
install --mode +x -D ./libSnowball.so $out/lib/libSnowball.so
install --mode +x -D ./libsnowballrt.so $out/lib/libsnowballrt.so
cp -r $src/stdlib "$out"
wrapProgram "$out/bin/unwrapped/snowball" --suffix LD_LIBRARY_PATH ':' "$out/lib"
cat <<EOF > "$out/bin/snowball"
#!/bin/sh
if [ ! -d "\$HOME/.snowball/stdlib" ]; then
echo -e "\x1b[32mCreating \$HOME/.snowball/stdlib\x1b[0m"
mkdir "\$HOME/.snowball"
cp -r "$out/stdlib" "\$HOME/.snowball/stdlib"
fi
exec "$out/bin/unwrapped/snowball" \$@
EOF
chmod +x "$out/bin/snowball"
runHook postInstall
'';

meta = {
description = "Snowball is a low-weight, statically typed, object oriented programming language";
homepage = "https://github.com/snowball-lang/snowball";
license = pkgs.lib.licenses.mit;
platforms = pkgs.lib.platforms.all;
};
};

packages.default = self.packages.${system}.snowball;

devShells.default = pkgs.mkShell {
packages = with pkgs; [
zstd
libsigsegv
cmake
pkg-config
glib
pcre2
libllvm
libbacktrace
libxml2
clang-tools
cmake-language-server
cppcheck
];
};
});
}
20 changes: 17 additions & 3 deletions src/builder/linker/linux/ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../../constants.h"
#include "../Linker.h"

#include <dlfcn.h>
#include <filesystem>
namespace fs = std::filesystem;

Expand All @@ -30,12 +31,25 @@ void Linker::constructLinkerArgs(std::string& input, std::string& output, std::v
if (ctx->withStd) {
// TODO: check if this works for all platforms
linkerArgs.push_back("-dynamic-linker");
linkerArgs.push_back("/lib64/ld-linux-x86-64.so.2");

fs::path ld_linux_path;

Check warning on line 35 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:35:14 [cppcoreguidelines-init-variables]

variable 'ld_linux_path' is not initialized

void* handle = dlopen("ld-linux-x86-64.so.2", RTLD_LAZY);
if (!handle) { DEBUG_CODEGEN("Error getting library path: %s", dlerror()); }

Check warning on line 38 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:38:10 [readability-implicit-bool-conversion]

implicit conversion 'void *' -> bool

Dl_info info;
if (dladdr(handle, &info)) {

Check warning on line 41 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:41:9 [readability-implicit-bool-conversion]

implicit conversion 'int' -> bool
ld_linux_path = info.dli_fname;
} else {
DEBUG_CODEGEN("Error getting library path: %s", dlerror());
}

linkerArgs.push_back(ld_linux_path);

auto path = std::string("/usr") + PATH_SEPARATOR + _SNOWBALL_LIBRARY_OBJ;
auto triple = getPlatformTriple();
assert(!triple.empty() && "Unsupported platform for linking!");

Check warning on line 51 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:51:5 [cppcoreguidelines-pro-bounds-array-to-pointer-decay]

do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead
auto platformPath = fs::path(path) / triple;
auto platformPath = ld_linux_path.parent_path();
linkerArgs.push_back(platformPath / "crt1.o");
linkerArgs.push_back(platformPath / "crti.o");
if (!isIAMCU) {

Check warning on line 55 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:55:5 [bugprone-branch-clone]

if with identical then and else branches
Expand Down Expand Up @@ -84,4 +98,4 @@ std::string Linker::getSharedLibraryName(std::string& library) { return library
} // namespace linker
} // namespace snowball

#endif
#endif

0 comments on commit a93550e

Please sign in to comment.