Skip to content

Commit

Permalink
(conan-io#18681) libelfin: migrate to Conan v2
Browse files Browse the repository at this point in the history
* libelfin: migrate to Conan v2

* libelfin: restore VirtualRunEnv in test_package

* libelfin: fix v1 build

* libelfin: fix patched sources not being used

* libelfin: add legacy pkg_config names

---------

Co-authored-by: Francisco Ramírez <franchuti688@gmail.com>
  • Loading branch information
valgur and franramirez688 committed Jan 1, 2024
1 parent 39ac1ed commit 0c3c53e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 77 deletions.
51 changes: 22 additions & 29 deletions recipes/libelfin/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.12)
project(libelfin CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(PythonInterp 3 REQUIRED)

file(GLOB_RECURSE elf_sources source_subfolder/elf/*.cc)
set(src ${CMAKE_CURRENT_LIST_DIR})

file(GLOB_RECURSE elf_sources ${src}/elf/*.cc)
set(elf_headers
source_subfolder/elf/common.hh
source_subfolder/elf/data.hh
source_subfolder/elf/elf++.hh)
file(GLOB_RECURSE dwarf_sources source_subfolder/dwarf/*.cc)
${src}/elf/common.hh
${src}/elf/data.hh
${src}/elf/elf++.hh)
file(GLOB_RECURSE dwarf_sources ${src}/dwarf/*.cc)
set(dwarf_headers
source_subfolder/dwarf/data.hh
source_subfolder/dwarf/dwarf++.hh
source_subfolder/dwarf/small_vector.hh)
${src}/dwarf/data.hh
${src}/dwarf/dwarf++.hh
${src}/dwarf/small_vector.hh)

add_custom_command(
OUTPUT source_subfolder/elf/to_string.cc
OUTPUT ${src}/elf/to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// Automatically generated' > to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// DO NOT EDIT' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
Expand All @@ -34,11 +33,11 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
COMMAND ${PYTHON_EXECUTABLE} enum-print.py -u --hex --no-type --mask shf --mask pf -x loos -x hios -x loproc -x hiproc < data.hh >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'ELFPP_END_NAMESPACE' >> to_string.cc
DEPENDS source_subfolder/elf/enum-print.py source_subfolder/elf/data.hh
WORKING_DIRECTORY source_subfolder/elf)
DEPENDS ${src}/elf/enum-print.py ${src}/elf/data.hh
WORKING_DIRECTORY ${src}/elf)

add_custom_command(
OUTPUT source_subfolder/dwarf/to_string.cc
OUTPUT ${src}/dwarf/to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// Automatically generated' > to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo '// DO NOT EDIT' >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo >> to_string.cc
Expand All @@ -49,29 +48,23 @@ add_custom_command(
COMMAND ${PYTHON_EXECUTABLE} ../elf/enum-print.py < dwarf++.hh >> to_string.cc
COMMAND ${PYTHON_EXECUTABLE} ../elf/enum-print.py -s _ -u --hex -x hi_user -x lo_user < data.hh >> to_string.cc
COMMAND ${CMAKE_COMMAND} -E echo 'DWARFPP_END_NAMESPACE' >> to_string.cc
DEPENDS source_subfolder/elf/enum-print.py source_subfolder/dwarf/data.hh
WORKING_DIRECTORY source_subfolder/dwarf)
DEPENDS ${src}/elf/enum-print.py ${src}/dwarf/data.hh
WORKING_DIRECTORY ${src}/dwarf)

add_library(elf++ ${elf_sources} source_subfolder/elf/to_string.cc)
add_library(elf++ ${elf_sources} ${src}/elf/to_string.cc)
set_target_properties(elf++ PROPERTIES
PUBLIC_HEADER "${elf_headers}"
VERSION ${CONAN_PACKAGE_VERSION})
VERSION ${libelfin_VERSION})

add_library(dwarf++ ${dwarf_sources} source_subfolder/dwarf/to_string.cc)
add_library(dwarf++ ${dwarf_sources} ${src}/dwarf/to_string.cc)
set_target_properties(dwarf++ PROPERTIES
PUBLIC_HEADER "${dwarf_headers}"
VERSION ${CONAN_PACKAGE_VERSION})
VERSION ${libelfin_VERSION})

include(GNUInstallDirs)
install(TARGETS elf++
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libelfin/elf)
install(TARGETS dwarf++
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libelfin/dwarf)

install(FILES source_subfolder/LICENSE DESTINATION licenses)
install(FILES ${src}/LICENSE DESTINATION licenses)
5 changes: 2 additions & 3 deletions recipes/libelfin/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sources:
patches:
"0.3":
- patch_file: "patches/commit-9d0db16d0a0b3c4f8aaa60a3e4dab295df34b6b2.patch"
base_path: "source_subfolder"
- patch_file: "patches/const-fix.patch"
base_path: "source_subfolder"
# patch_source: https://github.com/aclements/libelfin/pull/54
patch_source: "https://github.com/aclements/libelfin/pull/54"
patch_type: "bugfix"
84 changes: 51 additions & 33 deletions recipes/libelfin/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,82 @@
import os
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get
from conan.tools.microsoft import is_msvc

required_conan_version = ">=1.53.0"


class LibelfinConan(ConanFile):
name = "libelfin"
description = "C++11 library for reading ELF binaries and DWARFv4 debug information"
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/aclements/libelfin"
license = "MIT"
topics = ("conan", "elf", "dwarf", "libelfin")
topics = ("elf", "dwarf")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}

exports_sources = "CMakeLists.txt", "patches/*"
generators = "cmake"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}

_cmake = None
_source_subfolder = "source_subfolder"
def export_sources(self):
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)
export_conandata_patches(self)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.settings.compiler == "Visual Studio":
raise ConanInvalidConfiguration("libelfin doesn't support compiler: {} on OS: {}.".
format(self.settings.compiler, self.settings.os))
if self.options.shared:
del self.options.fPIC
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, "11")
self.options.rm_safe("fPIC")

def layout(self):
cmake_layout(self, src_folder="src")

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, 11)
if is_msvc(self):
raise ConanInvalidConfiguration(f"libelfin doesn't support compiler: {self.settings.compiler}.")

def source(self):
tools.get(**self.conan_data["sources"][self.version])
extracted_dir = self.name + "-" + self.version
os.rename(extracted_dir, self._source_subfolder)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.configure()
return self._cmake
def generate(self):
tc = CMakeToolchain(self)
tc.variables["libelfin_VERSION"] = self.version
tc.generate()

def _patch_sources(self):
apply_conandata_patches(self)
copy(self, "CMakeLists.txt", src=self.export_sources_folder, dst=self.source_folder)

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
self._patch_sources()
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.install()

def package_info(self):
self.cpp_info.components["libelf++"].names["pkg_config"] = "libelf++"
self.cpp_info.components["libelf++"].set_property("pkg_config_name", "libelf++")
self.cpp_info.components["libelf++"].libs = ["elf++"]
self.cpp_info.components["libdwarf++"].names["pkg_config"] = "libdwarf++"
self.cpp_info.components["libdwarf++"].set_property("pkg_config_name", "libdwarf++")
self.cpp_info.components["libdwarf++"].libs = ["dwarf++"]
self.cpp_info.components["libdwarf++"].requires = ["libelf++"]

# TODO: Legacy, to be removed on Conan 2.0
self.cpp_info.components["libelf++"].names["pkg_config"] = "libelf++"
self.cpp_info.components["libdwarf++"].names["pkg_config"] = "libdwarf++"
7 changes: 3 additions & 4 deletions recipes/libelfin/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(libelfin REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
target_link_libraries(${PROJECT_NAME} PRIVATE libelfin::libelfin)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON)
25 changes: 17 additions & 8 deletions recipes/libelfin/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from conans import ConanFile, CMake, tools
import os

from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake


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

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

def layout(self):
cmake_layout(self)

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

def test(self):
if tools.cross_building(self.settings):
return
bin_path = os.path.join("bin", "test_package")
elf_path = os.path.join(self.source_folder, "hello")
self.run("{} {}".format(bin_path, elf_path), run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
elf_path = os.path.join(self.source_folder, "hello")
self.run(f"{bin_path} {elf_path}", env="conanrun")
8 changes: 8 additions & 0 deletions recipes/libelfin/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
19 changes: 19 additions & 0 deletions recipes/libelfin/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"

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

def test(self):
if tools.cross_building(self.settings):
return
bin_path = os.path.join("bin", "test_package")
elf_path = os.path.join(self.source_folder, os.pardir, "test_package", "hello")
self.run(f"{bin_path} {elf_path}", run_environment=True)

0 comments on commit 0c3c53e

Please sign in to comment.