Skip to content

Commit

Permalink
feat(compilation): compilation directory depends on more cli flags (#270
Browse files Browse the repository at this point in the history
)

* feat(compilation): compilation directory depends on more cli flags

on optimization level, include dirs and the macro defines

* chore: remove `rt_ctx.output_dir`

* fix: store object files relative to project root
  • Loading branch information
Samy-33 authored Feb 21, 2025
1 parent 1cf9985 commit 5d7186c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compiler+runtime/include/cpp/jank/runtime/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace jank::runtime
/* TODO: This needs to be a dynamic var. */
native_unordered_map<native_persistent_string, native_vector<native_persistent_string>>
module_dependencies;
native_persistent_string output_dir;
native_persistent_string binary_cache_dir;
module::loader module_loader;

var_ptr current_file_var{};
Expand Down
1 change: 0 additions & 1 deletion compiler+runtime/include/cpp/jank/util/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace jank::util::cli
native_vector<native_persistent_string> libs;

/* Compilation. */
native_transient_string compilation_path{ "classes" };
native_integer optimization_level{};

/* Run command. */
Expand Down
10 changes: 8 additions & 2 deletions compiler+runtime/include/cpp/jank/util/dir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ namespace jank::util
native_persistent_string const &user_home_dir();
native_persistent_string const &user_cache_dir();
native_persistent_string const &user_config_dir();
native_persistent_string const &binary_cache_dir();
native_persistent_string const &
binary_cache_dir(native_integer const optimization_level,
native_vector<native_persistent_string> const &includes,
native_vector<native_persistent_string> const &defines);

native_persistent_string const &binary_version();
native_persistent_string const &
binary_version(native_integer const optimization_level,
native_vector<native_persistent_string> const &includes,
native_vector<native_persistent_string> const &defines);
}
8 changes: 5 additions & 3 deletions compiler+runtime/src/cpp/jank/runtime/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <llvm/TargetParser/Host.h>

#include <fmt/compile.h>
#include <regex>

#include <jank/native_persistent_string/fmt.hpp>
#include <jank/read/lex.hpp>
Expand All @@ -23,6 +22,7 @@
#include <jank/util/mapped_file.hpp>
#include <jank/util/process_location.hpp>
#include <jank/util/clang_format.hpp>
#include <jank/util/dir.hpp>
#include <jank/codegen/llvm_processor.hpp>
#include <jank/profile/time.hpp>

Expand All @@ -41,7 +41,9 @@ namespace jank::runtime

context::context(util::cli::options const &opts)
: jit_prc{ opts }
, output_dir{ opts.compilation_path }
, binary_cache_dir{ util::binary_cache_dir(opts.optimization_level,
opts.include_dirs,
opts.define_macros) }
, module_loader{ *this, opts.module_path }
{
auto const core(intern_ns(make_box<obj::symbol>("clojure.core")));
Expand Down Expand Up @@ -296,7 +298,7 @@ namespace jank::runtime
{
profile::timer const timer{ fmt::format("write_module {}", codegen_ctx->module_name) };
boost::filesystem::path const module_path{
fmt::format("{}/{}.o", output_dir, module::module_to_path(codegen_ctx->module_name))
fmt::format("{}/{}.o", binary_cache_dir, module::module_to_path(codegen_ctx->module_name))
};
boost::filesystem::create_directories(module_path.parent_path());

Expand Down
4 changes: 2 additions & 2 deletions compiler+runtime/src/cpp/jank/runtime/module/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ namespace jank::runtime::module
native_transient_string paths{ ps };
paths += fmt::format(":{}", (jank_path / "classes").string());
paths += fmt::format(":{}", (jank_path / "../src/jank").string());
paths += fmt::format(":{}", rt_ctx.output_dir);
paths += fmt::format(":{}", rt_ctx.binary_cache_dir);
this->paths = paths;

//fmt::println("module paths: {}", paths);
// fmt::println("module paths: {}", paths);

size_t start{};
size_t i{ paths.find(module_separator, start) };
Expand Down
3 changes: 0 additions & 3 deletions compiler+runtime/src/cpp/jank/util/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ namespace jank::util::cli
fmt::format(
"A {} separated list of directories, JAR files, and ZIP files to search for modules.",
runtime::module::loader::module_separator));
cli.add_option("--output-dir",
opts.compilation_path,
"The base directory where compiled modules are written.");
cli.add_flag("--profile", opts.profiler_enabled, "Enable compiler and runtime profiling.");
cli.add_option("--profile-output",
opts.profiler_file,
Expand Down
35 changes: 30 additions & 5 deletions compiler+runtime/src/cpp/jank/util/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <jank/native_persistent_string/fmt.hpp>
#include <jank/util/dir.hpp>
#include <jank/util/sha256.hpp>
#include <jank/util/string_builder.hpp>

namespace jank::util
{
Expand Down Expand Up @@ -61,15 +62,19 @@ namespace jank::util
return res;
}

native_persistent_string const &binary_cache_dir()
native_persistent_string const &
binary_cache_dir(native_integer const optimization_level,
native_vector<native_persistent_string> const &includes,
native_vector<native_persistent_string> const &defines)
{
static native_persistent_string res;
if(!res.empty())
{
return res;
}

return res = fmt::format("{}/{}", user_cache_dir(), binary_version());
return res
= fmt::format("{}/{}", "target", binary_version(optimization_level, includes, defines));
}

/* The binary version is composed of two things:
Expand All @@ -83,16 +88,36 @@ namespace jank::util
* every module. I think this is much safer than trying to reconcile ABI
* changes more granularly.
*/
native_persistent_string const &binary_version()
native_persistent_string const &
binary_version(native_integer const optimization_level,
native_vector<native_persistent_string> const &includes,
native_vector<native_persistent_string> const &defines)
{
static native_persistent_string res;
if(!res.empty())
{
return res;
}

auto const input(
fmt::format("{}.{}.{}", JANK_VERSION, clang::getClangRevision(), JANK_JIT_FLAGS));
string_builder sb;
for(auto const &inc : includes)
{
sb(inc);
}

sb(".");

for(auto const &def : defines)
{
sb(def);
}

auto const input(fmt::format("{}.{}.{}.{}.{}",
JANK_VERSION,
clang::getClangRevision(),
JANK_JIT_FLAGS,
optimization_level,
sb.release()));
res = fmt::format("{}-{}", llvm::sys::getDefaultTargetTriple(), util::sha256(input));

//fmt::println("binary_version {}", res);
Expand Down

0 comments on commit 5d7186c

Please sign in to comment.