Skip to content

Commit

Permalink
Compute root prefix as mamba install path (#3447)
Browse files Browse the repository at this point in the history
* Compute root prefix as mamba install path

* Run test only on linux

* Test conflict between conda and mamba with same base env in CI

* Change strategy

* Use target_compile_definitions instead of add_definitions

* Add test logs

* Add ENABLE_MAMBA_ROOT_PREFIX_FALLBACK OPTION
  • Loading branch information
Hind-M authored Sep 20, 2024
1 parent 1c75567 commit ad8ca3c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/unix_impl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ jobs:
--preset mamba-unix-shared-${{ inputs.build_type }} \
-D CMAKE_CXX_COMPILER_LAUNCHER=sccache \
-D CMAKE_C_COMPILER_LAUNCHER=sccache \
-D BUILD_LIBMAMBAPY=OFF
-D BUILD_LIBMAMBAPY=OFF \
-D ENABLE_MAMBA_ROOT_PREFIX_FALLBACK=OFF
cmake --build build/ --parallel
- name: Show build cache statistics
run: sccache --show-stats
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/windows_impl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ jobs:
-D CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreadedDLL" ^
-D CMAKE_CXX_COMPILER_LAUNCHER=sccache ^
-D CMAKE_C_COMPILER_LAUNCHER=sccache ^
-D BUILD_LIBMAMBAPY=OFF
-D BUILD_LIBMAMBAPY=OFF ^
-D ENABLE_MAMBA_ROOT_PREFIX_FALLBACK=OFF
if %errorlevel% neq 0 exit /b %errorlevel%
cmake --build build/ --parallel
if %errorlevel% neq 0 exit /b %errorlevel%
Expand Down
10 changes: 10 additions & 0 deletions libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,19 @@ endmacro()

set(libmamba_targets "")

option(
ENABLE_MAMBA_ROOT_PREFIX_FALLBACK
"Enable mamba (shared) root prefix to be set to install prefix"
ON
)

if(BUILD_SHARED)
message(STATUS "Adding shared libmamba target")
libmamba_create_target(libmamba-dyn SHARED libmamba)
if(ENABLE_MAMBA_ROOT_PREFIX_FALLBACK)
# Use mamba installation prefix to set root prefix (base)
target_compile_definitions(libmamba-dyn PUBLIC MAMBA_USE_INSTALL_PREFIX_AS_BASE)
endif()
endif()

if(BUILD_STATIC)
Expand Down
24 changes: 24 additions & 0 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ namespace mamba
}
}

auto
get_root_prefix_from_mamba_bin(const fs::u8path& mamba_bin_path) -> expected_t<fs::u8path>
{
if (mamba_bin_path.empty())
{
return make_unexpected(
"`mamba` binary not found.\nPlease set `MAMBA_ROOT_PREFIX`.",
mamba_error_code::incorrect_usage
);
}
// In linux and osx, the install path would be install_prefix/bin/mamba
// In windows, install_prefix/Scripts/mamba.exe
return { fs::weakly_canonical(mamba_bin_path.parent_path().parent_path()) };
}

auto validate_existing_root_prefix(const fs::u8path& candidate) -> expected_t<fs::u8path>
{
auto prefix = fs::u8path(util::expand_home(candidate.string()));
Expand Down Expand Up @@ -724,11 +739,20 @@ namespace mamba
}
else
{
#ifdef MAMBA_USE_INSTALL_PREFIX_AS_BASE
// mamba case
// set the root prefix as the mamba installation path
get_root_prefix_from_mamba_bin(util::which("mamba"))
.transform([&](fs::u8path&& p) { prefix = std::move(p); })
.or_else([](mamba_error&& error) { throw std::move(error); });
#else
// micromamba case
validate_existing_root_prefix(default_root_prefix_v1())
.or_else([](const auto& /* error */)
{ return validate_root_prefix(default_root_prefix_v2()); })
.transform([&](fs::u8path&& p) { prefix = std::move(p); })
.or_else([](mamba_error&& error) { throw std::move(error); });
#endif
}

if (env_name.configured())
Expand Down

0 comments on commit ad8ca3c

Please sign in to comment.