Skip to content

Commit

Permalink
mimalloc: Fix package error when single_object=True (#25250)
Browse files Browse the repository at this point in the history
Co-authored-by: Uilian Ries <[email protected]>
Co-authored-by: Abril Rincón Blanco <[email protected]>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent 3ddb2da commit e4c8f28
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 277 deletions.
40 changes: 5 additions & 35 deletions recipes/mimalloc/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, copy, rm, rmdir, replace_in_file, save, collect_libs
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, copy, rm, rmdir, replace_in_file, collect_libs
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime, VCVars
from conan.tools.scm import Version
from conan.tools.env import VirtualBuildEnv
from conan.tools.scm import Version
import os
import shutil
import textwrap

required_conan_version = ">=1.53.0"
required_conan_version = ">=2"


class MimallocConan(ConanFile):
Expand Down Expand Up @@ -75,7 +74,7 @@ def layout(self):
def validate(self):
# Currently, mimalloc some version do not work properly with shared MD builds.
# https://github.com/conan-io/conan-center-index/pull/10333#issuecomment-1114110046
if self.version in ["1.7.6", "1.7.7", "2.0.6", "2.0.7"] and \
if Version(self.version) == "1.7.6" and \
self.options.shared and \
is_msvc(self) and \
not is_msvc_static_runtime(self):
Expand Down Expand Up @@ -110,8 +109,7 @@ def generate(self):
tc.variables["MI_OVERRIDE"] = "ON" if self.options.override else "OFF"
tc.variables["MI_SECURE"] = "ON" if self.options.secure else "OFF"
tc.variables["MI_WIN_REDIRECT"] = "OFF"
if Version(self.version) >= "1.7.0":
tc.variables["MI_INSTALL_TOPLEVEL"] = "ON"
tc.variables["MI_INSTALL_TOPLEVEL"] = "ON"
tc.generate()
venv = VirtualBuildEnv(self)
venv.generate(scope="build")
Expand Down Expand Up @@ -141,8 +139,6 @@ def package(self):

if self.options.get_safe("single_object"):
rm(self, "*.a", os.path.join(self.package_folder, "lib"))
shutil.move(os.path.join(self.package_folder, self._obj_name + ".o"),
os.path.join(self.package_folder, "lib"))
shutil.copy(os.path.join(self.package_folder, "lib", self._obj_name + ".o"),
os.path.join(self.package_folder, "lib", self._obj_name))

Expand All @@ -158,27 +154,6 @@ def package(self):

rmdir(self, os.path.join(self.package_folder, "share"))

cmake_target = "mimalloc" if self.options.shared else "mimalloc-static"
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_file_rel_path),
{cmake_target: "mimalloc::mimalloc"}
)

def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent(f"""\
if(TARGET {aliased} AND NOT TARGET {alias})
add_library({alias} INTERFACE IMPORTED)
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""")
save(self, module_file, content)

@property
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

@property
def _obj_name(self):
name = "mimalloc"
Expand All @@ -204,11 +179,6 @@ def package_info(self):
self.cpp_info.set_property("cmake_file_name", "mimalloc")
self.cpp_info.set_property("cmake_target_name", "mimalloc" if self.options.shared else "mimalloc-static")

self.cpp_info.names["cmake_find_package"] = "mimalloc"
self.cpp_info.names["cmake_find_package_multi"] = "mimalloc"
self.cpp_info.build_modules["cmake_find_package"] = [self._module_file_rel_path]
self.cpp_info.build_modules["cmake_find_package_multi"] = [self._module_file_rel_path]

if self.options.get_safe("inject"):
self.cpp_info.includedirs = []
self.cpp_info.libdirs = []
Expand Down
53 changes: 5 additions & 48 deletions recipes/mimalloc/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES C CXX)

enable_testing()

option(BUILD_NO_CHANGES "Build no_changes sources" ON)
option(BUILD_INCLUDE_OVERRIDE "Build include_override sources" ON)
option(BUILD_MI_API "Build mi_api sources" ON)
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES C)

find_package(mimalloc REQUIRED CONFIG)
if(TARGET mimalloc-static)
set(MIMALLOC_LIBS mimalloc-static)
else()
set(MIMALLOC_LIBS mimalloc)
endif()

if(BUILD_NO_CHANGES)
add_executable(no_changes no_changes.c)
target_link_libraries(no_changes PRIVATE ${MIMALLOC_LIBS})
target_compile_features(no_changes PRIVATE c_std_11)
add_test(NAME no_changes COMMAND no_changes)

add_executable(no_changes_cpp no_changes.cpp)
target_link_libraries(no_changes_cpp PRIVATE ${MIMALLOC_LIBS})
target_compile_features(no_changes_cpp PRIVATE cxx_std_17)
add_test(NAME no_changes_cpp COMMAND no_changes_cpp)
endif()

