Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: have crt* located by dlopen and make repo a flake #156

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ install_manifest.txt
compile_commands.json
callgrind.out.*

install_deps_temp/
install_deps_temp/
result
.direnv/
.cmake/
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +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 +349,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.

105 changes: 105 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
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 = ''
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xml?

clang-tools
cmake-language-server
cppcheck
];
};
});
}
66 changes: 59 additions & 7 deletions src/builder/linker/linux/ld.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// only generate for linux
#if defined(__linux__) || defined(__gnu_linux__) || defined(__linux) || defined(__LINUX__)

#include "../../../ast/errors/error.h"
#include "../../../constants.h"
#include "../Linker.h"
#include <dlfcn.h>

#include <filesystem>
namespace fs = std::filesystem;
Expand All @@ -10,11 +12,11 @@
namespace snowball {
namespace linker {

void Linker::constructLinkerArgs(std::string& input, std::string& output, std::vector<std::string>& args) {

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

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:15:14 [readability-function-cognitive-complexity]

function 'constructLinkerArgs' has cognitive complexity of 39 (threshold 25)
const bool isIAMCU = target.isOSIAMCU();

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

variable 'isIAMCU' is not initialized
linkerArgs.clear();
if (ctx->isDynamic) {

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

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:18:3 [bugprone-branch-clone]

if with identical then and else branches
//linkerArgs.push_back("-pic");
// linkerArgs.push_back("-pic");
linkerArgs.push_back("--export-dynamic");
linkerArgs.push_back("-m");
linkerArgs.push_back("elf_x86_64");
Expand All @@ -30,16 +32,66 @@
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 36 in src/builder/linker/linux/ld.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

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

variable 'ld_linux_path' is not initialized

void* ld_linux_handle = dlopen("ld-linux-x86-64.so.2", RTLD_LAZY);
if (!ld_linux_handle) { Syntax::E<LINKER_ERR>(FMT("Error getting library path: %s", dlerror())); }

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

implicit conversion 'void *' -> bool

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

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:39:51 [cppcoreguidelines-pro-type-vararg]

do not call c-style vararg functions

Dl_info ld_linux_info;
if (dladdr(ld_linux_handle, &ld_linux_info)) {

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

implicit conversion 'int' -> bool
ld_linux_path = ld_linux_info.dli_fname;
} else {
Syntax::E<LINKER_ERR>(FMT("Error getting library path: %s", dlerror()));

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

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/builder/linker/linux/ld.cc:45:29 [cppcoreguidelines-pro-type-vararg]

do not call c-style vararg functions
}
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;
linkerArgs.push_back(platformPath / "crt1.o");
linkerArgs.push_back(platformPath / "crti.o");

if (!dlopen("crt1.o", RTLD_LAZY)) {

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

implicit conversion 'void *' -> bool
// dlopen returns `<absolute path>/crt1.o: only ET_DYN and ET_EXEC can be loaded`
// this is abusing that fact to get the absolute path
std::string err = dlerror();
std::size_t delim = err.find(':');
if (delim != std::string::npos) {
linkerArgs.push_back(err.substr(0, delim));
} else {
Syntax::E<LINKER_ERR>(err);
}
} else {
Syntax::E<LINKER_ERR>("crt1.o was loaded as a shared library");
}

if (!dlopen("crti.o", RTLD_LAZY)) {
// dlopen returns `<absolute path>/crt1.o: only ET_DYN and ET_EXEC can be loaded`
// this is abusing that fact to get the absolute path
std::string err = dlerror();
std::size_t delim = err.find(':');
if (delim != std::string::npos) {
linkerArgs.push_back(err.substr(0, delim));
} else {
Syntax::E<LINKER_ERR>(err);
}
} else {
Syntax::E<LINKER_ERR>("crti.o was loaded as a shared library");
}

if (!isIAMCU) {
linkerArgs.push_back(platformPath / "crtn.o");
if (!dlopen("crtn.o", RTLD_LAZY)) {
// dlopen returns `<absolute path>/crt1.o: only ET_DYN and ET_EXEC can be loaded`
// this is abusing that fact to get the absolute path
std::string err = dlerror();
std::size_t delim = err.find(':');
if (delim != std::string::npos) {
linkerArgs.push_back(err.substr(0, delim));
} else {
Syntax::E<LINKER_ERR>(err);
}
} else {
Syntax::E<LINKER_ERR>("crtn.o was loaded as a shared library");
}
} else {
// TODO: add crtbegin.o and crtend.o
}
Expand Down Expand Up @@ -84,4 +136,4 @@
} // namespace linker
} // namespace snowball

#endif
#endif
Loading