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

gperftools: add a test for plain malloc() support #22667

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
9 changes: 4 additions & 5 deletions recipes/gperftools/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ project(test_package LANGUAGES C)

find_package(gperftools REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE gperftools::gperftools)

add_executable(${PROJECT_NAME}_minimal test_package.c)
target_link_libraries(${PROJECT_NAME}_minimal PRIVATE gperftools::tcmalloc_minimal)
add_executable(${PROJECT_NAME}_indirect test_package_indirect.c)
target_link_libraries(${PROJECT_NAME}_indirect PRIVATE gperftools::tcmalloc_minimal)

add_executable(${PROJECT_NAME}_direct test_package_direct.c)
target_link_libraries(${PROJECT_NAME}_direct PRIVATE gperftools::tcmalloc_minimal)
27 changes: 24 additions & 3 deletions recipes/gperftools/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from conan import ConanFile
import io

from conan import ConanFile, conan_version
from conan.tools.apple import is_apple_os
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os
Expand All @@ -19,7 +22,25 @@ def build(self):
cmake.configure()
cmake.build()

def _test(self, executable):
bin_path = os.path.join(self.cpp.build.bindir, executable)
if conan_version >= "2.0.15":
stderr = io.StringIO()
kwargs = {} # workaround for a linter false positive
kwargs.update([("stderr", stderr)])
self.run(bin_path, env="conanrun", **kwargs)
stderr = stderr.getvalue()
self.output.info(stderr)
assert "MALLOC: " in stderr, "MALLOCSTATS was not successfully enabled: " + stderr
else:
self.run(bin_path, env="conanrun")

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
os.environ["MALLOCSTATS"] = "1"
self._test("test_package_direct")
if conan_version.major == 2 and is_apple_os(self) and not self.dependencies["gperftools"].options.shared:
# FIXME
self.output.warning(f"Indirect use of malloc() on {self.settings.os} for a static build is broken and is currently skipped")
else:
self._test("test_package_indirect")
10 changes: 10 additions & 0 deletions recipes/gperftools/all/test_package/test_package_indirect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
void *p = malloc(100);
printf("%p\n", p);
free(p);
valgur marked this conversation as resolved.
Show resolved Hide resolved
return p == 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Loading