if(BUILD_INCLUDE_OVERRIDE)
add_executable(include_override include_override.c)
target_link_libraries(include_override PRIVATE ${MIMALLOC_LIBS})
target_compile_features(include_override PRIVATE c_std_11)
add_test(NAME include_override COMMAND include_override)

add_executable(include_override_cpp include_override.cpp)
target_link_libraries(include_override_cpp PRIVATE ${MIMALLOC_LIBS})
target_compile_features(include_override_cpp PRIVATE cxx_std_17)
add_test(NAME include_override_cpp COMMAND include_override_cpp)
endif()

if(BUILD_MI_API)
add_executable(mi_api mi_api.c)
target_link_libraries(mi_api PRIVATE ${MIMALLOC_LIBS})
target_compile_features(mi_api PRIVATE c_std_11)
add_test(NAME mi_api COMMAND mi_api)

add_executable(mi_api_cpp mi_api.cpp)
target_link_libraries(mi_api_cpp PRIVATE ${MIMALLOC_LIBS})
target_compile_features(mi_api_cpp PRIVATE cxx_std_17)
add_test(NAME mi_api_cpp COMMAND mi_api_cpp)
endif()
add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE $<IF:$<TARGET_EXISTS:mimalloc>,mimalloc,mimalloc-static>)
target_compile_features(${PROJECT_NAME} PRIVATE c_std_99)
57 changes: 5 additions & 52 deletions recipes/mimalloc/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,25 @@
from conan import ConanFile
from conan.tools.build import build_jobs, can_run
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.env import Environment
from conan.tools.files import chdir
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os
import functools


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

@functools.lru_cache(1)
def _test_files(self):
# No override:
if not self.dependencies["mimalloc"].options.override:
return ["mi_api"]
# Injected override
elif self.dependencies["mimalloc"].options.get_safe("inject"):
if self.settings.os == "Macos":
# Could not simulate Macos preload, so just ignore it
return []
return ["no_changes"]
# Non injected override
return ["include_override", "mi_api"]

@property
def _lib_name(self):
name = "mimalloc" if self.settings.os == "Windows" else "libmimalloc"
if self.settings.os == "Windows" and not self.dependencies["mimalloc"].options.shared:
name += "-static"
if self.dependencies["mimalloc"].options.secure:
name += "-secure"
if self.settings.build_type not in ("Release", "RelWithDebInfo", "MinSizeRel"):
name += f"-{str(self.settings.build_type).lower()}"
return name

def generate(self):
tc = CMakeToolchain(self)
tc.variables["BUILD_NO_CHANGES"] = "no_changes" in self._test_files()
tc.variables["BUILD_INCLUDE_OVERRIDE"] = "include_override" in self._test_files()
tc.variables["BUILD_MI_API"] = "mi_api" in self._test_files()
tc.generate()

env = Environment()
env.define("MIMALLOC_VERBOSE", "1")
if self.dependencies["mimalloc"].options.get_safe("inject"):
if self.settings.os == "Linux":
env.define("LD_PRELOAD", f"{self._lib_name}.so")
elif self.settings.os == "Macos":
env.define("DYLD_FORCE_FLAT_NAMESPACE", "1")
insert_library = os.path.join(self.dependencies["mimalloc"].cpp_info.libdirs[0], f"{self._lib_name}.dylib")
env.define("DYLD_INSERT_LIBRARIES", insert_library)
env.vars(self, scope="run").save_script("mimalloc_env_file")

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
with chdir(self, self.build_folder):
self.run(f"ctest --output-on-failure -C {self.settings.build_type} -j {build_jobs(self)}", env="conanrun")
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
11 changes: 0 additions & 11 deletions recipes/mimalloc/all/test_package/include_override.c

This file was deleted.

16 changes: 0 additions & 16 deletions recipes/mimalloc/all/test_package/include_override.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions recipes/mimalloc/all/test_package/mi_api.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions recipes/mimalloc/all/test_package/no_changes.c

This file was deleted.

8 changes: 0 additions & 8 deletions recipes/mimalloc/all/test_package/no_changes.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <stdio.h>

int main() {
void *data = mi_malloc(1024);
void *data = mi_malloc(32);

printf("mimalloc version %d\n", mi_version());
return 0;
return EXIT_SUCCESS;
}
10 changes: 0 additions & 10 deletions recipes/mimalloc/all/test_v1_package/CMakeLists.txt

This file was deleted.

70 changes: 0 additions & 70 deletions recipes/mimalloc/all/test_v1_package/conanfile.py

This file was deleted.

0 comments on commit e4c8f28

Please sign in to comment.